xref: /openbmc/pldm/libpldmresponder/test/libpldmresponder_bios_string_attribute_test.cpp (revision 5ea723773b102e5820fb638b27175b23705c1a05)
15327988eSTom Joseph #include "common/test/mocked_utils.hpp"
25327988eSTom Joseph #include "libpldmresponder/bios_string_attribute.hpp"
35327988eSTom Joseph #include "mocked_bios.hpp"
45327988eSTom Joseph 
55327988eSTom Joseph #include <nlohmann/json.hpp>
65327988eSTom Joseph 
75327988eSTom Joseph #include <memory>
85327988eSTom Joseph 
95327988eSTom Joseph #include <gmock/gmock.h>
105327988eSTom Joseph #include <gtest/gtest.h>
115327988eSTom Joseph 
125327988eSTom Joseph using namespace pldm::responder::bios;
135079ac4aSBrad Bishop using namespace pldm::utils;
145327988eSTom Joseph using ::testing::_;
155327988eSTom Joseph using ::testing::ElementsAreArray;
165327988eSTom Joseph using ::testing::Return;
175327988eSTom Joseph using ::testing::StrEq;
185327988eSTom Joseph using ::testing::Throw;
195327988eSTom Joseph 
205327988eSTom Joseph class TestBIOSStringAttribute : public ::testing::Test
215327988eSTom Joseph {
225327988eSTom Joseph   public:
getStringInfo(const BIOSStringAttribute & biosStringAttribute)235327988eSTom Joseph     const auto& getStringInfo(const BIOSStringAttribute& biosStringAttribute)
245327988eSTom Joseph     {
255327988eSTom Joseph         return biosStringAttribute.stringInfo;
265327988eSTom Joseph     }
275327988eSTom Joseph };
285327988eSTom Joseph 
TEST_F(TestBIOSStringAttribute,CtorTest)295327988eSTom Joseph TEST_F(TestBIOSStringAttribute, CtorTest)
305327988eSTom Joseph {
315327988eSTom Joseph     auto jsonStringReadOnly = R"(  {
325327988eSTom Joseph             "attribute_name" : "str_example3",
335327988eSTom Joseph             "string_type" : "ASCII",
345327988eSTom Joseph             "minimum_string_length" : 1,
355327988eSTom Joseph             "maximum_string_length" : 100,
365327988eSTom Joseph             "default_string" : "ef",
37d6608096SArchana Kakani             "read_only" : true,
38d6608096SArchana Kakani             "help_text" : "HelpText",
39d6608096SArchana Kakani             "display_name" : "DisplayName"
405327988eSTom Joseph         })"_json;
415327988eSTom Joseph     BIOSStringAttribute stringReadOnly{jsonStringReadOnly, nullptr};
425327988eSTom Joseph     EXPECT_EQ(stringReadOnly.name, "str_example3");
435327988eSTom Joseph     EXPECT_TRUE(stringReadOnly.readOnly);
445327988eSTom Joseph 
455327988eSTom Joseph     auto& stringInfo = getStringInfo(stringReadOnly);
465327988eSTom Joseph     EXPECT_EQ(stringInfo.stringType,
475327988eSTom Joseph               static_cast<uint8_t>(BIOSStringAttribute::Encoding::ASCII));
485327988eSTom Joseph     EXPECT_EQ(stringInfo.minLength, 1);
495327988eSTom Joseph     EXPECT_EQ(stringInfo.maxLength, 100);
505327988eSTom Joseph     EXPECT_EQ(stringInfo.defLength, 2);
515327988eSTom Joseph     EXPECT_EQ(stringInfo.defString, "ef");
525327988eSTom Joseph 
535327988eSTom Joseph     auto jsonStringReadWrite = R"({
545327988eSTom Joseph             "attribute_name" : "str_example1",
555327988eSTom Joseph             "string_type" : "ASCII",
565327988eSTom Joseph             "minimum_string_length" : 1,
575327988eSTom Joseph             "maximum_string_length" : 100,
585327988eSTom Joseph             "default_string" : "abc",
59d6608096SArchana Kakani             "read_only" : false,
60d6608096SArchana Kakani             "help_text" : "HelpText",
61d6608096SArchana Kakani             "display_name" : "DisplayName",
625327988eSTom Joseph             "dbus" : {
635327988eSTom Joseph                 "object_path" : "/xyz/abc/def",
645327988eSTom Joseph                 "interface" : "xyz.openbmc_project.str_example1.value",
655327988eSTom Joseph                 "property_name" : "Str_example1",
665327988eSTom Joseph                 "property_type" : "string"
675327988eSTom Joseph             }
685327988eSTom Joseph         })"_json;
695327988eSTom Joseph     BIOSStringAttribute stringReadWrite{jsonStringReadWrite, nullptr};
705327988eSTom Joseph 
715327988eSTom Joseph     EXPECT_EQ(stringReadWrite.name, "str_example1");
725327988eSTom Joseph     EXPECT_TRUE(!stringReadWrite.readOnly);
735327988eSTom Joseph }
745327988eSTom Joseph 
TEST_F(TestBIOSStringAttribute,ConstructEntry)755327988eSTom Joseph TEST_F(TestBIOSStringAttribute, ConstructEntry)
765327988eSTom Joseph {
775327988eSTom Joseph     MockBIOSStringTable biosStringTable;
785327988eSTom Joseph     MockdBusHandler dbusHandler;
795327988eSTom Joseph 
805327988eSTom Joseph     auto jsonStringReadOnly = R"({
815327988eSTom Joseph             "attribute_name" : "str_example1",
825327988eSTom Joseph             "string_type" : "ASCII",
835327988eSTom Joseph             "minimum_string_length" : 1,
845327988eSTom Joseph             "maximum_string_length" : 100,
855327988eSTom Joseph             "default_string" : "abc",
86d6608096SArchana Kakani             "read_only" : true,
87d6608096SArchana Kakani             "help_text" : "HelpText",
88d6608096SArchana Kakani             "display_name" : "DisplayName"
895327988eSTom Joseph         })"_json;
905327988eSTom Joseph 
915327988eSTom Joseph     std::vector<uint8_t> expectedAttrEntry{
925327988eSTom Joseph         0,    0,       /* attr handle */
935327988eSTom Joseph         0x81,          /* attr type string read-only */
945327988eSTom Joseph         5,    0,       /* attr name handle */
955327988eSTom Joseph         1,             /* string type */
965327988eSTom Joseph         1,    0,       /* minimum length of the string in bytes */
975327988eSTom Joseph         100,  0,       /* maximum length of the string in bytes */
985327988eSTom Joseph         3,    0,       /* length of default string in length */
995327988eSTom Joseph         'a',  'b', 'c' /* default string  */
1005327988eSTom Joseph     };
1015327988eSTom Joseph 
1025327988eSTom Joseph     std::vector<uint8_t> expectedAttrValueEntry{
1035327988eSTom Joseph         0,    0,        /* attr handle */
1045327988eSTom Joseph         0x81,           /* attr type string read-only */
1055327988eSTom Joseph         3,    0,        /* current string length */
1062576aecdSManojkiran Eda         'a',  'b', 'c', /* default value string handle index */
1075327988eSTom Joseph     };
1085327988eSTom Joseph 
1095327988eSTom Joseph     ON_CALL(biosStringTable, findHandle(StrEq("str_example1")))
1105327988eSTom Joseph         .WillByDefault(Return(5));
1115327988eSTom Joseph     BIOSStringAttribute stringReadOnly{jsonStringReadOnly, nullptr};
1125327988eSTom Joseph 
1135327988eSTom Joseph     checkConstructEntry(stringReadOnly, biosStringTable, expectedAttrEntry,
1145327988eSTom Joseph                         expectedAttrValueEntry);
1155327988eSTom Joseph 
1165327988eSTom Joseph     auto jsonStringReadWrite = R"({
1175327988eSTom Joseph             "attribute_name" : "str_example1",
1185327988eSTom Joseph             "string_type" : "ASCII",
1195327988eSTom Joseph             "minimum_string_length" : 1,
1205327988eSTom Joseph             "maximum_string_length" : 100,
1215327988eSTom Joseph             "default_string" : "abc",
122d6608096SArchana Kakani             "read_only" : false,
123d6608096SArchana Kakani             "help_text" : "HelpText",
124d6608096SArchana Kakani             "display_name" : "DisplayName",
1255327988eSTom Joseph             "dbus" : {
1265327988eSTom Joseph                 "object_path" : "/xyz/abc/def",
1275327988eSTom Joseph                 "interface" : "xyz.openbmc_project.str_example1.value",
1285327988eSTom Joseph                 "property_name" : "Str_example1",
1295327988eSTom Joseph                 "property_type" : "string"
1305327988eSTom Joseph             }
1315327988eSTom Joseph         })"_json;
1325327988eSTom Joseph     BIOSStringAttribute stringReadWrite{jsonStringReadWrite, &dbusHandler};
1335327988eSTom Joseph 
1345327988eSTom Joseph     /* Set expected attr type to read-write */
1355327988eSTom Joseph     expectedAttrEntry[2] = PLDM_BIOS_STRING;
1365327988eSTom Joseph     expectedAttrValueEntry[2] = PLDM_BIOS_STRING;
1375327988eSTom Joseph 
1385327988eSTom Joseph     EXPECT_CALL(
1395327988eSTom Joseph         dbusHandler,
1405327988eSTom Joseph         getDbusPropertyVariant(StrEq("/xyz/abc/def"), StrEq("Str_example1"),
1415327988eSTom Joseph                                StrEq("xyz.openbmc_project.str_example1.value")))
1425327988eSTom Joseph         .WillOnce(Throw(std::exception()));
1435327988eSTom Joseph 
1445327988eSTom Joseph     checkConstructEntry(stringReadWrite, biosStringTable, expectedAttrEntry,
1455327988eSTom Joseph                         expectedAttrValueEntry);
1465327988eSTom Joseph 
1475327988eSTom Joseph     EXPECT_CALL(
1485327988eSTom Joseph         dbusHandler,
1495327988eSTom Joseph         getDbusPropertyVariant(StrEq("/xyz/abc/def"), StrEq("Str_example1"),
1505327988eSTom Joseph                                StrEq("xyz.openbmc_project.str_example1.value")))
1515327988eSTom Joseph         .WillOnce(Return(PropertyValue(std::string("abcd"))));
1525327988eSTom Joseph 
1535327988eSTom Joseph     expectedAttrValueEntry = {
1545327988eSTom Joseph         0,   0,             /* attr handle */
1555327988eSTom Joseph         1,                  /* attr type string read-write */
1565327988eSTom Joseph         4,   0,             /* current string length */
1572576aecdSManojkiran Eda         'a', 'b', 'c', 'd', /* default value string handle index */
1585327988eSTom Joseph     };
1595327988eSTom Joseph 
1605327988eSTom Joseph     checkConstructEntry(stringReadWrite, biosStringTable, expectedAttrEntry,
1615327988eSTom Joseph                         expectedAttrValueEntry);
1625327988eSTom Joseph }
1635327988eSTom Joseph 
TEST_F(TestBIOSStringAttribute,setAttrValueOnDbus)1645327988eSTom Joseph TEST_F(TestBIOSStringAttribute, setAttrValueOnDbus)
1655327988eSTom Joseph {
1665327988eSTom Joseph     auto jsonStringReadWrite = R"({
1675327988eSTom Joseph             "attribute_name" : "str_example1",
1685327988eSTom Joseph             "string_type" : "ASCII",
1695327988eSTom Joseph             "minimum_string_length" : 1,
1705327988eSTom Joseph             "maximum_string_length" : 100,
1715327988eSTom Joseph             "default_string" : "abc",
172d6608096SArchana Kakani             "read_only" : false,
173d6608096SArchana Kakani             "help_text" : "HelpText",
174d6608096SArchana Kakani             "display_name" : "DisplayName",
1755327988eSTom Joseph             "dbus" : {
1765327988eSTom Joseph                 "object_path" : "/xyz/abc/def",
1775327988eSTom Joseph                 "interface" : "xyz.openbmc_project.str_example1.value",
1785327988eSTom Joseph                 "property_name" : "Str_example1",
1795327988eSTom Joseph                 "property_type" : "string"
1805327988eSTom Joseph             }
1815327988eSTom Joseph         })"_json;
1825327988eSTom Joseph 
1835327988eSTom Joseph     MockdBusHandler dbusHandler;
1845327988eSTom Joseph     MockBIOSStringTable biosStringTable;
1855327988eSTom Joseph 
1865327988eSTom Joseph     BIOSStringAttribute stringReadWrite{jsonStringReadWrite, &dbusHandler};
1875327988eSTom Joseph     DBusMapping dbusMapping{"/xyz/abc/def",
1885327988eSTom Joseph                             "xyz.openbmc_project.str_example1.value",
1895327988eSTom Joseph                             "Str_example1", "string"};
1905327988eSTom Joseph     std::vector<uint8_t> attrValueEntry{
1915327988eSTom Joseph         0,   0,             /* attr handle */
1925327988eSTom Joseph         1,                  /* attr type string read-write */
1935327988eSTom Joseph         4,   0,             /* current string length */
1942576aecdSManojkiran Eda         'a', 'b', 'c', 'd', /* default value string handle index */
1955327988eSTom Joseph     };
196*5ea72377SPavithra Barithaya     auto entry = new (attrValueEntry.data()) pldm_bios_attr_val_table_entry;
1975327988eSTom Joseph     PropertyValue value = std::string("abcd");
1985327988eSTom Joseph     EXPECT_CALL(dbusHandler, setDbusProperty(dbusMapping, value)).Times(1);
1995327988eSTom Joseph     stringReadWrite.setAttrValueOnDbus(entry, nullptr, biosStringTable);
2005327988eSTom Joseph }
201