blob: 8abaac7efeef5256ead98c9a3ebca056569b7f1a [file] [log] [blame]
John Wange2efdcc2020-02-12 17:02:06 +08001#include "libpldmresponder/bios_attribute.hpp"
2
3#include <nlohmann/json.hpp>
4
5#include <gtest/gtest.h>
6
7using namespace pldm::responder::bios;
8
9class TestAttribute : public BIOSAttribute
10{
11 public:
12 TestAttribute(const Json& entry, DBusHandler* const dbusHandler) :
13 BIOSAttribute(entry, dbusHandler)
George Liu6492f522020-06-16 10:34:05 +080014 {}
John Wange2efdcc2020-02-12 17:02:06 +080015
16 void setAttrValueOnDbus(const pldm_bios_attr_val_table_entry*,
17 const pldm_bios_attr_table_entry*,
18 const BIOSStringTable&) override
George Liu6492f522020-06-16 10:34:05 +080019 {}
John Wange2efdcc2020-02-12 17:02:06 +080020
21 void constructEntry(const BIOSStringTable&, Table&, Table&) override
George Liu6492f522020-06-16 10:34:05 +080022 {}
John Wange2efdcc2020-02-12 17:02:06 +080023
24 const std::optional<DBusMapping>& getDbusMap()
25 {
26 return dBusMap;
27 }
Sampa Misra46ece062020-03-18 07:17:44 -050028
29 int updateAttrVal(Table& /*newValue*/, uint16_t /*attrHdl*/,
30 uint8_t /*attrType*/,
31 const PropertyValue& /*newPropVal*/) override
32 {
33 return PLDM_SUCCESS;
34 }
George Liu1244acf2020-08-14 09:11:11 +080035
36 void generateAttributeEntry(
37 const std::variant<int64_t, std::string>& /*attributevalue*/,
38 Table& /*attrValueEntry*/)
39 {}
John Wange2efdcc2020-02-12 17:02:06 +080040};
41
42TEST(BIOSAttribute, CtorTest)
43{
44 auto jsonReadOnly = R"({
45 "attribute_name" : "ReadOnly"
46 })"_json;
47
48 TestAttribute readOnly{jsonReadOnly, nullptr};
49 EXPECT_EQ(readOnly.name, "ReadOnly");
50 EXPECT_EQ(readOnly.readOnly, true);
51
52 auto jsonReadOnlyError = R"({
53 "attribute_nam":"ReadOnly"
54 })"_json;
55 using Json = nlohmann::json;
56
57 EXPECT_THROW((TestAttribute{jsonReadOnlyError, nullptr}), Json::exception);
58
59 auto jsonReadWrite = R"({
60 "attribute_name":"ReadWrite",
61 "dbus":
62 {
63 "object_path" : "/xyz/abc/def",
64 "interface" : "xyz.openbmc.FWBoot.Side",
65 "property_name" : "Side",
66 "property_type" : "bool"
67 }
68 })"_json;
69
70 TestAttribute readWrite{jsonReadWrite, nullptr};
71 EXPECT_EQ(readWrite.name, "ReadWrite");
72 EXPECT_EQ(readWrite.readOnly, false);
73 auto dbusMap = readWrite.getDbusMap();
74 EXPECT_NE(dbusMap, std::nullopt);
75 EXPECT_EQ(dbusMap->objectPath, "/xyz/abc/def");
76 EXPECT_EQ(dbusMap->interface, "xyz.openbmc.FWBoot.Side");
77 EXPECT_EQ(dbusMap->propertyName, "Side");
78 EXPECT_EQ(dbusMap->propertyType, "bool");
79
80 auto jsonReadWriteError = R"({
81 "attribute_name":"ReadWrite",
82 "dbus":
83 {
84 "object_path" : "/xyz/abc/def",
85 "interface" : "xyz.openbmc.FWBoot.Side",
86 "property_name" : "Side"
87 }
88 })"_json; // missing property_type.
89
90 EXPECT_THROW((TestAttribute{jsonReadWriteError, nullptr}), Json::exception);
Sampa Misra46ece062020-03-18 07:17:44 -050091}