xref: /openbmc/pldm/test/test_instance_id.hpp (revision b3b84b49)
1 #pragma once
2 
3 #include "common/instance_id.hpp"
4 
5 #include <unistd.h>
6 
7 #include <cstring>
8 #include <filesystem>
9 
10 static constexpr uintmax_t pldmMaxInstanceIds = 32;
11 
12 class TestInstanceIdDb : public pldm::InstanceIdDb
13 {
14   public:
TestInstanceIdDb()15     TestInstanceIdDb() : TestInstanceIdDb(createDb()) {}
16 
~TestInstanceIdDb()17     ~TestInstanceIdDb()
18     {
19         std::filesystem::remove(dbPath);
20     };
21 
22   private:
createDb()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 
TestInstanceIdDb(std::filesystem::path dbPath)38     TestInstanceIdDb(std::filesystem::path dbPath) :
39         InstanceIdDb(dbPath), dbPath(dbPath)
40     {}
41 
42     std::filesystem::path dbPath;
43 };
44