blob: d22f61c620fa6df63e668afe4ad64d9ebb9ecc51 [file] [log] [blame]
John Wang02700402019-10-06 16:34:29 +08001#include <string.h>
2
3#include <cstring>
John Wangccc04552019-10-14 14:28:25 +08004#include <utility>
John Wang02700402019-10-06 16:34:29 +08005#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
13using Table = std::vector<uint8_t>;
14
15void 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
22template <typename First, typename... Rest>
23void buildTable(Table& table, First& first, Rest&... rest)
24{
25 table.insert(table.end(), first.begin(), first.end());
26 buildTable(table, rest...);
27}
28
29TEST(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 Wang02700402019-10-06 16:34:29 +080046 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 Wang3ad21752019-10-06 16:42:21 +080051
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 Wang02700402019-10-06 16:34:29 +080076 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 Wang3ad21752019-10-06 16:42:21 +080095 rc =
96 pldm_bios_table_attr_entry_enum_decode_pv_hdls_check(entry, nullptr, 0);
97 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
John Wang02700402019-10-06 16:34:29 +080098}
99
John Wangccc04552019-10-14 14:28:25 +0800100TEST(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 Wang02700402019-10-06 16:34:29 +0800152TEST(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 Wangccc04552019-10-14 14:28:25 +0800192TEST(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 Wangca230822019-10-16 11:39:27 +0800272TEST(AttrTable, integerEntryEncodeTest)
273{
274 std::vector<uint8_t> integerEntry{
275 0, 0, /* attr handle */
276 3, /* attr type */
277 1, 0, /* attr name handle */
278 1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */
279 10, 0, 0, 0, 0, 0, 0, 0, /* upper bound */
280 2, 0, 0, 0, /* scalar increment */
281 3, 0, 0, 0, 0, 0, 0, 0, /* defaut value */
282 };
283
284 std::vector<uint16_t> pv_hdls{2, 3};
285 std::vector<uint8_t> defs{0};
286
287 struct pldm_bios_table_attr_entry_integer_info info = {
288 1, /* name handle */
289 false, /* read only */
290 1, /* lower bound */
291 10, /* upper bound */
292 2, /* sacalar increment */
293 3 /* default value */
294 };
295 auto encodeLength = pldm_bios_table_attr_entry_integer_encode_length();
296 EXPECT_EQ(encodeLength, integerEntry.size());
297
298 std::vector<uint8_t> encodeEntry(encodeLength, 0);
299 pldm_bios_table_attr_entry_integer_encode(encodeEntry.data(),
300 encodeEntry.size(), &info);
301 // set attr handle = 0
302 encodeEntry[0] = 0;
303 encodeEntry[1] = 0;
304
305 EXPECT_EQ(integerEntry, encodeEntry);
306
307 EXPECT_DEATH(pldm_bios_table_attr_entry_integer_encode(
308 encodeEntry.data(), encodeEntry.size() - 1, &info),
309 "length <= entry_length");
310
311 auto rc = pldm_bios_table_attr_entry_integer_encode_check(
312 encodeEntry.data(), encodeEntry.size(), &info);
313 EXPECT_EQ(rc, PLDM_SUCCESS);
314 // set attr handle = 0
315 encodeEntry[0] = 0;
316 encodeEntry[1] = 0;
317
318 EXPECT_EQ(integerEntry, encodeEntry);
319
320 rc = pldm_bios_table_attr_entry_integer_encode_check(
321 encodeEntry.data(), encodeEntry.size() - 1, &info);
322 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
323
324 info.lower_bound = 100;
325 info.upper_bound = 50;
326 rc = pldm_bios_table_attr_entry_integer_encode_check(
327 encodeEntry.data(), encodeEntry.size(), &info);
328 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
329}
330
John Wang02700402019-10-06 16:34:29 +0800331TEST(AttrTable, ItearatorTest)
332{
333 std::vector<uint8_t> enumEntry{
334 0, 0, /* attr handle */
335 0, /* attr type */
336 1, 0, /* attr name handle */
337 2, /* number of possible value */
338 2, 0, /* possible value handle */
339 3, 0, /* possible value handle */
340 1, /* number of default value */
341 0 /* defaut value string handle index */
342 };
343 std::vector<uint8_t> stringEntry{
344 1, 0, /* attr handle */
345 1, /* attr type */
346 12, 0, /* attr name handle */
347 1, /* string type */
348 1, 0, /* minimum length of the string in bytes */
349 100, 0, /* maximum length of the string in bytes */
350 3, 0, /* length of default string in length */
351 'a', 'b', 'c' /* default string */
352 };
John Wangca230822019-10-16 11:39:27 +0800353 std::vector<uint8_t> integerEntry{
354 0, 0, /* attr handle */
355 3, /* attr type */
356 1, 0, /* attr name handle */
357 1, 0, 0, 0, 0, 0, 0, 0, /* lower bound */
358 10, 0, 0, 0, 0, 0, 0, 0, /* upper bound */
359 2, 0, 0, 0, /* scalar increment */
360 3, 0, 0, 0, 0, 0, 0, 0, /* defaut value */
361 };
John Wang02700402019-10-06 16:34:29 +0800362
363 Table table;
John Wangca230822019-10-16 11:39:27 +0800364 buildTable(table, enumEntry, stringEntry, integerEntry, enumEntry);
John Wang02700402019-10-06 16:34:29 +0800365 auto iter = pldm_bios_table_iter_create(table.data(), table.size(),
366 PLDM_BIOS_ATTR_TABLE);
367 auto entry = pldm_bios_table_iter_attr_entry_value(iter);
368 auto rc = std::memcmp(entry, enumEntry.data(), enumEntry.size());
369 EXPECT_EQ(rc, 0);
370
371 pldm_bios_table_iter_next(iter);
372 entry = pldm_bios_table_iter_attr_entry_value(iter);
373 rc = std::memcmp(entry, stringEntry.data(), stringEntry.size());
374 EXPECT_EQ(rc, 0);
375
376 pldm_bios_table_iter_next(iter);
377 entry = pldm_bios_table_iter_attr_entry_value(iter);
John Wangca230822019-10-16 11:39:27 +0800378 rc = std::memcmp(entry, integerEntry.data(), integerEntry.size());
379 EXPECT_EQ(rc, 0);
380
381 pldm_bios_table_iter_next(iter);
382 entry = pldm_bios_table_iter_attr_entry_value(iter);
John Wang02700402019-10-06 16:34:29 +0800383 rc = std::memcmp(entry, enumEntry.data(), enumEntry.size());
384 EXPECT_EQ(rc, 0);
385
386 pldm_bios_table_iter_next(iter);
387 EXPECT_TRUE(pldm_bios_table_iter_is_end(iter));
388 pldm_bios_table_iter_free(iter);
389}
390
John Wang3ad21752019-10-06 16:42:21 +0800391TEST(AttrValTable, EnumEntryEncodeTest)
392{
393 std::vector<uint8_t> enumEntry{
394 0, 0, /* attr handle */
395 0, /* attr type */
396 2, /* number of current value */
397 0, /* current value string handle index */
398 1, /* current value string handle index */
399 };
400
401 auto length = pldm_bios_table_attr_value_entry_encode_enum_length(2);
402 EXPECT_EQ(length, enumEntry.size());
403 std::vector<uint8_t> encodeEntry(length, 0);
404 uint8_t handles[] = {0, 1};
405 pldm_bios_table_attr_value_entry_encode_enum(
406 encodeEntry.data(), encodeEntry.size(), 0, 0, 2, handles);
407 EXPECT_EQ(encodeEntry, enumEntry);
408
409 EXPECT_DEATH(
410 pldm_bios_table_attr_value_entry_encode_enum(
411 encodeEntry.data(), encodeEntry.size() - 1, 0, 0, 2, handles),
412 "length <= entry_length");
413
414 auto rc = pldm_bios_table_attr_value_entry_encode_enum_check(
415 encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_ENUMERATION, 2,
416 handles);
417 EXPECT_EQ(rc, PLDM_SUCCESS);
418 EXPECT_EQ(encodeEntry, enumEntry);
419 auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(
420 enumEntry.data());
421 entry->attr_type = PLDM_BIOS_ENUMERATION_READ_ONLY;
422 rc = pldm_bios_table_attr_value_entry_encode_enum_check(
423 encodeEntry.data(), encodeEntry.size(), 0,
424 PLDM_BIOS_ENUMERATION_READ_ONLY, 2, handles);
425 EXPECT_EQ(rc, PLDM_SUCCESS);
426 EXPECT_EQ(encodeEntry, enumEntry);
427 rc = pldm_bios_table_attr_value_entry_encode_enum_check(
428 encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_PASSWORD, 2,
429 handles);
430 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
431 rc = pldm_bios_table_attr_value_entry_encode_enum_check(
432 encodeEntry.data(), encodeEntry.size() - 1, 0, PLDM_BIOS_ENUMERATION, 2,
433 handles);
434 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
435}
436
437TEST(AttrValTable, stringEntryEncodeTest)
438{
439 std::vector<uint8_t> stringEntry{
440 0, 0, /* attr handle */
441 1, /* attr type */
442 3, 0, /* current string length */
443 'a', 'b', 'c', /* defaut value string handle index */
444 };
445
446 auto length = pldm_bios_table_attr_value_entry_encode_string_length(3);
447 EXPECT_EQ(length, stringEntry.size());
448 std::vector<uint8_t> encodeEntry(length, 0);
449 pldm_bios_table_attr_value_entry_encode_string(
450 encodeEntry.data(), encodeEntry.size(), 0, 1, 3, "abc");
451 EXPECT_EQ(encodeEntry, stringEntry);
452
453 EXPECT_DEATH(
454 pldm_bios_table_attr_value_entry_encode_string(
455 encodeEntry.data(), encodeEntry.size() - 1, 0, 1, 3, "abc"),
456 "length <= entry_length");
457
458 auto rc = pldm_bios_table_attr_value_entry_encode_string_check(
459 encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_STRING, 3, "abc");
460 EXPECT_EQ(rc, PLDM_SUCCESS);
461 EXPECT_EQ(encodeEntry, stringEntry);
462 auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(
463 stringEntry.data());
464 entry->attr_type = PLDM_BIOS_STRING_READ_ONLY;
465 rc = pldm_bios_table_attr_value_entry_encode_string_check(
466 encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_STRING_READ_ONLY,
467 3, "abc");
468 EXPECT_EQ(rc, PLDM_SUCCESS);
469 EXPECT_EQ(encodeEntry, stringEntry);
470 rc = pldm_bios_table_attr_value_entry_encode_string_check(
471 encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_PASSWORD, 3,
472 "abc");
473 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
474 rc = pldm_bios_table_attr_value_entry_encode_string_check(
475 encodeEntry.data(), encodeEntry.size() - 1, 0, PLDM_BIOS_STRING, 3,
476 "abc");
477 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
478}
479
John Wangca230822019-10-16 11:39:27 +0800480TEST(AttrValTable, integerEntryEncodeTest)
481{
482 std::vector<uint8_t> integerEntry{
483 0, 0, /* attr handle */
484 3, /* attr type */
485 10, 0, 0, 0, 0, 0, 0, 0, /* current value */
486 };
487
488 auto length = pldm_bios_table_attr_value_entry_encode_integer_length();
489 EXPECT_EQ(length, integerEntry.size());
490 std::vector<uint8_t> encodeEntry(length, 0);
491 pldm_bios_table_attr_value_entry_encode_integer(
492 encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_INTEGER, 10);
493 EXPECT_EQ(encodeEntry, integerEntry);
494
495 EXPECT_DEATH(pldm_bios_table_attr_value_entry_encode_integer(
496 encodeEntry.data(), encodeEntry.size() - 1, 0,
497 PLDM_BIOS_INTEGER, 10),
498 "length <= entry_length");
499
500 auto rc = pldm_bios_table_attr_value_entry_encode_integer_check(
501 encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_INTEGER, 10);
502 EXPECT_EQ(rc, PLDM_SUCCESS);
503 EXPECT_EQ(encodeEntry, integerEntry);
504 auto entry = reinterpret_cast<struct pldm_bios_attr_val_table_entry*>(
505 integerEntry.data());
506 entry->attr_type = PLDM_BIOS_INTEGER_READ_ONLY;
507 rc = pldm_bios_table_attr_value_entry_encode_integer_check(
508 encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_INTEGER_READ_ONLY,
509 10);
510 EXPECT_EQ(rc, PLDM_SUCCESS);
511 EXPECT_EQ(encodeEntry, integerEntry);
512
513 rc = pldm_bios_table_attr_value_entry_encode_integer_check(
514 encodeEntry.data(), encodeEntry.size(), 0, PLDM_BIOS_PASSWORD, 10);
515 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
516 rc = pldm_bios_table_attr_value_entry_encode_integer_check(
517 encodeEntry.data(), encodeEntry.size() - 1, 0,
518 PLDM_BIOS_INTEGER_READ_ONLY, 10);
519 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
520}
521
John Wangdd9a6282019-10-11 18:52:46 +0800522TEST(StringTable, EntryEncodeTest)
523{
524 std::vector<uint8_t> stringEntry{
525 0, 0, /* string handle*/
526 7, 0, /* string length */
527 'A', 'l', 'l', 'o', 'w', 'e', 'd', /* string */
528 };
529
530 const char* str = "Allowed";
531 auto str_length = std::strlen(str);
532 auto encodeLength = pldm_bios_table_string_entry_encode_length(str_length);
533 EXPECT_EQ(encodeLength, stringEntry.size());
534
535 std::vector<uint8_t> encodeEntry(encodeLength, 0);
536 pldm_bios_table_string_entry_encode(encodeEntry.data(), encodeEntry.size(),
537 str, str_length);
538 // set string handle = 0
539 encodeEntry[0] = 0;
540 encodeEntry[1] = 0;
541
542 EXPECT_EQ(stringEntry, encodeEntry);
543
544 EXPECT_DEATH(pldm_bios_table_string_entry_encode(encodeEntry.data(),
545 encodeEntry.size() - 1,
546 str, str_length),
547 "length <= entry_length");
548 auto rc = pldm_bios_table_string_entry_encode_check(
549 encodeEntry.data(), encodeEntry.size() - 1, str, str_length);
550 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
551}
552
553TEST(StringTable, EntryDecodeTest)
554{
555 std::vector<uint8_t> stringEntry{
556 4, 0, /* string handle*/
557 7, 0, /* string length */
558 'A', 'l', 'l', 'o', 'w', 'e', 'd', /* string */
559 };
560 auto entry = reinterpret_cast<struct pldm_bios_string_table_entry*>(
561 stringEntry.data());
562 auto handle = pldm_bios_table_string_entry_decode_handle(entry);
563 EXPECT_EQ(handle, 4);
564 auto strLength = pldm_bios_table_string_entry_decode_string_length(entry);
565 EXPECT_EQ(strLength, 7);
566
567 std::vector<char> buffer(strLength + 1, 0);
568 auto decodedLength = pldm_bios_table_string_entry_decode_string(
569 entry, buffer.data(), buffer.size());
570 EXPECT_EQ(decodedLength, strLength);
571 EXPECT_EQ(std::strcmp("Allowed", buffer.data()), 0);
572 decodedLength =
573 pldm_bios_table_string_entry_decode_string(entry, buffer.data(), 2);
574 EXPECT_EQ(decodedLength, 2);
575 EXPECT_EQ(std::strcmp("Al", buffer.data()), 0);
576
577 auto rc = pldm_bios_table_string_entry_decode_string_check(
578 entry, buffer.data(), buffer.size());
579 EXPECT_EQ(rc, PLDM_SUCCESS);
580 EXPECT_EQ(std::strcmp("Allowed", buffer.data()), 0);
581
582 rc = pldm_bios_table_string_entry_decode_string_check(entry, buffer.data(),
583 buffer.size() - 1);
584 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
585}
586
587TEST(StringTable, IteratorTest)
588{
589 std::vector<uint8_t> stringHello{
590 0, 0, /* string handle*/
591 5, 0, /* string length */
592 'H', 'e', 'l', 'l', 'o', /* string */
593 };
594 std::vector<uint8_t> stringWorld{
595 1, 0, /* string handle*/
596 6, 0, /* string length */
597 'W', 'o', 'r', 'l', 'd', '!', /* string */
598 };
599
600 Table table;
601 buildTable(table, stringHello, stringWorld);
602
603 auto iter = pldm_bios_table_iter_create(table.data(), table.size(),
604 PLDM_BIOS_STRING_TABLE);
605 auto entry = pldm_bios_table_iter_string_entry_value(iter);
606 auto rc = std::memcmp(entry, stringHello.data(), stringHello.size());
607 EXPECT_EQ(rc, 0);
608 pldm_bios_table_iter_next(iter);
609 entry = pldm_bios_table_iter_string_entry_value(iter);
610 rc = std::memcmp(entry, stringWorld.data(), stringWorld.size());
611 EXPECT_EQ(rc, 0);
612 pldm_bios_table_iter_next(iter);
613 EXPECT_TRUE(pldm_bios_table_iter_is_end(iter));
614 pldm_bios_table_iter_free(iter);
615}
616
617TEST(StringTable, FindTest)
618{
619 std::vector<uint8_t> stringHello{
620 1, 0, /* string handle*/
621 5, 0, /* string length */
622 'H', 'e', 'l', 'l', 'o', /* string */
623 };
624 std::vector<uint8_t> stringWorld{
625 2, 0, /* string handle*/
626 6, 0, /* string length */
627 'W', 'o', 'r', 'l', 'd', '!', /* string */
628 };
629 std::vector<uint8_t> stringHi{
630 3, 0, /* string handle*/
631 2, 0, /* string length */
632 'H', 'i', /* string */
633 };
634
635 Table table;
636 buildTable(table, stringHello, stringWorld, stringHi);
637
638 auto entry = pldm_bios_table_string_find_by_string(table.data(),
639 table.size(), "World!");
640 EXPECT_NE(entry, nullptr);
641 auto handle = pldm_bios_table_string_entry_decode_handle(entry);
642 EXPECT_EQ(handle, 2);
643
644 entry = pldm_bios_table_string_find_by_string(table.data(), table.size(),
645 "Worl");
646 EXPECT_EQ(entry, nullptr);
647
648 entry =
649 pldm_bios_table_string_find_by_handle(table.data(), table.size(), 3);
650 EXPECT_NE(entry, nullptr);
651 auto str_length = pldm_bios_table_string_entry_decode_string_length(entry);
652 EXPECT_EQ(str_length, 2);
653 std::vector<char> strBuf(str_length + 1, 0);
654 auto rc = pldm_bios_table_string_entry_decode_string_check(
655 entry, strBuf.data(), strBuf.size());
656 EXPECT_EQ(rc, PLDM_SUCCESS);
657 EXPECT_EQ(std::strcmp("Hi", strBuf.data()), 0);
658
659 entry =
660 pldm_bios_table_string_find_by_handle(table.data(), table.size(), 4);
661 EXPECT_EQ(entry, nullptr);
662}
663
John Wang02700402019-10-06 16:34:29 +0800664TEST(Itearator, DeathTest)
665{
666
667 Table table(256, 0);
668
669 /* first entry */
670 auto attr_entry =
671 reinterpret_cast<struct pldm_bios_attr_table_entry*>(table.data());
672 auto iter = pldm_bios_table_iter_create(table.data(), table.size(),
673 PLDM_BIOS_ATTR_TABLE);
674 attr_entry->attr_type = PLDM_BIOS_PASSWORD;
675 EXPECT_DEATH(pldm_bios_table_iter_next(iter), "attr_table_entry != NULL");
John Wang02700402019-10-06 16:34:29 +0800676 pldm_bios_table_iter_free(iter);
677}