1 #include "qemu/osdep.h"
3 #include <glib/gstdio.h>
4 #include <sys/socket.h>
7 #include "libqos/libqtest.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%cqga%cqemu-ga -m unix-listen -t %s -p %s %s %s",
65 cwd
, G_DIR_SEPARATOR
, G_DIR_SEPARATOR
,
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_test_rand_int();
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
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_test_rand_int();
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
);
216 static void test_qga_ping(gconstpointer fix
)
218 const TestFixture
*fixture
= fix
;
221 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-ping'}");
222 g_assert_nonnull(ret
);
223 qmp_assert_no_error(ret
);
228 static void test_qga_id(gconstpointer fix
)
230 const TestFixture
*fixture
= fix
;
233 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-ping', 'id': 1}");
234 g_assert_nonnull(ret
);
235 qmp_assert_no_error(ret
);
236 g_assert_cmpint(qdict_get_int(ret
, "id"), ==, 1);
241 static void test_qga_invalid_oob(gconstpointer fix
)
243 const TestFixture
*fixture
= fix
;
246 ret
= qmp_fd(fixture
->fd
, "{'exec-oob': 'guest-ping'}");
247 g_assert_nonnull(ret
);
249 qmp_expect_error_and_unref(ret
, "GenericError");
252 static void test_qga_invalid_args(gconstpointer fix
)
254 const TestFixture
*fixture
= fix
;
256 const gchar
*class, *desc
;
258 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-ping', "
259 "'arguments': {'foo': 42 }}");
260 g_assert_nonnull(ret
);
262 error
= qdict_get_qdict(ret
, "error");
263 class = qdict_get_try_str(error
, "class");
264 desc
= qdict_get_try_str(error
, "desc");
266 g_assert_cmpstr(class, ==, "GenericError");
267 g_assert_cmpstr(desc
, ==, "Parameter 'foo' is unexpected");
272 static void test_qga_invalid_cmd(gconstpointer fix
)
274 const TestFixture
*fixture
= fix
;
276 const gchar
*class, *desc
;
278 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-invalid-cmd'}");
279 g_assert_nonnull(ret
);
281 error
= qdict_get_qdict(ret
, "error");
282 class = qdict_get_try_str(error
, "class");
283 desc
= qdict_get_try_str(error
, "desc");
285 g_assert_cmpstr(class, ==, "CommandNotFound");
286 g_assert_cmpint(strlen(desc
), >, 0);
291 static void test_qga_info(gconstpointer fix
)
293 const TestFixture
*fixture
= fix
;
295 const gchar
*version
;
297 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-info'}");
298 g_assert_nonnull(ret
);
299 qmp_assert_no_error(ret
);
301 val
= qdict_get_qdict(ret
, "return");
302 version
= qdict_get_try_str(val
, "version");
303 g_assert_cmpstr(version
, ==, QEMU_VERSION
);
308 static void test_qga_get_vcpus(gconstpointer fix
)
310 const TestFixture
*fixture
= fix
;
313 const QListEntry
*entry
;
315 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-vcpus'}");
316 g_assert_nonnull(ret
);
317 qmp_assert_no_error(ret
);
319 /* check there is at least a cpu */
320 list
= qdict_get_qlist(ret
, "return");
321 entry
= qlist_first(list
);
322 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "online"));
323 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "logical-id"));
328 static void test_qga_get_fsinfo(gconstpointer fix
)
330 const TestFixture
*fixture
= fix
;
333 const QListEntry
*entry
;
335 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-fsinfo'}");
336 g_assert_nonnull(ret
);
337 qmp_assert_no_error(ret
);
339 /* sanity-check the response if there are any filesystems */
340 list
= qdict_get_qlist(ret
, "return");
341 entry
= qlist_first(list
);
343 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "name"));
344 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "mountpoint"));
345 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "type"));
346 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "disk"));
352 static void test_qga_get_memory_block_info(gconstpointer fix
)
354 const TestFixture
*fixture
= fix
;
358 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-memory-block-info'}");
359 g_assert_nonnull(ret
);
361 /* some systems might not expose memory block info in sysfs */
362 if (!qdict_haskey(ret
, "error")) {
363 /* check there is at least some memory */
364 val
= qdict_get_qdict(ret
, "return");
365 size
= qdict_get_int(val
, "size");
366 g_assert_cmpint(size
, >, 0);
372 static void test_qga_get_memory_blocks(gconstpointer fix
)
374 const TestFixture
*fixture
= fix
;
377 const QListEntry
*entry
;
379 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-memory-blocks'}");
380 g_assert_nonnull(ret
);
382 /* some systems might not expose memory block info in sysfs */
383 if (!qdict_haskey(ret
, "error")) {
384 list
= qdict_get_qlist(ret
, "return");
385 entry
= qlist_first(list
);
386 /* newer versions of qga may return empty list without error */
388 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
),
390 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "online"));
397 static void test_qga_network_get_interfaces(gconstpointer fix
)
399 const TestFixture
*fixture
= fix
;
402 const QListEntry
*entry
;
404 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-network-get-interfaces'}");
405 g_assert_nonnull(ret
);
406 qmp_assert_no_error(ret
);
408 /* check there is at least an interface */
409 list
= qdict_get_qlist(ret
, "return");
410 entry
= qlist_first(list
);
411 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "name"));
416 static void test_qga_file_ops(gconstpointer fix
)
418 const TestFixture
*fixture
= fix
;
419 const unsigned char helloworld
[] = "Hello World!\n";
430 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
431 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
432 g_assert_nonnull(ret
);
433 qmp_assert_no_error(ret
);
434 id
= qdict_get_int(ret
, "return");
437 enc
= g_base64_encode(helloworld
, sizeof(helloworld
));
439 ret
= qmp_fd(fixture
->fd
,
440 "{'execute': 'guest-file-write',"
441 " 'arguments': { 'handle': %" PRId64
", 'buf-b64': %s } }",
443 g_assert_nonnull(ret
);
444 qmp_assert_no_error(ret
);
446 val
= qdict_get_qdict(ret
, "return");
447 count
= qdict_get_int(val
, "count");
448 eof
= qdict_get_bool(val
, "eof");
449 g_assert_cmpint(count
, ==, sizeof(helloworld
));
450 g_assert_cmpint(eof
, ==, 0);
454 ret
= qmp_fd(fixture
->fd
,
455 "{'execute': 'guest-file-flush',"
456 " 'arguments': {'handle': %" PRId64
"} }",
461 ret
= qmp_fd(fixture
->fd
,
462 "{'execute': 'guest-file-close',"
463 " 'arguments': {'handle': %" PRId64
"} }",
468 path
= g_build_filename(fixture
->test_dir
, "foo", NULL
);
469 f
= fopen(path
, "r");
472 count
= fread(tmp
, 1, sizeof(tmp
), f
);
473 g_assert_cmpint(count
, ==, sizeof(helloworld
));
475 g_assert_cmpstr(tmp
, ==, (char *)helloworld
);
479 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
480 " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
481 g_assert_nonnull(ret
);
482 qmp_assert_no_error(ret
);
483 id
= qdict_get_int(ret
, "return");
487 ret
= qmp_fd(fixture
->fd
,
488 "{'execute': 'guest-file-read',"
489 " 'arguments': { 'handle': %" PRId64
"} }",
491 val
= qdict_get_qdict(ret
, "return");
492 count
= qdict_get_int(val
, "count");
493 eof
= qdict_get_bool(val
, "eof");
494 b64
= qdict_get_str(val
, "buf-b64");
495 g_assert_cmpint(count
, ==, sizeof(helloworld
));
497 g_assert_cmpstr(b64
, ==, enc
);
503 ret
= qmp_fd(fixture
->fd
,
504 "{'execute': 'guest-file-read',"
505 " 'arguments': { 'handle': %" PRId64
"} }",
507 val
= qdict_get_qdict(ret
, "return");
508 count
= qdict_get_int(val
, "count");
509 eof
= qdict_get_bool(val
, "eof");
510 b64
= qdict_get_str(val
, "buf-b64");
511 g_assert_cmpint(count
, ==, 0);
513 g_assert_cmpstr(b64
, ==, "");
517 ret
= qmp_fd(fixture
->fd
,
518 "{'execute': 'guest-file-seek',"
519 " 'arguments': { 'handle': %" PRId64
", "
520 " 'offset': %d, 'whence': %s } }",
522 qmp_assert_no_error(ret
);
523 val
= qdict_get_qdict(ret
, "return");
524 count
= qdict_get_int(val
, "position");
525 eof
= qdict_get_bool(val
, "eof");
526 g_assert_cmpint(count
, ==, 6);
531 ret
= qmp_fd(fixture
->fd
,
532 "{'execute': 'guest-file-read',"
533 " 'arguments': { 'handle': %" PRId64
"} }",
535 val
= qdict_get_qdict(ret
, "return");
536 count
= qdict_get_int(val
, "count");
537 eof
= qdict_get_bool(val
, "eof");
538 b64
= qdict_get_str(val
, "buf-b64");
539 g_assert_cmpint(count
, ==, sizeof(helloworld
) - 6);
541 dec
= g_base64_decode(b64
, &count
);
542 g_assert_cmpint(count
, ==, sizeof(helloworld
) - 6);
543 g_assert_cmpmem(dec
, count
, helloworld
+ 6, sizeof(helloworld
) - 6);
549 ret
= qmp_fd(fixture
->fd
,
550 "{'execute': 'guest-file-close',"
551 " 'arguments': {'handle': %" PRId64
"} }",
556 static void test_qga_file_write_read(gconstpointer fix
)
558 const TestFixture
*fixture
= fix
;
559 const unsigned char helloworld
[] = "Hello World!\n";
567 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
568 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
569 g_assert_nonnull(ret
);
570 qmp_assert_no_error(ret
);
571 id
= qdict_get_int(ret
, "return");
574 enc
= g_base64_encode(helloworld
, sizeof(helloworld
));
576 ret
= qmp_fd(fixture
->fd
,
577 "{'execute': 'guest-file-write',"
578 " 'arguments': { 'handle': %" PRId64
","
579 " 'buf-b64': %s } }", id
, enc
);
580 g_assert_nonnull(ret
);
581 qmp_assert_no_error(ret
);
583 val
= qdict_get_qdict(ret
, "return");
584 count
= qdict_get_int(val
, "count");
585 eof
= qdict_get_bool(val
, "eof");
586 g_assert_cmpint(count
, ==, sizeof(helloworld
));
587 g_assert_cmpint(eof
, ==, 0);
590 /* read (check implicit flush) */
591 ret
= qmp_fd(fixture
->fd
,
592 "{'execute': 'guest-file-read',"
593 " 'arguments': { 'handle': %" PRId64
"} }",
595 val
= qdict_get_qdict(ret
, "return");
596 count
= qdict_get_int(val
, "count");
597 eof
= qdict_get_bool(val
, "eof");
598 b64
= qdict_get_str(val
, "buf-b64");
599 g_assert_cmpint(count
, ==, 0);
601 g_assert_cmpstr(b64
, ==, "");
605 ret
= qmp_fd(fixture
->fd
,
606 "{'execute': 'guest-file-seek',"
607 " 'arguments': { 'handle': %" PRId64
", "
608 " 'offset': %d, 'whence': %s } }",
610 qmp_assert_no_error(ret
);
611 val
= qdict_get_qdict(ret
, "return");
612 count
= qdict_get_int(val
, "position");
613 eof
= qdict_get_bool(val
, "eof");
614 g_assert_cmpint(count
, ==, 0);
619 ret
= qmp_fd(fixture
->fd
,
620 "{'execute': 'guest-file-read',"
621 " 'arguments': { 'handle': %" PRId64
"} }",
623 val
= qdict_get_qdict(ret
, "return");
624 count
= qdict_get_int(val
, "count");
625 eof
= qdict_get_bool(val
, "eof");
626 b64
= qdict_get_str(val
, "buf-b64");
627 g_assert_cmpint(count
, ==, sizeof(helloworld
));
629 g_assert_cmpstr(b64
, ==, enc
);
634 ret
= qmp_fd(fixture
->fd
,
635 "{'execute': 'guest-file-close',"
636 " 'arguments': {'handle': %" PRId64
"} }",
641 static void test_qga_get_time(gconstpointer fix
)
643 const TestFixture
*fixture
= fix
;
647 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-time'}");
648 g_assert_nonnull(ret
);
649 qmp_assert_no_error(ret
);
651 time
= qdict_get_int(ret
, "return");
652 g_assert_cmpint(time
, >, 0);
657 static void test_qga_blacklist(gconstpointer data
)
661 const gchar
*class, *desc
;
663 fixture_setup(&fix
, "-b guest-ping,guest-get-time", NULL
);
665 /* check blacklist */
666 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-ping'}");
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, ==, "CommandNotFound");
672 g_assert_nonnull(g_strstr_len(desc
, -1, "has been disabled"));
675 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-get-time'}");
676 g_assert_nonnull(ret
);
677 error
= qdict_get_qdict(ret
, "error");
678 class = qdict_get_try_str(error
, "class");
679 desc
= qdict_get_try_str(error
, "desc");
680 g_assert_cmpstr(class, ==, "CommandNotFound");
681 g_assert_nonnull(g_strstr_len(desc
, -1, "has been disabled"));
684 /* check something work */
685 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-get-fsinfo'}");
686 qmp_assert_no_error(ret
);
689 fixture_tear_down(&fix
, NULL
);
692 static void test_qga_config(gconstpointer data
)
694 GError
*error
= NULL
;
695 char *cwd
, *cmd
, *out
, *err
, *str
, **strv
, **argv
= NULL
;
701 cwd
= g_get_current_dir();
702 cmd
= g_strdup_printf("%s%cqga%cqemu-ga -D",
703 cwd
, G_DIR_SEPARATOR
, G_DIR_SEPARATOR
);
705 g_shell_parse_argv(cmd
, NULL
, &argv
, &error
);
707 g_assert_no_error(error
);
709 env
[0] = g_strdup_printf("QGA_CONF=tests%cdata%ctest-qga-config",
710 G_DIR_SEPARATOR
, G_DIR_SEPARATOR
);
712 g_spawn_sync(NULL
, argv
, env
, 0,
713 NULL
, NULL
, &out
, &err
, &status
, &error
);
716 g_assert_no_error(error
);
717 g_assert_cmpstr(err
, ==, "");
718 g_assert_cmpint(status
, ==, 0);
720 kf
= g_key_file_new();
721 g_key_file_load_from_data(kf
, out
, -1, G_KEY_FILE_NONE
, &error
);
722 g_assert_no_error(error
);
724 str
= g_key_file_get_start_group(kf
);
725 g_assert_cmpstr(str
, ==, "general");
728 g_assert_false(g_key_file_get_boolean(kf
, "general", "daemon", &error
));
729 g_assert_no_error(error
);
731 str
= g_key_file_get_string(kf
, "general", "method", &error
);
732 g_assert_no_error(error
);
733 g_assert_cmpstr(str
, ==, "virtio-serial");
736 str
= g_key_file_get_string(kf
, "general", "path", &error
);
737 g_assert_no_error(error
);
738 g_assert_cmpstr(str
, ==, "/path/to/org.qemu.guest_agent.0");
741 str
= g_key_file_get_string(kf
, "general", "pidfile", &error
);
742 g_assert_no_error(error
);
743 g_assert_cmpstr(str
, ==, "/var/foo/qemu-ga.pid");
746 str
= g_key_file_get_string(kf
, "general", "statedir", &error
);
747 g_assert_no_error(error
);
748 g_assert_cmpstr(str
, ==, "/var/state");
751 g_assert_true(g_key_file_get_boolean(kf
, "general", "verbose", &error
));
752 g_assert_no_error(error
);
754 strv
= g_key_file_get_string_list(kf
, "general", "blacklist", &n
, &error
);
755 g_assert_cmpint(n
, ==, 2);
756 g_assert_true(g_strv_contains((const char * const *)strv
,
758 g_assert_true(g_strv_contains((const char * const *)strv
,
760 g_assert_no_error(error
);
769 static void test_qga_fsfreeze_status(gconstpointer fix
)
771 const TestFixture
*fixture
= fix
;
775 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fsfreeze-status'}");
776 g_assert_nonnull(ret
);
777 qmp_assert_no_error(ret
);
779 status
= qdict_get_try_str(ret
, "return");
780 g_assert_cmpstr(status
, ==, "thawed");
785 static void test_qga_guest_exec(gconstpointer fix
)
787 const TestFixture
*fixture
= fix
;
791 int64_t pid
, now
, exitcode
;
795 /* exec 'echo foo bar' */
796 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec', 'arguments': {"
797 " 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ],"
798 " 'capture-output': true } }");
799 g_assert_nonnull(ret
);
800 qmp_assert_no_error(ret
);
801 val
= qdict_get_qdict(ret
, "return");
802 pid
= qdict_get_int(val
, "pid");
803 g_assert_cmpint(pid
, >, 0);
806 /* wait for completion */
807 now
= g_get_monotonic_time();
809 ret
= qmp_fd(fixture
->fd
,
810 "{'execute': 'guest-exec-status',"
811 " 'arguments': { 'pid': %" PRId64
" } }", pid
);
812 g_assert_nonnull(ret
);
813 val
= qdict_get_qdict(ret
, "return");
814 exited
= qdict_get_bool(val
, "exited");
819 g_get_monotonic_time() < now
+ 5 * G_TIME_SPAN_SECOND
);
823 exitcode
= qdict_get_int(val
, "exitcode");
824 g_assert_cmpint(exitcode
, ==, 0);
825 out
= qdict_get_str(val
, "out-data");
826 decoded
= g_base64_decode(out
, &len
);
827 g_assert_cmpint(len
, ==, 12);
828 g_assert_cmpstr((char *)decoded
, ==, "\" test_str \"");
833 static void test_qga_guest_exec_invalid(gconstpointer fix
)
835 const TestFixture
*fixture
= fix
;
837 const gchar
*class, *desc
;
839 /* invalid command */
840 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec', 'arguments': {"
841 " 'path': '/bin/invalid-cmd42' } }");
842 g_assert_nonnull(ret
);
843 error
= qdict_get_qdict(ret
, "error");
844 g_assert_nonnull(error
);
845 class = qdict_get_str(error
, "class");
846 desc
= qdict_get_str(error
, "desc");
847 g_assert_cmpstr(class, ==, "GenericError");
848 g_assert_cmpint(strlen(desc
), >, 0);
852 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec-status',"
853 " 'arguments': { 'pid': 0 } }");
854 g_assert_nonnull(ret
);
855 error
= qdict_get_qdict(ret
, "error");
856 g_assert_nonnull(error
);
857 class = qdict_get_str(error
, "class");
858 desc
= qdict_get_str(error
, "desc");
859 g_assert_cmpstr(class, ==, "GenericError");
860 g_assert_cmpint(strlen(desc
), >, 0);
864 static void test_qga_guest_get_host_name(gconstpointer fix
)
866 const TestFixture
*fixture
= fix
;
869 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-host-name'}");
870 g_assert_nonnull(ret
);
871 qmp_assert_no_error(ret
);
873 val
= qdict_get_qdict(ret
, "return");
874 g_assert(qdict_haskey(val
, "host-name"));
879 static void test_qga_guest_get_timezone(gconstpointer fix
)
881 const TestFixture
*fixture
= fix
;
884 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-timezone'}");
885 g_assert_nonnull(ret
);
886 qmp_assert_no_error(ret
);
888 /* Make sure there's at least offset */
889 val
= qdict_get_qdict(ret
, "return");
890 g_assert(qdict_haskey(val
, "offset"));
895 static void test_qga_guest_get_users(gconstpointer fix
)
897 const TestFixture
*fixture
= fix
;
901 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-users'}");
902 g_assert_nonnull(ret
);
903 qmp_assert_no_error(ret
);
905 /* There is not much to test here */
906 val
= qdict_get_qlist(ret
, "return");
907 g_assert_nonnull(val
);
912 static void test_qga_guest_get_osinfo(gconstpointer data
)
919 cwd
= g_get_current_dir();
920 env
[0] = g_strdup_printf(
921 "QGA_OS_RELEASE=%s%ctests%cdata%ctest-qga-os-release",
922 cwd
, G_DIR_SEPARATOR
, G_DIR_SEPARATOR
, G_DIR_SEPARATOR
);
925 fixture_setup(&fixture
, NULL
, env
);
927 ret
= qmp_fd(fixture
.fd
, "{'execute': 'guest-get-osinfo'}");
928 g_assert_nonnull(ret
);
929 qmp_assert_no_error(ret
);
931 val
= qdict_get_qdict(ret
, "return");
933 str
= qdict_get_try_str(val
, "id");
934 g_assert_nonnull(str
);
935 g_assert_cmpstr(str
, ==, "qemu-ga-test");
937 str
= qdict_get_try_str(val
, "name");
938 g_assert_nonnull(str
);
939 g_assert_cmpstr(str
, ==, "QEMU-GA");
941 str
= qdict_get_try_str(val
, "pretty-name");
942 g_assert_nonnull(str
);
943 g_assert_cmpstr(str
, ==, "QEMU Guest Agent test");
945 str
= qdict_get_try_str(val
, "version");
946 g_assert_nonnull(str
);
947 g_assert_cmpstr(str
, ==, "Test 1");
949 str
= qdict_get_try_str(val
, "version-id");
950 g_assert_nonnull(str
);
951 g_assert_cmpstr(str
, ==, "1");
953 str
= qdict_get_try_str(val
, "variant");
954 g_assert_nonnull(str
);
955 g_assert_cmpstr(str
, ==, "Unit test \"'$`\\ and \\\\ etc.");
957 str
= qdict_get_try_str(val
, "variant-id");
958 g_assert_nonnull(str
);
959 g_assert_cmpstr(str
, ==, "unit-test");
963 fixture_tear_down(&fixture
, NULL
);
966 int main(int argc
, char **argv
)
971 setlocale (LC_ALL
, "");
972 g_test_init(&argc
, &argv
, NULL
);
973 fixture_setup(&fix
, NULL
, NULL
);
975 g_test_add_data_func("/qga/sync-delimited", &fix
, test_qga_sync_delimited
);
976 g_test_add_data_func("/qga/sync", &fix
, test_qga_sync
);
977 g_test_add_data_func("/qga/ping", &fix
, test_qga_ping
);
978 g_test_add_data_func("/qga/info", &fix
, test_qga_info
);
979 g_test_add_data_func("/qga/network-get-interfaces", &fix
,
980 test_qga_network_get_interfaces
);
981 if (!access("/sys/devices/system/cpu/cpu0", F_OK
)) {
982 g_test_add_data_func("/qga/get-vcpus", &fix
, test_qga_get_vcpus
);
984 g_test_add_data_func("/qga/get-fsinfo", &fix
, test_qga_get_fsinfo
);
985 g_test_add_data_func("/qga/get-memory-block-info", &fix
,
986 test_qga_get_memory_block_info
);
987 g_test_add_data_func("/qga/get-memory-blocks", &fix
,
988 test_qga_get_memory_blocks
);
989 g_test_add_data_func("/qga/file-ops", &fix
, test_qga_file_ops
);
990 g_test_add_data_func("/qga/file-write-read", &fix
, test_qga_file_write_read
);
991 g_test_add_data_func("/qga/get-time", &fix
, test_qga_get_time
);
992 g_test_add_data_func("/qga/id", &fix
, test_qga_id
);
993 g_test_add_data_func("/qga/invalid-oob", &fix
, test_qga_invalid_oob
);
994 g_test_add_data_func("/qga/invalid-cmd", &fix
, test_qga_invalid_cmd
);
995 g_test_add_data_func("/qga/invalid-args", &fix
, test_qga_invalid_args
);
996 g_test_add_data_func("/qga/fsfreeze-status", &fix
,
997 test_qga_fsfreeze_status
);
999 g_test_add_data_func("/qga/blacklist", NULL
, test_qga_blacklist
);
1000 g_test_add_data_func("/qga/config", NULL
, test_qga_config
);
1001 g_test_add_data_func("/qga/guest-exec", &fix
, test_qga_guest_exec
);
1002 g_test_add_data_func("/qga/guest-exec-invalid", &fix
,
1003 test_qga_guest_exec_invalid
);
1004 g_test_add_data_func("/qga/guest-get-osinfo", &fix
,
1005 test_qga_guest_get_osinfo
);
1006 g_test_add_data_func("/qga/guest-get-host-name", &fix
,
1007 test_qga_guest_get_host_name
);
1008 g_test_add_data_func("/qga/guest-get-timezone", &fix
,
1009 test_qga_guest_get_timezone
);
1010 g_test_add_data_func("/qga/guest-get-users", &fix
,
1011 test_qga_guest_get_users
);
1015 fixture_tear_down(&fix
, NULL
);