3 #include <glib/gstdio.h>
9 #include <sys/socket.h>
15 #include "config-host.h"
24 static int connect_qga(char *path
)
26 int s
, ret
, len
, i
= 0;
27 struct sockaddr_un remote
;
29 s
= socket(AF_UNIX
, SOCK_STREAM
, 0);
32 remote
.sun_family
= AF_UNIX
;
34 strcpy(remote
.sun_path
, path
);
35 len
= strlen(remote
.sun_path
) + sizeof(remote
.sun_family
);
36 ret
= connect(s
, (struct sockaddr
*)&remote
, len
);
38 g_usleep(G_USEC_PER_SEC
);
48 static void qga_watch(GPid pid
, gint status
, gpointer user_data
)
50 TestFixture
*fixture
= user_data
;
52 g_assert_cmpint(status
, ==, 0);
53 g_main_loop_quit(fixture
->loop
);
57 fixture_setup(TestFixture
*fixture
, gconstpointer data
)
59 const gchar
*extra_arg
= data
;
61 gchar
*cwd
, *path
, *cmd
, **argv
= NULL
;
63 fixture
->loop
= g_main_loop_new(NULL
, FALSE
);
65 fixture
->test_dir
= g_strdup("/tmp/qgatest.XXXXXX");
66 g_assert_nonnull(mkdtemp(fixture
->test_dir
));
68 path
= g_build_filename(fixture
->test_dir
, "sock", NULL
);
69 cwd
= g_get_current_dir();
70 cmd
= g_strdup_printf("%s%cqemu-ga -m unix-listen -t %s -p %s %s %s",
72 fixture
->test_dir
, path
,
73 getenv("QTEST_LOG") ? "-v" : "",
75 g_shell_parse_argv(cmd
, NULL
, &argv
, &error
);
76 g_assert_no_error(error
);
78 g_spawn_async(fixture
->test_dir
, argv
, NULL
,
79 G_SPAWN_SEARCH_PATH
|G_SPAWN_DO_NOT_REAP_CHILD
,
80 NULL
, NULL
, &fixture
->pid
, &error
);
81 g_assert_no_error(error
);
83 g_child_watch_add(fixture
->pid
, qga_watch
, fixture
);
85 fixture
->fd
= connect_qga(path
);
86 g_assert_cmpint(fixture
->fd
, !=, -1);
95 fixture_tear_down(TestFixture
*fixture
, gconstpointer data
)
99 kill(fixture
->pid
, SIGTERM
);
101 g_main_loop_run(fixture
->loop
);
102 g_main_loop_unref(fixture
->loop
);
104 g_spawn_close_pid(fixture
->pid
);
106 tmp
= g_build_filename(fixture
->test_dir
, "foo", NULL
);
110 tmp
= g_build_filename(fixture
->test_dir
, "qga.state", NULL
);
114 tmp
= g_build_filename(fixture
->test_dir
, "sock", NULL
);
118 g_rmdir(fixture
->test_dir
);
119 g_free(fixture
->test_dir
);
122 static void qmp_assertion_message_error(const char *domain
,
129 const char *class, *desc
;
133 error
= qdict_get_qdict(dict
, "error");
134 class = qdict_get_try_str(error
, "class");
135 desc
= qdict_get_try_str(error
, "desc");
137 s
= g_strdup_printf("assertion failed %s: %s %s", expr
, class, desc
);
138 g_assertion_message(domain
, file
, line
, func
, s
);
142 #define qmp_assert_no_error(err) do { \
143 if (qdict_haskey(err, "error")) { \
144 qmp_assertion_message_error(G_LOG_DOMAIN, __FILE__, __LINE__, \
145 G_STRFUNC, #err, err); \
149 static void test_qga_sync_delimited(gconstpointer fix
)
151 const TestFixture
*fixture
= fix
;
152 guint32 v
, r
= g_random_int();
157 cmd
= g_strdup_printf("%c{'execute': 'guest-sync-delimited',"
158 " 'arguments': {'id': %u } }", 0xff, r
);
159 qmp_fd_send(fixture
->fd
, cmd
);
162 v
= read(fixture
->fd
, &c
, 1);
163 g_assert_cmpint(v
, ==, 1);
164 g_assert_cmpint(c
, ==, 0xff);
166 ret
= qmp_fd_receive(fixture
->fd
);
167 g_assert_nonnull(ret
);
168 qmp_assert_no_error(ret
);
170 v
= qdict_get_int(ret
, "return");
171 g_assert_cmpint(r
, ==, v
);
176 static void test_qga_sync(gconstpointer fix
)
178 const TestFixture
*fixture
= fix
;
179 guint32 v
, r
= g_random_int();
183 cmd
= g_strdup_printf("%c{'execute': 'guest-sync',"
184 " 'arguments': {'id': %u } }", 0xff, r
);
185 ret
= qmp_fd(fixture
->fd
, cmd
);
188 g_assert_nonnull(ret
);
189 qmp_assert_no_error(ret
);
191 v
= qdict_get_int(ret
, "return");
192 g_assert_cmpint(r
, ==, v
);
197 static void test_qga_ping(gconstpointer fix
)
199 const TestFixture
*fixture
= fix
;
202 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-ping'}");
203 g_assert_nonnull(ret
);
204 qmp_assert_no_error(ret
);
209 static void test_qga_invalid_cmd(gconstpointer fix
)
211 const TestFixture
*fixture
= fix
;
213 const gchar
*class, *desc
;
215 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-invalid-cmd'}");
216 g_assert_nonnull(ret
);
218 error
= qdict_get_qdict(ret
, "error");
219 class = qdict_get_try_str(error
, "class");
220 desc
= qdict_get_try_str(error
, "desc");
222 g_assert_cmpstr(class, ==, "CommandNotFound");
223 g_assert_cmpint(strlen(desc
), >, 0);
228 static void test_qga_info(gconstpointer fix
)
230 const TestFixture
*fixture
= fix
;
232 const gchar
*version
;
234 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-info'}");
235 g_assert_nonnull(ret
);
236 qmp_assert_no_error(ret
);
238 val
= qdict_get_qdict(ret
, "return");
239 version
= qdict_get_try_str(val
, "version");
240 g_assert_cmpstr(version
, ==, QEMU_VERSION
);
245 static void test_qga_get_vcpus(gconstpointer fix
)
247 const TestFixture
*fixture
= fix
;
250 const QListEntry
*entry
;
252 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-vcpus'}");
253 g_assert_nonnull(ret
);
254 qmp_assert_no_error(ret
);
256 /* check there is at least a cpu */
257 list
= qdict_get_qlist(ret
, "return");
258 entry
= qlist_first(list
);
259 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "online"));
260 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "logical-id"));
265 static void test_qga_get_fsinfo(gconstpointer fix
)
267 const TestFixture
*fixture
= fix
;
270 const QListEntry
*entry
;
272 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-fsinfo'}");
273 g_assert_nonnull(ret
);
274 qmp_assert_no_error(ret
);
276 /* sanity-check the response if there are any filesystems */
277 list
= qdict_get_qlist(ret
, "return");
278 entry
= qlist_first(list
);
280 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "name"));
281 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "mountpoint"));
282 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "type"));
283 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "disk"));
289 static void test_qga_get_memory_block_info(gconstpointer fix
)
291 const TestFixture
*fixture
= fix
;
295 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-memory-block-info'}");
296 g_assert_nonnull(ret
);
298 /* some systems might not expose memory block info in sysfs */
299 if (!qdict_haskey(ret
, "error")) {
300 /* check there is at least some memory */
301 val
= qdict_get_qdict(ret
, "return");
302 size
= qdict_get_int(val
, "size");
303 g_assert_cmpint(size
, >, 0);
309 static void test_qga_get_memory_blocks(gconstpointer fix
)
311 const TestFixture
*fixture
= fix
;
314 const QListEntry
*entry
;
316 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-memory-blocks'}");
317 g_assert_nonnull(ret
);
319 /* some systems might not expose memory block info in sysfs */
320 if (!qdict_haskey(ret
, "error")) {
321 list
= qdict_get_qlist(ret
, "return");
322 entry
= qlist_first(list
);
323 /* newer versions of qga may return empty list without error */
325 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "phys-index"));
326 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "online"));
333 static void test_qga_network_get_interfaces(gconstpointer fix
)
335 const TestFixture
*fixture
= fix
;
338 const QListEntry
*entry
;
340 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-network-get-interfaces'}");
341 g_assert_nonnull(ret
);
342 qmp_assert_no_error(ret
);
344 /* check there is at least an interface */
345 list
= qdict_get_qlist(ret
, "return");
346 entry
= qlist_first(list
);
347 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "name"));
352 static void test_qga_file_ops(gconstpointer fix
)
354 const TestFixture
*fixture
= fix
;
355 const guchar helloworld
[] = "Hello World!\n";
357 gchar
*cmd
, *path
, *enc
;
366 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
367 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
368 g_assert_nonnull(ret
);
369 qmp_assert_no_error(ret
);
370 id
= qdict_get_int(ret
, "return");
373 enc
= g_base64_encode(helloworld
, sizeof(helloworld
));
375 cmd
= g_strdup_printf("{'execute': 'guest-file-write',"
376 " 'arguments': { 'handle': %" PRId64
","
377 " 'buf-b64': '%s' } }", id
, enc
);
378 ret
= qmp_fd(fixture
->fd
, cmd
);
379 g_assert_nonnull(ret
);
380 qmp_assert_no_error(ret
);
382 val
= qdict_get_qdict(ret
, "return");
383 count
= qdict_get_int(val
, "count");
384 eof
= qdict_get_bool(val
, "eof");
385 g_assert_cmpint(count
, ==, sizeof(helloworld
));
386 g_assert_cmpint(eof
, ==, 0);
391 cmd
= g_strdup_printf("{'execute': 'guest-file-flush',"
392 " 'arguments': {'handle': %" PRId64
"} }",
394 ret
= qmp_fd(fixture
->fd
, cmd
);
399 cmd
= g_strdup_printf("{'execute': 'guest-file-close',"
400 " 'arguments': {'handle': %" PRId64
"} }",
402 ret
= qmp_fd(fixture
->fd
, cmd
);
407 path
= g_build_filename(fixture
->test_dir
, "foo", NULL
);
408 f
= fopen(path
, "r");
410 count
= fread(tmp
, 1, sizeof(tmp
), f
);
411 g_assert_cmpint(count
, ==, sizeof(helloworld
));
413 g_assert_cmpstr(tmp
, ==, (char *)helloworld
);
417 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
418 " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
419 g_assert_nonnull(ret
);
420 qmp_assert_no_error(ret
);
421 id
= qdict_get_int(ret
, "return");
425 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
426 " 'arguments': { 'handle': %" PRId64
"} }",
428 ret
= qmp_fd(fixture
->fd
, cmd
);
429 val
= qdict_get_qdict(ret
, "return");
430 count
= qdict_get_int(val
, "count");
431 eof
= qdict_get_bool(val
, "eof");
432 b64
= qdict_get_str(val
, "buf-b64");
433 g_assert_cmpint(count
, ==, sizeof(helloworld
));
435 g_assert_cmpstr(b64
, ==, enc
);
442 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
443 " 'arguments': { 'handle': %" PRId64
"} }",
445 ret
= qmp_fd(fixture
->fd
, cmd
);
446 val
= qdict_get_qdict(ret
, "return");
447 count
= qdict_get_int(val
, "count");
448 eof
= qdict_get_bool(val
, "eof");
449 b64
= qdict_get_str(val
, "buf-b64");
450 g_assert_cmpint(count
, ==, 0);
452 g_assert_cmpstr(b64
, ==, "");
457 cmd
= g_strdup_printf("{'execute': 'guest-file-seek',"
458 " 'arguments': { 'handle': %" PRId64
", "
459 " 'offset': %d, 'whence': %d } }",
461 ret
= qmp_fd(fixture
->fd
, cmd
);
462 qmp_assert_no_error(ret
);
463 val
= qdict_get_qdict(ret
, "return");
464 count
= qdict_get_int(val
, "position");
465 eof
= qdict_get_bool(val
, "eof");
466 g_assert_cmpint(count
, ==, 6);
472 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
473 " 'arguments': { 'handle': %" PRId64
"} }",
475 ret
= qmp_fd(fixture
->fd
, cmd
);
476 val
= qdict_get_qdict(ret
, "return");
477 count
= qdict_get_int(val
, "count");
478 eof
= qdict_get_bool(val
, "eof");
479 b64
= qdict_get_str(val
, "buf-b64");
480 g_assert_cmpint(count
, ==, sizeof(helloworld
) - 6);
482 dec
= g_base64_decode(b64
, &count
);
483 g_assert_cmpint(count
, ==, sizeof(helloworld
) - 6);
484 g_assert_cmpmem(dec
, count
, helloworld
+ 6, sizeof(helloworld
) - 6);
491 cmd
= g_strdup_printf("{'execute': 'guest-file-close',"
492 " 'arguments': {'handle': %" PRId64
"} }",
494 ret
= qmp_fd(fixture
->fd
, cmd
);
499 static void test_qga_get_time(gconstpointer fix
)
501 const TestFixture
*fixture
= fix
;
505 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-time'}");
506 g_assert_nonnull(ret
);
507 qmp_assert_no_error(ret
);
509 time
= qdict_get_int(ret
, "return");
510 g_assert_cmpint(time
, >, 0);
515 static void test_qga_set_time(gconstpointer fix
)
517 const TestFixture
*fixture
= fix
;
519 int64_t current
, time
;
522 /* get current time */
523 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-time'}");
524 g_assert_nonnull(ret
);
525 qmp_assert_no_error(ret
);
526 current
= qdict_get_int(ret
, "return");
527 g_assert_cmpint(current
, >, 0);
530 /* set some old time */
531 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-set-time',"
532 " 'arguments': { 'time': 1000 } }");
533 g_assert_nonnull(ret
);
534 qmp_assert_no_error(ret
);
538 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-time'}");
539 g_assert_nonnull(ret
);
540 qmp_assert_no_error(ret
);
541 time
= qdict_get_int(ret
, "return");
542 g_assert_cmpint(time
/ 1000, <, G_USEC_PER_SEC
* 10);
545 /* set back current time */
546 cmd
= g_strdup_printf("{'execute': 'guest-set-time',"
547 " 'arguments': { 'time': %" PRId64
" } }",
548 current
+ time
* 1000);
549 ret
= qmp_fd(fixture
->fd
, cmd
);
551 g_assert_nonnull(ret
);
552 qmp_assert_no_error(ret
);
556 static void test_qga_fstrim(gconstpointer fix
)
558 const TestFixture
*fixture
= fix
;
561 const QListEntry
*entry
;
563 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fstrim',"
564 " arguments: { minimum: 4194304 } }");
565 g_assert_nonnull(ret
);
566 qmp_assert_no_error(ret
);
567 list
= qdict_get_qlist(ret
, "return");
568 entry
= qlist_first(list
);
569 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "paths"));
574 static void test_qga_blacklist(gconstpointer data
)
578 const gchar
*class, *desc
;
580 fixture_setup(&fix
, "-b guest-ping,guest-get-time");
582 /* check blacklist */
583 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-ping'}");
584 g_assert_nonnull(ret
);
585 error
= qdict_get_qdict(ret
, "error");
586 class = qdict_get_try_str(error
, "class");
587 desc
= qdict_get_try_str(error
, "desc");
588 g_assert_cmpstr(class, ==, "GenericError");
589 g_assert_nonnull(g_strstr_len(desc
, -1, "has been disabled"));
592 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-get-time'}");
593 g_assert_nonnull(ret
);
594 error
= qdict_get_qdict(ret
, "error");
595 class = qdict_get_try_str(error
, "class");
596 desc
= qdict_get_try_str(error
, "desc");
597 g_assert_cmpstr(class, ==, "GenericError");
598 g_assert_nonnull(g_strstr_len(desc
, -1, "has been disabled"));
601 /* check something work */
602 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-get-fsinfo'}");
603 qmp_assert_no_error(ret
);
606 fixture_tear_down(&fix
, NULL
);
609 static void test_qga_config(gconstpointer data
)
611 GError
*error
= NULL
;
612 char *cwd
, *cmd
, *out
, *err
, *str
, **strv
, *conf
, **argv
= NULL
;
617 const char *qga_config
=
620 "method=virtio-serial\n"
621 "path=/path/to/org.qemu.guest_agent.0\n"
622 "pidfile=/var/foo/qemu-ga.pid\n"
623 "statedir=/var/state\n"
625 "blacklist=guest-ping;guest-get-time\n";
627 tmp
= g_file_open_tmp(NULL
, &conf
, &error
);
628 g_assert_no_error(error
);
629 g_assert_cmpint(tmp
, >=, 0);
630 g_assert_cmpstr(conf
, !=, "");
632 g_file_set_contents(conf
, qga_config
, -1, &error
);
633 g_assert_no_error(error
);
635 cwd
= g_get_current_dir();
636 cmd
= g_strdup_printf("%s%cqemu-ga -D",
637 cwd
, G_DIR_SEPARATOR
);
638 g_shell_parse_argv(cmd
, NULL
, &argv
, &error
);
639 g_assert_no_error(error
);
641 env
[0] = g_strdup_printf("QGA_CONF=%s", conf
);
643 g_spawn_sync(NULL
, argv
, env
, 0,
644 NULL
, NULL
, &out
, &err
, &status
, &error
);
645 g_assert_no_error(error
);
646 g_assert_cmpstr(err
, ==, "");
647 g_assert_cmpint(status
, ==, 0);
649 kf
= g_key_file_new();
650 g_key_file_load_from_data(kf
, out
, -1, G_KEY_FILE_NONE
, &error
);
651 g_assert_no_error(error
);
653 str
= g_key_file_get_start_group(kf
);
654 g_assert_cmpstr(str
, ==, "general");
657 g_assert_false(g_key_file_get_boolean(kf
, "general", "daemon", &error
));
658 g_assert_no_error(error
);
660 str
= g_key_file_get_string(kf
, "general", "method", &error
);
661 g_assert_no_error(error
);
662 g_assert_cmpstr(str
, ==, "virtio-serial");
665 str
= g_key_file_get_string(kf
, "general", "path", &error
);
666 g_assert_no_error(error
);
667 g_assert_cmpstr(str
, ==, "/path/to/org.qemu.guest_agent.0");
670 str
= g_key_file_get_string(kf
, "general", "pidfile", &error
);
671 g_assert_no_error(error
);
672 g_assert_cmpstr(str
, ==, "/var/foo/qemu-ga.pid");
675 str
= g_key_file_get_string(kf
, "general", "statedir", &error
);
676 g_assert_no_error(error
);
677 g_assert_cmpstr(str
, ==, "/var/state");
680 g_assert_true(g_key_file_get_boolean(kf
, "general", "verbose", &error
));
681 g_assert_no_error(error
);
683 strv
= g_key_file_get_string_list(kf
, "general", "blacklist", &n
, &error
);
684 g_assert_cmpint(n
, ==, 2);
685 #if GLIB_CHECK_VERSION(2, 44, 0)
686 g_assert_true(g_strv_contains((const char * const *)strv
,
688 g_assert_true(g_strv_contains((const char * const *)strv
,
691 g_assert_no_error(error
);
703 static void test_qga_fsfreeze_status(gconstpointer fix
)
705 const TestFixture
*fixture
= fix
;
709 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fsfreeze-status'}");
710 g_assert_nonnull(ret
);
711 qmp_assert_no_error(ret
);
713 status
= qdict_get_try_str(ret
, "return");
714 g_assert_cmpstr(status
, ==, "thawed");
719 static void test_qga_fsfreeze_and_thaw(gconstpointer fix
)
721 const TestFixture
*fixture
= fix
;
725 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fsfreeze-freeze'}");
726 g_assert_nonnull(ret
);
727 qmp_assert_no_error(ret
);
730 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fsfreeze-status'}");
731 g_assert_nonnull(ret
);
732 qmp_assert_no_error(ret
);
733 status
= qdict_get_try_str(ret
, "return");
734 g_assert_cmpstr(status
, ==, "frozen");
737 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fsfreeze-thaw'}");
738 g_assert_nonnull(ret
);
739 qmp_assert_no_error(ret
);
743 int main(int argc
, char **argv
)
748 setlocale (LC_ALL
, "");
749 g_test_init(&argc
, &argv
, NULL
);
750 fixture_setup(&fix
, NULL
);
752 g_test_add_data_func("/qga/sync-delimited", &fix
, test_qga_sync_delimited
);
753 g_test_add_data_func("/qga/sync", &fix
, test_qga_sync
);
754 g_test_add_data_func("/qga/ping", &fix
, test_qga_ping
);
755 g_test_add_data_func("/qga/info", &fix
, test_qga_info
);
756 g_test_add_data_func("/qga/network-get-interfaces", &fix
,
757 test_qga_network_get_interfaces
);
758 g_test_add_data_func("/qga/get-vcpus", &fix
, test_qga_get_vcpus
);
759 g_test_add_data_func("/qga/get-fsinfo", &fix
, test_qga_get_fsinfo
);
760 g_test_add_data_func("/qga/get-memory-block-info", &fix
,
761 test_qga_get_memory_block_info
);
762 g_test_add_data_func("/qga/get-memory-blocks", &fix
,
763 test_qga_get_memory_blocks
);
764 g_test_add_data_func("/qga/file-ops", &fix
, test_qga_file_ops
);
765 g_test_add_data_func("/qga/get-time", &fix
, test_qga_get_time
);
766 g_test_add_data_func("/qga/invalid-cmd", &fix
, test_qga_invalid_cmd
);
767 g_test_add_data_func("/qga/fsfreeze-status", &fix
,
768 test_qga_fsfreeze_status
);
770 g_test_add_data_func("/qga/blacklist", NULL
, test_qga_blacklist
);
771 g_test_add_data_func("/qga/config", NULL
, test_qga_config
);
773 if (g_getenv("QGA_TEST_SIDE_EFFECTING")) {
774 g_test_add_data_func("/qga/fsfreeze-and-thaw", &fix
,
775 test_qga_fsfreeze_and_thaw
);
776 g_test_add_data_func("/qga/set-time", &fix
, test_qga_set_time
);
777 g_test_add_data_func("/qga/fstrim", &fix
, test_qga_fstrim
);
782 fixture_tear_down(&fix
, NULL
);