6 #include "virfilewrapper.h"
7 #include "qemu/qemu_firmware.h"
8 #include "configmake.h"
10 #define VIR_FROM_THIS VIR_FROM_QEMU
12 /* A very basic test. Parse given JSON firmware description into
13 * an internal structure, format it back and compare with the
14 * contents of the file (minus some keys that are not parsed).
17 testParseFormatFW(const void *opaque
)
19 const char *filename
= opaque
;
20 VIR_AUTOFREE(char *) path
= NULL
;
21 VIR_AUTOPTR(qemuFirmware
) fw
= NULL
;
22 VIR_AUTOFREE(char *) buf
= NULL
;
23 VIR_AUTOPTR(virJSONValue
) json
= NULL
;
24 VIR_AUTOFREE(char *) expected
= NULL
;
25 VIR_AUTOFREE(char *) actual
= NULL
;
27 if (virAsprintf(&path
, "%s/qemufirmwaredata/%s",
28 abs_srcdir
, filename
) < 0)
31 if (!(fw
= qemuFirmwareParse(path
)))
34 if (virFileReadAll(path
,
35 1024 * 1024, /* 1MiB */
39 if (!(json
= virJSONValueFromString(buf
)))
42 /* Description and tags are not parsed. */
43 if (virJSONValueObjectRemoveKey(json
, "description", NULL
) < 0 ||
44 virJSONValueObjectRemoveKey(json
, "tags", NULL
) < 0)
47 if (!(expected
= virJSONValueToString(json
, true)))
50 if (!(actual
= qemuFirmwareFormat(fw
)))
53 return virTestCompareToString(expected
, actual
);
58 testFWPrecedence(const void *opaque ATTRIBUTE_UNUSED
)
60 VIR_AUTOFREE(char *) fakehome
= NULL
;
61 VIR_AUTOSTRINGLIST fwList
= NULL
;
64 const char *expected
[] = {
65 PREFIX
"/share/qemu/firmware/40-bios.json",
66 SYSCONFDIR
"/qemu/firmware/40-ovmf-sb-keys.json",
67 PREFIX
"/share/qemu/firmware/50-ovmf-sb-keys.json",
68 PREFIX
"/share/qemu/firmware/61-ovmf.json",
69 PREFIX
"/share/qemu/firmware/70-aavmf.json",
71 const size_t nexpected
= ARRAY_CARDINALITY(expected
);
73 if (VIR_STRDUP(fakehome
, abs_srcdir
"/qemufirmwaredata/home/user/.config") < 0)
76 setenv("XDG_CONFIG_HOME", fakehome
, 1);
78 if (qemuFirmwareFetchConfigs(&fwList
, false) < 0)
82 fprintf(stderr
, "Expected a non-NULL result, but got a NULL result\n");
86 nfwList
= virStringListLength((const char **)fwList
);
88 for (i
= 0; i
< MAX(nfwList
, nexpected
); i
++) {
89 const char *e
= i
< nexpected
? expected
[i
] : NULL
;
90 const char *f
= i
< nfwList
? fwList
[i
] : NULL
;
92 if (STRNEQ_NULLABLE(e
, f
)) {
94 "Unexpected path (i=%zu). Expected %s got %s \n",
95 i
, NULLSTR(e
), NULLSTR(f
));
104 struct supportedData
{
108 unsigned int *interfaces
;
114 testSupportedFW(const void *opaque
)
116 const struct supportedData
*data
= opaque
;
117 uint64_t actualInterfaces
;
118 uint64_t expectedInterfaces
= 0;
122 for (i
= 0; i
< data
->ninterfaces
; i
++)
123 expectedInterfaces
|= 1ULL << data
->interfaces
[i
];
125 if (qemuFirmwareGetSupported(data
->machine
, data
->arch
, false,
126 &actualInterfaces
, &actualSecure
) < 0) {
127 fprintf(stderr
, "Unable to get list of supported interfaces\n");
131 if (actualInterfaces
!= expectedInterfaces
) {
133 "Mismatch in supported interfaces. "
134 "Expected 0x%" PRIx64
" got 0x%" PRIx64
"\n",
135 expectedInterfaces
, actualInterfaces
);
139 if (actualSecure
!= data
->secure
) {
141 "Mismatch in SMM requirement/support. "
142 "Expected %d got %d\n",
143 data
->secure
, actualSecure
);
156 virFileWrapperAddPrefix(SYSCONFDIR
"/qemu/firmware",
157 abs_srcdir
"/qemufirmwaredata/etc/qemu/firmware");
158 virFileWrapperAddPrefix(PREFIX
"/share/qemu/firmware",
159 abs_srcdir
"/qemufirmwaredata/usr/share/qemu/firmware");
160 virFileWrapperAddPrefix("/home/user/.config/qemu/firmware",
161 abs_srcdir
"/qemufirmwaredata/home/user/.config/qemu/firmware");
163 #define DO_PARSE_TEST(filename) \
165 if (virTestRun("QEMU FW " filename, \
166 testParseFormatFW, filename) < 0) \
170 DO_PARSE_TEST("usr/share/qemu/firmware/40-bios.json");
171 DO_PARSE_TEST("usr/share/qemu/firmware/50-ovmf-sb-keys.json");
172 DO_PARSE_TEST("usr/share/qemu/firmware/60-ovmf-sb.json");
173 DO_PARSE_TEST("usr/share/qemu/firmware/61-ovmf.json");
174 DO_PARSE_TEST("usr/share/qemu/firmware/70-aavmf.json");
176 if (virTestRun("QEMU FW precedence test", testFWPrecedence
, NULL
) < 0)
179 #define DO_SUPPORTED_TEST(machine, arch, secure, ...) \
181 unsigned int interfaces[] = {__VA_ARGS__}; \
182 struct supportedData data = {machine, arch, secure, \
183 interfaces, ARRAY_CARDINALITY(interfaces)}; \
184 if (virTestRun("QEMU FW SUPPORTED " machine " " #arch, \
185 testSupportedFW, &data) < 0) \
189 DO_SUPPORTED_TEST("pc-i440fx-3.1", VIR_ARCH_X86_64
, false,
190 VIR_DOMAIN_OS_DEF_FIRMWARE_BIOS
,
191 VIR_DOMAIN_OS_DEF_FIRMWARE_EFI
);
192 DO_SUPPORTED_TEST("pc-i440fx-3.1", VIR_ARCH_I686
, false,
193 VIR_DOMAIN_OS_DEF_FIRMWARE_BIOS
);
194 DO_SUPPORTED_TEST("pc-q35-3.1", VIR_ARCH_X86_64
, true,
195 VIR_DOMAIN_OS_DEF_FIRMWARE_BIOS
,
196 VIR_DOMAIN_OS_DEF_FIRMWARE_EFI
);
197 DO_SUPPORTED_TEST("pc-q35-3.1", VIR_ARCH_I686
, false,
198 VIR_DOMAIN_OS_DEF_FIRMWARE_BIOS
);
199 DO_SUPPORTED_TEST("virt-3.1", VIR_ARCH_AARCH64
, false,
200 VIR_DOMAIN_OS_DEF_FIRMWARE_EFI
);
202 virFileWrapperClearPrefixes();
204 return ret
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
208 VIR_TEST_MAIN(mymain
)