hw/arm/mps2: Fix ID register errors on AN511 and AN385
[qemu.git] / tests / test-qga.c
blobc552cc01258250bd8db07dbc7843a994fe8e8f4d
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(fixture->fd,
151 "\xff{'execute': 'guest-sync-delimited',"
152 " 'arguments': {'id': %u } }",
156 * Read and ignore garbage until resynchronized.
158 * Note that the full reset sequence would involve checking the
159 * response of guest-sync-delimited and repeating the loop if
160 * 'id' field of the response does not match the 'id' field of
161 * the request. Testing this fully would require inserting
162 * garbage in the response stream and is left as a future test
163 * to implement.
165 * TODO: The server shouldn't emit so much garbage (among other
166 * things, it loudly complains about the client's \xff being
167 * invalid JSON, even though it is a documented part of the
168 * handshake.
170 do {
171 v = read(fixture->fd, &c, 1);
172 g_assert_cmpint(v, ==, 1);
173 } while (c != 0xff);
175 ret = qmp_fd_receive(fixture->fd);
176 g_assert_nonnull(ret);
177 qmp_assert_no_error(ret);
179 v = qdict_get_int(ret, "return");
180 g_assert_cmpint(r, ==, v);
182 qobject_unref(ret);
185 static void test_qga_sync(gconstpointer fix)
187 const TestFixture *fixture = fix;
188 guint32 v, r = g_random_int();
189 QDict *ret;
192 * TODO guest-sync is inherently limited: we cannot distinguish
193 * failure caused by reacting to garbage on the wire prior to this
194 * command, from failure of this actual command. Clients are
195 * supposed to be able to send a raw '\xff' byte to at least
196 * re-synchronize the server's parser prior to this command, but
197 * we are not in a position to test that here because (at least
198 * for now) it causes the server to issue an error message about
199 * invalid JSON. Testing of '\xff' handling is done in
200 * guest-sync-delimited instead.
202 ret = qmp_fd(fixture->fd,
203 "{'execute': 'guest-sync', 'arguments': {'id': %u } }",
206 g_assert_nonnull(ret);
207 qmp_assert_no_error(ret);
209 v = qdict_get_int(ret, "return");
210 g_assert_cmpint(r, ==, v);
212 qobject_unref(ret);
215 static void test_qga_ping(gconstpointer fix)
217 const TestFixture *fixture = fix;
218 QDict *ret;
220 ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping'}");
221 g_assert_nonnull(ret);
222 qmp_assert_no_error(ret);
224 qobject_unref(ret);
227 static void test_qga_invalid_id(gconstpointer fix)
229 const TestFixture *fixture = fix;
230 QDict *ret, *error;
231 const char *class;
233 ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping', 'id': 1}");
234 g_assert_nonnull(ret);
236 error = qdict_get_qdict(ret, "error");
237 class = qdict_get_try_str(error, "class");
238 g_assert_cmpstr(class, ==, "GenericError");
240 qobject_unref(ret);
243 static void test_qga_invalid_oob(gconstpointer fix)
245 const TestFixture *fixture = fix;
246 QDict *ret, *error;
247 const char *class;
249 ret = qmp_fd(fixture->fd, "{'exec-oob': 'guest-ping'}");
250 g_assert_nonnull(ret);
252 error = qdict_get_qdict(ret, "error");
253 class = qdict_get_try_str(error, "class");
254 g_assert_cmpstr(class, ==, "GenericError");
256 qobject_unref(ret);
259 static void test_qga_invalid_args(gconstpointer fix)
261 const TestFixture *fixture = fix;
262 QDict *ret, *error;
263 const gchar *class, *desc;
265 ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping', "
266 "'arguments': {'foo': 42 }}");
267 g_assert_nonnull(ret);
269 error = qdict_get_qdict(ret, "error");
270 class = qdict_get_try_str(error, "class");
271 desc = qdict_get_try_str(error, "desc");
273 g_assert_cmpstr(class, ==, "GenericError");
274 g_assert_cmpstr(desc, ==, "Parameter 'foo' is unexpected");
276 qobject_unref(ret);
279 static void test_qga_invalid_cmd(gconstpointer fix)
281 const TestFixture *fixture = fix;
282 QDict *ret, *error;
283 const gchar *class, *desc;
285 ret = qmp_fd(fixture->fd, "{'execute': 'guest-invalid-cmd'}");
286 g_assert_nonnull(ret);
288 error = qdict_get_qdict(ret, "error");
289 class = qdict_get_try_str(error, "class");
290 desc = qdict_get_try_str(error, "desc");
292 g_assert_cmpstr(class, ==, "CommandNotFound");
293 g_assert_cmpint(strlen(desc), >, 0);
295 qobject_unref(ret);
298 static void test_qga_info(gconstpointer fix)
300 const TestFixture *fixture = fix;
301 QDict *ret, *val;
302 const gchar *version;
304 ret = qmp_fd(fixture->fd, "{'execute': 'guest-info'}");
305 g_assert_nonnull(ret);
306 qmp_assert_no_error(ret);
308 val = qdict_get_qdict(ret, "return");
309 version = qdict_get_try_str(val, "version");
310 g_assert_cmpstr(version, ==, QEMU_VERSION);
312 qobject_unref(ret);
315 static void test_qga_get_vcpus(gconstpointer fix)
317 const TestFixture *fixture = fix;
318 QDict *ret;
319 QList *list;
320 const QListEntry *entry;
322 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-vcpus'}");
323 g_assert_nonnull(ret);
324 qmp_assert_no_error(ret);
326 /* check there is at least a cpu */
327 list = qdict_get_qlist(ret, "return");
328 entry = qlist_first(list);
329 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "online"));
330 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "logical-id"));
332 qobject_unref(ret);
335 static void test_qga_get_fsinfo(gconstpointer fix)
337 const TestFixture *fixture = fix;
338 QDict *ret;
339 QList *list;
340 const QListEntry *entry;
342 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-fsinfo'}");
343 g_assert_nonnull(ret);
344 qmp_assert_no_error(ret);
346 /* sanity-check the response if there are any filesystems */
347 list = qdict_get_qlist(ret, "return");
348 entry = qlist_first(list);
349 if (entry) {
350 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "name"));
351 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "mountpoint"));
352 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "type"));
353 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "disk"));
356 qobject_unref(ret);
359 static void test_qga_get_memory_block_info(gconstpointer fix)
361 const TestFixture *fixture = fix;
362 QDict *ret, *val;
363 int64_t size;
365 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-block-info'}");
366 g_assert_nonnull(ret);
368 /* some systems might not expose memory block info in sysfs */
369 if (!qdict_haskey(ret, "error")) {
370 /* check there is at least some memory */
371 val = qdict_get_qdict(ret, "return");
372 size = qdict_get_int(val, "size");
373 g_assert_cmpint(size, >, 0);
376 qobject_unref(ret);
379 static void test_qga_get_memory_blocks(gconstpointer fix)
381 const TestFixture *fixture = fix;
382 QDict *ret;
383 QList *list;
384 const QListEntry *entry;
386 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-blocks'}");
387 g_assert_nonnull(ret);
389 /* some systems might not expose memory block info in sysfs */
390 if (!qdict_haskey(ret, "error")) {
391 list = qdict_get_qlist(ret, "return");
392 entry = qlist_first(list);
393 /* newer versions of qga may return empty list without error */
394 if (entry) {
395 g_assert(qdict_haskey(qobject_to(QDict, entry->value),
396 "phys-index"));
397 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "online"));
401 qobject_unref(ret);
404 static void test_qga_network_get_interfaces(gconstpointer fix)
406 const TestFixture *fixture = fix;
407 QDict *ret;
408 QList *list;
409 const QListEntry *entry;
411 ret = qmp_fd(fixture->fd, "{'execute': 'guest-network-get-interfaces'}");
412 g_assert_nonnull(ret);
413 qmp_assert_no_error(ret);
415 /* check there is at least an interface */
416 list = qdict_get_qlist(ret, "return");
417 entry = qlist_first(list);
418 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "name"));
420 qobject_unref(ret);
423 static void test_qga_file_ops(gconstpointer fix)
425 const TestFixture *fixture = fix;
426 const unsigned char helloworld[] = "Hello World!\n";
427 const char *b64;
428 gchar *path, *enc;
429 unsigned char *dec;
430 QDict *ret, *val;
431 int64_t id, eof;
432 gsize count;
433 FILE *f;
434 char tmp[100];
436 /* open */
437 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
438 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
439 g_assert_nonnull(ret);
440 qmp_assert_no_error(ret);
441 id = qdict_get_int(ret, "return");
442 qobject_unref(ret);
444 enc = g_base64_encode(helloworld, sizeof(helloworld));
445 /* write */
446 ret = qmp_fd(fixture->fd,
447 "{'execute': 'guest-file-write',"
448 " 'arguments': { 'handle': %" PRId64 ", 'buf-b64': %s } }",
449 id, enc);
450 g_assert_nonnull(ret);
451 qmp_assert_no_error(ret);
453 val = qdict_get_qdict(ret, "return");
454 count = qdict_get_int(val, "count");
455 eof = qdict_get_bool(val, "eof");
456 g_assert_cmpint(count, ==, sizeof(helloworld));
457 g_assert_cmpint(eof, ==, 0);
458 qobject_unref(ret);
460 /* flush */
461 ret = qmp_fd(fixture->fd,
462 "{'execute': 'guest-file-flush',"
463 " 'arguments': {'handle': %" PRId64 "} }",
464 id);
465 qobject_unref(ret);
467 /* close */
468 ret = qmp_fd(fixture->fd,
469 "{'execute': 'guest-file-close',"
470 " 'arguments': {'handle': %" PRId64 "} }",
471 id);
472 qobject_unref(ret);
474 /* check content */
475 path = g_build_filename(fixture->test_dir, "foo", NULL);
476 f = fopen(path, "r");
477 g_free(path);
478 g_assert_nonnull(f);
479 count = fread(tmp, 1, sizeof(tmp), f);
480 g_assert_cmpint(count, ==, sizeof(helloworld));
481 tmp[count] = 0;
482 g_assert_cmpstr(tmp, ==, (char *)helloworld);
483 fclose(f);
485 /* open */
486 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
487 " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
488 g_assert_nonnull(ret);
489 qmp_assert_no_error(ret);
490 id = qdict_get_int(ret, "return");
491 qobject_unref(ret);
493 /* read */
494 ret = qmp_fd(fixture->fd,
495 "{'execute': 'guest-file-read',"
496 " 'arguments': { 'handle': %" PRId64 "} }",
497 id);
498 val = qdict_get_qdict(ret, "return");
499 count = qdict_get_int(val, "count");
500 eof = qdict_get_bool(val, "eof");
501 b64 = qdict_get_str(val, "buf-b64");
502 g_assert_cmpint(count, ==, sizeof(helloworld));
503 g_assert(eof);
504 g_assert_cmpstr(b64, ==, enc);
506 qobject_unref(ret);
507 g_free(enc);
509 /* read eof */
510 ret = qmp_fd(fixture->fd,
511 "{'execute': 'guest-file-read',"
512 " 'arguments': { 'handle': %" PRId64 "} }",
513 id);
514 val = qdict_get_qdict(ret, "return");
515 count = qdict_get_int(val, "count");
516 eof = qdict_get_bool(val, "eof");
517 b64 = qdict_get_str(val, "buf-b64");
518 g_assert_cmpint(count, ==, 0);
519 g_assert(eof);
520 g_assert_cmpstr(b64, ==, "");
521 qobject_unref(ret);
523 /* seek */
524 ret = qmp_fd(fixture->fd,
525 "{'execute': 'guest-file-seek',"
526 " 'arguments': { 'handle': %" PRId64 ", "
527 " 'offset': %d, 'whence': %s } }",
528 id, 6, "set");
529 qmp_assert_no_error(ret);
530 val = qdict_get_qdict(ret, "return");
531 count = qdict_get_int(val, "position");
532 eof = qdict_get_bool(val, "eof");
533 g_assert_cmpint(count, ==, 6);
534 g_assert(!eof);
535 qobject_unref(ret);
537 /* partial read */
538 ret = qmp_fd(fixture->fd,
539 "{'execute': 'guest-file-read',"
540 " 'arguments': { 'handle': %" PRId64 "} }",
541 id);
542 val = qdict_get_qdict(ret, "return");
543 count = qdict_get_int(val, "count");
544 eof = qdict_get_bool(val, "eof");
545 b64 = qdict_get_str(val, "buf-b64");
546 g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
547 g_assert(eof);
548 dec = g_base64_decode(b64, &count);
549 g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
550 g_assert_cmpmem(dec, count, helloworld + 6, sizeof(helloworld) - 6);
551 g_free(dec);
553 qobject_unref(ret);
555 /* close */
556 ret = qmp_fd(fixture->fd,
557 "{'execute': 'guest-file-close',"
558 " 'arguments': {'handle': %" PRId64 "} }",
559 id);
560 qobject_unref(ret);
563 static void test_qga_file_write_read(gconstpointer fix)
565 const TestFixture *fixture = fix;
566 const unsigned char helloworld[] = "Hello World!\n";
567 const char *b64;
568 gchar *enc;
569 QDict *ret, *val;
570 int64_t id, eof;
571 gsize count;
573 /* open */
574 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
575 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
576 g_assert_nonnull(ret);
577 qmp_assert_no_error(ret);
578 id = qdict_get_int(ret, "return");
579 qobject_unref(ret);
581 enc = g_base64_encode(helloworld, sizeof(helloworld));
582 /* write */
583 ret = qmp_fd(fixture->fd,
584 "{'execute': 'guest-file-write',"
585 " 'arguments': { 'handle': %" PRId64 ","
586 " 'buf-b64': %s } }", id, enc);
587 g_assert_nonnull(ret);
588 qmp_assert_no_error(ret);
590 val = qdict_get_qdict(ret, "return");
591 count = qdict_get_int(val, "count");
592 eof = qdict_get_bool(val, "eof");
593 g_assert_cmpint(count, ==, sizeof(helloworld));
594 g_assert_cmpint(eof, ==, 0);
595 qobject_unref(ret);
597 /* read (check implicit flush) */
598 ret = qmp_fd(fixture->fd,
599 "{'execute': 'guest-file-read',"
600 " 'arguments': { 'handle': %" PRId64 "} }",
601 id);
602 val = qdict_get_qdict(ret, "return");
603 count = qdict_get_int(val, "count");
604 eof = qdict_get_bool(val, "eof");
605 b64 = qdict_get_str(val, "buf-b64");
606 g_assert_cmpint(count, ==, 0);
607 g_assert(eof);
608 g_assert_cmpstr(b64, ==, "");
609 qobject_unref(ret);
611 /* seek to 0 */
612 ret = qmp_fd(fixture->fd,
613 "{'execute': 'guest-file-seek',"
614 " 'arguments': { 'handle': %" PRId64 ", "
615 " 'offset': %d, 'whence': %s } }",
616 id, 0, "set");
617 qmp_assert_no_error(ret);
618 val = qdict_get_qdict(ret, "return");
619 count = qdict_get_int(val, "position");
620 eof = qdict_get_bool(val, "eof");
621 g_assert_cmpint(count, ==, 0);
622 g_assert(!eof);
623 qobject_unref(ret);
625 /* read */
626 ret = qmp_fd(fixture->fd,
627 "{'execute': 'guest-file-read',"
628 " 'arguments': { 'handle': %" PRId64 "} }",
629 id);
630 val = qdict_get_qdict(ret, "return");
631 count = qdict_get_int(val, "count");
632 eof = qdict_get_bool(val, "eof");
633 b64 = qdict_get_str(val, "buf-b64");
634 g_assert_cmpint(count, ==, sizeof(helloworld));
635 g_assert(eof);
636 g_assert_cmpstr(b64, ==, enc);
637 qobject_unref(ret);
638 g_free(enc);
640 /* close */
641 ret = qmp_fd(fixture->fd,
642 "{'execute': 'guest-file-close',"
643 " 'arguments': {'handle': %" PRId64 "} }",
644 id);
645 qobject_unref(ret);
648 static void test_qga_get_time(gconstpointer fix)
650 const TestFixture *fixture = fix;
651 QDict *ret;
652 int64_t time;
654 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
655 g_assert_nonnull(ret);
656 qmp_assert_no_error(ret);
658 time = qdict_get_int(ret, "return");
659 g_assert_cmpint(time, >, 0);
661 qobject_unref(ret);
664 static void test_qga_blacklist(gconstpointer data)
666 TestFixture fix;
667 QDict *ret, *error;
668 const gchar *class, *desc;
670 fixture_setup(&fix, "-b guest-ping,guest-get-time", NULL);
672 /* check blacklist */
673 ret = qmp_fd(fix.fd, "{'execute': 'guest-ping'}");
674 g_assert_nonnull(ret);
675 error = qdict_get_qdict(ret, "error");
676 class = qdict_get_try_str(error, "class");
677 desc = qdict_get_try_str(error, "desc");
678 g_assert_cmpstr(class, ==, "GenericError");
679 g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
680 qobject_unref(ret);
682 ret = qmp_fd(fix.fd, "{'execute': 'guest-get-time'}");
683 g_assert_nonnull(ret);
684 error = qdict_get_qdict(ret, "error");
685 class = qdict_get_try_str(error, "class");
686 desc = qdict_get_try_str(error, "desc");
687 g_assert_cmpstr(class, ==, "GenericError");
688 g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
689 qobject_unref(ret);
691 /* check something work */
692 ret = qmp_fd(fix.fd, "{'execute': 'guest-get-fsinfo'}");
693 qmp_assert_no_error(ret);
694 qobject_unref(ret);
696 fixture_tear_down(&fix, NULL);
699 static void test_qga_config(gconstpointer data)
701 GError *error = NULL;
702 char *cwd, *cmd, *out, *err, *str, **strv, **argv = NULL;
703 char *env[2];
704 int status;
705 gsize n;
706 GKeyFile *kf;
708 cwd = g_get_current_dir();
709 cmd = g_strdup_printf("%s%cqemu-ga -D",
710 cwd, G_DIR_SEPARATOR);
711 g_free(cwd);
712 g_shell_parse_argv(cmd, NULL, &argv, &error);
713 g_free(cmd);
714 g_assert_no_error(error);
716 env[0] = g_strdup_printf("QGA_CONF=tests%cdata%ctest-qga-config",
717 G_DIR_SEPARATOR, G_DIR_SEPARATOR);
718 env[1] = NULL;
719 g_spawn_sync(NULL, argv, env, 0,
720 NULL, NULL, &out, &err, &status, &error);
721 g_strfreev(argv);
723 g_assert_no_error(error);
724 g_assert_cmpstr(err, ==, "");
725 g_assert_cmpint(status, ==, 0);
727 kf = g_key_file_new();
728 g_key_file_load_from_data(kf, out, -1, G_KEY_FILE_NONE, &error);
729 g_assert_no_error(error);
731 str = g_key_file_get_start_group(kf);
732 g_assert_cmpstr(str, ==, "general");
733 g_free(str);
735 g_assert_false(g_key_file_get_boolean(kf, "general", "daemon", &error));
736 g_assert_no_error(error);
738 str = g_key_file_get_string(kf, "general", "method", &error);
739 g_assert_no_error(error);
740 g_assert_cmpstr(str, ==, "virtio-serial");
741 g_free(str);
743 str = g_key_file_get_string(kf, "general", "path", &error);
744 g_assert_no_error(error);
745 g_assert_cmpstr(str, ==, "/path/to/org.qemu.guest_agent.0");
746 g_free(str);
748 str = g_key_file_get_string(kf, "general", "pidfile", &error);
749 g_assert_no_error(error);
750 g_assert_cmpstr(str, ==, "/var/foo/qemu-ga.pid");
751 g_free(str);
753 str = g_key_file_get_string(kf, "general", "statedir", &error);
754 g_assert_no_error(error);
755 g_assert_cmpstr(str, ==, "/var/state");
756 g_free(str);
758 g_assert_true(g_key_file_get_boolean(kf, "general", "verbose", &error));
759 g_assert_no_error(error);
761 strv = g_key_file_get_string_list(kf, "general", "blacklist", &n, &error);
762 g_assert_cmpint(n, ==, 2);
763 g_assert_true(g_strv_contains((const char * const *)strv,
764 "guest-ping"));
765 g_assert_true(g_strv_contains((const char * const *)strv,
766 "guest-get-time"));
767 g_assert_no_error(error);
768 g_strfreev(strv);
770 g_free(out);
771 g_free(err);
772 g_free(env[0]);
773 g_key_file_free(kf);
776 static void test_qga_fsfreeze_status(gconstpointer fix)
778 const TestFixture *fixture = fix;
779 QDict *ret;
780 const gchar *status;
782 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-status'}");
783 g_assert_nonnull(ret);
784 qmp_assert_no_error(ret);
786 status = qdict_get_try_str(ret, "return");
787 g_assert_cmpstr(status, ==, "thawed");
789 qobject_unref(ret);
792 static void test_qga_guest_exec(gconstpointer fix)
794 const TestFixture *fixture = fix;
795 QDict *ret, *val;
796 const gchar *out;
797 guchar *decoded;
798 int64_t pid, now, exitcode;
799 gsize len;
800 bool exited;
802 /* exec 'echo foo bar' */
803 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
804 " 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ],"
805 " 'capture-output': true } }");
806 g_assert_nonnull(ret);
807 qmp_assert_no_error(ret);
808 val = qdict_get_qdict(ret, "return");
809 pid = qdict_get_int(val, "pid");
810 g_assert_cmpint(pid, >, 0);
811 qobject_unref(ret);
813 /* wait for completion */
814 now = g_get_monotonic_time();
815 do {
816 ret = qmp_fd(fixture->fd,
817 "{'execute': 'guest-exec-status',"
818 " 'arguments': { 'pid': %" PRId64 " } }", pid);
819 g_assert_nonnull(ret);
820 val = qdict_get_qdict(ret, "return");
821 exited = qdict_get_bool(val, "exited");
822 if (!exited) {
823 qobject_unref(ret);
825 } while (!exited &&
826 g_get_monotonic_time() < now + 5 * G_TIME_SPAN_SECOND);
827 g_assert(exited);
829 /* check stdout */
830 exitcode = qdict_get_int(val, "exitcode");
831 g_assert_cmpint(exitcode, ==, 0);
832 out = qdict_get_str(val, "out-data");
833 decoded = g_base64_decode(out, &len);
834 g_assert_cmpint(len, ==, 12);
835 g_assert_cmpstr((char *)decoded, ==, "\" test_str \"");
836 g_free(decoded);
837 qobject_unref(ret);
840 static void test_qga_guest_exec_invalid(gconstpointer fix)
842 const TestFixture *fixture = fix;
843 QDict *ret, *error;
844 const gchar *class, *desc;
846 /* invalid command */
847 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
848 " 'path': '/bin/invalid-cmd42' } }");
849 g_assert_nonnull(ret);
850 error = qdict_get_qdict(ret, "error");
851 g_assert_nonnull(error);
852 class = qdict_get_str(error, "class");
853 desc = qdict_get_str(error, "desc");
854 g_assert_cmpstr(class, ==, "GenericError");
855 g_assert_cmpint(strlen(desc), >, 0);
856 qobject_unref(ret);
858 /* invalid pid */
859 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec-status',"
860 " 'arguments': { 'pid': 0 } }");
861 g_assert_nonnull(ret);
862 error = qdict_get_qdict(ret, "error");
863 g_assert_nonnull(error);
864 class = qdict_get_str(error, "class");
865 desc = qdict_get_str(error, "desc");
866 g_assert_cmpstr(class, ==, "GenericError");
867 g_assert_cmpint(strlen(desc), >, 0);
868 qobject_unref(ret);
871 static void test_qga_guest_get_host_name(gconstpointer fix)
873 const TestFixture *fixture = fix;
874 QDict *ret, *val;
876 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-host-name'}");
877 g_assert_nonnull(ret);
878 qmp_assert_no_error(ret);
880 val = qdict_get_qdict(ret, "return");
881 g_assert(qdict_haskey(val, "host-name"));
883 qobject_unref(ret);
886 static void test_qga_guest_get_timezone(gconstpointer fix)
888 const TestFixture *fixture = fix;
889 QDict *ret, *val;
891 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-timezone'}");
892 g_assert_nonnull(ret);
893 qmp_assert_no_error(ret);
895 /* Make sure there's at least offset */
896 val = qdict_get_qdict(ret, "return");
897 g_assert(qdict_haskey(val, "offset"));
899 qobject_unref(ret);
902 static void test_qga_guest_get_users(gconstpointer fix)
904 const TestFixture *fixture = fix;
905 QDict *ret;
906 QList *val;
908 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-users'}");
909 g_assert_nonnull(ret);
910 qmp_assert_no_error(ret);
912 /* There is not much to test here */
913 val = qdict_get_qlist(ret, "return");
914 g_assert_nonnull(val);
916 qobject_unref(ret);
919 static void test_qga_guest_get_osinfo(gconstpointer data)
921 TestFixture fixture;
922 const gchar *str;
923 gchar *cwd, *env[2];
924 QDict *ret, *val;
926 cwd = g_get_current_dir();
927 env[0] = g_strdup_printf(
928 "QGA_OS_RELEASE=%s%ctests%cdata%ctest-qga-os-release",
929 cwd, G_DIR_SEPARATOR, G_DIR_SEPARATOR, G_DIR_SEPARATOR);
930 env[1] = NULL;
931 g_free(cwd);
932 fixture_setup(&fixture, NULL, env);
934 ret = qmp_fd(fixture.fd, "{'execute': 'guest-get-osinfo'}");
935 g_assert_nonnull(ret);
936 qmp_assert_no_error(ret);
938 val = qdict_get_qdict(ret, "return");
940 str = qdict_get_try_str(val, "id");
941 g_assert_nonnull(str);
942 g_assert_cmpstr(str, ==, "qemu-ga-test");
944 str = qdict_get_try_str(val, "name");
945 g_assert_nonnull(str);
946 g_assert_cmpstr(str, ==, "QEMU-GA");
948 str = qdict_get_try_str(val, "pretty-name");
949 g_assert_nonnull(str);
950 g_assert_cmpstr(str, ==, "QEMU Guest Agent test");
952 str = qdict_get_try_str(val, "version");
953 g_assert_nonnull(str);
954 g_assert_cmpstr(str, ==, "Test 1");
956 str = qdict_get_try_str(val, "version-id");
957 g_assert_nonnull(str);
958 g_assert_cmpstr(str, ==, "1");
960 str = qdict_get_try_str(val, "variant");
961 g_assert_nonnull(str);
962 g_assert_cmpstr(str, ==, "Unit test \"'$`\\ and \\\\ etc.");
964 str = qdict_get_try_str(val, "variant-id");
965 g_assert_nonnull(str);
966 g_assert_cmpstr(str, ==, "unit-test");
968 qobject_unref(ret);
969 g_free(env[0]);
970 fixture_tear_down(&fixture, NULL);
973 int main(int argc, char **argv)
975 TestFixture fix;
976 int ret;
978 setlocale (LC_ALL, "");
979 g_test_init(&argc, &argv, NULL);
980 fixture_setup(&fix, NULL, NULL);
982 g_test_add_data_func("/qga/sync-delimited", &fix, test_qga_sync_delimited);
983 g_test_add_data_func("/qga/sync", &fix, test_qga_sync);
984 g_test_add_data_func("/qga/ping", &fix, test_qga_ping);
985 g_test_add_data_func("/qga/info", &fix, test_qga_info);
986 g_test_add_data_func("/qga/network-get-interfaces", &fix,
987 test_qga_network_get_interfaces);
988 if (!access("/sys/devices/system/cpu/cpu0", F_OK)) {
989 g_test_add_data_func("/qga/get-vcpus", &fix, test_qga_get_vcpus);
991 g_test_add_data_func("/qga/get-fsinfo", &fix, test_qga_get_fsinfo);
992 g_test_add_data_func("/qga/get-memory-block-info", &fix,
993 test_qga_get_memory_block_info);
994 g_test_add_data_func("/qga/get-memory-blocks", &fix,
995 test_qga_get_memory_blocks);
996 g_test_add_data_func("/qga/file-ops", &fix, test_qga_file_ops);
997 g_test_add_data_func("/qga/file-write-read", &fix, test_qga_file_write_read);
998 g_test_add_data_func("/qga/get-time", &fix, test_qga_get_time);
999 g_test_add_data_func("/qga/invalid-id", &fix, test_qga_invalid_id);
1000 g_test_add_data_func("/qga/invalid-oob", &fix, test_qga_invalid_oob);
1001 g_test_add_data_func("/qga/invalid-cmd", &fix, test_qga_invalid_cmd);
1002 g_test_add_data_func("/qga/invalid-args", &fix, test_qga_invalid_args);
1003 g_test_add_data_func("/qga/fsfreeze-status", &fix,
1004 test_qga_fsfreeze_status);
1006 g_test_add_data_func("/qga/blacklist", NULL, test_qga_blacklist);
1007 g_test_add_data_func("/qga/config", NULL, test_qga_config);
1008 g_test_add_data_func("/qga/guest-exec", &fix, test_qga_guest_exec);
1009 g_test_add_data_func("/qga/guest-exec-invalid", &fix,
1010 test_qga_guest_exec_invalid);
1011 g_test_add_data_func("/qga/guest-get-osinfo", &fix,
1012 test_qga_guest_get_osinfo);
1013 g_test_add_data_func("/qga/guest-get-host-name", &fix,
1014 test_qga_guest_get_host_name);
1015 g_test_add_data_func("/qga/guest-get-timezone", &fix,
1016 test_qga_guest_get_timezone);
1017 g_test_add_data_func("/qga/guest-get-users", &fix,
1018 test_qga_guest_get_users);
1020 ret = g_test_run();
1022 fixture_tear_down(&fix, NULL);
1024 return ret;