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 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 7 | using namespace pldm::utils; |
John Wang | e2efdcc | 2020-02-12 17:02:06 +0800 | [diff] [blame] | 8 | using namespace pldm::responder::bios; |
| 9 | |
| 10 | class TestAttribute : public BIOSAttribute |
| 11 | { |
| 12 | public: |
| 13 | TestAttribute(const Json& entry, DBusHandler* const dbusHandler) : |
| 14 | BIOSAttribute(entry, dbusHandler) |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 15 | {} |
John Wang | e2efdcc | 2020-02-12 17:02:06 +0800 | [diff] [blame] | 16 | |
| 17 | void setAttrValueOnDbus(const pldm_bios_attr_val_table_entry*, |
| 18 | const pldm_bios_attr_table_entry*, |
| 19 | const BIOSStringTable&) override |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 20 | {} |
John Wang | e2efdcc | 2020-02-12 17:02:06 +0800 | [diff] [blame] | 21 | |
Tom Joseph | ca7b252 | 2020-11-18 12:27:11 +0530 | [diff] [blame] | 22 | void constructEntry( |
| 23 | const BIOSStringTable&, Table&, Table&, |
| 24 | std::optional<std::variant<int64_t, std::string>>) override |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 25 | {} |
John Wang | e2efdcc | 2020-02-12 17:02:06 +0800 | [diff] [blame] | 26 | |
| 27 | const std::optional<DBusMapping>& getDbusMap() |
| 28 | { |
| 29 | return dBusMap; |
| 30 | } |
Sampa Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 31 | |
| 32 | int updateAttrVal(Table& /*newValue*/, uint16_t /*attrHdl*/, |
| 33 | uint8_t /*attrType*/, |
| 34 | const PropertyValue& /*newPropVal*/) override |
| 35 | { |
| 36 | return PLDM_SUCCESS; |
| 37 | } |
George Liu | 1244acf | 2020-08-14 09:11:11 +0800 | [diff] [blame] | 38 | |
| 39 | void generateAttributeEntry( |
| 40 | const std::variant<int64_t, std::string>& /*attributevalue*/, |
Pavithra Barithaya | 165749d | 2023-07-10 01:05:26 -0500 | [diff] [blame] | 41 | Table& /*attrValueEntry*/) override |
George Liu | 1244acf | 2020-08-14 09:11:11 +0800 | [diff] [blame] | 42 | {} |
John Wang | e2efdcc | 2020-02-12 17:02:06 +0800 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | TEST(BIOSAttribute, CtorTest) |
| 46 | { |
| 47 | auto jsonReadOnly = R"({ |
George Liu | daa6923 | 2020-09-02 17:22:09 +0800 | [diff] [blame] | 48 | "attribute_name" : "ReadOnly", |
Archana Kakani | d660809 | 2024-05-23 01:14:56 -0500 | [diff] [blame^] | 49 | "read_only" : true, |
| 50 | "help_text" : "HelpText", |
| 51 | "display_name" : "DisplayName" |
John Wang | e2efdcc | 2020-02-12 17:02:06 +0800 | [diff] [blame] | 52 | })"_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", |
Archana Kakani | d660809 | 2024-05-23 01:14:56 -0500 | [diff] [blame^] | 67 | "read_only" : false, |
| 68 | "help_text" : "HelpText", |
| 69 | "display_name" : "DisplayName", |
John Wang | e2efdcc | 2020-02-12 17:02:06 +0800 | [diff] [blame] | 70 | "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 Liu | 92bb402 | 2020-09-03 14:58:24 +0800 | [diff] [blame] | 88 | |
| 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 Misra | 46ece06 | 2020-03-18 07:17:44 -0500 | [diff] [blame] | 100 | } |