John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 1 | #include "libpldmresponder/bios_string_attribute.hpp" |
| 2 | #include "mocked_bios.hpp" |
| 3 | #include "mocked_utils.hpp" |
| 4 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 5 | #include <nlohmann/json.hpp> |
| 6 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
John Wang | 29683b5 | 2020-02-27 16:41:44 +0800 | [diff] [blame] | 9 | #include <gmock/gmock.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | using namespace pldm::responder::bios; |
| 13 | using ::testing::_; |
| 14 | using ::testing::ElementsAreArray; |
| 15 | using ::testing::Return; |
| 16 | using ::testing::StrEq; |
| 17 | using ::testing::Throw; |
| 18 | |
| 19 | class TestBIOSStringAttribute : public ::testing::Test |
| 20 | { |
| 21 | public: |
| 22 | const auto& getStringInfo(const BIOSStringAttribute& biosStringAttribute) |
| 23 | { |
| 24 | return biosStringAttribute.stringInfo; |
| 25 | } |
| 26 | }; |
| 27 | |
| 28 | TEST_F(TestBIOSStringAttribute, CtorTest) |
| 29 | { |
| 30 | auto jsonStringReadOnly = R"( { |
| 31 | "attribute_name" : "str_example3", |
| 32 | "string_type" : "ASCII", |
| 33 | "minimum_string_length" : 1, |
| 34 | "maximum_string_length" : 100, |
| 35 | "default_string_length" : 2, |
| 36 | "default_string" : "ef" |
| 37 | })"_json; |
| 38 | BIOSStringAttribute stringReadOnly{jsonStringReadOnly, nullptr}; |
| 39 | EXPECT_EQ(stringReadOnly.name, "str_example3"); |
| 40 | EXPECT_TRUE(stringReadOnly.readOnly); |
| 41 | |
| 42 | auto& stringInfo = getStringInfo(stringReadOnly); |
| 43 | EXPECT_EQ(stringInfo.stringType, |
| 44 | static_cast<uint8_t>(BIOSStringAttribute::Encoding::ASCII)); |
| 45 | EXPECT_EQ(stringInfo.minLength, 1); |
| 46 | EXPECT_EQ(stringInfo.maxLength, 100); |
| 47 | EXPECT_EQ(stringInfo.defLength, 2); |
| 48 | EXPECT_EQ(stringInfo.defString, "ef"); |
| 49 | |
| 50 | auto jsonStringReadOnlyError = R"( { |
| 51 | "attribute_name" : "str_example3", |
| 52 | "string_type" : "ASCII", |
| 53 | "minimum_string_length" : 1, |
| 54 | "maximum_string_length" : 100, |
| 55 | "default_string" : "ef" |
| 56 | })"_json; // missing default_string_length |
| 57 | |
| 58 | EXPECT_THROW((BIOSStringAttribute{jsonStringReadOnlyError, nullptr}), |
| 59 | Json::exception); |
| 60 | |
| 61 | auto jsonStringReadWrite = R"({ |
| 62 | "attribute_name" : "str_example1", |
| 63 | "string_type" : "ASCII", |
| 64 | "minimum_string_length" : 1, |
| 65 | "maximum_string_length" : 100, |
| 66 | "default_string_length" : 3, |
| 67 | "default_string" : "abc", |
| 68 | "dbus" : { |
| 69 | "object_path" : "/xyz/abc/def", |
| 70 | "interface" : "xyz.openbmc_project.str_example1.value", |
| 71 | "property_name" : "Str_example1", |
| 72 | "property_type" : "string" |
| 73 | } |
| 74 | })"_json; |
| 75 | BIOSStringAttribute stringReadWrite{jsonStringReadWrite, nullptr}; |
| 76 | |
| 77 | EXPECT_EQ(stringReadWrite.name, "str_example1"); |
| 78 | EXPECT_TRUE(!stringReadWrite.readOnly); |
| 79 | } |
| 80 | |
| 81 | TEST_F(TestBIOSStringAttribute, ConstructEntry) |
| 82 | { |
| 83 | MockBIOSStringTable biosStringTable; |
| 84 | MockdBusHandler dbusHandler; |
| 85 | |
| 86 | auto jsonStringReadOnly = R"({ |
| 87 | "attribute_name" : "str_example1", |
| 88 | "string_type" : "ASCII", |
| 89 | "minimum_string_length" : 1, |
| 90 | "maximum_string_length" : 100, |
| 91 | "default_string_length" : 3, |
| 92 | "default_string" : "abc" |
| 93 | })"_json; |
| 94 | |
| 95 | std::vector<uint8_t> expectedAttrEntry{ |
| 96 | 0, 0, /* attr handle */ |
| 97 | 0x81, /* attr type string read-only */ |
| 98 | 5, 0, /* attr name handle */ |
| 99 | 1, /* string type */ |
| 100 | 1, 0, /* minimum length of the string in bytes */ |
| 101 | 100, 0, /* maximum length of the string in bytes */ |
| 102 | 3, 0, /* length of default string in length */ |
| 103 | 'a', 'b', 'c' /* default string */ |
| 104 | }; |
| 105 | |
| 106 | std::vector<uint8_t> expectedAttrValueEntry{ |
| 107 | 0, 0, /* attr handle */ |
| 108 | 0x81, /* attr type string read-only */ |
| 109 | 3, 0, /* current string length */ |
| 110 | 'a', 'b', 'c', /* defaut value string handle index */ |
| 111 | }; |
| 112 | |
| 113 | ON_CALL(biosStringTable, findHandle(StrEq("str_example1"))) |
| 114 | .WillByDefault(Return(5)); |
| 115 | BIOSStringAttribute stringReadOnly{jsonStringReadOnly, nullptr}; |
| 116 | |
| 117 | checkConstructEntry(stringReadOnly, biosStringTable, expectedAttrEntry, |
| 118 | expectedAttrValueEntry); |
| 119 | |
| 120 | auto jsonStringReadWrite = R"({ |
| 121 | "attribute_name" : "str_example1", |
| 122 | "string_type" : "ASCII", |
| 123 | "minimum_string_length" : 1, |
| 124 | "maximum_string_length" : 100, |
| 125 | "default_string_length" : 3, |
| 126 | "default_string" : "abc", |
| 127 | "dbus" : { |
| 128 | "object_path" : "/xyz/abc/def", |
| 129 | "interface" : "xyz.openbmc_project.str_example1.value", |
| 130 | "property_name" : "Str_example1", |
| 131 | "property_type" : "string" |
| 132 | } |
| 133 | })"_json; |
| 134 | BIOSStringAttribute stringReadWrite{jsonStringReadWrite, &dbusHandler}; |
| 135 | |
| 136 | /* Set expected attr type to read-write */ |
| 137 | expectedAttrEntry[2] = PLDM_BIOS_STRING; |
| 138 | expectedAttrValueEntry[2] = PLDM_BIOS_STRING; |
| 139 | |
| 140 | EXPECT_CALL( |
| 141 | dbusHandler, |
| 142 | getDbusPropertyVariant(StrEq("/xyz/abc/def"), StrEq("Str_example1"), |
| 143 | StrEq("xyz.openbmc_project.str_example1.value"))) |
| 144 | .WillOnce(Throw(std::exception())); |
| 145 | |
| 146 | checkConstructEntry(stringReadWrite, biosStringTable, expectedAttrEntry, |
| 147 | expectedAttrValueEntry); |
| 148 | |
| 149 | EXPECT_CALL( |
| 150 | dbusHandler, |
| 151 | getDbusPropertyVariant(StrEq("/xyz/abc/def"), StrEq("Str_example1"), |
| 152 | StrEq("xyz.openbmc_project.str_example1.value"))) |
| 153 | .WillOnce(Return(PropertyValue(std::string("abcd")))); |
| 154 | |
| 155 | expectedAttrValueEntry = { |
| 156 | 0, 0, /* attr handle */ |
| 157 | 1, /* attr type string read-write */ |
| 158 | 4, 0, /* current string length */ |
| 159 | 'a', 'b', 'c', 'd', /* defaut value string handle index */ |
| 160 | }; |
| 161 | |
| 162 | checkConstructEntry(stringReadWrite, biosStringTable, expectedAttrEntry, |
| 163 | expectedAttrValueEntry); |
| 164 | } |
| 165 | |
| 166 | TEST_F(TestBIOSStringAttribute, setAttrValueOnDbus) |
| 167 | { |
| 168 | auto jsonStringReadWrite = R"({ |
| 169 | "attribute_name" : "str_example1", |
| 170 | "string_type" : "ASCII", |
| 171 | "minimum_string_length" : 1, |
| 172 | "maximum_string_length" : 100, |
| 173 | "default_string_length" : 3, |
| 174 | "default_string" : "abc", |
| 175 | "dbus" : { |
| 176 | "object_path" : "/xyz/abc/def", |
| 177 | "interface" : "xyz.openbmc_project.str_example1.value", |
| 178 | "property_name" : "Str_example1", |
| 179 | "property_type" : "string" |
| 180 | } |
| 181 | })"_json; |
| 182 | |
| 183 | MockdBusHandler dbusHandler; |
| 184 | MockBIOSStringTable biosStringTable; |
| 185 | |
| 186 | BIOSStringAttribute stringReadWrite{jsonStringReadWrite, &dbusHandler}; |
| 187 | DBusMapping dbusMapping{"/xyz/abc/def", |
| 188 | "xyz.openbmc_project.str_example1.value", |
| 189 | "Str_example1", "string"}; |
| 190 | std::vector<uint8_t> attrValueEntry{ |
| 191 | 0, 0, /* attr handle */ |
| 192 | 1, /* attr type string read-write */ |
| 193 | 4, 0, /* current string length */ |
| 194 | 'a', 'b', 'c', 'd', /* defaut value string handle index */ |
| 195 | }; |
| 196 | auto entry = reinterpret_cast<pldm_bios_attr_val_table_entry*>( |
| 197 | attrValueEntry.data()); |
| 198 | PropertyValue value = std::string("abcd"); |
| 199 | EXPECT_CALL(dbusHandler, setDbusProperty(dbusMapping, value)).Times(1); |
| 200 | stringReadWrite.setAttrValueOnDbus(entry, nullptr, biosStringTable); |
| 201 | } |