Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Andrew Jeffery | 2abbce7 | 2023-10-18 10:17:35 +1030 | [diff] [blame] | 3 | #include "common/instance_id.hpp" |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 4 | |
Pavithra Barithaya | be231d8 | 2024-07-31 14:04:36 +0530 | [diff] [blame] | 5 | #include <unistd.h> |
| 6 | |
Pavithra Barithaya | b3b84b4 | 2024-08-23 11:43:57 +0530 | [diff] [blame] | 7 | #include <cstring> |
Pavithra Barithaya | be231d8 | 2024-07-31 14:04:36 +0530 | [diff] [blame] | 8 | #include <filesystem> |
| 9 | |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 10 | static constexpr uintmax_t pldmMaxInstanceIds = 32; |
| 11 | |
| 12 | class 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 | }; |