1 #include "libpldmresponder/bios_attribute.hpp"
2 
3 #include <nlohmann/json.hpp>
4 
5 #include <gtest/gtest.h>
6 
7 using namespace pldm::utils;
8 using namespace pldm::responder::bios;
9 
10 class TestAttribute : public BIOSAttribute
11 {
12   public:
13     TestAttribute(const Json& entry, DBusHandler* const dbusHandler) :
14         BIOSAttribute(entry, dbusHandler)
15     {}
16 
17     void setAttrValueOnDbus(const pldm_bios_attr_val_table_entry*,
18                             const pldm_bios_attr_table_entry*,
19                             const BIOSStringTable&) override
20     {}
21 
22     void constructEntry(
23         const BIOSStringTable&, Table&, Table&,
24         std::optional<std::variant<int64_t, std::string>>) override
25     {}
26 
27     const std::optional<DBusMapping>& getDbusMap()
28     {
29         return dBusMap;
30     }
31 
32     int updateAttrVal(Table& /*newValue*/, uint16_t /*attrHdl*/,
33                       uint8_t /*attrType*/,
34                       const PropertyValue& /*newPropVal*/) override
35     {
36         return PLDM_SUCCESS;
37     }
38 
39     void generateAttributeEntry(
40         const std::variant<int64_t, std::string>& /*attributevalue*/,
41         Table& /*attrValueEntry*/) override
42     {}
43 };
44 
45 TEST(BIOSAttribute, CtorTest)
46 {
47     auto jsonReadOnly = R"({
48       "attribute_name" : "ReadOnly",
49       "read_only" : true,
50       "help_text" : "HelpText",
51       "display_name" : "DisplayName"
52     })"_json;
53 
54     TestAttribute readOnly{jsonReadOnly, nullptr};
55     EXPECT_EQ(readOnly.name, "ReadOnly");
56     EXPECT_EQ(readOnly.readOnly, true);
57 
58     auto jsonReadOnlyError = R"({
59       "attribute_nam":"ReadOnly"
60     })"_json;
61     using Json = nlohmann::json;
62 
63     EXPECT_THROW((TestAttribute{jsonReadOnlyError, nullptr}), Json::exception);
64 
65     auto jsonReadWrite = R"({
66       "attribute_name":"ReadWrite",
67       "read_only" : false,
68       "help_text" : "HelpText",
69       "display_name" : "DisplayName",
70       "dbus":
71            {
72                "object_path" : "/xyz/abc/def",
73                "interface" : "xyz.openbmc.FWBoot.Side",
74                "property_name" : "Side",
75                "property_type" : "bool"
76            }
77     })"_json;
78 
79     TestAttribute readWrite{jsonReadWrite, nullptr};
80     EXPECT_EQ(readWrite.name, "ReadWrite");
81     EXPECT_EQ(readWrite.readOnly, false);
82     auto dbusMap = readWrite.getDbusMap();
83     EXPECT_NE(dbusMap, std::nullopt);
84     EXPECT_EQ(dbusMap->objectPath, "/xyz/abc/def");
85     EXPECT_EQ(dbusMap->interface, "xyz.openbmc.FWBoot.Side");
86     EXPECT_EQ(dbusMap->propertyName, "Side");
87     EXPECT_EQ(dbusMap->propertyType, "bool");
88 
89     auto jsonReadWriteError = R"({
90       "attribute_name":"ReadWrite",
91       "dbus":
92            {
93                "object_path" : "/xyz/abc/def",
94                "interface" : "xyz.openbmc.FWBoot.Side",
95                "property_name" : "Side"
96            }
97     })"_json; // missing property_type.
98 
99     EXPECT_THROW((TestAttribute{jsonReadWriteError, nullptr}), Json::exception);
100 }
101