]>
Commit | Line | Data |
---|---|---|
681c28a3 | 1 | #include "qemu/osdep.h" |
62c39b30 | 2 | #include <locale.h> |
62c39b30 | 3 | #include <glib/gstdio.h> |
62c39b30 MAL |
4 | #include <sys/socket.h> |
5 | #include <sys/un.h> | |
62c39b30 MAL |
6 | |
7 | #include "libqtest.h" | |
452fcdbc | 8 | #include "qapi/qmp/qdict.h" |
47e6b297 | 9 | #include "qapi/qmp/qlist.h" |
62c39b30 MAL |
10 | |
11 | typedef struct { | |
12 | char *test_dir; | |
13 | GMainLoop *loop; | |
14 | int fd; | |
15 | GPid pid; | |
16 | } TestFixture; | |
17 | ||
18 | static int connect_qga(char *path) | |
19 | { | |
20 | int s, ret, len, i = 0; | |
21 | struct sockaddr_un remote; | |
22 | ||
23 | s = socket(AF_UNIX, SOCK_STREAM, 0); | |
24 | g_assert(s != -1); | |
25 | ||
26 | remote.sun_family = AF_UNIX; | |
27 | do { | |
28 | strcpy(remote.sun_path, path); | |
29 | len = strlen(remote.sun_path) + sizeof(remote.sun_family); | |
30 | ret = connect(s, (struct sockaddr *)&remote, len); | |
31 | if (ret == -1) { | |
32 | g_usleep(G_USEC_PER_SEC); | |
33 | } | |
34 | if (i++ == 10) { | |
35 | return -1; | |
36 | } | |
37 | } while (ret == -1); | |
38 | ||
39 | return s; | |
40 | } | |
41 | ||
42 | static void qga_watch(GPid pid, gint status, gpointer user_data) | |
43 | { | |
44 | TestFixture *fixture = user_data; | |
45 | ||
46 | g_assert_cmpint(status, ==, 0); | |
47 | g_main_loop_quit(fixture->loop); | |
48 | } | |
49 | ||
50 | static void | |
c28afa76 | 51 | fixture_setup(TestFixture *fixture, gconstpointer data, gchar **envp) |
62c39b30 MAL |
52 | { |
53 | const gchar *extra_arg = data; | |
54 | GError *error = NULL; | |
55 | gchar *cwd, *path, *cmd, **argv = NULL; | |
56 | ||
57 | fixture->loop = g_main_loop_new(NULL, FALSE); | |
58 | ||
59 | fixture->test_dir = g_strdup("/tmp/qgatest.XXXXXX"); | |
60 | g_assert_nonnull(mkdtemp(fixture->test_dir)); | |
61 | ||
62 | path = g_build_filename(fixture->test_dir, "sock", NULL); | |
63 | cwd = g_get_current_dir(); | |
64 | cmd = g_strdup_printf("%s%cqemu-ga -m unix-listen -t %s -p %s %s %s", | |
65 | cwd, G_DIR_SEPARATOR, | |
66 | fixture->test_dir, path, | |
67 | getenv("QTEST_LOG") ? "-v" : "", | |
68 | extra_arg ?: ""); | |
69 | g_shell_parse_argv(cmd, NULL, &argv, &error); | |
70 | g_assert_no_error(error); | |
71 | ||
c28afa76 | 72 | g_spawn_async(fixture->test_dir, argv, envp, |
62c39b30 MAL |
73 | G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD, |
74 | NULL, NULL, &fixture->pid, &error); | |
75 | g_assert_no_error(error); | |
76 | ||
77 | g_child_watch_add(fixture->pid, qga_watch, fixture); | |
78 | ||
79 | fixture->fd = connect_qga(path); | |
80 | g_assert_cmpint(fixture->fd, !=, -1); | |
81 | ||
82 | g_strfreev(argv); | |
83 | g_free(cmd); | |
84 | g_free(cwd); | |
85 | g_free(path); | |
86 | } | |
87 | ||
88 | static void | |
89 | fixture_tear_down(TestFixture *fixture, gconstpointer data) | |
90 | { | |
91 | gchar *tmp; | |
92 | ||
93 | kill(fixture->pid, SIGTERM); | |
94 | ||
95 | g_main_loop_run(fixture->loop); | |
96 | g_main_loop_unref(fixture->loop); | |
97 | ||
98 | g_spawn_close_pid(fixture->pid); | |
99 | ||
100 | tmp = g_build_filename(fixture->test_dir, "foo", NULL); | |
101 | g_unlink(tmp); | |
102 | g_free(tmp); | |
103 | ||
104 | tmp = g_build_filename(fixture->test_dir, "qga.state", NULL); | |
105 | g_unlink(tmp); | |
106 | g_free(tmp); | |
107 | ||
108 | tmp = g_build_filename(fixture->test_dir, "sock", NULL); | |
109 | g_unlink(tmp); | |
110 | g_free(tmp); | |
111 | ||
112 | g_rmdir(fixture->test_dir); | |
113 | g_free(fixture->test_dir); | |
114 | } | |
115 | ||
116 | static void qmp_assertion_message_error(const char *domain, | |
117 | const char *file, | |
118 | int line, | |
119 | const char *func, | |
120 | const char *expr, | |
121 | QDict *dict) | |
122 | { | |
123 | const char *class, *desc; | |
124 | char *s; | |
125 | QDict *error; | |
126 | ||
127 | error = qdict_get_qdict(dict, "error"); | |
128 | class = qdict_get_try_str(error, "class"); | |
129 | desc = qdict_get_try_str(error, "desc"); | |
130 | ||
131 | s = g_strdup_printf("assertion failed %s: %s %s", expr, class, desc); | |
132 | g_assertion_message(domain, file, line, func, s); | |
133 | g_free(s); | |
134 | } | |
135 | ||
136 | #define qmp_assert_no_error(err) do { \ | |
137 | if (qdict_haskey(err, "error")) { \ | |
138 | qmp_assertion_message_error(G_LOG_DOMAIN, __FILE__, __LINE__, \ | |
139 | G_STRFUNC, #err, err); \ | |
140 | } \ | |
141 | } while (0) | |
142 | ||
143 | static void test_qga_sync_delimited(gconstpointer fix) | |
144 | { | |
145 | const TestFixture *fixture = fix; | |
146 | guint32 v, r = g_random_int(); | |
147 | unsigned char c; | |
148 | QDict *ret; | |
149 | gchar *cmd; | |
150 | ||
5229564b EB |
151 | cmd = g_strdup_printf("\xff{'execute': 'guest-sync-delimited'," |
152 | " 'arguments': {'id': %u } }", r); | |
62c39b30 MAL |
153 | qmp_fd_send(fixture->fd, cmd); |
154 | g_free(cmd); | |
155 | ||
5229564b EB |
156 | /* |
157 | * Read and ignore garbage until resynchronized. | |
158 | * | |
159 | * Note that the full reset sequence would involve checking the | |
160 | * response of guest-sync-delimited and repeating the loop if | |
161 | * 'id' field of the response does not match the 'id' field of | |
162 | * the request. Testing this fully would require inserting | |
163 | * garbage in the response stream and is left as a future test | |
164 | * to implement. | |
165 | * | |
166 | * TODO: The server shouldn't emit so much garbage (among other | |
167 | * things, it loudly complains about the client's \xff being | |
168 | * invalid JSON, even though it is a documented part of the | |
169 | * handshake. | |
170 | */ | |
171 | do { | |
172 | v = read(fixture->fd, &c, 1); | |
173 | g_assert_cmpint(v, ==, 1); | |
174 | } while (c != 0xff); | |
62c39b30 MAL |
175 | |
176 | ret = qmp_fd_receive(fixture->fd); | |
177 | g_assert_nonnull(ret); | |
178 | qmp_assert_no_error(ret); | |
179 | ||
180 | v = qdict_get_int(ret, "return"); | |
181 | g_assert_cmpint(r, ==, v); | |
182 | ||
cb3e7f08 | 183 | qobject_unref(ret); |
62c39b30 MAL |
184 | } |
185 | ||
186 | static void test_qga_sync(gconstpointer fix) | |
187 | { | |
188 | const TestFixture *fixture = fix; | |
189 | guint32 v, r = g_random_int(); | |
190 | QDict *ret; | |
191 | gchar *cmd; | |
192 | ||
5229564b EB |
193 | /* |
194 | * TODO guest-sync is inherently limited: we cannot distinguish | |
195 | * failure caused by reacting to garbage on the wire prior to this | |
196 | * command, from failure of this actual command. Clients are | |
197 | * supposed to be able to send a raw '\xff' byte to at least | |
198 | * re-synchronize the server's parser prior to this command, but | |
199 | * we are not in a position to test that here because (at least | |
200 | * for now) it causes the server to issue an error message about | |
201 | * invalid JSON. Testing of '\xff' handling is done in | |
202 | * guest-sync-delimited instead. | |
203 | */ | |
204 | cmd = g_strdup_printf("{'execute': 'guest-sync'," | |
205 | " 'arguments': {'id': %u } }", r); | |
62c39b30 MAL |
206 | ret = qmp_fd(fixture->fd, cmd); |
207 | g_free(cmd); | |
208 | ||
209 | g_assert_nonnull(ret); | |
210 | qmp_assert_no_error(ret); | |
211 | ||
212 | v = qdict_get_int(ret, "return"); | |
213 | g_assert_cmpint(r, ==, v); | |
214 | ||
cb3e7f08 | 215 | qobject_unref(ret); |
62c39b30 MAL |
216 | } |
217 | ||
218 | static void test_qga_ping(gconstpointer fix) | |
219 | { | |
220 | const TestFixture *fixture = fix; | |
221 | QDict *ret; | |
222 | ||
223 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping'}"); | |
224 | g_assert_nonnull(ret); | |
225 | qmp_assert_no_error(ret); | |
226 | ||
cb3e7f08 | 227 | qobject_unref(ret); |
62c39b30 MAL |
228 | } |
229 | ||
b5f84310 MA |
230 | static void test_qga_invalid_id(gconstpointer fix) |
231 | { | |
b5f84310 | 232 | const TestFixture *fixture = fix; |
0fa39d0b MA |
233 | QDict *ret, *error; |
234 | const char *class; | |
b5f84310 MA |
235 | |
236 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping', 'id': 1}"); | |
237 | g_assert_nonnull(ret); | |
b5f84310 | 238 | |
0fa39d0b MA |
239 | error = qdict_get_qdict(ret, "error"); |
240 | class = qdict_get_try_str(error, "class"); | |
241 | g_assert_cmpstr(class, ==, "GenericError"); | |
b5f84310 MA |
242 | |
243 | qobject_unref(ret); | |
244 | } | |
245 | ||
d4d7ed73 MA |
246 | static void test_qga_invalid_oob(gconstpointer fix) |
247 | { | |
d4d7ed73 | 248 | const TestFixture *fixture = fix; |
674ed722 MA |
249 | QDict *ret, *error; |
250 | const char *class; | |
d4d7ed73 MA |
251 | |
252 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping'," | |
253 | " 'control': {'run-oob': true}}"); | |
254 | g_assert_nonnull(ret); | |
d4d7ed73 | 255 | |
674ed722 MA |
256 | error = qdict_get_qdict(ret, "error"); |
257 | class = qdict_get_try_str(error, "class"); | |
258 | g_assert_cmpstr(class, ==, "GenericError"); | |
d4d7ed73 MA |
259 | |
260 | qobject_unref(ret); | |
261 | } | |
262 | ||
4bdadd86 MAL |
263 | static void test_qga_invalid_args(gconstpointer fix) |
264 | { | |
265 | const TestFixture *fixture = fix; | |
266 | QDict *ret, *error; | |
267 | const gchar *class, *desc; | |
268 | ||
269 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping', " | |
270 | "'arguments': {'foo': 42 }}"); | |
271 | g_assert_nonnull(ret); | |
272 | ||
273 | error = qdict_get_qdict(ret, "error"); | |
274 | class = qdict_get_try_str(error, "class"); | |
275 | desc = qdict_get_try_str(error, "desc"); | |
276 | ||
277 | g_assert_cmpstr(class, ==, "GenericError"); | |
910f738b | 278 | g_assert_cmpstr(desc, ==, "Parameter 'foo' is unexpected"); |
4bdadd86 | 279 | |
cb3e7f08 | 280 | qobject_unref(ret); |
4bdadd86 MAL |
281 | } |
282 | ||
62c39b30 MAL |
283 | static void test_qga_invalid_cmd(gconstpointer fix) |
284 | { | |
285 | const TestFixture *fixture = fix; | |
286 | QDict *ret, *error; | |
287 | const gchar *class, *desc; | |
288 | ||
289 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-invalid-cmd'}"); | |
290 | g_assert_nonnull(ret); | |
291 | ||
292 | error = qdict_get_qdict(ret, "error"); | |
293 | class = qdict_get_try_str(error, "class"); | |
294 | desc = qdict_get_try_str(error, "desc"); | |
295 | ||
296 | g_assert_cmpstr(class, ==, "CommandNotFound"); | |
297 | g_assert_cmpint(strlen(desc), >, 0); | |
298 | ||
cb3e7f08 | 299 | qobject_unref(ret); |
62c39b30 MAL |
300 | } |
301 | ||
302 | static void test_qga_info(gconstpointer fix) | |
303 | { | |
304 | const TestFixture *fixture = fix; | |
305 | QDict *ret, *val; | |
306 | const gchar *version; | |
307 | ||
308 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-info'}"); | |
309 | g_assert_nonnull(ret); | |
310 | qmp_assert_no_error(ret); | |
311 | ||
312 | val = qdict_get_qdict(ret, "return"); | |
313 | version = qdict_get_try_str(val, "version"); | |
314 | g_assert_cmpstr(version, ==, QEMU_VERSION); | |
315 | ||
cb3e7f08 | 316 | qobject_unref(ret); |
62c39b30 MAL |
317 | } |
318 | ||
319 | static void test_qga_get_vcpus(gconstpointer fix) | |
320 | { | |
321 | const TestFixture *fixture = fix; | |
322 | QDict *ret; | |
323 | QList *list; | |
324 | const QListEntry *entry; | |
325 | ||
326 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-vcpus'}"); | |
327 | g_assert_nonnull(ret); | |
328 | qmp_assert_no_error(ret); | |
329 | ||
330 | /* check there is at least a cpu */ | |
331 | list = qdict_get_qlist(ret, "return"); | |
332 | entry = qlist_first(list); | |
7dc847eb HR |
333 | g_assert(qdict_haskey(qobject_to(QDict, entry->value), "online")); |
334 | g_assert(qdict_haskey(qobject_to(QDict, entry->value), "logical-id")); | |
62c39b30 | 335 | |
cb3e7f08 | 336 | qobject_unref(ret); |
62c39b30 MAL |
337 | } |
338 | ||
339 | static void test_qga_get_fsinfo(gconstpointer fix) | |
340 | { | |
341 | const TestFixture *fixture = fix; | |
342 | QDict *ret; | |
343 | QList *list; | |
344 | const QListEntry *entry; | |
345 | ||
346 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-fsinfo'}"); | |
347 | g_assert_nonnull(ret); | |
348 | qmp_assert_no_error(ret); | |
349 | ||
b3e9e584 | 350 | /* sanity-check the response if there are any filesystems */ |
62c39b30 MAL |
351 | list = qdict_get_qlist(ret, "return"); |
352 | entry = qlist_first(list); | |
b3e9e584 | 353 | if (entry) { |
7dc847eb HR |
354 | g_assert(qdict_haskey(qobject_to(QDict, entry->value), "name")); |
355 | g_assert(qdict_haskey(qobject_to(QDict, entry->value), "mountpoint")); | |
356 | g_assert(qdict_haskey(qobject_to(QDict, entry->value), "type")); | |
357 | g_assert(qdict_haskey(qobject_to(QDict, entry->value), "disk")); | |
b3e9e584 | 358 | } |
62c39b30 | 359 | |
cb3e7f08 | 360 | qobject_unref(ret); |
62c39b30 MAL |
361 | } |
362 | ||
363 | static void test_qga_get_memory_block_info(gconstpointer fix) | |
364 | { | |
365 | const TestFixture *fixture = fix; | |
366 | QDict *ret, *val; | |
367 | int64_t size; | |
368 | ||
369 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-block-info'}"); | |
370 | g_assert_nonnull(ret); | |
371 | ||
372 | /* some systems might not expose memory block info in sysfs */ | |
373 | if (!qdict_haskey(ret, "error")) { | |
374 | /* check there is at least some memory */ | |
375 | val = qdict_get_qdict(ret, "return"); | |
376 | size = qdict_get_int(val, "size"); | |
377 | g_assert_cmpint(size, >, 0); | |
378 | } | |
379 | ||
cb3e7f08 | 380 | qobject_unref(ret); |
62c39b30 MAL |
381 | } |
382 | ||
383 | static void test_qga_get_memory_blocks(gconstpointer fix) | |
384 | { | |
385 | const TestFixture *fixture = fix; | |
386 | QDict *ret; | |
387 | QList *list; | |
388 | const QListEntry *entry; | |
389 | ||
390 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-blocks'}"); | |
391 | g_assert_nonnull(ret); | |
392 | ||
393 | /* some systems might not expose memory block info in sysfs */ | |
394 | if (!qdict_haskey(ret, "error")) { | |
395 | list = qdict_get_qlist(ret, "return"); | |
396 | entry = qlist_first(list); | |
397 | /* newer versions of qga may return empty list without error */ | |
398 | if (entry) { | |
7dc847eb HR |
399 | g_assert(qdict_haskey(qobject_to(QDict, entry->value), |
400 | "phys-index")); | |
401 | g_assert(qdict_haskey(qobject_to(QDict, entry->value), "online")); | |
62c39b30 MAL |
402 | } |
403 | } | |
404 | ||
cb3e7f08 | 405 | qobject_unref(ret); |
62c39b30 MAL |
406 | } |
407 | ||
408 | static void test_qga_network_get_interfaces(gconstpointer fix) | |
409 | { | |
410 | const TestFixture *fixture = fix; | |
411 | QDict *ret; | |
412 | QList *list; | |
413 | const QListEntry *entry; | |
414 | ||
415 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-network-get-interfaces'}"); | |
416 | g_assert_nonnull(ret); | |
417 | qmp_assert_no_error(ret); | |
418 | ||
419 | /* check there is at least an interface */ | |
420 | list = qdict_get_qlist(ret, "return"); | |
421 | entry = qlist_first(list); | |
7dc847eb | 422 | g_assert(qdict_haskey(qobject_to(QDict, entry->value), "name")); |
62c39b30 | 423 | |
cb3e7f08 | 424 | qobject_unref(ret); |
62c39b30 MAL |
425 | } |
426 | ||
427 | static void test_qga_file_ops(gconstpointer fix) | |
428 | { | |
429 | const TestFixture *fixture = fix; | |
4eaab85c | 430 | const unsigned char helloworld[] = "Hello World!\n"; |
62c39b30 MAL |
431 | const char *b64; |
432 | gchar *cmd, *path, *enc; | |
4eaab85c | 433 | unsigned char *dec; |
62c39b30 MAL |
434 | QDict *ret, *val; |
435 | int64_t id, eof; | |
436 | gsize count; | |
437 | FILE *f; | |
438 | char tmp[100]; | |
439 | ||
440 | /* open */ | |
441 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open'," | |
442 | " 'arguments': { 'path': 'foo', 'mode': 'w+' } }"); | |
443 | g_assert_nonnull(ret); | |
444 | qmp_assert_no_error(ret); | |
445 | id = qdict_get_int(ret, "return"); | |
cb3e7f08 | 446 | qobject_unref(ret); |
62c39b30 MAL |
447 | |
448 | enc = g_base64_encode(helloworld, sizeof(helloworld)); | |
449 | /* write */ | |
450 | cmd = g_strdup_printf("{'execute': 'guest-file-write'," | |
451 | " 'arguments': { 'handle': %" PRId64 "," | |
452 | " 'buf-b64': '%s' } }", id, enc); | |
453 | ret = qmp_fd(fixture->fd, cmd); | |
454 | g_assert_nonnull(ret); | |
455 | qmp_assert_no_error(ret); | |
456 | ||
457 | val = qdict_get_qdict(ret, "return"); | |
458 | count = qdict_get_int(val, "count"); | |
459 | eof = qdict_get_bool(val, "eof"); | |
460 | g_assert_cmpint(count, ==, sizeof(helloworld)); | |
461 | g_assert_cmpint(eof, ==, 0); | |
cb3e7f08 | 462 | qobject_unref(ret); |
62c39b30 MAL |
463 | g_free(cmd); |
464 | ||
465 | /* flush */ | |
466 | cmd = g_strdup_printf("{'execute': 'guest-file-flush'," | |
467 | " 'arguments': {'handle': %" PRId64 "} }", | |
468 | id); | |
469 | ret = qmp_fd(fixture->fd, cmd); | |
cb3e7f08 | 470 | qobject_unref(ret); |
62c39b30 MAL |
471 | g_free(cmd); |
472 | ||
473 | /* close */ | |
474 | cmd = g_strdup_printf("{'execute': 'guest-file-close'," | |
475 | " 'arguments': {'handle': %" PRId64 "} }", | |
476 | id); | |
477 | ret = qmp_fd(fixture->fd, cmd); | |
cb3e7f08 | 478 | qobject_unref(ret); |
62c39b30 MAL |
479 | g_free(cmd); |
480 | ||
481 | /* check content */ | |
482 | path = g_build_filename(fixture->test_dir, "foo", NULL); | |
483 | f = fopen(path, "r"); | |
1e271338 | 484 | g_free(path); |
62c39b30 MAL |
485 | g_assert_nonnull(f); |
486 | count = fread(tmp, 1, sizeof(tmp), f); | |
487 | g_assert_cmpint(count, ==, sizeof(helloworld)); | |
488 | tmp[count] = 0; | |
489 | g_assert_cmpstr(tmp, ==, (char *)helloworld); | |
490 | fclose(f); | |
491 | ||
492 | /* open */ | |
493 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open'," | |
494 | " 'arguments': { 'path': 'foo', 'mode': 'r' } }"); | |
495 | g_assert_nonnull(ret); | |
496 | qmp_assert_no_error(ret); | |
497 | id = qdict_get_int(ret, "return"); | |
cb3e7f08 | 498 | qobject_unref(ret); |
62c39b30 MAL |
499 | |
500 | /* read */ | |
501 | cmd = g_strdup_printf("{'execute': 'guest-file-read'," | |
502 | " 'arguments': { 'handle': %" PRId64 "} }", | |
503 | id); | |
504 | ret = qmp_fd(fixture->fd, cmd); | |
505 | val = qdict_get_qdict(ret, "return"); | |
506 | count = qdict_get_int(val, "count"); | |
507 | eof = qdict_get_bool(val, "eof"); | |
508 | b64 = qdict_get_str(val, "buf-b64"); | |
509 | g_assert_cmpint(count, ==, sizeof(helloworld)); | |
510 | g_assert(eof); | |
511 | g_assert_cmpstr(b64, ==, enc); | |
512 | ||
cb3e7f08 | 513 | qobject_unref(ret); |
62c39b30 MAL |
514 | g_free(cmd); |
515 | g_free(enc); | |
516 | ||
517 | /* read eof */ | |
518 | cmd = g_strdup_printf("{'execute': 'guest-file-read'," | |
519 | " 'arguments': { 'handle': %" PRId64 "} }", | |
520 | id); | |
521 | ret = qmp_fd(fixture->fd, cmd); | |
522 | val = qdict_get_qdict(ret, "return"); | |
523 | count = qdict_get_int(val, "count"); | |
524 | eof = qdict_get_bool(val, "eof"); | |
525 | b64 = qdict_get_str(val, "buf-b64"); | |
526 | g_assert_cmpint(count, ==, 0); | |
527 | g_assert(eof); | |
528 | g_assert_cmpstr(b64, ==, ""); | |
cb3e7f08 | 529 | qobject_unref(ret); |
62c39b30 MAL |
530 | g_free(cmd); |
531 | ||
532 | /* seek */ | |
533 | cmd = g_strdup_printf("{'execute': 'guest-file-seek'," | |
534 | " 'arguments': { 'handle': %" PRId64 ", " | |
0b4b4938 EB |
535 | " 'offset': %d, 'whence': '%s' } }", |
536 | id, 6, "set"); | |
62c39b30 MAL |
537 | ret = qmp_fd(fixture->fd, cmd); |
538 | qmp_assert_no_error(ret); | |
539 | val = qdict_get_qdict(ret, "return"); | |
540 | count = qdict_get_int(val, "position"); | |
541 | eof = qdict_get_bool(val, "eof"); | |
542 | g_assert_cmpint(count, ==, 6); | |
543 | g_assert(!eof); | |
cb3e7f08 | 544 | qobject_unref(ret); |
62c39b30 MAL |
545 | g_free(cmd); |
546 | ||
547 | /* partial read */ | |
548 | cmd = g_strdup_printf("{'execute': 'guest-file-read'," | |
549 | " 'arguments': { 'handle': %" PRId64 "} }", | |
550 | id); | |
551 | ret = qmp_fd(fixture->fd, cmd); | |
552 | val = qdict_get_qdict(ret, "return"); | |
553 | count = qdict_get_int(val, "count"); | |
554 | eof = qdict_get_bool(val, "eof"); | |
555 | b64 = qdict_get_str(val, "buf-b64"); | |
556 | g_assert_cmpint(count, ==, sizeof(helloworld) - 6); | |
557 | g_assert(eof); | |
558 | dec = g_base64_decode(b64, &count); | |
559 | g_assert_cmpint(count, ==, sizeof(helloworld) - 6); | |
560 | g_assert_cmpmem(dec, count, helloworld + 6, sizeof(helloworld) - 6); | |
561 | g_free(dec); | |
562 | ||
cb3e7f08 | 563 | qobject_unref(ret); |
62c39b30 MAL |
564 | g_free(cmd); |
565 | ||
566 | /* close */ | |
567 | cmd = g_strdup_printf("{'execute': 'guest-file-close'," | |
568 | " 'arguments': {'handle': %" PRId64 "} }", | |
569 | id); | |
570 | ret = qmp_fd(fixture->fd, cmd); | |
cb3e7f08 | 571 | qobject_unref(ret); |
62c39b30 MAL |
572 | g_free(cmd); |
573 | } | |
574 | ||
4eaab85c MAL |
575 | static void test_qga_file_write_read(gconstpointer fix) |
576 | { | |
577 | const TestFixture *fixture = fix; | |
578 | const unsigned char helloworld[] = "Hello World!\n"; | |
579 | const char *b64; | |
580 | gchar *cmd, *enc; | |
581 | QDict *ret, *val; | |
582 | int64_t id, eof; | |
583 | gsize count; | |
584 | ||
585 | /* open */ | |
586 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open'," | |
587 | " 'arguments': { 'path': 'foo', 'mode': 'w+' } }"); | |
588 | g_assert_nonnull(ret); | |
589 | qmp_assert_no_error(ret); | |
590 | id = qdict_get_int(ret, "return"); | |
cb3e7f08 | 591 | qobject_unref(ret); |
4eaab85c MAL |
592 | |
593 | enc = g_base64_encode(helloworld, sizeof(helloworld)); | |
594 | /* write */ | |
595 | cmd = g_strdup_printf("{'execute': 'guest-file-write'," | |
596 | " 'arguments': { 'handle': %" PRId64 "," | |
597 | " 'buf-b64': '%s' } }", id, enc); | |
598 | ret = qmp_fd(fixture->fd, cmd); | |
599 | g_assert_nonnull(ret); | |
600 | qmp_assert_no_error(ret); | |
601 | ||
602 | val = qdict_get_qdict(ret, "return"); | |
603 | count = qdict_get_int(val, "count"); | |
604 | eof = qdict_get_bool(val, "eof"); | |
605 | g_assert_cmpint(count, ==, sizeof(helloworld)); | |
606 | g_assert_cmpint(eof, ==, 0); | |
cb3e7f08 | 607 | qobject_unref(ret); |
4eaab85c MAL |
608 | g_free(cmd); |
609 | ||
610 | /* read (check implicit flush) */ | |
611 | cmd = g_strdup_printf("{'execute': 'guest-file-read'," | |
612 | " 'arguments': { 'handle': %" PRId64 "} }", | |
613 | id); | |
614 | ret = qmp_fd(fixture->fd, cmd); | |
615 | val = qdict_get_qdict(ret, "return"); | |
616 | count = qdict_get_int(val, "count"); | |
617 | eof = qdict_get_bool(val, "eof"); | |
618 | b64 = qdict_get_str(val, "buf-b64"); | |
619 | g_assert_cmpint(count, ==, 0); | |
620 | g_assert(eof); | |
621 | g_assert_cmpstr(b64, ==, ""); | |
cb3e7f08 | 622 | qobject_unref(ret); |
4eaab85c MAL |
623 | g_free(cmd); |
624 | ||
625 | /* seek to 0 */ | |
626 | cmd = g_strdup_printf("{'execute': 'guest-file-seek'," | |
627 | " 'arguments': { 'handle': %" PRId64 ", " | |
0b4b4938 EB |
628 | " 'offset': %d, 'whence': '%s' } }", |
629 | id, 0, "set"); | |
4eaab85c MAL |
630 | ret = qmp_fd(fixture->fd, cmd); |
631 | qmp_assert_no_error(ret); | |
632 | val = qdict_get_qdict(ret, "return"); | |
633 | count = qdict_get_int(val, "position"); | |
634 | eof = qdict_get_bool(val, "eof"); | |
635 | g_assert_cmpint(count, ==, 0); | |
636 | g_assert(!eof); | |
cb3e7f08 | 637 | qobject_unref(ret); |
4eaab85c MAL |
638 | g_free(cmd); |
639 | ||
640 | /* read */ | |
641 | cmd = g_strdup_printf("{'execute': 'guest-file-read'," | |
642 | " 'arguments': { 'handle': %" PRId64 "} }", | |
643 | id); | |
644 | ret = qmp_fd(fixture->fd, cmd); | |
645 | val = qdict_get_qdict(ret, "return"); | |
646 | count = qdict_get_int(val, "count"); | |
647 | eof = qdict_get_bool(val, "eof"); | |
648 | b64 = qdict_get_str(val, "buf-b64"); | |
649 | g_assert_cmpint(count, ==, sizeof(helloworld)); | |
650 | g_assert(eof); | |
651 | g_assert_cmpstr(b64, ==, enc); | |
cb3e7f08 | 652 | qobject_unref(ret); |
4eaab85c MAL |
653 | g_free(cmd); |
654 | g_free(enc); | |
655 | ||
656 | /* close */ | |
657 | cmd = g_strdup_printf("{'execute': 'guest-file-close'," | |
658 | " 'arguments': {'handle': %" PRId64 "} }", | |
659 | id); | |
660 | ret = qmp_fd(fixture->fd, cmd); | |
cb3e7f08 | 661 | qobject_unref(ret); |
4eaab85c MAL |
662 | g_free(cmd); |
663 | } | |
664 | ||
62c39b30 MAL |
665 | static void test_qga_get_time(gconstpointer fix) |
666 | { | |
667 | const TestFixture *fixture = fix; | |
668 | QDict *ret; | |
669 | int64_t time; | |
670 | ||
671 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}"); | |
672 | g_assert_nonnull(ret); | |
673 | qmp_assert_no_error(ret); | |
674 | ||
675 | time = qdict_get_int(ret, "return"); | |
676 | g_assert_cmpint(time, >, 0); | |
677 | ||
cb3e7f08 | 678 | qobject_unref(ret); |
62c39b30 MAL |
679 | } |
680 | ||
62c39b30 MAL |
681 | static void test_qga_blacklist(gconstpointer data) |
682 | { | |
683 | TestFixture fix; | |
684 | QDict *ret, *error; | |
685 | const gchar *class, *desc; | |
686 | ||
c28afa76 | 687 | fixture_setup(&fix, "-b guest-ping,guest-get-time", NULL); |
62c39b30 MAL |
688 | |
689 | /* check blacklist */ | |
690 | ret = qmp_fd(fix.fd, "{'execute': 'guest-ping'}"); | |
691 | g_assert_nonnull(ret); | |
692 | error = qdict_get_qdict(ret, "error"); | |
693 | class = qdict_get_try_str(error, "class"); | |
694 | desc = qdict_get_try_str(error, "desc"); | |
695 | g_assert_cmpstr(class, ==, "GenericError"); | |
696 | g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled")); | |
cb3e7f08 | 697 | qobject_unref(ret); |
62c39b30 MAL |
698 | |
699 | ret = qmp_fd(fix.fd, "{'execute': 'guest-get-time'}"); | |
700 | g_assert_nonnull(ret); | |
701 | error = qdict_get_qdict(ret, "error"); | |
702 | class = qdict_get_try_str(error, "class"); | |
703 | desc = qdict_get_try_str(error, "desc"); | |
704 | g_assert_cmpstr(class, ==, "GenericError"); | |
705 | g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled")); | |
cb3e7f08 | 706 | qobject_unref(ret); |
62c39b30 MAL |
707 | |
708 | /* check something work */ | |
709 | ret = qmp_fd(fix.fd, "{'execute': 'guest-get-fsinfo'}"); | |
710 | qmp_assert_no_error(ret); | |
cb3e7f08 | 711 | qobject_unref(ret); |
62c39b30 MAL |
712 | |
713 | fixture_tear_down(&fix, NULL); | |
714 | } | |
715 | ||
716 | static void test_qga_config(gconstpointer data) | |
717 | { | |
718 | GError *error = NULL; | |
1741b945 | 719 | char *cwd, *cmd, *out, *err, *str, **strv, **argv = NULL; |
62c39b30 | 720 | char *env[2]; |
1741b945 | 721 | int status; |
62c39b30 MAL |
722 | gsize n; |
723 | GKeyFile *kf; | |
62c39b30 MAL |
724 | |
725 | cwd = g_get_current_dir(); | |
726 | cmd = g_strdup_printf("%s%cqemu-ga -D", | |
727 | cwd, G_DIR_SEPARATOR); | |
1e271338 | 728 | g_free(cwd); |
62c39b30 | 729 | g_shell_parse_argv(cmd, NULL, &argv, &error); |
1e271338 | 730 | g_free(cmd); |
62c39b30 MAL |
731 | g_assert_no_error(error); |
732 | ||
1741b945 MAL |
733 | env[0] = g_strdup_printf("QGA_CONF=tests%cdata%ctest-qga-config", |
734 | G_DIR_SEPARATOR, G_DIR_SEPARATOR); | |
62c39b30 MAL |
735 | env[1] = NULL; |
736 | g_spawn_sync(NULL, argv, env, 0, | |
737 | NULL, NULL, &out, &err, &status, &error); | |
1e271338 MAL |
738 | g_strfreev(argv); |
739 | ||
62c39b30 MAL |
740 | g_assert_no_error(error); |
741 | g_assert_cmpstr(err, ==, ""); | |
742 | g_assert_cmpint(status, ==, 0); | |
743 | ||
744 | kf = g_key_file_new(); | |
745 | g_key_file_load_from_data(kf, out, -1, G_KEY_FILE_NONE, &error); | |
746 | g_assert_no_error(error); | |
747 | ||
748 | str = g_key_file_get_start_group(kf); | |
749 | g_assert_cmpstr(str, ==, "general"); | |
750 | g_free(str); | |
751 | ||
752 | g_assert_false(g_key_file_get_boolean(kf, "general", "daemon", &error)); | |
753 | g_assert_no_error(error); | |
754 | ||
755 | str = g_key_file_get_string(kf, "general", "method", &error); | |
756 | g_assert_no_error(error); | |
757 | g_assert_cmpstr(str, ==, "virtio-serial"); | |
758 | g_free(str); | |
759 | ||
760 | str = g_key_file_get_string(kf, "general", "path", &error); | |
761 | g_assert_no_error(error); | |
762 | g_assert_cmpstr(str, ==, "/path/to/org.qemu.guest_agent.0"); | |
763 | g_free(str); | |
764 | ||
765 | str = g_key_file_get_string(kf, "general", "pidfile", &error); | |
766 | g_assert_no_error(error); | |
767 | g_assert_cmpstr(str, ==, "/var/foo/qemu-ga.pid"); | |
768 | g_free(str); | |
769 | ||
770 | str = g_key_file_get_string(kf, "general", "statedir", &error); | |
771 | g_assert_no_error(error); | |
772 | g_assert_cmpstr(str, ==, "/var/state"); | |
773 | g_free(str); | |
774 | ||
775 | g_assert_true(g_key_file_get_boolean(kf, "general", "verbose", &error)); | |
776 | g_assert_no_error(error); | |
777 | ||
778 | strv = g_key_file_get_string_list(kf, "general", "blacklist", &n, &error); | |
779 | g_assert_cmpint(n, ==, 2); | |
62c39b30 MAL |
780 | g_assert_true(g_strv_contains((const char * const *)strv, |
781 | "guest-ping")); | |
782 | g_assert_true(g_strv_contains((const char * const *)strv, | |
783 | "guest-get-time")); | |
62c39b30 MAL |
784 | g_assert_no_error(error); |
785 | g_strfreev(strv); | |
786 | ||
787 | g_free(out); | |
788 | g_free(err); | |
62c39b30 MAL |
789 | g_free(env[0]); |
790 | g_key_file_free(kf); | |
62c39b30 MAL |
791 | } |
792 | ||
793 | static void test_qga_fsfreeze_status(gconstpointer fix) | |
794 | { | |
795 | const TestFixture *fixture = fix; | |
796 | QDict *ret; | |
797 | const gchar *status; | |
798 | ||
799 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-status'}"); | |
800 | g_assert_nonnull(ret); | |
801 | qmp_assert_no_error(ret); | |
802 | ||
803 | status = qdict_get_try_str(ret, "return"); | |
804 | g_assert_cmpstr(status, ==, "thawed"); | |
805 | ||
cb3e7f08 | 806 | qobject_unref(ret); |
62c39b30 MAL |
807 | } |
808 | ||
3dab9fa1 MAL |
809 | static void test_qga_guest_exec(gconstpointer fix) |
810 | { | |
811 | const TestFixture *fixture = fix; | |
812 | QDict *ret, *val; | |
813 | const gchar *out; | |
814 | guchar *decoded; | |
815 | int64_t pid, now, exitcode; | |
816 | gsize len; | |
817 | bool exited; | |
1792d7d0 | 818 | char *cmd; |
3dab9fa1 MAL |
819 | |
820 | /* exec 'echo foo bar' */ | |
821 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {" | |
822 | " 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ]," | |
823 | " 'capture-output': true } }"); | |
824 | g_assert_nonnull(ret); | |
825 | qmp_assert_no_error(ret); | |
826 | val = qdict_get_qdict(ret, "return"); | |
827 | pid = qdict_get_int(val, "pid"); | |
828 | g_assert_cmpint(pid, >, 0); | |
cb3e7f08 | 829 | qobject_unref(ret); |
3dab9fa1 MAL |
830 | |
831 | /* wait for completion */ | |
832 | now = g_get_monotonic_time(); | |
1792d7d0 EB |
833 | cmd = g_strdup_printf("{'execute': 'guest-exec-status'," |
834 | " 'arguments': { 'pid': %" PRId64 " } }", pid); | |
3dab9fa1 | 835 | do { |
1792d7d0 | 836 | ret = qmp_fd(fixture->fd, cmd); |
3dab9fa1 MAL |
837 | g_assert_nonnull(ret); |
838 | val = qdict_get_qdict(ret, "return"); | |
839 | exited = qdict_get_bool(val, "exited"); | |
840 | if (!exited) { | |
cb3e7f08 | 841 | qobject_unref(ret); |
3dab9fa1 MAL |
842 | } |
843 | } while (!exited && | |
844 | g_get_monotonic_time() < now + 5 * G_TIME_SPAN_SECOND); | |
845 | g_assert(exited); | |
1792d7d0 | 846 | g_free(cmd); |
3dab9fa1 MAL |
847 | |
848 | /* check stdout */ | |
849 | exitcode = qdict_get_int(val, "exitcode"); | |
850 | g_assert_cmpint(exitcode, ==, 0); | |
851 | out = qdict_get_str(val, "out-data"); | |
852 | decoded = g_base64_decode(out, &len); | |
853 | g_assert_cmpint(len, ==, 12); | |
854 | g_assert_cmpstr((char *)decoded, ==, "\" test_str \""); | |
855 | g_free(decoded); | |
cb3e7f08 | 856 | qobject_unref(ret); |
3dab9fa1 MAL |
857 | } |
858 | ||
859 | static void test_qga_guest_exec_invalid(gconstpointer fix) | |
860 | { | |
861 | const TestFixture *fixture = fix; | |
862 | QDict *ret, *error; | |
863 | const gchar *class, *desc; | |
864 | ||
865 | /* invalid command */ | |
866 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {" | |
867 | " 'path': '/bin/invalid-cmd42' } }"); | |
868 | g_assert_nonnull(ret); | |
869 | error = qdict_get_qdict(ret, "error"); | |
870 | g_assert_nonnull(error); | |
871 | class = qdict_get_str(error, "class"); | |
872 | desc = qdict_get_str(error, "desc"); | |
873 | g_assert_cmpstr(class, ==, "GenericError"); | |
874 | g_assert_cmpint(strlen(desc), >, 0); | |
cb3e7f08 | 875 | qobject_unref(ret); |
3dab9fa1 MAL |
876 | |
877 | /* invalid pid */ | |
878 | ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec-status'," | |
879 | " 'arguments': { 'pid': 0 } }"); | |
880 | g_assert_nonnull(ret); | |
881 | error = qdict_get_qdict(ret, "error"); | |
882 | g_assert_nonnull(error); | |
883 | class = qdict_get_str(error, "class"); | |
884 | desc = qdict_get_str(error, "desc"); | |
885 | g_assert_cmpstr(class, ==, "GenericError"); | |
886 | g_assert_cmpint(strlen(desc), >, 0); | |
cb3e7f08 | 887 | qobject_unref(ret); |
3dab9fa1 MAL |
888 | } |
889 | ||
339ca68b TG |
890 | static void test_qga_guest_get_osinfo(gconstpointer data) |
891 | { | |
892 | TestFixture fixture; | |
893 | const gchar *str; | |
894 | gchar *cwd, *env[2]; | |
895 | QDict *ret, *val; | |
896 | ||
897 | cwd = g_get_current_dir(); | |
898 | env[0] = g_strdup_printf( | |
899 | "QGA_OS_RELEASE=%s%ctests%cdata%ctest-qga-os-release", | |
900 | cwd, G_DIR_SEPARATOR, G_DIR_SEPARATOR, G_DIR_SEPARATOR); | |
901 | env[1] = NULL; | |
902 | g_free(cwd); | |
903 | fixture_setup(&fixture, NULL, env); | |
904 | ||
905 | ret = qmp_fd(fixture.fd, "{'execute': 'guest-get-osinfo'}"); | |
906 | g_assert_nonnull(ret); | |
907 | qmp_assert_no_error(ret); | |
908 | ||
909 | val = qdict_get_qdict(ret, "return"); | |
910 | ||
911 | str = qdict_get_try_str(val, "id"); | |
912 | g_assert_nonnull(str); | |
913 | g_assert_cmpstr(str, ==, "qemu-ga-test"); | |
914 | ||
915 | str = qdict_get_try_str(val, "name"); | |
916 | g_assert_nonnull(str); | |
917 | g_assert_cmpstr(str, ==, "QEMU-GA"); | |
918 | ||
919 | str = qdict_get_try_str(val, "pretty-name"); | |
920 | g_assert_nonnull(str); | |
921 | g_assert_cmpstr(str, ==, "QEMU Guest Agent test"); | |
922 | ||
923 | str = qdict_get_try_str(val, "version"); | |
924 | g_assert_nonnull(str); | |
925 | g_assert_cmpstr(str, ==, "Test 1"); | |
926 | ||
927 | str = qdict_get_try_str(val, "version-id"); | |
928 | g_assert_nonnull(str); | |
929 | g_assert_cmpstr(str, ==, "1"); | |
930 | ||
931 | str = qdict_get_try_str(val, "variant"); | |
932 | g_assert_nonnull(str); | |
933 | g_assert_cmpstr(str, ==, "Unit test \"'$`\\ and \\\\ etc."); | |
934 | ||
935 | str = qdict_get_try_str(val, "variant-id"); | |
936 | g_assert_nonnull(str); | |
937 | g_assert_cmpstr(str, ==, "unit-test"); | |
938 | ||
cb3e7f08 | 939 | qobject_unref(ret); |
339ca68b TG |
940 | g_free(env[0]); |
941 | fixture_tear_down(&fixture, NULL); | |
942 | } | |
943 | ||
62c39b30 MAL |
944 | int main(int argc, char **argv) |
945 | { | |
946 | TestFixture fix; | |
947 | int ret; | |
948 | ||
949 | setlocale (LC_ALL, ""); | |
950 | g_test_init(&argc, &argv, NULL); | |
c28afa76 | 951 | fixture_setup(&fix, NULL, NULL); |
62c39b30 MAL |
952 | |
953 | g_test_add_data_func("/qga/sync-delimited", &fix, test_qga_sync_delimited); | |
954 | g_test_add_data_func("/qga/sync", &fix, test_qga_sync); | |
955 | g_test_add_data_func("/qga/ping", &fix, test_qga_ping); | |
956 | g_test_add_data_func("/qga/info", &fix, test_qga_info); | |
957 | g_test_add_data_func("/qga/network-get-interfaces", &fix, | |
958 | test_qga_network_get_interfaces); | |
ec72c0e2 BR |
959 | if (!access("/sys/devices/system/cpu/cpu0", F_OK)) { |
960 | g_test_add_data_func("/qga/get-vcpus", &fix, test_qga_get_vcpus); | |
961 | } | |
62c39b30 MAL |
962 | g_test_add_data_func("/qga/get-fsinfo", &fix, test_qga_get_fsinfo); |
963 | g_test_add_data_func("/qga/get-memory-block-info", &fix, | |
964 | test_qga_get_memory_block_info); | |
965 | g_test_add_data_func("/qga/get-memory-blocks", &fix, | |
966 | test_qga_get_memory_blocks); | |
967 | g_test_add_data_func("/qga/file-ops", &fix, test_qga_file_ops); | |
4eaab85c | 968 | g_test_add_data_func("/qga/file-write-read", &fix, test_qga_file_write_read); |
62c39b30 | 969 | g_test_add_data_func("/qga/get-time", &fix, test_qga_get_time); |
b5f84310 | 970 | g_test_add_data_func("/qga/invalid-id", &fix, test_qga_invalid_id); |
d4d7ed73 | 971 | g_test_add_data_func("/qga/invalid-oob", &fix, test_qga_invalid_oob); |
62c39b30 | 972 | g_test_add_data_func("/qga/invalid-cmd", &fix, test_qga_invalid_cmd); |
4bdadd86 | 973 | g_test_add_data_func("/qga/invalid-args", &fix, test_qga_invalid_args); |
62c39b30 MAL |
974 | g_test_add_data_func("/qga/fsfreeze-status", &fix, |
975 | test_qga_fsfreeze_status); | |
976 | ||
977 | g_test_add_data_func("/qga/blacklist", NULL, test_qga_blacklist); | |
978 | g_test_add_data_func("/qga/config", NULL, test_qga_config); | |
3dab9fa1 MAL |
979 | g_test_add_data_func("/qga/guest-exec", &fix, test_qga_guest_exec); |
980 | g_test_add_data_func("/qga/guest-exec-invalid", &fix, | |
981 | test_qga_guest_exec_invalid); | |
339ca68b TG |
982 | g_test_add_data_func("/qga/guest-get-osinfo", &fix, |
983 | test_qga_guest_get_osinfo); | |
62c39b30 | 984 | |
62c39b30 MAL |
985 | ret = g_test_run(); |
986 | ||
987 | fixture_tear_down(&fix, NULL); | |
988 | ||
989 | return ret; | |
990 | } |