target/xtensa: extract test for syscall instruction
[qemu/ar7.git] / tests / test-qga.c
blob3d6377436a10f3e242a3e4d9c97a135d0164e7db
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;
249 ret = qmp_fd(fixture->fd, "{'exec-oob': 'guest-ping'}");
250 g_assert_nonnull(ret);
252 qmp_assert_error_class(ret, "GenericError");
255 static void test_qga_invalid_args(gconstpointer fix)
257 const TestFixture *fixture = fix;
258 QDict *ret, *error;
259 const gchar *class, *desc;
261 ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping', "
262 "'arguments': {'foo': 42 }}");
263 g_assert_nonnull(ret);
265 error = qdict_get_qdict(ret, "error");
266 class = qdict_get_try_str(error, "class");
267 desc = qdict_get_try_str(error, "desc");
269 g_assert_cmpstr(class, ==, "GenericError");
270 g_assert_cmpstr(desc, ==, "Parameter 'foo' is unexpected");
272 qobject_unref(ret);
275 static void test_qga_invalid_cmd(gconstpointer fix)
277 const TestFixture *fixture = fix;
278 QDict *ret, *error;
279 const gchar *class, *desc;
281 ret = qmp_fd(fixture->fd, "{'execute': 'guest-invalid-cmd'}");
282 g_assert_nonnull(ret);
284 error = qdict_get_qdict(ret, "error");
285 class = qdict_get_try_str(error, "class");
286 desc = qdict_get_try_str(error, "desc");
288 g_assert_cmpstr(class, ==, "CommandNotFound");
289 g_assert_cmpint(strlen(desc), >, 0);
291 qobject_unref(ret);
294 static void test_qga_info(gconstpointer fix)
296 const TestFixture *fixture = fix;
297 QDict *ret, *val;
298 const gchar *version;
300 ret = qmp_fd(fixture->fd, "{'execute': 'guest-info'}");
301 g_assert_nonnull(ret);
302 qmp_assert_no_error(ret);
304 val = qdict_get_qdict(ret, "return");
305 version = qdict_get_try_str(val, "version");
306 g_assert_cmpstr(version, ==, QEMU_VERSION);
308 qobject_unref(ret);
311 static void test_qga_get_vcpus(gconstpointer fix)
313 const TestFixture *fixture = fix;
314 QDict *ret;
315 QList *list;
316 const QListEntry *entry;
318 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-vcpus'}");
319 g_assert_nonnull(ret);
320 qmp_assert_no_error(ret);
322 /* check there is at least a cpu */
323 list = qdict_get_qlist(ret, "return");
324 entry = qlist_first(list);
325 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "online"));
326 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "logical-id"));
328 qobject_unref(ret);
331 static void test_qga_get_fsinfo(gconstpointer fix)
333 const TestFixture *fixture = fix;
334 QDict *ret;
335 QList *list;
336 const QListEntry *entry;
338 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-fsinfo'}");
339 g_assert_nonnull(ret);
340 qmp_assert_no_error(ret);
342 /* sanity-check the response if there are any filesystems */
343 list = qdict_get_qlist(ret, "return");
344 entry = qlist_first(list);
345 if (entry) {
346 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "name"));
347 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "mountpoint"));
348 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "type"));
349 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "disk"));
352 qobject_unref(ret);
355 static void test_qga_get_memory_block_info(gconstpointer fix)
357 const TestFixture *fixture = fix;
358 QDict *ret, *val;
359 int64_t size;
361 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-block-info'}");
362 g_assert_nonnull(ret);
364 /* some systems might not expose memory block info in sysfs */
365 if (!qdict_haskey(ret, "error")) {
366 /* check there is at least some memory */
367 val = qdict_get_qdict(ret, "return");
368 size = qdict_get_int(val, "size");
369 g_assert_cmpint(size, >, 0);
372 qobject_unref(ret);
375 static void test_qga_get_memory_blocks(gconstpointer fix)
377 const TestFixture *fixture = fix;
378 QDict *ret;
379 QList *list;
380 const QListEntry *entry;
382 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-blocks'}");
383 g_assert_nonnull(ret);
385 /* some systems might not expose memory block info in sysfs */
386 if (!qdict_haskey(ret, "error")) {
387 list = qdict_get_qlist(ret, "return");
388 entry = qlist_first(list);
389 /* newer versions of qga may return empty list without error */
390 if (entry) {
391 g_assert(qdict_haskey(qobject_to(QDict, entry->value),
392 "phys-index"));
393 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "online"));
397 qobject_unref(ret);
400 static void test_qga_network_get_interfaces(gconstpointer fix)
402 const TestFixture *fixture = fix;
403 QDict *ret;
404 QList *list;
405 const QListEntry *entry;
407 ret = qmp_fd(fixture->fd, "{'execute': 'guest-network-get-interfaces'}");
408 g_assert_nonnull(ret);
409 qmp_assert_no_error(ret);
411 /* check there is at least an interface */
412 list = qdict_get_qlist(ret, "return");
413 entry = qlist_first(list);
414 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "name"));
416 qobject_unref(ret);
419 static void test_qga_file_ops(gconstpointer fix)
421 const TestFixture *fixture = fix;
422 const unsigned char helloworld[] = "Hello World!\n";
423 const char *b64;
424 gchar *path, *enc;
425 unsigned char *dec;
426 QDict *ret, *val;
427 int64_t id, eof;
428 gsize count;
429 FILE *f;
430 char tmp[100];
432 /* open */
433 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
434 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
435 g_assert_nonnull(ret);
436 qmp_assert_no_error(ret);
437 id = qdict_get_int(ret, "return");
438 qobject_unref(ret);
440 enc = g_base64_encode(helloworld, sizeof(helloworld));
441 /* write */
442 ret = qmp_fd(fixture->fd,
443 "{'execute': 'guest-file-write',"
444 " 'arguments': { 'handle': %" PRId64 ", 'buf-b64': %s } }",
445 id, enc);
446 g_assert_nonnull(ret);
447 qmp_assert_no_error(ret);
449 val = qdict_get_qdict(ret, "return");
450 count = qdict_get_int(val, "count");
451 eof = qdict_get_bool(val, "eof");
452 g_assert_cmpint(count, ==, sizeof(helloworld));
453 g_assert_cmpint(eof, ==, 0);
454 qobject_unref(ret);
456 /* flush */
457 ret = qmp_fd(fixture->fd,
458 "{'execute': 'guest-file-flush',"
459 " 'arguments': {'handle': %" PRId64 "} }",
460 id);
461 qobject_unref(ret);
463 /* close */
464 ret = qmp_fd(fixture->fd,
465 "{'execute': 'guest-file-close',"
466 " 'arguments': {'handle': %" PRId64 "} }",
467 id);
468 qobject_unref(ret);
470 /* check content */
471 path = g_build_filename(fixture->test_dir, "foo", NULL);
472 f = fopen(path, "r");
473 g_free(path);
474 g_assert_nonnull(f);
475 count = fread(tmp, 1, sizeof(tmp), f);
476 g_assert_cmpint(count, ==, sizeof(helloworld));
477 tmp[count] = 0;
478 g_assert_cmpstr(tmp, ==, (char *)helloworld);
479 fclose(f);
481 /* open */
482 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
483 " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
484 g_assert_nonnull(ret);
485 qmp_assert_no_error(ret);
486 id = qdict_get_int(ret, "return");
487 qobject_unref(ret);
489 /* read */
490 ret = qmp_fd(fixture->fd,
491 "{'execute': 'guest-file-read',"
492 " 'arguments': { 'handle': %" PRId64 "} }",
493 id);
494 val = qdict_get_qdict(ret, "return");
495 count = qdict_get_int(val, "count");
496 eof = qdict_get_bool(val, "eof");
497 b64 = qdict_get_str(val, "buf-b64");
498 g_assert_cmpint(count, ==, sizeof(helloworld));
499 g_assert(eof);
500 g_assert_cmpstr(b64, ==, enc);
502 qobject_unref(ret);
503 g_free(enc);
505 /* read eof */
506 ret = qmp_fd(fixture->fd,
507 "{'execute': 'guest-file-read',"
508 " 'arguments': { 'handle': %" PRId64 "} }",
509 id);
510 val = qdict_get_qdict(ret, "return");
511 count = qdict_get_int(val, "count");
512 eof = qdict_get_bool(val, "eof");
513 b64 = qdict_get_str(val, "buf-b64");
514 g_assert_cmpint(count, ==, 0);
515 g_assert(eof);
516 g_assert_cmpstr(b64, ==, "");
517 qobject_unref(ret);
519 /* seek */
520 ret = qmp_fd(fixture->fd,
521 "{'execute': 'guest-file-seek',"
522 " 'arguments': { 'handle': %" PRId64 ", "
523 " 'offset': %d, 'whence': %s } }",
524 id, 6, "set");
525 qmp_assert_no_error(ret);
526 val = qdict_get_qdict(ret, "return");
527 count = qdict_get_int(val, "position");
528 eof = qdict_get_bool(val, "eof");
529 g_assert_cmpint(count, ==, 6);
530 g_assert(!eof);
531 qobject_unref(ret);
533 /* partial read */
534 ret = qmp_fd(fixture->fd,
535 "{'execute': 'guest-file-read',"
536 " 'arguments': { 'handle': %" PRId64 "} }",
537 id);
538 val = qdict_get_qdict(ret, "return");
539 count = qdict_get_int(val, "count");
540 eof = qdict_get_bool(val, "eof");
541 b64 = qdict_get_str(val, "buf-b64");
542 g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
543 g_assert(eof);
544 dec = g_base64_decode(b64, &count);
545 g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
546 g_assert_cmpmem(dec, count, helloworld + 6, sizeof(helloworld) - 6);
547 g_free(dec);
549 qobject_unref(ret);
551 /* close */
552 ret = qmp_fd(fixture->fd,
553 "{'execute': 'guest-file-close',"
554 " 'arguments': {'handle': %" PRId64 "} }",
555 id);
556 qobject_unref(ret);
559 static void test_qga_file_write_read(gconstpointer fix)
561 const TestFixture *fixture = fix;
562 const unsigned char helloworld[] = "Hello World!\n";
563 const char *b64;
564 gchar *enc;
565 QDict *ret, *val;
566 int64_t id, eof;
567 gsize count;
569 /* open */
570 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
571 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
572 g_assert_nonnull(ret);
573 qmp_assert_no_error(ret);
574 id = qdict_get_int(ret, "return");
575 qobject_unref(ret);
577 enc = g_base64_encode(helloworld, sizeof(helloworld));
578 /* write */
579 ret = qmp_fd(fixture->fd,
580 "{'execute': 'guest-file-write',"
581 " 'arguments': { 'handle': %" PRId64 ","
582 " 'buf-b64': %s } }", id, enc);
583 g_assert_nonnull(ret);
584 qmp_assert_no_error(ret);
586 val = qdict_get_qdict(ret, "return");
587 count = qdict_get_int(val, "count");
588 eof = qdict_get_bool(val, "eof");
589 g_assert_cmpint(count, ==, sizeof(helloworld));
590 g_assert_cmpint(eof, ==, 0);
591 qobject_unref(ret);
593 /* read (check implicit flush) */
594 ret = qmp_fd(fixture->fd,
595 "{'execute': 'guest-file-read',"
596 " 'arguments': { 'handle': %" PRId64 "} }",
597 id);
598 val = qdict_get_qdict(ret, "return");
599 count = qdict_get_int(val, "count");
600 eof = qdict_get_bool(val, "eof");
601 b64 = qdict_get_str(val, "buf-b64");
602 g_assert_cmpint(count, ==, 0);
603 g_assert(eof);
604 g_assert_cmpstr(b64, ==, "");
605 qobject_unref(ret);
607 /* seek to 0 */
608 ret = qmp_fd(fixture->fd,
609 "{'execute': 'guest-file-seek',"
610 " 'arguments': { 'handle': %" PRId64 ", "
611 " 'offset': %d, 'whence': %s } }",
612 id, 0, "set");
613 qmp_assert_no_error(ret);
614 val = qdict_get_qdict(ret, "return");
615 count = qdict_get_int(val, "position");
616 eof = qdict_get_bool(val, "eof");
617 g_assert_cmpint(count, ==, 0);
618 g_assert(!eof);
619 qobject_unref(ret);
621 /* read */
622 ret = qmp_fd(fixture->fd,
623 "{'execute': 'guest-file-read',"
624 " 'arguments': { 'handle': %" PRId64 "} }",
625 id);
626 val = qdict_get_qdict(ret, "return");
627 count = qdict_get_int(val, "count");
628 eof = qdict_get_bool(val, "eof");
629 b64 = qdict_get_str(val, "buf-b64");
630 g_assert_cmpint(count, ==, sizeof(helloworld));
631 g_assert(eof);
632 g_assert_cmpstr(b64, ==, enc);
633 qobject_unref(ret);
634 g_free(enc);
636 /* close */
637 ret = qmp_fd(fixture->fd,
638 "{'execute': 'guest-file-close',"
639 " 'arguments': {'handle': %" PRId64 "} }",
640 id);
641 qobject_unref(ret);
644 static void test_qga_get_time(gconstpointer fix)
646 const TestFixture *fixture = fix;
647 QDict *ret;
648 int64_t time;
650 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
651 g_assert_nonnull(ret);
652 qmp_assert_no_error(ret);
654 time = qdict_get_int(ret, "return");
655 g_assert_cmpint(time, >, 0);
657 qobject_unref(ret);
660 static void test_qga_blacklist(gconstpointer data)
662 TestFixture fix;
663 QDict *ret, *error;
664 const gchar *class, *desc;
666 fixture_setup(&fix, "-b guest-ping,guest-get-time", NULL);
668 /* check blacklist */
669 ret = qmp_fd(fix.fd, "{'execute': 'guest-ping'}");
670 g_assert_nonnull(ret);
671 error = qdict_get_qdict(ret, "error");
672 class = qdict_get_try_str(error, "class");
673 desc = qdict_get_try_str(error, "desc");
674 g_assert_cmpstr(class, ==, "GenericError");
675 g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
676 qobject_unref(ret);
678 ret = qmp_fd(fix.fd, "{'execute': 'guest-get-time'}");
679 g_assert_nonnull(ret);
680 error = qdict_get_qdict(ret, "error");
681 class = qdict_get_try_str(error, "class");
682 desc = qdict_get_try_str(error, "desc");
683 g_assert_cmpstr(class, ==, "GenericError");
684 g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
685 qobject_unref(ret);
687 /* check something work */
688 ret = qmp_fd(fix.fd, "{'execute': 'guest-get-fsinfo'}");
689 qmp_assert_no_error(ret);
690 qobject_unref(ret);
692 fixture_tear_down(&fix, NULL);
695 static void test_qga_config(gconstpointer data)
697 GError *error = NULL;
698 char *cwd, *cmd, *out, *err, *str, **strv, **argv = NULL;
699 char *env[2];
700 int status;
701 gsize n;
702 GKeyFile *kf;
704 cwd = g_get_current_dir();
705 cmd = g_strdup_printf("%s%cqemu-ga -D",
706 cwd, G_DIR_SEPARATOR);
707 g_free(cwd);
708 g_shell_parse_argv(cmd, NULL, &argv, &error);
709 g_free(cmd);
710 g_assert_no_error(error);
712 env[0] = g_strdup_printf("QGA_CONF=tests%cdata%ctest-qga-config",
713 G_DIR_SEPARATOR, G_DIR_SEPARATOR);
714 env[1] = NULL;
715 g_spawn_sync(NULL, argv, env, 0,
716 NULL, NULL, &out, &err, &status, &error);
717 g_strfreev(argv);
719 g_assert_no_error(error);
720 g_assert_cmpstr(err, ==, "");
721 g_assert_cmpint(status, ==, 0);
723 kf = g_key_file_new();
724 g_key_file_load_from_data(kf, out, -1, G_KEY_FILE_NONE, &error);
725 g_assert_no_error(error);
727 str = g_key_file_get_start_group(kf);
728 g_assert_cmpstr(str, ==, "general");
729 g_free(str);
731 g_assert_false(g_key_file_get_boolean(kf, "general", "daemon", &error));
732 g_assert_no_error(error);
734 str = g_key_file_get_string(kf, "general", "method", &error);
735 g_assert_no_error(error);
736 g_assert_cmpstr(str, ==, "virtio-serial");
737 g_free(str);
739 str = g_key_file_get_string(kf, "general", "path", &error);
740 g_assert_no_error(error);
741 g_assert_cmpstr(str, ==, "/path/to/org.qemu.guest_agent.0");
742 g_free(str);
744 str = g_key_file_get_string(kf, "general", "pidfile", &error);
745 g_assert_no_error(error);
746 g_assert_cmpstr(str, ==, "/var/foo/qemu-ga.pid");
747 g_free(str);
749 str = g_key_file_get_string(kf, "general", "statedir", &error);
750 g_assert_no_error(error);
751 g_assert_cmpstr(str, ==, "/var/state");
752 g_free(str);
754 g_assert_true(g_key_file_get_boolean(kf, "general", "verbose", &error));
755 g_assert_no_error(error);
757 strv = g_key_file_get_string_list(kf, "general", "blacklist", &n, &error);
758 g_assert_cmpint(n, ==, 2);
759 g_assert_true(g_strv_contains((const char * const *)strv,
760 "guest-ping"));
761 g_assert_true(g_strv_contains((const char * const *)strv,
762 "guest-get-time"));
763 g_assert_no_error(error);
764 g_strfreev(strv);
766 g_free(out);
767 g_free(err);
768 g_free(env[0]);
769 g_key_file_free(kf);
772 static void test_qga_fsfreeze_status(gconstpointer fix)
774 const TestFixture *fixture = fix;
775 QDict *ret;
776 const gchar *status;
778 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-status'}");
779 g_assert_nonnull(ret);
780 qmp_assert_no_error(ret);
782 status = qdict_get_try_str(ret, "return");
783 g_assert_cmpstr(status, ==, "thawed");
785 qobject_unref(ret);
788 static void test_qga_guest_exec(gconstpointer fix)
790 const TestFixture *fixture = fix;
791 QDict *ret, *val;
792 const gchar *out;
793 guchar *decoded;
794 int64_t pid, now, exitcode;
795 gsize len;
796 bool exited;
798 /* exec 'echo foo bar' */
799 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
800 " 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ],"
801 " 'capture-output': true } }");
802 g_assert_nonnull(ret);
803 qmp_assert_no_error(ret);
804 val = qdict_get_qdict(ret, "return");
805 pid = qdict_get_int(val, "pid");
806 g_assert_cmpint(pid, >, 0);
807 qobject_unref(ret);
809 /* wait for completion */
810 now = g_get_monotonic_time();
811 do {
812 ret = qmp_fd(fixture->fd,
813 "{'execute': 'guest-exec-status',"
814 " 'arguments': { 'pid': %" PRId64 " } }", pid);
815 g_assert_nonnull(ret);
816 val = qdict_get_qdict(ret, "return");
817 exited = qdict_get_bool(val, "exited");
818 if (!exited) {
819 qobject_unref(ret);
821 } while (!exited &&
822 g_get_monotonic_time() < now + 5 * G_TIME_SPAN_SECOND);
823 g_assert(exited);
825 /* check stdout */
826 exitcode = qdict_get_int(val, "exitcode");
827 g_assert_cmpint(exitcode, ==, 0);
828 out = qdict_get_str(val, "out-data");
829 decoded = g_base64_decode(out, &len);
830 g_assert_cmpint(len, ==, 12);
831 g_assert_cmpstr((char *)decoded, ==, "\" test_str \"");
832 g_free(decoded);
833 qobject_unref(ret);
836 static void test_qga_guest_exec_invalid(gconstpointer fix)
838 const TestFixture *fixture = fix;
839 QDict *ret, *error;
840 const gchar *class, *desc;
842 /* invalid command */
843 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
844 " 'path': '/bin/invalid-cmd42' } }");
845 g_assert_nonnull(ret);
846 error = qdict_get_qdict(ret, "error");
847 g_assert_nonnull(error);
848 class = qdict_get_str(error, "class");
849 desc = qdict_get_str(error, "desc");
850 g_assert_cmpstr(class, ==, "GenericError");
851 g_assert_cmpint(strlen(desc), >, 0);
852 qobject_unref(ret);
854 /* invalid pid */
855 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec-status',"
856 " 'arguments': { 'pid': 0 } }");
857 g_assert_nonnull(ret);
858 error = qdict_get_qdict(ret, "error");
859 g_assert_nonnull(error);
860 class = qdict_get_str(error, "class");
861 desc = qdict_get_str(error, "desc");
862 g_assert_cmpstr(class, ==, "GenericError");
863 g_assert_cmpint(strlen(desc), >, 0);
864 qobject_unref(ret);
867 static void test_qga_guest_get_host_name(gconstpointer fix)
869 const TestFixture *fixture = fix;
870 QDict *ret, *val;
872 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-host-name'}");
873 g_assert_nonnull(ret);
874 qmp_assert_no_error(ret);
876 val = qdict_get_qdict(ret, "return");
877 g_assert(qdict_haskey(val, "host-name"));
879 qobject_unref(ret);
882 static void test_qga_guest_get_timezone(gconstpointer fix)
884 const TestFixture *fixture = fix;
885 QDict *ret, *val;
887 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-timezone'}");
888 g_assert_nonnull(ret);
889 qmp_assert_no_error(ret);
891 /* Make sure there's at least offset */
892 val = qdict_get_qdict(ret, "return");
893 g_assert(qdict_haskey(val, "offset"));
895 qobject_unref(ret);
898 static void test_qga_guest_get_users(gconstpointer fix)
900 const TestFixture *fixture = fix;
901 QDict *ret;
902 QList *val;
904 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-users'}");
905 g_assert_nonnull(ret);
906 qmp_assert_no_error(ret);
908 /* There is not much to test here */
909 val = qdict_get_qlist(ret, "return");
910 g_assert_nonnull(val);
912 qobject_unref(ret);
915 static void test_qga_guest_get_osinfo(gconstpointer data)
917 TestFixture fixture;
918 const gchar *str;
919 gchar *cwd, *env[2];
920 QDict *ret, *val;
922 cwd = g_get_current_dir();
923 env[0] = g_strdup_printf(
924 "QGA_OS_RELEASE=%s%ctests%cdata%ctest-qga-os-release",
925 cwd, G_DIR_SEPARATOR, G_DIR_SEPARATOR, G_DIR_SEPARATOR);
926 env[1] = NULL;
927 g_free(cwd);
928 fixture_setup(&fixture, NULL, env);
930 ret = qmp_fd(fixture.fd, "{'execute': 'guest-get-osinfo'}");
931 g_assert_nonnull(ret);
932 qmp_assert_no_error(ret);
934 val = qdict_get_qdict(ret, "return");
936 str = qdict_get_try_str(val, "id");
937 g_assert_nonnull(str);
938 g_assert_cmpstr(str, ==, "qemu-ga-test");
940 str = qdict_get_try_str(val, "name");
941 g_assert_nonnull(str);
942 g_assert_cmpstr(str, ==, "QEMU-GA");
944 str = qdict_get_try_str(val, "pretty-name");
945 g_assert_nonnull(str);
946 g_assert_cmpstr(str, ==, "QEMU Guest Agent test");
948 str = qdict_get_try_str(val, "version");
949 g_assert_nonnull(str);
950 g_assert_cmpstr(str, ==, "Test 1");
952 str = qdict_get_try_str(val, "version-id");
953 g_assert_nonnull(str);
954 g_assert_cmpstr(str, ==, "1");
956 str = qdict_get_try_str(val, "variant");
957 g_assert_nonnull(str);
958 g_assert_cmpstr(str, ==, "Unit test \"'$`\\ and \\\\ etc.");
960 str = qdict_get_try_str(val, "variant-id");
961 g_assert_nonnull(str);
962 g_assert_cmpstr(str, ==, "unit-test");
964 qobject_unref(ret);
965 g_free(env[0]);
966 fixture_tear_down(&fixture, NULL);
969 int main(int argc, char **argv)
971 TestFixture fix;
972 int ret;
974 setlocale (LC_ALL, "");
975 g_test_init(&argc, &argv, NULL);
976 fixture_setup(&fix, NULL, NULL);
978 g_test_add_data_func("/qga/sync-delimited", &fix, test_qga_sync_delimited);
979 g_test_add_data_func("/qga/sync", &fix, test_qga_sync);
980 g_test_add_data_func("/qga/ping", &fix, test_qga_ping);
981 g_test_add_data_func("/qga/info", &fix, test_qga_info);
982 g_test_add_data_func("/qga/network-get-interfaces", &fix,
983 test_qga_network_get_interfaces);
984 if (!access("/sys/devices/system/cpu/cpu0", F_OK)) {
985 g_test_add_data_func("/qga/get-vcpus", &fix, test_qga_get_vcpus);
987 g_test_add_data_func("/qga/get-fsinfo", &fix, test_qga_get_fsinfo);
988 g_test_add_data_func("/qga/get-memory-block-info", &fix,
989 test_qga_get_memory_block_info);
990 g_test_add_data_func("/qga/get-memory-blocks", &fix,
991 test_qga_get_memory_blocks);
992 g_test_add_data_func("/qga/file-ops", &fix, test_qga_file_ops);
993 g_test_add_data_func("/qga/file-write-read", &fix, test_qga_file_write_read);
994 g_test_add_data_func("/qga/get-time", &fix, test_qga_get_time);
995 g_test_add_data_func("/qga/invalid-id", &fix, test_qga_invalid_id);
996 g_test_add_data_func("/qga/invalid-oob", &fix, test_qga_invalid_oob);
997 g_test_add_data_func("/qga/invalid-cmd", &fix, test_qga_invalid_cmd);
998 g_test_add_data_func("/qga/invalid-args", &fix, test_qga_invalid_args);
999 g_test_add_data_func("/qga/fsfreeze-status", &fix,
1000 test_qga_fsfreeze_status);
1002 g_test_add_data_func("/qga/blacklist", NULL, test_qga_blacklist);
1003 g_test_add_data_func("/qga/config", NULL, test_qga_config);
1004 g_test_add_data_func("/qga/guest-exec", &fix, test_qga_guest_exec);
1005 g_test_add_data_func("/qga/guest-exec-invalid", &fix,
1006 test_qga_guest_exec_invalid);
1007 g_test_add_data_func("/qga/guest-get-osinfo", &fix,
1008 test_qga_guest_get_osinfo);
1009 g_test_add_data_func("/qga/guest-get-host-name", &fix,
1010 test_qga_guest_get_host_name);
1011 g_test_add_data_func("/qga/guest-get-timezone", &fix,
1012 test_qga_guest_get_timezone);
1013 g_test_add_data_func("/qga/guest-get-users", &fix,
1014 test_qga_guest_get_users);
1016 ret = g_test_run();
1018 fixture_tear_down(&fix, NULL);
1020 return ret;