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 /* check there is at least a fs */
277 list
= qdict_get_qlist(ret
, "return");
278 entry
= qlist_first(list
);
279 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "name"));
280 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "mountpoint"));
281 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "type"));
282 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "disk"));
287 static void test_qga_get_memory_block_info(gconstpointer fix
)
289 const TestFixture
*fixture
= fix
;
293 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-memory-block-info'}");
294 g_assert_nonnull(ret
);
296 /* some systems might not expose memory block info in sysfs */
297 if (!qdict_haskey(ret
, "error")) {
298 /* check there is at least some memory */
299 val
= qdict_get_qdict(ret
, "return");
300 size
= qdict_get_int(val
, "size");
301 g_assert_cmpint(size
, >, 0);
307 static void test_qga_get_memory_blocks(gconstpointer fix
)
309 const TestFixture
*fixture
= fix
;
312 const QListEntry
*entry
;
314 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-memory-blocks'}");
315 g_assert_nonnull(ret
);
317 /* some systems might not expose memory block info in sysfs */
318 if (!qdict_haskey(ret
, "error")) {
319 list
= qdict_get_qlist(ret
, "return");
320 entry
= qlist_first(list
);
321 /* newer versions of qga may return empty list without error */
323 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "phys-index"));
324 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "online"));
331 static void test_qga_network_get_interfaces(gconstpointer fix
)
333 const TestFixture
*fixture
= fix
;
336 const QListEntry
*entry
;
338 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-network-get-interfaces'}");
339 g_assert_nonnull(ret
);
340 qmp_assert_no_error(ret
);
342 /* check there is at least an interface */
343 list
= qdict_get_qlist(ret
, "return");
344 entry
= qlist_first(list
);
345 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "name"));
350 static void test_qga_file_ops(gconstpointer fix
)
352 const TestFixture
*fixture
= fix
;
353 const guchar helloworld
[] = "Hello World!\n";
355 gchar
*cmd
, *path
, *enc
;
364 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
365 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
366 g_assert_nonnull(ret
);
367 qmp_assert_no_error(ret
);
368 id
= qdict_get_int(ret
, "return");
371 enc
= g_base64_encode(helloworld
, sizeof(helloworld
));
373 cmd
= g_strdup_printf("{'execute': 'guest-file-write',"
374 " 'arguments': { 'handle': %" PRId64
","
375 " 'buf-b64': '%s' } }", id
, enc
);
376 ret
= qmp_fd(fixture
->fd
, cmd
);
377 g_assert_nonnull(ret
);
378 qmp_assert_no_error(ret
);
380 val
= qdict_get_qdict(ret
, "return");
381 count
= qdict_get_int(val
, "count");
382 eof
= qdict_get_bool(val
, "eof");
383 g_assert_cmpint(count
, ==, sizeof(helloworld
));
384 g_assert_cmpint(eof
, ==, 0);
389 cmd
= g_strdup_printf("{'execute': 'guest-file-flush',"
390 " 'arguments': {'handle': %" PRId64
"} }",
392 ret
= qmp_fd(fixture
->fd
, cmd
);
397 cmd
= g_strdup_printf("{'execute': 'guest-file-close',"
398 " 'arguments': {'handle': %" PRId64
"} }",
400 ret
= qmp_fd(fixture
->fd
, cmd
);
405 path
= g_build_filename(fixture
->test_dir
, "foo", NULL
);
406 f
= fopen(path
, "r");
408 count
= fread(tmp
, 1, sizeof(tmp
), f
);
409 g_assert_cmpint(count
, ==, sizeof(helloworld
));
411 g_assert_cmpstr(tmp
, ==, (char *)helloworld
);
415 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
416 " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
417 g_assert_nonnull(ret
);
418 qmp_assert_no_error(ret
);
419 id
= qdict_get_int(ret
, "return");
423 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
424 " 'arguments': { 'handle': %" PRId64
"} }",
426 ret
= qmp_fd(fixture
->fd
, cmd
);
427 val
= qdict_get_qdict(ret
, "return");
428 count
= qdict_get_int(val
, "count");
429 eof
= qdict_get_bool(val
, "eof");
430 b64
= qdict_get_str(val
, "buf-b64");
431 g_assert_cmpint(count
, ==, sizeof(helloworld
));
433 g_assert_cmpstr(b64
, ==, enc
);
440 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
441 " 'arguments': { 'handle': %" PRId64
"} }",
443 ret
= qmp_fd(fixture
->fd
, cmd
);
444 val
= qdict_get_qdict(ret
, "return");
445 count
= qdict_get_int(val
, "count");
446 eof
= qdict_get_bool(val
, "eof");
447 b64
= qdict_get_str(val
, "buf-b64");
448 g_assert_cmpint(count
, ==, 0);
450 g_assert_cmpstr(b64
, ==, "");
455 cmd
= g_strdup_printf("{'execute': 'guest-file-seek',"
456 " 'arguments': { 'handle': %" PRId64
", "
457 " 'offset': %d, 'whence': %d } }",
459 ret
= qmp_fd(fixture
->fd
, cmd
);
460 qmp_assert_no_error(ret
);
461 val
= qdict_get_qdict(ret
, "return");
462 count
= qdict_get_int(val
, "position");
463 eof
= qdict_get_bool(val
, "eof");
464 g_assert_cmpint(count
, ==, 6);
470 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
471 " 'arguments': { 'handle': %" PRId64
"} }",
473 ret
= qmp_fd(fixture
->fd
, cmd
);
474 val
= qdict_get_qdict(ret
, "return");
475 count
= qdict_get_int(val
, "count");
476 eof
= qdict_get_bool(val
, "eof");
477 b64
= qdict_get_str(val
, "buf-b64");
478 g_assert_cmpint(count
, ==, sizeof(helloworld
) - 6);
480 dec
= g_base64_decode(b64
, &count
);
481 g_assert_cmpint(count
, ==, sizeof(helloworld
) - 6);
482 g_assert_cmpmem(dec
, count
, helloworld
+ 6, sizeof(helloworld
) - 6);
489 cmd
= g_strdup_printf("{'execute': 'guest-file-close',"
490 " 'arguments': {'handle': %" PRId64
"} }",
492 ret
= qmp_fd(fixture
->fd
, cmd
);
497 static void test_qga_get_time(gconstpointer fix
)
499 const TestFixture
*fixture
= fix
;
503 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-time'}");
504 g_assert_nonnull(ret
);
505 qmp_assert_no_error(ret
);
507 time
= qdict_get_int(ret
, "return");
508 g_assert_cmpint(time
, >, 0);
513 static void test_qga_set_time(gconstpointer fix
)
515 const TestFixture
*fixture
= fix
;
517 int64_t current
, time
;
520 /* get current time */
521 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-time'}");
522 g_assert_nonnull(ret
);
523 qmp_assert_no_error(ret
);
524 current
= qdict_get_int(ret
, "return");
525 g_assert_cmpint(current
, >, 0);
528 /* set some old time */
529 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-set-time',"
530 " 'arguments': { 'time': 1000 } }");
531 g_assert_nonnull(ret
);
532 qmp_assert_no_error(ret
);
536 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-time'}");
537 g_assert_nonnull(ret
);
538 qmp_assert_no_error(ret
);
539 time
= qdict_get_int(ret
, "return");
540 g_assert_cmpint(time
/ 1000, <, G_USEC_PER_SEC
* 10);
543 /* set back current time */
544 cmd
= g_strdup_printf("{'execute': 'guest-set-time',"
545 " 'arguments': { 'time': %" PRId64
" } }",
546 current
+ time
* 1000);
547 ret
= qmp_fd(fixture
->fd
, cmd
);
549 g_assert_nonnull(ret
);
550 qmp_assert_no_error(ret
);
554 static void test_qga_fstrim(gconstpointer fix
)
556 const TestFixture
*fixture
= fix
;
559 const QListEntry
*entry
;
561 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fstrim',"
562 " arguments: { minimum: 4194304 } }");
563 g_assert_nonnull(ret
);
564 qmp_assert_no_error(ret
);
565 list
= qdict_get_qlist(ret
, "return");
566 entry
= qlist_first(list
);
567 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "paths"));
572 static void test_qga_blacklist(gconstpointer data
)
576 const gchar
*class, *desc
;
578 fixture_setup(&fix
, "-b guest-ping,guest-get-time");
580 /* check blacklist */
581 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-ping'}");
582 g_assert_nonnull(ret
);
583 error
= qdict_get_qdict(ret
, "error");
584 class = qdict_get_try_str(error
, "class");
585 desc
= qdict_get_try_str(error
, "desc");
586 g_assert_cmpstr(class, ==, "GenericError");
587 g_assert_nonnull(g_strstr_len(desc
, -1, "has been disabled"));
590 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-get-time'}");
591 g_assert_nonnull(ret
);
592 error
= qdict_get_qdict(ret
, "error");
593 class = qdict_get_try_str(error
, "class");
594 desc
= qdict_get_try_str(error
, "desc");
595 g_assert_cmpstr(class, ==, "GenericError");
596 g_assert_nonnull(g_strstr_len(desc
, -1, "has been disabled"));
599 /* check something work */
600 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-get-fsinfo'}");
601 qmp_assert_no_error(ret
);
604 fixture_tear_down(&fix
, NULL
);
607 static void test_qga_config(gconstpointer data
)
609 GError
*error
= NULL
;
610 char *cwd
, *cmd
, *out
, *err
, *str
, **strv
, *conf
, **argv
= NULL
;
615 const char *qga_config
=
618 "method=virtio-serial\n"
619 "path=/path/to/org.qemu.guest_agent.0\n"
620 "pidfile=/var/foo/qemu-ga.pid\n"
621 "statedir=/var/state\n"
623 "blacklist=guest-ping;guest-get-time\n";
625 tmp
= g_file_open_tmp(NULL
, &conf
, &error
);
626 g_assert_no_error(error
);
627 g_assert_cmpint(tmp
, >=, 0);
628 g_assert_cmpstr(conf
, !=, "");
630 g_file_set_contents(conf
, qga_config
, -1, &error
);
631 g_assert_no_error(error
);
633 cwd
= g_get_current_dir();
634 cmd
= g_strdup_printf("%s%cqemu-ga -D",
635 cwd
, G_DIR_SEPARATOR
);
636 g_shell_parse_argv(cmd
, NULL
, &argv
, &error
);
637 g_assert_no_error(error
);
639 env
[0] = g_strdup_printf("QGA_CONF=%s", conf
);
641 g_spawn_sync(NULL
, argv
, env
, 0,
642 NULL
, NULL
, &out
, &err
, &status
, &error
);
643 g_assert_no_error(error
);
644 g_assert_cmpstr(err
, ==, "");
645 g_assert_cmpint(status
, ==, 0);
647 kf
= g_key_file_new();
648 g_key_file_load_from_data(kf
, out
, -1, G_KEY_FILE_NONE
, &error
);
649 g_assert_no_error(error
);
651 str
= g_key_file_get_start_group(kf
);
652 g_assert_cmpstr(str
, ==, "general");
655 g_assert_false(g_key_file_get_boolean(kf
, "general", "daemon", &error
));
656 g_assert_no_error(error
);
658 str
= g_key_file_get_string(kf
, "general", "method", &error
);
659 g_assert_no_error(error
);
660 g_assert_cmpstr(str
, ==, "virtio-serial");
663 str
= g_key_file_get_string(kf
, "general", "path", &error
);
664 g_assert_no_error(error
);
665 g_assert_cmpstr(str
, ==, "/path/to/org.qemu.guest_agent.0");
668 str
= g_key_file_get_string(kf
, "general", "pidfile", &error
);
669 g_assert_no_error(error
);
670 g_assert_cmpstr(str
, ==, "/var/foo/qemu-ga.pid");
673 str
= g_key_file_get_string(kf
, "general", "statedir", &error
);
674 g_assert_no_error(error
);
675 g_assert_cmpstr(str
, ==, "/var/state");
678 g_assert_true(g_key_file_get_boolean(kf
, "general", "verbose", &error
));
679 g_assert_no_error(error
);
681 strv
= g_key_file_get_string_list(kf
, "general", "blacklist", &n
, &error
);
682 g_assert_cmpint(n
, ==, 2);
683 #if GLIB_CHECK_VERSION(2, 44, 0)
684 g_assert_true(g_strv_contains((const char * const *)strv
,
686 g_assert_true(g_strv_contains((const char * const *)strv
,
689 g_assert_no_error(error
);
701 static void test_qga_fsfreeze_status(gconstpointer fix
)
703 const TestFixture
*fixture
= fix
;
707 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fsfreeze-status'}");
708 g_assert_nonnull(ret
);
709 qmp_assert_no_error(ret
);
711 status
= qdict_get_try_str(ret
, "return");
712 g_assert_cmpstr(status
, ==, "thawed");
717 static void test_qga_fsfreeze_and_thaw(gconstpointer fix
)
719 const TestFixture
*fixture
= fix
;
723 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fsfreeze-freeze'}");
724 g_assert_nonnull(ret
);
725 qmp_assert_no_error(ret
);
728 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fsfreeze-status'}");
729 g_assert_nonnull(ret
);
730 qmp_assert_no_error(ret
);
731 status
= qdict_get_try_str(ret
, "return");
732 g_assert_cmpstr(status
, ==, "frozen");
735 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fsfreeze-thaw'}");
736 g_assert_nonnull(ret
);
737 qmp_assert_no_error(ret
);
741 int main(int argc
, char **argv
)
746 setlocale (LC_ALL
, "");
747 g_test_init(&argc
, &argv
, NULL
);
748 fixture_setup(&fix
, NULL
);
750 g_test_add_data_func("/qga/sync-delimited", &fix
, test_qga_sync_delimited
);
751 g_test_add_data_func("/qga/sync", &fix
, test_qga_sync
);
752 g_test_add_data_func("/qga/ping", &fix
, test_qga_ping
);
753 g_test_add_data_func("/qga/info", &fix
, test_qga_info
);
754 g_test_add_data_func("/qga/network-get-interfaces", &fix
,
755 test_qga_network_get_interfaces
);
756 g_test_add_data_func("/qga/get-vcpus", &fix
, test_qga_get_vcpus
);
757 g_test_add_data_func("/qga/get-fsinfo", &fix
, test_qga_get_fsinfo
);
758 g_test_add_data_func("/qga/get-memory-block-info", &fix
,
759 test_qga_get_memory_block_info
);
760 g_test_add_data_func("/qga/get-memory-blocks", &fix
,
761 test_qga_get_memory_blocks
);
762 g_test_add_data_func("/qga/file-ops", &fix
, test_qga_file_ops
);
763 g_test_add_data_func("/qga/get-time", &fix
, test_qga_get_time
);
764 g_test_add_data_func("/qga/invalid-cmd", &fix
, test_qga_invalid_cmd
);
765 g_test_add_data_func("/qga/fsfreeze-status", &fix
,
766 test_qga_fsfreeze_status
);
768 g_test_add_data_func("/qga/blacklist", NULL
, test_qga_blacklist
);
769 g_test_add_data_func("/qga/config", NULL
, test_qga_config
);
771 if (g_getenv("QGA_TEST_SIDE_EFFECTING")) {
772 g_test_add_data_func("/qga/fsfreeze-and-thaw", &fix
,
773 test_qga_fsfreeze_and_thaw
);
774 g_test_add_data_func("/qga/set-time", &fix
, test_qga_set_time
);
775 g_test_add_data_func("/qga/fstrim", &fix
, test_qga_fstrim
);
780 fixture_tear_down(&fix
, NULL
);