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_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_random_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_invalid_id(gconstpointer fix
)
230 const TestFixture
*fixture
= fix
;
234 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-ping', 'id': 1}");
235 g_assert_nonnull(ret
);
237 error
= qdict_get_qdict(ret
, "error");
238 class = qdict_get_try_str(error
, "class");
239 g_assert_cmpstr(class, ==, "GenericError");
244 static void test_qga_invalid_oob(gconstpointer fix
)
246 const TestFixture
*fixture
= fix
;
250 ret
= qmp_fd(fixture
->fd
, "{'exec-oob': 'guest-ping'}");
251 g_assert_nonnull(ret
);
253 error
= qdict_get_qdict(ret
, "error");
254 class = qdict_get_try_str(error
, "class");
255 g_assert_cmpstr(class, ==, "GenericError");
260 static void test_qga_invalid_args(gconstpointer fix
)
262 const TestFixture
*fixture
= fix
;
264 const gchar
*class, *desc
;
266 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-ping', "
267 "'arguments': {'foo': 42 }}");
268 g_assert_nonnull(ret
);
270 error
= qdict_get_qdict(ret
, "error");
271 class = qdict_get_try_str(error
, "class");
272 desc
= qdict_get_try_str(error
, "desc");
274 g_assert_cmpstr(class, ==, "GenericError");
275 g_assert_cmpstr(desc
, ==, "Parameter 'foo' is unexpected");
280 static void test_qga_invalid_cmd(gconstpointer fix
)
282 const TestFixture
*fixture
= fix
;
284 const gchar
*class, *desc
;
286 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-invalid-cmd'}");
287 g_assert_nonnull(ret
);
289 error
= qdict_get_qdict(ret
, "error");
290 class = qdict_get_try_str(error
, "class");
291 desc
= qdict_get_try_str(error
, "desc");
293 g_assert_cmpstr(class, ==, "CommandNotFound");
294 g_assert_cmpint(strlen(desc
), >, 0);
299 static void test_qga_info(gconstpointer fix
)
301 const TestFixture
*fixture
= fix
;
303 const gchar
*version
;
305 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-info'}");
306 g_assert_nonnull(ret
);
307 qmp_assert_no_error(ret
);
309 val
= qdict_get_qdict(ret
, "return");
310 version
= qdict_get_try_str(val
, "version");
311 g_assert_cmpstr(version
, ==, QEMU_VERSION
);
316 static void test_qga_get_vcpus(gconstpointer fix
)
318 const TestFixture
*fixture
= fix
;
321 const QListEntry
*entry
;
323 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-vcpus'}");
324 g_assert_nonnull(ret
);
325 qmp_assert_no_error(ret
);
327 /* check there is at least a cpu */
328 list
= qdict_get_qlist(ret
, "return");
329 entry
= qlist_first(list
);
330 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "online"));
331 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "logical-id"));
336 static void test_qga_get_fsinfo(gconstpointer fix
)
338 const TestFixture
*fixture
= fix
;
341 const QListEntry
*entry
;
343 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-fsinfo'}");
344 g_assert_nonnull(ret
);
345 qmp_assert_no_error(ret
);
347 /* sanity-check the response if there are any filesystems */
348 list
= qdict_get_qlist(ret
, "return");
349 entry
= qlist_first(list
);
351 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "name"));
352 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "mountpoint"));
353 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "type"));
354 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "disk"));
360 static void test_qga_get_memory_block_info(gconstpointer fix
)
362 const TestFixture
*fixture
= fix
;
366 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-memory-block-info'}");
367 g_assert_nonnull(ret
);
369 /* some systems might not expose memory block info in sysfs */
370 if (!qdict_haskey(ret
, "error")) {
371 /* check there is at least some memory */
372 val
= qdict_get_qdict(ret
, "return");
373 size
= qdict_get_int(val
, "size");
374 g_assert_cmpint(size
, >, 0);
380 static void test_qga_get_memory_blocks(gconstpointer fix
)
382 const TestFixture
*fixture
= fix
;
385 const QListEntry
*entry
;
387 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-memory-blocks'}");
388 g_assert_nonnull(ret
);
390 /* some systems might not expose memory block info in sysfs */
391 if (!qdict_haskey(ret
, "error")) {
392 list
= qdict_get_qlist(ret
, "return");
393 entry
= qlist_first(list
);
394 /* newer versions of qga may return empty list without error */
396 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
),
398 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "online"));
405 static void test_qga_network_get_interfaces(gconstpointer fix
)
407 const TestFixture
*fixture
= fix
;
410 const QListEntry
*entry
;
412 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-network-get-interfaces'}");
413 g_assert_nonnull(ret
);
414 qmp_assert_no_error(ret
);
416 /* check there is at least an interface */
417 list
= qdict_get_qlist(ret
, "return");
418 entry
= qlist_first(list
);
419 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "name"));
424 static void test_qga_file_ops(gconstpointer fix
)
426 const TestFixture
*fixture
= fix
;
427 const unsigned char helloworld
[] = "Hello World!\n";
438 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
439 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
440 g_assert_nonnull(ret
);
441 qmp_assert_no_error(ret
);
442 id
= qdict_get_int(ret
, "return");
445 enc
= g_base64_encode(helloworld
, sizeof(helloworld
));
447 ret
= qmp_fd(fixture
->fd
,
448 "{'execute': 'guest-file-write',"
449 " 'arguments': { 'handle': %" PRId64
", 'buf-b64': %s } }",
451 g_assert_nonnull(ret
);
452 qmp_assert_no_error(ret
);
454 val
= qdict_get_qdict(ret
, "return");
455 count
= qdict_get_int(val
, "count");
456 eof
= qdict_get_bool(val
, "eof");
457 g_assert_cmpint(count
, ==, sizeof(helloworld
));
458 g_assert_cmpint(eof
, ==, 0);
462 ret
= qmp_fd(fixture
->fd
,
463 "{'execute': 'guest-file-flush',"
464 " 'arguments': {'handle': %" PRId64
"} }",
469 ret
= qmp_fd(fixture
->fd
,
470 "{'execute': 'guest-file-close',"
471 " 'arguments': {'handle': %" PRId64
"} }",
476 path
= g_build_filename(fixture
->test_dir
, "foo", NULL
);
477 f
= fopen(path
, "r");
480 count
= fread(tmp
, 1, sizeof(tmp
), f
);
481 g_assert_cmpint(count
, ==, sizeof(helloworld
));
483 g_assert_cmpstr(tmp
, ==, (char *)helloworld
);
487 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
488 " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
489 g_assert_nonnull(ret
);
490 qmp_assert_no_error(ret
);
491 id
= qdict_get_int(ret
, "return");
495 ret
= qmp_fd(fixture
->fd
,
496 "{'execute': 'guest-file-read',"
497 " 'arguments': { 'handle': %" PRId64
"} }",
499 val
= qdict_get_qdict(ret
, "return");
500 count
= qdict_get_int(val
, "count");
501 eof
= qdict_get_bool(val
, "eof");
502 b64
= qdict_get_str(val
, "buf-b64");
503 g_assert_cmpint(count
, ==, sizeof(helloworld
));
505 g_assert_cmpstr(b64
, ==, enc
);
511 ret
= qmp_fd(fixture
->fd
,
512 "{'execute': 'guest-file-read',"
513 " 'arguments': { 'handle': %" PRId64
"} }",
515 val
= qdict_get_qdict(ret
, "return");
516 count
= qdict_get_int(val
, "count");
517 eof
= qdict_get_bool(val
, "eof");
518 b64
= qdict_get_str(val
, "buf-b64");
519 g_assert_cmpint(count
, ==, 0);
521 g_assert_cmpstr(b64
, ==, "");
525 ret
= qmp_fd(fixture
->fd
,
526 "{'execute': 'guest-file-seek',"
527 " 'arguments': { 'handle': %" PRId64
", "
528 " 'offset': %d, 'whence': %s } }",
530 qmp_assert_no_error(ret
);
531 val
= qdict_get_qdict(ret
, "return");
532 count
= qdict_get_int(val
, "position");
533 eof
= qdict_get_bool(val
, "eof");
534 g_assert_cmpint(count
, ==, 6);
539 ret
= qmp_fd(fixture
->fd
,
540 "{'execute': 'guest-file-read',"
541 " 'arguments': { 'handle': %" PRId64
"} }",
543 val
= qdict_get_qdict(ret
, "return");
544 count
= qdict_get_int(val
, "count");
545 eof
= qdict_get_bool(val
, "eof");
546 b64
= qdict_get_str(val
, "buf-b64");
547 g_assert_cmpint(count
, ==, sizeof(helloworld
) - 6);
549 dec
= g_base64_decode(b64
, &count
);
550 g_assert_cmpint(count
, ==, sizeof(helloworld
) - 6);
551 g_assert_cmpmem(dec
, count
, helloworld
+ 6, sizeof(helloworld
) - 6);
557 ret
= qmp_fd(fixture
->fd
,
558 "{'execute': 'guest-file-close',"
559 " 'arguments': {'handle': %" PRId64
"} }",
564 static void test_qga_file_write_read(gconstpointer fix
)
566 const TestFixture
*fixture
= fix
;
567 const unsigned char helloworld
[] = "Hello World!\n";
575 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
576 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
577 g_assert_nonnull(ret
);
578 qmp_assert_no_error(ret
);
579 id
= qdict_get_int(ret
, "return");
582 enc
= g_base64_encode(helloworld
, sizeof(helloworld
));
584 ret
= qmp_fd(fixture
->fd
,
585 "{'execute': 'guest-file-write',"
586 " 'arguments': { 'handle': %" PRId64
","
587 " 'buf-b64': %s } }", id
, enc
);
588 g_assert_nonnull(ret
);
589 qmp_assert_no_error(ret
);
591 val
= qdict_get_qdict(ret
, "return");
592 count
= qdict_get_int(val
, "count");
593 eof
= qdict_get_bool(val
, "eof");
594 g_assert_cmpint(count
, ==, sizeof(helloworld
));
595 g_assert_cmpint(eof
, ==, 0);
598 /* read (check implicit flush) */
599 ret
= qmp_fd(fixture
->fd
,
600 "{'execute': 'guest-file-read',"
601 " 'arguments': { 'handle': %" PRId64
"} }",
603 val
= qdict_get_qdict(ret
, "return");
604 count
= qdict_get_int(val
, "count");
605 eof
= qdict_get_bool(val
, "eof");
606 b64
= qdict_get_str(val
, "buf-b64");
607 g_assert_cmpint(count
, ==, 0);
609 g_assert_cmpstr(b64
, ==, "");
613 ret
= qmp_fd(fixture
->fd
,
614 "{'execute': 'guest-file-seek',"
615 " 'arguments': { 'handle': %" PRId64
", "
616 " 'offset': %d, 'whence': %s } }",
618 qmp_assert_no_error(ret
);
619 val
= qdict_get_qdict(ret
, "return");
620 count
= qdict_get_int(val
, "position");
621 eof
= qdict_get_bool(val
, "eof");
622 g_assert_cmpint(count
, ==, 0);
627 ret
= qmp_fd(fixture
->fd
,
628 "{'execute': 'guest-file-read',"
629 " 'arguments': { 'handle': %" PRId64
"} }",
631 val
= qdict_get_qdict(ret
, "return");
632 count
= qdict_get_int(val
, "count");
633 eof
= qdict_get_bool(val
, "eof");
634 b64
= qdict_get_str(val
, "buf-b64");
635 g_assert_cmpint(count
, ==, sizeof(helloworld
));
637 g_assert_cmpstr(b64
, ==, enc
);
642 ret
= qmp_fd(fixture
->fd
,
643 "{'execute': 'guest-file-close',"
644 " 'arguments': {'handle': %" PRId64
"} }",
649 static void test_qga_get_time(gconstpointer fix
)
651 const TestFixture
*fixture
= fix
;
655 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-time'}");
656 g_assert_nonnull(ret
);
657 qmp_assert_no_error(ret
);
659 time
= qdict_get_int(ret
, "return");
660 g_assert_cmpint(time
, >, 0);
665 static void test_qga_blacklist(gconstpointer data
)
669 const gchar
*class, *desc
;
671 fixture_setup(&fix
, "-b guest-ping,guest-get-time", NULL
);
673 /* check blacklist */
674 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-ping'}");
675 g_assert_nonnull(ret
);
676 error
= qdict_get_qdict(ret
, "error");
677 class = qdict_get_try_str(error
, "class");
678 desc
= qdict_get_try_str(error
, "desc");
679 g_assert_cmpstr(class, ==, "GenericError");
680 g_assert_nonnull(g_strstr_len(desc
, -1, "has been disabled"));
683 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-get-time'}");
684 g_assert_nonnull(ret
);
685 error
= qdict_get_qdict(ret
, "error");
686 class = qdict_get_try_str(error
, "class");
687 desc
= qdict_get_try_str(error
, "desc");
688 g_assert_cmpstr(class, ==, "GenericError");
689 g_assert_nonnull(g_strstr_len(desc
, -1, "has been disabled"));
692 /* check something work */
693 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-get-fsinfo'}");
694 qmp_assert_no_error(ret
);
697 fixture_tear_down(&fix
, NULL
);
700 static void test_qga_config(gconstpointer data
)
702 GError
*error
= NULL
;
703 char *cwd
, *cmd
, *out
, *err
, *str
, **strv
, **argv
= NULL
;
709 cwd
= g_get_current_dir();
710 cmd
= g_strdup_printf("%s%cqemu-ga -D",
711 cwd
, G_DIR_SEPARATOR
);
713 g_shell_parse_argv(cmd
, NULL
, &argv
, &error
);
715 g_assert_no_error(error
);
717 env
[0] = g_strdup_printf("QGA_CONF=tests%cdata%ctest-qga-config",
718 G_DIR_SEPARATOR
, G_DIR_SEPARATOR
);
720 g_spawn_sync(NULL
, argv
, env
, 0,
721 NULL
, NULL
, &out
, &err
, &status
, &error
);
724 g_assert_no_error(error
);
725 g_assert_cmpstr(err
, ==, "");
726 g_assert_cmpint(status
, ==, 0);
728 kf
= g_key_file_new();
729 g_key_file_load_from_data(kf
, out
, -1, G_KEY_FILE_NONE
, &error
);
730 g_assert_no_error(error
);
732 str
= g_key_file_get_start_group(kf
);
733 g_assert_cmpstr(str
, ==, "general");
736 g_assert_false(g_key_file_get_boolean(kf
, "general", "daemon", &error
));
737 g_assert_no_error(error
);
739 str
= g_key_file_get_string(kf
, "general", "method", &error
);
740 g_assert_no_error(error
);
741 g_assert_cmpstr(str
, ==, "virtio-serial");
744 str
= g_key_file_get_string(kf
, "general", "path", &error
);
745 g_assert_no_error(error
);
746 g_assert_cmpstr(str
, ==, "/path/to/org.qemu.guest_agent.0");
749 str
= g_key_file_get_string(kf
, "general", "pidfile", &error
);
750 g_assert_no_error(error
);
751 g_assert_cmpstr(str
, ==, "/var/foo/qemu-ga.pid");
754 str
= g_key_file_get_string(kf
, "general", "statedir", &error
);
755 g_assert_no_error(error
);
756 g_assert_cmpstr(str
, ==, "/var/state");
759 g_assert_true(g_key_file_get_boolean(kf
, "general", "verbose", &error
));
760 g_assert_no_error(error
);
762 strv
= g_key_file_get_string_list(kf
, "general", "blacklist", &n
, &error
);
763 g_assert_cmpint(n
, ==, 2);
764 g_assert_true(g_strv_contains((const char * const *)strv
,
766 g_assert_true(g_strv_contains((const char * const *)strv
,
768 g_assert_no_error(error
);
777 static void test_qga_fsfreeze_status(gconstpointer fix
)
779 const TestFixture
*fixture
= fix
;
783 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fsfreeze-status'}");
784 g_assert_nonnull(ret
);
785 qmp_assert_no_error(ret
);
787 status
= qdict_get_try_str(ret
, "return");
788 g_assert_cmpstr(status
, ==, "thawed");
793 static void test_qga_guest_exec(gconstpointer fix
)
795 const TestFixture
*fixture
= fix
;
799 int64_t pid
, now
, exitcode
;
803 /* exec 'echo foo bar' */
804 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec', 'arguments': {"
805 " 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ],"
806 " 'capture-output': true } }");
807 g_assert_nonnull(ret
);
808 qmp_assert_no_error(ret
);
809 val
= qdict_get_qdict(ret
, "return");
810 pid
= qdict_get_int(val
, "pid");
811 g_assert_cmpint(pid
, >, 0);
814 /* wait for completion */
815 now
= g_get_monotonic_time();
817 ret
= qmp_fd(fixture
->fd
,
818 "{'execute': 'guest-exec-status',"
819 " 'arguments': { 'pid': %" PRId64
" } }", pid
);
820 g_assert_nonnull(ret
);
821 val
= qdict_get_qdict(ret
, "return");
822 exited
= qdict_get_bool(val
, "exited");
827 g_get_monotonic_time() < now
+ 5 * G_TIME_SPAN_SECOND
);
831 exitcode
= qdict_get_int(val
, "exitcode");
832 g_assert_cmpint(exitcode
, ==, 0);
833 out
= qdict_get_str(val
, "out-data");
834 decoded
= g_base64_decode(out
, &len
);
835 g_assert_cmpint(len
, ==, 12);
836 g_assert_cmpstr((char *)decoded
, ==, "\" test_str \"");
841 static void test_qga_guest_exec_invalid(gconstpointer fix
)
843 const TestFixture
*fixture
= fix
;
845 const gchar
*class, *desc
;
847 /* invalid command */
848 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec', 'arguments': {"
849 " 'path': '/bin/invalid-cmd42' } }");
850 g_assert_nonnull(ret
);
851 error
= qdict_get_qdict(ret
, "error");
852 g_assert_nonnull(error
);
853 class = qdict_get_str(error
, "class");
854 desc
= qdict_get_str(error
, "desc");
855 g_assert_cmpstr(class, ==, "GenericError");
856 g_assert_cmpint(strlen(desc
), >, 0);
860 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec-status',"
861 " 'arguments': { 'pid': 0 } }");
862 g_assert_nonnull(ret
);
863 error
= qdict_get_qdict(ret
, "error");
864 g_assert_nonnull(error
);
865 class = qdict_get_str(error
, "class");
866 desc
= qdict_get_str(error
, "desc");
867 g_assert_cmpstr(class, ==, "GenericError");
868 g_assert_cmpint(strlen(desc
), >, 0);
872 static void test_qga_guest_get_host_name(gconstpointer fix
)
874 const TestFixture
*fixture
= fix
;
877 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-host-name'}");
878 g_assert_nonnull(ret
);
879 qmp_assert_no_error(ret
);
881 val
= qdict_get_qdict(ret
, "return");
882 g_assert(qdict_haskey(val
, "host-name"));
887 static void test_qga_guest_get_timezone(gconstpointer fix
)
889 const TestFixture
*fixture
= fix
;
892 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-timezone'}");
893 g_assert_nonnull(ret
);
894 qmp_assert_no_error(ret
);
896 /* Make sure there's at least offset */
897 val
= qdict_get_qdict(ret
, "return");
898 g_assert(qdict_haskey(val
, "offset"));
903 static void test_qga_guest_get_users(gconstpointer fix
)
905 const TestFixture
*fixture
= fix
;
909 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-users'}");
910 g_assert_nonnull(ret
);
911 qmp_assert_no_error(ret
);
913 /* There is not much to test here */
914 val
= qdict_get_qlist(ret
, "return");
915 g_assert_nonnull(val
);
920 static void test_qga_guest_get_osinfo(gconstpointer data
)
927 cwd
= g_get_current_dir();
928 env
[0] = g_strdup_printf(
929 "QGA_OS_RELEASE=%s%ctests%cdata%ctest-qga-os-release",
930 cwd
, G_DIR_SEPARATOR
, G_DIR_SEPARATOR
, G_DIR_SEPARATOR
);
933 fixture_setup(&fixture
, NULL
, env
);
935 ret
= qmp_fd(fixture
.fd
, "{'execute': 'guest-get-osinfo'}");
936 g_assert_nonnull(ret
);
937 qmp_assert_no_error(ret
);
939 val
= qdict_get_qdict(ret
, "return");
941 str
= qdict_get_try_str(val
, "id");
942 g_assert_nonnull(str
);
943 g_assert_cmpstr(str
, ==, "qemu-ga-test");
945 str
= qdict_get_try_str(val
, "name");
946 g_assert_nonnull(str
);
947 g_assert_cmpstr(str
, ==, "QEMU-GA");
949 str
= qdict_get_try_str(val
, "pretty-name");
950 g_assert_nonnull(str
);
951 g_assert_cmpstr(str
, ==, "QEMU Guest Agent test");
953 str
= qdict_get_try_str(val
, "version");
954 g_assert_nonnull(str
);
955 g_assert_cmpstr(str
, ==, "Test 1");
957 str
= qdict_get_try_str(val
, "version-id");
958 g_assert_nonnull(str
);
959 g_assert_cmpstr(str
, ==, "1");
961 str
= qdict_get_try_str(val
, "variant");
962 g_assert_nonnull(str
);
963 g_assert_cmpstr(str
, ==, "Unit test \"'$`\\ and \\\\ etc.");
965 str
= qdict_get_try_str(val
, "variant-id");
966 g_assert_nonnull(str
);
967 g_assert_cmpstr(str
, ==, "unit-test");
971 fixture_tear_down(&fixture
, NULL
);
974 int main(int argc
, char **argv
)
979 setlocale (LC_ALL
, "");
980 g_test_init(&argc
, &argv
, NULL
);
981 fixture_setup(&fix
, NULL
, NULL
);
983 g_test_add_data_func("/qga/sync-delimited", &fix
, test_qga_sync_delimited
);
984 g_test_add_data_func("/qga/sync", &fix
, test_qga_sync
);
985 g_test_add_data_func("/qga/ping", &fix
, test_qga_ping
);
986 g_test_add_data_func("/qga/info", &fix
, test_qga_info
);
987 g_test_add_data_func("/qga/network-get-interfaces", &fix
,
988 test_qga_network_get_interfaces
);
989 if (!access("/sys/devices/system/cpu/cpu0", F_OK
)) {
990 g_test_add_data_func("/qga/get-vcpus", &fix
, test_qga_get_vcpus
);
992 g_test_add_data_func("/qga/get-fsinfo", &fix
, test_qga_get_fsinfo
);
993 g_test_add_data_func("/qga/get-memory-block-info", &fix
,
994 test_qga_get_memory_block_info
);
995 g_test_add_data_func("/qga/get-memory-blocks", &fix
,
996 test_qga_get_memory_blocks
);
997 g_test_add_data_func("/qga/file-ops", &fix
, test_qga_file_ops
);
998 g_test_add_data_func("/qga/file-write-read", &fix
, test_qga_file_write_read
);
999 g_test_add_data_func("/qga/get-time", &fix
, test_qga_get_time
);
1000 g_test_add_data_func("/qga/invalid-id", &fix
, test_qga_invalid_id
);
1001 g_test_add_data_func("/qga/invalid-oob", &fix
, test_qga_invalid_oob
);
1002 g_test_add_data_func("/qga/invalid-cmd", &fix
, test_qga_invalid_cmd
);
1003 g_test_add_data_func("/qga/invalid-args", &fix
, test_qga_invalid_args
);
1004 g_test_add_data_func("/qga/fsfreeze-status", &fix
,
1005 test_qga_fsfreeze_status
);
1007 g_test_add_data_func("/qga/blacklist", NULL
, test_qga_blacklist
);
1008 g_test_add_data_func("/qga/config", NULL
, test_qga_config
);
1009 g_test_add_data_func("/qga/guest-exec", &fix
, test_qga_guest_exec
);
1010 g_test_add_data_func("/qga/guest-exec-invalid", &fix
,
1011 test_qga_guest_exec_invalid
);
1012 g_test_add_data_func("/qga/guest-get-osinfo", &fix
,
1013 test_qga_guest_get_osinfo
);
1014 g_test_add_data_func("/qga/guest-get-host-name", &fix
,
1015 test_qga_guest_get_host_name
);
1016 g_test_add_data_func("/qga/guest-get-timezone", &fix
,
1017 test_qga_guest_get_timezone
);
1018 g_test_add_data_func("/qga/guest-get-users", &fix
,
1019 test_qga_guest_get_users
);
1023 fixture_tear_down(&fix
, NULL
);