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 | |
Andrew Jeffery | fbe61d7 | 2023-04-05 20:28:23 +0930 | [diff] [blame] | 409 | uint64_t lower; |
| 410 | uint64_t upper; |
| 411 | uint64_t def; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 412 | uint32_t scalar; |
| 413 | auto entry = reinterpret_cast<struct pldm_bios_attr_table_entry*>( |
| 414 | integerEntry.data()); |
| 415 | pldm_bios_table_attr_entry_integer_decode(entry, &lower, &upper, &scalar, |
| 416 | &def); |
| 417 | EXPECT_EQ(lower, 1u); |
| 418 | EXPECT_EQ(upper, 10u); |
| 419 | EXPECT_EQ(scalar, 2u); |
| 420 | EXPECT_EQ(def, 3u); |
| 421 | } |
| 422 | |
| 423 | TEST(AttrTable, ItearatorTest) |
| 424 | { |
| 425 | std::vector<uint8_t> enumEntry{ |
| 426 | 0, 0, /* attr handle */ |
| 427 | 0, /* attr type */ |
| 428 | 1, 0, /* attr name handle */ |
| 429 | 2, /* number of possible value */ |
| 430 | 2, 0, /* possible value handle */ |
| 431 | 3, 0, /* possible value handle */ |
| 432 | 1, /* number of default value */ |
| 433 | 0 /* defaut value string handle index */ |
| 434 | }; |
| 435 | std::vector<uint8_t> stringEntry{ |
| 436 | 1, 0, /* attr handle */ |
| 437 | 1, /* attr type */ |
| 438 | 12, 0, /* attr name handle */ |
| 439 | 1, /* string type */ |
| 440 | 1, 0, /* minimum length of the string in bytes */ |
| 441 | 100, 0, /* maximum length of the string in bytes */ |
| 442 | 3, 0, /* length of default string in length */ |
| 443 | 'a', 'b', 'c' /* default string */ |
| 444 | }; |
| 445 | std::vector<uint8_t> integerEntry{ |
| 446 | 0, 0, /* attr handle */ |
| 447 | 3, /* attr type */ |
| 448 | 1, 0, /* attr name handle */ |
| 449 | 1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */ |
| 450 | 10, 0, 0, 0, 0, 0, 0, 0, /* upper bound */ |
| 451 | 2, 0, 0, 0, /* scalar increment */ |
| 452 | 3, 0, 0, 0, 0, 0, 0, 0, /* defaut value */ |
| 453 | }; |
| 454 | |
| 455 | Table table; |
| 456 | buildTable(table, enumEntry, stringEntry, integerEntry, enumEntry); |
| 457 | auto iter = pldm_bios_table_iter_create(table.data(), table.size(), |
| 458 | PLDM_BIOS_ATTR_TABLE); |
| 459 | auto entry = pldm_bios_table_iter_attr_entry_value(iter); |
| 460 | auto rc = std::memcmp(entry, enumEntry.data(), enumEntry.size()); |
| 461 | EXPECT_EQ(rc, 0); |
| 462 | |
| 463 | pldm_bios_table_iter_next(iter); |
| 464 | entry = pldm_bios_table_iter_attr_entry_value(iter); |
| 465 | rc = std::memcmp(entry, stringEntry.data(), stringEntry.size()); |
| 466 | EXPECT_EQ(rc, 0); |
| 467 | |
| 468 | pldm_bios_table_iter_next(iter); |
| 469 | entry = pldm_bios_table_iter_attr_entry_value(iter); |
| 470 | rc = std::memcmp(entry, integerEntry.data(), integerEntry.size()); |
| 471 | EXPECT_EQ(rc, 0); |
| 472 | |
| 473 | pldm_bios_table_iter_next(iter); |
| 474 | entry = pldm_bios_table_iter_attr_entry_value(iter); |
| 475 | rc = std::memcmp(entry, enumEntry.data(), enumEntry.size()); |
| 476 | EXPECT_EQ(rc, 0); |
| 477 | |
| 478 | pldm_bios_table_iter_next(iter); |
| 479 | EXPECT_TRUE(pldm_bios_table_iter_is_end(iter)); |
| 480 | pldm_bios_table_iter_free(iter); |
| 481 | } |
| 482 | |
| 483 | TEST(AttrTable, FindTest) |
| 484 | { |
| 485 | std::vector<uint8_t> enumEntry{ |
| 486 | 0, 0, /* attr handle */ |
| 487 | 0, /* attr type */ |
| 488 | 1, 0, /* attr name handle */ |
| 489 | 2, /* number of possible value */ |
| 490 | 2, 0, /* possible value handle */ |
| 491 | 3, 0, /* possible value handle */ |
| 492 | 1, /* number of default value */ |
| 493 | 0 /* defaut value string handle index */ |
| 494 | }; |
| 495 | std::vector<uint8_t> stringEntry{ |
| 496 | 1, 0, /* attr handle */ |
| 497 | 1, /* attr type */ |
| 498 | 2, 0, /* attr name handle */ |
| 499 | 1, /* string type */ |
| 500 | 1, 0, /* minimum length of the string in bytes */ |
| 501 | 100, 0, /* maximum length of the string in bytes */ |
| 502 | 3, 0, /* length of default string in length */ |
| 503 | 'a', 'b', 'c' /* default string */ |
| 504 | }; |
| 505 | std::vector<uint8_t> integerEntry{ |
| 506 | 0, 0, /* attr handle */ |
| 507 | 3, /* attr type */ |
| 508 | 3, 0, /* attr name handle */ |
| 509 | 1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */ |
| 510 | 10, 0, 0, 0, 0, 0, 0, 0, /* upper bound */ |
| 511 | 2, 0, 0, 0, /* scalar increment */ |
| 512 | 3, 0, 0, 0, 0, 0, 0, 0, /* defaut value */ |
| 513 | }; |
| 514 | |
| 515 | Table table; |
| 516 | buildTable(table, enumEntry, stringEntry, integerEntry, enumEntry); |
| 517 | |
| 518 | auto entry = |
| 519 | pldm_bios_table_attr_find_by_handle(table.data(), table.size(), 1); |
| 520 | EXPECT_NE(entry, nullptr); |
| 521 | auto p = reinterpret_cast<const uint8_t*>(entry); |
| 522 | EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()), |
| 523 | ElementsAreArray(stringEntry)); |
| 524 | |
| 525 | entry = pldm_bios_table_attr_find_by_handle(table.data(), table.size(), 3); |
| 526 | EXPECT_EQ(entry, nullptr); |
| 527 | |
| 528 | entry = pldm_bios_table_attr_find_by_string_handle(table.data(), |
| 529 | table.size(), 2); |
| 530 | EXPECT_NE(entry, nullptr); |
| 531 | p = reinterpret_cast<const uint8_t*>(entry); |
| 532 | EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()), |
| 533 | ElementsAreArray(stringEntry)); |
| 534 | |
| 535 | entry = pldm_bios_table_attr_find_by_string_handle(table.data(), |
| 536 | table.size(), 4); |
| 537 | EXPECT_EQ(entry, nullptr); |
| 538 | } |
| 539 | |
| 540 | TEST(AttrValTable, HeaderDecodeTest) |
| 541 | { |
| 542 | std::vector<uint8_t> enumEntry{ |
| 543 | 1, 0, /* attr handle */ |
| 544 | 0, /* attr type */ |
| 545 | 2, /* number of current value */ |
| 546 | 0, /* current value string handle index */ |
| 547 | 1, /* current value string handle index */ |
| 548 | }; |
| 549 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 550 | enumEntry.data()); |
| 551 | auto attrHandle = |
| 552 | pldm_bios_table_attr_value_entry_decode_attribute_handle(entry); |
| 553 | EXPECT_EQ(attrHandle, 1); |
| 554 | auto attrType = |
| 555 | pldm_bios_table_attr_value_entry_decode_attribute_type(entry); |
| 556 | EXPECT_EQ(attrType, 0); |
| 557 | } |
| 558 | |
| 559 | TEST(AttrValTable, EnumEntryEncodeTest) |
| 560 | { |
| 561 | std::vector<uint8_t> enumEntry{ |
| 562 | 0, 0, /* attr handle */ |
| 563 | 0, /* attr type */ |
| 564 | 2, /* number of current value */ |
| 565 | 0, /* current value string handle index */ |
| 566 | 1, /* current value string handle index */ |
| 567 | }; |
| 568 | |
| 569 | auto length = pldm_bios_table_attr_value_entry_encode_enum_length(2); |
| 570 | EXPECT_EQ(length, enumEntry.size()); |
| 571 | std::vector<uint8_t> encodeEntry(length, 0); |
| 572 | uint8_t handles[] = {0, 1}; |
| 573 | pldm_bios_table_attr_value_entry_encode_enum( |
| 574 | encodeEntry.data(), encodeEntry.size(), 0, 0, 2, handles); |
| 575 | EXPECT_EQ(encodeEntry, enumEntry); |
| 576 | |
| 577 | EXPECT_DEATH( |
| 578 | pldm_bios_table_attr_value_entry_encode_enum( |
| 579 | encodeEntry.data(), encodeEntry.size() - 1, 0, 0, 2, handles), |
| 580 | "length <= entry_length"); |
| 581 | |
| 582 | auto rc = pldm_bios_table_attr_value_entry_encode_enum_check( |
| 583 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_ENUMERATION, 2, |
| 584 | handles); |
| 585 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 586 | EXPECT_EQ(encodeEntry, enumEntry); |
| 587 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 588 | enumEntry.data()); |
| 589 | entry->attr_type = PLDM_BIOS_ENUMERATION_READ_ONLY; |
| 590 | rc = pldm_bios_table_attr_value_entry_encode_enum_check( |
| 591 | encodeEntry.data(), encodeEntry.size(), 0, |
| 592 | PLDM_BIOS_ENUMERATION_READ_ONLY, 2, handles); |
| 593 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 594 | EXPECT_EQ(encodeEntry, enumEntry); |
| 595 | rc = pldm_bios_table_attr_value_entry_encode_enum_check( |
| 596 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_PASSWORD, 2, |
| 597 | handles); |
| 598 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 599 | rc = pldm_bios_table_attr_value_entry_encode_enum_check( |
| 600 | encodeEntry.data(), encodeEntry.size() - 1, 0, PLDM_BIOS_ENUMERATION, 2, |
| 601 | handles); |
| 602 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 603 | } |
| 604 | |
| 605 | TEST(AttrValTable, EnumEntryDecodeTest) |
| 606 | { |
| 607 | std::vector<uint8_t> enumEntry{ |
| 608 | 0, 0, /* attr handle */ |
| 609 | 0, /* attr type */ |
| 610 | 2, /* number of current value */ |
| 611 | 0, /* current value string handle index */ |
| 612 | 1, /* current value string handle index */ |
| 613 | }; |
| 614 | |
| 615 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 616 | enumEntry.data()); |
| 617 | auto number = pldm_bios_table_attr_value_entry_enum_decode_number(entry); |
| 618 | EXPECT_EQ(2, number); |
| 619 | |
| 620 | std::vector<uint8_t> handles(2, 0); |
| 621 | auto rc = pldm_bios_table_attr_value_entry_enum_decode_handles( |
| 622 | entry, handles.data(), handles.size()); |
| 623 | EXPECT_EQ(rc, 2); |
| 624 | EXPECT_EQ(handles[0], 0); |
| 625 | EXPECT_EQ(handles[1], 1); |
| 626 | } |
| 627 | |
| 628 | TEST(AttrValTable, stringEntryEncodeTest) |
| 629 | { |
| 630 | std::vector<uint8_t> stringEntry{ |
| 631 | 0, 0, /* attr handle */ |
| 632 | 1, /* attr type */ |
| 633 | 3, 0, /* current string length */ |
| 634 | 'a', 'b', 'c', /* defaut value string handle index */ |
| 635 | }; |
| 636 | |
| 637 | auto length = pldm_bios_table_attr_value_entry_encode_string_length(3); |
| 638 | EXPECT_EQ(length, stringEntry.size()); |
| 639 | std::vector<uint8_t> encodeEntry(length, 0); |
| 640 | pldm_bios_table_attr_value_entry_encode_string( |
| 641 | encodeEntry.data(), encodeEntry.size(), 0, 1, 3, "abc"); |
| 642 | EXPECT_EQ(encodeEntry, stringEntry); |
| 643 | |
| 644 | EXPECT_DEATH( |
| 645 | pldm_bios_table_attr_value_entry_encode_string( |
| 646 | encodeEntry.data(), encodeEntry.size() - 1, 0, 1, 3, "abc"), |
| 647 | "length <= entry_length"); |
| 648 | |
| 649 | auto rc = pldm_bios_table_attr_value_entry_encode_string_check( |
| 650 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_STRING, 3, "abc"); |
| 651 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 652 | EXPECT_EQ(encodeEntry, stringEntry); |
| 653 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 654 | stringEntry.data()); |
| 655 | entry->attr_type = PLDM_BIOS_STRING_READ_ONLY; |
| 656 | rc = pldm_bios_table_attr_value_entry_encode_string_check( |
| 657 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_STRING_READ_ONLY, |
| 658 | 3, "abc"); |
| 659 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 660 | EXPECT_EQ(encodeEntry, stringEntry); |
| 661 | rc = pldm_bios_table_attr_value_entry_encode_string_check( |
| 662 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_PASSWORD, 3, |
| 663 | "abc"); |
| 664 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 665 | rc = pldm_bios_table_attr_value_entry_encode_string_check( |
| 666 | encodeEntry.data(), encodeEntry.size() - 1, 0, PLDM_BIOS_STRING, 3, |
| 667 | "abc"); |
| 668 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 669 | } |
| 670 | |
| 671 | TEST(AttrValTable, StringEntryDecodeTest) |
| 672 | { |
| 673 | std::vector<uint8_t> stringEntry{ |
| 674 | 0, 0, /* attr handle */ |
| 675 | 1, /* attr type */ |
| 676 | 3, 0, /* current string length */ |
| 677 | 'a', 'b', 'c', /* defaut value string handle index */ |
| 678 | }; |
| 679 | |
| 680 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 681 | stringEntry.data()); |
| 682 | auto length = pldm_bios_table_attr_value_entry_string_decode_length(entry); |
| 683 | EXPECT_EQ(3, length); |
| 684 | |
| 685 | auto handle = pldm_bios_table_attr_value_entry_decode_handle(entry); |
| 686 | EXPECT_EQ(0, handle); |
| 687 | |
| 688 | auto entryLength = pldm_bios_table_attr_value_entry_length(entry); |
| 689 | EXPECT_EQ(stringEntry.size(), entryLength); |
| 690 | |
| 691 | variable_field currentString{}; |
| 692 | pldm_bios_table_attr_value_entry_string_decode_string(entry, |
| 693 | ¤tString); |
| 694 | EXPECT_THAT(std::vector<uint8_t>(currentString.ptr, |
| 695 | currentString.ptr + currentString.length), |
| 696 | ElementsAreArray(std::vector<uint8_t>{'a', 'b', 'c'})); |
| 697 | } |
| 698 | |
| 699 | TEST(AttrValTable, integerEntryEncodeTest) |
| 700 | { |
| 701 | std::vector<uint8_t> integerEntry{ |
| 702 | 0, 0, /* attr handle */ |
| 703 | 3, /* attr type */ |
| 704 | 10, 0, 0, 0, 0, 0, 0, 0, /* current value */ |
| 705 | }; |
| 706 | |
| 707 | auto length = pldm_bios_table_attr_value_entry_encode_integer_length(); |
| 708 | EXPECT_EQ(length, integerEntry.size()); |
| 709 | std::vector<uint8_t> encodeEntry(length, 0); |
| 710 | pldm_bios_table_attr_value_entry_encode_integer( |
| 711 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_INTEGER, 10); |
| 712 | EXPECT_EQ(encodeEntry, integerEntry); |
| 713 | |
| 714 | EXPECT_DEATH(pldm_bios_table_attr_value_entry_encode_integer( |
| 715 | encodeEntry.data(), encodeEntry.size() - 1, 0, |
| 716 | PLDM_BIOS_INTEGER, 10), |
| 717 | "length <= entry_length"); |
| 718 | |
| 719 | auto rc = pldm_bios_table_attr_value_entry_encode_integer_check( |
| 720 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_INTEGER, 10); |
| 721 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 722 | EXPECT_EQ(encodeEntry, integerEntry); |
| 723 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 724 | integerEntry.data()); |
| 725 | entry->attr_type = PLDM_BIOS_INTEGER_READ_ONLY; |
| 726 | rc = pldm_bios_table_attr_value_entry_encode_integer_check( |
| 727 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_INTEGER_READ_ONLY, |
| 728 | 10); |
| 729 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 730 | EXPECT_EQ(encodeEntry, integerEntry); |
| 731 | |
| 732 | rc = pldm_bios_table_attr_value_entry_encode_integer_check( |
| 733 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_PASSWORD, 10); |
| 734 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 735 | rc = pldm_bios_table_attr_value_entry_encode_integer_check( |
| 736 | encodeEntry.data(), encodeEntry.size() - 1, 0, |
| 737 | PLDM_BIOS_INTEGER_READ_ONLY, 10); |
| 738 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 739 | } |
| 740 | |
| 741 | TEST(AttrValTable, integerEntryDecodeTest) |
| 742 | { |
| 743 | std::vector<uint8_t> integerEntry{ |
| 744 | 0, 0, /* attr handle */ |
| 745 | 3, /* attr type */ |
| 746 | 10, 0, 0, 0, 0, 0, 0, 0, /* current value */ |
| 747 | }; |
| 748 | |
| 749 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 750 | integerEntry.data()); |
| 751 | auto cv = pldm_bios_table_attr_value_entry_integer_decode_cv(entry); |
| 752 | EXPECT_EQ(cv, 10u); |
| 753 | } |
| 754 | |
| 755 | TEST(AttrValTable, IteratorTest) |
| 756 | { |
| 757 | std::vector<uint8_t> enumEntry{ |
| 758 | 0, 0, /* attr handle */ |
| 759 | 0, /* attr type */ |
| 760 | 2, /* number of current value */ |
| 761 | 0, /* current value string handle index */ |
| 762 | 1, /* current value string handle index */ |
| 763 | }; |
| 764 | std::vector<uint8_t> stringEntry{ |
| 765 | 0, 0, /* attr handle */ |
| 766 | 1, /* attr type */ |
| 767 | 3, 0, /* current string length */ |
| 768 | 'a', 'b', 'c', /* defaut value string handle index */ |
| 769 | }; |
| 770 | std::vector<uint8_t> integerEntry{ |
| 771 | 0, 0, /* attr handle */ |
| 772 | 3, /* attr type */ |
| 773 | 10, 0, 0, 0, 0, 0, 0, 0, /* current value */ |
| 774 | }; |
| 775 | |
| 776 | Table table; |
| 777 | buildTable(table, enumEntry, stringEntry, integerEntry, enumEntry); |
| 778 | |
| 779 | auto iter = pldm_bios_table_iter_create(table.data(), table.size(), |
| 780 | PLDM_BIOS_ATTR_VAL_TABLE); |
| 781 | auto entry = pldm_bios_table_iter_attr_value_entry_value(iter); |
| 782 | |
| 783 | auto p = reinterpret_cast<const uint8_t*>(entry); |
| 784 | EXPECT_THAT(std::vector<uint8_t>(p, p + enumEntry.size()), |
| 785 | ElementsAreArray(enumEntry)); |
| 786 | |
| 787 | pldm_bios_table_iter_next(iter); |
| 788 | entry = pldm_bios_table_iter_attr_value_entry_value(iter); |
| 789 | p = reinterpret_cast<const uint8_t*>(entry); |
| 790 | EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()), |
| 791 | ElementsAreArray(stringEntry)); |
| 792 | |
| 793 | pldm_bios_table_iter_next(iter); |
| 794 | entry = pldm_bios_table_iter_attr_value_entry_value(iter); |
| 795 | p = reinterpret_cast<const uint8_t*>(entry); |
| 796 | EXPECT_THAT(std::vector<uint8_t>(p, p + integerEntry.size()), |
| 797 | ElementsAreArray(integerEntry)); |
| 798 | |
| 799 | pldm_bios_table_iter_next(iter); |
| 800 | entry = pldm_bios_table_iter_attr_value_entry_value(iter); |
| 801 | p = reinterpret_cast<const uint8_t*>(entry); |
| 802 | EXPECT_THAT(std::vector<uint8_t>(p, p + enumEntry.size()), |
| 803 | ElementsAreArray(enumEntry)); |
| 804 | |
| 805 | pldm_bios_table_iter_next(iter); |
| 806 | EXPECT_TRUE(pldm_bios_table_iter_is_end(iter)); |
| 807 | |
| 808 | pldm_bios_table_iter_free(iter); |
| 809 | } |
| 810 | |
| 811 | TEST(AttrValTable, FindTest) |
| 812 | { |
| 813 | std::vector<uint8_t> enumEntry{ |
| 814 | 0, 0, /* attr handle */ |
| 815 | 0, /* attr type */ |
| 816 | 2, /* number of current value */ |
| 817 | 0, /* current value string handle index */ |
| 818 | 1, /* current value string handle index */ |
| 819 | }; |
| 820 | std::vector<uint8_t> stringEntry{ |
| 821 | 1, 0, /* attr handle */ |
| 822 | 1, /* attr type */ |
| 823 | 3, 0, /* current string length */ |
| 824 | 'a', 'b', 'c', /* defaut value string handle index */ |
| 825 | }; |
| 826 | std::vector<uint8_t> integerEntry{ |
| 827 | 2, 0, /* attr handle */ |
| 828 | 3, /* attr type */ |
| 829 | 10, 0, 0, 0, 0, 0, 0, 0, /* current value */ |
| 830 | }; |
| 831 | |
| 832 | Table table; |
| 833 | buildTable(table, enumEntry, stringEntry, integerEntry); |
| 834 | |
| 835 | auto entry = pldm_bios_table_attr_value_find_by_handle(table.data(), |
| 836 | table.size(), 1); |
| 837 | EXPECT_NE(entry, nullptr); |
| 838 | auto p = reinterpret_cast<const uint8_t*>(entry); |
| 839 | EXPECT_THAT(std::vector<uint8_t>(p, p + stringEntry.size()), |
| 840 | ElementsAreArray(stringEntry)); |
| 841 | |
| 842 | entry = pldm_bios_table_attr_value_find_by_handle(table.data(), |
| 843 | table.size(), 3); |
| 844 | EXPECT_EQ(entry, nullptr); |
| 845 | |
| 846 | auto firstEntry = |
| 847 | reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(table.data()); |
| 848 | firstEntry->attr_type = PLDM_BIOS_PASSWORD; |
| 849 | EXPECT_DEATH(pldm_bios_table_attr_value_find_by_handle(table.data(), |
| 850 | table.size(), 1), |
| 851 | "entry_length != NULL"); |
| 852 | } |
| 853 | |
| 854 | TEST(AttrValTable, CopyAndUpdateTest) |
| 855 | { |
| 856 | std::vector<uint8_t> enumEntry{ |
| 857 | 0, 0, /* attr handle */ |
| 858 | 0, /* attr type */ |
| 859 | 2, /* number of current value */ |
| 860 | 0, /* current value string handle index */ |
| 861 | 1, /* current value string handle index */ |
| 862 | }; |
| 863 | std::vector<uint8_t> stringEntry{ |
| 864 | 1, 0, /* attr handle */ |
| 865 | 1, /* attr type */ |
| 866 | 3, 0, /* current string length */ |
| 867 | 'a', 'b', 'c', /* defaut value string handle index */ |
| 868 | }; |
| 869 | std::vector<uint8_t> integerEntry{ |
| 870 | 2, 0, /* attr handle */ |
| 871 | 3, /* attr type */ |
| 872 | 10, 0, 0, 0, 0, 0, 0, 0, /* current value */ |
| 873 | }; |
| 874 | |
| 875 | Table srcTable; |
| 876 | buildTable(srcTable, enumEntry, stringEntry, integerEntry); |
| 877 | |
| 878 | std::vector<uint8_t> stringEntry1{ |
| 879 | 1, 0, /* attr handle */ |
| 880 | 1, /* attr type */ |
| 881 | 3, 0, /* current string length */ |
| 882 | 'd', 'e', 'f', /* defaut value string handle index */ |
| 883 | }; |
| 884 | |
| 885 | Table expectTable; |
| 886 | buildTable(expectTable, enumEntry, stringEntry1, integerEntry); |
| 887 | Table destTable(expectTable.size() + 10); |
| 888 | auto destLength = destTable.size(); |
| 889 | auto rc = pldm_bios_table_attr_value_copy_and_update( |
| 890 | srcTable.data(), srcTable.size(), destTable.data(), &destLength, |
| 891 | stringEntry1.data(), stringEntry1.size()); |
| 892 | |
| 893 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 894 | EXPECT_EQ(destLength, expectTable.size()); |
| 895 | destTable.resize(destLength); |
| 896 | EXPECT_THAT(destTable, ElementsAreArray(expectTable)); |
| 897 | |
| 898 | std::vector<uint8_t> stringEntry2{ |
| 899 | 1, 0, /* attr handle */ |
| 900 | 1, /* attr type */ |
| 901 | 5, 0, /* current string length */ |
| 902 | 'd', 'e', 'f', 'a', 'b', /* defaut value string handle index */ |
| 903 | }; |
| 904 | expectTable.resize(0); |
| 905 | buildTable(expectTable, enumEntry, stringEntry2, integerEntry); |
| 906 | destTable.resize(expectTable.size() + 10); |
| 907 | destLength = destTable.size(); |
| 908 | rc = pldm_bios_table_attr_value_copy_and_update( |
| 909 | srcTable.data(), srcTable.size(), destTable.data(), &destLength, |
| 910 | stringEntry2.data(), stringEntry2.size()); |
| 911 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 912 | EXPECT_EQ(destLength, expectTable.size()); |
| 913 | destTable.resize(destLength); |
| 914 | EXPECT_THAT(destTable, ElementsAreArray(expectTable)); |
| 915 | |
| 916 | std::vector<uint8_t> stringEntry3{ |
| 917 | 1, 0, /* attr handle */ |
| 918 | 1, /* attr type */ |
| 919 | 1, 0, /* current string length */ |
| 920 | 'd', /* defaut value string handle index */ |
| 921 | }; |
| 922 | expectTable.resize(0); |
| 923 | buildTable(expectTable, enumEntry, stringEntry3, integerEntry); |
| 924 | destTable.resize(expectTable.size() + 10); |
| 925 | destLength = destTable.size(); |
| 926 | rc = pldm_bios_table_attr_value_copy_and_update( |
| 927 | srcTable.data(), srcTable.size(), destTable.data(), &destLength, |
| 928 | stringEntry3.data(), stringEntry3.size()); |
| 929 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 930 | EXPECT_EQ(destLength, expectTable.size()); |
| 931 | destTable.resize(destLength); |
| 932 | EXPECT_THAT(destTable, ElementsAreArray(expectTable)); |
| 933 | |
| 934 | stringEntry3[2] = PLDM_BIOS_INTEGER; // set attribute type to integer |
| 935 | rc = pldm_bios_table_attr_value_copy_and_update( |
| 936 | srcTable.data(), srcTable.size(), destTable.data(), &destLength, |
| 937 | stringEntry3.data(), stringEntry3.size()); |
| 938 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 939 | stringEntry3[2] = PLDM_BIOS_STRING; // set attribute type to string |
| 940 | |
| 941 | destTable.resize(expectTable.size() - 1); |
| 942 | destLength = destTable.size(); |
| 943 | rc = pldm_bios_table_attr_value_copy_and_update( |
| 944 | srcTable.data(), srcTable.size(), destTable.data(), &destLength, |
| 945 | stringEntry3.data(), stringEntry3.size()); |
| 946 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 947 | } |
| 948 | |
| 949 | TEST(StringTable, EntryEncodeTest) |
| 950 | { |
| 951 | std::vector<uint8_t> stringEntry{ |
| 952 | 0, 0, /* string handle*/ |
| 953 | 7, 0, /* string length */ |
| 954 | 'A', 'l', 'l', 'o', 'w', 'e', 'd', /* string */ |
| 955 | }; |
| 956 | |
| 957 | const char* str = "Allowed"; |
| 958 | auto str_length = std::strlen(str); |
| 959 | auto encodeLength = pldm_bios_table_string_entry_encode_length(str_length); |
| 960 | EXPECT_EQ(encodeLength, stringEntry.size()); |
| 961 | |
| 962 | std::vector<uint8_t> encodeEntry(encodeLength, 0); |
| 963 | pldm_bios_table_string_entry_encode(encodeEntry.data(), encodeEntry.size(), |
| 964 | str, str_length); |
| 965 | // set string handle = 0 |
| 966 | encodeEntry[0] = 0; |
| 967 | encodeEntry[1] = 0; |
| 968 | |
| 969 | EXPECT_EQ(stringEntry, encodeEntry); |
| 970 | |
| 971 | EXPECT_DEATH(pldm_bios_table_string_entry_encode(encodeEntry.data(), |
| 972 | encodeEntry.size() - 1, |
| 973 | str, str_length), |
| 974 | "length <= entry_length"); |
| 975 | auto rc = pldm_bios_table_string_entry_encode_check( |
| 976 | encodeEntry.data(), encodeEntry.size() - 1, str, str_length); |
| 977 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 978 | } |
| 979 | |
| 980 | TEST(StringTable, EntryDecodeTest) |
| 981 | { |
| 982 | std::vector<uint8_t> stringEntry{ |
| 983 | 4, 0, /* string handle*/ |
| 984 | 7, 0, /* string length */ |
| 985 | 'A', 'l', 'l', 'o', 'w', 'e', 'd', /* string */ |
| 986 | }; |
| 987 | auto entry = reinterpret_cast<struct pldm_bios_string_table_entry*>( |
| 988 | stringEntry.data()); |
| 989 | auto handle = pldm_bios_table_string_entry_decode_handle(entry); |
| 990 | EXPECT_EQ(handle, 4); |
| 991 | auto strLength = pldm_bios_table_string_entry_decode_string_length(entry); |
| 992 | EXPECT_EQ(strLength, 7); |
| 993 | |
| 994 | std::vector<char> buffer(strLength + 1, 0); |
| 995 | auto decodedLength = pldm_bios_table_string_entry_decode_string( |
| 996 | entry, buffer.data(), buffer.size()); |
| 997 | EXPECT_EQ(decodedLength, strLength); |
| 998 | EXPECT_EQ(std::strcmp("Allowed", buffer.data()), 0); |
| 999 | decodedLength = pldm_bios_table_string_entry_decode_string( |
| 1000 | entry, buffer.data(), 2 + 1 /* sizeof '\0'*/); |
| 1001 | EXPECT_EQ(decodedLength, 2); |
| 1002 | EXPECT_EQ(std::strcmp("Al", buffer.data()), 0); |
| 1003 | |
| 1004 | auto rc = pldm_bios_table_string_entry_decode_string_check( |
| 1005 | entry, buffer.data(), buffer.size()); |
| 1006 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1007 | EXPECT_EQ(std::strcmp("Allowed", buffer.data()), 0); |
| 1008 | |
| 1009 | rc = pldm_bios_table_string_entry_decode_string_check(entry, buffer.data(), |
| 1010 | buffer.size() - 1); |
| 1011 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1012 | } |
| 1013 | |
| 1014 | TEST(StringTable, IteratorTest) |
| 1015 | { |
| 1016 | std::vector<uint8_t> stringHello{ |
| 1017 | 0, 0, /* string handle*/ |
| 1018 | 5, 0, /* string length */ |
| 1019 | 'H', 'e', 'l', 'l', 'o', /* string */ |
| 1020 | }; |
| 1021 | std::vector<uint8_t> stringWorld{ |
| 1022 | 1, 0, /* string handle*/ |
| 1023 | 6, 0, /* string length */ |
| 1024 | 'W', 'o', 'r', 'l', 'd', '!', /* string */ |
| 1025 | }; |
| 1026 | |
| 1027 | Table table; |
| 1028 | buildTable(table, stringHello, stringWorld); |
| 1029 | |
| 1030 | auto iter = pldm_bios_table_iter_create(table.data(), table.size(), |
| 1031 | PLDM_BIOS_STRING_TABLE); |
| 1032 | auto entry = pldm_bios_table_iter_string_entry_value(iter); |
| 1033 | auto rc = std::memcmp(entry, stringHello.data(), stringHello.size()); |
| 1034 | EXPECT_EQ(rc, 0); |
| 1035 | pldm_bios_table_iter_next(iter); |
| 1036 | entry = pldm_bios_table_iter_string_entry_value(iter); |
| 1037 | rc = std::memcmp(entry, stringWorld.data(), stringWorld.size()); |
| 1038 | EXPECT_EQ(rc, 0); |
| 1039 | pldm_bios_table_iter_next(iter); |
| 1040 | EXPECT_TRUE(pldm_bios_table_iter_is_end(iter)); |
| 1041 | pldm_bios_table_iter_free(iter); |
| 1042 | } |
| 1043 | |
| 1044 | TEST(StringTable, FindTest) |
| 1045 | { |
| 1046 | std::vector<uint8_t> stringHello{ |
| 1047 | 1, 0, /* string handle*/ |
| 1048 | 5, 0, /* string length */ |
| 1049 | 'H', 'e', 'l', 'l', 'o', /* string */ |
| 1050 | }; |
| 1051 | std::vector<uint8_t> stringWorld{ |
| 1052 | 2, 0, /* string handle*/ |
| 1053 | 6, 0, /* string length */ |
| 1054 | 'W', 'o', 'r', 'l', 'd', '!', /* string */ |
| 1055 | }; |
| 1056 | std::vector<uint8_t> stringHi{ |
| 1057 | 3, 0, /* string handle*/ |
| 1058 | 2, 0, /* string length */ |
| 1059 | 'H', 'i', /* string */ |
| 1060 | }; |
| 1061 | |
| 1062 | Table table; |
| 1063 | buildTable(table, stringHello, stringWorld, stringHi); |
| 1064 | |
| 1065 | auto entry = pldm_bios_table_string_find_by_string(table.data(), |
| 1066 | table.size(), "World!"); |
| 1067 | EXPECT_NE(entry, nullptr); |
| 1068 | auto handle = pldm_bios_table_string_entry_decode_handle(entry); |
| 1069 | EXPECT_EQ(handle, 2); |
| 1070 | |
| 1071 | entry = pldm_bios_table_string_find_by_string(table.data(), table.size(), |
| 1072 | "Worl"); |
| 1073 | EXPECT_EQ(entry, nullptr); |
| 1074 | |
| 1075 | entry = |
| 1076 | pldm_bios_table_string_find_by_handle(table.data(), table.size(), 3); |
| 1077 | EXPECT_NE(entry, nullptr); |
| 1078 | auto str_length = pldm_bios_table_string_entry_decode_string_length(entry); |
| 1079 | EXPECT_EQ(str_length, 2); |
| 1080 | std::vector<char> strBuf(str_length + 1, 0); |
| 1081 | auto rc = pldm_bios_table_string_entry_decode_string_check( |
| 1082 | entry, strBuf.data(), strBuf.size()); |
| 1083 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1084 | EXPECT_EQ(std::strcmp("Hi", strBuf.data()), 0); |
| 1085 | |
| 1086 | entry = |
| 1087 | pldm_bios_table_string_find_by_handle(table.data(), table.size(), 4); |
| 1088 | EXPECT_EQ(entry, nullptr); |
| 1089 | } |
| 1090 | |
| 1091 | TEST(Itearator, DeathTest) |
| 1092 | { |
| 1093 | |
| 1094 | Table table(256, 0); |
| 1095 | |
| 1096 | /* first entry */ |
| 1097 | auto attr_entry = |
| 1098 | reinterpret_cast<struct pldm_bios_attr_table_entry*>(table.data()); |
| 1099 | auto iter = pldm_bios_table_iter_create(table.data(), table.size(), |
| 1100 | PLDM_BIOS_ATTR_TABLE); |
| 1101 | attr_entry->attr_type = PLDM_BIOS_PASSWORD; |
| 1102 | EXPECT_DEATH(pldm_bios_table_iter_next(iter), "attr_table_entry != NULL"); |
| 1103 | pldm_bios_table_iter_free(iter); |
| 1104 | } |
| 1105 | |
| 1106 | TEST(PadAndChecksum, PadAndChecksum) |
| 1107 | { |
| 1108 | EXPECT_EQ(4u, pldm_bios_table_pad_checksum_size(0)); |
| 1109 | EXPECT_EQ(7u, pldm_bios_table_pad_checksum_size(1)); |
| 1110 | EXPECT_EQ(6u, pldm_bios_table_pad_checksum_size(2)); |
| 1111 | EXPECT_EQ(5u, pldm_bios_table_pad_checksum_size(3)); |
| 1112 | EXPECT_EQ(4u, pldm_bios_table_pad_checksum_size(4)); |
| 1113 | |
| 1114 | // The table is borrowed from |
| 1115 | // https://github.com/openbmc/pldm/commit/69d3e7fb2d9935773f4fbf44326c33f3fc0a3c38 |
| 1116 | // refer to the commit message |
| 1117 | Table attrValTable = {0x09, 0x00, 0x01, 0x02, 0x00, 0x65, 0x66}; |
| 1118 | auto sizeWithoutPad = attrValTable.size(); |
| 1119 | attrValTable.resize(sizeWithoutPad + |
| 1120 | pldm_bios_table_pad_checksum_size(sizeWithoutPad)); |
| 1121 | pldm_bios_table_append_pad_checksum(attrValTable.data(), |
| 1122 | attrValTable.size(), sizeWithoutPad); |
| 1123 | Table expectedTable = {0x09, 0x00, 0x01, 0x02, 0x00, 0x65, |
| 1124 | 0x66, 0x00, 0x6d, 0x81, 0x4a, 0xb6}; |
| 1125 | EXPECT_EQ(attrValTable, expectedTable); |
| 1126 | } |
| 1127 | |
| 1128 | TEST(BIOSTableChecksum, testBIOSTableChecksum) |
| 1129 | { |
| 1130 | std::vector<uint8_t> stringTable{ |
| 1131 | 1, 0, /* string handle*/ |
| 1132 | 5, 0, /* string length */ |
| 1133 | 'T', 'a', 'b', 'l', 'e', /* string */ |
| 1134 | }; |
| 1135 | |
| 1136 | buildTable(stringTable); |
| 1137 | |
| 1138 | EXPECT_EQ(true, |
| 1139 | pldm_bios_table_checksum(stringTable.data(), stringTable.size())); |
| 1140 | } |