pldmd: move to libpldm instance ID alloc/free
Refactor the dbus_api::Requester class to be implemented in terms of
libpldm's instance ID database. To make that easier to deal with we
introduce a light-weight RAII C++ binding along with a helper class for
unit tests.
Change-Id: Ia03de8245dfb114e6266ba36dcf26ca4398a4ce0
Signed-off-by: Rashmica Gupta <rashmica@linux.ibm.com>
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/test/meson.build b/test/meson.build
index 04bbfdc..1ae20c8 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -1,11 +1,8 @@
pldmd_inc = include_directories('../')
test_src = declare_dependency(
- sources: [
- '../pldmd/instance_id.cpp'],
include_directories:pldmd_inc)
tests = [
- 'pldmd_instanceid_test',
'pldmd_registration_test',
]
diff --git a/test/pldmd_instanceid_test.cpp b/test/pldmd_instanceid_test.cpp
deleted file mode 100644
index 4a1f9f6..0000000
--- a/test/pldmd_instanceid_test.cpp
+++ /dev/null
@@ -1,39 +0,0 @@
-#include "pldmd/instance_id.hpp"
-
-#include <stdexcept>
-
-#include <gtest/gtest.h>
-
-using namespace pldm;
-
-TEST(InstanceId, testNext)
-{
- InstanceId id;
- ASSERT_EQ(id.next(), 0);
- ASSERT_EQ(id.next(), 1);
-}
-
-TEST(InstanceId, testAllUsed)
-{
- InstanceId id;
- for (size_t i = 0; i < maxInstanceIds; ++i)
- {
- ASSERT_EQ(id.next(), i);
- }
- EXPECT_THROW(id.next(), std::runtime_error);
-}
-
-TEST(InstanceId, testMarkfree)
-{
- InstanceId id;
- for (size_t i = 0; i < maxInstanceIds; ++i)
- {
- ASSERT_EQ(id.next(), i);
- }
- id.markFree(5);
- ASSERT_EQ(id.next(), 5);
- id.markFree(0);
- ASSERT_EQ(id.next(), 0);
- EXPECT_THROW(id.next(), std::runtime_error);
- EXPECT_THROW(id.markFree(32), std::out_of_range);
-}
diff --git a/test/test_instance_id.hpp b/test/test_instance_id.hpp
new file mode 100644
index 0000000..6979e56
--- /dev/null
+++ b/test/test_instance_id.hpp
@@ -0,0 +1,38 @@
+#pragma once
+
+#include "instance_id.hpp"
+
+static constexpr uintmax_t pldmMaxInstanceIds = 32;
+
+class TestInstanceIdDb : public pldm::InstanceIdDb
+{
+ public:
+ TestInstanceIdDb() : TestInstanceIdDb(createDb()) {}
+
+ ~TestInstanceIdDb()
+ {
+ std::filesystem::remove(dbPath);
+ };
+
+ private:
+ static std::filesystem::path createDb()
+ {
+ static const char dbTmpl[] = "/tmp/db.XXXXXX";
+ char dbName[sizeof(dbTmpl)] = {};
+
+ ::strncpy(dbName, dbTmpl, sizeof(dbName));
+ ::close(::mkstemp(dbName));
+
+ std::filesystem::path dbPath(dbName);
+ std::filesystem::resize_file(
+ dbPath, static_cast<uintmax_t>(PLDM_MAX_TIDS) * pldmMaxInstanceIds);
+
+ return dbPath;
+ };
+
+ TestInstanceIdDb(std::filesystem::path dbPath) :
+ InstanceIdDb(dbPath), dbPath(dbPath)
+ {}
+
+ std::filesystem::path dbPath;
+};