blob: 27d246a34ddc2a2b6a48a4e6cf3879c0197713bb [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)
14 {
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
23 void constructEntry(const BIOSStringTable&, Table&, Table&) override
24 {
25 }
26
27 const std::optional<DBusMapping>& getDbusMap()
28 {
29 return dBusMap;
30 }
31};
32
33TEST(BIOSAttribute, CtorTest)
34{
35 auto jsonReadOnly = R"({
36 "attribute_name" : "ReadOnly"
37 })"_json;
38
39 TestAttribute readOnly{jsonReadOnly, nullptr};
40 EXPECT_EQ(readOnly.name, "ReadOnly");
41 EXPECT_EQ(readOnly.readOnly, true);
42
43 auto jsonReadOnlyError = R"({
44 "attribute_nam":"ReadOnly"
45 })"_json;
46 using Json = nlohmann::json;
47
48 EXPECT_THROW((TestAttribute{jsonReadOnlyError, nullptr}), Json::exception);
49
50 auto jsonReadWrite = R"({
51 "attribute_name":"ReadWrite",
52 "dbus":
53 {
54 "object_path" : "/xyz/abc/def",
55 "interface" : "xyz.openbmc.FWBoot.Side",
56 "property_name" : "Side",
57 "property_type" : "bool"
58 }
59 })"_json;
60
61 TestAttribute readWrite{jsonReadWrite, nullptr};
62 EXPECT_EQ(readWrite.name, "ReadWrite");
63 EXPECT_EQ(readWrite.readOnly, false);
64 auto dbusMap = readWrite.getDbusMap();
65 EXPECT_NE(dbusMap, std::nullopt);
66 EXPECT_EQ(dbusMap->objectPath, "/xyz/abc/def");
67 EXPECT_EQ(dbusMap->interface, "xyz.openbmc.FWBoot.Side");
68 EXPECT_EQ(dbusMap->propertyName, "Side");
69 EXPECT_EQ(dbusMap->propertyType, "bool");
70
71 auto jsonReadWriteError = R"({
72 "attribute_name":"ReadWrite",
73 "dbus":
74 {
75 "object_path" : "/xyz/abc/def",
76 "interface" : "xyz.openbmc.FWBoot.Side",
77 "property_name" : "Side"
78 }
79 })"_json; // missing property_type.
80
81 EXPECT_THROW((TestAttribute{jsonReadWriteError, nullptr}), Json::exception);
82}