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