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
;
249 ret
= qmp_fd(fixture
->fd
, "{'exec-oob': 'guest-ping'}");
250 g_assert_nonnull(ret
);
252 qmp_assert_error_class(ret
, "GenericError");
255 static void test_qga_invalid_args(gconstpointer fix
)
257 const TestFixture
*fixture
= fix
;
259 const gchar
*class, *desc
;
261 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-ping', "
262 "'arguments': {'foo': 42 }}");
263 g_assert_nonnull(ret
);
265 error
= qdict_get_qdict(ret
, "error");
266 class = qdict_get_try_str(error
, "class");
267 desc
= qdict_get_try_str(error
, "desc");
269 g_assert_cmpstr(class, ==, "GenericError");
270 g_assert_cmpstr(desc
, ==, "Parameter 'foo' is unexpected");
275 static void test_qga_invalid_cmd(gconstpointer fix
)
277 const TestFixture
*fixture
= fix
;
279 const gchar
*class, *desc
;
281 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-invalid-cmd'}");
282 g_assert_nonnull(ret
);
284 error
= qdict_get_qdict(ret
, "error");
285 class = qdict_get_try_str(error
, "class");
286 desc
= qdict_get_try_str(error
, "desc");
288 g_assert_cmpstr(class, ==, "CommandNotFound");
289 g_assert_cmpint(strlen(desc
), >, 0);
294 static void test_qga_info(gconstpointer fix
)
296 const TestFixture
*fixture
= fix
;
298 const gchar
*version
;
300 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-info'}");
301 g_assert_nonnull(ret
);
302 qmp_assert_no_error(ret
);
304 val
= qdict_get_qdict(ret
, "return");
305 version
= qdict_get_try_str(val
, "version");
306 g_assert_cmpstr(version
, ==, QEMU_VERSION
);
311 static void test_qga_get_vcpus(gconstpointer fix
)
313 const TestFixture
*fixture
= fix
;
316 const QListEntry
*entry
;
318 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-vcpus'}");
319 g_assert_nonnull(ret
);
320 qmp_assert_no_error(ret
);
322 /* check there is at least a cpu */
323 list
= qdict_get_qlist(ret
, "return");
324 entry
= qlist_first(list
);
325 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "online"));
326 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "logical-id"));
331 static void test_qga_get_fsinfo(gconstpointer fix
)
333 const TestFixture
*fixture
= fix
;
336 const QListEntry
*entry
;
338 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-fsinfo'}");
339 g_assert_nonnull(ret
);
340 qmp_assert_no_error(ret
);
342 /* sanity-check the response if there are any filesystems */
343 list
= qdict_get_qlist(ret
, "return");
344 entry
= qlist_first(list
);
346 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "name"));
347 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "mountpoint"));
348 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "type"));
349 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "disk"));
355 static void test_qga_get_memory_block_info(gconstpointer fix
)
357 const TestFixture
*fixture
= fix
;
361 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-memory-block-info'}");
362 g_assert_nonnull(ret
);
364 /* some systems might not expose memory block info in sysfs */
365 if (!qdict_haskey(ret
, "error")) {
366 /* check there is at least some memory */
367 val
= qdict_get_qdict(ret
, "return");
368 size
= qdict_get_int(val
, "size");
369 g_assert_cmpint(size
, >, 0);
375 static void test_qga_get_memory_blocks(gconstpointer fix
)
377 const TestFixture
*fixture
= fix
;
380 const QListEntry
*entry
;
382 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-memory-blocks'}");
383 g_assert_nonnull(ret
);
385 /* some systems might not expose memory block info in sysfs */
386 if (!qdict_haskey(ret
, "error")) {
387 list
= qdict_get_qlist(ret
, "return");
388 entry
= qlist_first(list
);
389 /* newer versions of qga may return empty list without error */
391 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
),
393 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "online"));
400 static void test_qga_network_get_interfaces(gconstpointer fix
)
402 const TestFixture
*fixture
= fix
;
405 const QListEntry
*entry
;
407 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-network-get-interfaces'}");
408 g_assert_nonnull(ret
);
409 qmp_assert_no_error(ret
);
411 /* check there is at least an interface */
412 list
= qdict_get_qlist(ret
, "return");
413 entry
= qlist_first(list
);
414 g_assert(qdict_haskey(qobject_to(QDict
, entry
->value
), "name"));
419 static void test_qga_file_ops(gconstpointer fix
)
421 const TestFixture
*fixture
= fix
;
422 const unsigned char helloworld
[] = "Hello World!\n";
433 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
434 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
435 g_assert_nonnull(ret
);
436 qmp_assert_no_error(ret
);
437 id
= qdict_get_int(ret
, "return");
440 enc
= g_base64_encode(helloworld
, sizeof(helloworld
));
442 ret
= qmp_fd(fixture
->fd
,
443 "{'execute': 'guest-file-write',"
444 " 'arguments': { 'handle': %" PRId64
", 'buf-b64': %s } }",
446 g_assert_nonnull(ret
);
447 qmp_assert_no_error(ret
);
449 val
= qdict_get_qdict(ret
, "return");
450 count
= qdict_get_int(val
, "count");
451 eof
= qdict_get_bool(val
, "eof");
452 g_assert_cmpint(count
, ==, sizeof(helloworld
));
453 g_assert_cmpint(eof
, ==, 0);
457 ret
= qmp_fd(fixture
->fd
,
458 "{'execute': 'guest-file-flush',"
459 " 'arguments': {'handle': %" PRId64
"} }",
464 ret
= qmp_fd(fixture
->fd
,
465 "{'execute': 'guest-file-close',"
466 " 'arguments': {'handle': %" PRId64
"} }",
471 path
= g_build_filename(fixture
->test_dir
, "foo", NULL
);
472 f
= fopen(path
, "r");
475 count
= fread(tmp
, 1, sizeof(tmp
), f
);
476 g_assert_cmpint(count
, ==, sizeof(helloworld
));
478 g_assert_cmpstr(tmp
, ==, (char *)helloworld
);
482 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
483 " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
484 g_assert_nonnull(ret
);
485 qmp_assert_no_error(ret
);
486 id
= qdict_get_int(ret
, "return");
490 ret
= qmp_fd(fixture
->fd
,
491 "{'execute': 'guest-file-read',"
492 " 'arguments': { 'handle': %" PRId64
"} }",
494 val
= qdict_get_qdict(ret
, "return");
495 count
= qdict_get_int(val
, "count");
496 eof
= qdict_get_bool(val
, "eof");
497 b64
= qdict_get_str(val
, "buf-b64");
498 g_assert_cmpint(count
, ==, sizeof(helloworld
));
500 g_assert_cmpstr(b64
, ==, enc
);
506 ret
= qmp_fd(fixture
->fd
,
507 "{'execute': 'guest-file-read',"
508 " 'arguments': { 'handle': %" PRId64
"} }",
510 val
= qdict_get_qdict(ret
, "return");
511 count
= qdict_get_int(val
, "count");
512 eof
= qdict_get_bool(val
, "eof");
513 b64
= qdict_get_str(val
, "buf-b64");
514 g_assert_cmpint(count
, ==, 0);
516 g_assert_cmpstr(b64
, ==, "");
520 ret
= qmp_fd(fixture
->fd
,
521 "{'execute': 'guest-file-seek',"
522 " 'arguments': { 'handle': %" PRId64
", "
523 " 'offset': %d, 'whence': %s } }",
525 qmp_assert_no_error(ret
);
526 val
= qdict_get_qdict(ret
, "return");
527 count
= qdict_get_int(val
, "position");
528 eof
= qdict_get_bool(val
, "eof");
529 g_assert_cmpint(count
, ==, 6);
534 ret
= qmp_fd(fixture
->fd
,
535 "{'execute': 'guest-file-read',"
536 " 'arguments': { 'handle': %" PRId64
"} }",
538 val
= qdict_get_qdict(ret
, "return");
539 count
= qdict_get_int(val
, "count");
540 eof
= qdict_get_bool(val
, "eof");
541 b64
= qdict_get_str(val
, "buf-b64");
542 g_assert_cmpint(count
, ==, sizeof(helloworld
) - 6);
544 dec
= g_base64_decode(b64
, &count
);
545 g_assert_cmpint(count
, ==, sizeof(helloworld
) - 6);
546 g_assert_cmpmem(dec
, count
, helloworld
+ 6, sizeof(helloworld
) - 6);
552 ret
= qmp_fd(fixture
->fd
,
553 "{'execute': 'guest-file-close',"
554 " 'arguments': {'handle': %" PRId64
"} }",
559 static void test_qga_file_write_read(gconstpointer fix
)
561 const TestFixture
*fixture
= fix
;
562 const unsigned char helloworld
[] = "Hello World!\n";
570 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-file-open',"
571 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
572 g_assert_nonnull(ret
);
573 qmp_assert_no_error(ret
);
574 id
= qdict_get_int(ret
, "return");
577 enc
= g_base64_encode(helloworld
, sizeof(helloworld
));
579 ret
= qmp_fd(fixture
->fd
,
580 "{'execute': 'guest-file-write',"
581 " 'arguments': { 'handle': %" PRId64
","
582 " 'buf-b64': %s } }", id
, enc
);
583 g_assert_nonnull(ret
);
584 qmp_assert_no_error(ret
);
586 val
= qdict_get_qdict(ret
, "return");
587 count
= qdict_get_int(val
, "count");
588 eof
= qdict_get_bool(val
, "eof");
589 g_assert_cmpint(count
, ==, sizeof(helloworld
));
590 g_assert_cmpint(eof
, ==, 0);
593 /* read (check implicit flush) */
594 ret
= qmp_fd(fixture
->fd
,
595 "{'execute': 'guest-file-read',"
596 " 'arguments': { 'handle': %" PRId64
"} }",
598 val
= qdict_get_qdict(ret
, "return");
599 count
= qdict_get_int(val
, "count");
600 eof
= qdict_get_bool(val
, "eof");
601 b64
= qdict_get_str(val
, "buf-b64");
602 g_assert_cmpint(count
, ==, 0);
604 g_assert_cmpstr(b64
, ==, "");
608 ret
= qmp_fd(fixture
->fd
,
609 "{'execute': 'guest-file-seek',"
610 " 'arguments': { 'handle': %" PRId64
", "
611 " 'offset': %d, 'whence': %s } }",
613 qmp_assert_no_error(ret
);
614 val
= qdict_get_qdict(ret
, "return");
615 count
= qdict_get_int(val
, "position");
616 eof
= qdict_get_bool(val
, "eof");
617 g_assert_cmpint(count
, ==, 0);
622 ret
= qmp_fd(fixture
->fd
,
623 "{'execute': 'guest-file-read',"
624 " 'arguments': { 'handle': %" PRId64
"} }",
626 val
= qdict_get_qdict(ret
, "return");
627 count
= qdict_get_int(val
, "count");
628 eof
= qdict_get_bool(val
, "eof");
629 b64
= qdict_get_str(val
, "buf-b64");
630 g_assert_cmpint(count
, ==, sizeof(helloworld
));
632 g_assert_cmpstr(b64
, ==, enc
);
637 ret
= qmp_fd(fixture
->fd
,
638 "{'execute': 'guest-file-close',"
639 " 'arguments': {'handle': %" PRId64
"} }",
644 static void test_qga_get_time(gconstpointer fix
)
646 const TestFixture
*fixture
= fix
;
650 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-time'}");
651 g_assert_nonnull(ret
);
652 qmp_assert_no_error(ret
);
654 time
= qdict_get_int(ret
, "return");
655 g_assert_cmpint(time
, >, 0);
660 static void test_qga_blacklist(gconstpointer data
)
664 const gchar
*class, *desc
;
666 fixture_setup(&fix
, "-b guest-ping,guest-get-time", NULL
);
668 /* check blacklist */
669 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-ping'}");
670 g_assert_nonnull(ret
);
671 error
= qdict_get_qdict(ret
, "error");
672 class = qdict_get_try_str(error
, "class");
673 desc
= qdict_get_try_str(error
, "desc");
674 g_assert_cmpstr(class, ==, "GenericError");
675 g_assert_nonnull(g_strstr_len(desc
, -1, "has been disabled"));
678 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-get-time'}");
679 g_assert_nonnull(ret
);
680 error
= qdict_get_qdict(ret
, "error");
681 class = qdict_get_try_str(error
, "class");
682 desc
= qdict_get_try_str(error
, "desc");
683 g_assert_cmpstr(class, ==, "GenericError");
684 g_assert_nonnull(g_strstr_len(desc
, -1, "has been disabled"));
687 /* check something work */
688 ret
= qmp_fd(fix
.fd
, "{'execute': 'guest-get-fsinfo'}");
689 qmp_assert_no_error(ret
);
692 fixture_tear_down(&fix
, NULL
);
695 static void test_qga_config(gconstpointer data
)
697 GError
*error
= NULL
;
698 char *cwd
, *cmd
, *out
, *err
, *str
, **strv
, **argv
= NULL
;
704 cwd
= g_get_current_dir();
705 cmd
= g_strdup_printf("%s%cqemu-ga -D",
706 cwd
, G_DIR_SEPARATOR
);
708 g_shell_parse_argv(cmd
, NULL
, &argv
, &error
);
710 g_assert_no_error(error
);
712 env
[0] = g_strdup_printf("QGA_CONF=tests%cdata%ctest-qga-config",
713 G_DIR_SEPARATOR
, G_DIR_SEPARATOR
);
715 g_spawn_sync(NULL
, argv
, env
, 0,
716 NULL
, NULL
, &out
, &err
, &status
, &error
);
719 g_assert_no_error(error
);
720 g_assert_cmpstr(err
, ==, "");
721 g_assert_cmpint(status
, ==, 0);
723 kf
= g_key_file_new();
724 g_key_file_load_from_data(kf
, out
, -1, G_KEY_FILE_NONE
, &error
);
725 g_assert_no_error(error
);
727 str
= g_key_file_get_start_group(kf
);
728 g_assert_cmpstr(str
, ==, "general");
731 g_assert_false(g_key_file_get_boolean(kf
, "general", "daemon", &error
));
732 g_assert_no_error(error
);
734 str
= g_key_file_get_string(kf
, "general", "method", &error
);
735 g_assert_no_error(error
);
736 g_assert_cmpstr(str
, ==, "virtio-serial");
739 str
= g_key_file_get_string(kf
, "general", "path", &error
);
740 g_assert_no_error(error
);
741 g_assert_cmpstr(str
, ==, "/path/to/org.qemu.guest_agent.0");
744 str
= g_key_file_get_string(kf
, "general", "pidfile", &error
);
745 g_assert_no_error(error
);
746 g_assert_cmpstr(str
, ==, "/var/foo/qemu-ga.pid");
749 str
= g_key_file_get_string(kf
, "general", "statedir", &error
);
750 g_assert_no_error(error
);
751 g_assert_cmpstr(str
, ==, "/var/state");
754 g_assert_true(g_key_file_get_boolean(kf
, "general", "verbose", &error
));
755 g_assert_no_error(error
);
757 strv
= g_key_file_get_string_list(kf
, "general", "blacklist", &n
, &error
);
758 g_assert_cmpint(n
, ==, 2);
759 g_assert_true(g_strv_contains((const char * const *)strv
,
761 g_assert_true(g_strv_contains((const char * const *)strv
,
763 g_assert_no_error(error
);
772 static void test_qga_fsfreeze_status(gconstpointer fix
)
774 const TestFixture
*fixture
= fix
;
778 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-fsfreeze-status'}");
779 g_assert_nonnull(ret
);
780 qmp_assert_no_error(ret
);
782 status
= qdict_get_try_str(ret
, "return");
783 g_assert_cmpstr(status
, ==, "thawed");
788 static void test_qga_guest_exec(gconstpointer fix
)
790 const TestFixture
*fixture
= fix
;
794 int64_t pid
, now
, exitcode
;
798 /* exec 'echo foo bar' */
799 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec', 'arguments': {"
800 " 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ],"
801 " 'capture-output': true } }");
802 g_assert_nonnull(ret
);
803 qmp_assert_no_error(ret
);
804 val
= qdict_get_qdict(ret
, "return");
805 pid
= qdict_get_int(val
, "pid");
806 g_assert_cmpint(pid
, >, 0);
809 /* wait for completion */
810 now
= g_get_monotonic_time();
812 ret
= qmp_fd(fixture
->fd
,
813 "{'execute': 'guest-exec-status',"
814 " 'arguments': { 'pid': %" PRId64
" } }", pid
);
815 g_assert_nonnull(ret
);
816 val
= qdict_get_qdict(ret
, "return");
817 exited
= qdict_get_bool(val
, "exited");
822 g_get_monotonic_time() < now
+ 5 * G_TIME_SPAN_SECOND
);
826 exitcode
= qdict_get_int(val
, "exitcode");
827 g_assert_cmpint(exitcode
, ==, 0);
828 out
= qdict_get_str(val
, "out-data");
829 decoded
= g_base64_decode(out
, &len
);
830 g_assert_cmpint(len
, ==, 12);
831 g_assert_cmpstr((char *)decoded
, ==, "\" test_str \"");
836 static void test_qga_guest_exec_invalid(gconstpointer fix
)
838 const TestFixture
*fixture
= fix
;
840 const gchar
*class, *desc
;
842 /* invalid command */
843 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec', 'arguments': {"
844 " 'path': '/bin/invalid-cmd42' } }");
845 g_assert_nonnull(ret
);
846 error
= qdict_get_qdict(ret
, "error");
847 g_assert_nonnull(error
);
848 class = qdict_get_str(error
, "class");
849 desc
= qdict_get_str(error
, "desc");
850 g_assert_cmpstr(class, ==, "GenericError");
851 g_assert_cmpint(strlen(desc
), >, 0);
855 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-exec-status',"
856 " 'arguments': { 'pid': 0 } }");
857 g_assert_nonnull(ret
);
858 error
= qdict_get_qdict(ret
, "error");
859 g_assert_nonnull(error
);
860 class = qdict_get_str(error
, "class");
861 desc
= qdict_get_str(error
, "desc");
862 g_assert_cmpstr(class, ==, "GenericError");
863 g_assert_cmpint(strlen(desc
), >, 0);
867 static void test_qga_guest_get_host_name(gconstpointer fix
)
869 const TestFixture
*fixture
= fix
;
872 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-host-name'}");
873 g_assert_nonnull(ret
);
874 qmp_assert_no_error(ret
);
876 val
= qdict_get_qdict(ret
, "return");
877 g_assert(qdict_haskey(val
, "host-name"));
882 static void test_qga_guest_get_timezone(gconstpointer fix
)
884 const TestFixture
*fixture
= fix
;
887 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-timezone'}");
888 g_assert_nonnull(ret
);
889 qmp_assert_no_error(ret
);
891 /* Make sure there's at least offset */
892 val
= qdict_get_qdict(ret
, "return");
893 g_assert(qdict_haskey(val
, "offset"));
898 static void test_qga_guest_get_users(gconstpointer fix
)
900 const TestFixture
*fixture
= fix
;
904 ret
= qmp_fd(fixture
->fd
, "{'execute': 'guest-get-users'}");
905 g_assert_nonnull(ret
);
906 qmp_assert_no_error(ret
);
908 /* There is not much to test here */
909 val
= qdict_get_qlist(ret
, "return");
910 g_assert_nonnull(val
);
915 static void test_qga_guest_get_osinfo(gconstpointer data
)
922 cwd
= g_get_current_dir();
923 env
[0] = g_strdup_printf(
924 "QGA_OS_RELEASE=%s%ctests%cdata%ctest-qga-os-release",
925 cwd
, G_DIR_SEPARATOR
, G_DIR_SEPARATOR
, G_DIR_SEPARATOR
);
928 fixture_setup(&fixture
, NULL
, env
);
930 ret
= qmp_fd(fixture
.fd
, "{'execute': 'guest-get-osinfo'}");
931 g_assert_nonnull(ret
);
932 qmp_assert_no_error(ret
);
934 val
= qdict_get_qdict(ret
, "return");
936 str
= qdict_get_try_str(val
, "id");
937 g_assert_nonnull(str
);
938 g_assert_cmpstr(str
, ==, "qemu-ga-test");
940 str
= qdict_get_try_str(val
, "name");
941 g_assert_nonnull(str
);
942 g_assert_cmpstr(str
, ==, "QEMU-GA");
944 str
= qdict_get_try_str(val
, "pretty-name");
945 g_assert_nonnull(str
);
946 g_assert_cmpstr(str
, ==, "QEMU Guest Agent test");
948 str
= qdict_get_try_str(val
, "version");
949 g_assert_nonnull(str
);
950 g_assert_cmpstr(str
, ==, "Test 1");
952 str
= qdict_get_try_str(val
, "version-id");
953 g_assert_nonnull(str
);
954 g_assert_cmpstr(str
, ==, "1");
956 str
= qdict_get_try_str(val
, "variant");
957 g_assert_nonnull(str
);
958 g_assert_cmpstr(str
, ==, "Unit test \"'$`\\ and \\\\ etc.");
960 str
= qdict_get_try_str(val
, "variant-id");
961 g_assert_nonnull(str
);
962 g_assert_cmpstr(str
, ==, "unit-test");
966 fixture_tear_down(&fixture
, NULL
);
969 int main(int argc
, char **argv
)
974 setlocale (LC_ALL
, "");
975 g_test_init(&argc
, &argv
, NULL
);
976 fixture_setup(&fix
, NULL
, NULL
);
978 g_test_add_data_func("/qga/sync-delimited", &fix
, test_qga_sync_delimited
);
979 g_test_add_data_func("/qga/sync", &fix
, test_qga_sync
);
980 g_test_add_data_func("/qga/ping", &fix
, test_qga_ping
);
981 g_test_add_data_func("/qga/info", &fix
, test_qga_info
);
982 g_test_add_data_func("/qga/network-get-interfaces", &fix
,
983 test_qga_network_get_interfaces
);
984 if (!access("/sys/devices/system/cpu/cpu0", F_OK
)) {
985 g_test_add_data_func("/qga/get-vcpus", &fix
, test_qga_get_vcpus
);
987 g_test_add_data_func("/qga/get-fsinfo", &fix
, test_qga_get_fsinfo
);
988 g_test_add_data_func("/qga/get-memory-block-info", &fix
,
989 test_qga_get_memory_block_info
);
990 g_test_add_data_func("/qga/get-memory-blocks", &fix
,
991 test_qga_get_memory_blocks
);
992 g_test_add_data_func("/qga/file-ops", &fix
, test_qga_file_ops
);
993 g_test_add_data_func("/qga/file-write-read", &fix
, test_qga_file_write_read
);
994 g_test_add_data_func("/qga/get-time", &fix
, test_qga_get_time
);
995 g_test_add_data_func("/qga/invalid-id", &fix
, test_qga_invalid_id
);
996 g_test_add_data_func("/qga/invalid-oob", &fix
, test_qga_invalid_oob
);
997 g_test_add_data_func("/qga/invalid-cmd", &fix
, test_qga_invalid_cmd
);
998 g_test_add_data_func("/qga/invalid-args", &fix
, test_qga_invalid_args
);
999 g_test_add_data_func("/qga/fsfreeze-status", &fix
,
1000 test_qga_fsfreeze_status
);
1002 g_test_add_data_func("/qga/blacklist", NULL
, test_qga_blacklist
);
1003 g_test_add_data_func("/qga/config", NULL
, test_qga_config
);
1004 g_test_add_data_func("/qga/guest-exec", &fix
, test_qga_guest_exec
);
1005 g_test_add_data_func("/qga/guest-exec-invalid", &fix
,
1006 test_qga_guest_exec_invalid
);
1007 g_test_add_data_func("/qga/guest-get-osinfo", &fix
,
1008 test_qga_guest_get_osinfo
);
1009 g_test_add_data_func("/qga/guest-get-host-name", &fix
,
1010 test_qga_guest_get_host_name
);
1011 g_test_add_data_func("/qga/guest-get-timezone", &fix
,
1012 test_qga_guest_get_timezone
);
1013 g_test_add_data_func("/qga/guest-get-users", &fix
,
1014 test_qga_guest_get_users
);
1018 fixture_tear_down(&fix
, NULL
);