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