blob: 305e7bec189fdc06a6299fd4563336cf404dd69d [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
Brad Bishop5079ac42021-08-19 18:35:06 -04007using namespace pldm::utils;
John Wange2efdcc2020-02-12 17:02:06 +08008using namespace pldm::responder::bios;
9
10class TestAttribute : public BIOSAttribute
11{
12 public:
13 TestAttribute(const Json& entry, DBusHandler* const dbusHandler) :
14 BIOSAttribute(entry, dbusHandler)
George Liu6492f522020-06-16 10:34:05 +080015 {}
John Wange2efdcc2020-02-12 17:02:06 +080016
17 void setAttrValueOnDbus(const pldm_bios_attr_val_table_entry*,
18 const pldm_bios_attr_table_entry*,
19 const BIOSStringTable&) override
George Liu6492f522020-06-16 10:34:05 +080020 {}
John Wange2efdcc2020-02-12 17:02:06 +080021
Tom Josephca7b2522020-11-18 12:27:11 +053022 void constructEntry(
23 const BIOSStringTable&, Table&, Table&,
24 std::optional<std::variant<int64_t, std::string>>) override
George Liu6492f522020-06-16 10:34:05 +080025 {}
John Wange2efdcc2020-02-12 17:02:06 +080026
27 const std::optional<DBusMapping>& getDbusMap()
28 {
29 return dBusMap;
30 }
Sampa Misra46ece062020-03-18 07:17:44 -050031
32 int updateAttrVal(Table& /*newValue*/, uint16_t /*attrHdl*/,
33 uint8_t /*attrType*/,
34 const PropertyValue& /*newPropVal*/) override
35 {
36 return PLDM_SUCCESS;
37 }
George Liu1244acf2020-08-14 09:11:11 +080038
39 void generateAttributeEntry(
40 const std::variant<int64_t, std::string>& /*attributevalue*/,
41 Table& /*attrValueEntry*/)
42 {}
John Wange2efdcc2020-02-12 17:02:06 +080043};
44
45TEST(BIOSAttribute, CtorTest)
46{
47 auto jsonReadOnly = R"({
George Liudaa69232020-09-02 17:22:09 +080048 "attribute_name" : "ReadOnly",
George Liu92bb4022020-09-03 14:58:24 +080049 "readOnly" : true,
50 "helpText" : "HelpText",
51 "displayName" : "DisplayName"
John Wange2efdcc2020-02-12 17:02:06 +080052 })"_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",
George Liudaa69232020-09-02 17:22:09 +080067 "readOnly" : false,
George Liu92bb4022020-09-03 14:58:24 +080068 "helpText" : "HelpText",
69 "displayName" : "DisplayName",
John Wange2efdcc2020-02-12 17:02:06 +080070 "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");
George Liu92bb4022020-09-03 14:58:24 +080088
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);
Sampa Misra46ece062020-03-18 07:17:44 -0500100}