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