1 #include "qemu/osdep.h"
3 #include <glib/gstdio.h>
4 #include <sys/socket.h>
8 #include "qapi/qmp/qdict.h"
9 #include "qapi/qmp/qlist.h"
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);
26 remote
.sun_family
= AF_UNIX
;
28 strcpy(remote
.sun_path
, path
);
29 len
= strlen(remote
.sun_path
) + sizeof(remote
.sun_family
);
30 ret
= connect(s
, (struct sockaddr
*)&remote
, len
);
32 g_usleep(G_USEC_PER_SEC
);
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
);
51 fixture_setup(TestFixture
*fixture
, gconstpointer data
, gchar
**envp
)
53 const gchar
*extra_arg
= data
;
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",
66 fixture
->test_dir
, path
,
67 getenv("QTEST_LOG") ? "-v" : "",
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);
89 fixture_tear_down(TestFixture
*fixture
, gconstpointer data
)
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
);
104 tmp
= g_build_filename(fixture
->test_dir
, "qga.state", NULL
);
108 tmp
= g_build_filename(fixture
->test_dir
, "sock", NULL
);
112 g_rmdir(fixture
->test_dir
);
113 g_free(fixture
->test_dir
);
116 static void qmp_assertion_message_error(const char *domain
,
123 const char *class, *desc
;
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
);
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); \
143 static void test_qga_sync_delimited(gconstpointer fix
)
145 const TestFixture
*fixture
= fix
;
146 guint32 v
, r
= g_random_int();
151 cmd
= g_strdup_printf("\xff{'execute': 'guest-sync-delimited',"
152 " 'arguments': {'id': %u } }", r
);
153 qmp_fd_send(fixture
->fd
, cmd
);
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
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
172 v
= read(fixture
->fd
, &c
, 1);
173 g_assert_cmpint(v
, ==, 1);
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
);
186 static void test_qga_sync(gconstpointer fix
)
188 const TestFixture
*fixture
= fix
;
189 guint32 v
, r
= g_random_int();
194 * TODO guest-sync is inherently limited: we cannot distinguish
195 * failure caused by reacting to garbage on the wire prior to this
196 * command, from failure of this actual command. Clients are
197 * supposed to be able to send a raw '\xff' byte to at least
198 * re-synchronize the server's parser prior to this command, but
199 * we are not in a position to test that here because (at least
200 * for now) it causes the server to issue an error message about
201 * invalid JSON. Testing of '\xff' handling is done in
202 * guest-sync-delimited instead.
204 cmd
= g_strdup_printf("{'execute': 'guest-sync',"
205 " 'arguments': {'id': %u } }", r
);
206 ret
= qmp_fd(fixture
->fd
, cmd
);
209 g_assert_nonnull(ret
);
210 qmp_assert_no_error(ret
);
212 v
= qdict_get_int(ret
, "return");
213 g_assert_cmpint(r
, ==, v
);
218 static void test_qga_ping(gconstpointer fix
)
220 const TestFixture
*fixture
= fix
;
223 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-ping'}");
224 g_assert_nonnull(ret
);
225 qmp_assert_no_error(ret
);
230 static void test_qga_invalid_args(gconstpointer fix
)
232 const TestFixture
*fixture
= fix
;
234 const gchar
*class, *desc
;
236 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-ping', "
237 "'arguments': {'foo': 42 }}");
238 g_assert_nonnull(ret
);
240 error
= qdict_get_qdict(ret
, "error");
241 class = qdict_get_try_str(error
, "class");
242 desc
= qdict_get_try_str(error
, "desc");
244 g_assert_cmpstr(class, ==, "GenericError");
245 g_assert_cmpstr(desc
, ==, "Parameter 'foo' is unexpected");
250 static void test_qga_invalid_cmd(gconstpointer fix
)
252 const TestFixture
*fixture
= fix
;
254 const gchar
*class, *desc
;
256 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-invalid-cmd'}");
257 g_assert_nonnull(ret
);
259 error
= qdict_get_qdict(ret
, "error");
260 class = qdict_get_try_str(error
, "class");
261 desc
= qdict_get_try_str(error
, "desc");
263 g_assert_cmpstr(class, ==, "CommandNotFound");
264 g_assert_cmpint(strlen(desc
), >, 0);
269 static void test_qga_info(gconstpointer fix
)
271 const TestFixture
*fixture
= fix
;
273 const gchar
*version
;
275 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-info'}");
276 g_assert_nonnull(ret
);
277 qmp_assert_no_error(ret
);
279 val
= qdict_get_qdict(ret
, "return");
280 version
= qdict_get_try_str(val
, "version");
281 g_assert_cmpstr(version
, ==, QEMU_VERSION
);
286 static void test_qga_get_vcpus(gconstpointer fix
)
288 const TestFixture
*fixture
= fix
;
291 const QListEntry
*entry
;
293 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-vcpus'}");
294 g_assert_nonnull(ret
);
295 qmp_assert_no_error(ret
);
297 /* check there is at least a cpu */
298 list
= qdict_get_qlist(ret
, "return");
299 entry
= qlist_first(list
);
300 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "online"));
301 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "logical-id"));
306 static void test_qga_get_fsinfo(gconstpointer fix
)
308 const TestFixture
*fixture
= fix
;
311 const QListEntry
*entry
;
313 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-fsinfo'}");
314 g_assert_nonnull(ret
);
315 qmp_assert_no_error(ret
);
317 /* sanity-check the response if there are any filesystems */
318 list
= qdict_get_qlist(ret
, "return");
319 entry
= qlist_first(list
);
321 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "name"));
322 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "mountpoint"));
323 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "type"));
324 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "disk"));
330 static void test_qga_get_memory_block_info(gconstpointer fix
)
332 const TestFixture
*fixture
= fix
;
336 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-memory-block-info'}");
337 g_assert_nonnull(ret
);
339 /* some systems might not expose memory block info in sysfs */
340 if (!qdict_haskey(ret
, "error")) {
341 /* check there is at least some memory */
342 val
= qdict_get_qdict(ret
, "return");
343 size
= qdict_get_int(val
, "size");
344 g_assert_cmpint(size
, >, 0);
350 static void test_qga_get_memory_blocks(gconstpointer fix
)
352 const TestFixture
*fixture
= fix
;
355 const QListEntry
*entry
;
357 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-memory-blocks'}");
358 g_assert_nonnull(ret
);
360 /* some systems might not expose memory block info in sysfs */
361 if (!qdict_haskey(ret
, "error")) {
362 list
= qdict_get_qlist(ret
, "return");
363 entry
= qlist_first(list
);
364 /* newer versions of qga may return empty list without error */
366 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
),
368 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "online"));
375 static void test_qga_network_get_interfaces(gconstpointer fix
)
377 const TestFixture
*fixture
= fix
;
380 const QListEntry
*entry
;
382 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-network-get-interfaces'}");
383 g_assert_nonnull(ret
);
384 qmp_assert_no_error(ret
);
386 /* check there is at least an interface */
387 list
= qdict_get_qlist(ret
, "return");
388 entry
= qlist_first(list
);
389 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "name"));
394 static void test_qga_file_ops(gconstpointer fix
)
396 const TestFixture
*fixture
= fix
;
397 const unsigned char helloworld
[] = "Hello World!\n";
399 gchar
*cmd
, *path
, *enc
;
408 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
409 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
410 g_assert_nonnull(ret
);
411 qmp_assert_no_error(ret
);
412 id
= qdict_get_int(ret
, "return");
415 enc
= g_base64_encode(helloworld
, sizeof(helloworld
));
417 cmd
= g_strdup_printf("{'execute': 'guest-file-write',"
418 " 'arguments': { 'handle': %" PRId64
","
419 " 'buf-b64': '%s' } }", id
, enc
);
420 ret
= qmp_fd(fixture
->fd
, cmd
);
421 g_assert_nonnull(ret
);
422 qmp_assert_no_error(ret
);
424 val
= qdict_get_qdict(ret
, "return");
425 count
= qdict_get_int(val
, "count");
426 eof
= qdict_get_bool(val
, "eof");
427 g_assert_cmpint(count
, ==, sizeof(helloworld
));
428 g_assert_cmpint(eof
, ==, 0);
433 cmd
= g_strdup_printf("{'execute': 'guest-file-flush',"
434 " 'arguments': {'handle': %" PRId64
"} }",
436 ret
= qmp_fd(fixture
->fd
, cmd
);
441 cmd
= g_strdup_printf("{'execute': 'guest-file-close',"
442 " 'arguments': {'handle': %" PRId64
"} }",
444 ret
= qmp_fd(fixture
->fd
, cmd
);
449 path
= g_build_filename(fixture
->test_dir
, "foo", NULL
);
450 f
= fopen(path
, "r");
453 count
= fread(tmp
, 1, sizeof(tmp
), f
);
454 g_assert_cmpint(count
, ==, sizeof(helloworld
));
456 g_assert_cmpstr(tmp
, ==, (char *)helloworld
);
460 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
461 " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
462 g_assert_nonnull(ret
);
463 qmp_assert_no_error(ret
);
464 id
= qdict_get_int(ret
, "return");
468 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
469 " 'arguments': { 'handle': %" PRId64
"} }",
471 ret
= qmp_fd(fixture
->fd
, cmd
);
472 val
= qdict_get_qdict(ret
, "return");
473 count
= qdict_get_int(val
, "count");
474 eof
= qdict_get_bool(val
, "eof");
475 b64
= qdict_get_str(val
, "buf-b64");
476 g_assert_cmpint(count
, ==, sizeof(helloworld
));
478 g_assert_cmpstr(b64
, ==, enc
);
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
, ==, 0);
495 g_assert_cmpstr(b64
, ==, "");
500 cmd
= g_strdup_printf("{'execute': 'guest-file-seek',"
501 " 'arguments': { 'handle': %" PRId64
", "
502 " 'offset': %d, 'whence': '%s' } }",
504 ret
= qmp_fd(fixture
->fd
, cmd
);
505 qmp_assert_no_error(ret
);
506 val
= qdict_get_qdict(ret
, "return");
507 count
= qdict_get_int(val
, "position");
508 eof
= qdict_get_bool(val
, "eof");
509 g_assert_cmpint(count
, ==, 6);
515 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
516 " 'arguments': { 'handle': %" PRId64
"} }",
518 ret
= qmp_fd(fixture
->fd
, cmd
);
519 val
= qdict_get_qdict(ret
, "return");
520 count
= qdict_get_int(val
, "count");
521 eof
= qdict_get_bool(val
, "eof");
522 b64
= qdict_get_str(val
, "buf-b64");
523 g_assert_cmpint(count
, ==, sizeof(helloworld
) - 6);
525 dec
= g_base64_decode(b64
, &count
);
526 g_assert_cmpint(count
, ==, sizeof(helloworld
) - 6);
527 g_assert_cmpmem(dec
, count
, helloworld
+ 6, sizeof(helloworld
) - 6);
534 cmd
= g_strdup_printf("{'execute': 'guest-file-close',"
535 " 'arguments': {'handle': %" PRId64
"} }",
537 ret
= qmp_fd(fixture
->fd
, cmd
);
542 static void test_qga_file_write_read(gconstpointer fix
)
544 const TestFixture
*fixture
= fix
;
545 const unsigned char helloworld
[] = "Hello World!\n";
553 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
554 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
555 g_assert_nonnull(ret
);
556 qmp_assert_no_error(ret
);
557 id
= qdict_get_int(ret
, "return");
560 enc
= g_base64_encode(helloworld
, sizeof(helloworld
));
562 cmd
= g_strdup_printf("{'execute': 'guest-file-write',"
563 " 'arguments': { 'handle': %" PRId64
","
564 " 'buf-b64': '%s' } }", id
, enc
);
565 ret
= qmp_fd(fixture
->fd
, cmd
);
566 g_assert_nonnull(ret
);
567 qmp_assert_no_error(ret
);
569 val
= qdict_get_qdict(ret
, "return");
570 count
= qdict_get_int(val
, "count");
571 eof
= qdict_get_bool(val
, "eof");
572 g_assert_cmpint(count
, ==, sizeof(helloworld
));
573 g_assert_cmpint(eof
, ==, 0);
577 /* read (check implicit flush) */
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
, ==, 0);
588 g_assert_cmpstr(b64
, ==, "");
593 cmd
= g_strdup_printf("{'execute': 'guest-file-seek',"
594 " 'arguments': { 'handle': %" PRId64
", "
595 " 'offset': %d, 'whence': '%s' } }",
597 ret
= qmp_fd(fixture
->fd
, cmd
);
598 qmp_assert_no_error(ret
);
599 val
= qdict_get_qdict(ret
, "return");
600 count
= qdict_get_int(val
, "position");
601 eof
= qdict_get_bool(val
, "eof");
602 g_assert_cmpint(count
, ==, 0);
608 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
609 " 'arguments': { 'handle': %" PRId64
"} }",
611 ret
= qmp_fd(fixture
->fd
, cmd
);
612 val
= qdict_get_qdict(ret
, "return");
613 count
= qdict_get_int(val
, "count");
614 eof
= qdict_get_bool(val
, "eof");
615 b64
= qdict_get_str(val
, "buf-b64");
616 g_assert_cmpint(count
, ==, sizeof(helloworld
));
618 g_assert_cmpstr(b64
, ==, enc
);
624 cmd
= g_strdup_printf("{'execute': 'guest-file-close',"
625 " 'arguments': {'handle': %" PRId64
"} }",
627 ret
= qmp_fd(fixture
->fd
, cmd
);
632 static void test_qga_get_time(gconstpointer fix
)
634 const TestFixture
*fixture
= fix
;
638 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-time'}");
639 g_assert_nonnull(ret
);
640 qmp_assert_no_error(ret
);
642 time
= qdict_get_int(ret
, "return");
643 g_assert_cmpint(time
, >, 0);
648 static void test_qga_blacklist(gconstpointer data
)
652 const gchar
*class, *desc
;
654 fixture_setup(&fix
, "-b guest-ping,guest-get-time", NULL
);
656 /* check blacklist */
657 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-ping'}");
658 g_assert_nonnull(ret
);
659 error
= qdict_get_qdict(ret
, "error");
660 class = qdict_get_try_str(error
, "class");
661 desc
= qdict_get_try_str(error
, "desc");
662 g_assert_cmpstr(class, ==, "GenericError");
663 g_assert_nonnull(g_strstr_len(desc
, -1, "has been disabled"));
666 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-get-time'}");
667 g_assert_nonnull(ret
);
668 error
= qdict_get_qdict(ret
, "error");
669 class = qdict_get_try_str(error
, "class");
670 desc
= qdict_get_try_str(error
, "desc");
671 g_assert_cmpstr(class, ==, "GenericError");
672 g_assert_nonnull(g_strstr_len(desc
, -1, "has been disabled"));
675 /* check something work */
676 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-get-fsinfo'}");
677 qmp_assert_no_error(ret
);
680 fixture_tear_down(&fix
, NULL
);
683 static void test_qga_config(gconstpointer data
)
685 GError
*error
= NULL
;
686 char *cwd
, *cmd
, *out
, *err
, *str
, **strv
, **argv
= NULL
;
692 cwd
= g_get_current_dir();
693 cmd
= g_strdup_printf("%s%cqemu-ga -D",
694 cwd
, G_DIR_SEPARATOR
);
696 g_shell_parse_argv(cmd
, NULL
, &argv
, &error
);
698 g_assert_no_error(error
);
700 env
[0] = g_strdup_printf("QGA_CONF=tests%cdata%ctest-qga-config",
701 G_DIR_SEPARATOR
, G_DIR_SEPARATOR
);
703 g_spawn_sync(NULL
, argv
, env
, 0,
704 NULL
, NULL
, &out
, &err
, &status
, &error
);
707 g_assert_no_error(error
);
708 g_assert_cmpstr(err
, ==, "");
709 g_assert_cmpint(status
, ==, 0);
711 kf
= g_key_file_new();
712 g_key_file_load_from_data(kf
, out
, -1, G_KEY_FILE_NONE
, &error
);
713 g_assert_no_error(error
);
715 str
= g_key_file_get_start_group(kf
);
716 g_assert_cmpstr(str
, ==, "general");
719 g_assert_false(g_key_file_get_boolean(kf
, "general", "daemon", &error
));
720 g_assert_no_error(error
);
722 str
= g_key_file_get_string(kf
, "general", "method", &error
);
723 g_assert_no_error(error
);
724 g_assert_cmpstr(str
, ==, "virtio-serial");
727 str
= g_key_file_get_string(kf
, "general", "path", &error
);
728 g_assert_no_error(error
);
729 g_assert_cmpstr(str
, ==, "/path/to/org.qemu.guest_agent.0");
732 str
= g_key_file_get_string(kf
, "general", "pidfile", &error
);
733 g_assert_no_error(error
);
734 g_assert_cmpstr(str
, ==, "/var/foo/qemu-ga.pid");
737 str
= g_key_file_get_string(kf
, "general", "statedir", &error
);
738 g_assert_no_error(error
);
739 g_assert_cmpstr(str
, ==, "/var/state");
742 g_assert_true(g_key_file_get_boolean(kf
, "general", "verbose", &error
));
743 g_assert_no_error(error
);
745 strv
= g_key_file_get_string_list(kf
, "general", "blacklist", &n
, &error
);
746 g_assert_cmpint(n
, ==, 2);
747 #if GLIB_CHECK_VERSION(2, 44, 0)
748 g_assert_true(g_strv_contains((const char * const *)strv
,
750 g_assert_true(g_strv_contains((const char * const *)strv
,
753 g_assert_no_error(error
);
762 static void test_qga_fsfreeze_status(gconstpointer fix
)
764 const TestFixture
*fixture
= fix
;
768 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fsfreeze-status'}");
769 g_assert_nonnull(ret
);
770 qmp_assert_no_error(ret
);
772 status
= qdict_get_try_str(ret
, "return");
773 g_assert_cmpstr(status
, ==, "thawed");
778 static void test_qga_guest_exec(gconstpointer fix
)
780 const TestFixture
*fixture
= fix
;
784 int64_t pid
, now
, exitcode
;
789 /* exec 'echo foo bar' */
790 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec', 'arguments': {"
791 " 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ],"
792 " 'capture-output': true } }");
793 g_assert_nonnull(ret
);
794 qmp_assert_no_error(ret
);
795 val
= qdict_get_qdict(ret
, "return");
796 pid
= qdict_get_int(val
, "pid");
797 g_assert_cmpint(pid
, >, 0);
800 /* wait for completion */
801 now
= g_get_monotonic_time();
802 cmd
= g_strdup_printf("{'execute': 'guest-exec-status',"
803 " 'arguments': { 'pid': %" PRId64
" } }", pid
);
805 ret
= qmp_fd(fixture
->fd
, cmd
);
806 g_assert_nonnull(ret
);
807 val
= qdict_get_qdict(ret
, "return");
808 exited
= qdict_get_bool(val
, "exited");
813 g_get_monotonic_time() < now
+ 5 * G_TIME_SPAN_SECOND
);
818 exitcode
= qdict_get_int(val
, "exitcode");
819 g_assert_cmpint(exitcode
, ==, 0);
820 out
= qdict_get_str(val
, "out-data");
821 decoded
= g_base64_decode(out
, &len
);
822 g_assert_cmpint(len
, ==, 12);
823 g_assert_cmpstr((char *)decoded
, ==, "\" test_str \"");
828 static void test_qga_guest_exec_invalid(gconstpointer fix
)
830 const TestFixture
*fixture
= fix
;
832 const gchar
*class, *desc
;
834 /* invalid command */
835 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec', 'arguments': {"
836 " 'path': '/bin/invalid-cmd42' } }");
837 g_assert_nonnull(ret
);
838 error
= qdict_get_qdict(ret
, "error");
839 g_assert_nonnull(error
);
840 class = qdict_get_str(error
, "class");
841 desc
= qdict_get_str(error
, "desc");
842 g_assert_cmpstr(class, ==, "GenericError");
843 g_assert_cmpint(strlen(desc
), >, 0);
847 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec-status',"
848 " 'arguments': { 'pid': 0 } }");
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);
859 static void test_qga_guest_get_osinfo(gconstpointer data
)
866 cwd
= g_get_current_dir();
867 env
[0] = g_strdup_printf(
868 "QGA_OS_RELEASE=%s%ctests%cdata%ctest-qga-os-release",
869 cwd
, G_DIR_SEPARATOR
, G_DIR_SEPARATOR
, G_DIR_SEPARATOR
);
872 fixture_setup(&fixture
, NULL
, env
);
874 ret
= qmp_fd(fixture
.fd
, "{'execute': 'guest-get-osinfo'}");
875 g_assert_nonnull(ret
);
876 qmp_assert_no_error(ret
);
878 val
= qdict_get_qdict(ret
, "return");
880 str
= qdict_get_try_str(val
, "id");
881 g_assert_nonnull(str
);
882 g_assert_cmpstr(str
, ==, "qemu-ga-test");
884 str
= qdict_get_try_str(val
, "name");
885 g_assert_nonnull(str
);
886 g_assert_cmpstr(str
, ==, "QEMU-GA");
888 str
= qdict_get_try_str(val
, "pretty-name");
889 g_assert_nonnull(str
);
890 g_assert_cmpstr(str
, ==, "QEMU Guest Agent test");
892 str
= qdict_get_try_str(val
, "version");
893 g_assert_nonnull(str
);
894 g_assert_cmpstr(str
, ==, "Test 1");
896 str
= qdict_get_try_str(val
, "version-id");
897 g_assert_nonnull(str
);
898 g_assert_cmpstr(str
, ==, "1");
900 str
= qdict_get_try_str(val
, "variant");
901 g_assert_nonnull(str
);
902 g_assert_cmpstr(str
, ==, "Unit test \"'$`\\ and \\\\ etc.");
904 str
= qdict_get_try_str(val
, "variant-id");
905 g_assert_nonnull(str
);
906 g_assert_cmpstr(str
, ==, "unit-test");
910 fixture_tear_down(&fixture
, NULL
);
913 int main(int argc
, char **argv
)
918 setlocale (LC_ALL
, "");
919 g_test_init(&argc
, &argv
, NULL
);
920 fixture_setup(&fix
, NULL
, NULL
);
922 g_test_add_data_func("/qga/sync-delimited", &fix
, test_qga_sync_delimited
);
923 g_test_add_data_func("/qga/sync", &fix
, test_qga_sync
);
924 g_test_add_data_func("/qga/ping", &fix
, test_qga_ping
);
925 g_test_add_data_func("/qga/info", &fix
, test_qga_info
);
926 g_test_add_data_func("/qga/network-get-interfaces", &fix
,
927 test_qga_network_get_interfaces
);
928 if (!access("/sys/devices/system/cpu/cpu0", F_OK
)) {
929 g_test_add_data_func("/qga/get-vcpus", &fix
, test_qga_get_vcpus
);
931 g_test_add_data_func("/qga/get-fsinfo", &fix
, test_qga_get_fsinfo
);
932 g_test_add_data_func("/qga/get-memory-block-info", &fix
,
933 test_qga_get_memory_block_info
);
934 g_test_add_data_func("/qga/get-memory-blocks", &fix
,
935 test_qga_get_memory_blocks
);
936 g_test_add_data_func("/qga/file-ops", &fix
, test_qga_file_ops
);
937 g_test_add_data_func("/qga/file-write-read", &fix
, test_qga_file_write_read
);
938 g_test_add_data_func("/qga/get-time", &fix
, test_qga_get_time
);
939 g_test_add_data_func("/qga/invalid-cmd", &fix
, test_qga_invalid_cmd
);
940 g_test_add_data_func("/qga/invalid-args", &fix
, test_qga_invalid_args
);
941 g_test_add_data_func("/qga/fsfreeze-status", &fix
,
942 test_qga_fsfreeze_status
);
944 g_test_add_data_func("/qga/blacklist", NULL
, test_qga_blacklist
);
945 g_test_add_data_func("/qga/config", NULL
, test_qga_config
);
946 g_test_add_data_func("/qga/guest-exec", &fix
, test_qga_guest_exec
);
947 g_test_add_data_func("/qga/guest-exec-invalid", &fix
,
948 test_qga_guest_exec_invalid
);
949 g_test_add_data_func("/qga/guest-get-osinfo", &fix
,
950 test_qga_guest_get_osinfo
);
954 fixture_tear_down(&fix
, NULL
);