15327988eSTom Joseph #include "common/test/mocked_utils.hpp"
25327988eSTom Joseph #include "libpldmresponder/bios_integer_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 
125079ac4aSBrad Bishop using namespace pldm::utils;
135079ac4aSBrad Bishop using namespace pldm::responder::bios;
145079ac4aSBrad Bishop 
155327988eSTom Joseph using ::testing::_;
165327988eSTom Joseph using ::testing::ElementsAreArray;
175327988eSTom Joseph using ::testing::Return;
185327988eSTom Joseph using ::testing::StrEq;
195327988eSTom Joseph using ::testing::Throw;
205327988eSTom Joseph 
215327988eSTom Joseph class TestBIOSIntegerAttribute : public ::testing::Test
225327988eSTom Joseph {
235327988eSTom Joseph   public:
getIntegerInfo(const BIOSIntegerAttribute & attribute)245327988eSTom Joseph     const auto& getIntegerInfo(const BIOSIntegerAttribute& attribute)
255327988eSTom Joseph     {
265327988eSTom Joseph         return attribute.integerInfo;
275327988eSTom Joseph     }
285327988eSTom Joseph };
295327988eSTom Joseph 
TEST_F(TestBIOSIntegerAttribute,CtorTest)305327988eSTom Joseph TEST_F(TestBIOSIntegerAttribute, CtorTest)
315327988eSTom Joseph {
325327988eSTom Joseph     auto jsonIntegerReadOnly = R"({
335327988eSTom Joseph          "attribute_name" : "SBE_IMAGE_MINIMUM_VALID_ECS",
345327988eSTom Joseph          "lower_bound" : 1,
355327988eSTom Joseph          "upper_bound" : 15,
365327988eSTom Joseph          "scalar_increment" : 1,
375327988eSTom Joseph          "default_value" : 2,
38d6608096SArchana Kakani          "read_only" : true,
39d6608096SArchana Kakani          "help_text" : "HelpText",
40d6608096SArchana Kakani          "display_name" : "DisplayName"
415327988eSTom Joseph       })"_json;
425327988eSTom Joseph 
435327988eSTom Joseph     BIOSIntegerAttribute integerReadOnly{jsonIntegerReadOnly, nullptr};
445327988eSTom Joseph     EXPECT_EQ(integerReadOnly.name, "SBE_IMAGE_MINIMUM_VALID_ECS");
455327988eSTom Joseph     EXPECT_TRUE(integerReadOnly.readOnly);
465327988eSTom Joseph     auto& integerInfo = getIntegerInfo(integerReadOnly);
475327988eSTom Joseph     EXPECT_EQ(integerInfo.lowerBound, 1);
485327988eSTom Joseph     EXPECT_EQ(integerInfo.upperBound, 15);
495327988eSTom Joseph     EXPECT_EQ(integerInfo.scalarIncrement, 1);
505327988eSTom Joseph     EXPECT_EQ(integerInfo.defaultValue, 2);
515327988eSTom Joseph 
525327988eSTom Joseph     auto jsonIntegerReadOnlyError = R"({
535327988eSTom Joseph          "attribute_name" : "SBE_IMAGE_MINIMUM_VALID_ECS",
545327988eSTom Joseph          "lower_bound" : 1,
555327988eSTom Joseph          "upper_bound" : 15,
565327988eSTom Joseph          "scalar_increment" : 1,
575327988eSTom Joseph          "default_valu" : 2,
58d6608096SArchana Kakani          "read_only" : true,
59d6608096SArchana Kakani          "help_text" : "HelpText",
60d6608096SArchana Kakani          "display_name" : "DisplayName"
615327988eSTom Joseph       })"_json; // default_valu -> default_value
625327988eSTom Joseph     EXPECT_THROW((BIOSIntegerAttribute{jsonIntegerReadOnlyError, nullptr}),
635327988eSTom Joseph                  Json::exception);
645327988eSTom Joseph 
655327988eSTom Joseph     auto jsonIntegerReadWrite = R"({
665327988eSTom Joseph          "attribute_name" : "VDD_AVSBUS_RAIL",
675327988eSTom Joseph          "lower_bound" : 0,
685327988eSTom Joseph          "upper_bound" : 15,
695327988eSTom Joseph          "scalar_increment" : 1,
705327988eSTom Joseph          "default_value" : 0,
71d6608096SArchana Kakani          "read_only" : false,
72d6608096SArchana Kakani          "help_text" : "HelpText",
73d6608096SArchana Kakani          "display_name" : "DisplayName",
745327988eSTom Joseph          "dbus":{
755327988eSTom Joseph             "object_path" : "/xyz/openbmc_project/avsbus",
765327988eSTom Joseph             "interface" : "xyz.openbmc.AvsBus.Manager",
775327988eSTom Joseph             "property_type" : "uint8_t",
785327988eSTom Joseph             "property_name" : "Rail"
795327988eSTom Joseph          }
805327988eSTom Joseph       })"_json;
815327988eSTom Joseph 
825327988eSTom Joseph     BIOSIntegerAttribute integerReadWrite{jsonIntegerReadWrite, nullptr};
835327988eSTom Joseph     EXPECT_EQ(integerReadWrite.name, "VDD_AVSBUS_RAIL");
845327988eSTom Joseph     EXPECT_TRUE(!integerReadWrite.readOnly);
855327988eSTom Joseph }
865327988eSTom Joseph 
TEST_F(TestBIOSIntegerAttribute,ConstructEntry)875327988eSTom Joseph TEST_F(TestBIOSIntegerAttribute, ConstructEntry)
885327988eSTom Joseph {
895327988eSTom Joseph     MockBIOSStringTable biosStringTable;
905327988eSTom Joseph     MockdBusHandler dbusHandler;
915327988eSTom Joseph 
925327988eSTom Joseph     auto jsonIntegerReadOnly = R"({
935327988eSTom Joseph          "attribute_name" : "VDD_AVSBUS_RAIL",
945327988eSTom Joseph          "lower_bound" : 1,
955327988eSTom Joseph          "upper_bound" : 15,
965327988eSTom Joseph          "scalar_increment" : 1,
975327988eSTom Joseph          "default_value" : 2,
98d6608096SArchana Kakani          "read_only" : true,
99d6608096SArchana Kakani          "help_text" : "HelpText",
100d6608096SArchana Kakani          "display_name" : "DisplayName"
1015327988eSTom Joseph       })"_json;
1025327988eSTom Joseph 
1035327988eSTom Joseph     std::vector<uint8_t> expectedAttrEntry{
1045327988eSTom Joseph         0,    0,                   /* attr handle */
1055327988eSTom Joseph         0x83,                      /* attr type integer read-only*/
1065327988eSTom Joseph         5,    0,                   /* attr name handle */
1075327988eSTom Joseph         1,    0, 0, 0, 0, 0, 0, 0, /* lower bound */
1085327988eSTom Joseph         15,   0, 0, 0, 0, 0, 0, 0, /* upper bound */
1095327988eSTom Joseph         1,    0, 0, 0,             /* scalar increment */
110*2576aecdSManojkiran Eda         2,    0, 0, 0, 0, 0, 0, 0, /* default value */
1115327988eSTom Joseph     };
1125327988eSTom Joseph     std::vector<uint8_t> expectedAttrValueEntry{
1135327988eSTom Joseph         0,    0,                   /* attr handle */
1145327988eSTom Joseph         0x83,                      /* attr type integer read-only*/
1155327988eSTom Joseph         2,    0, 0, 0, 0, 0, 0, 0, /* current value */
1165327988eSTom Joseph     };
1175327988eSTom Joseph 
1185327988eSTom Joseph     BIOSIntegerAttribute integerReadOnly{jsonIntegerReadOnly, nullptr};
1195327988eSTom Joseph 
1205327988eSTom Joseph     ON_CALL(biosStringTable, findHandle(StrEq("VDD_AVSBUS_RAIL")))
1215327988eSTom Joseph         .WillByDefault(Return(5));
1225327988eSTom Joseph 
1235327988eSTom Joseph     checkConstructEntry(integerReadOnly, biosStringTable, expectedAttrEntry,
1245327988eSTom Joseph                         expectedAttrValueEntry);
1255327988eSTom Joseph 
1265327988eSTom Joseph     auto jsonIntegerReadWrite = R"({
1275327988eSTom Joseph          "attribute_name" : "VDD_AVSBUS_RAIL",
1285327988eSTom Joseph          "lower_bound" : 1,
1295327988eSTom Joseph          "upper_bound" : 15,
1305327988eSTom Joseph          "scalar_increment" : 1,
1315327988eSTom Joseph          "default_value" : 2,
132d6608096SArchana Kakani          "read_only" : false,
133d6608096SArchana Kakani          "help_text" : "HelpText",
134d6608096SArchana Kakani          "display_name" : "DisplayName",
1355327988eSTom Joseph          "dbus":{
1365327988eSTom Joseph             "object_path" : "/xyz/openbmc_project/avsbus",
1375327988eSTom Joseph             "interface" : "xyz.openbmc.AvsBus.Manager",
1385327988eSTom Joseph             "property_type" : "uint8_t",
1395327988eSTom Joseph             "property_name" : "Rail"
1405327988eSTom Joseph          }
1415327988eSTom Joseph       })"_json;
1425327988eSTom Joseph     BIOSIntegerAttribute integerReadWrite{jsonIntegerReadWrite, &dbusHandler};
1435327988eSTom Joseph 
1445327988eSTom Joseph     EXPECT_CALL(dbusHandler,
1455327988eSTom Joseph                 getDbusPropertyVariant(StrEq("/xyz/openbmc_project/avsbus"),
1465327988eSTom Joseph                                        StrEq("Rail"),
1475327988eSTom Joseph                                        StrEq("xyz.openbmc.AvsBus.Manager")))
1485327988eSTom Joseph         .WillOnce(Throw(std::exception()));
1495327988eSTom Joseph 
1505327988eSTom Joseph     /* Set expected attr type to read-write */
1515327988eSTom Joseph     expectedAttrEntry[2] = PLDM_BIOS_INTEGER;
1525327988eSTom Joseph     expectedAttrValueEntry[2] = PLDM_BIOS_INTEGER;
1535327988eSTom Joseph 
1545327988eSTom Joseph     checkConstructEntry(integerReadWrite, biosStringTable, expectedAttrEntry,
1555327988eSTom Joseph                         expectedAttrValueEntry);
1565327988eSTom Joseph 
1575327988eSTom Joseph     EXPECT_CALL(dbusHandler,
1585327988eSTom Joseph                 getDbusPropertyVariant(StrEq("/xyz/openbmc_project/avsbus"),
1595327988eSTom Joseph                                        StrEq("Rail"),
1605327988eSTom Joseph                                        StrEq("xyz.openbmc.AvsBus.Manager")))
1615327988eSTom Joseph         .WillOnce(Return(PropertyValue(uint8_t(7))));
1625327988eSTom Joseph 
1635327988eSTom Joseph     expectedAttrValueEntry = {
1645327988eSTom Joseph         0, 0,                   /* attr handle */
1655327988eSTom Joseph         3,                      /* attr type integer read-write*/
1665327988eSTom Joseph         7, 0, 0, 0, 0, 0, 0, 0, /* current value */
1675327988eSTom Joseph     };
1685327988eSTom Joseph 
1695327988eSTom Joseph     checkConstructEntry(integerReadWrite, biosStringTable, expectedAttrEntry,
1705327988eSTom Joseph                         expectedAttrValueEntry);
1715327988eSTom Joseph }
1725327988eSTom Joseph 
TEST_F(TestBIOSIntegerAttribute,setAttrValueOnDbus)1735327988eSTom Joseph TEST_F(TestBIOSIntegerAttribute, setAttrValueOnDbus)
1745327988eSTom Joseph {
1755327988eSTom Joseph     MockdBusHandler dbusHandler;
1765327988eSTom Joseph     MockBIOSStringTable biosStringTable;
1775327988eSTom Joseph 
1785327988eSTom Joseph     auto jsonIntegerReadWrite = R"({
1795327988eSTom Joseph          "attribute_name" : "VDD_AVSBUS_RAIL",
1805327988eSTom Joseph          "lower_bound" : 1,
1815327988eSTom Joseph          "upper_bound" : 15,
1825327988eSTom Joseph          "scalar_increment" : 1,
1835327988eSTom Joseph          "default_value" : 2,
184d6608096SArchana Kakani          "read_only" : false,
185d6608096SArchana Kakani          "help_text" : "HelpText",
186d6608096SArchana Kakani          "display_name" : "DisplayName",
1875327988eSTom Joseph          "dbus":{
1885327988eSTom Joseph             "object_path" : "/xyz/openbmc_project/avsbus",
1895327988eSTom Joseph             "interface" : "xyz.openbmc.AvsBus.Manager",
1905327988eSTom Joseph             "property_type" : "uint8_t",
1915327988eSTom Joseph             "property_name" : "Rail"
1925327988eSTom Joseph          }
1935327988eSTom Joseph       })"_json;
1945327988eSTom Joseph     BIOSIntegerAttribute integerReadWrite{jsonIntegerReadWrite, &dbusHandler};
1955327988eSTom Joseph     DBusMapping dbusMapping{"/xyz/openbmc_project/avsbus",
1965327988eSTom Joseph                             "xyz.openbmc.AvsBus.Manager", "Rail", "uint8_t"};
1975327988eSTom Joseph     std::vector<uint8_t> attrValueEntry = {
1985327988eSTom Joseph         0, 0,                   /* attr handle */
1995327988eSTom Joseph         3,                      /* attr type integer read-write*/
2005327988eSTom Joseph         7, 0, 0, 0, 0, 0, 0, 0, /* current value */
2015327988eSTom Joseph     };
2025327988eSTom Joseph 
2035327988eSTom Joseph     auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>(
2045327988eSTom Joseph         attrValueEntry.data());
2055327988eSTom Joseph     EXPECT_CALL(dbusHandler,
2065327988eSTom Joseph                 setDbusProperty(dbusMapping, PropertyValue{uint8_t(7)}))
2075327988eSTom Joseph         .Times(1);
2085327988eSTom Joseph     integerReadWrite.setAttrValueOnDbus(entry, nullptr, biosStringTable);
2095327988eSTom Joseph }
210