Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1 | #include <endian.h> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 2 | |
Manojkiran Eda | 9a8e497 | 2022-11-28 16:38:21 +0530 | [diff] [blame] | 3 | #include <algorithm> |
| 4 | #include <cstdint> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 5 | #include <cstring> |
Manojkiran Eda | 9a8e497 | 2022-11-28 16:38:21 +0530 | [diff] [blame] | 6 | #include <iterator> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 7 | #include <string> |
| 8 | #include <utility> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "libpldm/base.h" |
| 12 | #include "libpldm/bios.h" |
| 13 | #include "libpldm/bios_table.h" |
| 14 | #include "libpldm/utils.h" |
| 15 | |
| 16 | #include <gmock/gmock.h> |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | using testing::ElementsAreArray; |
| 20 | using Table = std::vector<uint8_t>; |
| 21 | |
| 22 | void buildTable(Table& table) |
| 23 | { |
| 24 | auto padSize = ((table.size() % 4) ? (4 - table.size() % 4) : 0); |
| 25 | table.insert(table.end(), padSize, 0); |
| 26 | uint32_t checksum = crc32(table.data(), table.size()); |
| 27 | checksum = htole32(checksum); |
| 28 | uint8_t a[4]; |
| 29 | std::memcpy(a, &checksum, sizeof(checksum)); |
| 30 | table.insert(table.end(), std::begin(a), std::end(a)); |
| 31 | } |
| 32 | |
| 33 | template <typename First, typename... Rest> |
| 34 | void buildTable(Table& table, First& first, Rest&... rest) |
| 35 | { |
| 36 | table.insert(table.end(), first.begin(), first.end()); |
| 37 | buildTable(table, rest...); |
| 38 | } |
| 39 | |
| 40 | TEST(AttrTable, HeaderDecodeTest) |
| 41 | { |
| 42 | std::vector<uint8_t> enumEntry{ |
| 43 | 2, 0, /* attr handle */ |
| 44 | 0, /* attr type */ |
| 45 | 1, 0, /* attr name handle (string handle) */ |
| 46 | 2, /* number of possible value */ |
| 47 | 2, 0, /* possible value handle */ |
| 48 | 3, 0, /* possible value handle */ |
| 49 | 1, /* number of default value */ |
| 50 | 0 /* defaut value string handle index */ |
| 51 | }; |
| 52 | auto entry = |
| 53 | reinterpret_cast<struct pldm_bios_attr_table_entry*>(enumEntry.data()); |
| 54 | auto attrHandle = pldm_bios_table_attr_entry_decode_attribute_handle(entry); |
| 55 | EXPECT_EQ(attrHandle, 2); |
| 56 | auto attrType = pldm_bios_table_attr_entry_decode_attribute_type(entry); |
| 57 | EXPECT_EQ(attrType, 0); |
| 58 | auto stringHandle = pldm_bios_table_attr_entry_decode_string_handle(entry); |
| 59 | EXPECT_EQ(stringHandle, 1); |
| 60 | } |
| 61 | |
| 62 | TEST(AttrTable, EnumEntryDecodeTest) |
| 63 | { |
| 64 | std::vector<uint8_t> enumEntry{ |
| 65 | 0, 0, /* attr handle */ |
| 66 | 0, /* attr type */ |
| 67 | 1, 0, /* attr name handle */ |
| 68 | 2, /* number of possible value */ |
| 69 | 2, 0, /* possible value handle */ |
| 70 | 3, 0, /* possible value handle */ |
| 71 | 1, /* number of default value */ |
| 72 | 1 /* defaut value string handle index */ |
| 73 | }; |
| 74 | |
| 75 | auto entry = |
| 76 | reinterpret_cast<struct pldm_bios_attr_table_entry*>(enumEntry.data()); |
| 77 | uint8_t pvNumber = pldm_bios_table_attr_entry_enum_decode_pv_num(entry); |
| 78 | EXPECT_EQ(pvNumber, 2); |
| 79 | pvNumber = 0; |
| 80 | auto rc = |
| 81 | pldm_bios_table_attr_entry_enum_decode_pv_num_check(entry, &pvNumber); |
| 82 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 83 | EXPECT_EQ(pvNumber, 2); |
| 84 | |
| 85 | std::vector<uint16_t> pvHandles(pvNumber, 0); |
| 86 | pvNumber = pldm_bios_table_attr_entry_enum_decode_pv_hdls( |
| 87 | entry, pvHandles.data(), pvHandles.size()); |
| 88 | EXPECT_EQ(pvNumber, 2); |
| 89 | EXPECT_EQ(pvHandles[0], 2); |
| 90 | EXPECT_EQ(pvHandles[1], 3); |
| 91 | pvHandles.resize(1); |
| 92 | pvNumber = pldm_bios_table_attr_entry_enum_decode_pv_hdls( |
| 93 | entry, pvHandles.data(), pvHandles.size()); |
| 94 | EXPECT_EQ(pvNumber, 1); |
| 95 | EXPECT_EQ(pvHandles[0], 2); |
| 96 | |
| 97 | pvHandles.resize(2); |
| 98 | rc = pldm_bios_table_attr_entry_enum_decode_pv_hdls_check( |
| 99 | entry, pvHandles.data(), pvHandles.size()); |
| 100 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 101 | EXPECT_EQ(pvHandles[0], 2); |
| 102 | EXPECT_EQ(pvHandles[1], 3); |
| 103 | rc = pldm_bios_table_attr_entry_enum_decode_pv_hdls_check( |
| 104 | entry, pvHandles.data(), 1); |
| 105 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 106 | |
| 107 | uint8_t defNumber = pldm_bios_table_attr_entry_enum_decode_def_num(entry); |
| 108 | EXPECT_EQ(defNumber, 1); |
| 109 | std::vector<uint8_t> defIndices(defNumber); |
| 110 | rc = pldm_bios_table_attr_entry_enum_decode_def_indices( |
| 111 | entry, defIndices.data(), defIndices.size()); |
| 112 | EXPECT_EQ(rc, defNumber); |
| 113 | EXPECT_THAT(defIndices, ElementsAreArray({1})); |
| 114 | |
| 115 | defNumber = 0; |
| 116 | rc = |
| 117 | pldm_bios_table_attr_entry_enum_decode_def_num_check(entry, &defNumber); |
| 118 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 119 | EXPECT_EQ(defNumber, 1); |
| 120 | |
| 121 | rc = |
| 122 | pldm_bios_table_attr_entry_enum_decode_pv_num_check(nullptr, &pvNumber); |
| 123 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 124 | rc = pldm_bios_table_attr_entry_enum_decode_def_num_check(entry, nullptr); |
| 125 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 126 | |
| 127 | entry->attr_type = PLDM_BIOS_STRING; |
| 128 | rc = pldm_bios_table_attr_entry_enum_decode_pv_num_check(entry, &pvNumber); |
| 129 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 130 | |
| 131 | rc = |
| 132 | pldm_bios_table_attr_entry_enum_decode_def_num_check(entry, &defNumber); |
| 133 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 134 | rc = |
| 135 | pldm_bios_table_attr_entry_enum_decode_pv_hdls_check(entry, nullptr, 0); |
| 136 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 137 | } |
| 138 | |
| 139 | TEST(AttrTable, EnumEntryEncodeTest) |
| 140 | { |
| 141 | std::vector<uint8_t> enumEntry{ |
| 142 | 0, 0, /* attr handle */ |
| 143 | 0, /* attr type */ |
| 144 | 1, 0, /* attr name handle */ |
| 145 | 2, /* number of possible value */ |
| 146 | 2, 0, /* possible value handle */ |
| 147 | 3, 0, /* possible value handle */ |
| 148 | 1, /* number of default value */ |
| 149 | 0 /* defaut value string handle index */ |
| 150 | }; |
| 151 | |
| 152 | std::vector<uint16_t> pv_hdls{2, 3}; |
| 153 | std::vector<uint8_t> defs{0}; |
| 154 | |
| 155 | struct pldm_bios_table_attr_entry_enum_info info = { |
| 156 | 1, /* name handle */ |
| 157 | false, /* read only */ |
| 158 | 2, /* pv number */ |
| 159 | pv_hdls.data(), /* pv handle */ |
| 160 | 1, /*def number */ |
| 161 | defs.data() /*def index*/ |
| 162 | }; |
| 163 | auto encodeLength = pldm_bios_table_attr_entry_enum_encode_length(2, 1); |
| 164 | EXPECT_EQ(encodeLength, enumEntry.size()); |
| 165 | |
| 166 | std::vector<uint8_t> encodeEntry(encodeLength, 0); |
| 167 | pldm_bios_table_attr_entry_enum_encode(encodeEntry.data(), |
| 168 | encodeEntry.size(), &info); |
| 169 | // set attr handle = 0 |
| 170 | encodeEntry[0] = 0; |
| 171 | encodeEntry[1] = 0; |
| 172 | |
| 173 | EXPECT_EQ(enumEntry, encodeEntry); |
| 174 | |
| 175 | EXPECT_DEATH(pldm_bios_table_attr_entry_enum_encode( |
| 176 | encodeEntry.data(), encodeEntry.size() - 1, &info), |
| 177 | "length <= entry_length"); |
| 178 | auto rc = pldm_bios_table_attr_entry_enum_encode_check( |
| 179 | encodeEntry.data(), encodeEntry.size(), &info); |
| 180 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 181 | // set attr handle = 0 |
| 182 | encodeEntry[0] = 0; |
| 183 | encodeEntry[1] = 0; |
| 184 | |
| 185 | EXPECT_EQ(enumEntry, encodeEntry); |
| 186 | rc = pldm_bios_table_attr_entry_enum_encode_check( |
| 187 | encodeEntry.data(), encodeEntry.size() - 1, &info); |
| 188 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 189 | } |
| 190 | |
| 191 | TEST(AttrTable, StringEntryDecodeTest) |
| 192 | { |
| 193 | std::vector<uint8_t> stringEntry{ |
| 194 | 1, 0, /* attr handle */ |
| 195 | 1, /* attr type */ |
| 196 | 12, 0, /* attr name handle */ |
| 197 | 1, /* string type */ |
| 198 | 1, 0, /* minimum length of the string in bytes */ |
| 199 | 100, 0, /* maximum length of the string in bytes */ |
| 200 | 3, 0, /* length of default string in length */ |
| 201 | 'a', 'b', 'c' /* default string */ |
| 202 | }; |
| 203 | |
| 204 | auto entry = reinterpret_cast<struct pldm_bios_attr_table_entry*>( |
| 205 | stringEntry.data()); |
| 206 | auto stringType = |
| 207 | pldm_bios_table_attr_entry_string_decode_string_type(entry); |
| 208 | EXPECT_EQ(stringType, 1); |
| 209 | auto minLength = pldm_bios_table_attr_entry_string_decode_min_length(entry); |
| 210 | EXPECT_EQ(minLength, 1); |
| 211 | auto maxLength = pldm_bios_table_attr_entry_string_decode_max_length(entry); |
| 212 | EXPECT_EQ(maxLength, 100); |
| 213 | |
| 214 | uint16_t defStringLength = |
| 215 | pldm_bios_table_attr_entry_string_decode_def_string_length(entry); |
| 216 | EXPECT_EQ(defStringLength, 3); |
| 217 | std::vector<char> defString(defStringLength + 1); |
| 218 | auto rc = pldm_bios_table_attr_entry_string_decode_def_string( |
| 219 | entry, defString.data(), defString.size()); |
| 220 | EXPECT_EQ(rc, 3); |
| 221 | EXPECT_STREQ(defString.data(), "abc"); |
| 222 | rc = pldm_bios_table_attr_entry_string_decode_def_string( |
| 223 | entry, defString.data(), defString.size() - 1); |
| 224 | EXPECT_EQ(rc, 2); |
| 225 | EXPECT_STREQ(defString.data(), "ab"); |
| 226 | |
| 227 | defStringLength = 0; |
| 228 | rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 229 | entry, &defStringLength); |
| 230 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 231 | EXPECT_EQ(defStringLength, 3); |
| 232 | |
| 233 | rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 234 | entry, nullptr); |
| 235 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 236 | rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 237 | nullptr, &defStringLength); |
| 238 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 239 | entry->attr_type = PLDM_BIOS_INTEGER; |
| 240 | rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 241 | entry, &defStringLength); |
| 242 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 243 | rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 244 | nullptr, &defStringLength); |
| 245 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 246 | } |
| 247 | |
| 248 | TEST(AttrTable, StringEntryEncodeTest) |
| 249 | { |
| 250 | std::vector<uint8_t> stringEntry{ |
| 251 | 0, 0, /* attr handle */ |
| 252 | 1, /* attr type */ |
| 253 | 3, 0, /* attr name handle */ |
| 254 | 1, /* string type */ |
| 255 | 1, 0, /* min string length */ |
| 256 | 100, 0, /* max string length */ |
| 257 | 3, 0, /* default string length */ |
| 258 | 'a', 'b', 'c', /* defaul string */ |
| 259 | }; |
| 260 | |
| 261 | struct pldm_bios_table_attr_entry_string_info info = { |
| 262 | 3, /* name handle */ |
| 263 | false, /* read only */ |
| 264 | 1, /* string type ascii */ |
| 265 | 1, /* min length */ |
| 266 | 100, /* max length */ |
| 267 | 3, /* def length */ |
| 268 | "abc", /* def string */ |
| 269 | }; |
| 270 | auto encodeLength = pldm_bios_table_attr_entry_string_encode_length(3); |
| 271 | EXPECT_EQ(encodeLength, stringEntry.size()); |
| 272 | |
| 273 | std::vector<uint8_t> encodeEntry(encodeLength, 0); |
| 274 | pldm_bios_table_attr_entry_string_encode(encodeEntry.data(), |
| 275 | encodeEntry.size(), &info); |
| 276 | // set attr handle = 0 |
| 277 | encodeEntry[0] = 0; |
| 278 | encodeEntry[1] = 0; |
| 279 | |
| 280 | EXPECT_EQ(stringEntry, encodeEntry); |
| 281 | |
| 282 | EXPECT_DEATH(pldm_bios_table_attr_entry_string_encode( |
| 283 | encodeEntry.data(), encodeEntry.size() - 1, &info), |
| 284 | "length <= entry_length"); |
| 285 | auto rc = pldm_bios_table_attr_entry_string_encode_check( |
| 286 | encodeEntry.data(), encodeEntry.size(), &info); |
| 287 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 288 | // set attr handle = 0 |
| 289 | encodeEntry[0] = 0; |
| 290 | encodeEntry[1] = 0; |
| 291 | |
| 292 | EXPECT_EQ(stringEntry, encodeEntry); |
| 293 | rc = pldm_bios_table_attr_entry_string_encode_check( |
| 294 | encodeEntry.data(), encodeEntry.size() - 1, &info); |
| 295 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 296 | std::swap(info.max_length, info.min_length); |
| 297 | const char* errmsg; |
| 298 | rc = pldm_bios_table_attr_entry_string_info_check(&info, &errmsg); |
| 299 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 300 | EXPECT_STREQ( |
| 301 | "MinimumStingLength should not be greater than MaximumStringLength", |
| 302 | errmsg); |
| 303 | rc = pldm_bios_table_attr_entry_string_encode_check( |
| 304 | encodeEntry.data(), encodeEntry.size(), &info); |
| 305 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 306 | std::swap(info.max_length, info.min_length); |
| 307 | |
| 308 | std::vector<uint8_t> stringEntryLength0{ |
| 309 | 0, 0, /* attr handle */ |
| 310 | 1, /* attr type */ |
| 311 | 3, 0, /* attr name handle */ |
| 312 | 1, /* string type */ |
| 313 | 1, 0, /* min string length */ |
| 314 | 100, 0, /* max string length */ |
| 315 | 0, 0, /* default string length */ |
| 316 | }; |
| 317 | |
| 318 | info.def_length = 0; |
| 319 | info.def_string = nullptr; |
| 320 | |
| 321 | encodeLength = pldm_bios_table_attr_entry_string_encode_length(0); |
| 322 | EXPECT_EQ(encodeLength, stringEntryLength0.size()); |
| 323 | |
| 324 | encodeEntry.resize(encodeLength); |
| 325 | pldm_bios_table_attr_entry_string_encode(encodeEntry.data(), |
| 326 | encodeEntry.size(), &info); |
| 327 | // set attr handle = 0 |
| 328 | encodeEntry[0] = 0; |
| 329 | encodeEntry[1] = 0; |
| 330 | |
| 331 | EXPECT_EQ(stringEntryLength0, encodeEntry); |
| 332 | } |
| 333 | |
| 334 | TEST(AttrTable, integerEntryEncodeTest) |
| 335 | { |
| 336 | std::vector<uint8_t> integerEntry{ |
| 337 | 0, 0, /* attr handle */ |
| 338 | 3, /* attr type */ |
| 339 | 1, 0, /* attr name handle */ |
| 340 | 1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */ |
| 341 | 10, 0, 0, 0, 0, 0, 0, 0, /* upper bound */ |
| 342 | 2, 0, 0, 0, /* scalar increment */ |
| 343 | 3, 0, 0, 0, 0, 0, 0, 0, /* defaut value */ |
| 344 | }; |
| 345 | |
| 346 | std::vector<uint16_t> pv_hdls{2, 3}; |
| 347 | std::vector<uint8_t> defs{0}; |
| 348 | |
| 349 | struct pldm_bios_table_attr_entry_integer_info info = { |
| 350 | 1, /* name handle */ |
| 351 | false, /* read only */ |
| 352 | 1, /* lower bound */ |
| 353 | 10, /* upper bound */ |
| 354 | 2, /* sacalar increment */ |
| 355 | 3 /* default value */ |
| 356 | }; |
| 357 | auto encodeLength = pldm_bios_table_attr_entry_integer_encode_length(); |
| 358 | EXPECT_EQ(encodeLength, integerEntry.size()); |
| 359 | |
| 360 | std::vector<uint8_t> encodeEntry(encodeLength, 0); |
| 361 | pldm_bios_table_attr_entry_integer_encode(encodeEntry.data(), |
| 362 | encodeEntry.size(), &info); |
| 363 | // set attr handle = 0 |
| 364 | encodeEntry[0] = 0; |
| 365 | encodeEntry[1] = 0; |
| 366 | |
| 367 | EXPECT_EQ(integerEntry, encodeEntry); |
| 368 | |
| 369 | EXPECT_DEATH(pldm_bios_table_attr_entry_integer_encode( |
| 370 | encodeEntry.data(), encodeEntry.size() - 1, &info), |
| 371 | "length <= entry_length"); |
| 372 | |
| 373 | auto rc = pldm_bios_table_attr_entry_integer_encode_check( |
| 374 | encodeEntry.data(), encodeEntry.size(), &info); |
| 375 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 376 | // set attr handle = 0 |
| 377 | encodeEntry[0] = 0; |
| 378 | encodeEntry[1] = 0; |
| 379 | |
| 380 | EXPECT_EQ(integerEntry, encodeEntry); |
| 381 | |
| 382 | rc = pldm_bios_table_attr_entry_integer_encode_check( |
| 383 | encodeEntry.data(), encodeEntry.size() - 1, &info); |
| 384 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 385 | |
| 386 | info.lower_bound = 100; |
| 387 | info.upper_bound = 50; |
| 388 | const char* errmsg; |
| 389 | rc = pldm_bios_table_attr_entry_integer_info_check(&info, &errmsg); |
| 390 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 391 | EXPECT_STREQ("LowerBound should not be greater than UpperBound", errmsg); |
| 392 | rc = pldm_bios_table_attr_entry_integer_encode_check( |
| 393 | encodeEntry.data(), encodeEntry.size(), &info); |
| 394 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 395 | } |
| 396 | |
| 397 | TEST(AttrTable, integerEntryDecodeTest) |
| 398 | { |
| 399 | std::vector<uint8_t> integerEntry{ |
| 400 | 0, 0, /* attr handle */ |
| 401 | 3, /* attr type */ |
| 402 | 1, 0, /* attr name handle */ |
| 403 | 1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */ |
| 404 | 10, 0, 0, 0, 0, 0, 0, 0, /* upper bound */ |
| 405 | 2, 0, 0, 0, /* scalar increment */ |
| 406 | 3, 0, 0, 0, 0, 0, 0, 0, /* defaut value */ |
| 407 | }; |
| 408 | |
| 409 | uint64_t lower, upper, def; |
| 410 | uint32_t scalar; |
| 411 | auto entry = reinterpret_cast<struct pldm_bios_attr_table_entry*>( |
| 412 | integerEntry.data()); |
| 413 | pldm_bios_table_attr_entry_integer_decode(entry, &lower, &upper, &scalar, |
| 414 | &def); |
| 415 | EXPECT_EQ(lower, 1u); |
| 416 | EXPECT_EQ(upper, 10u); |
| 417 | EXPECT_EQ(scalar, 2u); |
| 418 | EXPECT_EQ(def, 3u); |
| 419 | } |
| 420 | |
| 421 | TEST(AttrTable, ItearatorTest) |
| 422 | { |
| 423 | std::vector<uint8_t> enumEntry{ |
| 424 | 0, 0, /* attr handle */ |
| 425 | 0, /* attr type */ |
| 426 | 1, 0, /* attr name handle */ |
| 427 | 2, /* number of possible value */ |
| 428 | 2, 0, /* possible value handle */ |
| 429 | 3, 0, /* possible value handle */ |
| 430 | 1, /* number of default value */ |
| 431 | 0 /* defaut value string handle index */ |
| 432 | }; |
| 433 | std::vector<uint8_t> stringEntry{ |
| 434 | 1, 0, /* attr handle */ |
| 435 | 1, /* attr type */ |
| 436 | 12, 0, /* attr name handle */ |
| 437 | 1, /* string type */ |
| 438 | 1, 0, /* minimum length of the string in bytes */ |
| 439 | 100, 0, /* maximum length of the string in bytes */ |
| 440 | 3, 0, /* length of default string in length */ |
| 441 | 'a', 'b', 'c' /* default string */ |
| 442 | }; |
| 443 | std::vector<uint8_t> integerEntry{ |
| 444 | 0, 0, /* attr handle */ |
| 445 | 3, /* attr type */ |
| 446 | 1, 0, /* attr name handle */ |
| 447 | 1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */ |
| 448 | 10, 0, 0, 0, 0, 0, 0, 0, /* upper bound */ |
| 449 | 2, 0, 0, 0, /* scalar increment */ |
| 450 | 3, 0, 0, 0, 0, 0, 0, 0, /* defaut value */ |
| 451 | }; |
| 452 | |
| 453 | Table table; |
| 454 | buildTable(table, enumEntry, stringEntry, integerEntry, enumEntry); |
| 455 | auto iter = pldm_bios_table_iter_create(table.data(), table.size(), |
| 456 | PLDM_BIOS_ATTR_TABLE); |
| 457 | auto entry = pldm_bios_table_iter_attr_entry_value(iter); |
| 458 | auto rc = std::memcmp(entry, enumEntry.data(), enumEntry.size()); |
| 459 | EXPECT_EQ(rc, 0); |
| 460 | |
| 461 | pldm_bios_table_iter_next(iter); |
| 462 | entry = pldm_bios_table_iter_attr_entry_value(iter); |
| 463 | rc = std::memcmp(entry, stringEntry.data(), stringEntry.size()); |
| 464 | EXPECT_EQ(rc, 0); |
| 465 | |
| 466 | pldm_bios_table_iter_next(iter); |
| 467 | entry = pldm_bios_table_iter_attr_entry_value(iter); |
| 468 | rc = std::memcmp(entry, integerEntry.data(), integerEntry.size()); |
| 469 | EXPECT_EQ(rc, 0); |
| 470 | |
| 471 | pldm_bios_table_iter_next(iter); |
| 472 | entry = pldm_bios_table_iter_attr_entry_value(iter); |
| 473 | rc = std::memcmp(entry, enumEntry.data(), enumEntry.size()); |
| 474 | EXPECT_EQ(rc, 0); |
| 475 | |
| 476 | pldm_bios_table_iter_next(iter); |
| 477 | EXPECT_TRUE(pldm_bios_table_iter_is_end(iter)); |
| 478 | pldm_bios_table_iter_free(iter); |
| 479 | } |
| 480 | |
| 481 | TEST(AttrTable, FindTest) |
| 482 | { |
| 483 | std::vector<uint8_t> enumEntry{ |
| 484 | 0, 0, /* attr handle */ |
| 485 | 0, /* attr type */ |
| 486 | 1, 0, /* attr name handle */ |
| 487 | 2, /* number of possible value */ |
| 488 | 2, 0, /* possible value handle */ |
| 489 | 3, 0, /* possible value handle */ |
| 490 | 1, /* number of default value */ |
| 491 | 0 /* defaut value string handle index */ |
| 492 | }; |
| 493 | std::vector<uint8_t> stringEntry{ |
| 494 | 1, 0, /* attr handle */ |
| 495 | 1, /* attr type */ |
| 496 | 2, 0, /* attr name handle */ |
| 497 | 1, /* string type */ |
| 498 | 1, 0, /* minimum length of the string in bytes */ |
| 499 | 100, 0, /* maximum length of the string in bytes */ |
| 500 | 3, 0, /* length of default string in length */ |
| 501 | 'a', 'b', 'c' /* default string */ |
| 502 | }; |
| 503 | std::vector<uint8_t> integerEntry{ |
| 504 | 0, 0, /* attr handle */ |
| 505 | 3, /* attr type */ |
| 506 | 3, 0, /* attr name handle */ |
| 507 | 1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */ |
| 508 | 10, 0, 0, 0, 0, 0, 0, 0, /* upper bound */ |
| 509 | 2, 0, 0, 0, /* scalar increment */ |
| 510 | 3, 0, 0, 0, 0, 0, 0, 0, /* defaut value */ |
| 511 | }; |
| 512 | |
| 513 | Table table; |
| 514 | buildTable(table, enumEntry, stringEntry, integerEntry, enumEntry); |
| 515 | |
| 516 | auto entry = |
| 517 | pldm_bios_table_attr_find_by_handle(table.data(), table.size(), 1); |
| 518 | EXPECT_NE(entry, nullptr); |
| 519 | auto p = reinterpret_cast<const uint8_t*>(entry); |
| 520 | EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()), |
| 521 | ElementsAreArray(stringEntry)); |
| 522 | |
| 523 | entry = pldm_bios_table_attr_find_by_handle(table.data(), table.size(), 3); |
| 524 | EXPECT_EQ(entry, nullptr); |
| 525 | |
| 526 | entry = pldm_bios_table_attr_find_by_string_handle(table.data(), |
| 527 | table.size(), 2); |
| 528 | EXPECT_NE(entry, nullptr); |
| 529 | p = reinterpret_cast<const uint8_t*>(entry); |
| 530 | EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()), |
| 531 | ElementsAreArray(stringEntry)); |
| 532 | |
| 533 | entry = pldm_bios_table_attr_find_by_string_handle(table.data(), |
| 534 | table.size(), 4); |
| 535 | EXPECT_EQ(entry, nullptr); |
| 536 | } |
| 537 | |
| 538 | TEST(AttrValTable, HeaderDecodeTest) |
| 539 | { |
| 540 | std::vector<uint8_t> enumEntry{ |
| 541 | 1, 0, /* attr handle */ |
| 542 | 0, /* attr type */ |
| 543 | 2, /* number of current value */ |
| 544 | 0, /* current value string handle index */ |
| 545 | 1, /* current value string handle index */ |
| 546 | }; |
| 547 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 548 | enumEntry.data()); |
| 549 | auto attrHandle = |
| 550 | pldm_bios_table_attr_value_entry_decode_attribute_handle(entry); |
| 551 | EXPECT_EQ(attrHandle, 1); |
| 552 | auto attrType = |
| 553 | pldm_bios_table_attr_value_entry_decode_attribute_type(entry); |
| 554 | EXPECT_EQ(attrType, 0); |
| 555 | } |
| 556 | |
| 557 | TEST(AttrValTable, EnumEntryEncodeTest) |
| 558 | { |
| 559 | std::vector<uint8_t> enumEntry{ |
| 560 | 0, 0, /* attr handle */ |
| 561 | 0, /* attr type */ |
| 562 | 2, /* number of current value */ |
| 563 | 0, /* current value string handle index */ |
| 564 | 1, /* current value string handle index */ |
| 565 | }; |
| 566 | |
| 567 | auto length = pldm_bios_table_attr_value_entry_encode_enum_length(2); |
| 568 | EXPECT_EQ(length, enumEntry.size()); |
| 569 | std::vector<uint8_t> encodeEntry(length, 0); |
| 570 | uint8_t handles[] = {0, 1}; |
| 571 | pldm_bios_table_attr_value_entry_encode_enum( |
| 572 | encodeEntry.data(), encodeEntry.size(), 0, 0, 2, handles); |
| 573 | EXPECT_EQ(encodeEntry, enumEntry); |
| 574 | |
| 575 | EXPECT_DEATH( |
| 576 | pldm_bios_table_attr_value_entry_encode_enum( |
| 577 | encodeEntry.data(), encodeEntry.size() - 1, 0, 0, 2, handles), |
| 578 | "length <= entry_length"); |
| 579 | |
| 580 | auto rc = pldm_bios_table_attr_value_entry_encode_enum_check( |
| 581 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_ENUMERATION, 2, |
| 582 | handles); |
| 583 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 584 | EXPECT_EQ(encodeEntry, enumEntry); |
| 585 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 586 | enumEntry.data()); |
| 587 | entry->attr_type = PLDM_BIOS_ENUMERATION_READ_ONLY; |
| 588 | rc = pldm_bios_table_attr_value_entry_encode_enum_check( |
| 589 | encodeEntry.data(), encodeEntry.size(), 0, |
| 590 | PLDM_BIOS_ENUMERATION_READ_ONLY, 2, handles); |
| 591 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 592 | EXPECT_EQ(encodeEntry, enumEntry); |
| 593 | rc = pldm_bios_table_attr_value_entry_encode_enum_check( |
| 594 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_PASSWORD, 2, |
| 595 | handles); |
| 596 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 597 | rc = pldm_bios_table_attr_value_entry_encode_enum_check( |
| 598 | encodeEntry.data(), encodeEntry.size() - 1, 0, PLDM_BIOS_ENUMERATION, 2, |
| 599 | handles); |
| 600 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 601 | } |
| 602 | |
| 603 | TEST(AttrValTable, EnumEntryDecodeTest) |
| 604 | { |
| 605 | std::vector<uint8_t> enumEntry{ |
| 606 | 0, 0, /* attr handle */ |
| 607 | 0, /* attr type */ |
| 608 | 2, /* number of current value */ |
| 609 | 0, /* current value string handle index */ |
| 610 | 1, /* current value string handle index */ |
| 611 | }; |
| 612 | |
| 613 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 614 | enumEntry.data()); |
| 615 | auto number = pldm_bios_table_attr_value_entry_enum_decode_number(entry); |
| 616 | EXPECT_EQ(2, number); |
| 617 | |
| 618 | std::vector<uint8_t> handles(2, 0); |
| 619 | auto rc = pldm_bios_table_attr_value_entry_enum_decode_handles( |
| 620 | entry, handles.data(), handles.size()); |
| 621 | EXPECT_EQ(rc, 2); |
| 622 | EXPECT_EQ(handles[0], 0); |
| 623 | EXPECT_EQ(handles[1], 1); |
| 624 | } |
| 625 | |
| 626 | TEST(AttrValTable, stringEntryEncodeTest) |
| 627 | { |
| 628 | std::vector<uint8_t> stringEntry{ |
| 629 | 0, 0, /* attr handle */ |
| 630 | 1, /* attr type */ |
| 631 | 3, 0, /* current string length */ |
| 632 | 'a', 'b', 'c', /* defaut value string handle index */ |
| 633 | }; |
| 634 | |
| 635 | auto length = pldm_bios_table_attr_value_entry_encode_string_length(3); |
| 636 | EXPECT_EQ(length, stringEntry.size()); |
| 637 | std::vector<uint8_t> encodeEntry(length, 0); |
| 638 | pldm_bios_table_attr_value_entry_encode_string( |
| 639 | encodeEntry.data(), encodeEntry.size(), 0, 1, 3, "abc"); |
| 640 | EXPECT_EQ(encodeEntry, stringEntry); |
| 641 | |
| 642 | EXPECT_DEATH( |
| 643 | pldm_bios_table_attr_value_entry_encode_string( |
| 644 | encodeEntry.data(), encodeEntry.size() - 1, 0, 1, 3, "abc"), |
| 645 | "length <= entry_length"); |
| 646 | |
| 647 | auto rc = pldm_bios_table_attr_value_entry_encode_string_check( |
| 648 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_STRING, 3, "abc"); |
| 649 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 650 | EXPECT_EQ(encodeEntry, stringEntry); |
| 651 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 652 | stringEntry.data()); |
| 653 | entry->attr_type = PLDM_BIOS_STRING_READ_ONLY; |
| 654 | rc = pldm_bios_table_attr_value_entry_encode_string_check( |
| 655 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_STRING_READ_ONLY, |
| 656 | 3, "abc"); |
| 657 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 658 | EXPECT_EQ(encodeEntry, stringEntry); |
| 659 | rc = pldm_bios_table_attr_value_entry_encode_string_check( |
| 660 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_PASSWORD, 3, |
| 661 | "abc"); |
| 662 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 663 | rc = pldm_bios_table_attr_value_entry_encode_string_check( |
| 664 | encodeEntry.data(), encodeEntry.size() - 1, 0, PLDM_BIOS_STRING, 3, |
| 665 | "abc"); |
| 666 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 667 | } |
| 668 | |
| 669 | TEST(AttrValTable, StringEntryDecodeTest) |
| 670 | { |
| 671 | std::vector<uint8_t> stringEntry{ |
| 672 | 0, 0, /* attr handle */ |
| 673 | 1, /* attr type */ |
| 674 | 3, 0, /* current string length */ |
| 675 | 'a', 'b', 'c', /* defaut value string handle index */ |
| 676 | }; |
| 677 | |
| 678 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 679 | stringEntry.data()); |
| 680 | auto length = pldm_bios_table_attr_value_entry_string_decode_length(entry); |
| 681 | EXPECT_EQ(3, length); |
| 682 | |
| 683 | auto handle = pldm_bios_table_attr_value_entry_decode_handle(entry); |
| 684 | EXPECT_EQ(0, handle); |
| 685 | |
| 686 | auto entryLength = pldm_bios_table_attr_value_entry_length(entry); |
| 687 | EXPECT_EQ(stringEntry.size(), entryLength); |
| 688 | |
| 689 | variable_field currentString{}; |
| 690 | pldm_bios_table_attr_value_entry_string_decode_string(entry, |
| 691 | ¤tString); |
| 692 | EXPECT_THAT(std::vector<uint8_t>(currentString.ptr, |
| 693 | currentString.ptr + currentString.length), |
| 694 | ElementsAreArray(std::vector<uint8_t>{'a', 'b', 'c'})); |
| 695 | } |
| 696 | |
| 697 | TEST(AttrValTable, integerEntryEncodeTest) |
| 698 | { |
| 699 | std::vector<uint8_t> integerEntry{ |
| 700 | 0, 0, /* attr handle */ |
| 701 | 3, /* attr type */ |
| 702 | 10, 0, 0, 0, 0, 0, 0, 0, /* current value */ |
| 703 | }; |
| 704 | |
| 705 | auto length = pldm_bios_table_attr_value_entry_encode_integer_length(); |
| 706 | EXPECT_EQ(length, integerEntry.size()); |
| 707 | std::vector<uint8_t> encodeEntry(length, 0); |
| 708 | pldm_bios_table_attr_value_entry_encode_integer( |
| 709 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_INTEGER, 10); |
| 710 | EXPECT_EQ(encodeEntry, integerEntry); |
| 711 | |
| 712 | EXPECT_DEATH(pldm_bios_table_attr_value_entry_encode_integer( |
| 713 | encodeEntry.data(), encodeEntry.size() - 1, 0, |
| 714 | PLDM_BIOS_INTEGER, 10), |
| 715 | "length <= entry_length"); |
| 716 | |
| 717 | auto rc = pldm_bios_table_attr_value_entry_encode_integer_check( |
| 718 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_INTEGER, 10); |
| 719 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 720 | EXPECT_EQ(encodeEntry, integerEntry); |
| 721 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 722 | integerEntry.data()); |
| 723 | entry->attr_type = PLDM_BIOS_INTEGER_READ_ONLY; |
| 724 | rc = pldm_bios_table_attr_value_entry_encode_integer_check( |
| 725 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_INTEGER_READ_ONLY, |
| 726 | 10); |
| 727 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 728 | EXPECT_EQ(encodeEntry, integerEntry); |
| 729 | |
| 730 | rc = pldm_bios_table_attr_value_entry_encode_integer_check( |
| 731 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_PASSWORD, 10); |
| 732 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 733 | rc = pldm_bios_table_attr_value_entry_encode_integer_check( |
| 734 | encodeEntry.data(), encodeEntry.size() - 1, 0, |
| 735 | PLDM_BIOS_INTEGER_READ_ONLY, 10); |
| 736 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 737 | } |
| 738 | |
| 739 | TEST(AttrValTable, integerEntryDecodeTest) |
| 740 | { |
| 741 | std::vector<uint8_t> integerEntry{ |
| 742 | 0, 0, /* attr handle */ |
| 743 | 3, /* attr type */ |
| 744 | 10, 0, 0, 0, 0, 0, 0, 0, /* current value */ |
| 745 | }; |
| 746 | |
| 747 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 748 | integerEntry.data()); |
| 749 | auto cv = pldm_bios_table_attr_value_entry_integer_decode_cv(entry); |
| 750 | EXPECT_EQ(cv, 10u); |
| 751 | } |
| 752 | |
| 753 | TEST(AttrValTable, IteratorTest) |
| 754 | { |
| 755 | std::vector<uint8_t> enumEntry{ |
| 756 | 0, 0, /* attr handle */ |
| 757 | 0, /* attr type */ |
| 758 | 2, /* number of current value */ |
| 759 | 0, /* current value string handle index */ |
| 760 | 1, /* current value string handle index */ |
| 761 | }; |
| 762 | std::vector<uint8_t> stringEntry{ |
| 763 | 0, 0, /* attr handle */ |
| 764 | 1, /* attr type */ |
| 765 | 3, 0, /* current string length */ |
| 766 | 'a', 'b', 'c', /* defaut value string handle index */ |
| 767 | }; |
| 768 | std::vector<uint8_t> integerEntry{ |
| 769 | 0, 0, /* attr handle */ |
| 770 | 3, /* attr type */ |
| 771 | 10, 0, 0, 0, 0, 0, 0, 0, /* current value */ |
| 772 | }; |
| 773 | |
| 774 | Table table; |
| 775 | buildTable(table, enumEntry, stringEntry, integerEntry, enumEntry); |
| 776 | |
| 777 | auto iter = pldm_bios_table_iter_create(table.data(), table.size(), |
| 778 | PLDM_BIOS_ATTR_VAL_TABLE); |
| 779 | auto entry = pldm_bios_table_iter_attr_value_entry_value(iter); |
| 780 | |
| 781 | auto p = reinterpret_cast<const uint8_t*>(entry); |
| 782 | EXPECT_THAT(std::vector<uint8_t>(p, p + enumEntry.size()), |
| 783 | ElementsAreArray(enumEntry)); |
| 784 | |
| 785 | pldm_bios_table_iter_next(iter); |
| 786 | entry = pldm_bios_table_iter_attr_value_entry_value(iter); |
| 787 | p = reinterpret_cast<const uint8_t*>(entry); |
| 788 | EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()), |
| 789 | ElementsAreArray(stringEntry)); |
| 790 | |
| 791 | pldm_bios_table_iter_next(iter); |
| 792 | entry = pldm_bios_table_iter_attr_value_entry_value(iter); |
| 793 | p = reinterpret_cast<const uint8_t*>(entry); |
| 794 | EXPECT_THAT(std::vector<uint8_t>(p, p + integerEntry.size()), |
| 795 | ElementsAreArray(integerEntry)); |
| 796 | |
| 797 | pldm_bios_table_iter_next(iter); |
| 798 | entry = pldm_bios_table_iter_attr_value_entry_value(iter); |
| 799 | p = reinterpret_cast<const uint8_t*>(entry); |
| 800 | EXPECT_THAT(std::vector<uint8_t>(p, p + enumEntry.size()), |
| 801 | ElementsAreArray(enumEntry)); |
| 802 | |
| 803 | pldm_bios_table_iter_next(iter); |
| 804 | EXPECT_TRUE(pldm_bios_table_iter_is_end(iter)); |
| 805 | |
| 806 | pldm_bios_table_iter_free(iter); |
| 807 | } |
| 808 | |
| 809 | TEST(AttrValTable, FindTest) |
| 810 | { |
| 811 | std::vector<uint8_t> enumEntry{ |
| 812 | 0, 0, /* attr handle */ |
| 813 | 0, /* attr type */ |
| 814 | 2, /* number of current value */ |
| 815 | 0, /* current value string handle index */ |
| 816 | 1, /* current value string handle index */ |
| 817 | }; |
| 818 | std::vector<uint8_t> stringEntry{ |
| 819 | 1, 0, /* attr handle */ |
| 820 | 1, /* attr type */ |
| 821 | 3, 0, /* current string length */ |
| 822 | 'a', 'b', 'c', /* defaut value string handle index */ |
| 823 | }; |
| 824 | std::vector<uint8_t> integerEntry{ |
| 825 | 2, 0, /* attr handle */ |
| 826 | 3, /* attr type */ |
| 827 | 10, 0, 0, 0, 0, 0, 0, 0, /* current value */ |
| 828 | }; |
| 829 | |
| 830 | Table table; |
| 831 | buildTable(table, enumEntry, stringEntry, integerEntry); |
| 832 | |
| 833 | auto entry = pldm_bios_table_attr_value_find_by_handle(table.data(), |
| 834 | table.size(), 1); |
| 835 | EXPECT_NE(entry, nullptr); |
| 836 | auto p = reinterpret_cast<const uint8_t*>(entry); |
| 837 | EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()), |
| 838 | ElementsAreArray(stringEntry)); |
| 839 | |
| 840 | entry = pldm_bios_table_attr_value_find_by_handle(table.data(), |
| 841 | table.size(), 3); |
| 842 | EXPECT_EQ(entry, nullptr); |
| 843 | |
| 844 | auto firstEntry = |
| 845 | reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(table.data()); |
| 846 | firstEntry->attr_type = PLDM_BIOS_PASSWORD; |
| 847 | EXPECT_DEATH(pldm_bios_table_attr_value_find_by_handle(table.data(), |
| 848 | table.size(), 1), |
| 849 | "entry_length != NULL"); |
| 850 | } |
| 851 | |
| 852 | TEST(AttrValTable, CopyAndUpdateTest) |
| 853 | { |
| 854 | std::vector<uint8_t> enumEntry{ |
| 855 | 0, 0, /* attr handle */ |
| 856 | 0, /* attr type */ |
| 857 | 2, /* number of current value */ |
| 858 | 0, /* current value string handle index */ |
| 859 | 1, /* current value string handle index */ |
| 860 | }; |
| 861 | std::vector<uint8_t> stringEntry{ |
| 862 | 1, 0, /* attr handle */ |
| 863 | 1, /* attr type */ |
| 864 | 3, 0, /* current string length */ |
| 865 | 'a', 'b', 'c', /* defaut value string handle index */ |
| 866 | }; |
| 867 | std::vector<uint8_t> integerEntry{ |
| 868 | 2, 0, /* attr handle */ |
| 869 | 3, /* attr type */ |
| 870 | 10, 0, 0, 0, 0, 0, 0, 0, /* current value */ |
| 871 | }; |
| 872 | |
| 873 | Table srcTable; |
| 874 | buildTable(srcTable, enumEntry, stringEntry, integerEntry); |
| 875 | |
| 876 | std::vector<uint8_t> stringEntry1{ |
| 877 | 1, 0, /* attr handle */ |
| 878 | 1, /* attr type */ |
| 879 | 3, 0, /* current string length */ |
| 880 | 'd', 'e', 'f', /* defaut value string handle index */ |
| 881 | }; |
| 882 | |
| 883 | Table expectTable; |
| 884 | buildTable(expectTable, enumEntry, stringEntry1, integerEntry); |
| 885 | Table destTable(expectTable.size() + 10); |
| 886 | auto destLength = destTable.size(); |
| 887 | auto rc = pldm_bios_table_attr_value_copy_and_update( |
| 888 | srcTable.data(), srcTable.size(), destTable.data(), &destLength, |
| 889 | stringEntry1.data(), stringEntry1.size()); |
| 890 | |
| 891 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 892 | EXPECT_EQ(destLength, expectTable.size()); |
| 893 | destTable.resize(destLength); |
| 894 | EXPECT_THAT(destTable, ElementsAreArray(expectTable)); |
| 895 | |
| 896 | std::vector<uint8_t> stringEntry2{ |
| 897 | 1, 0, /* attr handle */ |
| 898 | 1, /* attr type */ |
| 899 | 5, 0, /* current string length */ |
| 900 | 'd', 'e', 'f', 'a', 'b', /* defaut value string handle index */ |
| 901 | }; |
| 902 | expectTable.resize(0); |
| 903 | buildTable(expectTable, enumEntry, stringEntry2, integerEntry); |
| 904 | destTable.resize(expectTable.size() + 10); |
| 905 | destLength = destTable.size(); |
| 906 | rc = pldm_bios_table_attr_value_copy_and_update( |
| 907 | srcTable.data(), srcTable.size(), destTable.data(), &destLength, |
| 908 | stringEntry2.data(), stringEntry2.size()); |
| 909 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 910 | EXPECT_EQ(destLength, expectTable.size()); |
| 911 | destTable.resize(destLength); |
| 912 | EXPECT_THAT(destTable, ElementsAreArray(expectTable)); |
| 913 | |
| 914 | std::vector<uint8_t> stringEntry3{ |
| 915 | 1, 0, /* attr handle */ |
| 916 | 1, /* attr type */ |
| 917 | 1, 0, /* current string length */ |
| 918 | 'd', /* defaut value string handle index */ |
| 919 | }; |
| 920 | expectTable.resize(0); |
| 921 | buildTable(expectTable, enumEntry, stringEntry3, integerEntry); |
| 922 | destTable.resize(expectTable.size() + 10); |
| 923 | destLength = destTable.size(); |
| 924 | rc = pldm_bios_table_attr_value_copy_and_update( |
| 925 | srcTable.data(), srcTable.size(), destTable.data(), &destLength, |
| 926 | stringEntry3.data(), stringEntry3.size()); |
| 927 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 928 | EXPECT_EQ(destLength, expectTable.size()); |
| 929 | destTable.resize(destLength); |
| 930 | EXPECT_THAT(destTable, ElementsAreArray(expectTable)); |
| 931 | |
| 932 | stringEntry3[2] = PLDM_BIOS_INTEGER; // set attribute type to integer |
| 933 | rc = pldm_bios_table_attr_value_copy_and_update( |
| 934 | srcTable.data(), srcTable.size(), destTable.data(), &destLength, |
| 935 | stringEntry3.data(), stringEntry3.size()); |
| 936 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 937 | stringEntry3[2] = PLDM_BIOS_STRING; // set attribute type to string |
| 938 | |
| 939 | destTable.resize(expectTable.size() - 1); |
| 940 | destLength = destTable.size(); |
| 941 | rc = pldm_bios_table_attr_value_copy_and_update( |
| 942 | srcTable.data(), srcTable.size(), destTable.data(), &destLength, |
| 943 | stringEntry3.data(), stringEntry3.size()); |
| 944 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 945 | } |
| 946 | |
| 947 | TEST(StringTable, EntryEncodeTest) |
| 948 | { |
| 949 | std::vector<uint8_t> stringEntry{ |
| 950 | 0, 0, /* string handle*/ |
| 951 | 7, 0, /* string length */ |
| 952 | 'A', 'l', 'l', 'o', 'w', 'e', 'd', /* string */ |
| 953 | }; |
| 954 | |
| 955 | const char* str = "Allowed"; |
| 956 | auto str_length = std::strlen(str); |
| 957 | auto encodeLength = pldm_bios_table_string_entry_encode_length(str_length); |
| 958 | EXPECT_EQ(encodeLength, stringEntry.size()); |
| 959 | |
| 960 | std::vector<uint8_t> encodeEntry(encodeLength, 0); |
| 961 | pldm_bios_table_string_entry_encode(encodeEntry.data(), encodeEntry.size(), |
| 962 | str, str_length); |
| 963 | // set string handle = 0 |
| 964 | encodeEntry[0] = 0; |
| 965 | encodeEntry[1] = 0; |
| 966 | |
| 967 | EXPECT_EQ(stringEntry, encodeEntry); |
| 968 | |
| 969 | EXPECT_DEATH(pldm_bios_table_string_entry_encode(encodeEntry.data(), |
| 970 | encodeEntry.size() - 1, |
| 971 | str, str_length), |
| 972 | "length <= entry_length"); |
| 973 | auto rc = pldm_bios_table_string_entry_encode_check( |
| 974 | encodeEntry.data(), encodeEntry.size() - 1, str, str_length); |
| 975 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 976 | } |
| 977 | |
| 978 | TEST(StringTable, EntryDecodeTest) |
| 979 | { |
| 980 | std::vector<uint8_t> stringEntry{ |
| 981 | 4, 0, /* string handle*/ |
| 982 | 7, 0, /* string length */ |
| 983 | 'A', 'l', 'l', 'o', 'w', 'e', 'd', /* string */ |
| 984 | }; |
| 985 | auto entry = reinterpret_cast<struct pldm_bios_string_table_entry*>( |
| 986 | stringEntry.data()); |
| 987 | auto handle = pldm_bios_table_string_entry_decode_handle(entry); |
| 988 | EXPECT_EQ(handle, 4); |
| 989 | auto strLength = pldm_bios_table_string_entry_decode_string_length(entry); |
| 990 | EXPECT_EQ(strLength, 7); |
| 991 | |
| 992 | std::vector<char> buffer(strLength + 1, 0); |
| 993 | auto decodedLength = pldm_bios_table_string_entry_decode_string( |
| 994 | entry, buffer.data(), buffer.size()); |
| 995 | EXPECT_EQ(decodedLength, strLength); |
| 996 | EXPECT_EQ(std::strcmp("Allowed", buffer.data()), 0); |
| 997 | decodedLength = pldm_bios_table_string_entry_decode_string( |
| 998 | entry, buffer.data(), 2 + 1 /* sizeof '\0'*/); |
| 999 | EXPECT_EQ(decodedLength, 2); |
| 1000 | EXPECT_EQ(std::strcmp("Al", buffer.data()), 0); |
| 1001 | |
| 1002 | auto rc = pldm_bios_table_string_entry_decode_string_check( |
| 1003 | entry, buffer.data(), buffer.size()); |
| 1004 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1005 | EXPECT_EQ(std::strcmp("Allowed", buffer.data()), 0); |
| 1006 | |
| 1007 | rc = pldm_bios_table_string_entry_decode_string_check(entry, buffer.data(), |
| 1008 | buffer.size() - 1); |
| 1009 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1010 | } |
| 1011 | |
| 1012 | TEST(StringTable, IteratorTest) |
| 1013 | { |
| 1014 | std::vector<uint8_t> stringHello{ |
| 1015 | 0, 0, /* string handle*/ |
| 1016 | 5, 0, /* string length */ |
| 1017 | 'H', 'e', 'l', 'l', 'o', /* string */ |
| 1018 | }; |
| 1019 | std::vector<uint8_t> stringWorld{ |
| 1020 | 1, 0, /* string handle*/ |
| 1021 | 6, 0, /* string length */ |
| 1022 | 'W', 'o', 'r', 'l', 'd', '!', /* string */ |
| 1023 | }; |
| 1024 | |
| 1025 | Table table; |
| 1026 | buildTable(table, stringHello, stringWorld); |
| 1027 | |
| 1028 | auto iter = pldm_bios_table_iter_create(table.data(), table.size(), |
| 1029 | PLDM_BIOS_STRING_TABLE); |
| 1030 | auto entry = pldm_bios_table_iter_string_entry_value(iter); |
| 1031 | auto rc = std::memcmp(entry, stringHello.data(), stringHello.size()); |
| 1032 | EXPECT_EQ(rc, 0); |
| 1033 | pldm_bios_table_iter_next(iter); |
| 1034 | entry = pldm_bios_table_iter_string_entry_value(iter); |
| 1035 | rc = std::memcmp(entry, stringWorld.data(), stringWorld.size()); |
| 1036 | EXPECT_EQ(rc, 0); |
| 1037 | pldm_bios_table_iter_next(iter); |
| 1038 | EXPECT_TRUE(pldm_bios_table_iter_is_end(iter)); |
| 1039 | pldm_bios_table_iter_free(iter); |
| 1040 | } |
| 1041 | |
| 1042 | TEST(StringTable, FindTest) |
| 1043 | { |
| 1044 | std::vector<uint8_t> stringHello{ |
| 1045 | 1, 0, /* string handle*/ |
| 1046 | 5, 0, /* string length */ |
| 1047 | 'H', 'e', 'l', 'l', 'o', /* string */ |
| 1048 | }; |
| 1049 | std::vector<uint8_t> stringWorld{ |
| 1050 | 2, 0, /* string handle*/ |
| 1051 | 6, 0, /* string length */ |
| 1052 | 'W', 'o', 'r', 'l', 'd', '!', /* string */ |
| 1053 | }; |
| 1054 | std::vector<uint8_t> stringHi{ |
| 1055 | 3, 0, /* string handle*/ |
| 1056 | 2, 0, /* string length */ |
| 1057 | 'H', 'i', /* string */ |
| 1058 | }; |
| 1059 | |
| 1060 | Table table; |
| 1061 | buildTable(table, stringHello, stringWorld, stringHi); |
| 1062 | |
| 1063 | auto entry = pldm_bios_table_string_find_by_string(table.data(), |
| 1064 | table.size(), "World!"); |
| 1065 | EXPECT_NE(entry, nullptr); |
| 1066 | auto handle = pldm_bios_table_string_entry_decode_handle(entry); |
| 1067 | EXPECT_EQ(handle, 2); |
| 1068 | |
| 1069 | entry = pldm_bios_table_string_find_by_string(table.data(), table.size(), |
| 1070 | "Worl"); |
| 1071 | EXPECT_EQ(entry, nullptr); |
| 1072 | |
| 1073 | entry = |
| 1074 | pldm_bios_table_string_find_by_handle(table.data(), table.size(), 3); |
| 1075 | EXPECT_NE(entry, nullptr); |
| 1076 | auto str_length = pldm_bios_table_string_entry_decode_string_length(entry); |
| 1077 | EXPECT_EQ(str_length, 2); |
| 1078 | std::vector<char> strBuf(str_length + 1, 0); |
| 1079 | auto rc = pldm_bios_table_string_entry_decode_string_check( |
| 1080 | entry, strBuf.data(), strBuf.size()); |
| 1081 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1082 | EXPECT_EQ(std::strcmp("Hi", strBuf.data()), 0); |
| 1083 | |
| 1084 | entry = |
| 1085 | pldm_bios_table_string_find_by_handle(table.data(), table.size(), 4); |
| 1086 | EXPECT_EQ(entry, nullptr); |
| 1087 | } |
| 1088 | |
| 1089 | TEST(Itearator, DeathTest) |
| 1090 | { |
| 1091 | |
| 1092 | Table table(256, 0); |
| 1093 | |
| 1094 | /* first entry */ |
| 1095 | auto attr_entry = |
| 1096 | reinterpret_cast<struct pldm_bios_attr_table_entry*>(table.data()); |
| 1097 | auto iter = pldm_bios_table_iter_create(table.data(), table.size(), |
| 1098 | PLDM_BIOS_ATTR_TABLE); |
| 1099 | attr_entry->attr_type = PLDM_BIOS_PASSWORD; |
| 1100 | EXPECT_DEATH(pldm_bios_table_iter_next(iter), "attr_table_entry != NULL"); |
| 1101 | pldm_bios_table_iter_free(iter); |
| 1102 | } |
| 1103 | |
| 1104 | TEST(PadAndChecksum, PadAndChecksum) |
| 1105 | { |
| 1106 | EXPECT_EQ(4u, pldm_bios_table_pad_checksum_size(0)); |
| 1107 | EXPECT_EQ(7u, pldm_bios_table_pad_checksum_size(1)); |
| 1108 | EXPECT_EQ(6u, pldm_bios_table_pad_checksum_size(2)); |
| 1109 | EXPECT_EQ(5u, pldm_bios_table_pad_checksum_size(3)); |
| 1110 | EXPECT_EQ(4u, pldm_bios_table_pad_checksum_size(4)); |
| 1111 | |
| 1112 | // The table is borrowed from |
| 1113 | // https://github.com/openbmc/pldm/commit/69d3e7fb2d9935773f4fbf44326c33f3fc0a3c38 |
| 1114 | // refer to the commit message |
| 1115 | Table attrValTable = {0x09, 0x00, 0x01, 0x02, 0x00, 0x65, 0x66}; |
| 1116 | auto sizeWithoutPad = attrValTable.size(); |
| 1117 | attrValTable.resize(sizeWithoutPad + |
| 1118 | pldm_bios_table_pad_checksum_size(sizeWithoutPad)); |
| 1119 | pldm_bios_table_append_pad_checksum(attrValTable.data(), |
| 1120 | attrValTable.size(), sizeWithoutPad); |
| 1121 | Table expectedTable = {0x09, 0x00, 0x01, 0x02, 0x00, 0x65, |
| 1122 | 0x66, 0x00, 0x6d, 0x81, 0x4a, 0xb6}; |
| 1123 | EXPECT_EQ(attrValTable, expectedTable); |
| 1124 | } |
| 1125 | |
| 1126 | TEST(BIOSTableChecksum, testBIOSTableChecksum) |
| 1127 | { |
| 1128 | std::vector<uint8_t> stringTable{ |
| 1129 | 1, 0, /* string handle*/ |
| 1130 | 5, 0, /* string length */ |
| 1131 | 'T', 'a', 'b', 'l', 'e', /* string */ |
| 1132 | }; |
| 1133 | |
| 1134 | buildTable(stringTable); |
| 1135 | |
| 1136 | EXPECT_EQ(true, |
| 1137 | pldm_bios_table_checksum(stringTable.data(), stringTable.size())); |
| 1138 | } |