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
)
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
, NULL
,
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("%c{'execute': 'guest-sync-delimited',"
150 " 'arguments': {'id': %u } }", 0xff, r
);
151 qmp_fd_send(fixture
->fd
, cmd
);
154 v
= read(fixture
->fd
, &c
, 1);
155 g_assert_cmpint(v
, ==, 1);
156 g_assert_cmpint(c
, ==, 0xff);
158 ret
= qmp_fd_receive(fixture
->fd
);
159 g_assert_nonnull(ret
);
160 qmp_assert_no_error(ret
);
162 v
= qdict_get_int(ret
, "return");
163 g_assert_cmpint(r
, ==, v
);
168 static void test_qga_sync(gconstpointer fix
)
170 const TestFixture
*fixture
= fix
;
171 guint32 v
, r
= g_random_int();
175 cmd
= g_strdup_printf("%c{'execute': 'guest-sync',"
176 " 'arguments': {'id': %u } }", 0xff, r
);
177 ret
= qmp_fd(fixture
->fd
, cmd
);
180 g_assert_nonnull(ret
);
181 qmp_assert_no_error(ret
);
183 v
= qdict_get_int(ret
, "return");
184 g_assert_cmpint(r
, ==, v
);
189 static void test_qga_ping(gconstpointer fix
)
191 const TestFixture
*fixture
= fix
;
194 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-ping'}");
195 g_assert_nonnull(ret
);
196 qmp_assert_no_error(ret
);
201 static void test_qga_invalid_cmd(gconstpointer fix
)
203 const TestFixture
*fixture
= fix
;
205 const gchar
*class, *desc
;
207 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-invalid-cmd'}");
208 g_assert_nonnull(ret
);
210 error
= qdict_get_qdict(ret
, "error");
211 class = qdict_get_try_str(error
, "class");
212 desc
= qdict_get_try_str(error
, "desc");
214 g_assert_cmpstr(class, ==, "CommandNotFound");
215 g_assert_cmpint(strlen(desc
), >, 0);
220 static void test_qga_info(gconstpointer fix
)
222 const TestFixture
*fixture
= fix
;
224 const gchar
*version
;
226 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-info'}");
227 g_assert_nonnull(ret
);
228 qmp_assert_no_error(ret
);
230 val
= qdict_get_qdict(ret
, "return");
231 version
= qdict_get_try_str(val
, "version");
232 g_assert_cmpstr(version
, ==, QEMU_VERSION
);
237 static void test_qga_get_vcpus(gconstpointer fix
)
239 const TestFixture
*fixture
= fix
;
242 const QListEntry
*entry
;
244 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-vcpus'}");
245 g_assert_nonnull(ret
);
246 qmp_assert_no_error(ret
);
248 /* check there is at least a cpu */
249 list
= qdict_get_qlist(ret
, "return");
250 entry
= qlist_first(list
);
251 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "online"));
252 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "logical-id"));
257 static void test_qga_get_fsinfo(gconstpointer fix
)
259 const TestFixture
*fixture
= fix
;
262 const QListEntry
*entry
;
264 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-fsinfo'}");
265 g_assert_nonnull(ret
);
266 qmp_assert_no_error(ret
);
268 /* sanity-check the response if there are any filesystems */
269 list
= qdict_get_qlist(ret
, "return");
270 entry
= qlist_first(list
);
272 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "name"));
273 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "mountpoint"));
274 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "type"));
275 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "disk"));
281 static void test_qga_get_memory_block_info(gconstpointer fix
)
283 const TestFixture
*fixture
= fix
;
287 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-memory-block-info'}");
288 g_assert_nonnull(ret
);
290 /* some systems might not expose memory block info in sysfs */
291 if (!qdict_haskey(ret
, "error")) {
292 /* check there is at least some memory */
293 val
= qdict_get_qdict(ret
, "return");
294 size
= qdict_get_int(val
, "size");
295 g_assert_cmpint(size
, >, 0);
301 static void test_qga_get_memory_blocks(gconstpointer fix
)
303 const TestFixture
*fixture
= fix
;
306 const QListEntry
*entry
;
308 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-memory-blocks'}");
309 g_assert_nonnull(ret
);
311 /* some systems might not expose memory block info in sysfs */
312 if (!qdict_haskey(ret
, "error")) {
313 list
= qdict_get_qlist(ret
, "return");
314 entry
= qlist_first(list
);
315 /* newer versions of qga may return empty list without error */
317 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "phys-index"));
318 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "online"));
325 static void test_qga_network_get_interfaces(gconstpointer fix
)
327 const TestFixture
*fixture
= fix
;
330 const QListEntry
*entry
;
332 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-network-get-interfaces'}");
333 g_assert_nonnull(ret
);
334 qmp_assert_no_error(ret
);
336 /* check there is at least an interface */
337 list
= qdict_get_qlist(ret
, "return");
338 entry
= qlist_first(list
);
339 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "name"));
344 static void test_qga_file_ops(gconstpointer fix
)
346 const TestFixture
*fixture
= fix
;
347 const unsigned char helloworld
[] = "Hello World!\n";
349 gchar
*cmd
, *path
, *enc
;
358 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
359 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
360 g_assert_nonnull(ret
);
361 qmp_assert_no_error(ret
);
362 id
= qdict_get_int(ret
, "return");
365 enc
= g_base64_encode(helloworld
, sizeof(helloworld
));
367 cmd
= g_strdup_printf("{'execute': 'guest-file-write',"
368 " 'arguments': { 'handle': %" PRId64
","
369 " 'buf-b64': '%s' } }", id
, enc
);
370 ret
= qmp_fd(fixture
->fd
, cmd
);
371 g_assert_nonnull(ret
);
372 qmp_assert_no_error(ret
);
374 val
= qdict_get_qdict(ret
, "return");
375 count
= qdict_get_int(val
, "count");
376 eof
= qdict_get_bool(val
, "eof");
377 g_assert_cmpint(count
, ==, sizeof(helloworld
));
378 g_assert_cmpint(eof
, ==, 0);
383 cmd
= g_strdup_printf("{'execute': 'guest-file-flush',"
384 " 'arguments': {'handle': %" PRId64
"} }",
386 ret
= qmp_fd(fixture
->fd
, cmd
);
391 cmd
= g_strdup_printf("{'execute': 'guest-file-close',"
392 " 'arguments': {'handle': %" PRId64
"} }",
394 ret
= qmp_fd(fixture
->fd
, cmd
);
399 path
= g_build_filename(fixture
->test_dir
, "foo", NULL
);
400 f
= fopen(path
, "r");
402 count
= fread(tmp
, 1, sizeof(tmp
), f
);
403 g_assert_cmpint(count
, ==, sizeof(helloworld
));
405 g_assert_cmpstr(tmp
, ==, (char *)helloworld
);
409 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
410 " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
411 g_assert_nonnull(ret
);
412 qmp_assert_no_error(ret
);
413 id
= qdict_get_int(ret
, "return");
417 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
418 " 'arguments': { 'handle': %" PRId64
"} }",
420 ret
= qmp_fd(fixture
->fd
, cmd
);
421 val
= qdict_get_qdict(ret
, "return");
422 count
= qdict_get_int(val
, "count");
423 eof
= qdict_get_bool(val
, "eof");
424 b64
= qdict_get_str(val
, "buf-b64");
425 g_assert_cmpint(count
, ==, sizeof(helloworld
));
427 g_assert_cmpstr(b64
, ==, enc
);
434 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
435 " 'arguments': { 'handle': %" PRId64
"} }",
437 ret
= qmp_fd(fixture
->fd
, cmd
);
438 val
= qdict_get_qdict(ret
, "return");
439 count
= qdict_get_int(val
, "count");
440 eof
= qdict_get_bool(val
, "eof");
441 b64
= qdict_get_str(val
, "buf-b64");
442 g_assert_cmpint(count
, ==, 0);
444 g_assert_cmpstr(b64
, ==, "");
449 cmd
= g_strdup_printf("{'execute': 'guest-file-seek',"
450 " 'arguments': { 'handle': %" PRId64
", "
451 " 'offset': %d, 'whence': '%s' } }",
453 ret
= qmp_fd(fixture
->fd
, cmd
);
454 qmp_assert_no_error(ret
);
455 val
= qdict_get_qdict(ret
, "return");
456 count
= qdict_get_int(val
, "position");
457 eof
= qdict_get_bool(val
, "eof");
458 g_assert_cmpint(count
, ==, 6);
464 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
465 " 'arguments': { 'handle': %" PRId64
"} }",
467 ret
= qmp_fd(fixture
->fd
, cmd
);
468 val
= qdict_get_qdict(ret
, "return");
469 count
= qdict_get_int(val
, "count");
470 eof
= qdict_get_bool(val
, "eof");
471 b64
= qdict_get_str(val
, "buf-b64");
472 g_assert_cmpint(count
, ==, sizeof(helloworld
) - 6);
474 dec
= g_base64_decode(b64
, &count
);
475 g_assert_cmpint(count
, ==, sizeof(helloworld
) - 6);
476 g_assert_cmpmem(dec
, count
, helloworld
+ 6, sizeof(helloworld
) - 6);
483 cmd
= g_strdup_printf("{'execute': 'guest-file-close',"
484 " 'arguments': {'handle': %" PRId64
"} }",
486 ret
= qmp_fd(fixture
->fd
, cmd
);
491 static void test_qga_file_write_read(gconstpointer fix
)
493 const TestFixture
*fixture
= fix
;
494 const unsigned char helloworld
[] = "Hello World!\n";
502 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
503 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
504 g_assert_nonnull(ret
);
505 qmp_assert_no_error(ret
);
506 id
= qdict_get_int(ret
, "return");
509 enc
= g_base64_encode(helloworld
, sizeof(helloworld
));
511 cmd
= g_strdup_printf("{'execute': 'guest-file-write',"
512 " 'arguments': { 'handle': %" PRId64
","
513 " 'buf-b64': '%s' } }", id
, enc
);
514 ret
= qmp_fd(fixture
->fd
, cmd
);
515 g_assert_nonnull(ret
);
516 qmp_assert_no_error(ret
);
518 val
= qdict_get_qdict(ret
, "return");
519 count
= qdict_get_int(val
, "count");
520 eof
= qdict_get_bool(val
, "eof");
521 g_assert_cmpint(count
, ==, sizeof(helloworld
));
522 g_assert_cmpint(eof
, ==, 0);
526 /* read (check implicit flush) */
527 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
528 " 'arguments': { 'handle': %" PRId64
"} }",
530 ret
= qmp_fd(fixture
->fd
, cmd
);
531 val
= qdict_get_qdict(ret
, "return");
532 count
= qdict_get_int(val
, "count");
533 eof
= qdict_get_bool(val
, "eof");
534 b64
= qdict_get_str(val
, "buf-b64");
535 g_assert_cmpint(count
, ==, 0);
537 g_assert_cmpstr(b64
, ==, "");
542 cmd
= g_strdup_printf("{'execute': 'guest-file-seek',"
543 " 'arguments': { 'handle': %" PRId64
", "
544 " 'offset': %d, 'whence': '%s' } }",
546 ret
= qmp_fd(fixture
->fd
, cmd
);
547 qmp_assert_no_error(ret
);
548 val
= qdict_get_qdict(ret
, "return");
549 count
= qdict_get_int(val
, "position");
550 eof
= qdict_get_bool(val
, "eof");
551 g_assert_cmpint(count
, ==, 0);
557 cmd
= g_strdup_printf("{'execute': 'guest-file-read',"
558 " 'arguments': { 'handle': %" PRId64
"} }",
560 ret
= qmp_fd(fixture
->fd
, cmd
);
561 val
= qdict_get_qdict(ret
, "return");
562 count
= qdict_get_int(val
, "count");
563 eof
= qdict_get_bool(val
, "eof");
564 b64
= qdict_get_str(val
, "buf-b64");
565 g_assert_cmpint(count
, ==, sizeof(helloworld
));
567 g_assert_cmpstr(b64
, ==, enc
);
573 cmd
= g_strdup_printf("{'execute': 'guest-file-close',"
574 " 'arguments': {'handle': %" PRId64
"} }",
576 ret
= qmp_fd(fixture
->fd
, cmd
);
581 static void test_qga_get_time(gconstpointer fix
)
583 const TestFixture
*fixture
= fix
;
587 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-time'}");
588 g_assert_nonnull(ret
);
589 qmp_assert_no_error(ret
);
591 time
= qdict_get_int(ret
, "return");
592 g_assert_cmpint(time
, >, 0);
597 static void test_qga_set_time(gconstpointer fix
)
599 const TestFixture
*fixture
= fix
;
601 int64_t current
, time
;
604 /* get current time */
605 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-time'}");
606 g_assert_nonnull(ret
);
607 qmp_assert_no_error(ret
);
608 current
= qdict_get_int(ret
, "return");
609 g_assert_cmpint(current
, >, 0);
612 /* set some old time */
613 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-set-time',"
614 " 'arguments': { 'time': 1000 } }");
615 g_assert_nonnull(ret
);
616 qmp_assert_no_error(ret
);
620 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-time'}");
621 g_assert_nonnull(ret
);
622 qmp_assert_no_error(ret
);
623 time
= qdict_get_int(ret
, "return");
624 g_assert_cmpint(time
/ 1000, <, G_USEC_PER_SEC
* 10);
627 /* set back current time */
628 cmd
= g_strdup_printf("{'execute': 'guest-set-time',"
629 " 'arguments': { 'time': %" PRId64
" } }",
630 current
+ time
* 1000);
631 ret
= qmp_fd(fixture
->fd
, cmd
);
633 g_assert_nonnull(ret
);
634 qmp_assert_no_error(ret
);
638 static void test_qga_fstrim(gconstpointer fix
)
640 const TestFixture
*fixture
= fix
;
643 const QListEntry
*entry
;
645 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fstrim',"
646 " arguments: { minimum: 4194304 } }");
647 g_assert_nonnull(ret
);
648 qmp_assert_no_error(ret
);
649 list
= qdict_get_qlist(ret
, "return");
650 entry
= qlist_first(list
);
651 g_assert(qdict_haskey(qobject_to_qdict(entry
->value
), "paths"));
656 static void test_qga_blacklist(gconstpointer data
)
660 const gchar
*class, *desc
;
662 fixture_setup(&fix
, "-b guest-ping,guest-get-time");
664 /* check blacklist */
665 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-ping'}");
666 g_assert_nonnull(ret
);
667 error
= qdict_get_qdict(ret
, "error");
668 class = qdict_get_try_str(error
, "class");
669 desc
= qdict_get_try_str(error
, "desc");
670 g_assert_cmpstr(class, ==, "GenericError");
671 g_assert_nonnull(g_strstr_len(desc
, -1, "has been disabled"));
674 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-get-time'}");
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 /* check something work */
684 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-get-fsinfo'}");
685 qmp_assert_no_error(ret
);
688 fixture_tear_down(&fix
, NULL
);
691 static void test_qga_config(gconstpointer data
)
693 GError
*error
= NULL
;
694 char *cwd
, *cmd
, *out
, *err
, *str
, **strv
, *conf
, **argv
= NULL
;
699 const char *qga_config
=
702 "method=virtio-serial\n"
703 "path=/path/to/org.qemu.guest_agent.0\n"
704 "pidfile=/var/foo/qemu-ga.pid\n"
705 "statedir=/var/state\n"
707 "blacklist=guest-ping;guest-get-time\n";
709 tmp
= g_file_open_tmp(NULL
, &conf
, &error
);
710 g_assert_no_error(error
);
711 g_assert_cmpint(tmp
, >=, 0);
712 g_assert_cmpstr(conf
, !=, "");
714 g_file_set_contents(conf
, qga_config
, -1, &error
);
715 g_assert_no_error(error
);
717 cwd
= g_get_current_dir();
718 cmd
= g_strdup_printf("%s%cqemu-ga -D",
719 cwd
, G_DIR_SEPARATOR
);
720 g_shell_parse_argv(cmd
, NULL
, &argv
, &error
);
721 g_assert_no_error(error
);
723 env
[0] = g_strdup_printf("QGA_CONF=%s", conf
);
725 g_spawn_sync(NULL
, argv
, env
, 0,
726 NULL
, NULL
, &out
, &err
, &status
, &error
);
727 g_assert_no_error(error
);
728 g_assert_cmpstr(err
, ==, "");
729 g_assert_cmpint(status
, ==, 0);
731 kf
= g_key_file_new();
732 g_key_file_load_from_data(kf
, out
, -1, G_KEY_FILE_NONE
, &error
);
733 g_assert_no_error(error
);
735 str
= g_key_file_get_start_group(kf
);
736 g_assert_cmpstr(str
, ==, "general");
739 g_assert_false(g_key_file_get_boolean(kf
, "general", "daemon", &error
));
740 g_assert_no_error(error
);
742 str
= g_key_file_get_string(kf
, "general", "method", &error
);
743 g_assert_no_error(error
);
744 g_assert_cmpstr(str
, ==, "virtio-serial");
747 str
= g_key_file_get_string(kf
, "general", "path", &error
);
748 g_assert_no_error(error
);
749 g_assert_cmpstr(str
, ==, "/path/to/org.qemu.guest_agent.0");
752 str
= g_key_file_get_string(kf
, "general", "pidfile", &error
);
753 g_assert_no_error(error
);
754 g_assert_cmpstr(str
, ==, "/var/foo/qemu-ga.pid");
757 str
= g_key_file_get_string(kf
, "general", "statedir", &error
);
758 g_assert_no_error(error
);
759 g_assert_cmpstr(str
, ==, "/var/state");
762 g_assert_true(g_key_file_get_boolean(kf
, "general", "verbose", &error
));
763 g_assert_no_error(error
);
765 strv
= g_key_file_get_string_list(kf
, "general", "blacklist", &n
, &error
);
766 g_assert_cmpint(n
, ==, 2);
767 #if GLIB_CHECK_VERSION(2, 44, 0)
768 g_assert_true(g_strv_contains((const char * const *)strv
,
770 g_assert_true(g_strv_contains((const char * const *)strv
,
773 g_assert_no_error(error
);
785 static void test_qga_fsfreeze_status(gconstpointer fix
)
787 const TestFixture
*fixture
= fix
;
791 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fsfreeze-status'}");
792 g_assert_nonnull(ret
);
793 qmp_assert_no_error(ret
);
795 status
= qdict_get_try_str(ret
, "return");
796 g_assert_cmpstr(status
, ==, "thawed");
801 static void test_qga_fsfreeze_and_thaw(gconstpointer fix
)
803 const TestFixture
*fixture
= fix
;
807 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fsfreeze-freeze'}");
808 g_assert_nonnull(ret
);
809 qmp_assert_no_error(ret
);
812 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fsfreeze-status'}");
813 g_assert_nonnull(ret
);
814 qmp_assert_no_error(ret
);
815 status
= qdict_get_try_str(ret
, "return");
816 g_assert_cmpstr(status
, ==, "frozen");
819 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fsfreeze-thaw'}");
820 g_assert_nonnull(ret
);
821 qmp_assert_no_error(ret
);
825 static void test_qga_guest_exec(gconstpointer fix
)
827 const TestFixture
*fixture
= fix
;
831 int64_t pid
, now
, exitcode
;
835 /* exec 'echo foo bar' */
836 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec', 'arguments': {"
837 " 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ],"
838 " 'capture-output': true } }");
839 g_assert_nonnull(ret
);
840 qmp_assert_no_error(ret
);
841 val
= qdict_get_qdict(ret
, "return");
842 pid
= qdict_get_int(val
, "pid");
843 g_assert_cmpint(pid
, >, 0);
846 /* wait for completion */
847 now
= g_get_monotonic_time();
849 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec-status',"
850 " 'arguments': { 'pid': %" PRId64
" } }", pid
);
851 g_assert_nonnull(ret
);
852 val
= qdict_get_qdict(ret
, "return");
853 exited
= qdict_get_bool(val
, "exited");
858 g_get_monotonic_time() < now
+ 5 * G_TIME_SPAN_SECOND
);
862 exitcode
= qdict_get_int(val
, "exitcode");
863 g_assert_cmpint(exitcode
, ==, 0);
864 out
= qdict_get_str(val
, "out-data");
865 decoded
= g_base64_decode(out
, &len
);
866 g_assert_cmpint(len
, ==, 12);
867 g_assert_cmpstr((char *)decoded
, ==, "\" test_str \"");
872 static void test_qga_guest_exec_invalid(gconstpointer fix
)
874 const TestFixture
*fixture
= fix
;
876 const gchar
*class, *desc
;
878 /* invalid command */
879 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec', 'arguments': {"
880 " 'path': '/bin/invalid-cmd42' } }");
881 g_assert_nonnull(ret
);
882 error
= qdict_get_qdict(ret
, "error");
883 g_assert_nonnull(error
);
884 class = qdict_get_str(error
, "class");
885 desc
= qdict_get_str(error
, "desc");
886 g_assert_cmpstr(class, ==, "GenericError");
887 g_assert_cmpint(strlen(desc
), >, 0);
891 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec-status',"
892 " 'arguments': { 'pid': 0 } }");
893 g_assert_nonnull(ret
);
894 error
= qdict_get_qdict(ret
, "error");
895 g_assert_nonnull(error
);
896 class = qdict_get_str(error
, "class");
897 desc
= qdict_get_str(error
, "desc");
898 g_assert_cmpstr(class, ==, "GenericError");
899 g_assert_cmpint(strlen(desc
), >, 0);
903 int main(int argc
, char **argv
)
908 setlocale (LC_ALL
, "");
909 g_test_init(&argc
, &argv
, NULL
);
910 fixture_setup(&fix
, NULL
);
912 g_test_add_data_func("/qga/sync-delimited", &fix
, test_qga_sync_delimited
);
913 g_test_add_data_func("/qga/sync", &fix
, test_qga_sync
);
914 g_test_add_data_func("/qga/ping", &fix
, test_qga_ping
);
915 g_test_add_data_func("/qga/info", &fix
, test_qga_info
);
916 g_test_add_data_func("/qga/network-get-interfaces", &fix
,
917 test_qga_network_get_interfaces
);
918 g_test_add_data_func("/qga/get-vcpus", &fix
, test_qga_get_vcpus
);
919 g_test_add_data_func("/qga/get-fsinfo", &fix
, test_qga_get_fsinfo
);
920 g_test_add_data_func("/qga/get-memory-block-info", &fix
,
921 test_qga_get_memory_block_info
);
922 g_test_add_data_func("/qga/get-memory-blocks", &fix
,
923 test_qga_get_memory_blocks
);
924 g_test_add_data_func("/qga/file-ops", &fix
, test_qga_file_ops
);
925 g_test_add_data_func("/qga/file-write-read", &fix
, test_qga_file_write_read
);
926 g_test_add_data_func("/qga/get-time", &fix
, test_qga_get_time
);
927 g_test_add_data_func("/qga/invalid-cmd", &fix
, test_qga_invalid_cmd
);
928 g_test_add_data_func("/qga/fsfreeze-status", &fix
,
929 test_qga_fsfreeze_status
);
931 g_test_add_data_func("/qga/blacklist", NULL
, test_qga_blacklist
);
932 g_test_add_data_func("/qga/config", NULL
, test_qga_config
);
933 g_test_add_data_func("/qga/guest-exec", &fix
, test_qga_guest_exec
);
934 g_test_add_data_func("/qga/guest-exec-invalid", &fix
,
935 test_qga_guest_exec_invalid
);
937 if (g_getenv("QGA_TEST_SIDE_EFFECTING")) {
938 g_test_add_data_func("/qga/fsfreeze-and-thaw", &fix
,
939 test_qga_fsfreeze_and_thaw
);
940 g_test_add_data_func("/qga/set-time", &fix
, test_qga_set_time
);
941 g_test_add_data_func("/qga/fstrim", &fix
, test_qga_fstrim
);
946 fixture_tear_down(&fix
, NULL
);