backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / qemufirmwaretest.c
blob2228359a7bea3d62bed66b2e01861ea1ff6c451c
1 #include <config.h>
3 #include <inttypes.h>
5 #include "testutils.h"
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).
16 static int
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)
29 return -1;
31 if (!(fw = qemuFirmwareParse(path)))
32 return -1;
34 if (virFileReadAll(path,
35 1024 * 1024, /* 1MiB */
36 &buf) < 0)
37 return -1;
39 if (!(json = virJSONValueFromString(buf)))
40 return -1;
42 /* Description and tags are not parsed. */
43 if (virJSONValueObjectRemoveKey(json, "description", NULL) < 0 ||
44 virJSONValueObjectRemoveKey(json, "tags", NULL) < 0)
45 return -1;
47 if (!(expected = virJSONValueToString(json, true)))
48 return -1;
50 if (!(actual = qemuFirmwareFormat(fw)))
51 return -1;
53 return virTestCompareToString(expected, actual);
57 static int
58 testFWPrecedence(const void *opaque ATTRIBUTE_UNUSED)
60 VIR_AUTOFREE(char *) fakehome = NULL;
61 VIR_AUTOSTRINGLIST fwList = NULL;
62 size_t nfwList;
63 size_t i;
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)
74 return -1;
76 setenv("XDG_CONFIG_HOME", fakehome, 1);
78 if (qemuFirmwareFetchConfigs(&fwList, false) < 0)
79 return -1;
81 if (!fwList) {
82 fprintf(stderr, "Expected a non-NULL result, but got a NULL result\n");
83 return -1;
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)) {
93 fprintf(stderr,
94 "Unexpected path (i=%zu). Expected %s got %s \n",
95 i, NULLSTR(e), NULLSTR(f));
96 return -1;
100 return 0;
104 struct supportedData {
105 const char *machine;
106 virArch arch;
107 bool secure;
108 unsigned int *interfaces;
109 size_t ninterfaces;
113 static int
114 testSupportedFW(const void *opaque)
116 const struct supportedData *data = opaque;
117 uint64_t actualInterfaces;
118 uint64_t expectedInterfaces = 0;
119 bool actualSecure;
120 size_t i;
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");
128 return -1;
131 if (actualInterfaces != expectedInterfaces) {
132 fprintf(stderr,
133 "Mismatch in supported interfaces. "
134 "Expected 0x%" PRIx64 " got 0x%" PRIx64 "\n",
135 expectedInterfaces, actualInterfaces);
136 return -1;
139 if (actualSecure != data->secure) {
140 fprintf(stderr,
141 "Mismatch in SMM requirement/support. "
142 "Expected %d got %d\n",
143 data->secure, actualSecure);
144 return -1;
147 return 0;
151 static int
152 mymain(void)
154 int ret = 0;
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) \
164 do { \
165 if (virTestRun("QEMU FW " filename, \
166 testParseFormatFW, filename) < 0) \
167 ret = -1; \
168 } while (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)
177 ret = -1;
179 #define DO_SUPPORTED_TEST(machine, arch, secure, ...) \
180 do { \
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) \
186 ret = -1; \
187 } while (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)