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