blob: 2ca9d7b870a6c976242f46c3953eed06f6767a73 [file] [log] [blame]
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +09301#pragma once
2
Andrew Jeffery2abbce72023-10-18 10:17:35 +10303#include "common/instance_id.hpp"
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +09304
Pavithra Barithayabe231d82024-07-31 14:04:36 +05305#include <unistd.h>
6
Pavithra Barithayab3b84b42024-08-23 11:43:57 +05307#include <cstring>
Pavithra Barithayabe231d82024-07-31 14:04:36 +05308#include <filesystem>
9
Andrew Jeffery7c1dc7e2023-04-28 14:52:16 +093010static constexpr uintmax_t pldmMaxInstanceIds = 32;
11
12class TestInstanceIdDb : public pldm::InstanceIdDb
13{
14 public:
15 TestInstanceIdDb() : TestInstanceIdDb(createDb()) {}
16
17 ~TestInstanceIdDb()
18 {
19 std::filesystem::remove(dbPath);
20 };
21
22 private:
23 static std::filesystem::path createDb()
24 {
25 static const char dbTmpl[] = "/tmp/db.XXXXXX";
26 char dbName[sizeof(dbTmpl)] = {};
27
28 ::strncpy(dbName, dbTmpl, sizeof(dbName));
29 ::close(::mkstemp(dbName));
30
31 std::filesystem::path dbPath(dbName);
32 std::filesystem::resize_file(
33 dbPath, static_cast<uintmax_t>(PLDM_MAX_TIDS) * pldmMaxInstanceIds);
34
35 return dbPath;
36 };
37
38 TestInstanceIdDb(std::filesystem::path dbPath) :
39 InstanceIdDb(dbPath), dbPath(dbPath)
40 {}
41
42 std::filesystem::path dbPath;
43};