Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 1 | #include "libpldmresponder/bios.hpp" |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 2 | #include "libpldmresponder/bios_parser.hpp" |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 3 | #include "libpldmresponder/bios_table.hpp" |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 4 | |
| 5 | #include <string.h> |
| 6 | |
| 7 | #include <array> |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 8 | #include <cstring> |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 9 | #include <ctime> |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 10 | #include <filesystem> |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 11 | |
| 12 | #include "libpldm/base.h" |
| 13 | #include "libpldm/bios.h" |
| 14 | |
| 15 | #include <gtest/gtest.h> |
| 16 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 17 | using namespace pldm; |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 18 | using namespace pldm::responder; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 19 | using namespace pldm::responder::bios; |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 20 | using namespace pldm::responder::utils; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 21 | using namespace bios_parser; |
Sampa Misra | 032bd50 | 2019-03-06 05:03:22 -0600 | [diff] [blame] | 22 | |
| 23 | TEST(epochToBCDTime, testTime) |
| 24 | { |
| 25 | struct tm time |
| 26 | { |
| 27 | }; |
| 28 | time.tm_year = 119; |
| 29 | time.tm_mon = 3; |
| 30 | time.tm_mday = 13; |
| 31 | time.tm_hour = 5; |
| 32 | time.tm_min = 18; |
| 33 | time.tm_sec = 13; |
| 34 | time.tm_isdst = -1; |
| 35 | |
| 36 | time_t epochTime = mktime(&time); |
| 37 | uint8_t seconds = 0; |
| 38 | uint8_t minutes = 0; |
| 39 | uint8_t hours = 0; |
| 40 | uint8_t day = 0; |
| 41 | uint8_t month = 0; |
| 42 | uint16_t year = 0; |
| 43 | |
| 44 | epochToBCDTime(epochTime, seconds, minutes, hours, day, month, year); |
| 45 | |
| 46 | ASSERT_EQ(0x13, seconds); |
| 47 | ASSERT_EQ(0x18, minutes); |
| 48 | ASSERT_EQ(0x5, hours); |
| 49 | ASSERT_EQ(0x13, day); |
| 50 | ASSERT_EQ(0x4, month); |
| 51 | ASSERT_EQ(0x2019, year); |
| 52 | } |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 53 | |
Xiaochao Ma | 60227a0 | 2019-12-04 09:00:12 +0800 | [diff] [blame] | 54 | TEST(timeToEpoch, testTime0) |
| 55 | { |
| 56 | std::time_t ret = 1555132693; |
| 57 | |
| 58 | uint8_t sec = 13; |
| 59 | uint8_t min = 18; |
| 60 | uint8_t hours = 5; |
| 61 | uint8_t day = 13; |
| 62 | uint8_t month = 4; |
| 63 | uint16_t year = 2019; |
| 64 | |
| 65 | std::time_t timeSec = 0; |
| 66 | timeSec = timeToEpoch(sec, min, hours, day, month, year); |
| 67 | |
| 68 | EXPECT_EQ(ret, timeSec); |
| 69 | } |
| 70 | |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 71 | TEST(GetBIOSStrings, allScenarios) |
| 72 | { |
| 73 | using namespace bios_parser; |
| 74 | // All the BIOS Strings in the BIOS JSON config files. |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 75 | Strings vec{"HMCManagedState", |
| 76 | "On", |
| 77 | "Off", |
| 78 | "FWBootSide", |
| 79 | "Perm", |
| 80 | "Temp", |
| 81 | "InbandCodeUpdate", |
| 82 | "Allowed", |
| 83 | "NotAllowed", |
| 84 | "CodeUpdatePolicy", |
| 85 | "Concurrent", |
| 86 | "Disruptive", |
| 87 | "VDD_AVSBUS_RAIL", |
| 88 | "SBE_IMAGE_MINIMUM_VALID_ECS", |
| 89 | "INTEGER_INVALID_CASE", |
| 90 | "str_example1", |
| 91 | "str_example2", |
| 92 | "str_example3"}; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 93 | |
| 94 | Strings nullVec{}; |
| 95 | |
| 96 | // Invalid directory |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 97 | bios_parser::setupConfig("./bios_json_invalid"); |
| 98 | auto strings = bios_parser::getStrings(); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 99 | ASSERT_EQ(strings == nullVec, true); |
| 100 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 101 | bios_parser::setupConfig("./bios_jsons"); |
| 102 | strings = bios_parser::getStrings(); |
Carol Wang | 612f35b | 2019-08-26 17:14:26 +0800 | [diff] [blame] | 103 | std::sort(strings.begin(), strings.end()); |
| 104 | std::sort(vec.begin(), vec.end()); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 105 | ASSERT_EQ(strings == vec, true); |
| 106 | } |
| 107 | |
Carol Wang | 69d3e7f | 2019-09-04 16:43:15 +0800 | [diff] [blame] | 108 | TEST(getAttrValue, enumScenarios) |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 109 | { |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 110 | using namespace bios_parser::bios_enum; |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 111 | // All the BIOS Strings in the BIOS JSON config files. |
| 112 | AttrValuesMap valueMap{ |
| 113 | {"HMCManagedState", {false, {"On", "Off"}, {"On"}}}, |
| 114 | {"FWBootSide", {false, {"Perm", "Temp"}, {"Perm"}}}, |
| 115 | {"InbandCodeUpdate", {false, {"Allowed", "NotAllowed"}, {"Allowed"}}}, |
| 116 | {"CodeUpdatePolicy", |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 117 | {true, {"Concurrent", "Disruptive"}, {"Concurrent"}}}}; |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 118 | |
Carol Wang | 69d3e7f | 2019-09-04 16:43:15 +0800 | [diff] [blame] | 119 | auto values = bios_parser::bios_enum::getValues(); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 120 | ASSERT_EQ(valueMap == values, true); |
| 121 | |
Carol Wang | 69d3e7f | 2019-09-04 16:43:15 +0800 | [diff] [blame] | 122 | bios_parser::bios_enum::CurrentValues cv{"Concurrent"}; |
| 123 | auto value = bios_parser::bios_enum::getAttrValue("CodeUpdatePolicy"); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 124 | ASSERT_EQ(value == cv, true); |
| 125 | |
| 126 | // Invalid attribute name |
Carol Wang | 69d3e7f | 2019-09-04 16:43:15 +0800 | [diff] [blame] | 127 | ASSERT_THROW(bios_parser::bios_enum::getAttrValue("CodeUpdatePolic"), |
| 128 | std::out_of_range); |
| 129 | } |
| 130 | |
| 131 | TEST(getAttrValue, stringScenarios) |
| 132 | { |
| 133 | // All the BIOS Strings in the BIOS JSON config files. |
| 134 | bios_parser::bios_string::AttrValuesMap valueMap{ |
| 135 | {"str_example1", {false, 1, 1, 100, 3, "abc"}}, |
| 136 | {"str_example2", {false, 2, 0, 100, 0, ""}}, |
| 137 | {"str_example3", {true, 0, 1, 100, 2, "ef"}}}; |
| 138 | |
Carol Wang | 69d3e7f | 2019-09-04 16:43:15 +0800 | [diff] [blame] | 139 | auto values = bios_parser::bios_string::getValues(); |
| 140 | ASSERT_EQ(valueMap == values, true); |
| 141 | |
| 142 | // Test the attribute without dbus |
| 143 | std::string cv = "ef"; |
| 144 | auto value = bios_parser::bios_string::getAttrValue("str_example3"); |
| 145 | EXPECT_EQ(value, cv); |
| 146 | |
| 147 | // Invalid attribute name |
| 148 | ASSERT_THROW(bios_parser::bios_string::getAttrValue("str_example"), |
| 149 | std::out_of_range); |
Tom Joseph | 52552ef | 2019-06-20 09:50:15 +0530 | [diff] [blame] | 150 | } |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 151 | |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 152 | TEST(getAttrValue, integerScenarios) |
| 153 | { |
| 154 | using namespace bios_parser::bios_integer; |
| 155 | AttrValuesMap valueMap{ |
| 156 | {"VDD_AVSBUS_RAIL", {false, 0, 15, 1, 0}}, |
| 157 | {"SBE_IMAGE_MINIMUM_VALID_ECS", {true, 1, 15, 1, 2}}}; |
| 158 | |
| 159 | auto values = getValues(); |
| 160 | EXPECT_EQ(valueMap, values); |
| 161 | auto value = getAttrValue("SBE_IMAGE_MINIMUM_VALID_ECS"); |
| 162 | EXPECT_EQ(value, 2); |
| 163 | |
| 164 | EXPECT_THROW(getAttrValue("VDM"), std::out_of_range); |
| 165 | } |
| 166 | |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 167 | TEST(traverseBIOSTable, attrTableScenarios) |
| 168 | { |
| 169 | std::vector<uint8_t> enumEntry{ |
| 170 | 0, 0, /* attr handle */ |
| 171 | 0, /* attr type */ |
| 172 | 1, 0, /* attr name handle */ |
| 173 | 2, /* number of possible value */ |
| 174 | 2, 0, /* possible value handle */ |
| 175 | 3, 0, /* possible value handle */ |
| 176 | 1, /* number of default value */ |
| 177 | 0 /* defaut value string handle index */ |
| 178 | }; |
| 179 | std::vector<uint8_t> stringEntry{ |
| 180 | 4, 0, /* attr handle */ |
| 181 | 1, /* attr type */ |
| 182 | 12, 0, /* attr name handle */ |
| 183 | 1, /* string type */ |
| 184 | 1, 0, /* minimum length of the string in bytes */ |
| 185 | 100, 0, /* maximum length of the string in bytes */ |
| 186 | 3, 0, /* length of default string in length */ |
| 187 | 'a', 'b', 'c' /* default string */ |
| 188 | }; |
| 189 | std::vector<uint8_t> table; |
| 190 | table.insert(table.end(), enumEntry.begin(), enumEntry.end()); |
| 191 | table.insert(table.end(), stringEntry.begin(), stringEntry.end()); |
| 192 | auto padSize = ((table.size() % 4) ? (4 - table.size() % 4) : 0); |
| 193 | |
| 194 | table.insert(table.end(), padSize, 0); |
| 195 | table.insert(table.end(), sizeof(uint32_t) /*checksum*/, 0); |
| 196 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 197 | pldm::responder::bios::traverseBIOSAttrTable( |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 198 | table, [&](const struct pldm_bios_attr_table_entry* entry) { |
| 199 | int rc; |
| 200 | switch (entry->attr_type) |
| 201 | { |
| 202 | case 0: |
| 203 | rc = std::memcmp(entry, enumEntry.data(), enumEntry.size()); |
| 204 | EXPECT_EQ(rc, 0); |
| 205 | break; |
| 206 | case 1: |
| 207 | rc = std::memcmp(entry, stringEntry.data(), |
| 208 | stringEntry.size()); |
| 209 | EXPECT_EQ(rc, 0); |
| 210 | break; |
| 211 | default: |
| 212 | break; |
| 213 | } |
| 214 | }); |
| 215 | } |
| 216 | |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 217 | namespace fs = std::filesystem; |
| 218 | class TestAllBIOSTables : public ::testing::Test |
| 219 | { |
| 220 | public: |
| 221 | static void SetUpTestCase() // will execute once at the begining of all |
| 222 | // TestAllBIOSTables objects |
| 223 | { |
| 224 | char tmpdir[] = "/tmp/allBiosTables.XXXXXX"; |
| 225 | biosPath = fs::path(mkdtemp(tmpdir)); |
| 226 | } |
| 227 | |
| 228 | static void TearDownTestCase() // will be executed once at th eend of all |
| 229 | // TestAllBIOSTables objects |
| 230 | { |
| 231 | fs::remove_all(biosPath); |
| 232 | } |
| 233 | |
| 234 | static fs::path biosPath; |
| 235 | }; |
| 236 | |
| 237 | fs::path TestAllBIOSTables::biosPath; |
| 238 | |
| 239 | TEST_F(TestAllBIOSTables, GetBIOSTableTestBadRequest) |
| 240 | { |
| 241 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_REQ_BYTES> |
| 242 | requestPayload{}; |
| 243 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 244 | struct pldm_get_bios_table_req* req = |
| 245 | (struct pldm_get_bios_table_req*)request->payload; |
| 246 | req->transfer_handle = 9; |
| 247 | req->transfer_op_flag = PLDM_GET_FIRSTPART; |
| 248 | req->table_type = 0xFF; |
| 249 | |
| 250 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 251 | |
| 252 | auto response = internal::buildBIOSTables(request, requestPayloadLength, |
| 253 | "./bios_jsons", biosPath.c_str()); |
| 254 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 255 | |
| 256 | struct pldm_get_bios_table_resp* resp = |
| 257 | reinterpret_cast<struct pldm_get_bios_table_resp*>( |
| 258 | responsePtr->payload); |
| 259 | |
| 260 | ASSERT_EQ(PLDM_INVALID_BIOS_TABLE_TYPE, resp->completion_code); |
| 261 | } |
| 262 | |
| 263 | TEST_F(TestAllBIOSTables, buildBIOSTablesTestBadRequest) |
| 264 | { |
| 265 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_REQ_BYTES> |
| 266 | requestPayload{}; |
| 267 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 268 | struct pldm_get_bios_table_req* req = |
| 269 | (struct pldm_get_bios_table_req*)request->payload; |
| 270 | req->transfer_handle = 9; |
| 271 | req->transfer_op_flag = PLDM_GET_FIRSTPART; |
| 272 | req->table_type = PLDM_BIOS_ATTR_VAL_TABLE; |
| 273 | |
| 274 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 275 | |
| 276 | auto response = internal::buildBIOSTables(request, requestPayloadLength, |
| 277 | "./bios_jsons", biosPath.c_str()); |
| 278 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 279 | struct pldm_get_bios_table_resp* resp = |
| 280 | reinterpret_cast<struct pldm_get_bios_table_resp*>( |
| 281 | responsePtr->payload); |
| 282 | ASSERT_EQ(PLDM_BIOS_TABLE_UNAVAILABLE, resp->completion_code); |
| 283 | |
| 284 | req->table_type = PLDM_BIOS_ATTR_TABLE; |
| 285 | response = internal::buildBIOSTables(request, requestPayloadLength, |
| 286 | "./bios_jsons", biosPath.c_str()); |
| 287 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 288 | resp = reinterpret_cast<struct pldm_get_bios_table_resp*>( |
| 289 | responsePtr->payload); |
| 290 | ASSERT_EQ(PLDM_BIOS_TABLE_UNAVAILABLE, resp->completion_code); |
| 291 | } |
| 292 | |
| 293 | TEST_F(TestAllBIOSTables, GetBIOSStringTableTestGoodRequest) |
| 294 | { |
| 295 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_REQ_BYTES> |
| 296 | requestPayload{}; |
| 297 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 298 | struct pldm_get_bios_table_req* req = |
| 299 | (struct pldm_get_bios_table_req*)request->payload; |
| 300 | req->transfer_handle = 9; |
| 301 | req->transfer_op_flag = PLDM_GET_FIRSTPART; |
| 302 | req->table_type = PLDM_BIOS_STRING_TABLE; |
| 303 | |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 304 | Strings biosStrings = getStrings(); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 305 | std::sort(biosStrings.begin(), biosStrings.end()); |
| 306 | biosStrings.erase(std::unique(biosStrings.begin(), biosStrings.end()), |
| 307 | biosStrings.end()); |
| 308 | |
| 309 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 310 | uint8_t times = 0; |
| 311 | while (times < 2) |
| 312 | { // first time fresh table second time existing table |
| 313 | auto response = internal::buildBIOSTables( |
| 314 | request, requestPayloadLength, "./bios_jsons", biosPath.c_str()); |
| 315 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 316 | |
| 317 | struct pldm_get_bios_table_resp* resp = |
| 318 | reinterpret_cast<struct pldm_get_bios_table_resp*>( |
| 319 | responsePtr->payload); |
| 320 | |
| 321 | ASSERT_EQ(0, resp->completion_code); |
| 322 | ASSERT_EQ(0, resp->next_transfer_handle); |
| 323 | ASSERT_EQ(PLDM_START_AND_END, resp->transfer_flag); |
| 324 | |
| 325 | uint16_t strLen = 0; |
| 326 | uint8_t* tableData = reinterpret_cast<uint8_t*>(resp->table_data); |
| 327 | |
| 328 | for (auto elem : biosStrings) |
| 329 | { |
| 330 | struct pldm_bios_string_table_entry* ptr = |
| 331 | reinterpret_cast<struct pldm_bios_string_table_entry*>( |
| 332 | tableData); |
| 333 | strLen = ptr->string_length; |
| 334 | ASSERT_EQ(strLen, elem.size()); |
| 335 | ASSERT_EQ(0, memcmp(elem.c_str(), ptr->name, strLen)); |
| 336 | tableData += (sizeof(pldm_bios_string_table_entry) - 1) + strLen; |
| 337 | |
| 338 | } // end for |
| 339 | times++; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | TEST_F(TestAllBIOSTables, getBIOSAttributeTableTestGoodRequest) |
| 344 | { |
| 345 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_REQ_BYTES> |
| 346 | requestPayload{}; |
| 347 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 348 | struct pldm_get_bios_table_req* req = |
| 349 | (struct pldm_get_bios_table_req*)request->payload; |
| 350 | req->transfer_handle = 9; |
| 351 | req->transfer_op_flag = PLDM_GET_FIRSTPART; |
| 352 | req->table_type = PLDM_BIOS_ATTR_TABLE; |
| 353 | |
| 354 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 355 | |
| 356 | uint8_t times = 0; |
| 357 | while (times < 2) |
| 358 | { // first time fresh table second time existing table |
| 359 | auto response = internal::buildBIOSTables( |
| 360 | request, requestPayloadLength, "./bios_jsons", biosPath.c_str()); |
| 361 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 362 | |
| 363 | struct pldm_get_bios_table_resp* resp = |
| 364 | reinterpret_cast<struct pldm_get_bios_table_resp*>( |
| 365 | responsePtr->payload); |
| 366 | |
| 367 | ASSERT_EQ(0, resp->completion_code); |
| 368 | ASSERT_EQ(0, resp->next_transfer_handle); |
| 369 | ASSERT_EQ(PLDM_START_AND_END, resp->transfer_flag); |
| 370 | |
| 371 | uint32_t attrTableLen = |
| 372 | response.size() - sizeof(pldm_msg_hdr) - |
| 373 | (sizeof(resp->completion_code) + |
| 374 | sizeof(resp->next_transfer_handle) + sizeof(resp->transfer_flag)); |
| 375 | uint32_t traversed = 0; |
| 376 | uint16_t attrHdl = 0; |
| 377 | uint16_t stringHdl = 0; |
| 378 | uint8_t attrType = 0; |
| 379 | uint8_t numPosVals = 0; |
| 380 | uint8_t numDefVals = 0; |
| 381 | uint8_t defIndex = 0; |
| 382 | |
| 383 | uint8_t* tableData = reinterpret_cast<uint8_t*>(resp->table_data); |
| 384 | while (1) |
| 385 | { |
| 386 | struct pldm_bios_attr_table_entry* ptr = |
| 387 | reinterpret_cast<struct pldm_bios_attr_table_entry*>(tableData); |
| 388 | attrHdl = ptr->attr_handle; |
| 389 | attrType = ptr->attr_type; |
| 390 | EXPECT_EQ(0, attrHdl); |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 391 | EXPECT_EQ(PLDM_BIOS_ENUMERATION_READ_ONLY, attrType); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 392 | stringHdl = ptr->string_handle; |
| 393 | EXPECT_EQ(stringHdl, 1); |
| 394 | tableData += sizeof(attrHdl) + sizeof(attrType) + sizeof(stringHdl); |
| 395 | numPosVals = *tableData; |
| 396 | EXPECT_EQ(numPosVals, 2); |
| 397 | traversed += sizeof(attrHdl) + sizeof(attrType) + sizeof(stringHdl); |
| 398 | traversed += sizeof(numPosVals); |
| 399 | PossibleValuesByHandle possiVals; |
| 400 | tableData++; |
| 401 | uint16_t* temp = reinterpret_cast<uint16_t*>(tableData); |
| 402 | uint32_t i = 0; |
| 403 | while (i < numPosVals) |
| 404 | { |
| 405 | uint16_t val = *temp; |
| 406 | possiVals.push_back(val); |
| 407 | temp++; |
| 408 | i++; |
| 409 | } |
| 410 | EXPECT_EQ(possiVals.size(), 2); |
| 411 | tableData += numPosVals * sizeof(stringHdl); |
| 412 | traversed += numPosVals * sizeof(stringHdl); |
| 413 | numDefVals = *tableData; |
| 414 | EXPECT_EQ(numDefVals, 1); |
| 415 | tableData += numDefVals * sizeof(defIndex); |
| 416 | traversed += numDefVals + sizeof(numDefVals); |
| 417 | |
| 418 | break; // test for first row only |
| 419 | |
| 420 | if ((attrTableLen - traversed) < 8) |
| 421 | { |
| 422 | // reached pad |
| 423 | break; |
| 424 | } |
| 425 | |
| 426 | } // end while |
| 427 | times++; |
| 428 | } |
| 429 | |
| 430 | } // end TEST |
| 431 | |
| 432 | TEST_F(TestAllBIOSTables, getBIOSAttributeValueTableTestGoodRequest) |
| 433 | { |
| 434 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_REQ_BYTES> |
| 435 | requestPayload{}; |
| 436 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 437 | struct pldm_get_bios_table_req* req = |
| 438 | (struct pldm_get_bios_table_req*)request->payload; |
| 439 | req->transfer_handle = 9; |
| 440 | req->transfer_op_flag = PLDM_GET_FIRSTPART; |
| 441 | req->table_type = PLDM_BIOS_ATTR_VAL_TABLE; |
| 442 | |
| 443 | std::string attrName("CodeUpdatePolicy"); |
John Wang | ecb7d57 | 2019-10-17 13:38:53 +0800 | [diff] [blame] | 444 | bios_enum::CurrentValues currVals = bios_enum::getAttrValue(attrName); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 445 | |
| 446 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 447 | |
| 448 | uint8_t times = 0; |
| 449 | while (times < 2) |
| 450 | { // first time frest table second time existing table |
| 451 | auto response = internal::buildBIOSTables( |
| 452 | request, requestPayloadLength, "./bios_jsons", biosPath.c_str()); |
| 453 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 454 | |
| 455 | struct pldm_get_bios_table_resp* resp = |
| 456 | reinterpret_cast<struct pldm_get_bios_table_resp*>( |
| 457 | responsePtr->payload); |
| 458 | |
| 459 | ASSERT_EQ(0, resp->completion_code); |
| 460 | ASSERT_EQ(0, resp->next_transfer_handle); |
| 461 | ASSERT_EQ(PLDM_START_AND_END, resp->transfer_flag); |
| 462 | |
| 463 | uint32_t attrValTableLen = |
| 464 | response.size() - sizeof(pldm_msg_hdr) - |
| 465 | (sizeof(resp->completion_code) + |
| 466 | sizeof(resp->next_transfer_handle) + sizeof(resp->transfer_flag)); |
| 467 | uint32_t traversed = 0; |
| 468 | uint8_t* tableData = reinterpret_cast<uint8_t*>(resp->table_data); |
| 469 | |
| 470 | uint16_t attrHdl; |
| 471 | uint8_t attrType; |
| 472 | uint8_t numCurrVals; |
| 473 | uint8_t currValStrIndex; |
| 474 | |
| 475 | while (1) |
| 476 | { |
| 477 | struct pldm_bios_attr_val_table_entry* ptr = |
| 478 | reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 479 | tableData); |
| 480 | attrHdl = ptr->attr_handle; |
| 481 | attrType = ptr->attr_type; |
| 482 | EXPECT_EQ(0, attrHdl); |
John Wang | e96e7e5 | 2019-10-05 17:47:30 +0800 | [diff] [blame] | 483 | EXPECT_EQ(PLDM_BIOS_ENUMERATION_READ_ONLY, attrType); |
Sampa Misra | b37be31 | 2019-07-03 02:26:41 -0500 | [diff] [blame] | 484 | tableData += sizeof(attrHdl) + sizeof(attrType); |
| 485 | traversed += sizeof(attrHdl) + sizeof(attrType); |
| 486 | numCurrVals = *tableData; |
| 487 | EXPECT_EQ(1, numCurrVals); |
| 488 | tableData += sizeof(numCurrVals); |
| 489 | traversed += sizeof(numCurrVals); |
| 490 | currValStrIndex = *tableData; |
| 491 | EXPECT_EQ(0, currValStrIndex); |
| 492 | tableData += numCurrVals; |
| 493 | traversed += numCurrVals; |
| 494 | break; // testing for first row |
| 495 | if ((attrValTableLen - traversed) < 8) |
| 496 | { |
| 497 | break; |
| 498 | } |
| 499 | } |
| 500 | times++; |
| 501 | } |
| 502 | |
| 503 | } // end TEST |
Carol Wang | 69d3e7f | 2019-09-04 16:43:15 +0800 | [diff] [blame] | 504 | |
| 505 | class TestSingleTypeBIOSTable : public ::testing::Test |
| 506 | { |
| 507 | public: |
| 508 | void SetUp() override |
| 509 | { // will be executed before each individual test defined |
| 510 | // in TestSingleTypeBIOSTable |
| 511 | char tmpdir[] = "/tmp/singleTypeBIOSTable.XXXXXX"; |
| 512 | destBiosPath = fs::path(mkdtemp(tmpdir)); |
| 513 | } |
| 514 | |
| 515 | void TearDown() override |
| 516 | { // will be executed after each individual test |
| 517 | // defined in TestSingleTypeBIOSTable |
| 518 | fs::remove_all(destBiosPath); |
| 519 | } |
| 520 | |
| 521 | void CopySingleJsonFile(std::string file) |
| 522 | { |
| 523 | fs::path srcDir("./bios_jsons"); |
| 524 | fs::path srcBiosPath = srcDir / file; |
| 525 | std::filesystem::copy(srcBiosPath, destBiosPath); |
| 526 | } |
| 527 | |
| 528 | fs::path destBiosPath; |
| 529 | }; |
| 530 | |
| 531 | TEST_F(TestSingleTypeBIOSTable, getBIOSAttributeValueTableBasedOnStringTypeTest) |
| 532 | { |
| 533 | // Copy string json file to the destination |
| 534 | TestSingleTypeBIOSTable::CopySingleJsonFile(bios_parser::bIOSStrJson); |
| 535 | auto fpath = TestSingleTypeBIOSTable::destBiosPath.c_str(); |
| 536 | |
| 537 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_REQ_BYTES> |
| 538 | requestPayload{}; |
| 539 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 540 | struct pldm_get_bios_table_req* req = |
| 541 | (struct pldm_get_bios_table_req*)request->payload; |
| 542 | |
| 543 | // Get string table with string json file only |
| 544 | req->transfer_handle = 9; |
| 545 | req->transfer_op_flag = PLDM_GET_FIRSTPART; |
| 546 | req->table_type = PLDM_BIOS_STRING_TABLE; |
| 547 | |
| 548 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 549 | auto str_response = |
| 550 | internal::buildBIOSTables(request, requestPayloadLength, fpath, fpath); |
| 551 | |
| 552 | // Get attribute table with string json file only |
| 553 | req->transfer_handle = 9; |
| 554 | req->transfer_op_flag = PLDM_GET_FIRSTPART; |
| 555 | req->table_type = PLDM_BIOS_ATTR_TABLE; |
| 556 | |
| 557 | auto attr_response = |
| 558 | internal::buildBIOSTables(request, requestPayloadLength, fpath, fpath); |
| 559 | |
| 560 | // Get attribute value table with string type |
| 561 | req->transfer_handle = 9; |
| 562 | req->transfer_op_flag = PLDM_GET_FIRSTPART; |
| 563 | req->table_type = PLDM_BIOS_ATTR_VAL_TABLE; |
| 564 | |
| 565 | // Test attribute str_example3 here, which has no dbus |
| 566 | for (uint8_t times = 0; times < 2; times++) |
| 567 | { // first time first table second time existing table |
| 568 | auto response = internal::buildBIOSTables(request, requestPayloadLength, |
| 569 | fpath, fpath); |
| 570 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 571 | |
| 572 | struct pldm_get_bios_table_resp* resp = |
| 573 | reinterpret_cast<struct pldm_get_bios_table_resp*>( |
| 574 | responsePtr->payload); |
| 575 | |
| 576 | ASSERT_EQ(0, resp->completion_code); |
| 577 | ASSERT_EQ(0, resp->next_transfer_handle); |
| 578 | ASSERT_EQ(PLDM_START_AND_END, resp->transfer_flag); |
| 579 | |
| 580 | uint32_t attrValTableLen = |
| 581 | response.size() - sizeof(pldm_msg_hdr) - |
| 582 | (sizeof(struct pldm_get_bios_table_resp) - 1); |
| 583 | uint32_t traversed = 0; |
| 584 | uint8_t* tableData = reinterpret_cast<uint8_t*>(resp->table_data); |
| 585 | |
| 586 | while (true) |
| 587 | { |
| 588 | struct pldm_bios_attr_val_table_entry* ptr = |
| 589 | reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 590 | tableData); |
| 591 | uint16_t attrHdl = ptr->attr_handle; |
| 592 | uint8_t attrType = ptr->attr_type; |
| 593 | EXPECT_EQ(PLDM_BIOS_STRING_READ_ONLY, attrType); |
| 594 | tableData += sizeof(attrHdl) + sizeof(attrType); |
| 595 | traversed += sizeof(attrHdl) + sizeof(attrType); |
| 596 | auto sizeDefaultStr = *(reinterpret_cast<uint16_t*>(tableData)); |
| 597 | EXPECT_EQ(2, sizeDefaultStr); |
| 598 | tableData += sizeof(uint16_t); |
| 599 | traversed += sizeof(uint16_t); |
| 600 | EXPECT_EQ('e', *tableData); |
| 601 | EXPECT_EQ('f', *(tableData + 1)); |
| 602 | tableData += sizeDefaultStr; |
| 603 | traversed += sizeDefaultStr; |
| 604 | break; // testing for first row |
| 605 | if ((attrValTableLen - traversed) < 8) |
| 606 | { |
| 607 | break; |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | } // end TEST |
John Wang | dbbc9ff | 2019-10-25 13:53:46 +0800 | [diff] [blame] | 613 | |
| 614 | TEST_F(TestSingleTypeBIOSTable, |
| 615 | getBIOSAttributeValueTableBasedOnIntegerTypeTest) |
| 616 | { |
| 617 | // Copy integer json file to the destination |
| 618 | TestSingleTypeBIOSTable::CopySingleJsonFile(bios_parser::bIOSIntegerJson); |
| 619 | auto fpath = TestSingleTypeBIOSTable::destBiosPath.c_str(); |
| 620 | |
| 621 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_REQ_BYTES> |
| 622 | requestPayload{}; |
| 623 | auto request = reinterpret_cast<pldm_msg*>(requestPayload.data()); |
| 624 | struct pldm_get_bios_table_req* req = |
| 625 | (struct pldm_get_bios_table_req*)request->payload; |
| 626 | |
| 627 | // Get string table with integer json file only |
| 628 | req->transfer_handle = 9; |
| 629 | req->transfer_op_flag = PLDM_GET_FIRSTPART; |
| 630 | req->table_type = PLDM_BIOS_STRING_TABLE; |
| 631 | |
| 632 | size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr); |
| 633 | internal::buildBIOSTables(request, requestPayloadLength, fpath, fpath); |
| 634 | |
| 635 | // Get attribute table with integer json file only |
| 636 | req->transfer_handle = 9; |
| 637 | req->transfer_op_flag = PLDM_GET_FIRSTPART; |
| 638 | req->table_type = PLDM_BIOS_ATTR_TABLE; |
| 639 | |
| 640 | auto attr_response = |
| 641 | internal::buildBIOSTables(request, requestPayloadLength, fpath, fpath); |
| 642 | |
| 643 | // Get attribute value table with integer type |
| 644 | req->transfer_handle = 9; |
| 645 | req->transfer_op_flag = PLDM_GET_FIRSTPART; |
| 646 | req->table_type = PLDM_BIOS_ATTR_VAL_TABLE; |
| 647 | |
| 648 | // Test attribute SBE_IMAGE_MINIMUM_VALID_ECS here, which has no dbus |
| 649 | for (uint8_t times = 0; times < 2; times++) |
| 650 | { // first time first table second time existing table |
| 651 | auto response = internal::buildBIOSTables(request, requestPayloadLength, |
| 652 | fpath, fpath); |
| 653 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 654 | |
| 655 | struct pldm_get_bios_table_resp* resp = |
| 656 | reinterpret_cast<struct pldm_get_bios_table_resp*>( |
| 657 | responsePtr->payload); |
| 658 | |
| 659 | EXPECT_EQ(0, resp->completion_code); |
| 660 | EXPECT_EQ(0, resp->next_transfer_handle); |
| 661 | EXPECT_EQ(PLDM_START_AND_END, resp->transfer_flag); |
| 662 | uint8_t* tableData = reinterpret_cast<uint8_t*>(resp->table_data); |
| 663 | auto ptr = |
| 664 | reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(tableData); |
| 665 | uint16_t attrHdl = ptr->attr_handle; |
| 666 | uint8_t attrType = ptr->attr_type; |
| 667 | EXPECT_EQ(PLDM_BIOS_INTEGER_READ_ONLY, attrType); |
| 668 | tableData += sizeof(attrHdl) + sizeof(attrType); |
| 669 | auto cv = *(reinterpret_cast<uint64_t*>(tableData)); |
| 670 | EXPECT_EQ(2, cv); |
| 671 | } |
| 672 | } |