| John Wang | 3be7085 | 2020-02-13 15:59:04 +0800 | [diff] [blame] | 1 | #include "libpldmresponder/bios_enum_attribute.hpp" | 
|  | 2 | #include "mocked_bios.hpp" | 
|  | 3 | #include "mocked_utils.hpp" | 
|  | 4 |  | 
| John Wang | 3be7085 | 2020-02-13 15:59:04 +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 | 3be7085 | 2020-02-13 15:59:04 +0800 | [diff] [blame] | 9 | #include <gmock/gmock.h> | 
|  | 10 | #include <gtest/gtest.h> | 
|  | 11 |  | 
|  | 12 | using ::testing::_; | 
|  | 13 | using ::testing::ElementsAreArray; | 
|  | 14 | using ::testing::Return; | 
|  | 15 | using ::testing::StrEq; | 
|  | 16 | using ::testing::Throw; | 
|  | 17 |  | 
|  | 18 | class TestBIOSEnumAttribute : public ::testing::Test | 
|  | 19 | { | 
|  | 20 | public: | 
|  | 21 | const auto& getPossibleValues(const BIOSEnumAttribute& attribute) | 
|  | 22 | { | 
|  | 23 | return attribute.possibleValues; | 
|  | 24 | } | 
|  | 25 |  | 
|  | 26 | const auto& getDefaultValue(const BIOSEnumAttribute& attribute) | 
|  | 27 | { | 
|  | 28 | return attribute.defaultValue; | 
|  | 29 | } | 
|  | 30 | }; | 
|  | 31 |  | 
|  | 32 | TEST_F(TestBIOSEnumAttribute, CtorTest) | 
|  | 33 | { | 
|  | 34 | auto jsonEnumReadOnly = R"({ | 
|  | 35 | "attribute_name" : "CodeUpdatePolicy", | 
|  | 36 | "possible_values" : [ "Concurrent", "Disruptive" ], | 
| George Liu | daa6923 | 2020-09-02 17:22:09 +0800 | [diff] [blame^] | 37 | "default_values" : [ "Concurrent" ], | 
|  | 38 | "readOnly" : true | 
| John Wang | 3be7085 | 2020-02-13 15:59:04 +0800 | [diff] [blame] | 39 | })"_json; | 
|  | 40 |  | 
|  | 41 | BIOSEnumAttribute enumReadOnly{jsonEnumReadOnly, nullptr}; | 
|  | 42 | EXPECT_EQ(enumReadOnly.name, "CodeUpdatePolicy"); | 
|  | 43 | EXPECT_TRUE(enumReadOnly.readOnly); | 
|  | 44 | EXPECT_THAT(getPossibleValues(enumReadOnly), | 
|  | 45 | ElementsAreArray({"Concurrent", "Disruptive"})); | 
|  | 46 | EXPECT_EQ(getDefaultValue(enumReadOnly), "Concurrent"); | 
|  | 47 |  | 
|  | 48 | auto jsonEnumReadOnlyError = R"({ | 
|  | 49 | "attribute_name" : "CodeUpdatePolicy", | 
|  | 50 | "possible_value" : [ "Concurrent", "Disruptive" ], | 
| George Liu | daa6923 | 2020-09-02 17:22:09 +0800 | [diff] [blame^] | 51 | "default_values" : [ "Concurrent" ], | 
|  | 52 | "readOnly" : true | 
| John Wang | 3be7085 | 2020-02-13 15:59:04 +0800 | [diff] [blame] | 53 | })"_json; // possible_value -> possible_values | 
|  | 54 | EXPECT_THROW((BIOSEnumAttribute{jsonEnumReadOnlyError, nullptr}), | 
|  | 55 | Json::exception); | 
|  | 56 |  | 
|  | 57 | auto jsonEnumReadWrite = R"({ | 
|  | 58 | "attribute_name" : "FWBootSide", | 
|  | 59 | "possible_values" : [ "Perm", "Temp" ], | 
|  | 60 | "default_values" : [ "Perm" ], | 
| George Liu | daa6923 | 2020-09-02 17:22:09 +0800 | [diff] [blame^] | 61 | "readOnly" : false, | 
| John Wang | 3be7085 | 2020-02-13 15:59:04 +0800 | [diff] [blame] | 62 | "dbus": | 
|  | 63 | { | 
|  | 64 | "object_path" : "/xyz/abc/def", | 
|  | 65 | "interface" : "xyz.openbmc.FWBoot.Side", | 
|  | 66 | "property_name" : "Side", | 
|  | 67 | "property_type" : "bool", | 
|  | 68 | "property_values" : [true, false] | 
|  | 69 | } | 
|  | 70 | })"_json; | 
|  | 71 |  | 
|  | 72 | BIOSEnumAttribute enumReadWrite{jsonEnumReadWrite, nullptr}; | 
|  | 73 | EXPECT_EQ(enumReadWrite.name, "FWBootSide"); | 
|  | 74 | EXPECT_TRUE(!enumReadWrite.readOnly); | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | TEST_F(TestBIOSEnumAttribute, ConstructEntry) | 
|  | 78 | { | 
|  | 79 | MockBIOSStringTable biosStringTable; | 
|  | 80 | MockdBusHandler dbusHandler; | 
|  | 81 |  | 
|  | 82 | auto jsonEnumReadOnly = R"({ | 
|  | 83 | "attribute_name" : "CodeUpdatePolicy", | 
|  | 84 | "possible_values" : [ "Concurrent", "Disruptive" ], | 
| George Liu | daa6923 | 2020-09-02 17:22:09 +0800 | [diff] [blame^] | 85 | "default_values" : [ "Disruptive" ], | 
|  | 86 | "readOnly" : true | 
| John Wang | 3be7085 | 2020-02-13 15:59:04 +0800 | [diff] [blame] | 87 | })"_json; | 
|  | 88 |  | 
|  | 89 | std::vector<uint8_t> expectedAttrEntry{ | 
|  | 90 | 0,    0, /* attr handle */ | 
|  | 91 | 0x80,    /* attr type enum read-only*/ | 
|  | 92 | 4,    0, /* attr name handle */ | 
|  | 93 | 2,       /* number of possible value */ | 
|  | 94 | 2,    0, /* possible value handle */ | 
|  | 95 | 3,    0, /* possible value handle */ | 
|  | 96 | 1,       /* number of default value */ | 
|  | 97 | 1        /* defaut value string handle index */ | 
|  | 98 | }; | 
|  | 99 |  | 
|  | 100 | std::vector<uint8_t> expectedAttrValueEntry{ | 
|  | 101 | 0, 0, /* attr handle */ | 
|  | 102 | 0x80, /* attr type enum read-only*/ | 
|  | 103 | 1,    /* number of current value */ | 
|  | 104 | 1     /* current value string handle index */ | 
|  | 105 | }; | 
|  | 106 |  | 
|  | 107 | BIOSEnumAttribute enumReadOnly{jsonEnumReadOnly, nullptr}; | 
|  | 108 |  | 
|  | 109 | ON_CALL(biosStringTable, findHandle(StrEq("Concurrent"))) | 
|  | 110 | .WillByDefault(Return(2)); | 
|  | 111 | ON_CALL(biosStringTable, findHandle(StrEq("Disruptive"))) | 
|  | 112 | .WillByDefault(Return(3)); | 
|  | 113 | ON_CALL(biosStringTable, findHandle(StrEq("CodeUpdatePolicy"))) | 
|  | 114 | .WillByDefault(Return(4)); | 
|  | 115 |  | 
|  | 116 | checkConstructEntry(enumReadOnly, biosStringTable, expectedAttrEntry, | 
|  | 117 | expectedAttrValueEntry); | 
|  | 118 |  | 
|  | 119 | auto jsonEnumReadWrite = R"({ | 
|  | 120 | "attribute_name" : "CodeUpdatePolicy", | 
|  | 121 | "possible_values" : [ "Concurrent", "Disruptive" ], | 
|  | 122 | "default_values" : [ "Disruptive" ], | 
| George Liu | daa6923 | 2020-09-02 17:22:09 +0800 | [diff] [blame^] | 123 | "readOnly" : false, | 
| John Wang | 3be7085 | 2020-02-13 15:59:04 +0800 | [diff] [blame] | 124 | "dbus": | 
|  | 125 | { | 
|  | 126 | "object_path" : "/xyz/abc/def", | 
|  | 127 | "interface" : "xyz.openbmc.abc.def", | 
|  | 128 | "property_name" : "Policy", | 
|  | 129 | "property_type" : "bool", | 
|  | 130 | "property_values" : [true, false] | 
|  | 131 | } | 
|  | 132 | })"_json; | 
|  | 133 |  | 
|  | 134 | BIOSEnumAttribute enumReadWrite{jsonEnumReadWrite, &dbusHandler}; | 
|  | 135 |  | 
|  | 136 | EXPECT_CALL(dbusHandler, | 
|  | 137 | getDbusPropertyVariant(StrEq("/xyz/abc/def"), StrEq("Policy"), | 
|  | 138 | StrEq("xyz.openbmc.abc.def"))) | 
|  | 139 | .WillOnce(Throw(std::exception())); | 
|  | 140 |  | 
|  | 141 | /* Set expected attr type to read-write */ | 
|  | 142 | expectedAttrEntry[2] = PLDM_BIOS_ENUMERATION; | 
|  | 143 | expectedAttrValueEntry[2] = PLDM_BIOS_ENUMERATION; | 
|  | 144 |  | 
|  | 145 | checkConstructEntry(enumReadWrite, biosStringTable, expectedAttrEntry, | 
|  | 146 | expectedAttrValueEntry); | 
|  | 147 |  | 
|  | 148 | EXPECT_CALL(dbusHandler, | 
|  | 149 | getDbusPropertyVariant(StrEq("/xyz/abc/def"), StrEq("Policy"), | 
|  | 150 | StrEq("xyz.openbmc.abc.def"))) | 
|  | 151 | .WillOnce(Return(PropertyValue(true))); | 
|  | 152 |  | 
|  | 153 | expectedAttrValueEntry = { | 
|  | 154 | 0, 0, /* attr handle */ | 
|  | 155 | 0,    /* attr type enum read-write*/ | 
|  | 156 | 1,    /* number of current value */ | 
|  | 157 | 0     /* current value string handle index */ | 
|  | 158 | }; | 
|  | 159 |  | 
|  | 160 | checkConstructEntry(enumReadWrite, biosStringTable, expectedAttrEntry, | 
|  | 161 | expectedAttrValueEntry); | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | TEST_F(TestBIOSEnumAttribute, setAttrValueOnDbus) | 
|  | 165 | { | 
|  | 166 | MockBIOSStringTable biosStringTable; | 
|  | 167 | MockdBusHandler dbusHandler; | 
|  | 168 |  | 
|  | 169 | auto jsonEnumReadWrite = R"({ | 
|  | 170 | "attribute_name" : "CodeUpdatePolicy", | 
|  | 171 | "possible_values" : [ "Concurrent", "Disruptive" ], | 
|  | 172 | "default_values" : [ "Disruptive" ], | 
| George Liu | daa6923 | 2020-09-02 17:22:09 +0800 | [diff] [blame^] | 173 | "readOnly" : false, | 
| John Wang | 3be7085 | 2020-02-13 15:59:04 +0800 | [diff] [blame] | 174 | "dbus": | 
|  | 175 | { | 
|  | 176 | "object_path" : "/xyz/abc/def", | 
|  | 177 | "interface" : "xyz.openbmc.abc.def", | 
|  | 178 | "property_name" : "Policy", | 
|  | 179 | "property_type" : "bool", | 
|  | 180 | "property_values" : [true, false] | 
|  | 181 | } | 
|  | 182 | })"_json; | 
|  | 183 | DBusMapping dbusMapping{"/xyz/abc/def", "xyz.openbmc.abc.def", "Policy", | 
|  | 184 | "bool"}; | 
|  | 185 |  | 
|  | 186 | BIOSEnumAttribute enumReadWrite{jsonEnumReadWrite, &dbusHandler}; | 
|  | 187 |  | 
|  | 188 | std::vector<uint8_t> attrEntry{ | 
|  | 189 | 0, 0, /* attr handle */ | 
|  | 190 | 0,    /* attr type enum read-only*/ | 
|  | 191 | 4, 0, /* attr name handle */ | 
|  | 192 | 2,    /* number of possible value */ | 
|  | 193 | 2, 0, /* possible value handle */ | 
|  | 194 | 3, 0, /* possible value handle */ | 
|  | 195 | 1,    /* number of default value */ | 
|  | 196 | 1     /* defaut value string handle index */ | 
|  | 197 | }; | 
|  | 198 |  | 
|  | 199 | ON_CALL(biosStringTable, findString(2)) | 
|  | 200 | .WillByDefault(Return(std::string("Concurrent"))); | 
|  | 201 | ON_CALL(biosStringTable, findString(3)) | 
|  | 202 | .WillByDefault(Return(std::string("Disruptive"))); | 
|  | 203 |  | 
|  | 204 | std::vector<uint8_t> attrValueEntry{ | 
|  | 205 | 0, 0, /* attr handle */ | 
|  | 206 | 0,    /* attr type enum read-only*/ | 
|  | 207 | 1,    /* number of current value */ | 
|  | 208 | 0     /* current value string handle index */ | 
|  | 209 | }; | 
|  | 210 |  | 
|  | 211 | EXPECT_CALL(dbusHandler, | 
|  | 212 | setDbusProperty(dbusMapping, PropertyValue{bool(true)})) | 
|  | 213 | .Times(1); | 
|  | 214 | enumReadWrite.setAttrValueOnDbus( | 
|  | 215 | reinterpret_cast<pldm_bios_attr_val_table_entry*>( | 
|  | 216 | attrValueEntry.data()), | 
|  | 217 | reinterpret_cast<pldm_bios_attr_table_entry*>(attrEntry.data()), | 
|  | 218 | biosStringTable); | 
|  | 219 | } |