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/>.
22 #include "testutils.h"
23 #include "virpcivpd.h"
25 #define LIBVIRT_VIRPCIVPDPRIV_H_ALLOW
27 #include "virpcivpdpriv.h"
30 #define VIR_FROM_THIS VIR_FROM_NONE
34 VIR_LOG_INIT("tests.vpdtest");
37 typedef struct _TestPCIVPDKeywordValue
{
41 } TestPCIVPDKeywordValue
;
44 testPCIVPDResourceBasic(const void *data G_GNUC_UNUSED
)
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.
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
);
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
))
94 /* Do a basic vendor field check. */
95 virPCIVPDResourceUpdateKeyword(res
, true, "V0", "vendor0");
97 if (res
->ro
->vendor_specific
->len
!= 1)
100 custom
= g_ptr_array_index(res
->ro
->vendor_specific
, 0);
101 if (custom
->idx
!= '0' || STRNEQ(custom
->value
, "vendor0"))
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
);
112 res
->rw
= g_steal_pointer(&rw
);
113 virPCIVPDResourceUpdateKeyword(res
, false, "YA", "tag1");
114 if (STRNEQ(res
->rw
->asset_tag
, "tag1"))
117 virPCIVPDResourceUpdateKeyword(res
, false, "asset_tag", "tag2");
118 if (STRNEQ(res
->rw
->asset_tag
, "tag2"))
121 /* Do a basic system field check. */
122 virPCIVPDResourceUpdateKeyword(res
, false, "Y0", "system0");
124 if (res
->rw
->system_specific
->len
!= 1)
127 custom
= g_ptr_array_index(res
->rw
->system_specific
, 0);
128 if (custom
->idx
!= '0' || STRNEQ(custom
->value
, "system0"))
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"))
150 testPCIVPDResourceCustomCompareIndex(const void *data G_GNUC_UNUSED
)
152 g_autoptr(virPCIVPDResourceCustom
) a
= NULL
;
153 g_autoptr(virPCIVPDResourceCustom
) b
= NULL
;
156 if (!virPCIVPDResourceCustomCompareIndex(a
, b
))
160 a
= g_new0(virPCIVPDResourceCustom
, 1);
161 if (virPCIVPDResourceCustomCompareIndex(a
, b
))
165 if (virPCIVPDResourceCustomCompareIndex(b
, a
))
168 /* Same index, different strings */
169 b
= g_new0(virPCIVPDResourceCustom
, 1);
171 a
->value
= g_strdup("42");
173 b
->value
= g_strdup("24");
174 if (!virPCIVPDResourceCustomCompareIndex(b
, a
))
177 /* Different index, different strings */
179 if (virPCIVPDResourceCustomCompareIndex(b
, a
))
182 virPCIVPDResourceCustomFree(a
);
183 virPCIVPDResourceCustomFree(b
);
184 a
= g_new0(virPCIVPDResourceCustom
, 1);
185 b
= g_new0(virPCIVPDResourceCustom
, 1);
187 /* Same index, same strings */
189 a
->value
= g_strdup("42");
191 b
->value
= g_strdup("42");
192 if (!virPCIVPDResourceCustomCompareIndex(b
, a
))
195 /* Different index, same strings */
197 if (virPCIVPDResourceCustomCompareIndex(b
, a
))
200 /* Different index, same value pointers */
201 g_clear_pointer(&b
->value
, g_free
);
203 if (virPCIVPDResourceCustomCompareIndex(b
, a
)) {
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");
223 custom
= g_ptr_array_index(arr
, 0);
224 if (custom
== NULL
|| custom
->idx
!= 'A' || STRNEQ_NULLABLE(custom
->value
, "testval"))
228 virPCIVPDResourceCustomUpsertValue(arr
, 'A', "testval");
233 custom
= g_ptr_array_index(arr
, 0);
234 if (custom
== NULL
|| custom
->idx
!= 'A' || STRNEQ_NULLABLE(custom
->value
, "testval"))
237 /* Existing value updates. */
238 virPCIVPDResourceCustomUpsertValue(arr
, 'A', "testvalnew");
243 custom
= g_ptr_array_index(arr
, 0);
244 if (custom
== NULL
|| custom
->idx
!= 'A' || STRNEQ_NULLABLE(custom
->value
, "testvalnew"))
247 /* Inserting multiple values */
248 virPCIVPDResourceCustomUpsertValue(arr
, '1', "42");
253 custom
= g_ptr_array_index(arr
, 1);
254 if (custom
== NULL
|| custom
->idx
!= '1' || STRNEQ_NULLABLE(custom
->value
, "42"))
261 typedef struct _TestPCIVPDExpectedString
{
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.
273 testPCIVPDIsValidTextValue(const void *data G_GNUC_UNUSED
)
277 const TestPCIVPDExpectedString textValueCases
[] = {
281 {"DCM1001008FC52101008FC53201008FC54301008FC5", true},
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},
291 {"MBF2H332A-AEEOT", true},
292 {"under_score_example", true},
297 /* The first and last code points are outside ASCII (multi-byte in UTF-8). */
300 for (i
= 0; i
< G_N_ELEMENTS(textValueCases
); ++i
) {
301 if (virPCIVPDResourceIsValidTextValue(textValueCases
[i
].keyword
) !=
302 textValueCases
[i
].expected
)
309 * testPCIVPDGetFieldValueFormat:
311 * A simple test to assess the functionality of the
312 * virPCIVPDResourceGetFieldValueFormat function.
315 testPCIVPDGetFieldValueFormat(const void *data G_GNUC_UNUSED
)
317 typedef struct _TestPCIVPDExpectedFieldValueFormat
{
319 virPCIVPDResourceFieldValueFormat expected
;
320 } TestPCIVPDExpectedFieldValueFormat
;
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
},
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
)
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', \
397 'R', 'W', 0x02, 0x00, 0x00
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
);
412 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
413 "Read-only keywords are missing from the VPD resource.");
417 if (STRNEQ_NULLABLE(res
->ro
->part_number
, "42")) {
419 } else if (STRNEQ_NULLABLE(res
->ro
->change_level
, "4242")) {
422 if (!res
->ro
->vendor_specific
)
425 custom
= g_ptr_array_index(res
->ro
->vendor_specific
, 0);
426 if (custom
->idx
!= 'A' || STRNEQ_NULLABLE(custom
->value
, "EX"))
433 testVirPCIVPDParseFullVPD(const void *opaque G_GNUC_UNUSED
)
435 VIR_AUTOCLOSE fd
= -1;
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)
453 res
= virPCIVPDParse(fd
);
456 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
457 "The resource pointer is NULL after parsing which is unexpected");
462 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
463 "Read-only keywords are missing from the VPD resource.");
465 } else if (!res
->rw
) {
466 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
467 "Read-write keywords are missing from the VPD resource.");
471 if (testVirPCIVPDValidateExampleReadOnlyFields(res
))
474 if (STRNEQ_NULLABLE(res
->rw
->asset_tag
, "!<>:"))
477 if (!res
->rw
->vendor_specific
)
480 custom
= g_ptr_array_index(res
->rw
->vendor_specific
, 0);
481 if (custom
->idx
!= 'Z' || STRNEQ_NULLABLE(custom
->value
, "42"))
484 if (!res
->rw
->system_specific
)
487 custom
= g_ptr_array_index(res
->rw
->system_specific
, 0);
488 if (custom
->idx
!= 'F' || STRNEQ_NULLABLE(custom
->value
, "EX"))
491 custom
= g_ptr_array_index(res
->rw
->system_specific
, 1);
492 if (custom
->idx
!= 'E' || STRNEQ_NULLABLE(custom
->value
, ""))
500 testVirPCIVPDParseZeroLengthRW(const void *opaque G_GNUC_UNUSED
)
502 VIR_AUTOCLOSE fd
= -1;
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',
515 PCI_VPD_RESOURCE_END_VAL
518 dataLen
= G_N_ELEMENTS(fullVPDExample
);
519 if ((fd
= virCreateAnonymousFile(fullVPDExample
, dataLen
)) < 0)
522 res
= virPCIVPDParse(fd
);
525 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
526 "The resource pointer is NULL after parsing which is unexpected");
531 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
532 "Read-only keywords are missing from the VPD resource.");
534 } else if (!res
->rw
) {
535 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
536 "Read-write keywords are missing from the VPD resource.");
540 if (testVirPCIVPDValidateExampleReadOnlyFields(res
))
543 custom
= g_ptr_array_index(res
->rw
->vendor_specific
, 0);
544 if (custom
->idx
!= 'Z' || STRNEQ_NULLABLE(custom
->value
, "42"))
552 testVirPCIVPDParseNoRW(const void *opaque G_GNUC_UNUSED
)
554 VIR_AUTOCLOSE fd
= -1;
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)
573 res
= virPCIVPDParse(fd
);
576 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
577 "The resource pointer is NULL after parsing which is unexpected");
582 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
583 "Read-only keywords are missing from the VPD resource.");
585 } else if (!res
->rw
) {
586 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
587 "Read-write keywords are missing from the VPD resource.");
591 if (testVirPCIVPDValidateExampleReadOnlyFields(res
))
594 custom
= g_ptr_array_index(res
->rw
->vendor_specific
, 0);
595 if (custom
->idx
!= 'Z' || STRNEQ_NULLABLE(custom
->value
, "42"))
603 testVirPCIVPDParseFullVPDSkipInvalidKeywords(const void *opaque G_GNUC_UNUSED
)
605 VIR_AUTOCLOSE fd
= -1;
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)
627 res
= virPCIVPDParse(fd
);
630 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
631 "The resource pointer is NULL after parsing which is unexpected.");
635 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
636 "The RO portion of the VPD resource is NULL.");
640 if (testVirPCIVPDValidateExampleReadOnlyFields(res
))
647 testVirPCIVPDParseFullVPDSkipInvalidValues(const void *opaque G_GNUC_UNUSED
)
649 VIR_AUTOCLOSE fd
= -1;
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
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)
681 res
= virPCIVPDParse(fd
);
684 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
685 "The resource pointer is NULL after parsing which is unexpected.");
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
692 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
693 "The RO section consisting of only invalid fields got parsed successfully");
697 virReportError(VIR_ERR_INTERNAL_ERROR
, "%s",
698 "Could not successfully parse an RW section with some invalid fields");
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");
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");
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");
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");
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.");
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");
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")) {
739 } else if (custom
->idx
== '3') {
740 if (STRNEQ(custom
->value
, "2.8.20")) {
743 } else if (custom
->idx
== '6') {
744 if (STRNEQ(custom
->value
, "1.5.35")) {
755 testVirPCIVPDParseFullVPDInvalid(const void *opaque G_GNUC_UNUSED
)
759 # define VPD_INVALID_ZERO_BYTE \
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, \
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, \
845 'V', 'Z', 0x02, '4', '2', \
846 PCI_VPD_RESOURCE_END_VAL
849 # define TEST_INVALID_VPD(invalidVPD) \
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) \
857 if ((res = virPCIVPDParse(fd))) { \
858 virReportError(VIR_ERR_INTERNAL_ERROR, "%s", \
859 "Successfully parsed an invalid VPD - this is not expected"); \
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
);
885 if (virTestRun("Basic functionality of virPCIVPDResource ", testPCIVPDResourceBasic
, NULL
) < 0)
887 if (virTestRun("Custom field index comparison",
888 testPCIVPDResourceCustomCompareIndex
, NULL
) < 0)
890 if (virTestRun("Custom field value insertion and updates ",
891 testPCIVPDResourceCustomUpsertValue
, NULL
) < 0)
893 if (virTestRun("Valid text values ", testPCIVPDIsValidTextValue
, NULL
) < 0)
895 if (virTestRun("Determining a field value format by a key ",
896 testPCIVPDGetFieldValueFormat
, NULL
) < 0)
898 if (virTestRun("Parsing a VPD resource with a zero-length RW ",
899 testVirPCIVPDParseZeroLengthRW
, NULL
) < 0)
901 if (virTestRun("Parsing a VPD resource without an RW ",
902 testVirPCIVPDParseNoRW
, NULL
) < 0)
904 if (virTestRun("Parsing a VPD resource with an invalid values ",
905 testVirPCIVPDParseFullVPDSkipInvalidValues
, NULL
) < 0)
907 if (virTestRun("Parsing a VPD resource with an invalid keyword ",
908 testVirPCIVPDParseFullVPDSkipInvalidKeywords
, NULL
) < 0)
910 if (virTestRun("Parsing VPD resources from a full VPD ", testVirPCIVPDParseFullVPD
, NULL
) < 0)
912 if (virTestRun("Parsing invalid VPD records ", testVirPCIVPDParseFullVPDInvalid
, NULL
) < 0)
915 return ret
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
918 VIR_TEST_MAIN(mymain
)
920 #else /* ! __linux__ */
926 #endif /* ! __linux__ */