1 #include "libpldmresponder/bios_table.hpp"
2 
3 #include <stdlib.h>
4 
5 #include <algorithm>
6 #include <vector>
7 
8 #include <gtest/gtest.h>
9 
10 using namespace pldm::responder::bios;
11 
12 class TestBIOSTable : public testing::Test
13 {
14   public:
SetUp()15     void SetUp() override
16     {
17         char tmpdir[] = "/tmp/pldm_bios_table.XXXXXX";
18         dir = fs::path(mkdtemp(tmpdir));
19     }
20 
TearDown()21     void TearDown() override
22     {
23         fs::remove_all(dir);
24     }
25 
26     fs::path dir;
27 };
28 
TEST_F(TestBIOSTable,testStoreLoad)29 TEST_F(TestBIOSTable, testStoreLoad)
30 {
31     std::vector<uint8_t> table{10, 34, 56, 100, 44, 55, 69, 21, 48, 2, 7, 82};
32     fs::path file(dir / "t1");
33     BIOSTable t(file.string().c_str());
34     std::vector<uint8_t> out{};
35 
36     ASSERT_THROW(t.load(out), fs::filesystem_error);
37 
38     ASSERT_EQ(true, t.isEmpty());
39 
40     t.store(table);
41     t.load(out);
42     ASSERT_EQ(true, std::equal(table.begin(), table.end(), out.begin()));
43 }
44 
TEST_F(TestBIOSTable,testLoadOntoExisting)45 TEST_F(TestBIOSTable, testLoadOntoExisting)
46 {
47     std::vector<uint8_t> table{10, 34, 56, 100, 44, 55, 69, 21, 48, 2, 7, 82};
48     fs::path file(dir / "t1");
49     BIOSTable t(file.string().c_str());
50     std::vector<uint8_t> out{99, 99};
51 
52     ASSERT_THROW(t.load(out), fs::filesystem_error);
53 
54     ASSERT_EQ(true, t.isEmpty());
55 
56     t.store(table);
57     t.load(out);
58     ASSERT_EQ(true, std::equal(table.begin(), table.end(), out.begin() + 2));
59     ASSERT_EQ(out[0], 99);
60     ASSERT_EQ(out[1], 99);
61 }
62