John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 1 | #include <string.h> |
| 2 | |
| 3 | #include <cstring> |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 4 | #include <utility> |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 5 | #include <vector> |
| 6 | |
| 7 | #include "libpldm/base.h" |
| 8 | #include "libpldm/bios.h" |
| 9 | #include "libpldm/bios_table.h" |
| 10 | |
| 11 | #include <gtest/gtest.h> |
| 12 | |
| 13 | using Table = std::vector<uint8_t>; |
| 14 | |
| 15 | void buildTable(Table& table) |
| 16 | { |
| 17 | auto padSize = ((table.size() % 4) ? (4 - table.size() % 4) : 0); |
| 18 | table.insert(table.end(), padSize, 0); |
| 19 | table.insert(table.end(), sizeof(uint32_t) /*checksum*/, 0); |
| 20 | } |
| 21 | |
| 22 | template <typename First, typename... Rest> |
| 23 | void buildTable(Table& table, First& first, Rest&... rest) |
| 24 | { |
| 25 | table.insert(table.end(), first.begin(), first.end()); |
| 26 | buildTable(table, rest...); |
| 27 | } |
| 28 | |
| 29 | TEST(AttrTable, EnumEntryDecodeTest) |
| 30 | { |
| 31 | std::vector<uint8_t> enumEntry{ |
| 32 | 0, 0, /* attr handle */ |
| 33 | 0, /* attr type */ |
| 34 | 1, 0, /* attr name handle */ |
| 35 | 2, /* number of possible value */ |
| 36 | 2, 0, /* possible value handle */ |
| 37 | 3, 0, /* possible value handle */ |
| 38 | 1, /* number of default value */ |
| 39 | 0 /* defaut value string handle index */ |
| 40 | }; |
| 41 | |
| 42 | auto entry = |
| 43 | reinterpret_cast<struct pldm_bios_attr_table_entry*>(enumEntry.data()); |
| 44 | uint8_t pvNumber = pldm_bios_table_attr_entry_enum_decode_pv_num(entry); |
| 45 | EXPECT_EQ(pvNumber, 2); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 46 | pvNumber = 0; |
| 47 | auto rc = |
| 48 | pldm_bios_table_attr_entry_enum_decode_pv_num_check(entry, &pvNumber); |
| 49 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 50 | EXPECT_EQ(pvNumber, 2); |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 51 | |
| 52 | std::vector<uint16_t> pvHandles(pvNumber, 0); |
| 53 | pvNumber = pldm_bios_table_attr_entry_enum_decode_pv_hdls( |
| 54 | entry, pvHandles.data(), pvHandles.size()); |
| 55 | EXPECT_EQ(pvNumber, 2); |
| 56 | EXPECT_EQ(pvHandles[0], 2); |
| 57 | EXPECT_EQ(pvHandles[1], 3); |
| 58 | pvHandles.resize(1); |
| 59 | pvNumber = pldm_bios_table_attr_entry_enum_decode_pv_hdls( |
| 60 | entry, pvHandles.data(), pvHandles.size()); |
| 61 | EXPECT_EQ(pvNumber, 1); |
| 62 | EXPECT_EQ(pvHandles[0], 2); |
| 63 | |
| 64 | pvHandles.resize(2); |
| 65 | rc = pldm_bios_table_attr_entry_enum_decode_pv_hdls_check( |
| 66 | entry, pvHandles.data(), pvHandles.size()); |
| 67 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 68 | EXPECT_EQ(pvHandles[0], 2); |
| 69 | EXPECT_EQ(pvHandles[1], 3); |
| 70 | rc = pldm_bios_table_attr_entry_enum_decode_pv_hdls_check( |
| 71 | entry, pvHandles.data(), 1); |
| 72 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 73 | |
| 74 | uint8_t defNumber = pldm_bios_table_attr_entry_enum_decode_def_num(entry); |
| 75 | EXPECT_EQ(defNumber, 1); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 76 | defNumber = 0; |
| 77 | rc = |
| 78 | pldm_bios_table_attr_entry_enum_decode_def_num_check(entry, &defNumber); |
| 79 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 80 | EXPECT_EQ(defNumber, 1); |
| 81 | |
| 82 | rc = |
| 83 | pldm_bios_table_attr_entry_enum_decode_pv_num_check(nullptr, &pvNumber); |
| 84 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 85 | rc = pldm_bios_table_attr_entry_enum_decode_def_num_check(entry, nullptr); |
| 86 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 87 | |
| 88 | entry->attr_type = PLDM_BIOS_STRING; |
| 89 | rc = pldm_bios_table_attr_entry_enum_decode_pv_num_check(entry, &pvNumber); |
| 90 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 91 | |
| 92 | rc = |
| 93 | pldm_bios_table_attr_entry_enum_decode_def_num_check(entry, &defNumber); |
| 94 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 95 | rc = |
| 96 | pldm_bios_table_attr_entry_enum_decode_pv_hdls_check(entry, nullptr, 0); |
| 97 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 98 | } |
| 99 | |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 100 | TEST(AttrTable, EnumEntryEncodeTest) |
| 101 | { |
| 102 | std::vector<uint8_t> enumEntry{ |
| 103 | 0, 0, /* attr handle */ |
| 104 | 0, /* attr type */ |
| 105 | 1, 0, /* attr name handle */ |
| 106 | 2, /* number of possible value */ |
| 107 | 2, 0, /* possible value handle */ |
| 108 | 3, 0, /* possible value handle */ |
| 109 | 1, /* number of default value */ |
| 110 | 0 /* defaut value string handle index */ |
| 111 | }; |
| 112 | |
| 113 | std::vector<uint16_t> pv_hdls{2, 3}; |
| 114 | std::vector<uint8_t> defs{0}; |
| 115 | |
| 116 | struct pldm_bios_table_attr_entry_enum_info info = { |
| 117 | 1, /* name handle */ |
| 118 | false, /* read only */ |
| 119 | 2, /* pv number */ |
| 120 | pv_hdls.data(), /* pv handle */ |
| 121 | 1, /*def number */ |
| 122 | defs.data() /*def index*/ |
| 123 | }; |
| 124 | auto encodeLength = pldm_bios_table_attr_entry_enum_encode_length(2, 1); |
| 125 | EXPECT_EQ(encodeLength, enumEntry.size()); |
| 126 | |
| 127 | std::vector<uint8_t> encodeEntry(encodeLength, 0); |
| 128 | pldm_bios_table_attr_entry_enum_encode(encodeEntry.data(), |
| 129 | encodeEntry.size(), &info); |
| 130 | // set attr handle = 0 |
| 131 | encodeEntry[0] = 0; |
| 132 | encodeEntry[1] = 0; |
| 133 | |
| 134 | EXPECT_EQ(enumEntry, encodeEntry); |
| 135 | |
| 136 | EXPECT_DEATH(pldm_bios_table_attr_entry_enum_encode( |
| 137 | encodeEntry.data(), encodeEntry.size() - 1, &info), |
| 138 | "length <= entry_length"); |
| 139 | auto rc = pldm_bios_table_attr_entry_enum_encode_check( |
| 140 | encodeEntry.data(), encodeEntry.size(), &info); |
| 141 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 142 | // set attr handle = 0 |
| 143 | encodeEntry[0] = 0; |
| 144 | encodeEntry[1] = 0; |
| 145 | |
| 146 | EXPECT_EQ(enumEntry, encodeEntry); |
| 147 | rc = pldm_bios_table_attr_entry_enum_encode_check( |
| 148 | encodeEntry.data(), encodeEntry.size() - 1, &info); |
| 149 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 150 | } |
| 151 | |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 152 | TEST(AttrTable, StringEntryDecodeTest) |
| 153 | { |
| 154 | std::vector<uint8_t> stringEntry{ |
| 155 | 1, 0, /* attr handle */ |
| 156 | 1, /* attr type */ |
| 157 | 12, 0, /* attr name handle */ |
| 158 | 1, /* string type */ |
| 159 | 1, 0, /* minimum length of the string in bytes */ |
| 160 | 100, 0, /* maximum length of the string in bytes */ |
| 161 | 3, 0, /* length of default string in length */ |
| 162 | 'a', 'b', 'c' /* default string */ |
| 163 | }; |
| 164 | |
| 165 | auto entry = reinterpret_cast<struct pldm_bios_attr_table_entry*>( |
| 166 | stringEntry.data()); |
| 167 | uint16_t def_string_length = |
| 168 | pldm_bios_table_attr_entry_string_decode_def_string_length(entry); |
| 169 | EXPECT_EQ(def_string_length, 3); |
| 170 | |
| 171 | def_string_length = 0; |
| 172 | auto rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 173 | entry, &def_string_length); |
| 174 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 175 | EXPECT_EQ(def_string_length, 3); |
| 176 | |
| 177 | rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 178 | entry, nullptr); |
| 179 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 180 | rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 181 | nullptr, &def_string_length); |
| 182 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 183 | entry->attr_type = PLDM_BIOS_INTEGER; |
| 184 | rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 185 | entry, &def_string_length); |
| 186 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 187 | rc = pldm_bios_table_attr_entry_string_decode_def_string_length_check( |
| 188 | nullptr, &def_string_length); |
| 189 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 190 | } |
| 191 | |
John Wang | ccc0455 | 2019-10-14 14:28:25 +0800 | [diff] [blame] | 192 | TEST(AttrTable, StringEntryEncodeTest) |
| 193 | { |
| 194 | std::vector<uint8_t> stringEntry{ |
| 195 | 0, 0, /* attr handle */ |
| 196 | 1, /* attr type */ |
| 197 | 3, 0, /* attr name handle */ |
| 198 | 1, /* string type */ |
| 199 | 1, 0, /* min string length */ |
| 200 | 100, 0, /* max string length */ |
| 201 | 3, 0, /* default string length */ |
| 202 | 'a', 'b', 'c', /* defaul string */ |
| 203 | }; |
| 204 | |
| 205 | struct pldm_bios_table_attr_entry_string_info info = { |
| 206 | 3, /* name handle */ |
| 207 | false, /* read only */ |
| 208 | 1, /* string type ascii */ |
| 209 | 1, /* min length */ |
| 210 | 100, /* max length */ |
| 211 | 3, /* def length */ |
| 212 | "abc", /* def string */ |
| 213 | }; |
| 214 | auto encodeLength = pldm_bios_table_attr_entry_string_encode_length(3); |
| 215 | EXPECT_EQ(encodeLength, stringEntry.size()); |
| 216 | |
| 217 | std::vector<uint8_t> encodeEntry(encodeLength, 0); |
| 218 | pldm_bios_table_attr_entry_string_encode(encodeEntry.data(), |
| 219 | encodeEntry.size(), &info); |
| 220 | // set attr handle = 0 |
| 221 | encodeEntry[0] = 0; |
| 222 | encodeEntry[1] = 0; |
| 223 | |
| 224 | EXPECT_EQ(stringEntry, encodeEntry); |
| 225 | |
| 226 | EXPECT_DEATH(pldm_bios_table_attr_entry_string_encode( |
| 227 | encodeEntry.data(), encodeEntry.size() - 1, &info), |
| 228 | "length <= entry_length"); |
| 229 | auto rc = pldm_bios_table_attr_entry_string_encode_check( |
| 230 | encodeEntry.data(), encodeEntry.size(), &info); |
| 231 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 232 | // set attr handle = 0 |
| 233 | encodeEntry[0] = 0; |
| 234 | encodeEntry[1] = 0; |
| 235 | |
| 236 | EXPECT_EQ(stringEntry, encodeEntry); |
| 237 | rc = pldm_bios_table_attr_entry_string_encode_check( |
| 238 | encodeEntry.data(), encodeEntry.size() - 1, &info); |
| 239 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 240 | std::swap(info.max_length, info.min_length); |
| 241 | rc = pldm_bios_table_attr_entry_string_encode_check( |
| 242 | encodeEntry.data(), encodeEntry.size(), &info); |
| 243 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 244 | std::swap(info.max_length, info.min_length); |
| 245 | |
| 246 | std::vector<uint8_t> stringEntryLength0{ |
| 247 | 0, 0, /* attr handle */ |
| 248 | 1, /* attr type */ |
| 249 | 3, 0, /* attr name handle */ |
| 250 | 1, /* string type */ |
| 251 | 1, 0, /* min string length */ |
| 252 | 100, 0, /* max string length */ |
| 253 | 0, 0, /* default string length */ |
| 254 | }; |
| 255 | |
| 256 | info.def_length = 0; |
| 257 | info.def_string = nullptr; |
| 258 | |
| 259 | encodeLength = pldm_bios_table_attr_entry_string_encode_length(0); |
| 260 | EXPECT_EQ(encodeLength, stringEntryLength0.size()); |
| 261 | |
| 262 | encodeEntry.resize(encodeLength); |
| 263 | pldm_bios_table_attr_entry_string_encode(encodeEntry.data(), |
| 264 | encodeEntry.size(), &info); |
| 265 | // set attr handle = 0 |
| 266 | encodeEntry[0] = 0; |
| 267 | encodeEntry[1] = 0; |
| 268 | |
| 269 | EXPECT_EQ(stringEntryLength0, encodeEntry); |
| 270 | } |
| 271 | |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 272 | TEST(AttrTable, ItearatorTest) |
| 273 | { |
| 274 | std::vector<uint8_t> enumEntry{ |
| 275 | 0, 0, /* attr handle */ |
| 276 | 0, /* attr type */ |
| 277 | 1, 0, /* attr name handle */ |
| 278 | 2, /* number of possible value */ |
| 279 | 2, 0, /* possible value handle */ |
| 280 | 3, 0, /* possible value handle */ |
| 281 | 1, /* number of default value */ |
| 282 | 0 /* defaut value string handle index */ |
| 283 | }; |
| 284 | std::vector<uint8_t> stringEntry{ |
| 285 | 1, 0, /* attr handle */ |
| 286 | 1, /* attr type */ |
| 287 | 12, 0, /* attr name handle */ |
| 288 | 1, /* string type */ |
| 289 | 1, 0, /* minimum length of the string in bytes */ |
| 290 | 100, 0, /* maximum length of the string in bytes */ |
| 291 | 3, 0, /* length of default string in length */ |
| 292 | 'a', 'b', 'c' /* default string */ |
| 293 | }; |
| 294 | |
| 295 | Table table; |
| 296 | buildTable(table, enumEntry, stringEntry, enumEntry); |
| 297 | auto iter = pldm_bios_table_iter_create(table.data(), table.size(), |
| 298 | PLDM_BIOS_ATTR_TABLE); |
| 299 | auto entry = pldm_bios_table_iter_attr_entry_value(iter); |
| 300 | auto rc = std::memcmp(entry, enumEntry.data(), enumEntry.size()); |
| 301 | EXPECT_EQ(rc, 0); |
| 302 | |
| 303 | pldm_bios_table_iter_next(iter); |
| 304 | entry = pldm_bios_table_iter_attr_entry_value(iter); |
| 305 | rc = std::memcmp(entry, stringEntry.data(), stringEntry.size()); |
| 306 | EXPECT_EQ(rc, 0); |
| 307 | |
| 308 | pldm_bios_table_iter_next(iter); |
| 309 | entry = pldm_bios_table_iter_attr_entry_value(iter); |
| 310 | rc = std::memcmp(entry, enumEntry.data(), enumEntry.size()); |
| 311 | EXPECT_EQ(rc, 0); |
| 312 | |
| 313 | pldm_bios_table_iter_next(iter); |
| 314 | EXPECT_TRUE(pldm_bios_table_iter_is_end(iter)); |
| 315 | pldm_bios_table_iter_free(iter); |
| 316 | } |
| 317 | |
John Wang | 3ad2175 | 2019-10-06 16:42:21 +0800 | [diff] [blame] | 318 | TEST(AttrValTable, EnumEntryEncodeTest) |
| 319 | { |
| 320 | std::vector<uint8_t> enumEntry{ |
| 321 | 0, 0, /* attr handle */ |
| 322 | 0, /* attr type */ |
| 323 | 2, /* number of current value */ |
| 324 | 0, /* current value string handle index */ |
| 325 | 1, /* current value string handle index */ |
| 326 | }; |
| 327 | |
| 328 | auto length = pldm_bios_table_attr_value_entry_encode_enum_length(2); |
| 329 | EXPECT_EQ(length, enumEntry.size()); |
| 330 | std::vector<uint8_t> encodeEntry(length, 0); |
| 331 | uint8_t handles[] = {0, 1}; |
| 332 | pldm_bios_table_attr_value_entry_encode_enum( |
| 333 | encodeEntry.data(), encodeEntry.size(), 0, 0, 2, handles); |
| 334 | EXPECT_EQ(encodeEntry, enumEntry); |
| 335 | |
| 336 | EXPECT_DEATH( |
| 337 | pldm_bios_table_attr_value_entry_encode_enum( |
| 338 | encodeEntry.data(), encodeEntry.size() - 1, 0, 0, 2, handles), |
| 339 | "length <= entry_length"); |
| 340 | |
| 341 | auto rc = pldm_bios_table_attr_value_entry_encode_enum_check( |
| 342 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_ENUMERATION, 2, |
| 343 | handles); |
| 344 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 345 | EXPECT_EQ(encodeEntry, enumEntry); |
| 346 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 347 | enumEntry.data()); |
| 348 | entry->attr_type = PLDM_BIOS_ENUMERATION_READ_ONLY; |
| 349 | rc = pldm_bios_table_attr_value_entry_encode_enum_check( |
| 350 | encodeEntry.data(), encodeEntry.size(), 0, |
| 351 | PLDM_BIOS_ENUMERATION_READ_ONLY, 2, handles); |
| 352 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 353 | EXPECT_EQ(encodeEntry, enumEntry); |
| 354 | rc = pldm_bios_table_attr_value_entry_encode_enum_check( |
| 355 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_PASSWORD, 2, |
| 356 | handles); |
| 357 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 358 | rc = pldm_bios_table_attr_value_entry_encode_enum_check( |
| 359 | encodeEntry.data(), encodeEntry.size() - 1, 0, PLDM_BIOS_ENUMERATION, 2, |
| 360 | handles); |
| 361 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 362 | } |
| 363 | |
| 364 | TEST(AttrValTable, stringEntryEncodeTest) |
| 365 | { |
| 366 | std::vector<uint8_t> stringEntry{ |
| 367 | 0, 0, /* attr handle */ |
| 368 | 1, /* attr type */ |
| 369 | 3, 0, /* current string length */ |
| 370 | 'a', 'b', 'c', /* defaut value string handle index */ |
| 371 | }; |
| 372 | |
| 373 | auto length = pldm_bios_table_attr_value_entry_encode_string_length(3); |
| 374 | EXPECT_EQ(length, stringEntry.size()); |
| 375 | std::vector<uint8_t> encodeEntry(length, 0); |
| 376 | pldm_bios_table_attr_value_entry_encode_string( |
| 377 | encodeEntry.data(), encodeEntry.size(), 0, 1, 3, "abc"); |
| 378 | EXPECT_EQ(encodeEntry, stringEntry); |
| 379 | |
| 380 | EXPECT_DEATH( |
| 381 | pldm_bios_table_attr_value_entry_encode_string( |
| 382 | encodeEntry.data(), encodeEntry.size() - 1, 0, 1, 3, "abc"), |
| 383 | "length <= entry_length"); |
| 384 | |
| 385 | auto rc = pldm_bios_table_attr_value_entry_encode_string_check( |
| 386 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_STRING, 3, "abc"); |
| 387 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 388 | EXPECT_EQ(encodeEntry, stringEntry); |
| 389 | auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>( |
| 390 | stringEntry.data()); |
| 391 | entry->attr_type = PLDM_BIOS_STRING_READ_ONLY; |
| 392 | rc = pldm_bios_table_attr_value_entry_encode_string_check( |
| 393 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_STRING_READ_ONLY, |
| 394 | 3, "abc"); |
| 395 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 396 | EXPECT_EQ(encodeEntry, stringEntry); |
| 397 | rc = pldm_bios_table_attr_value_entry_encode_string_check( |
| 398 | encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_PASSWORD, 3, |
| 399 | "abc"); |
| 400 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 401 | rc = pldm_bios_table_attr_value_entry_encode_string_check( |
| 402 | encodeEntry.data(), encodeEntry.size() - 1, 0, PLDM_BIOS_STRING, 3, |
| 403 | "abc"); |
| 404 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 405 | } |
| 406 | |
John Wang | dd9a628 | 2019-10-11 18:52:46 +0800 | [diff] [blame] | 407 | TEST(StringTable, EntryEncodeTest) |
| 408 | { |
| 409 | std::vector<uint8_t> stringEntry{ |
| 410 | 0, 0, /* string handle*/ |
| 411 | 7, 0, /* string length */ |
| 412 | 'A', 'l', 'l', 'o', 'w', 'e', 'd', /* string */ |
| 413 | }; |
| 414 | |
| 415 | const char* str = "Allowed"; |
| 416 | auto str_length = std::strlen(str); |
| 417 | auto encodeLength = pldm_bios_table_string_entry_encode_length(str_length); |
| 418 | EXPECT_EQ(encodeLength, stringEntry.size()); |
| 419 | |
| 420 | std::vector<uint8_t> encodeEntry(encodeLength, 0); |
| 421 | pldm_bios_table_string_entry_encode(encodeEntry.data(), encodeEntry.size(), |
| 422 | str, str_length); |
| 423 | // set string handle = 0 |
| 424 | encodeEntry[0] = 0; |
| 425 | encodeEntry[1] = 0; |
| 426 | |
| 427 | EXPECT_EQ(stringEntry, encodeEntry); |
| 428 | |
| 429 | EXPECT_DEATH(pldm_bios_table_string_entry_encode(encodeEntry.data(), |
| 430 | encodeEntry.size() - 1, |
| 431 | str, str_length), |
| 432 | "length <= entry_length"); |
| 433 | auto rc = pldm_bios_table_string_entry_encode_check( |
| 434 | encodeEntry.data(), encodeEntry.size() - 1, str, str_length); |
| 435 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 436 | } |
| 437 | |
| 438 | TEST(StringTable, EntryDecodeTest) |
| 439 | { |
| 440 | std::vector<uint8_t> stringEntry{ |
| 441 | 4, 0, /* string handle*/ |
| 442 | 7, 0, /* string length */ |
| 443 | 'A', 'l', 'l', 'o', 'w', 'e', 'd', /* string */ |
| 444 | }; |
| 445 | auto entry = reinterpret_cast<struct pldm_bios_string_table_entry*>( |
| 446 | stringEntry.data()); |
| 447 | auto handle = pldm_bios_table_string_entry_decode_handle(entry); |
| 448 | EXPECT_EQ(handle, 4); |
| 449 | auto strLength = pldm_bios_table_string_entry_decode_string_length(entry); |
| 450 | EXPECT_EQ(strLength, 7); |
| 451 | |
| 452 | std::vector<char> buffer(strLength + 1, 0); |
| 453 | auto decodedLength = pldm_bios_table_string_entry_decode_string( |
| 454 | entry, buffer.data(), buffer.size()); |
| 455 | EXPECT_EQ(decodedLength, strLength); |
| 456 | EXPECT_EQ(std::strcmp("Allowed", buffer.data()), 0); |
| 457 | decodedLength = |
| 458 | pldm_bios_table_string_entry_decode_string(entry, buffer.data(), 2); |
| 459 | EXPECT_EQ(decodedLength, 2); |
| 460 | EXPECT_EQ(std::strcmp("Al", buffer.data()), 0); |
| 461 | |
| 462 | auto rc = pldm_bios_table_string_entry_decode_string_check( |
| 463 | entry, buffer.data(), buffer.size()); |
| 464 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 465 | EXPECT_EQ(std::strcmp("Allowed", buffer.data()), 0); |
| 466 | |
| 467 | rc = pldm_bios_table_string_entry_decode_string_check(entry, buffer.data(), |
| 468 | buffer.size() - 1); |
| 469 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 470 | } |
| 471 | |
| 472 | TEST(StringTable, IteratorTest) |
| 473 | { |
| 474 | std::vector<uint8_t> stringHello{ |
| 475 | 0, 0, /* string handle*/ |
| 476 | 5, 0, /* string length */ |
| 477 | 'H', 'e', 'l', 'l', 'o', /* string */ |
| 478 | }; |
| 479 | std::vector<uint8_t> stringWorld{ |
| 480 | 1, 0, /* string handle*/ |
| 481 | 6, 0, /* string length */ |
| 482 | 'W', 'o', 'r', 'l', 'd', '!', /* string */ |
| 483 | }; |
| 484 | |
| 485 | Table table; |
| 486 | buildTable(table, stringHello, stringWorld); |
| 487 | |
| 488 | auto iter = pldm_bios_table_iter_create(table.data(), table.size(), |
| 489 | PLDM_BIOS_STRING_TABLE); |
| 490 | auto entry = pldm_bios_table_iter_string_entry_value(iter); |
| 491 | auto rc = std::memcmp(entry, stringHello.data(), stringHello.size()); |
| 492 | EXPECT_EQ(rc, 0); |
| 493 | pldm_bios_table_iter_next(iter); |
| 494 | entry = pldm_bios_table_iter_string_entry_value(iter); |
| 495 | rc = std::memcmp(entry, stringWorld.data(), stringWorld.size()); |
| 496 | EXPECT_EQ(rc, 0); |
| 497 | pldm_bios_table_iter_next(iter); |
| 498 | EXPECT_TRUE(pldm_bios_table_iter_is_end(iter)); |
| 499 | pldm_bios_table_iter_free(iter); |
| 500 | } |
| 501 | |
| 502 | TEST(StringTable, FindTest) |
| 503 | { |
| 504 | std::vector<uint8_t> stringHello{ |
| 505 | 1, 0, /* string handle*/ |
| 506 | 5, 0, /* string length */ |
| 507 | 'H', 'e', 'l', 'l', 'o', /* string */ |
| 508 | }; |
| 509 | std::vector<uint8_t> stringWorld{ |
| 510 | 2, 0, /* string handle*/ |
| 511 | 6, 0, /* string length */ |
| 512 | 'W', 'o', 'r', 'l', 'd', '!', /* string */ |
| 513 | }; |
| 514 | std::vector<uint8_t> stringHi{ |
| 515 | 3, 0, /* string handle*/ |
| 516 | 2, 0, /* string length */ |
| 517 | 'H', 'i', /* string */ |
| 518 | }; |
| 519 | |
| 520 | Table table; |
| 521 | buildTable(table, stringHello, stringWorld, stringHi); |
| 522 | |
| 523 | auto entry = pldm_bios_table_string_find_by_string(table.data(), |
| 524 | table.size(), "World!"); |
| 525 | EXPECT_NE(entry, nullptr); |
| 526 | auto handle = pldm_bios_table_string_entry_decode_handle(entry); |
| 527 | EXPECT_EQ(handle, 2); |
| 528 | |
| 529 | entry = pldm_bios_table_string_find_by_string(table.data(), table.size(), |
| 530 | "Worl"); |
| 531 | EXPECT_EQ(entry, nullptr); |
| 532 | |
| 533 | entry = |
| 534 | pldm_bios_table_string_find_by_handle(table.data(), table.size(), 3); |
| 535 | EXPECT_NE(entry, nullptr); |
| 536 | auto str_length = pldm_bios_table_string_entry_decode_string_length(entry); |
| 537 | EXPECT_EQ(str_length, 2); |
| 538 | std::vector<char> strBuf(str_length + 1, 0); |
| 539 | auto rc = pldm_bios_table_string_entry_decode_string_check( |
| 540 | entry, strBuf.data(), strBuf.size()); |
| 541 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 542 | EXPECT_EQ(std::strcmp("Hi", strBuf.data()), 0); |
| 543 | |
| 544 | entry = |
| 545 | pldm_bios_table_string_find_by_handle(table.data(), table.size(), 4); |
| 546 | EXPECT_EQ(entry, nullptr); |
| 547 | } |
| 548 | |
John Wang | 0270040 | 2019-10-06 16:34:29 +0800 | [diff] [blame] | 549 | TEST(Itearator, DeathTest) |
| 550 | { |
| 551 | |
| 552 | Table table(256, 0); |
| 553 | |
| 554 | /* first entry */ |
| 555 | auto attr_entry = |
| 556 | reinterpret_cast<struct pldm_bios_attr_table_entry*>(table.data()); |
| 557 | auto iter = pldm_bios_table_iter_create(table.data(), table.size(), |
| 558 | PLDM_BIOS_ATTR_TABLE); |
| 559 | attr_entry->attr_type = PLDM_BIOS_PASSWORD; |
| 560 | EXPECT_DEATH(pldm_bios_table_iter_next(iter), "attr_table_entry != NULL"); |
| 561 | attr_entry->attr_type = PLDM_BIOS_INTEGER; |
| 562 | EXPECT_DEATH(pldm_bios_table_iter_next(iter), "attr_table_entry != NULL"); |
| 563 | pldm_bios_table_iter_free(iter); |
| 564 | } |