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