blob: fee0000c6c5d2507d8a48bef0efeb2b31576de64 [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 <string.h>
6#include <unistd.h>
7
8#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};