docs: Reword virDomainGetEmulatorPinInfo description
[libvirt.git] / tests / virpcivpdtest.c
bloba6311bfe762dd7ef2b9e90df0ce721e35411d91f
1 /*
2 * Copyright (C) 2021 Canonical Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; If not, see
16 * <http://www.gnu.org/licenses/>.
19 #include <config.h>
21 #include "internal.h"
22 #include "testutils.h"
23 #include "virpcivpd.h"
25 #define LIBVIRT_VIRPCIVPDPRIV_H_ALLOW
27 #include "virpcivpdpriv.h"
28 #include "virlog.h"
30 #define VIR_FROM_THIS VIR_FROM_NONE
32 #ifdef __linux__
34 VIR_LOG_INIT("tests.vpdtest");
37 typedef struct _TestPCIVPDKeywordValue {
38 const char *keyword;
39 const char *value;
40 char **actual;
41 } TestPCIVPDKeywordValue;
43 static int
44 testPCIVPDResourceBasic(const void *data G_GNUC_UNUSED)
46 size_t i = 0;
47 g_autoptr(virPCIVPDResourceRO) ro = virPCIVPDResourceRONew();
48 g_autoptr(virPCIVPDResourceRW) rw = virPCIVPDResourceRWNew();
49 /* Note: when the same keyword is updated multiple times the
50 * virPCIVPDResourceUpdateKeyword function is expected to free the
51 * previous value whether it is a fixed keyword or a custom one.
52 * */
53 const TestPCIVPDKeywordValue readOnlyCases[] = {
54 {.keyword = "EC", .value = "level1", .actual = &ro->change_level},
55 {.keyword = "EC", .value = "level2", .actual = &ro->change_level},
56 {.keyword = "change_level", .value = "level3", .actual = &ro->change_level},
57 {.keyword = "PN", .value = "number1", .actual = &ro->part_number},
58 {.keyword = "PN", .value = "number2", .actual = &ro->part_number},
59 {.keyword = "part_number", .value = "number3", .actual = &ro->part_number},
60 {.keyword = "MN", .value = "id1", .actual = &ro->manufacture_id},
61 {.keyword = "MN", .value = "id2", .actual = &ro->manufacture_id},
62 {.keyword = "manufacture_id", .value = "id3", &ro->manufacture_id},
63 {.keyword = "SN", .value = "serial1", .actual = &ro->serial_number},
64 {.keyword = "SN", .value = "serial2", .actual = &ro->serial_number},
65 {.keyword = "serial_number", .value = "serial3", .actual = &ro->serial_number},
67 const TestPCIVPDKeywordValue unsupportedFieldCases[] = {
68 {.keyword = "FG", .value = "42", .actual = NULL},
69 {.keyword = "LC", .value = "42", .actual = NULL},
70 {.keyword = "PG", .value = "42", .actual = NULL},
71 {.keyword = "CP", .value = "42", .actual = NULL},
72 {.keyword = "EX", .value = "42", .actual = NULL},
74 size_t numROCases = G_N_ELEMENTS(readOnlyCases);
75 size_t numUnsupportedCases = G_N_ELEMENTS(unsupportedFieldCases);
76 g_autoptr(virPCIVPDResource) res = g_new0(virPCIVPDResource, 1);
77 virPCIVPDResourceCustom *custom = NULL;
79 g_autofree char *val = g_strdup("testval");
80 res->name = g_steal_pointer(&val);
82 /* Initialize RO */
83 res->ro = g_steal_pointer(&ro);
85 /* Update keywords one by one and compare actual values with the expected ones. */
86 for (i = 0; i < numROCases; ++i) {
87 virPCIVPDResourceUpdateKeyword(res, true,
88 readOnlyCases[i].keyword,
89 readOnlyCases[i].value);
90 if (STRNEQ(readOnlyCases[i].value, *readOnlyCases[i].actual))
91 return -1;
94 /* Do a basic vendor field check. */
95 virPCIVPDResourceUpdateKeyword(res, true, "V0", "vendor0");
97 if (res->ro->vendor_specific->len != 1)
98 return -1;
100 custom = g_ptr_array_index(res->ro->vendor_specific, 0);
101 if (custom->idx != '0' || STRNEQ(custom->value, "vendor0"))
102 return -1;
104 /* Make sure unsupported RO keyword updates are not fatal. */
105 for (i = 0; i < numUnsupportedCases; ++i) {
106 virPCIVPDResourceUpdateKeyword(res, true,
107 unsupportedFieldCases[i].keyword,
108 unsupportedFieldCases[i].value);
111 /* Initialize RW */
112 res->rw = g_steal_pointer(&rw);
113 virPCIVPDResourceUpdateKeyword(res, false, "YA", "tag1");
114 if (STRNEQ(res->rw->asset_tag, "tag1"))
115 return -1;
117 virPCIVPDResourceUpdateKeyword(res, false, "asset_tag", "tag2");
118 if (STRNEQ(res->rw->asset_tag, "tag2"))
119 return -1;
121 /* Do a basic system field check. */
122 virPCIVPDResourceUpdateKeyword(res, false, "Y0", "system0");
124 if (res->rw->system_specific->len != 1)
125 return -1;
127 custom = g_ptr_array_index(res->rw->system_specific, 0);
128 if (custom->idx != '0' || STRNEQ(custom->value, "system0"))
129 return -1;
131 /* Make sure unsupported RW keyword updates are not fatal. */
132 for (i = 0; i < numUnsupportedCases; ++i) {
133 /* This test is deliberately left in despite
134 * virPCIVPDResourceUpdateKeyword always succeeding to prevent
135 * possible regressions if the function is ever rewritten */
136 virPCIVPDResourceUpdateKeyword(res, false,
137 unsupportedFieldCases[i].keyword,
138 unsupportedFieldCases[i].value);
142 /* Just make sure the name has not been changed during keyword updates. */
143 if (!STREQ_NULLABLE(res->name, "testval"))
144 return -1;
146 return 0;
149 static int
150 testPCIVPDResourceCustomCompareIndex(const void *data G_GNUC_UNUSED)
152 g_autoptr(virPCIVPDResourceCustom) a = NULL;
153 g_autoptr(virPCIVPDResourceCustom) b = NULL;
155 /* Both are NULL */
156 if (!virPCIVPDResourceCustomCompareIndex(a, b))
157 return -1;
159 /* a is not NULL */
160 a = g_new0(virPCIVPDResourceCustom, 1);
161 if (virPCIVPDResourceCustomCompareIndex(a, b))
162 return -1;
164 /* Reverse */
165 if (virPCIVPDResourceCustomCompareIndex(b, a))
166 return -1;
168 /* Same index, different strings */
169 b = g_new0(virPCIVPDResourceCustom, 1);
170 a->idx = 'z';
171 a->value = g_strdup("42");
172 b->idx = 'z';
173 b->value = g_strdup("24");
174 if (!virPCIVPDResourceCustomCompareIndex(b, a))
175 return -1;
177 /* Different index, different strings */
178 a->idx = 'a';
179 if (virPCIVPDResourceCustomCompareIndex(b, a))
180 return -1;
182 virPCIVPDResourceCustomFree(a);
183 virPCIVPDResourceCustomFree(b);
184 a = g_new0(virPCIVPDResourceCustom, 1);
185 b = g_new0(virPCIVPDResourceCustom, 1);
187 /* Same index, same strings */
188 a->idx = 'z';
189 a->value = g_strdup("42");
190 b->idx = 'z';
191 b->value = g_strdup("42");
192 if (!virPCIVPDResourceCustomCompareIndex(b, a))
193 return -1;
195 /* Different index, same strings */
196 a->idx = 'a';
197 if (virPCIVPDResourceCustomCompareIndex(b, a))
198 return -1;
200 /* Different index, same value pointers */
201 g_clear_pointer(&b->value, g_free);
202 b->value = a->value;
203 if (virPCIVPDResourceCustomCompareIndex(b, a)) {
204 b->value = NULL;
205 return -1;
208 b->value = NULL;
210 return 0;
213 static int
214 testPCIVPDResourceCustomUpsertValue(const void *data G_GNUC_UNUSED)
216 g_autoptr(GPtrArray) arr = g_ptr_array_new_full(0, (GDestroyNotify)virPCIVPDResourceCustomFree);
217 virPCIVPDResourceCustom *custom = NULL;
218 virPCIVPDResourceCustomUpsertValue(arr, 'A', "testval");
220 if (arr->len != 1)
221 return -1;
223 custom = g_ptr_array_index(arr, 0);
224 if (custom == NULL || custom->idx != 'A' || STRNEQ_NULLABLE(custom->value, "testval"))
225 return -1;
227 /* Idempotency */
228 virPCIVPDResourceCustomUpsertValue(arr, 'A', "testval");
230 if (arr->len != 1)
231 return -1;
233 custom = g_ptr_array_index(arr, 0);
234 if (custom == NULL || custom->idx != 'A' || STRNEQ_NULLABLE(custom->value, "testval"))
235 return -1;
237 /* Existing value updates. */
238 virPCIVPDResourceCustomUpsertValue(arr, 'A', "testvalnew");
240 if (arr->len != 1)
241 return -1;
243 custom = g_ptr_array_index(arr, 0);
244 if (custom == NULL || custom->idx != 'A' || STRNEQ_NULLABLE(custom->value, "testvalnew"))
245 return -1;
247 /* Inserting multiple values */
248 virPCIVPDResourceCustomUpsertValue(arr, '1', "42");
250 if (arr->len != 2)
251 return -1;
253 custom = g_ptr_array_index(arr, 1);
254 if (custom == NULL || custom->idx != '1' || STRNEQ_NULLABLE(custom->value, "42"))
255 return -1;
257 return 0;
261 typedef struct _TestPCIVPDExpectedString {
262 const char *keyword;
263 bool expected;
264 } TestPCIVPDExpectedString;
267 * testPCIVPDIsValidTextValue:
269 * Test expected text value validation. Static metadata about possible values is taken
270 * from the PCI(e) standards and based on some real-world hardware examples.
271 * */
272 static int
273 testPCIVPDIsValidTextValue(const void *data G_GNUC_UNUSED)
275 size_t i = 0;
277 const TestPCIVPDExpectedString textValueCases[] = {
278 /* Numbers */
279 {"42", true},
280 /* Alphanumeric */
281 {"DCM1001008FC52101008FC53201008FC54301008FC5", true},
282 /* Dots */
283 {"DSV1028VPDR.VER1.0", true},
284 /* Whitespace presence */
285 {"NMVIntel Corp", true},
286 /* Comma and spaces */
287 {"BlueField-2 DPU 25GbE Dual-Port SFP56, Tall Bracket", true},
288 /* Equal signs and colons. */
289 {"MLX:MN=MLNX:CSKU=V2:UUID=V3:PCI=V0:MODL=BF2H332A", true},
290 /* Dashes */
291 {"MBF2H332A-AEEOT", true},
292 {"under_score_example", true},
293 {"", true},
294 {";", true},
295 {"\\42", true},
296 {"N/A", true},
297 /* The first and last code points are outside ASCII (multi-byte in UTF-8). */
298 {"гbl🐧", false},
300 for (i = 0; i < G_N_ELEMENTS(textValueCases); ++i) {
301 if (virPCIVPDResourceIsValidTextValue(textValueCases[i].keyword) !=
302 textValueCases[i].expected)
303 return -1;
305 return 0;
309 * testPCIVPDGetFieldValueFormat:
311 * A simple test to assess the functionality of the
312 * virPCIVPDResourceGetFieldValueFormat function.
313 * */
314 static int
315 testPCIVPDGetFieldValueFormat(const void *data G_GNUC_UNUSED)
317 typedef struct _TestPCIVPDExpectedFieldValueFormat {
318 const char *keyword;
319 virPCIVPDResourceFieldValueFormat expected;
320 } TestPCIVPDExpectedFieldValueFormat;
322 size_t i = 0;
324 const TestPCIVPDExpectedFieldValueFormat valueFormatCases[] = {
325 {"SN", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_TEXT},
326 {"EC", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_TEXT},
327 {"MN", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_TEXT},
328 {"PN", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_TEXT},
329 {"RV", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_RESVD},
330 {"RW", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_RDWR},
331 {"VA", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_TEXT},
332 {"YA", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_TEXT},
333 {"YZ", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_TEXT},
334 {"CP", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_BINARY},
335 /* Invalid keywords. */
336 {"", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST},
337 {"sn", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST},
338 {"ec", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST},
339 {"mn", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST},
340 {"pn", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST},
341 {"4", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST},
342 {"42", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST},
343 {"Y", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST},
344 {"V", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST},
345 {"v", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST},
346 {"vA", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST},
347 {"va", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST},
348 {"ya", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST},
349 {"Ya", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST},
350 /* 2 bytes but not present in the spec. */
351 {"EX", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST},
352 /* Many numeric bytes. */
353 {"4242", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST},
354 /* Many letters. */
355 {"EXAMPLE", VIR_PCI_VPD_RESOURCE_FIELD_VALUE_FORMAT_LAST},
357 for (i = 0; i < G_N_ELEMENTS(valueFormatCases); ++i) {
358 if (virPCIVPDResourceGetFieldValueFormat(valueFormatCases[i].keyword) !=
359 valueFormatCases[i].expected)
360 return -1;
362 return 0;
365 # define VPD_STRING_RESOURCE_EXAMPLE_HEADER \
366 PCI_VPD_LARGE_RESOURCE_FLAG | PCI_VPD_STRING_RESOURCE_FLAG, 0x08, 0x00
368 # define VPD_STRING_RESOURCE_EXAMPLE_DATA \
369 't', 'e', 's', 't', 'n', 'a', 'm', 'e'
371 # define VPD_R_FIELDS_EXAMPLE_HEADER \
372 PCI_VPD_LARGE_RESOURCE_FLAG | PCI_VPD_READ_ONLY_LARGE_RESOURCE_FLAG, 0x16, 0x00
374 # define VPD_R_EXAMPLE_VALID_RV_FIELD \
375 'R', 'V', 0x02, 0x31, 0x00
377 # define VPD_R_EXAMPLE_INVALID_RV_FIELD \
378 'R', 'V', 0x02, 0xFF, 0x00
380 # define VPD_R_EXAMPLE_FIELDS \
381 'P', 'N', 0x02, '4', '2', \
382 'E', 'C', 0x04, '4', '2', '4', '2', \
383 'V', 'A', 0x02, 'E', 'X'
385 # define VPD_R_FIELDS_EXAMPLE_DATA \
386 VPD_R_EXAMPLE_FIELDS, \
387 VPD_R_EXAMPLE_VALID_RV_FIELD
389 # define VPD_W_FIELDS_EXAMPLE_HEADER \
390 PCI_VPD_LARGE_RESOURCE_FLAG | PCI_VPD_READ_WRITE_LARGE_RESOURCE_FLAG, 0x19, 0x00
392 # define VPD_W_EXAMPLE_FIELDS \
393 'V', 'Z', 0x02, '4', '2', \
394 'Y', 'A', 0x04, '!', '<', '>', ':', \
395 'Y', 'F', 0x02, 'E', 'X', \
396 'Y', 'E', 0x00, \
397 'R', 'W', 0x02, 0x00, 0x00
399 static int
400 testVirPCIVPDValidateExampleReadOnlyFields(virPCIVPDResource *res)
402 const char *expectedName = "testname";
403 virPCIVPDResourceCustom *custom = NULL;
404 if (STRNEQ(res->name, expectedName)) {
405 virReportError(VIR_ERR_INTERNAL_ERROR,
406 "Unexpected string resource value: %s, expected: %s",
407 res->name, expectedName);
408 return -1;
411 if (!res->ro) {
412 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
413 "Read-only keywords are missing from the VPD resource.");
414 return -1;
417 if (STRNEQ_NULLABLE(res->ro->part_number, "42")) {
418 return -1;
419 } else if (STRNEQ_NULLABLE(res->ro->change_level, "4242")) {
420 return -1;
422 if (!res->ro->vendor_specific)
423 return -1;
425 custom = g_ptr_array_index(res->ro->vendor_specific, 0);
426 if (custom->idx != 'A' || STRNEQ_NULLABLE(custom->value, "EX"))
427 return -1;
429 return 0;
432 static int
433 testVirPCIVPDParseFullVPD(const void *opaque G_GNUC_UNUSED)
435 VIR_AUTOCLOSE fd = -1;
436 size_t dataLen = 0;
438 g_autoptr(virPCIVPDResource) res = NULL;
439 /* Note: Custom fields are supposed to be freed by the resource cleanup code. */
440 virPCIVPDResourceCustom *custom = NULL;
442 const uint8_t fullVPDExample[] = {
443 VPD_STRING_RESOURCE_EXAMPLE_HEADER, VPD_STRING_RESOURCE_EXAMPLE_DATA,
444 VPD_R_FIELDS_EXAMPLE_HEADER, VPD_R_FIELDS_EXAMPLE_DATA,
445 VPD_W_FIELDS_EXAMPLE_HEADER, VPD_W_EXAMPLE_FIELDS,
446 PCI_VPD_RESOURCE_END_VAL
449 dataLen = G_N_ELEMENTS(fullVPDExample);
450 if ((fd = virCreateAnonymousFile(fullVPDExample, dataLen)) < 0)
451 return -1;
453 res = virPCIVPDParse(fd);
455 if (!res) {
456 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
457 "The resource pointer is NULL after parsing which is unexpected");
458 return -1;
461 if (!res->ro) {
462 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
463 "Read-only keywords are missing from the VPD resource.");
464 return -1;
465 } else if (!res->rw) {
466 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
467 "Read-write keywords are missing from the VPD resource.");
468 return -1;
471 if (testVirPCIVPDValidateExampleReadOnlyFields(res))
472 return -1;
474 if (STRNEQ_NULLABLE(res->rw->asset_tag, "!<>:"))
475 return -1;
477 if (!res->rw->vendor_specific)
478 return -1;
480 custom = g_ptr_array_index(res->rw->vendor_specific, 0);
481 if (custom->idx != 'Z' || STRNEQ_NULLABLE(custom->value, "42"))
482 return -1;
484 if (!res->rw->system_specific)
485 return -1;
487 custom = g_ptr_array_index(res->rw->system_specific, 0);
488 if (custom->idx != 'F' || STRNEQ_NULLABLE(custom->value, "EX"))
489 return -1;
491 custom = g_ptr_array_index(res->rw->system_specific, 1);
492 if (custom->idx != 'E' || STRNEQ_NULLABLE(custom->value, ""))
493 return -1;
495 custom = NULL;
496 return 0;
499 static int
500 testVirPCIVPDParseZeroLengthRW(const void *opaque G_GNUC_UNUSED)
502 VIR_AUTOCLOSE fd = -1;
503 size_t dataLen = 0;
505 g_autoptr(virPCIVPDResource) res = NULL;
506 virPCIVPDResourceCustom *custom = NULL;
508 /* The RW field has a zero length which means there is no more RW space left. */
509 const uint8_t fullVPDExample[] = {
510 VPD_STRING_RESOURCE_EXAMPLE_HEADER, VPD_STRING_RESOURCE_EXAMPLE_DATA,
511 VPD_R_FIELDS_EXAMPLE_HEADER, VPD_R_FIELDS_EXAMPLE_DATA,
512 PCI_VPD_LARGE_RESOURCE_FLAG | PCI_VPD_READ_WRITE_LARGE_RESOURCE_FLAG, 0x08, 0x00,
513 'V', 'Z', 0x02, '4', '2',
514 'R', 'W', 0x00,
515 PCI_VPD_RESOURCE_END_VAL
518 dataLen = G_N_ELEMENTS(fullVPDExample);
519 if ((fd = virCreateAnonymousFile(fullVPDExample, dataLen)) < 0)
520 return -1;
522 res = virPCIVPDParse(fd);
524 if (!res) {
525 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
526 "The resource pointer is NULL after parsing which is unexpected");
527 return -1;
530 if (!res->ro) {
531 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
532 "Read-only keywords are missing from the VPD resource.");
533 return -1;
534 } else if (!res->rw) {
535 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
536 "Read-write keywords are missing from the VPD resource.");
537 return -1;
540 if (testVirPCIVPDValidateExampleReadOnlyFields(res))
541 return -1;
543 custom = g_ptr_array_index(res->rw->vendor_specific, 0);
544 if (custom->idx != 'Z' || STRNEQ_NULLABLE(custom->value, "42"))
545 return -1;
547 custom = NULL;
548 return 0;
551 static int
552 testVirPCIVPDParseNoRW(const void *opaque G_GNUC_UNUSED)
554 VIR_AUTOCLOSE fd = -1;
555 size_t dataLen = 0;
557 g_autoptr(virPCIVPDResource) res = NULL;
558 virPCIVPDResourceCustom *custom = NULL;
560 /* The RW field has a zero length which means there is no more RW space left. */
561 const uint8_t fullVPDExample[] = {
562 VPD_STRING_RESOURCE_EXAMPLE_HEADER, VPD_STRING_RESOURCE_EXAMPLE_DATA,
563 VPD_R_FIELDS_EXAMPLE_HEADER, VPD_R_FIELDS_EXAMPLE_DATA,
564 PCI_VPD_LARGE_RESOURCE_FLAG | PCI_VPD_READ_WRITE_LARGE_RESOURCE_FLAG, 0x05, 0x00,
565 'V', 'Z', 0x02, '4', '2',
566 PCI_VPD_RESOURCE_END_VAL
569 dataLen = G_N_ELEMENTS(fullVPDExample);
570 if ((fd = virCreateAnonymousFile(fullVPDExample, dataLen)) < 0)
571 return -1;
573 res = virPCIVPDParse(fd);
575 if (!res) {
576 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
577 "The resource pointer is NULL after parsing which is unexpected");
578 return -1;
581 if (!res->ro) {
582 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
583 "Read-only keywords are missing from the VPD resource.");
584 return -1;
585 } else if (!res->rw) {
586 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
587 "Read-write keywords are missing from the VPD resource.");
588 return -1;
591 if (testVirPCIVPDValidateExampleReadOnlyFields(res))
592 return -1;
594 custom = g_ptr_array_index(res->rw->vendor_specific, 0);
595 if (custom->idx != 'Z' || STRNEQ_NULLABLE(custom->value, "42"))
596 return -1;
598 custom = NULL;
599 return 0;
602 static int
603 testVirPCIVPDParseFullVPDSkipInvalidKeywords(const void *opaque G_GNUC_UNUSED)
605 VIR_AUTOCLOSE fd = -1;
606 size_t dataLen = 0;
608 g_autoptr(virPCIVPDResource) res = NULL;
610 const uint8_t fullVPDExample[] = {
611 VPD_STRING_RESOURCE_EXAMPLE_HEADER,
612 VPD_STRING_RESOURCE_EXAMPLE_DATA,
613 PCI_VPD_LARGE_RESOURCE_FLAG | PCI_VPD_READ_ONLY_LARGE_RESOURCE_FLAG, 0x25, 0x00,
614 VPD_R_EXAMPLE_FIELDS,
615 /* The keywords below (except for "RV") are invalid but will be skipped by the parser */
616 0x07, 'A', 0x02, 0x00, 0x00,
617 'V', 0x07, 0x02, 0x00, 0x00,
618 'e', 'x', 0x02, 0x00, 0x00,
619 'R', 'V', 0x02, 0x9A, 0x00,
620 PCI_VPD_RESOURCE_END_VAL
623 dataLen = G_N_ELEMENTS(fullVPDExample);
624 if ((fd = virCreateAnonymousFile(fullVPDExample, dataLen)) < 0)
625 return -1;
627 res = virPCIVPDParse(fd);
629 if (!res) {
630 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
631 "The resource pointer is NULL after parsing which is unexpected.");
632 return -1;
634 if (!res->ro) {
635 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
636 "The RO portion of the VPD resource is NULL.");
637 return -1;
640 if (testVirPCIVPDValidateExampleReadOnlyFields(res))
641 return -1;
643 return 0;
646 static int
647 testVirPCIVPDParseFullVPDSkipInvalidValues(const void *opaque G_GNUC_UNUSED)
649 VIR_AUTOCLOSE fd = -1;
650 size_t dataLen = 0;
651 size_t i = 0;
652 virPCIVPDResourceCustom *custom = NULL;
654 g_autoptr(virPCIVPDResource) res = NULL;
656 /* This example is based on real-world hardware which was programmed by the vendor with
657 * invalid field values in both the RO section and RW section. The RO section contains
658 * fields that are not valid per the spec but accepted by Libvirt as printable ASCII
659 * characters. The RW field has a 0 length which means there is no more space in the
660 * RW section. */
661 const uint8_t fullVPDExample[] = {
662 0x82, 0x23, 0x00, 0x48, 0x50, 0x20, 0x45, 0x74, 0x68, 0x65, 0x72, 0x6e, 0x65, 0x74,
663 0x20, 0x31, 0x47, 0x62, 0x20, 0x32, 0x2d, 0x70, 0x6f, 0x72, 0x74, 0x20, 0x33, 0x36,
664 0x31, 0x69, 0x20, 0x41, 0x64, 0x61, 0x70, 0x74, 0x65, 0x72, 0x90, 0x42, 0x00, 0x50,
665 0x4e, 0x03, 0x4e, 0x2f, 0x41, 0x45, 0x43, 0x03, 0x4e, 0x2f, 0x41, 0x53, 0x4e, 0x03,
666 0x4e, 0x2f, 0x41, 0x56, 0x30, 0x29, 0x34, 0x57, 0x2f, 0x31, 0x57, 0x20, 0x50, 0x43,
667 0x49, 0x65, 0x47, 0x32, 0x78, 0x34, 0x20, 0x32, 0x70, 0x20, 0x31, 0x47, 0x62, 0x45,
668 0x20, 0x52, 0x4a, 0x34, 0x35, 0x20, 0x49, 0x6e, 0x74, 0x65, 0x6c, 0x20, 0x69, 0x33,
669 0x35, 0x30, 0x20, 0x20, 0x20, 0x52, 0x56, 0x01, 0x63, 0x91, 0x47, 0x00, 0x56, 0x31,
670 0x06, 0x35, 0x2e, 0x37, 0x2e, 0x30, 0x36, 0x56, 0x33, 0x06, 0x32, 0x2e, 0x38, 0x2e,
671 0x32, 0x30, 0x56, 0x36, 0x06, 0x31, 0x2e, 0x35, 0x2e, 0x33, 0x35, 0x59, 0x41, 0x03,
672 0x4e, 0x2f, 0x41, 0x59, 0x42, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
673 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x59, 0x43, 0x0D, 0xff, 0xff, 0xff,
674 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 'R', 'W', 0x00, 0x78,
677 dataLen = G_N_ELEMENTS(fullVPDExample);
678 if ((fd = virCreateAnonymousFile(fullVPDExample, dataLen)) < 0)
679 return -1;
681 res = virPCIVPDParse(fd);
683 if (!res) {
684 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
685 "The resource pointer is NULL after parsing which is unexpected.");
686 return -1;
688 /* Some values in the read-write section are invalid but parsing should succeed
689 * considering the parser is implemented to be graceful about invalid keywords and
690 * values. */
691 if (!res->ro) {
692 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
693 "The RO section consisting of only invalid fields got parsed successfully");
694 return -1;
696 if (!res->rw) {
697 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
698 "Could not successfully parse an RW section with some invalid fields");
699 return -1;
702 if (!STREQ_NULLABLE(res->ro->change_level, "N/A")) {
703 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
704 "Could not parse a change level field with acceptable contents");
705 return -1;
707 if (!STREQ_NULLABLE(res->ro->part_number, "N/A")) {
708 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
709 "Could not parse a part number field with acceptable contents");
710 return -1;
712 if (!STREQ_NULLABLE(res->ro->serial_number, "N/A")) {
713 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
714 "Could not parse a serial number with acceptable contents");
715 return -1;
717 if (!STREQ_NULLABLE(res->rw->asset_tag, "N/A")) {
718 /* The asset tag has an invalid value in this case so it should be NULL. */
719 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
720 "Could not parse an asset tag with acceptable contents");
721 return -1;
723 if (res->rw->vendor_specific->len != 3) {
724 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
725 "The number of parsed vendor fields is not equal to the expected number.");
726 return -1;
728 if (res->rw->system_specific->len > 0) {
729 virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
730 "Successfully parsed some systems-specific fields while none are valid");
731 return -1;
733 for (i = 0; i < res->rw->vendor_specific->len; ++i) {
734 custom = ((virPCIVPDResourceCustom*)g_ptr_array_index(res->rw->vendor_specific, i));
735 if (custom->idx == '1') {
736 if (STRNEQ(custom->value, "5.7.06")) {
737 return -1;
739 } else if (custom->idx == '3') {
740 if (STRNEQ(custom->value, "2.8.20")) {
741 return -1;
743 } else if (custom->idx == '6') {
744 if (STRNEQ(custom->value, "1.5.35")) {
745 return -1;
750 return 0;
754 static int
755 testVirPCIVPDParseFullVPDInvalid(const void *opaque G_GNUC_UNUSED)
757 size_t dataLen = 0;
759 # define VPD_INVALID_ZERO_BYTE \
760 0x00
762 # define VPD_INVALID_STRING_HEADER_DATA_LONG \
763 PCI_VPD_LARGE_RESOURCE_FLAG | PCI_VPD_STRING_RESOURCE_FLAG, 0x04, 0x00, \
764 VPD_STRING_RESOURCE_EXAMPLE_DATA, \
765 PCI_VPD_LARGE_RESOURCE_FLAG | PCI_VPD_READ_ONLY_LARGE_RESOURCE_FLAG, 0x05, 0x00, \
766 'R', 'V', 0x02, 0xDA, 0x00, \
767 PCI_VPD_RESOURCE_END_VAL
769 # define VPD_INVALID_STRING_HEADER_DATA_SHORT \
770 PCI_VPD_LARGE_RESOURCE_FLAG | PCI_VPD_STRING_RESOURCE_FLAG, 0x0A, 0x00, \
771 VPD_STRING_RESOURCE_EXAMPLE_DATA, \
772 PCI_VPD_LARGE_RESOURCE_FLAG | PCI_VPD_READ_ONLY_LARGE_RESOURCE_FLAG, 0x05, 0x00, \
773 'R', 'V', 0x02, 0xD4, 0x00, \
774 PCI_VPD_RESOURCE_END_VAL
776 # define VPD_NO_VPD_R \
777 VPD_STRING_RESOURCE_EXAMPLE_HEADER, \
778 VPD_STRING_RESOURCE_EXAMPLE_DATA, \
779 PCI_VPD_RESOURCE_END_VAL
781 # define VPD_R_NO_RV \
782 VPD_STRING_RESOURCE_EXAMPLE_HEADER, \
783 VPD_STRING_RESOURCE_EXAMPLE_DATA, \
784 VPD_R_FIELDS_EXAMPLE_HEADER, \
785 VPD_R_EXAMPLE_FIELDS, \
786 PCI_VPD_RESOURCE_END_VAL
788 # define VPD_R_INVALID_RV \
789 VPD_STRING_RESOURCE_EXAMPLE_HEADER, \
790 VPD_STRING_RESOURCE_EXAMPLE_DATA, \
791 VPD_R_FIELDS_EXAMPLE_HEADER, \
792 VPD_R_EXAMPLE_FIELDS, \
793 VPD_R_EXAMPLE_INVALID_RV_FIELD, \
794 PCI_VPD_RESOURCE_END_VAL
796 # define VPD_R_INVALID_RV_ZERO_LENGTH \
797 VPD_STRING_RESOURCE_EXAMPLE_HEADER, \
798 VPD_STRING_RESOURCE_EXAMPLE_DATA, \
799 PCI_VPD_LARGE_RESOURCE_FLAG | PCI_VPD_READ_ONLY_LARGE_RESOURCE_FLAG, 0x14, 0x00, \
800 VPD_R_EXAMPLE_FIELDS, \
801 'R', 'V', 0x00, \
802 PCI_VPD_RESOURCE_END_VAL
804 /* The RW key is not expected in a VPD-R record. */
805 # define VPD_R_UNEXPECTED_RW_IN_VPD_R_KEY \
806 VPD_STRING_RESOURCE_EXAMPLE_HEADER, \
807 VPD_STRING_RESOURCE_EXAMPLE_DATA, \
808 PCI_VPD_LARGE_RESOURCE_FLAG | PCI_VPD_READ_ONLY_LARGE_RESOURCE_FLAG, 0x1B, 0x00, \
809 VPD_R_EXAMPLE_FIELDS, \
810 'R', 'W', 0x02, 0x00, 0x00, \
811 'R', 'V', 0x02, 0x81, 0x00, \
812 PCI_VPD_RESOURCE_END_VAL
814 # define VPD_INVALID_STRING_RESOURCE_VALUE \
815 VPD_STRING_RESOURCE_EXAMPLE_HEADER, \
816 't', 0x03, 's', 't', 'n', 'a', 'm', 'e', \
817 PCI_VPD_LARGE_RESOURCE_FLAG | PCI_VPD_READ_ONLY_LARGE_RESOURCE_FLAG, 0x0A, 0x00, \
818 'S', 'N', 0x02, 0x04, 0x02, \
819 'R', 'V', 0x02, 0x8A, 0x00, \
820 PCI_VPD_RESOURCE_END_VAL
822 /* The SN field has a length field that goes past the resource boundaries. */
823 # define VPD_INVALID_SN_FIELD_LENGTH \
824 VPD_STRING_RESOURCE_EXAMPLE_HEADER, \
825 't', 'e', 's', 't', 'n', 'a', 'm', 'e', \
826 PCI_VPD_LARGE_RESOURCE_FLAG | PCI_VPD_READ_ONLY_LARGE_RESOURCE_FLAG, 0x0A, 0x00, \
827 'S', 'N', 0x42, 0x04, 0x02, \
828 'R', 'V', 0x02, 0xE8, 0x00, \
829 PCI_VPD_RESOURCE_END_VAL
831 /* The RV field is not the last one in VPD-R while the checksum is valid. */
832 # define VPD_INVALID_RV_NOT_LAST \
833 VPD_STRING_RESOURCE_EXAMPLE_HEADER, \
834 't', 'e', 's', 't', 'n', 'a', 'm', 'e', \
835 PCI_VPD_LARGE_RESOURCE_FLAG | PCI_VPD_READ_ONLY_LARGE_RESOURCE_FLAG, 0x0A, 0x00, \
836 'R', 'V', 0x02, 0xD1, 0x00, \
837 'S', 'N', 0x02, 0x04, 0x02, \
838 PCI_VPD_RESOURCE_END_VAL
840 # define VPD_INVALID_RW_NOT_LAST \
841 VPD_STRING_RESOURCE_EXAMPLE_HEADER, VPD_STRING_RESOURCE_EXAMPLE_DATA, \
842 VPD_R_FIELDS_EXAMPLE_HEADER, VPD_R_FIELDS_EXAMPLE_DATA, \
843 PCI_VPD_LARGE_RESOURCE_FLAG | PCI_VPD_READ_WRITE_LARGE_RESOURCE_FLAG, 0x08, 0x00, \
844 'R', 'W', 0x00, \
845 'V', 'Z', 0x02, '4', '2', \
846 PCI_VPD_RESOURCE_END_VAL
849 # define TEST_INVALID_VPD(invalidVPD) \
850 do { \
851 VIR_AUTOCLOSE fd = -1; \
852 g_autoptr(virPCIVPDResource) res = NULL; \
853 const uint8_t testCase[] = { invalidVPD }; \
854 dataLen = G_N_ELEMENTS(testCase); \
855 if ((fd = virCreateAnonymousFile(testCase, dataLen)) < 0) \
856 return -1; \
857 if ((res = virPCIVPDParse(fd))) { \
858 virReportError(VIR_ERR_INTERNAL_ERROR, "%s", \
859 "Successfully parsed an invalid VPD - this is not expected"); \
860 return -1; \
862 } while (0);
864 TEST_INVALID_VPD(VPD_INVALID_ZERO_BYTE);
865 TEST_INVALID_VPD(VPD_INVALID_STRING_HEADER_DATA_SHORT);
866 TEST_INVALID_VPD(VPD_INVALID_STRING_HEADER_DATA_LONG);
867 TEST_INVALID_VPD(VPD_NO_VPD_R);
868 TEST_INVALID_VPD(VPD_R_NO_RV);
869 TEST_INVALID_VPD(VPD_R_INVALID_RV);
870 TEST_INVALID_VPD(VPD_R_INVALID_RV_ZERO_LENGTH);
871 TEST_INVALID_VPD(VPD_R_UNEXPECTED_RW_IN_VPD_R_KEY);
872 TEST_INVALID_VPD(VPD_INVALID_STRING_RESOURCE_VALUE);
873 TEST_INVALID_VPD(VPD_INVALID_SN_FIELD_LENGTH);
874 TEST_INVALID_VPD(VPD_INVALID_RV_NOT_LAST);
875 TEST_INVALID_VPD(VPD_INVALID_RW_NOT_LAST);
877 return 0;
880 static int
881 mymain(void)
883 int ret = 0;
885 if (virTestRun("Basic functionality of virPCIVPDResource ", testPCIVPDResourceBasic, NULL) < 0)
886 ret = -1;
887 if (virTestRun("Custom field index comparison",
888 testPCIVPDResourceCustomCompareIndex, NULL) < 0)
889 ret = -1;
890 if (virTestRun("Custom field value insertion and updates ",
891 testPCIVPDResourceCustomUpsertValue, NULL) < 0)
892 ret = -1;
893 if (virTestRun("Valid text values ", testPCIVPDIsValidTextValue, NULL) < 0)
894 ret = -1;
895 if (virTestRun("Determining a field value format by a key ",
896 testPCIVPDGetFieldValueFormat, NULL) < 0)
897 ret = -1;
898 if (virTestRun("Parsing a VPD resource with a zero-length RW ",
899 testVirPCIVPDParseZeroLengthRW, NULL) < 0)
900 ret = -1;
901 if (virTestRun("Parsing a VPD resource without an RW ",
902 testVirPCIVPDParseNoRW, NULL) < 0)
903 ret = -1;
904 if (virTestRun("Parsing a VPD resource with an invalid values ",
905 testVirPCIVPDParseFullVPDSkipInvalidValues, NULL) < 0)
906 ret = -1;
907 if (virTestRun("Parsing a VPD resource with an invalid keyword ",
908 testVirPCIVPDParseFullVPDSkipInvalidKeywords, NULL) < 0)
909 ret = -1;
910 if (virTestRun("Parsing VPD resources from a full VPD ", testVirPCIVPDParseFullVPD, NULL) < 0)
911 ret = -1;
912 if (virTestRun("Parsing invalid VPD records ", testVirPCIVPDParseFullVPDInvalid, NULL) < 0)
913 ret = -1;
915 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
918 VIR_TEST_MAIN(mymain)
920 #else /* ! __linux__ */
922 main(void)
924 return EXIT_AM_SKIP;
926 #endif /* ! __linux__ */