1 #include "qemu/osdep.h"
3 #include <glib/gstdio.h>
4 #include <sys/socket.h>
16 static int connect_qga(char *path
)
18 int s
, ret
, len
, i
= 0;
19 struct sockaddr_un remote
;
21 s
= socket(AF_UNIX
, SOCK_STREAM
, 0);
24 remote
.sun_family
= AF_UNIX
;
26 strcpy(remote
.sun_path
, path
);
27 len
= strlen(remote
.sun_path
) + sizeof(remote
.sun_family
);
28 ret
= connect(s
, (struct sockaddr
*)&remote
, len
);
30 g_usleep(G_USEC_PER_SEC
);
40 static void qga_watch(GPid pid
, gint status
, gpointer user_data
)
42 TestFixture
*fixture
= user_data
;
44 g_assert_cmpint(status
, ==, 0);
45 g_main_loop_quit(fixture
->loop
);
49 fixture_setup(TestFixture
*fixture
, gconstpointer data
, gchar
**envp
)
51 const gchar
*extra_arg
= data
;
53 gchar
*cwd
, *path
, *cmd
, **argv
= NULL
;
55 fixture
->loop
= g_main_loop_new(NULL
, FALSE
);
57 fixture
->test_dir
= g_strdup("/tmp/qgatest.XXXXXX");
58 g_assert_nonnull(mkdtemp(fixture
->test_dir
));
60 path
= g_build_filename(fixture
->test_dir
, "sock", NULL
);
61 cwd
= g_get_current_dir();
62 cmd
= g_strdup_printf("%s%cqemu-ga -m unix-listen -t %s -p %s %s %s",
64 fixture
->test_dir
, path
,
65 getenv("QTEST_LOG") ? "-v" : "",
67 g_shell_parse_argv(cmd
, NULL
, &argv
, &error
);
68 g_assert_no_error(error
);
70 g_spawn_async(fixture
->test_dir
, argv
, envp
,
71 G_SPAWN_SEARCH_PATH
|G_SPAWN_DO_NOT_REAP_CHILD
,
72 NULL
, NULL
, &fixture
->pid
, &error
);
73 g_assert_no_error(error
);
75 g_child_watch_add(fixture
->pid
, qga_watch
, fixture
);
77 fixture
->fd
= connect_qga(path
);
78 g_assert_cmpint(fixture
->fd
, !=, -1);
87 fixture_tear_down(TestFixture
*fixture
, gconstpointer data
)
91 kill(fixture
->pid
, SIGTERM
);
93 g_main_loop_run(fixture
->loop
);
94 g_main_loop_unref(fixture
->loop
);
96 g_spawn_close_pid(fixture
->pid
);
98 tmp
= g_build_filename(fixture
->test_dir
, "foo", NULL
);
102 tmp
= g_build_filename(fixture
->test_dir
, "qga.state", NULL
);
106 tmp
= g_build_filename(fixture
->test_dir
, "sock", NULL
);
110 g_rmdir(fixture
->test_dir
);
111 g_free(fixture
->test_dir
);
114 static void qmp_assertion_message_error(const char *domain
,
121 const char *class, *desc
;
125 error
= qdict_get_qdict(dict
, "error");
126 class = qdict_get_try_str(error
, "class");
127 desc
= qdict_get_try_str(error
, "desc");
129 s
= g_strdup_printf("assertion failed %s: %s %s", expr
, class, desc
);
130 g_assertion_message(domain
, file
, line
, func
, s
);
134 #define qmp_assert_no_error(err) do { \
135 if (qdict_haskey(err, "error")) { \
136 qmp_assertion_message_error(G_LOG_DOMAIN, __FILE__, __LINE__, \
137 G_STRFUNC, #err, err); \
141 static void test_qga_sync_delimited(gconstpointer fix
)
143 const TestFixture
*fixture
= fix
;
144 guint32 v
, r
= g_random_int();
149 cmd
= g_strdup_printf("\xff{'execute': 'guest-sync-delimited',"
150 " 'arguments': {'id': %u } }", r
);
151 qmp_fd_send(fixture
->fd
, cmd
);
155 * Read and ignore garbage until resynchronized.
157 * Note that the full reset sequence would involve checking the
158 * response of guest-sync-delimited and repeating the loop if
159 * 'id' field of the response does not match the 'id' field of
160 * the request. Testing this fully would require inserting
161 * garbage in the response stream and is left as a future test
164 * TODO: The server shouldn't emit so much garbage (among other
165 * things, it loudly complains about the client's \xff being
166 * invalid JSON, even though it is a documented part of the
170 v
= read(fixture
->fd
, &c
, 1);
171 g_assert_cmpint(v
, ==, 1);
174 ret
= qmp_fd_receive(fixture
->fd
);
175 g_assert_nonnull(ret
);
176 qmp_assert_no_error(ret
);
178 v
= qdict_get_int(ret
, "return");
179 g_assert_cmpint(r
, ==, v
);
184 static void test_qga_sync(gconstpointer fix
)
186 const TestFixture
*fixture
= fix
;
187 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 cmd
= g_strdup_printf("{'execute': 'guest-sync',"
203 " 'arguments': {'id': %u } }", r
);
204 ret
= qmp_fd(fixture
->fd
, cmd
);
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_args(gconstpointer fix
)
230 const TestFixture
*fixture
= fix
;
232 const gchar
*class, *desc
;
234 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-ping', "
235 "'arguments': {'foo': 42 }}");
236 g_assert_nonnull(ret
);
238 error
= qdict_get_qdict(ret
, "error");
239 class = qdict_get_try_str(error
, "class");
240 desc
= qdict_get_try_str(error
, "desc");
242 g_assert_cmpstr(class, ==, "GenericError");
243 g_assert_cmpstr(desc
, ==, "Parameter 'foo' is unexpected");
248 static void test_qga_invalid_cmd(gconstpointer fix
)
250 const TestFixture
*fixture
= fix
;
252 const gchar
*class, *desc
;
254 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-invalid-cmd'}");
255 g_assert_nonnull(ret
);
257 error
= qdict_get_qdict(ret
, "error");
258 class = qdict_get_try_str(error
, "class");
259 desc
= qdict_get_try_str(error
, "desc");
261 g_assert_cmpstr(class, ==, "CommandNotFound");
262 g_assert_cmpint(strlen(desc
), >, 0);
267 static void test_qga_info(gconstpointer fix
)
269 const TestFixture
*fixture
= fix
;
271 const gchar
*version
;
273 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-info'}");
274 g_assert_nonnull(ret
);
275 qmp_assert_no_error(ret
);
277 val
= qdict_get_qdict(ret
, "return");
278 version
= qdict_get_try_str(val
, "version");
279 g_assert_cmpstr(version
, ==, QEMU_VERSION
);
284 static void test_qga_get_vcpus(gconstpointer fix
)
286 const TestFixture
*fixture
= fix
;
289 const QListEntry
*entry
;
291 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-vcpus'}");
292 g_assert_nonnull(ret
);
293 qmp_assert_no_error(ret
);
295 /* check there is at least a cpu */
296 list
= qdict_get_qlist(ret
, "return");
297 entry
= qlist_first(list
);
298 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "online"));
299 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "logical-id"));
304 static void test_qga_get_fsinfo(gconstpointer fix
)
306 const TestFixture
*fixture
= fix
;
309 const QListEntry
*entry
;
311 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-fsinfo'}");
312 g_assert_nonnull(ret
);
313 qmp_assert_no_error(ret
);
315 /* sanity-check the response if there are any filesystems */
316 list
= qdict_get_qlist(ret
, "return");
317 entry
= qlist_first(list
);
319 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "name"));
320 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "mountpoint"));
321 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "type"));
322 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "disk"));
328 static void test_qga_get_memory_block_info(gconstpointer fix
)
330 const TestFixture
*fixture
= fix
;
334 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-memory-block-info'}");
335 g_assert_nonnull(ret
);
337 /* some systems might not expose memory block info in sysfs */
338 if (!qdict_haskey(ret
, "error")) {
339 /* check there is at least some memory */
340 val
= qdict_get_qdict(ret
, "return");
341 size
= qdict_get_int(val
, "size");
342 g_assert_cmpint(size
, >, 0);
348 static void test_qga_get_memory_blocks(gconstpointer fix
)
350 const TestFixture
*fixture
= fix
;
353 const QListEntry
*entry
;
355 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-memory-blocks'}");
356 g_assert_nonnull(ret
);
358 /* some systems might not expose memory block info in sysfs */
359 if (!qdict_haskey(ret
, "error")) {
360 list
= qdict_get_qlist(ret
, "return");
361 entry
= qlist_first(list
);
362 /* newer versions of qga may return empty list without error */
364 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "phys-index"));
365 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "online"));
372 static void test_qga_network_get_interfaces(gconstpointer fix
)
374 const TestFixture
*fixture
= fix
;
377 const QListEntry
*entry
;
379 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-network-get-interfaces'}");
380 g_assert_nonnull(ret
);
381 qmp_assert_no_error(ret
);
383 /* check there is at least an interface */
384 list
= qdict_get_qlist(ret
, "return");
385 entry
= qlist_first(list
);
386 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "name"));
391 static void test_qga_file_ops(gconstpointer fix
)
393 const TestFixture
*fixture
= fix
;
394 const unsigned char helloworld
[] = "Hello World!\n";
396 gchar
*cmd
, *path
, *enc
;
405 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
406 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
407 g_assert_nonnull(ret
);
408 qmp_assert_no_error(ret
);
409 id
= qdict_get_int(ret
, "return");
412 enc
= g_base64_encode(helloworld
, sizeof(helloworld
));
414 cmd
= g_strdup_printf("{'execute': 'guest-file-write',"
415 " 'arguments': { 'handle': %" PRId64
","
416 " 'buf-b64': '%s' } }", id
, enc
);
417 ret
= qmp_fd(fixture
->fd
, cmd
);
418 g_assert_nonnull(ret
);
419 qmp_assert_no_error(ret
);
421 val
= qdict_get_qdict(ret
, "return");
422 count
= qdict_get_int(val
, "count");
423 eof
= qdict_get_bool(val
, "eof");
424 g_assert_cmpint(count
, ==, sizeof(helloworld
));
425 g_assert_cmpint(eof
, ==, 0);
430 cmd
= g_strdup_printf("{'execute': 'guest-file-flush',"
431 " 'arguments': {'handle': %" PRId64
"} }",
433 ret
= qmp_fd(fixture
->fd
, cmd
);
438 cmd
= g_strdup_printf("{'execute': 'guest-file-close',"
439 " 'arguments': {'handle': %" PRId64
"} }",
441 ret
= qmp_fd(fixture
->fd
, cmd
);
446 path
= g_build_filename(fixture
->test_dir
, "foo", NULL
);
447 f
= fopen(path
, "r");
450 count
= fread(tmp
, 1, sizeof(tmp
), f
);
451 g_assert_cmpint(count
, ==, sizeof(helloworld
));
453 g_assert_cmpstr(tmp
, ==, (char *)helloworld
);
457 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
458 " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
459 g_assert_nonnull(ret
);
460 qmp_assert_no_error(ret
);
461 id
= qdict_get_int(ret
, "return");
465 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
466 " 'arguments': { 'handle': %" PRId64
"} }",
468 ret
= qmp_fd(fixture
->fd
, cmd
);
469 val
= qdict_get_qdict(ret
, "return");
470 count
= qdict_get_int(val
, "count");
471 eof
= qdict_get_bool(val
, "eof");
472 b64
= qdict_get_str(val
, "buf-b64");
473 g_assert_cmpint(count
, ==, sizeof(helloworld
));
475 g_assert_cmpstr(b64
, ==, enc
);
482 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
483 " 'arguments': { 'handle': %" PRId64
"} }",
485 ret
= qmp_fd(fixture
->fd
, cmd
);
486 val
= qdict_get_qdict(ret
, "return");
487 count
= qdict_get_int(val
, "count");
488 eof
= qdict_get_bool(val
, "eof");
489 b64
= qdict_get_str(val
, "buf-b64");
490 g_assert_cmpint(count
, ==, 0);
492 g_assert_cmpstr(b64
, ==, "");
497 cmd
= g_strdup_printf("{'execute': 'guest-file-seek',"
498 " 'arguments': { 'handle': %" PRId64
", "
499 " 'offset': %d, 'whence': '%s' } }",
501 ret
= qmp_fd(fixture
->fd
, cmd
);
502 qmp_assert_no_error(ret
);
503 val
= qdict_get_qdict(ret
, "return");
504 count
= qdict_get_int(val
, "position");
505 eof
= qdict_get_bool(val
, "eof");
506 g_assert_cmpint(count
, ==, 6);
512 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
513 " 'arguments': { 'handle': %" PRId64
"} }",
515 ret
= qmp_fd(fixture
->fd
, cmd
);
516 val
= qdict_get_qdict(ret
, "return");
517 count
= qdict_get_int(val
, "count");
518 eof
= qdict_get_bool(val
, "eof");
519 b64
= qdict_get_str(val
, "buf-b64");
520 g_assert_cmpint(count
, ==, sizeof(helloworld
) - 6);
522 dec
= g_base64_decode(b64
, &count
);
523 g_assert_cmpint(count
, ==, sizeof(helloworld
) - 6);
524 g_assert_cmpmem(dec
, count
, helloworld
+ 6, sizeof(helloworld
) - 6);
531 cmd
= g_strdup_printf("{'execute': 'guest-file-close',"
532 " 'arguments': {'handle': %" PRId64
"} }",
534 ret
= qmp_fd(fixture
->fd
, cmd
);
539 static void test_qga_file_write_read(gconstpointer fix
)
541 const TestFixture
*fixture
= fix
;
542 const unsigned char helloworld
[] = "Hello World!\n";
550 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
551 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
552 g_assert_nonnull(ret
);
553 qmp_assert_no_error(ret
);
554 id
= qdict_get_int(ret
, "return");
557 enc
= g_base64_encode(helloworld
, sizeof(helloworld
));
559 cmd
= g_strdup_printf("{'execute': 'guest-file-write',"
560 " 'arguments': { 'handle': %" PRId64
","
561 " 'buf-b64': '%s' } }", id
, enc
);
562 ret
= qmp_fd(fixture
->fd
, cmd
);
563 g_assert_nonnull(ret
);
564 qmp_assert_no_error(ret
);
566 val
= qdict_get_qdict(ret
, "return");
567 count
= qdict_get_int(val
, "count");
568 eof
= qdict_get_bool(val
, "eof");
569 g_assert_cmpint(count
, ==, sizeof(helloworld
));
570 g_assert_cmpint(eof
, ==, 0);
574 /* read (check implicit flush) */
575 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
576 " 'arguments': { 'handle': %" PRId64
"} }",
578 ret
= qmp_fd(fixture
->fd
, cmd
);
579 val
= qdict_get_qdict(ret
, "return");
580 count
= qdict_get_int(val
, "count");
581 eof
= qdict_get_bool(val
, "eof");
582 b64
= qdict_get_str(val
, "buf-b64");
583 g_assert_cmpint(count
, ==, 0);
585 g_assert_cmpstr(b64
, ==, "");
590 cmd
= g_strdup_printf("{'execute': 'guest-file-seek',"
591 " 'arguments': { 'handle': %" PRId64
", "
592 " 'offset': %d, 'whence': '%s' } }",
594 ret
= qmp_fd(fixture
->fd
, cmd
);
595 qmp_assert_no_error(ret
);
596 val
= qdict_get_qdict(ret
, "return");
597 count
= qdict_get_int(val
, "position");
598 eof
= qdict_get_bool(val
, "eof");
599 g_assert_cmpint(count
, ==, 0);
605 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
606 " 'arguments': { 'handle': %" PRId64
"} }",
608 ret
= qmp_fd(fixture
->fd
, cmd
);
609 val
= qdict_get_qdict(ret
, "return");
610 count
= qdict_get_int(val
, "count");
611 eof
= qdict_get_bool(val
, "eof");
612 b64
= qdict_get_str(val
, "buf-b64");
613 g_assert_cmpint(count
, ==, sizeof(helloworld
));
615 g_assert_cmpstr(b64
, ==, enc
);
621 cmd
= g_strdup_printf("{'execute': 'guest-file-close',"
622 " 'arguments': {'handle': %" PRId64
"} }",
624 ret
= qmp_fd(fixture
->fd
, cmd
);
629 static void test_qga_get_time(gconstpointer fix
)
631 const TestFixture
*fixture
= fix
;
635 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-time'}");
636 g_assert_nonnull(ret
);
637 qmp_assert_no_error(ret
);
639 time
= qdict_get_int(ret
, "return");
640 g_assert_cmpint(time
, >, 0);
645 static void test_qga_blacklist(gconstpointer data
)
649 const gchar
*class, *desc
;
651 fixture_setup(&fix
, "-b guest-ping,guest-get-time", NULL
);
653 /* check blacklist */
654 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-ping'}");
655 g_assert_nonnull(ret
);
656 error
= qdict_get_qdict(ret
, "error");
657 class = qdict_get_try_str(error
, "class");
658 desc
= qdict_get_try_str(error
, "desc");
659 g_assert_cmpstr(class, ==, "GenericError");
660 g_assert_nonnull(g_strstr_len(desc
, -1, "has been disabled"));
663 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-get-time'}");
664 g_assert_nonnull(ret
);
665 error
= qdict_get_qdict(ret
, "error");
666 class = qdict_get_try_str(error
, "class");
667 desc
= qdict_get_try_str(error
, "desc");
668 g_assert_cmpstr(class, ==, "GenericError");
669 g_assert_nonnull(g_strstr_len(desc
, -1, "has been disabled"));
672 /* check something work */
673 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-get-fsinfo'}");
674 qmp_assert_no_error(ret
);
677 fixture_tear_down(&fix
, NULL
);
680 static void test_qga_config(gconstpointer data
)
682 GError
*error
= NULL
;
683 char *cwd
, *cmd
, *out
, *err
, *str
, **strv
, **argv
= NULL
;
689 cwd
= g_get_current_dir();
690 cmd
= g_strdup_printf("%s%cqemu-ga -D",
691 cwd
, G_DIR_SEPARATOR
);
693 g_shell_parse_argv(cmd
, NULL
, &argv
, &error
);
695 g_assert_no_error(error
);
697 env
[0] = g_strdup_printf("QGA_CONF=tests%cdata%ctest-qga-config",
698 G_DIR_SEPARATOR
, G_DIR_SEPARATOR
);
700 g_spawn_sync(NULL
, argv
, env
, 0,
701 NULL
, NULL
, &out
, &err
, &status
, &error
);
704 g_assert_no_error(error
);
705 g_assert_cmpstr(err
, ==, "");
706 g_assert_cmpint(status
, ==, 0);
708 kf
= g_key_file_new();
709 g_key_file_load_from_data(kf
, out
, -1, G_KEY_FILE_NONE
, &error
);
710 g_assert_no_error(error
);
712 str
= g_key_file_get_start_group(kf
);
713 g_assert_cmpstr(str
, ==, "general");
716 g_assert_false(g_key_file_get_boolean(kf
, "general", "daemon", &error
));
717 g_assert_no_error(error
);
719 str
= g_key_file_get_string(kf
, "general", "method", &error
);
720 g_assert_no_error(error
);
721 g_assert_cmpstr(str
, ==, "virtio-serial");
724 str
= g_key_file_get_string(kf
, "general", "path", &error
);
725 g_assert_no_error(error
);
726 g_assert_cmpstr(str
, ==, "/path/to/org.qemu.guest_agent.0");
729 str
= g_key_file_get_string(kf
, "general", "pidfile", &error
);
730 g_assert_no_error(error
);
731 g_assert_cmpstr(str
, ==, "/var/foo/qemu-ga.pid");
734 str
= g_key_file_get_string(kf
, "general", "statedir", &error
);
735 g_assert_no_error(error
);
736 g_assert_cmpstr(str
, ==, "/var/state");
739 g_assert_true(g_key_file_get_boolean(kf
, "general", "verbose", &error
));
740 g_assert_no_error(error
);
742 strv
= g_key_file_get_string_list(kf
, "general", "blacklist", &n
, &error
);
743 g_assert_cmpint(n
, ==, 2);
744 #if GLIB_CHECK_VERSION(2, 44, 0)
745 g_assert_true(g_strv_contains((const char * const *)strv
,
747 g_assert_true(g_strv_contains((const char * const *)strv
,
750 g_assert_no_error(error
);
759 static void test_qga_fsfreeze_status(gconstpointer fix
)
761 const TestFixture
*fixture
= fix
;
765 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fsfreeze-status'}");
766 g_assert_nonnull(ret
);
767 qmp_assert_no_error(ret
);
769 status
= qdict_get_try_str(ret
, "return");
770 g_assert_cmpstr(status
, ==, "thawed");
775 static void test_qga_guest_exec(gconstpointer fix
)
777 const TestFixture
*fixture
= fix
;
781 int64_t pid
, now
, exitcode
;
786 /* exec 'echo foo bar' */
787 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec', 'arguments': {"
788 " 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ],"
789 " 'capture-output': true } }");
790 g_assert_nonnull(ret
);
791 qmp_assert_no_error(ret
);
792 val
= qdict_get_qdict(ret
, "return");
793 pid
= qdict_get_int(val
, "pid");
794 g_assert_cmpint(pid
, >, 0);
797 /* wait for completion */
798 now
= g_get_monotonic_time();
799 cmd
= g_strdup_printf("{'execute': 'guest-exec-status',"
800 " 'arguments': { 'pid': %" PRId64
" } }", pid
);
802 ret
= qmp_fd(fixture
->fd
, cmd
);
803 g_assert_nonnull(ret
);
804 val
= qdict_get_qdict(ret
, "return");
805 exited
= qdict_get_bool(val
, "exited");
810 g_get_monotonic_time() < now
+ 5 * G_TIME_SPAN_SECOND
);
815 exitcode
= qdict_get_int(val
, "exitcode");
816 g_assert_cmpint(exitcode
, ==, 0);
817 out
= qdict_get_str(val
, "out-data");
818 decoded
= g_base64_decode(out
, &len
);
819 g_assert_cmpint(len
, ==, 12);
820 g_assert_cmpstr((char *)decoded
, ==, "\" test_str \"");
825 static void test_qga_guest_exec_invalid(gconstpointer fix
)
827 const TestFixture
*fixture
= fix
;
829 const gchar
*class, *desc
;
831 /* invalid command */
832 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec', 'arguments': {"
833 " 'path': '/bin/invalid-cmd42' } }");
834 g_assert_nonnull(ret
);
835 error
= qdict_get_qdict(ret
, "error");
836 g_assert_nonnull(error
);
837 class = qdict_get_str(error
, "class");
838 desc
= qdict_get_str(error
, "desc");
839 g_assert_cmpstr(class, ==, "GenericError");
840 g_assert_cmpint(strlen(desc
), >, 0);
844 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec-status',"
845 " 'arguments': { 'pid': 0 } }");
846 g_assert_nonnull(ret
);
847 error
= qdict_get_qdict(ret
, "error");
848 g_assert_nonnull(error
);
849 class = qdict_get_str(error
, "class");
850 desc
= qdict_get_str(error
, "desc");
851 g_assert_cmpstr(class, ==, "GenericError");
852 g_assert_cmpint(strlen(desc
), >, 0);
856 static void test_qga_guest_get_osinfo(gconstpointer data
)
863 cwd
= g_get_current_dir();
864 env
[0] = g_strdup_printf(
865 "QGA_OS_RELEASE=%s%ctests%cdata%ctest-qga-os-release",
866 cwd
, G_DIR_SEPARATOR
, G_DIR_SEPARATOR
, G_DIR_SEPARATOR
);
869 fixture_setup(&fixture
, NULL
, env
);
871 ret
= qmp_fd(fixture
.fd
, "{'execute': 'guest-get-osinfo'}");
872 g_assert_nonnull(ret
);
873 qmp_assert_no_error(ret
);
875 val
= qdict_get_qdict(ret
, "return");
877 str
= qdict_get_try_str(val
, "id");
878 g_assert_nonnull(str
);
879 g_assert_cmpstr(str
, ==, "qemu-ga-test");
881 str
= qdict_get_try_str(val
, "name");
882 g_assert_nonnull(str
);
883 g_assert_cmpstr(str
, ==, "QEMU-GA");
885 str
= qdict_get_try_str(val
, "pretty-name");
886 g_assert_nonnull(str
);
887 g_assert_cmpstr(str
, ==, "QEMU Guest Agent test");
889 str
= qdict_get_try_str(val
, "version");
890 g_assert_nonnull(str
);
891 g_assert_cmpstr(str
, ==, "Test 1");
893 str
= qdict_get_try_str(val
, "version-id");
894 g_assert_nonnull(str
);
895 g_assert_cmpstr(str
, ==, "1");
897 str
= qdict_get_try_str(val
, "variant");
898 g_assert_nonnull(str
);
899 g_assert_cmpstr(str
, ==, "Unit test \"'$`\\ and \\\\ etc.");
901 str
= qdict_get_try_str(val
, "variant-id");
902 g_assert_nonnull(str
);
903 g_assert_cmpstr(str
, ==, "unit-test");
907 fixture_tear_down(&fixture
, NULL
);
910 int main(int argc
, char **argv
)
915 setlocale (LC_ALL
, "");
916 g_test_init(&argc
, &argv
, NULL
);
917 fixture_setup(&fix
, NULL
, NULL
);
919 g_test_add_data_func("/qga/sync-delimited", &fix
, test_qga_sync_delimited
);
920 g_test_add_data_func("/qga/sync", &fix
, test_qga_sync
);
921 g_test_add_data_func("/qga/ping", &fix
, test_qga_ping
);
922 g_test_add_data_func("/qga/info", &fix
, test_qga_info
);
923 g_test_add_data_func("/qga/network-get-interfaces", &fix
,
924 test_qga_network_get_interfaces
);
925 if (!access("/sys/devices/system/cpu/cpu0", F_OK
)) {
926 g_test_add_data_func("/qga/get-vcpus", &fix
, test_qga_get_vcpus
);
928 g_test_add_data_func("/qga/get-fsinfo", &fix
, test_qga_get_fsinfo
);
929 g_test_add_data_func("/qga/get-memory-block-info", &fix
,
930 test_qga_get_memory_block_info
);
931 g_test_add_data_func("/qga/get-memory-blocks", &fix
,
932 test_qga_get_memory_blocks
);
933 g_test_add_data_func("/qga/file-ops", &fix
, test_qga_file_ops
);
934 g_test_add_data_func("/qga/file-write-read", &fix
, test_qga_file_write_read
);
935 g_test_add_data_func("/qga/get-time", &fix
, test_qga_get_time
);
936 g_test_add_data_func("/qga/invalid-cmd", &fix
, test_qga_invalid_cmd
);
937 g_test_add_data_func("/qga/invalid-args", &fix
, test_qga_invalid_args
);
938 g_test_add_data_func("/qga/fsfreeze-status", &fix
,
939 test_qga_fsfreeze_status
);
941 g_test_add_data_func("/qga/blacklist", NULL
, test_qga_blacklist
);
942 g_test_add_data_func("/qga/config", NULL
, test_qga_config
);
943 g_test_add_data_func("/qga/guest-exec", &fix
, test_qga_guest_exec
);
944 g_test_add_data_func("/qga/guest-exec-invalid", &fix
,
945 test_qga_guest_exec_invalid
);
946 g_test_add_data_func("/qga/guest-get-osinfo", &fix
,
947 test_qga_guest_get_osinfo
);
951 fixture_tear_down(&fix
, NULL
);