1 #include "qemu/osdep.h"
3 #include <glib/gstdio.h>
4 #include <sys/socket.h>
16 static int connect_qga(char *path)
18 int s, ret, len, i = 0;
19 struct sockaddr_un remote;
21 s = socket(AF_UNIX, SOCK_STREAM, 0);
24 remote.sun_family = AF_UNIX;
26 strcpy(remote.sun_path, path);
27 len = strlen(remote.sun_path) + sizeof(remote.sun_family);
28 ret = connect(s, (struct sockaddr *)&remote, len);
30 g_usleep(G_USEC_PER_SEC);
40 static void qga_watch(GPid pid, gint status, gpointer user_data)
42 TestFixture *fixture = user_data;
44 g_assert_cmpint(status, ==, 0);
45 g_main_loop_quit(fixture->loop);
49 fixture_setup(TestFixture *fixture, gconstpointer data)
51 const gchar *extra_arg = data;
53 gchar *cwd, *path, *cmd, **argv = NULL;
55 fixture->loop = g_main_loop_new(NULL, FALSE);
57 fixture->test_dir = g_strdup("/tmp/qgatest.XXXXXX");
58 g_assert_nonnull(mkdtemp(fixture->test_dir));
60 path = g_build_filename(fixture->test_dir, "sock", NULL);
61 cwd = g_get_current_dir();
62 cmd = g_strdup_printf("%s%cqemu-ga -m unix-listen -t %s -p %s %s %s",
64 fixture->test_dir, path,
65 getenv("QTEST_LOG") ? "-v" : "",
67 g_shell_parse_argv(cmd, NULL, &argv, &error);
68 g_assert_no_error(error);
70 g_spawn_async(fixture->test_dir, argv, NULL,
71 G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
72 NULL, NULL, &fixture->pid, &error);
73 g_assert_no_error(error);
75 g_child_watch_add(fixture->pid, qga_watch, fixture);
77 fixture->fd = connect_qga(path);
78 g_assert_cmpint(fixture->fd, !=, -1);
87 fixture_tear_down(TestFixture *fixture, gconstpointer data)
91 kill(fixture->pid, SIGTERM);
93 g_main_loop_run(fixture->loop);
94 g_main_loop_unref(fixture->loop);
96 g_spawn_close_pid(fixture->pid);
98 tmp = g_build_filename(fixture->test_dir, "foo", NULL);
102 tmp = g_build_filename(fixture->test_dir, "qga.state", NULL);
106 tmp = g_build_filename(fixture->test_dir, "sock", NULL);
110 g_rmdir(fixture->test_dir);
111 g_free(fixture->test_dir);
114 static void qmp_assertion_message_error(const char *domain,
121 const char *class, *desc;
125 error = qdict_get_qdict(dict, "error");
126 class = qdict_get_try_str(error, "class");
127 desc = qdict_get_try_str(error, "desc");
129 s = g_strdup_printf("assertion failed %s: %s %s", expr, class, desc);
130 g_assertion_message(domain, file, line, func, s);
134 #define qmp_assert_no_error(err) do { \
135 if (qdict_haskey(err, "error")) { \
136 qmp_assertion_message_error(G_LOG_DOMAIN, __FILE__, __LINE__, \
137 G_STRFUNC, #err, err); \
141 static void test_qga_sync_delimited(gconstpointer fix)
143 const TestFixture *fixture = fix;
144 guint32 v, r = g_random_int();
149 cmd = g_strdup_printf("%c{'execute': 'guest-sync-delimited',"
150 " 'arguments': {'id': %u } }", 0xff, r);
151 qmp_fd_send(fixture->fd, cmd);
154 v = read(fixture->fd, &c, 1);
155 g_assert_cmpint(v, ==, 1);
156 g_assert_cmpint(c, ==, 0xff);
158 ret = qmp_fd_receive(fixture->fd);
159 g_assert_nonnull(ret);
160 qmp_assert_no_error(ret);
162 v = qdict_get_int(ret, "return");
163 g_assert_cmpint(r, ==, v);
168 static void test_qga_sync(gconstpointer fix)
170 const TestFixture *fixture = fix;
171 guint32 v, r = g_random_int();
175 cmd = g_strdup_printf("%c{'execute': 'guest-sync',"
176 " 'arguments': {'id': %u } }", 0xff, r);
177 ret = qmp_fd(fixture->fd, cmd);
180 g_assert_nonnull(ret);
181 qmp_assert_no_error(ret);
183 v = qdict_get_int(ret, "return");
184 g_assert_cmpint(r, ==, v);
189 static void test_qga_ping(gconstpointer fix)
191 const TestFixture *fixture = fix;
194 ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping'}");
195 g_assert_nonnull(ret);
196 qmp_assert_no_error(ret);
201 static void test_qga_invalid_args(gconstpointer fix)
203 const TestFixture *fixture = fix;
205 const gchar *class, *desc;
207 ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping', "
208 "'arguments': {'foo': 42 }}");
209 g_assert_nonnull(ret);
211 error = qdict_get_qdict(ret, "error");
212 class = qdict_get_try_str(error, "class");
213 desc = qdict_get_try_str(error, "desc");
215 g_assert_cmpstr(class, ==, "GenericError");
216 g_assert_cmpstr(desc, ==, "QMP input object member 'foo' is unexpected");
221 static void test_qga_invalid_cmd(gconstpointer fix)
223 const TestFixture *fixture = fix;
225 const gchar *class, *desc;
227 ret = qmp_fd(fixture->fd, "{'execute': 'guest-invalid-cmd'}");
228 g_assert_nonnull(ret);
230 error = qdict_get_qdict(ret, "error");
231 class = qdict_get_try_str(error, "class");
232 desc = qdict_get_try_str(error, "desc");
234 g_assert_cmpstr(class, ==, "CommandNotFound");
235 g_assert_cmpint(strlen(desc), >, 0);
240 static void test_qga_info(gconstpointer fix)
242 const TestFixture *fixture = fix;
244 const gchar *version;
246 ret = qmp_fd(fixture->fd, "{'execute': 'guest-info'}");
247 g_assert_nonnull(ret);
248 qmp_assert_no_error(ret);
250 val = qdict_get_qdict(ret, "return");
251 version = qdict_get_try_str(val, "version");
252 g_assert_cmpstr(version, ==, QEMU_VERSION);
257 static void test_qga_get_vcpus(gconstpointer fix)
259 const TestFixture *fixture = fix;
262 const QListEntry *entry;
264 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-vcpus'}");
265 g_assert_nonnull(ret);
266 qmp_assert_no_error(ret);
268 /* check there is at least a cpu */
269 list = qdict_get_qlist(ret, "return");
270 entry = qlist_first(list);
271 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "online"));
272 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "logical-id"));
277 static void test_qga_get_fsinfo(gconstpointer fix)
279 const TestFixture *fixture = fix;
282 const QListEntry *entry;
284 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-fsinfo'}");
285 g_assert_nonnull(ret);
286 qmp_assert_no_error(ret);
288 /* sanity-check the response if there are any filesystems */
289 list = qdict_get_qlist(ret, "return");
290 entry = qlist_first(list);
292 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "name"));
293 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "mountpoint"));
294 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "type"));
295 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "disk"));
301 static void test_qga_get_memory_block_info(gconstpointer fix)
303 const TestFixture *fixture = fix;
307 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-block-info'}");
308 g_assert_nonnull(ret);
310 /* some systems might not expose memory block info in sysfs */
311 if (!qdict_haskey(ret, "error")) {
312 /* check there is at least some memory */
313 val = qdict_get_qdict(ret, "return");
314 size = qdict_get_int(val, "size");
315 g_assert_cmpint(size, >, 0);
321 static void test_qga_get_memory_blocks(gconstpointer fix)
323 const TestFixture *fixture = fix;
326 const QListEntry *entry;
328 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-blocks'}");
329 g_assert_nonnull(ret);
331 /* some systems might not expose memory block info in sysfs */
332 if (!qdict_haskey(ret, "error")) {
333 list = qdict_get_qlist(ret, "return");
334 entry = qlist_first(list);
335 /* newer versions of qga may return empty list without error */
337 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "phys-index"));
338 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "online"));
345 static void test_qga_network_get_interfaces(gconstpointer fix)
347 const TestFixture *fixture = fix;
350 const QListEntry *entry;
352 ret = qmp_fd(fixture->fd, "{'execute': 'guest-network-get-interfaces'}");
353 g_assert_nonnull(ret);
354 qmp_assert_no_error(ret);
356 /* check there is at least an interface */
357 list = qdict_get_qlist(ret, "return");
358 entry = qlist_first(list);
359 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "name"));
364 static void test_qga_file_ops(gconstpointer fix)
366 const TestFixture *fixture = fix;
367 const unsigned char helloworld[] = "Hello World!\n";
369 gchar *cmd, *path, *enc;
378 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
379 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
380 g_assert_nonnull(ret);
381 qmp_assert_no_error(ret);
382 id = qdict_get_int(ret, "return");
385 enc = g_base64_encode(helloworld, sizeof(helloworld));
387 cmd = g_strdup_printf("{'execute': 'guest-file-write',"
388 " 'arguments': { 'handle': %" PRId64 ","
389 " 'buf-b64': '%s' } }", id, enc);
390 ret = qmp_fd(fixture->fd, cmd);
391 g_assert_nonnull(ret);
392 qmp_assert_no_error(ret);
394 val = qdict_get_qdict(ret, "return");
395 count = qdict_get_int(val, "count");
396 eof = qdict_get_bool(val, "eof");
397 g_assert_cmpint(count, ==, sizeof(helloworld));
398 g_assert_cmpint(eof, ==, 0);
403 cmd = g_strdup_printf("{'execute': 'guest-file-flush',"
404 " 'arguments': {'handle': %" PRId64 "} }",
406 ret = qmp_fd(fixture->fd, cmd);
411 cmd = g_strdup_printf("{'execute': 'guest-file-close',"
412 " 'arguments': {'handle': %" PRId64 "} }",
414 ret = qmp_fd(fixture->fd, cmd);
419 path = g_build_filename(fixture->test_dir, "foo", NULL);
420 f = fopen(path, "r");
423 count = fread(tmp, 1, sizeof(tmp), f);
424 g_assert_cmpint(count, ==, sizeof(helloworld));
426 g_assert_cmpstr(tmp, ==, (char *)helloworld);
430 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
431 " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
432 g_assert_nonnull(ret);
433 qmp_assert_no_error(ret);
434 id = qdict_get_int(ret, "return");
438 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
439 " 'arguments': { 'handle': %" PRId64 "} }",
441 ret = qmp_fd(fixture->fd, cmd);
442 val = qdict_get_qdict(ret, "return");
443 count = qdict_get_int(val, "count");
444 eof = qdict_get_bool(val, "eof");
445 b64 = qdict_get_str(val, "buf-b64");
446 g_assert_cmpint(count, ==, sizeof(helloworld));
448 g_assert_cmpstr(b64, ==, enc);
455 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
456 " 'arguments': { 'handle': %" PRId64 "} }",
458 ret = qmp_fd(fixture->fd, cmd);
459 val = qdict_get_qdict(ret, "return");
460 count = qdict_get_int(val, "count");
461 eof = qdict_get_bool(val, "eof");
462 b64 = qdict_get_str(val, "buf-b64");
463 g_assert_cmpint(count, ==, 0);
465 g_assert_cmpstr(b64, ==, "");
470 cmd = g_strdup_printf("{'execute': 'guest-file-seek',"
471 " 'arguments': { 'handle': %" PRId64 ", "
472 " 'offset': %d, 'whence': '%s' } }",
474 ret = qmp_fd(fixture->fd, cmd);
475 qmp_assert_no_error(ret);
476 val = qdict_get_qdict(ret, "return");
477 count = qdict_get_int(val, "position");
478 eof = qdict_get_bool(val, "eof");
479 g_assert_cmpint(count, ==, 6);
485 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
486 " 'arguments': { 'handle': %" PRId64 "} }",
488 ret = qmp_fd(fixture->fd, cmd);
489 val = qdict_get_qdict(ret, "return");
490 count = qdict_get_int(val, "count");
491 eof = qdict_get_bool(val, "eof");
492 b64 = qdict_get_str(val, "buf-b64");
493 g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
495 dec = g_base64_decode(b64, &count);
496 g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
497 g_assert_cmpmem(dec, count, helloworld + 6, sizeof(helloworld) - 6);
504 cmd = g_strdup_printf("{'execute': 'guest-file-close',"
505 " 'arguments': {'handle': %" PRId64 "} }",
507 ret = qmp_fd(fixture->fd, cmd);
512 static void test_qga_file_write_read(gconstpointer fix)
514 const TestFixture *fixture = fix;
515 const unsigned char helloworld[] = "Hello World!\n";
523 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
524 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
525 g_assert_nonnull(ret);
526 qmp_assert_no_error(ret);
527 id = qdict_get_int(ret, "return");
530 enc = g_base64_encode(helloworld, sizeof(helloworld));
532 cmd = g_strdup_printf("{'execute': 'guest-file-write',"
533 " 'arguments': { 'handle': %" PRId64 ","
534 " 'buf-b64': '%s' } }", id, enc);
535 ret = qmp_fd(fixture->fd, cmd);
536 g_assert_nonnull(ret);
537 qmp_assert_no_error(ret);
539 val = qdict_get_qdict(ret, "return");
540 count = qdict_get_int(val, "count");
541 eof = qdict_get_bool(val, "eof");
542 g_assert_cmpint(count, ==, sizeof(helloworld));
543 g_assert_cmpint(eof, ==, 0);
547 /* read (check implicit flush) */
548 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
549 " 'arguments': { 'handle': %" PRId64 "} }",
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, ==, 0);
558 g_assert_cmpstr(b64, ==, "");
563 cmd = g_strdup_printf("{'execute': 'guest-file-seek',"
564 " 'arguments': { 'handle': %" PRId64 ", "
565 " 'offset': %d, 'whence': '%s' } }",
567 ret = qmp_fd(fixture->fd, cmd);
568 qmp_assert_no_error(ret);
569 val = qdict_get_qdict(ret, "return");
570 count = qdict_get_int(val, "position");
571 eof = qdict_get_bool(val, "eof");
572 g_assert_cmpint(count, ==, 0);
578 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
579 " 'arguments': { 'handle': %" PRId64 "} }",
581 ret = qmp_fd(fixture->fd, cmd);
582 val = qdict_get_qdict(ret, "return");
583 count = qdict_get_int(val, "count");
584 eof = qdict_get_bool(val, "eof");
585 b64 = qdict_get_str(val, "buf-b64");
586 g_assert_cmpint(count, ==, sizeof(helloworld));
588 g_assert_cmpstr(b64, ==, enc);
594 cmd = g_strdup_printf("{'execute': 'guest-file-close',"
595 " 'arguments': {'handle': %" PRId64 "} }",
597 ret = qmp_fd(fixture->fd, cmd);
602 static void test_qga_get_time(gconstpointer fix)
604 const TestFixture *fixture = fix;
608 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
609 g_assert_nonnull(ret);
610 qmp_assert_no_error(ret);
612 time = qdict_get_int(ret, "return");
613 g_assert_cmpint(time, >, 0);
618 static void test_qga_set_time(gconstpointer fix)
620 const TestFixture *fixture = fix;
622 int64_t current, time;
625 /* get current time */
626 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
627 g_assert_nonnull(ret);
628 qmp_assert_no_error(ret);
629 current = qdict_get_int(ret, "return");
630 g_assert_cmpint(current, >, 0);
633 /* set some old time */
634 ret = qmp_fd(fixture->fd, "{'execute': 'guest-set-time',"
635 " 'arguments': { 'time': 1000 } }");
636 g_assert_nonnull(ret);
637 qmp_assert_no_error(ret);
641 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
642 g_assert_nonnull(ret);
643 qmp_assert_no_error(ret);
644 time = qdict_get_int(ret, "return");
645 g_assert_cmpint(time / 1000, <, G_USEC_PER_SEC * 10);
648 /* set back current time */
649 cmd = g_strdup_printf("{'execute': 'guest-set-time',"
650 " 'arguments': { 'time': %" PRId64 " } }",
651 current + time * 1000);
652 ret = qmp_fd(fixture->fd, cmd);
654 g_assert_nonnull(ret);
655 qmp_assert_no_error(ret);
659 static void test_qga_fstrim(gconstpointer fix)
661 const TestFixture *fixture = fix;
664 const QListEntry *entry;
666 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fstrim',"
667 " arguments: { minimum: 4194304 } }");
668 g_assert_nonnull(ret);
669 qmp_assert_no_error(ret);
670 list = qdict_get_qlist(ret, "return");
671 entry = qlist_first(list);
672 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "paths"));
677 static void test_qga_blacklist(gconstpointer data)
681 const gchar *class, *desc;
683 fixture_setup(&fix, "-b guest-ping,guest-get-time");
685 /* check blacklist */
686 ret = qmp_fd(fix.fd, "{'execute': 'guest-ping'}");
687 g_assert_nonnull(ret);
688 error = qdict_get_qdict(ret, "error");
689 class = qdict_get_try_str(error, "class");
690 desc = qdict_get_try_str(error, "desc");
691 g_assert_cmpstr(class, ==, "GenericError");
692 g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
695 ret = qmp_fd(fix.fd, "{'execute': 'guest-get-time'}");
696 g_assert_nonnull(ret);
697 error = qdict_get_qdict(ret, "error");
698 class = qdict_get_try_str(error, "class");
699 desc = qdict_get_try_str(error, "desc");
700 g_assert_cmpstr(class, ==, "GenericError");
701 g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
704 /* check something work */
705 ret = qmp_fd(fix.fd, "{'execute': 'guest-get-fsinfo'}");
706 qmp_assert_no_error(ret);
709 fixture_tear_down(&fix, NULL);
712 static void test_qga_config(gconstpointer data)
714 GError *error = NULL;
715 char *cwd, *cmd, *out, *err, *str, **strv, **argv = NULL;
721 cwd = g_get_current_dir();
722 cmd = g_strdup_printf("%s%cqemu-ga -D",
723 cwd, G_DIR_SEPARATOR);
725 g_shell_parse_argv(cmd, NULL, &argv, &error);
727 g_assert_no_error(error);
729 env[0] = g_strdup_printf("QGA_CONF=tests%cdata%ctest-qga-config",
730 G_DIR_SEPARATOR, G_DIR_SEPARATOR);
732 g_spawn_sync(NULL, argv, env, 0,
733 NULL, NULL, &out, &err, &status, &error);
736 g_assert_no_error(error);
737 g_assert_cmpstr(err, ==, "");
738 g_assert_cmpint(status, ==, 0);
740 kf = g_key_file_new();
741 g_key_file_load_from_data(kf, out, -1, G_KEY_FILE_NONE, &error);
742 g_assert_no_error(error);
744 str = g_key_file_get_start_group(kf);
745 g_assert_cmpstr(str, ==, "general");
748 g_assert_false(g_key_file_get_boolean(kf, "general", "daemon", &error));
749 g_assert_no_error(error);
751 str = g_key_file_get_string(kf, "general", "method", &error);
752 g_assert_no_error(error);
753 g_assert_cmpstr(str, ==, "virtio-serial");
756 str = g_key_file_get_string(kf, "general", "path", &error);
757 g_assert_no_error(error);
758 g_assert_cmpstr(str, ==, "/path/to/org.qemu.guest_agent.0");
761 str = g_key_file_get_string(kf, "general", "pidfile", &error);
762 g_assert_no_error(error);
763 g_assert_cmpstr(str, ==, "/var/foo/qemu-ga.pid");
766 str = g_key_file_get_string(kf, "general", "statedir", &error);
767 g_assert_no_error(error);
768 g_assert_cmpstr(str, ==, "/var/state");
771 g_assert_true(g_key_file_get_boolean(kf, "general", "verbose", &error));
772 g_assert_no_error(error);
774 strv = g_key_file_get_string_list(kf, "general", "blacklist", &n, &error);
775 g_assert_cmpint(n, ==, 2);
776 #if GLIB_CHECK_VERSION(2, 44, 0)
777 g_assert_true(g_strv_contains((const char * const *)strv,
779 g_assert_true(g_strv_contains((const char * const *)strv,
782 g_assert_no_error(error);
791 static void test_qga_fsfreeze_status(gconstpointer fix)
793 const TestFixture *fixture = fix;
797 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-status'}");
798 g_assert_nonnull(ret);
799 qmp_assert_no_error(ret);
801 status = qdict_get_try_str(ret, "return");
802 g_assert_cmpstr(status, ==, "thawed");
807 static void test_qga_fsfreeze_and_thaw(gconstpointer fix)
809 const TestFixture *fixture = fix;
813 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-freeze'}");
814 g_assert_nonnull(ret);
815 qmp_assert_no_error(ret);
818 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-status'}");
819 g_assert_nonnull(ret);
820 qmp_assert_no_error(ret);
821 status = qdict_get_try_str(ret, "return");
822 g_assert_cmpstr(status, ==, "frozen");
825 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-thaw'}");
826 g_assert_nonnull(ret);
827 qmp_assert_no_error(ret);
831 static void test_qga_guest_exec(gconstpointer fix)
833 const TestFixture *fixture = fix;
837 int64_t pid, now, exitcode;
842 /* exec 'echo foo bar' */
843 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
844 " 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ],"
845 " 'capture-output': true } }");
846 g_assert_nonnull(ret);
847 qmp_assert_no_error(ret);
848 val = qdict_get_qdict(ret, "return");
849 pid = qdict_get_int(val, "pid");
850 g_assert_cmpint(pid, >, 0);
853 /* wait for completion */
854 now = g_get_monotonic_time();
855 cmd = g_strdup_printf("{'execute': 'guest-exec-status',"
856 " 'arguments': { 'pid': %" PRId64 " } }", pid);
858 ret = qmp_fd(fixture->fd, cmd);
859 g_assert_nonnull(ret);
860 val = qdict_get_qdict(ret, "return");
861 exited = qdict_get_bool(val, "exited");
866 g_get_monotonic_time() < now + 5 * G_TIME_SPAN_SECOND);
871 exitcode = qdict_get_int(val, "exitcode");
872 g_assert_cmpint(exitcode, ==, 0);
873 out = qdict_get_str(val, "out-data");
874 decoded = g_base64_decode(out, &len);
875 g_assert_cmpint(len, ==, 12);
876 g_assert_cmpstr((char *)decoded, ==, "\" test_str \"");
881 static void test_qga_guest_exec_invalid(gconstpointer fix)
883 const TestFixture *fixture = fix;
885 const gchar *class, *desc;
887 /* invalid command */
888 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
889 " 'path': '/bin/invalid-cmd42' } }");
890 g_assert_nonnull(ret);
891 error = qdict_get_qdict(ret, "error");
892 g_assert_nonnull(error);
893 class = qdict_get_str(error, "class");
894 desc = qdict_get_str(error, "desc");
895 g_assert_cmpstr(class, ==, "GenericError");
896 g_assert_cmpint(strlen(desc), >, 0);
900 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec-status',"
901 " 'arguments': { 'pid': 0 } }");
902 g_assert_nonnull(ret);
903 error = qdict_get_qdict(ret, "error");
904 g_assert_nonnull(error);
905 class = qdict_get_str(error, "class");
906 desc = qdict_get_str(error, "desc");
907 g_assert_cmpstr(class, ==, "GenericError");
908 g_assert_cmpint(strlen(desc), >, 0);
912 int main(int argc, char **argv)
917 setlocale (LC_ALL, "");
918 g_test_init(&argc, &argv, NULL);
919 fixture_setup(&fix, NULL);
921 g_test_add_data_func("/qga/sync-delimited", &fix, test_qga_sync_delimited);
922 g_test_add_data_func("/qga/sync", &fix, test_qga_sync);
923 g_test_add_data_func("/qga/ping", &fix, test_qga_ping);
924 g_test_add_data_func("/qga/info", &fix, test_qga_info);
925 g_test_add_data_func("/qga/network-get-interfaces", &fix,
926 test_qga_network_get_interfaces);
927 g_test_add_data_func("/qga/get-vcpus", &fix, test_qga_get_vcpus);
928 g_test_add_data_func("/qga/get-fsinfo", &fix, test_qga_get_fsinfo);
929 g_test_add_data_func("/qga/get-memory-block-info", &fix,
930 test_qga_get_memory_block_info);
931 g_test_add_data_func("/qga/get-memory-blocks", &fix,
932 test_qga_get_memory_blocks);
933 g_test_add_data_func("/qga/file-ops", &fix, test_qga_file_ops);
934 g_test_add_data_func("/qga/file-write-read", &fix, test_qga_file_write_read);
935 g_test_add_data_func("/qga/get-time", &fix, test_qga_get_time);
936 g_test_add_data_func("/qga/invalid-cmd", &fix, test_qga_invalid_cmd);
937 g_test_add_data_func("/qga/invalid-args", &fix, test_qga_invalid_args);
938 g_test_add_data_func("/qga/fsfreeze-status", &fix,
939 test_qga_fsfreeze_status);
941 g_test_add_data_func("/qga/blacklist", NULL, test_qga_blacklist);
942 g_test_add_data_func("/qga/config", NULL, test_qga_config);
943 g_test_add_data_func("/qga/guest-exec", &fix, test_qga_guest_exec);
944 g_test_add_data_func("/qga/guest-exec-invalid", &fix,
945 test_qga_guest_exec_invalid);
947 if (g_getenv("QGA_TEST_SIDE_EFFECTING")) {
948 g_test_add_data_func("/qga/fsfreeze-and-thaw", &fix,
949 test_qga_fsfreeze_and_thaw);
950 g_test_add_data_func("/qga/set-time", &fix, test_qga_set_time);
951 g_test_add_data_func("/qga/fstrim", &fix, test_qga_fstrim);
956 fixture_tear_down(&fix, NULL);