dma/xlnx-zdma: Advance the descriptor address when stopping
[qemu/ar7.git] / tests / test-qga.c
blobd2b2435bb46e28181866a3704ad10803402cb210
1 #include "qemu/osdep.h"
2 #include <locale.h>
3 #include <glib/gstdio.h>
4 #include <sys/socket.h>
5 #include <sys/un.h>
7 #include "libqtest.h"
8 #include "qapi/qmp/qdict.h"
9 #include "qapi/qmp/qlist.h"
11 typedef struct {
12 char *test_dir;
13 GMainLoop *loop;
14 int fd;
15 GPid pid;
16 } TestFixture;
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);
24 g_assert(s != -1);
26 remote.sun_family = AF_UNIX;
27 do {
28 strcpy(remote.sun_path, path);
29 len = strlen(remote.sun_path) + sizeof(remote.sun_family);
30 ret = connect(s, (struct sockaddr *)&remote, len);
31 if (ret == -1) {
32 g_usleep(G_USEC_PER_SEC);
34 if (i++ == 10) {
35 return -1;
37 } while (ret == -1);
39 return s;
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);
50 static void
51 fixture_setup(TestFixture *fixture, gconstpointer data, gchar **envp)
53 const gchar *extra_arg = data;
54 GError *error = NULL;
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",
65 cwd, G_DIR_SEPARATOR,
66 fixture->test_dir, path,
67 getenv("QTEST_LOG") ? "-v" : "",
68 extra_arg ?: "");
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);
82 g_strfreev(argv);
83 g_free(cmd);
84 g_free(cwd);
85 g_free(path);
88 static void
89 fixture_tear_down(TestFixture *fixture, gconstpointer data)
91 gchar *tmp;
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);
101 g_unlink(tmp);
102 g_free(tmp);
104 tmp = g_build_filename(fixture->test_dir, "qga.state", NULL);
105 g_unlink(tmp);
106 g_free(tmp);
108 tmp = g_build_filename(fixture->test_dir, "sock", NULL);
109 g_unlink(tmp);
110 g_free(tmp);
112 g_rmdir(fixture->test_dir);
113 g_free(fixture->test_dir);
116 static void qmp_assertion_message_error(const char *domain,
117 const char *file,
118 int line,
119 const char *func,
120 const char *expr,
121 QDict *dict)
123 const char *class, *desc;
124 char *s;
125 QDict *error;
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);
133 g_free(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); \
141 } while (0)
143 static void test_qga_sync_delimited(gconstpointer fix)
145 const TestFixture *fixture = fix;
146 guint32 v, r = g_test_rand_int();
147 unsigned char c;
148 QDict *ret;
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
164 * to implement.
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
169 * handshake.
171 do {
172 v = read(fixture->fd, &c, 1);
173 g_assert_cmpint(v, ==, 1);
174 } while (c != 0xff);
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);
183 qobject_unref(ret);
186 static void test_qga_sync(gconstpointer fix)
188 const TestFixture *fixture = fix;
189 guint32 v, r = g_test_rand_int();
190 QDict *ret;
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);
213 qobject_unref(ret);
216 static void test_qga_ping(gconstpointer fix)
218 const TestFixture *fixture = fix;
219 QDict *ret;
221 ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping'}");
222 g_assert_nonnull(ret);
223 qmp_assert_no_error(ret);
225 qobject_unref(ret);
228 static void test_qga_id(gconstpointer fix)
230 const TestFixture *fixture = fix;
231 QDict *ret;
233 ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping', 'id': 1}");
234 g_assert_nonnull(ret);
235 qmp_assert_no_error(ret);
236 g_assert_cmpint(qdict_get_int(ret, "id"), ==, 1);
238 qobject_unref(ret);
241 static void test_qga_invalid_oob(gconstpointer fix)
243 const TestFixture *fixture = fix;
244 QDict *ret;
246 ret = qmp_fd(fixture->fd, "{'exec-oob': 'guest-ping'}");
247 g_assert_nonnull(ret);
249 qmp_assert_error_class(ret, "GenericError");
252 static void test_qga_invalid_args(gconstpointer fix)
254 const TestFixture *fixture = fix;
255 QDict *ret, *error;
256 const gchar *class, *desc;
258 ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping', "
259 "'arguments': {'foo': 42 }}");
260 g_assert_nonnull(ret);
262 error = qdict_get_qdict(ret, "error");
263 class = qdict_get_try_str(error, "class");
264 desc = qdict_get_try_str(error, "desc");
266 g_assert_cmpstr(class, ==, "GenericError");
267 g_assert_cmpstr(desc, ==, "Parameter 'foo' is unexpected");
269 qobject_unref(ret);
272 static void test_qga_invalid_cmd(gconstpointer fix)
274 const TestFixture *fixture = fix;
275 QDict *ret, *error;
276 const gchar *class, *desc;
278 ret = qmp_fd(fixture->fd, "{'execute': 'guest-invalid-cmd'}");
279 g_assert_nonnull(ret);
281 error = qdict_get_qdict(ret, "error");
282 class = qdict_get_try_str(error, "class");
283 desc = qdict_get_try_str(error, "desc");
285 g_assert_cmpstr(class, ==, "CommandNotFound");
286 g_assert_cmpint(strlen(desc), >, 0);
288 qobject_unref(ret);
291 static void test_qga_info(gconstpointer fix)
293 const TestFixture *fixture = fix;
294 QDict *ret, *val;
295 const gchar *version;
297 ret = qmp_fd(fixture->fd, "{'execute': 'guest-info'}");
298 g_assert_nonnull(ret);
299 qmp_assert_no_error(ret);
301 val = qdict_get_qdict(ret, "return");
302 version = qdict_get_try_str(val, "version");
303 g_assert_cmpstr(version, ==, QEMU_VERSION);
305 qobject_unref(ret);
308 static void test_qga_get_vcpus(gconstpointer fix)
310 const TestFixture *fixture = fix;
311 QDict *ret;
312 QList *list;
313 const QListEntry *entry;
315 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-vcpus'}");
316 g_assert_nonnull(ret);
317 qmp_assert_no_error(ret);
319 /* check there is at least a cpu */
320 list = qdict_get_qlist(ret, "return");
321 entry = qlist_first(list);
322 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "online"));
323 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "logical-id"));
325 qobject_unref(ret);
328 static void test_qga_get_fsinfo(gconstpointer fix)
330 const TestFixture *fixture = fix;
331 QDict *ret;
332 QList *list;
333 const QListEntry *entry;
335 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-fsinfo'}");
336 g_assert_nonnull(ret);
337 qmp_assert_no_error(ret);
339 /* sanity-check the response if there are any filesystems */
340 list = qdict_get_qlist(ret, "return");
341 entry = qlist_first(list);
342 if (entry) {
343 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "name"));
344 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "mountpoint"));
345 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "type"));
346 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "disk"));
349 qobject_unref(ret);
352 static void test_qga_get_memory_block_info(gconstpointer fix)
354 const TestFixture *fixture = fix;
355 QDict *ret, *val;
356 int64_t size;
358 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-block-info'}");
359 g_assert_nonnull(ret);
361 /* some systems might not expose memory block info in sysfs */
362 if (!qdict_haskey(ret, "error")) {
363 /* check there is at least some memory */
364 val = qdict_get_qdict(ret, "return");
365 size = qdict_get_int(val, "size");
366 g_assert_cmpint(size, >, 0);
369 qobject_unref(ret);
372 static void test_qga_get_memory_blocks(gconstpointer fix)
374 const TestFixture *fixture = fix;
375 QDict *ret;
376 QList *list;
377 const QListEntry *entry;
379 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-blocks'}");
380 g_assert_nonnull(ret);
382 /* some systems might not expose memory block info in sysfs */
383 if (!qdict_haskey(ret, "error")) {
384 list = qdict_get_qlist(ret, "return");
385 entry = qlist_first(list);
386 /* newer versions of qga may return empty list without error */
387 if (entry) {
388 g_assert(qdict_haskey(qobject_to(QDict, entry->value),
389 "phys-index"));
390 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "online"));
394 qobject_unref(ret);
397 static void test_qga_network_get_interfaces(gconstpointer fix)
399 const TestFixture *fixture = fix;
400 QDict *ret;
401 QList *list;
402 const QListEntry *entry;
404 ret = qmp_fd(fixture->fd, "{'execute': 'guest-network-get-interfaces'}");
405 g_assert_nonnull(ret);
406 qmp_assert_no_error(ret);
408 /* check there is at least an interface */
409 list = qdict_get_qlist(ret, "return");
410 entry = qlist_first(list);
411 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "name"));
413 qobject_unref(ret);
416 static void test_qga_file_ops(gconstpointer fix)
418 const TestFixture *fixture = fix;
419 const unsigned char helloworld[] = "Hello World!\n";
420 const char *b64;
421 gchar *path, *enc;
422 unsigned char *dec;
423 QDict *ret, *val;
424 int64_t id, eof;
425 gsize count;
426 FILE *f;
427 char tmp[100];
429 /* open */
430 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
431 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
432 g_assert_nonnull(ret);
433 qmp_assert_no_error(ret);
434 id = qdict_get_int(ret, "return");
435 qobject_unref(ret);
437 enc = g_base64_encode(helloworld, sizeof(helloworld));
438 /* write */
439 ret = qmp_fd(fixture->fd,
440 "{'execute': 'guest-file-write',"
441 " 'arguments': { 'handle': %" PRId64 ", 'buf-b64': %s } }",
442 id, enc);
443 g_assert_nonnull(ret);
444 qmp_assert_no_error(ret);
446 val = qdict_get_qdict(ret, "return");
447 count = qdict_get_int(val, "count");
448 eof = qdict_get_bool(val, "eof");
449 g_assert_cmpint(count, ==, sizeof(helloworld));
450 g_assert_cmpint(eof, ==, 0);
451 qobject_unref(ret);
453 /* flush */
454 ret = qmp_fd(fixture->fd,
455 "{'execute': 'guest-file-flush',"
456 " 'arguments': {'handle': %" PRId64 "} }",
457 id);
458 qobject_unref(ret);
460 /* close */
461 ret = qmp_fd(fixture->fd,
462 "{'execute': 'guest-file-close',"
463 " 'arguments': {'handle': %" PRId64 "} }",
464 id);
465 qobject_unref(ret);
467 /* check content */
468 path = g_build_filename(fixture->test_dir, "foo", NULL);
469 f = fopen(path, "r");
470 g_free(path);
471 g_assert_nonnull(f);
472 count = fread(tmp, 1, sizeof(tmp), f);
473 g_assert_cmpint(count, ==, sizeof(helloworld));
474 tmp[count] = 0;
475 g_assert_cmpstr(tmp, ==, (char *)helloworld);
476 fclose(f);
478 /* open */
479 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
480 " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
481 g_assert_nonnull(ret);
482 qmp_assert_no_error(ret);
483 id = qdict_get_int(ret, "return");
484 qobject_unref(ret);
486 /* read */
487 ret = qmp_fd(fixture->fd,
488 "{'execute': 'guest-file-read',"
489 " 'arguments': { 'handle': %" PRId64 "} }",
490 id);
491 val = qdict_get_qdict(ret, "return");
492 count = qdict_get_int(val, "count");
493 eof = qdict_get_bool(val, "eof");
494 b64 = qdict_get_str(val, "buf-b64");
495 g_assert_cmpint(count, ==, sizeof(helloworld));
496 g_assert(eof);
497 g_assert_cmpstr(b64, ==, enc);
499 qobject_unref(ret);
500 g_free(enc);
502 /* read eof */
503 ret = qmp_fd(fixture->fd,
504 "{'execute': 'guest-file-read',"
505 " 'arguments': { 'handle': %" PRId64 "} }",
506 id);
507 val = qdict_get_qdict(ret, "return");
508 count = qdict_get_int(val, "count");
509 eof = qdict_get_bool(val, "eof");
510 b64 = qdict_get_str(val, "buf-b64");
511 g_assert_cmpint(count, ==, 0);
512 g_assert(eof);
513 g_assert_cmpstr(b64, ==, "");
514 qobject_unref(ret);
516 /* seek */
517 ret = qmp_fd(fixture->fd,
518 "{'execute': 'guest-file-seek',"
519 " 'arguments': { 'handle': %" PRId64 ", "
520 " 'offset': %d, 'whence': %s } }",
521 id, 6, "set");
522 qmp_assert_no_error(ret);
523 val = qdict_get_qdict(ret, "return");
524 count = qdict_get_int(val, "position");
525 eof = qdict_get_bool(val, "eof");
526 g_assert_cmpint(count, ==, 6);
527 g_assert(!eof);
528 qobject_unref(ret);
530 /* partial read */
531 ret = qmp_fd(fixture->fd,
532 "{'execute': 'guest-file-read',"
533 " 'arguments': { 'handle': %" PRId64 "} }",
534 id);
535 val = qdict_get_qdict(ret, "return");
536 count = qdict_get_int(val, "count");
537 eof = qdict_get_bool(val, "eof");
538 b64 = qdict_get_str(val, "buf-b64");
539 g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
540 g_assert(eof);
541 dec = g_base64_decode(b64, &count);
542 g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
543 g_assert_cmpmem(dec, count, helloworld + 6, sizeof(helloworld) - 6);
544 g_free(dec);
546 qobject_unref(ret);
548 /* close */
549 ret = qmp_fd(fixture->fd,
550 "{'execute': 'guest-file-close',"
551 " 'arguments': {'handle': %" PRId64 "} }",
552 id);
553 qobject_unref(ret);
556 static void test_qga_file_write_read(gconstpointer fix)
558 const TestFixture *fixture = fix;
559 const unsigned char helloworld[] = "Hello World!\n";
560 const char *b64;
561 gchar *enc;
562 QDict *ret, *val;
563 int64_t id, eof;
564 gsize count;
566 /* open */
567 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
568 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
569 g_assert_nonnull(ret);
570 qmp_assert_no_error(ret);
571 id = qdict_get_int(ret, "return");
572 qobject_unref(ret);
574 enc = g_base64_encode(helloworld, sizeof(helloworld));
575 /* write */
576 ret = qmp_fd(fixture->fd,
577 "{'execute': 'guest-file-write',"
578 " 'arguments': { 'handle': %" PRId64 ","
579 " 'buf-b64': %s } }", id, enc);
580 g_assert_nonnull(ret);
581 qmp_assert_no_error(ret);
583 val = qdict_get_qdict(ret, "return");
584 count = qdict_get_int(val, "count");
585 eof = qdict_get_bool(val, "eof");
586 g_assert_cmpint(count, ==, sizeof(helloworld));
587 g_assert_cmpint(eof, ==, 0);
588 qobject_unref(ret);
590 /* read (check implicit flush) */
591 ret = qmp_fd(fixture->fd,
592 "{'execute': 'guest-file-read',"
593 " 'arguments': { 'handle': %" PRId64 "} }",
594 id);
595 val = qdict_get_qdict(ret, "return");
596 count = qdict_get_int(val, "count");
597 eof = qdict_get_bool(val, "eof");
598 b64 = qdict_get_str(val, "buf-b64");
599 g_assert_cmpint(count, ==, 0);
600 g_assert(eof);
601 g_assert_cmpstr(b64, ==, "");
602 qobject_unref(ret);
604 /* seek to 0 */
605 ret = qmp_fd(fixture->fd,
606 "{'execute': 'guest-file-seek',"
607 " 'arguments': { 'handle': %" PRId64 ", "
608 " 'offset': %d, 'whence': %s } }",
609 id, 0, "set");
610 qmp_assert_no_error(ret);
611 val = qdict_get_qdict(ret, "return");
612 count = qdict_get_int(val, "position");
613 eof = qdict_get_bool(val, "eof");
614 g_assert_cmpint(count, ==, 0);
615 g_assert(!eof);
616 qobject_unref(ret);
618 /* read */
619 ret = qmp_fd(fixture->fd,
620 "{'execute': 'guest-file-read',"
621 " 'arguments': { 'handle': %" PRId64 "} }",
622 id);
623 val = qdict_get_qdict(ret, "return");
624 count = qdict_get_int(val, "count");
625 eof = qdict_get_bool(val, "eof");
626 b64 = qdict_get_str(val, "buf-b64");
627 g_assert_cmpint(count, ==, sizeof(helloworld));
628 g_assert(eof);
629 g_assert_cmpstr(b64, ==, enc);
630 qobject_unref(ret);
631 g_free(enc);
633 /* close */
634 ret = qmp_fd(fixture->fd,
635 "{'execute': 'guest-file-close',"
636 " 'arguments': {'handle': %" PRId64 "} }",
637 id);
638 qobject_unref(ret);
641 static void test_qga_get_time(gconstpointer fix)
643 const TestFixture *fixture = fix;
644 QDict *ret;
645 int64_t time;
647 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
648 g_assert_nonnull(ret);
649 qmp_assert_no_error(ret);
651 time = qdict_get_int(ret, "return");
652 g_assert_cmpint(time, >, 0);
654 qobject_unref(ret);
657 static void test_qga_blacklist(gconstpointer data)
659 TestFixture fix;
660 QDict *ret, *error;
661 const gchar *class, *desc;
663 fixture_setup(&fix, "-b guest-ping,guest-get-time", NULL);
665 /* check blacklist */
666 ret = qmp_fd(fix.fd, "{'execute': 'guest-ping'}");
667 g_assert_nonnull(ret);
668 error = qdict_get_qdict(ret, "error");
669 class = qdict_get_try_str(error, "class");
670 desc = qdict_get_try_str(error, "desc");
671 g_assert_cmpstr(class, ==, "CommandNotFound");
672 g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
673 qobject_unref(ret);
675 ret = qmp_fd(fix.fd, "{'execute': 'guest-get-time'}");
676 g_assert_nonnull(ret);
677 error = qdict_get_qdict(ret, "error");
678 class = qdict_get_try_str(error, "class");
679 desc = qdict_get_try_str(error, "desc");
680 g_assert_cmpstr(class, ==, "CommandNotFound");
681 g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
682 qobject_unref(ret);
684 /* check something work */
685 ret = qmp_fd(fix.fd, "{'execute': 'guest-get-fsinfo'}");
686 qmp_assert_no_error(ret);
687 qobject_unref(ret);
689 fixture_tear_down(&fix, NULL);
692 static void test_qga_config(gconstpointer data)
694 GError *error = NULL;
695 char *cwd, *cmd, *out, *err, *str, **strv, **argv = NULL;
696 char *env[2];
697 int status;
698 gsize n;
699 GKeyFile *kf;
701 cwd = g_get_current_dir();
702 cmd = g_strdup_printf("%s%cqemu-ga -D",
703 cwd, G_DIR_SEPARATOR);
704 g_free(cwd);
705 g_shell_parse_argv(cmd, NULL, &argv, &error);
706 g_free(cmd);
707 g_assert_no_error(error);
709 env[0] = g_strdup_printf("QGA_CONF=tests%cdata%ctest-qga-config",
710 G_DIR_SEPARATOR, G_DIR_SEPARATOR);
711 env[1] = NULL;
712 g_spawn_sync(NULL, argv, env, 0,
713 NULL, NULL, &out, &err, &status, &error);
714 g_strfreev(argv);
716 g_assert_no_error(error);
717 g_assert_cmpstr(err, ==, "");
718 g_assert_cmpint(status, ==, 0);
720 kf = g_key_file_new();
721 g_key_file_load_from_data(kf, out, -1, G_KEY_FILE_NONE, &error);
722 g_assert_no_error(error);
724 str = g_key_file_get_start_group(kf);
725 g_assert_cmpstr(str, ==, "general");
726 g_free(str);
728 g_assert_false(g_key_file_get_boolean(kf, "general", "daemon", &error));
729 g_assert_no_error(error);
731 str = g_key_file_get_string(kf, "general", "method", &error);
732 g_assert_no_error(error);
733 g_assert_cmpstr(str, ==, "virtio-serial");
734 g_free(str);
736 str = g_key_file_get_string(kf, "general", "path", &error);
737 g_assert_no_error(error);
738 g_assert_cmpstr(str, ==, "/path/to/org.qemu.guest_agent.0");
739 g_free(str);
741 str = g_key_file_get_string(kf, "general", "pidfile", &error);
742 g_assert_no_error(error);
743 g_assert_cmpstr(str, ==, "/var/foo/qemu-ga.pid");
744 g_free(str);
746 str = g_key_file_get_string(kf, "general", "statedir", &error);
747 g_assert_no_error(error);
748 g_assert_cmpstr(str, ==, "/var/state");
749 g_free(str);
751 g_assert_true(g_key_file_get_boolean(kf, "general", "verbose", &error));
752 g_assert_no_error(error);
754 strv = g_key_file_get_string_list(kf, "general", "blacklist", &n, &error);
755 g_assert_cmpint(n, ==, 2);
756 g_assert_true(g_strv_contains((const char * const *)strv,
757 "guest-ping"));
758 g_assert_true(g_strv_contains((const char * const *)strv,
759 "guest-get-time"));
760 g_assert_no_error(error);
761 g_strfreev(strv);
763 g_free(out);
764 g_free(err);
765 g_free(env[0]);
766 g_key_file_free(kf);
769 static void test_qga_fsfreeze_status(gconstpointer fix)
771 const TestFixture *fixture = fix;
772 QDict *ret;
773 const gchar *status;
775 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-status'}");
776 g_assert_nonnull(ret);
777 qmp_assert_no_error(ret);
779 status = qdict_get_try_str(ret, "return");
780 g_assert_cmpstr(status, ==, "thawed");
782 qobject_unref(ret);
785 static void test_qga_guest_exec(gconstpointer fix)
787 const TestFixture *fixture = fix;
788 QDict *ret, *val;
789 const gchar *out;
790 guchar *decoded;
791 int64_t pid, now, exitcode;
792 gsize len;
793 bool exited;
795 /* exec 'echo foo bar' */
796 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
797 " 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ],"
798 " 'capture-output': true } }");
799 g_assert_nonnull(ret);
800 qmp_assert_no_error(ret);
801 val = qdict_get_qdict(ret, "return");
802 pid = qdict_get_int(val, "pid");
803 g_assert_cmpint(pid, >, 0);
804 qobject_unref(ret);
806 /* wait for completion */
807 now = g_get_monotonic_time();
808 do {
809 ret = qmp_fd(fixture->fd,
810 "{'execute': 'guest-exec-status',"
811 " 'arguments': { 'pid': %" PRId64 " } }", pid);
812 g_assert_nonnull(ret);
813 val = qdict_get_qdict(ret, "return");
814 exited = qdict_get_bool(val, "exited");
815 if (!exited) {
816 qobject_unref(ret);
818 } while (!exited &&
819 g_get_monotonic_time() < now + 5 * G_TIME_SPAN_SECOND);
820 g_assert(exited);
822 /* check stdout */
823 exitcode = qdict_get_int(val, "exitcode");
824 g_assert_cmpint(exitcode, ==, 0);
825 out = qdict_get_str(val, "out-data");
826 decoded = g_base64_decode(out, &len);
827 g_assert_cmpint(len, ==, 12);
828 g_assert_cmpstr((char *)decoded, ==, "\" test_str \"");
829 g_free(decoded);
830 qobject_unref(ret);
833 static void test_qga_guest_exec_invalid(gconstpointer fix)
835 const TestFixture *fixture = fix;
836 QDict *ret, *error;
837 const gchar *class, *desc;
839 /* invalid command */
840 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
841 " 'path': '/bin/invalid-cmd42' } }");
842 g_assert_nonnull(ret);
843 error = qdict_get_qdict(ret, "error");
844 g_assert_nonnull(error);
845 class = qdict_get_str(error, "class");
846 desc = qdict_get_str(error, "desc");
847 g_assert_cmpstr(class, ==, "GenericError");
848 g_assert_cmpint(strlen(desc), >, 0);
849 qobject_unref(ret);
851 /* invalid pid */
852 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec-status',"
853 " 'arguments': { 'pid': 0 } }");
854 g_assert_nonnull(ret);
855 error = qdict_get_qdict(ret, "error");
856 g_assert_nonnull(error);
857 class = qdict_get_str(error, "class");
858 desc = qdict_get_str(error, "desc");
859 g_assert_cmpstr(class, ==, "GenericError");
860 g_assert_cmpint(strlen(desc), >, 0);
861 qobject_unref(ret);
864 static void test_qga_guest_get_host_name(gconstpointer fix)
866 const TestFixture *fixture = fix;
867 QDict *ret, *val;
869 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-host-name'}");
870 g_assert_nonnull(ret);
871 qmp_assert_no_error(ret);
873 val = qdict_get_qdict(ret, "return");
874 g_assert(qdict_haskey(val, "host-name"));
876 qobject_unref(ret);
879 static void test_qga_guest_get_timezone(gconstpointer fix)
881 const TestFixture *fixture = fix;
882 QDict *ret, *val;
884 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-timezone'}");
885 g_assert_nonnull(ret);
886 qmp_assert_no_error(ret);
888 /* Make sure there's at least offset */
889 val = qdict_get_qdict(ret, "return");
890 g_assert(qdict_haskey(val, "offset"));
892 qobject_unref(ret);
895 static void test_qga_guest_get_users(gconstpointer fix)
897 const TestFixture *fixture = fix;
898 QDict *ret;
899 QList *val;
901 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-users'}");
902 g_assert_nonnull(ret);
903 qmp_assert_no_error(ret);
905 /* There is not much to test here */
906 val = qdict_get_qlist(ret, "return");
907 g_assert_nonnull(val);
909 qobject_unref(ret);
912 static void test_qga_guest_get_osinfo(gconstpointer data)
914 TestFixture fixture;
915 const gchar *str;
916 gchar *cwd, *env[2];
917 QDict *ret, *val;
919 cwd = g_get_current_dir();
920 env[0] = g_strdup_printf(
921 "QGA_OS_RELEASE=%s%ctests%cdata%ctest-qga-os-release",
922 cwd, G_DIR_SEPARATOR, G_DIR_SEPARATOR, G_DIR_SEPARATOR);
923 env[1] = NULL;
924 g_free(cwd);
925 fixture_setup(&fixture, NULL, env);
927 ret = qmp_fd(fixture.fd, "{'execute': 'guest-get-osinfo'}");
928 g_assert_nonnull(ret);
929 qmp_assert_no_error(ret);
931 val = qdict_get_qdict(ret, "return");
933 str = qdict_get_try_str(val, "id");
934 g_assert_nonnull(str);
935 g_assert_cmpstr(str, ==, "qemu-ga-test");
937 str = qdict_get_try_str(val, "name");
938 g_assert_nonnull(str);
939 g_assert_cmpstr(str, ==, "QEMU-GA");
941 str = qdict_get_try_str(val, "pretty-name");
942 g_assert_nonnull(str);
943 g_assert_cmpstr(str, ==, "QEMU Guest Agent test");
945 str = qdict_get_try_str(val, "version");
946 g_assert_nonnull(str);
947 g_assert_cmpstr(str, ==, "Test 1");
949 str = qdict_get_try_str(val, "version-id");
950 g_assert_nonnull(str);
951 g_assert_cmpstr(str, ==, "1");
953 str = qdict_get_try_str(val, "variant");
954 g_assert_nonnull(str);
955 g_assert_cmpstr(str, ==, "Unit test \"'$`\\ and \\\\ etc.");
957 str = qdict_get_try_str(val, "variant-id");
958 g_assert_nonnull(str);
959 g_assert_cmpstr(str, ==, "unit-test");
961 qobject_unref(ret);
962 g_free(env[0]);
963 fixture_tear_down(&fixture, NULL);
966 int main(int argc, char **argv)
968 TestFixture fix;
969 int ret;
971 setlocale (LC_ALL, "");
972 g_test_init(&argc, &argv, NULL);
973 fixture_setup(&fix, NULL, NULL);
975 g_test_add_data_func("/qga/sync-delimited", &fix, test_qga_sync_delimited);
976 g_test_add_data_func("/qga/sync", &fix, test_qga_sync);
977 g_test_add_data_func("/qga/ping", &fix, test_qga_ping);
978 g_test_add_data_func("/qga/info", &fix, test_qga_info);
979 g_test_add_data_func("/qga/network-get-interfaces", &fix,
980 test_qga_network_get_interfaces);
981 if (!access("/sys/devices/system/cpu/cpu0", F_OK)) {
982 g_test_add_data_func("/qga/get-vcpus", &fix, test_qga_get_vcpus);
984 g_test_add_data_func("/qga/get-fsinfo", &fix, test_qga_get_fsinfo);
985 g_test_add_data_func("/qga/get-memory-block-info", &fix,
986 test_qga_get_memory_block_info);
987 g_test_add_data_func("/qga/get-memory-blocks", &fix,
988 test_qga_get_memory_blocks);
989 g_test_add_data_func("/qga/file-ops", &fix, test_qga_file_ops);
990 g_test_add_data_func("/qga/file-write-read", &fix, test_qga_file_write_read);
991 g_test_add_data_func("/qga/get-time", &fix, test_qga_get_time);
992 g_test_add_data_func("/qga/id", &fix, test_qga_id);
993 g_test_add_data_func("/qga/invalid-oob", &fix, test_qga_invalid_oob);
994 g_test_add_data_func("/qga/invalid-cmd", &fix, test_qga_invalid_cmd);
995 g_test_add_data_func("/qga/invalid-args", &fix, test_qga_invalid_args);
996 g_test_add_data_func("/qga/fsfreeze-status", &fix,
997 test_qga_fsfreeze_status);
999 g_test_add_data_func("/qga/blacklist", NULL, test_qga_blacklist);
1000 g_test_add_data_func("/qga/config", NULL, test_qga_config);
1001 g_test_add_data_func("/qga/guest-exec", &fix, test_qga_guest_exec);
1002 g_test_add_data_func("/qga/guest-exec-invalid", &fix,
1003 test_qga_guest_exec_invalid);
1004 g_test_add_data_func("/qga/guest-get-osinfo", &fix,
1005 test_qga_guest_get_osinfo);
1006 g_test_add_data_func("/qga/guest-get-host-name", &fix,
1007 test_qga_guest_get_host_name);
1008 g_test_add_data_func("/qga/guest-get-timezone", &fix,
1009 test_qga_guest_get_timezone);
1010 g_test_add_data_func("/qga/guest-get-users", &fix,
1011 test_qga_guest_get_users);
1013 ret = g_test_run();
1015 fixture_tear_down(&fix, NULL);
1017 return ret;