json: Update references to RFC 7159 to RFC 8259
[qemu/ar7.git] / tests / test-qga.c
blobf69cdf6c03f235797ca0e9980b2f3f67e39f0775
1 #include "qemu/osdep.h"
2 #include <locale.h>
3 #include <glib/gstdio.h>
4 #include <sys/socket.h>
5 #include <sys/un.h>
7 #include "libqtest.h"
8 #include "qapi/qmp/qdict.h"
9 #include "qapi/qmp/qlist.h"
11 typedef struct {
12 char *test_dir;
13 GMainLoop *loop;
14 int fd;
15 GPid pid;
16 } TestFixture;
18 static int connect_qga(char *path)
20 int s, ret, len, i = 0;
21 struct sockaddr_un remote;
23 s = socket(AF_UNIX, SOCK_STREAM, 0);
24 g_assert(s != -1);
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);
34 if (i++ == 10) {
35 return -1;
37 } while (ret == -1);
39 return s;
42 static void qga_watch(GPid pid, gint status, gpointer user_data)
44 TestFixture *fixture = user_data;
46 g_assert_cmpint(status, ==, 0);
47 g_main_loop_quit(fixture->loop);
50 static void
51 fixture_setup(TestFixture *fixture, gconstpointer data, gchar **envp)
53 const gchar *extra_arg = data;
54 GError *error = NULL;
55 gchar *cwd, *path, *cmd, **argv = NULL;
57 fixture->loop = g_main_loop_new(NULL, FALSE);
59 fixture->test_dir = g_strdup("/tmp/qgatest.XXXXXX");
60 g_assert_nonnull(mkdtemp(fixture->test_dir));
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);
72 g_spawn_async(fixture->test_dir, argv, envp,
73 G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
74 NULL, NULL, &fixture->pid, &error);
75 g_assert_no_error(error);
77 g_child_watch_add(fixture->pid, qga_watch, fixture);
79 fixture->fd = connect_qga(path);
80 g_assert_cmpint(fixture->fd, !=, -1);
82 g_strfreev(argv);
83 g_free(cmd);
84 g_free(cwd);
85 g_free(path);
88 static void
89 fixture_tear_down(TestFixture *fixture, gconstpointer data)
91 gchar *tmp;
93 kill(fixture->pid, SIGTERM);
95 g_main_loop_run(fixture->loop);
96 g_main_loop_unref(fixture->loop);
98 g_spawn_close_pid(fixture->pid);
100 tmp = g_build_filename(fixture->test_dir, "foo", NULL);
101 g_unlink(tmp);
102 g_free(tmp);
104 tmp = g_build_filename(fixture->test_dir, "qga.state", NULL);
105 g_unlink(tmp);
106 g_free(tmp);
108 tmp = g_build_filename(fixture->test_dir, "sock", NULL);
109 g_unlink(tmp);
110 g_free(tmp);
112 g_rmdir(fixture->test_dir);
113 g_free(fixture->test_dir);
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)
123 const char *class, *desc;
124 char *s;
125 QDict *error;
127 error = qdict_get_qdict(dict, "error");
128 class = qdict_get_try_str(error, "class");
129 desc = qdict_get_try_str(error, "desc");
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);
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); \
141 } while (0)
143 static void test_qga_sync_delimited(gconstpointer fix)
145 const TestFixture *fixture = fix;
146 guint32 v, r = g_random_int();
147 unsigned char c;
148 QDict *ret;
150 qmp_fd_send_raw(fixture->fd, "\xff");
151 qmp_fd_send(fixture->fd,
152 "{'execute': 'guest-sync-delimited',"
153 " 'arguments': {'id': %u } }",
157 * Read and ignore garbage until resynchronized.
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.
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.
171 do {
172 v = read(fixture->fd, &c, 1);
173 g_assert_cmpint(v, ==, 1);
174 } while (c != 0xff);
176 ret = qmp_fd_receive(fixture->fd);
177 g_assert_nonnull(ret);
178 qmp_assert_no_error(ret);
180 v = qdict_get_int(ret, "return");
181 g_assert_cmpint(r, ==, v);
183 qobject_unref(ret);
186 static void test_qga_sync(gconstpointer fix)
188 const TestFixture *fixture = fix;
189 guint32 v, r = g_random_int();
190 QDict *ret;
193 * TODO guest-sync is inherently limited: we cannot distinguish
194 * failure caused by reacting to garbage on the wire prior to this
195 * command, from failure of this actual command. Clients are
196 * supposed to be able to send a raw '\xff' byte to at least
197 * re-synchronize the server's parser prior to this command, but
198 * we are not in a position to test that here because (at least
199 * for now) it causes the server to issue an error message about
200 * invalid JSON. Testing of '\xff' handling is done in
201 * guest-sync-delimited instead.
203 ret = qmp_fd(fixture->fd,
204 "{'execute': 'guest-sync', 'arguments': {'id': %u } }",
207 g_assert_nonnull(ret);
208 qmp_assert_no_error(ret);
210 v = qdict_get_int(ret, "return");
211 g_assert_cmpint(r, ==, v);
213 qobject_unref(ret);
216 static void test_qga_ping(gconstpointer fix)
218 const TestFixture *fixture = fix;
219 QDict *ret;
221 ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping'}");
222 g_assert_nonnull(ret);
223 qmp_assert_no_error(ret);
225 qobject_unref(ret);
228 static void test_qga_invalid_id(gconstpointer fix)
230 const TestFixture *fixture = fix;
231 QDict *ret, *error;
232 const char *class;
234 ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping', 'id': 1}");
235 g_assert_nonnull(ret);
237 error = qdict_get_qdict(ret, "error");
238 class = qdict_get_try_str(error, "class");
239 g_assert_cmpstr(class, ==, "GenericError");
241 qobject_unref(ret);
244 static void test_qga_invalid_oob(gconstpointer fix)
246 const TestFixture *fixture = fix;
247 QDict *ret, *error;
248 const char *class;
250 ret = qmp_fd(fixture->fd, "{'exec-oob': 'guest-ping'}");
251 g_assert_nonnull(ret);
253 error = qdict_get_qdict(ret, "error");
254 class = qdict_get_try_str(error, "class");
255 g_assert_cmpstr(class, ==, "GenericError");
257 qobject_unref(ret);
260 static void test_qga_invalid_args(gconstpointer fix)
262 const TestFixture *fixture = fix;
263 QDict *ret, *error;
264 const gchar *class, *desc;
266 ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping', "
267 "'arguments': {'foo': 42 }}");
268 g_assert_nonnull(ret);
270 error = qdict_get_qdict(ret, "error");
271 class = qdict_get_try_str(error, "class");
272 desc = qdict_get_try_str(error, "desc");
274 g_assert_cmpstr(class, ==, "GenericError");
275 g_assert_cmpstr(desc, ==, "Parameter 'foo' is unexpected");
277 qobject_unref(ret);
280 static void test_qga_invalid_cmd(gconstpointer fix)
282 const TestFixture *fixture = fix;
283 QDict *ret, *error;
284 const gchar *class, *desc;
286 ret = qmp_fd(fixture->fd, "{'execute': 'guest-invalid-cmd'}");
287 g_assert_nonnull(ret);
289 error = qdict_get_qdict(ret, "error");
290 class = qdict_get_try_str(error, "class");
291 desc = qdict_get_try_str(error, "desc");
293 g_assert_cmpstr(class, ==, "CommandNotFound");
294 g_assert_cmpint(strlen(desc), >, 0);
296 qobject_unref(ret);
299 static void test_qga_info(gconstpointer fix)
301 const TestFixture *fixture = fix;
302 QDict *ret, *val;
303 const gchar *version;
305 ret = qmp_fd(fixture->fd, "{'execute': 'guest-info'}");
306 g_assert_nonnull(ret);
307 qmp_assert_no_error(ret);
309 val = qdict_get_qdict(ret, "return");
310 version = qdict_get_try_str(val, "version");
311 g_assert_cmpstr(version, ==, QEMU_VERSION);
313 qobject_unref(ret);
316 static void test_qga_get_vcpus(gconstpointer fix)
318 const TestFixture *fixture = fix;
319 QDict *ret;
320 QList *list;
321 const QListEntry *entry;
323 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-vcpus'}");
324 g_assert_nonnull(ret);
325 qmp_assert_no_error(ret);
327 /* check there is at least a cpu */
328 list = qdict_get_qlist(ret, "return");
329 entry = qlist_first(list);
330 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "online"));
331 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "logical-id"));
333 qobject_unref(ret);
336 static void test_qga_get_fsinfo(gconstpointer fix)
338 const TestFixture *fixture = fix;
339 QDict *ret;
340 QList *list;
341 const QListEntry *entry;
343 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-fsinfo'}");
344 g_assert_nonnull(ret);
345 qmp_assert_no_error(ret);
347 /* sanity-check the response if there are any filesystems */
348 list = qdict_get_qlist(ret, "return");
349 entry = qlist_first(list);
350 if (entry) {
351 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "name"));
352 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "mountpoint"));
353 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "type"));
354 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "disk"));
357 qobject_unref(ret);
360 static void test_qga_get_memory_block_info(gconstpointer fix)
362 const TestFixture *fixture = fix;
363 QDict *ret, *val;
364 int64_t size;
366 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-block-info'}");
367 g_assert_nonnull(ret);
369 /* some systems might not expose memory block info in sysfs */
370 if (!qdict_haskey(ret, "error")) {
371 /* check there is at least some memory */
372 val = qdict_get_qdict(ret, "return");
373 size = qdict_get_int(val, "size");
374 g_assert_cmpint(size, >, 0);
377 qobject_unref(ret);
380 static void test_qga_get_memory_blocks(gconstpointer fix)
382 const TestFixture *fixture = fix;
383 QDict *ret;
384 QList *list;
385 const QListEntry *entry;
387 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-blocks'}");
388 g_assert_nonnull(ret);
390 /* some systems might not expose memory block info in sysfs */
391 if (!qdict_haskey(ret, "error")) {
392 list = qdict_get_qlist(ret, "return");
393 entry = qlist_first(list);
394 /* newer versions of qga may return empty list without error */
395 if (entry) {
396 g_assert(qdict_haskey(qobject_to(QDict, entry->value),
397 "phys-index"));
398 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "online"));
402 qobject_unref(ret);
405 static void test_qga_network_get_interfaces(gconstpointer fix)
407 const TestFixture *fixture = fix;
408 QDict *ret;
409 QList *list;
410 const QListEntry *entry;
412 ret = qmp_fd(fixture->fd, "{'execute': 'guest-network-get-interfaces'}");
413 g_assert_nonnull(ret);
414 qmp_assert_no_error(ret);
416 /* check there is at least an interface */
417 list = qdict_get_qlist(ret, "return");
418 entry = qlist_first(list);
419 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "name"));
421 qobject_unref(ret);
424 static void test_qga_file_ops(gconstpointer fix)
426 const TestFixture *fixture = fix;
427 const unsigned char helloworld[] = "Hello World!\n";
428 const char *b64;
429 gchar *path, *enc;
430 unsigned char *dec;
431 QDict *ret, *val;
432 int64_t id, eof;
433 gsize count;
434 FILE *f;
435 char tmp[100];
437 /* open */
438 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
439 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
440 g_assert_nonnull(ret);
441 qmp_assert_no_error(ret);
442 id = qdict_get_int(ret, "return");
443 qobject_unref(ret);
445 enc = g_base64_encode(helloworld, sizeof(helloworld));
446 /* write */
447 ret = qmp_fd(fixture->fd,
448 "{'execute': 'guest-file-write',"
449 " 'arguments': { 'handle': %" PRId64 ", 'buf-b64': %s } }",
450 id, enc);
451 g_assert_nonnull(ret);
452 qmp_assert_no_error(ret);
454 val = qdict_get_qdict(ret, "return");
455 count = qdict_get_int(val, "count");
456 eof = qdict_get_bool(val, "eof");
457 g_assert_cmpint(count, ==, sizeof(helloworld));
458 g_assert_cmpint(eof, ==, 0);
459 qobject_unref(ret);
461 /* flush */
462 ret = qmp_fd(fixture->fd,
463 "{'execute': 'guest-file-flush',"
464 " 'arguments': {'handle': %" PRId64 "} }",
465 id);
466 qobject_unref(ret);
468 /* close */
469 ret = qmp_fd(fixture->fd,
470 "{'execute': 'guest-file-close',"
471 " 'arguments': {'handle': %" PRId64 "} }",
472 id);
473 qobject_unref(ret);
475 /* check content */
476 path = g_build_filename(fixture->test_dir, "foo", NULL);
477 f = fopen(path, "r");
478 g_free(path);
479 g_assert_nonnull(f);
480 count = fread(tmp, 1, sizeof(tmp), f);
481 g_assert_cmpint(count, ==, sizeof(helloworld));
482 tmp[count] = 0;
483 g_assert_cmpstr(tmp, ==, (char *)helloworld);
484 fclose(f);
486 /* open */
487 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
488 " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
489 g_assert_nonnull(ret);
490 qmp_assert_no_error(ret);
491 id = qdict_get_int(ret, "return");
492 qobject_unref(ret);
494 /* read */
495 ret = qmp_fd(fixture->fd,
496 "{'execute': 'guest-file-read',"
497 " 'arguments': { 'handle': %" PRId64 "} }",
498 id);
499 val = qdict_get_qdict(ret, "return");
500 count = qdict_get_int(val, "count");
501 eof = qdict_get_bool(val, "eof");
502 b64 = qdict_get_str(val, "buf-b64");
503 g_assert_cmpint(count, ==, sizeof(helloworld));
504 g_assert(eof);
505 g_assert_cmpstr(b64, ==, enc);
507 qobject_unref(ret);
508 g_free(enc);
510 /* read eof */
511 ret = qmp_fd(fixture->fd,
512 "{'execute': 'guest-file-read',"
513 " 'arguments': { 'handle': %" PRId64 "} }",
514 id);
515 val = qdict_get_qdict(ret, "return");
516 count = qdict_get_int(val, "count");
517 eof = qdict_get_bool(val, "eof");
518 b64 = qdict_get_str(val, "buf-b64");
519 g_assert_cmpint(count, ==, 0);
520 g_assert(eof);
521 g_assert_cmpstr(b64, ==, "");
522 qobject_unref(ret);
524 /* seek */
525 ret = qmp_fd(fixture->fd,
526 "{'execute': 'guest-file-seek',"
527 " 'arguments': { 'handle': %" PRId64 ", "
528 " 'offset': %d, 'whence': %s } }",
529 id, 6, "set");
530 qmp_assert_no_error(ret);
531 val = qdict_get_qdict(ret, "return");
532 count = qdict_get_int(val, "position");
533 eof = qdict_get_bool(val, "eof");
534 g_assert_cmpint(count, ==, 6);
535 g_assert(!eof);
536 qobject_unref(ret);
538 /* partial read */
539 ret = qmp_fd(fixture->fd,
540 "{'execute': 'guest-file-read',"
541 " 'arguments': { 'handle': %" PRId64 "} }",
542 id);
543 val = qdict_get_qdict(ret, "return");
544 count = qdict_get_int(val, "count");
545 eof = qdict_get_bool(val, "eof");
546 b64 = qdict_get_str(val, "buf-b64");
547 g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
548 g_assert(eof);
549 dec = g_base64_decode(b64, &count);
550 g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
551 g_assert_cmpmem(dec, count, helloworld + 6, sizeof(helloworld) - 6);
552 g_free(dec);
554 qobject_unref(ret);
556 /* close */
557 ret = qmp_fd(fixture->fd,
558 "{'execute': 'guest-file-close',"
559 " 'arguments': {'handle': %" PRId64 "} }",
560 id);
561 qobject_unref(ret);
564 static void test_qga_file_write_read(gconstpointer fix)
566 const TestFixture *fixture = fix;
567 const unsigned char helloworld[] = "Hello World!\n";
568 const char *b64;
569 gchar *enc;
570 QDict *ret, *val;
571 int64_t id, eof;
572 gsize count;
574 /* open */
575 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
576 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
577 g_assert_nonnull(ret);
578 qmp_assert_no_error(ret);
579 id = qdict_get_int(ret, "return");
580 qobject_unref(ret);
582 enc = g_base64_encode(helloworld, sizeof(helloworld));
583 /* write */
584 ret = qmp_fd(fixture->fd,
585 "{'execute': 'guest-file-write',"
586 " 'arguments': { 'handle': %" PRId64 ","
587 " 'buf-b64': %s } }", id, enc);
588 g_assert_nonnull(ret);
589 qmp_assert_no_error(ret);
591 val = qdict_get_qdict(ret, "return");
592 count = qdict_get_int(val, "count");
593 eof = qdict_get_bool(val, "eof");
594 g_assert_cmpint(count, ==, sizeof(helloworld));
595 g_assert_cmpint(eof, ==, 0);
596 qobject_unref(ret);
598 /* read (check implicit flush) */
599 ret = qmp_fd(fixture->fd,
600 "{'execute': 'guest-file-read',"
601 " 'arguments': { 'handle': %" PRId64 "} }",
602 id);
603 val = qdict_get_qdict(ret, "return");
604 count = qdict_get_int(val, "count");
605 eof = qdict_get_bool(val, "eof");
606 b64 = qdict_get_str(val, "buf-b64");
607 g_assert_cmpint(count, ==, 0);
608 g_assert(eof);
609 g_assert_cmpstr(b64, ==, "");
610 qobject_unref(ret);
612 /* seek to 0 */
613 ret = qmp_fd(fixture->fd,
614 "{'execute': 'guest-file-seek',"
615 " 'arguments': { 'handle': %" PRId64 ", "
616 " 'offset': %d, 'whence': %s } }",
617 id, 0, "set");
618 qmp_assert_no_error(ret);
619 val = qdict_get_qdict(ret, "return");
620 count = qdict_get_int(val, "position");
621 eof = qdict_get_bool(val, "eof");
622 g_assert_cmpint(count, ==, 0);
623 g_assert(!eof);
624 qobject_unref(ret);
626 /* read */
627 ret = qmp_fd(fixture->fd,
628 "{'execute': 'guest-file-read',"
629 " 'arguments': { 'handle': %" PRId64 "} }",
630 id);
631 val = qdict_get_qdict(ret, "return");
632 count = qdict_get_int(val, "count");
633 eof = qdict_get_bool(val, "eof");
634 b64 = qdict_get_str(val, "buf-b64");
635 g_assert_cmpint(count, ==, sizeof(helloworld));
636 g_assert(eof);
637 g_assert_cmpstr(b64, ==, enc);
638 qobject_unref(ret);
639 g_free(enc);
641 /* close */
642 ret = qmp_fd(fixture->fd,
643 "{'execute': 'guest-file-close',"
644 " 'arguments': {'handle': %" PRId64 "} }",
645 id);
646 qobject_unref(ret);
649 static void test_qga_get_time(gconstpointer fix)
651 const TestFixture *fixture = fix;
652 QDict *ret;
653 int64_t time;
655 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
656 g_assert_nonnull(ret);
657 qmp_assert_no_error(ret);
659 time = qdict_get_int(ret, "return");
660 g_assert_cmpint(time, >, 0);
662 qobject_unref(ret);
665 static void test_qga_blacklist(gconstpointer data)
667 TestFixture fix;
668 QDict *ret, *error;
669 const gchar *class, *desc;
671 fixture_setup(&fix, "-b guest-ping,guest-get-time", NULL);
673 /* check blacklist */
674 ret = qmp_fd(fix.fd, "{'execute': 'guest-ping'}");
675 g_assert_nonnull(ret);
676 error = qdict_get_qdict(ret, "error");
677 class = qdict_get_try_str(error, "class");
678 desc = qdict_get_try_str(error, "desc");
679 g_assert_cmpstr(class, ==, "GenericError");
680 g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
681 qobject_unref(ret);
683 ret = qmp_fd(fix.fd, "{'execute': 'guest-get-time'}");
684 g_assert_nonnull(ret);
685 error = qdict_get_qdict(ret, "error");
686 class = qdict_get_try_str(error, "class");
687 desc = qdict_get_try_str(error, "desc");
688 g_assert_cmpstr(class, ==, "GenericError");
689 g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
690 qobject_unref(ret);
692 /* check something work */
693 ret = qmp_fd(fix.fd, "{'execute': 'guest-get-fsinfo'}");
694 qmp_assert_no_error(ret);
695 qobject_unref(ret);
697 fixture_tear_down(&fix, NULL);
700 static void test_qga_config(gconstpointer data)
702 GError *error = NULL;
703 char *cwd, *cmd, *out, *err, *str, **strv, **argv = NULL;
704 char *env[2];
705 int status;
706 gsize n;
707 GKeyFile *kf;
709 cwd = g_get_current_dir();
710 cmd = g_strdup_printf("%s%cqemu-ga -D",
711 cwd, G_DIR_SEPARATOR);
712 g_free(cwd);
713 g_shell_parse_argv(cmd, NULL, &argv, &error);
714 g_free(cmd);
715 g_assert_no_error(error);
717 env[0] = g_strdup_printf("QGA_CONF=tests%cdata%ctest-qga-config",
718 G_DIR_SEPARATOR, G_DIR_SEPARATOR);
719 env[1] = NULL;
720 g_spawn_sync(NULL, argv, env, 0,
721 NULL, NULL, &out, &err, &status, &error);
722 g_strfreev(argv);
724 g_assert_no_error(error);
725 g_assert_cmpstr(err, ==, "");
726 g_assert_cmpint(status, ==, 0);
728 kf = g_key_file_new();
729 g_key_file_load_from_data(kf, out, -1, G_KEY_FILE_NONE, &error);
730 g_assert_no_error(error);
732 str = g_key_file_get_start_group(kf);
733 g_assert_cmpstr(str, ==, "general");
734 g_free(str);
736 g_assert_false(g_key_file_get_boolean(kf, "general", "daemon", &error));
737 g_assert_no_error(error);
739 str = g_key_file_get_string(kf, "general", "method", &error);
740 g_assert_no_error(error);
741 g_assert_cmpstr(str, ==, "virtio-serial");
742 g_free(str);
744 str = g_key_file_get_string(kf, "general", "path", &error);
745 g_assert_no_error(error);
746 g_assert_cmpstr(str, ==, "/path/to/org.qemu.guest_agent.0");
747 g_free(str);
749 str = g_key_file_get_string(kf, "general", "pidfile", &error);
750 g_assert_no_error(error);
751 g_assert_cmpstr(str, ==, "/var/foo/qemu-ga.pid");
752 g_free(str);
754 str = g_key_file_get_string(kf, "general", "statedir", &error);
755 g_assert_no_error(error);
756 g_assert_cmpstr(str, ==, "/var/state");
757 g_free(str);
759 g_assert_true(g_key_file_get_boolean(kf, "general", "verbose", &error));
760 g_assert_no_error(error);
762 strv = g_key_file_get_string_list(kf, "general", "blacklist", &n, &error);
763 g_assert_cmpint(n, ==, 2);
764 g_assert_true(g_strv_contains((const char * const *)strv,
765 "guest-ping"));
766 g_assert_true(g_strv_contains((const char * const *)strv,
767 "guest-get-time"));
768 g_assert_no_error(error);
769 g_strfreev(strv);
771 g_free(out);
772 g_free(err);
773 g_free(env[0]);
774 g_key_file_free(kf);
777 static void test_qga_fsfreeze_status(gconstpointer fix)
779 const TestFixture *fixture = fix;
780 QDict *ret;
781 const gchar *status;
783 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-status'}");
784 g_assert_nonnull(ret);
785 qmp_assert_no_error(ret);
787 status = qdict_get_try_str(ret, "return");
788 g_assert_cmpstr(status, ==, "thawed");
790 qobject_unref(ret);
793 static void test_qga_guest_exec(gconstpointer fix)
795 const TestFixture *fixture = fix;
796 QDict *ret, *val;
797 const gchar *out;
798 guchar *decoded;
799 int64_t pid, now, exitcode;
800 gsize len;
801 bool exited;
803 /* exec 'echo foo bar' */
804 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
805 " 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ],"
806 " 'capture-output': true } }");
807 g_assert_nonnull(ret);
808 qmp_assert_no_error(ret);
809 val = qdict_get_qdict(ret, "return");
810 pid = qdict_get_int(val, "pid");
811 g_assert_cmpint(pid, >, 0);
812 qobject_unref(ret);
814 /* wait for completion */
815 now = g_get_monotonic_time();
816 do {
817 ret = qmp_fd(fixture->fd,
818 "{'execute': 'guest-exec-status',"
819 " 'arguments': { 'pid': %" PRId64 " } }", pid);
820 g_assert_nonnull(ret);
821 val = qdict_get_qdict(ret, "return");
822 exited = qdict_get_bool(val, "exited");
823 if (!exited) {
824 qobject_unref(ret);
826 } while (!exited &&
827 g_get_monotonic_time() < now + 5 * G_TIME_SPAN_SECOND);
828 g_assert(exited);
830 /* check stdout */
831 exitcode = qdict_get_int(val, "exitcode");
832 g_assert_cmpint(exitcode, ==, 0);
833 out = qdict_get_str(val, "out-data");
834 decoded = g_base64_decode(out, &len);
835 g_assert_cmpint(len, ==, 12);
836 g_assert_cmpstr((char *)decoded, ==, "\" test_str \"");
837 g_free(decoded);
838 qobject_unref(ret);
841 static void test_qga_guest_exec_invalid(gconstpointer fix)
843 const TestFixture *fixture = fix;
844 QDict *ret, *error;
845 const gchar *class, *desc;
847 /* invalid command */
848 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
849 " 'path': '/bin/invalid-cmd42' } }");
850 g_assert_nonnull(ret);
851 error = qdict_get_qdict(ret, "error");
852 g_assert_nonnull(error);
853 class = qdict_get_str(error, "class");
854 desc = qdict_get_str(error, "desc");
855 g_assert_cmpstr(class, ==, "GenericError");
856 g_assert_cmpint(strlen(desc), >, 0);
857 qobject_unref(ret);
859 /* invalid pid */
860 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec-status',"
861 " 'arguments': { 'pid': 0 } }");
862 g_assert_nonnull(ret);
863 error = qdict_get_qdict(ret, "error");
864 g_assert_nonnull(error);
865 class = qdict_get_str(error, "class");
866 desc = qdict_get_str(error, "desc");
867 g_assert_cmpstr(class, ==, "GenericError");
868 g_assert_cmpint(strlen(desc), >, 0);
869 qobject_unref(ret);
872 static void test_qga_guest_get_host_name(gconstpointer fix)
874 const TestFixture *fixture = fix;
875 QDict *ret, *val;
877 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-host-name'}");
878 g_assert_nonnull(ret);
879 qmp_assert_no_error(ret);
881 val = qdict_get_qdict(ret, "return");
882 g_assert(qdict_haskey(val, "host-name"));
884 qobject_unref(ret);
887 static void test_qga_guest_get_timezone(gconstpointer fix)
889 const TestFixture *fixture = fix;
890 QDict *ret, *val;
892 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-timezone'}");
893 g_assert_nonnull(ret);
894 qmp_assert_no_error(ret);
896 /* Make sure there's at least offset */
897 val = qdict_get_qdict(ret, "return");
898 g_assert(qdict_haskey(val, "offset"));
900 qobject_unref(ret);
903 static void test_qga_guest_get_users(gconstpointer fix)
905 const TestFixture *fixture = fix;
906 QDict *ret;
907 QList *val;
909 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-users'}");
910 g_assert_nonnull(ret);
911 qmp_assert_no_error(ret);
913 /* There is not much to test here */
914 val = qdict_get_qlist(ret, "return");
915 g_assert_nonnull(val);
917 qobject_unref(ret);
920 static void test_qga_guest_get_osinfo(gconstpointer data)
922 TestFixture fixture;
923 const gchar *str;
924 gchar *cwd, *env[2];
925 QDict *ret, *val;
927 cwd = g_get_current_dir();
928 env[0] = g_strdup_printf(
929 "QGA_OS_RELEASE=%s%ctests%cdata%ctest-qga-os-release",
930 cwd, G_DIR_SEPARATOR, G_DIR_SEPARATOR, G_DIR_SEPARATOR);
931 env[1] = NULL;
932 g_free(cwd);
933 fixture_setup(&fixture, NULL, env);
935 ret = qmp_fd(fixture.fd, "{'execute': 'guest-get-osinfo'}");
936 g_assert_nonnull(ret);
937 qmp_assert_no_error(ret);
939 val = qdict_get_qdict(ret, "return");
941 str = qdict_get_try_str(val, "id");
942 g_assert_nonnull(str);
943 g_assert_cmpstr(str, ==, "qemu-ga-test");
945 str = qdict_get_try_str(val, "name");
946 g_assert_nonnull(str);
947 g_assert_cmpstr(str, ==, "QEMU-GA");
949 str = qdict_get_try_str(val, "pretty-name");
950 g_assert_nonnull(str);
951 g_assert_cmpstr(str, ==, "QEMU Guest Agent test");
953 str = qdict_get_try_str(val, "version");
954 g_assert_nonnull(str);
955 g_assert_cmpstr(str, ==, "Test 1");
957 str = qdict_get_try_str(val, "version-id");
958 g_assert_nonnull(str);
959 g_assert_cmpstr(str, ==, "1");
961 str = qdict_get_try_str(val, "variant");
962 g_assert_nonnull(str);
963 g_assert_cmpstr(str, ==, "Unit test \"'$`\\ and \\\\ etc.");
965 str = qdict_get_try_str(val, "variant-id");
966 g_assert_nonnull(str);
967 g_assert_cmpstr(str, ==, "unit-test");
969 qobject_unref(ret);
970 g_free(env[0]);
971 fixture_tear_down(&fixture, NULL);
974 int main(int argc, char **argv)
976 TestFixture fix;
977 int ret;
979 setlocale (LC_ALL, "");
980 g_test_init(&argc, &argv, NULL);
981 fixture_setup(&fix, NULL, NULL);
983 g_test_add_data_func("/qga/sync-delimited", &fix, test_qga_sync_delimited);
984 g_test_add_data_func("/qga/sync", &fix, test_qga_sync);
985 g_test_add_data_func("/qga/ping", &fix, test_qga_ping);
986 g_test_add_data_func("/qga/info", &fix, test_qga_info);
987 g_test_add_data_func("/qga/network-get-interfaces", &fix,
988 test_qga_network_get_interfaces);
989 if (!access("/sys/devices/system/cpu/cpu0", F_OK)) {
990 g_test_add_data_func("/qga/get-vcpus", &fix, test_qga_get_vcpus);
992 g_test_add_data_func("/qga/get-fsinfo", &fix, test_qga_get_fsinfo);
993 g_test_add_data_func("/qga/get-memory-block-info", &fix,
994 test_qga_get_memory_block_info);
995 g_test_add_data_func("/qga/get-memory-blocks", &fix,
996 test_qga_get_memory_blocks);
997 g_test_add_data_func("/qga/file-ops", &fix, test_qga_file_ops);
998 g_test_add_data_func("/qga/file-write-read", &fix, test_qga_file_write_read);
999 g_test_add_data_func("/qga/get-time", &fix, test_qga_get_time);
1000 g_test_add_data_func("/qga/invalid-id", &fix, test_qga_invalid_id);
1001 g_test_add_data_func("/qga/invalid-oob", &fix, test_qga_invalid_oob);
1002 g_test_add_data_func("/qga/invalid-cmd", &fix, test_qga_invalid_cmd);
1003 g_test_add_data_func("/qga/invalid-args", &fix, test_qga_invalid_args);
1004 g_test_add_data_func("/qga/fsfreeze-status", &fix,
1005 test_qga_fsfreeze_status);
1007 g_test_add_data_func("/qga/blacklist", NULL, test_qga_blacklist);
1008 g_test_add_data_func("/qga/config", NULL, test_qga_config);
1009 g_test_add_data_func("/qga/guest-exec", &fix, test_qga_guest_exec);
1010 g_test_add_data_func("/qga/guest-exec-invalid", &fix,
1011 test_qga_guest_exec_invalid);
1012 g_test_add_data_func("/qga/guest-get-osinfo", &fix,
1013 test_qga_guest_get_osinfo);
1014 g_test_add_data_func("/qga/guest-get-host-name", &fix,
1015 test_qga_guest_get_host_name);
1016 g_test_add_data_func("/qga/guest-get-timezone", &fix,
1017 test_qga_guest_get_timezone);
1018 g_test_add_data_func("/qga/guest-get-users", &fix,
1019 test_qga_guest_get_users);
1021 ret = g_test_run();
1023 fixture_tear_down(&fix, NULL);
1025 return ret;