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