forked from libvirt/libvirt
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathqemuvhostusertest.c
129 lines (97 loc) · 3.53 KB
/
qemuvhostusertest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <config.h>
#include <inttypes.h>
#include "testutils.h"
#include "virfilewrapper.h"
#include "qemu/qemu_vhost_user.h"
#include "configmake.h"
#define VIR_FROM_THIS VIR_FROM_QEMU
/* A very basic test. Parse given JSON vhostuser description into
* an internal structure, format it back and compare with the
* contents of the file (minus some keys that are not parsed).
*/
static int
testParseFormatVU(const void *opaque)
{
const char *filename = opaque;
g_autofree char *path = NULL;
g_autoptr(qemuVhostUser) vu = NULL;
g_autofree char *buf = NULL;
g_autoptr(virJSONValue) json = NULL;
g_autofree char *expected = NULL;
g_autofree char *actual = NULL;
path = g_strdup_printf("%s/qemuvhostuserdata/%s", abs_srcdir, filename);
if (!(vu = qemuVhostUserParse(path)))
return -1;
if (virFileReadAll(path,
1024 * 1024, /* 1MiB */
&buf) < 0)
return -1;
if (!(json = virJSONValueFromString(buf)))
return -1;
/* Description and tags are not parsed. */
if (virJSONValueObjectRemoveKey(json, "description", NULL) < 0 ||
virJSONValueObjectRemoveKey(json, "tags", NULL) < 0)
return -1;
if (!(expected = virJSONValueToString(json, true)))
return -1;
if (!(actual = qemuVhostUserFormat(vu)))
return -1;
return virTestCompareToString(expected, actual);
}
static int
testVUPrecedence(const void *opaque G_GNUC_UNUSED)
{
g_autofree char *fakehome = NULL;
VIR_AUTOSTRINGLIST vuList = NULL;
size_t nvuList;
size_t i;
const char *expected[] = {
PREFIX "/share/qemu/vhost-user/30-gpu.json",
SYSCONFDIR "/qemu/vhost-user/40-gpu.json",
PREFIX "/share/qemu/vhost-user/60-gpu.json",
};
const size_t nexpected = G_N_ELEMENTS(expected);
fakehome = g_strdup(abs_srcdir "/qemuvhostuserdata/home/user/.config");
g_setenv("XDG_CONFIG_HOME", fakehome, TRUE);
if (qemuVhostUserFetchConfigs(&vuList, false) < 0)
return -1;
if (!vuList) {
fprintf(stderr, "Expected a non-NULL result, but got a NULL result\n");
return -1;
}
nvuList = virStringListLength((const char **)vuList);
for (i = 0; i < MAX(nvuList, nexpected); i++) {
const char *e = i < nexpected ? expected[i] : NULL;
const char *f = i < nvuList ? vuList[i] : NULL;
if (STRNEQ_NULLABLE(e, f)) {
fprintf(stderr,
"Unexpected path (i=%zu). Expected %s got %s \n",
i, NULLSTR(e), NULLSTR(f));
return -1;
}
}
return 0;
}
static int
mymain(void)
{
int ret = 0;
virFileWrapperAddPrefix(SYSCONFDIR "/qemu/vhost-user",
abs_srcdir "/qemuvhostuserdata/etc/qemu/vhost-user");
virFileWrapperAddPrefix(PREFIX "/share/qemu/vhost-user",
abs_srcdir "/qemuvhostuserdata/usr/share/qemu/vhost-user");
virFileWrapperAddPrefix("/home/user/.config/qemu/vhost-user",
abs_srcdir "/qemuvhostuserdata/home/user/.config/qemu/vhost-user");
#define DO_PARSE_TEST(filename) \
do { \
if (virTestRun("QEMU vhost-user " filename, \
testParseFormatVU, filename) < 0) \
ret = -1; \
} while (0)
DO_PARSE_TEST("usr/share/qemu/vhost-user/50-gpu.json");
if (virTestRun("QEMU vhost-user precedence test", testVUPrecedence, NULL) < 0)
ret = -1;
virFileWrapperClearPrefixes();
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
VIR_TEST_MAIN(mymain)