Merge remote-tracking branch 'remotes/jnsnow/tags/ide-pull-request' into staging
[qemu/kevin.git] / tests / test-qga.c
blobe6ab788f31a0e283ac8bf3e8b4b801d1a44e91af
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_random_int();
147 unsigned char c;
148 QDict *ret;
149 gchar *cmd;
151 cmd = g_strdup_printf("\xff{'execute': 'guest-sync-delimited',"
152 " 'arguments': {'id': %u } }", r);
153 qmp_fd_send(fixture->fd, cmd);
154 g_free(cmd);
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 QDECREF(ret);
186 static void test_qga_sync(gconstpointer fix)
188 const TestFixture *fixture = fix;
189 guint32 v, r = g_random_int();
190 QDict *ret;
191 gchar *cmd;
194 * TODO guest-sync is inherently limited: we cannot distinguish
195 * failure caused by reacting to garbage on the wire prior to this
196 * command, from failure of this actual command. Clients are
197 * supposed to be able to send a raw '\xff' byte to at least
198 * re-synchronize the server's parser prior to this command, but
199 * we are not in a position to test that here because (at least
200 * for now) it causes the server to issue an error message about
201 * invalid JSON. Testing of '\xff' handling is done in
202 * guest-sync-delimited instead.
204 cmd = g_strdup_printf("{'execute': 'guest-sync',"
205 " 'arguments': {'id': %u } }", r);
206 ret = qmp_fd(fixture->fd, cmd);
207 g_free(cmd);
209 g_assert_nonnull(ret);
210 qmp_assert_no_error(ret);
212 v = qdict_get_int(ret, "return");
213 g_assert_cmpint(r, ==, v);
215 QDECREF(ret);
218 static void test_qga_ping(gconstpointer fix)
220 const TestFixture *fixture = fix;
221 QDict *ret;
223 ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping'}");
224 g_assert_nonnull(ret);
225 qmp_assert_no_error(ret);
227 QDECREF(ret);
230 static void test_qga_invalid_args(gconstpointer fix)
232 const TestFixture *fixture = fix;
233 QDict *ret, *error;
234 const gchar *class, *desc;
236 ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping', "
237 "'arguments': {'foo': 42 }}");
238 g_assert_nonnull(ret);
240 error = qdict_get_qdict(ret, "error");
241 class = qdict_get_try_str(error, "class");
242 desc = qdict_get_try_str(error, "desc");
244 g_assert_cmpstr(class, ==, "GenericError");
245 g_assert_cmpstr(desc, ==, "Parameter 'foo' is unexpected");
247 QDECREF(ret);
250 static void test_qga_invalid_cmd(gconstpointer fix)
252 const TestFixture *fixture = fix;
253 QDict *ret, *error;
254 const gchar *class, *desc;
256 ret = qmp_fd(fixture->fd, "{'execute': 'guest-invalid-cmd'}");
257 g_assert_nonnull(ret);
259 error = qdict_get_qdict(ret, "error");
260 class = qdict_get_try_str(error, "class");
261 desc = qdict_get_try_str(error, "desc");
263 g_assert_cmpstr(class, ==, "CommandNotFound");
264 g_assert_cmpint(strlen(desc), >, 0);
266 QDECREF(ret);
269 static void test_qga_info(gconstpointer fix)
271 const TestFixture *fixture = fix;
272 QDict *ret, *val;
273 const gchar *version;
275 ret = qmp_fd(fixture->fd, "{'execute': 'guest-info'}");
276 g_assert_nonnull(ret);
277 qmp_assert_no_error(ret);
279 val = qdict_get_qdict(ret, "return");
280 version = qdict_get_try_str(val, "version");
281 g_assert_cmpstr(version, ==, QEMU_VERSION);
283 QDECREF(ret);
286 static void test_qga_get_vcpus(gconstpointer fix)
288 const TestFixture *fixture = fix;
289 QDict *ret;
290 QList *list;
291 const QListEntry *entry;
293 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-vcpus'}");
294 g_assert_nonnull(ret);
295 qmp_assert_no_error(ret);
297 /* check there is at least a cpu */
298 list = qdict_get_qlist(ret, "return");
299 entry = qlist_first(list);
300 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "online"));
301 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "logical-id"));
303 QDECREF(ret);
306 static void test_qga_get_fsinfo(gconstpointer fix)
308 const TestFixture *fixture = fix;
309 QDict *ret;
310 QList *list;
311 const QListEntry *entry;
313 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-fsinfo'}");
314 g_assert_nonnull(ret);
315 qmp_assert_no_error(ret);
317 /* sanity-check the response if there are any filesystems */
318 list = qdict_get_qlist(ret, "return");
319 entry = qlist_first(list);
320 if (entry) {
321 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "name"));
322 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "mountpoint"));
323 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "type"));
324 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "disk"));
327 QDECREF(ret);
330 static void test_qga_get_memory_block_info(gconstpointer fix)
332 const TestFixture *fixture = fix;
333 QDict *ret, *val;
334 int64_t size;
336 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-block-info'}");
337 g_assert_nonnull(ret);
339 /* some systems might not expose memory block info in sysfs */
340 if (!qdict_haskey(ret, "error")) {
341 /* check there is at least some memory */
342 val = qdict_get_qdict(ret, "return");
343 size = qdict_get_int(val, "size");
344 g_assert_cmpint(size, >, 0);
347 QDECREF(ret);
350 static void test_qga_get_memory_blocks(gconstpointer fix)
352 const TestFixture *fixture = fix;
353 QDict *ret;
354 QList *list;
355 const QListEntry *entry;
357 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-blocks'}");
358 g_assert_nonnull(ret);
360 /* some systems might not expose memory block info in sysfs */
361 if (!qdict_haskey(ret, "error")) {
362 list = qdict_get_qlist(ret, "return");
363 entry = qlist_first(list);
364 /* newer versions of qga may return empty list without error */
365 if (entry) {
366 g_assert(qdict_haskey(qobject_to(QDict, entry->value),
367 "phys-index"));
368 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "online"));
372 QDECREF(ret);
375 static void test_qga_network_get_interfaces(gconstpointer fix)
377 const TestFixture *fixture = fix;
378 QDict *ret;
379 QList *list;
380 const QListEntry *entry;
382 ret = qmp_fd(fixture->fd, "{'execute': 'guest-network-get-interfaces'}");
383 g_assert_nonnull(ret);
384 qmp_assert_no_error(ret);
386 /* check there is at least an interface */
387 list = qdict_get_qlist(ret, "return");
388 entry = qlist_first(list);
389 g_assert(qdict_haskey(qobject_to(QDict, entry->value), "name"));
391 QDECREF(ret);
394 static void test_qga_file_ops(gconstpointer fix)
396 const TestFixture *fixture = fix;
397 const unsigned char helloworld[] = "Hello World!\n";
398 const char *b64;
399 gchar *cmd, *path, *enc;
400 unsigned char *dec;
401 QDict *ret, *val;
402 int64_t id, eof;
403 gsize count;
404 FILE *f;
405 char tmp[100];
407 /* open */
408 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
409 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
410 g_assert_nonnull(ret);
411 qmp_assert_no_error(ret);
412 id = qdict_get_int(ret, "return");
413 QDECREF(ret);
415 enc = g_base64_encode(helloworld, sizeof(helloworld));
416 /* write */
417 cmd = g_strdup_printf("{'execute': 'guest-file-write',"
418 " 'arguments': { 'handle': %" PRId64 ","
419 " 'buf-b64': '%s' } }", id, enc);
420 ret = qmp_fd(fixture->fd, cmd);
421 g_assert_nonnull(ret);
422 qmp_assert_no_error(ret);
424 val = qdict_get_qdict(ret, "return");
425 count = qdict_get_int(val, "count");
426 eof = qdict_get_bool(val, "eof");
427 g_assert_cmpint(count, ==, sizeof(helloworld));
428 g_assert_cmpint(eof, ==, 0);
429 QDECREF(ret);
430 g_free(cmd);
432 /* flush */
433 cmd = g_strdup_printf("{'execute': 'guest-file-flush',"
434 " 'arguments': {'handle': %" PRId64 "} }",
435 id);
436 ret = qmp_fd(fixture->fd, cmd);
437 QDECREF(ret);
438 g_free(cmd);
440 /* close */
441 cmd = g_strdup_printf("{'execute': 'guest-file-close',"
442 " 'arguments': {'handle': %" PRId64 "} }",
443 id);
444 ret = qmp_fd(fixture->fd, cmd);
445 QDECREF(ret);
446 g_free(cmd);
448 /* check content */
449 path = g_build_filename(fixture->test_dir, "foo", NULL);
450 f = fopen(path, "r");
451 g_free(path);
452 g_assert_nonnull(f);
453 count = fread(tmp, 1, sizeof(tmp), f);
454 g_assert_cmpint(count, ==, sizeof(helloworld));
455 tmp[count] = 0;
456 g_assert_cmpstr(tmp, ==, (char *)helloworld);
457 fclose(f);
459 /* open */
460 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
461 " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
462 g_assert_nonnull(ret);
463 qmp_assert_no_error(ret);
464 id = qdict_get_int(ret, "return");
465 QDECREF(ret);
467 /* read */
468 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
469 " 'arguments': { 'handle': %" PRId64 "} }",
470 id);
471 ret = qmp_fd(fixture->fd, cmd);
472 val = qdict_get_qdict(ret, "return");
473 count = qdict_get_int(val, "count");
474 eof = qdict_get_bool(val, "eof");
475 b64 = qdict_get_str(val, "buf-b64");
476 g_assert_cmpint(count, ==, sizeof(helloworld));
477 g_assert(eof);
478 g_assert_cmpstr(b64, ==, enc);
480 QDECREF(ret);
481 g_free(cmd);
482 g_free(enc);
484 /* read eof */
485 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
486 " 'arguments': { 'handle': %" PRId64 "} }",
487 id);
488 ret = qmp_fd(fixture->fd, cmd);
489 val = qdict_get_qdict(ret, "return");
490 count = qdict_get_int(val, "count");
491 eof = qdict_get_bool(val, "eof");
492 b64 = qdict_get_str(val, "buf-b64");
493 g_assert_cmpint(count, ==, 0);
494 g_assert(eof);
495 g_assert_cmpstr(b64, ==, "");
496 QDECREF(ret);
497 g_free(cmd);
499 /* seek */
500 cmd = g_strdup_printf("{'execute': 'guest-file-seek',"
501 " 'arguments': { 'handle': %" PRId64 ", "
502 " 'offset': %d, 'whence': '%s' } }",
503 id, 6, "set");
504 ret = qmp_fd(fixture->fd, cmd);
505 qmp_assert_no_error(ret);
506 val = qdict_get_qdict(ret, "return");
507 count = qdict_get_int(val, "position");
508 eof = qdict_get_bool(val, "eof");
509 g_assert_cmpint(count, ==, 6);
510 g_assert(!eof);
511 QDECREF(ret);
512 g_free(cmd);
514 /* partial read */
515 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
516 " 'arguments': { 'handle': %" PRId64 "} }",
517 id);
518 ret = qmp_fd(fixture->fd, cmd);
519 val = qdict_get_qdict(ret, "return");
520 count = qdict_get_int(val, "count");
521 eof = qdict_get_bool(val, "eof");
522 b64 = qdict_get_str(val, "buf-b64");
523 g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
524 g_assert(eof);
525 dec = g_base64_decode(b64, &count);
526 g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
527 g_assert_cmpmem(dec, count, helloworld + 6, sizeof(helloworld) - 6);
528 g_free(dec);
530 QDECREF(ret);
531 g_free(cmd);
533 /* close */
534 cmd = g_strdup_printf("{'execute': 'guest-file-close',"
535 " 'arguments': {'handle': %" PRId64 "} }",
536 id);
537 ret = qmp_fd(fixture->fd, cmd);
538 QDECREF(ret);
539 g_free(cmd);
542 static void test_qga_file_write_read(gconstpointer fix)
544 const TestFixture *fixture = fix;
545 const unsigned char helloworld[] = "Hello World!\n";
546 const char *b64;
547 gchar *cmd, *enc;
548 QDict *ret, *val;
549 int64_t id, eof;
550 gsize count;
552 /* open */
553 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
554 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
555 g_assert_nonnull(ret);
556 qmp_assert_no_error(ret);
557 id = qdict_get_int(ret, "return");
558 QDECREF(ret);
560 enc = g_base64_encode(helloworld, sizeof(helloworld));
561 /* write */
562 cmd = g_strdup_printf("{'execute': 'guest-file-write',"
563 " 'arguments': { 'handle': %" PRId64 ","
564 " 'buf-b64': '%s' } }", id, enc);
565 ret = qmp_fd(fixture->fd, cmd);
566 g_assert_nonnull(ret);
567 qmp_assert_no_error(ret);
569 val = qdict_get_qdict(ret, "return");
570 count = qdict_get_int(val, "count");
571 eof = qdict_get_bool(val, "eof");
572 g_assert_cmpint(count, ==, sizeof(helloworld));
573 g_assert_cmpint(eof, ==, 0);
574 QDECREF(ret);
575 g_free(cmd);
577 /* read (check implicit flush) */
578 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
579 " 'arguments': { 'handle': %" PRId64 "} }",
580 id);
581 ret = qmp_fd(fixture->fd, cmd);
582 val = qdict_get_qdict(ret, "return");
583 count = qdict_get_int(val, "count");
584 eof = qdict_get_bool(val, "eof");
585 b64 = qdict_get_str(val, "buf-b64");
586 g_assert_cmpint(count, ==, 0);
587 g_assert(eof);
588 g_assert_cmpstr(b64, ==, "");
589 QDECREF(ret);
590 g_free(cmd);
592 /* seek to 0 */
593 cmd = g_strdup_printf("{'execute': 'guest-file-seek',"
594 " 'arguments': { 'handle': %" PRId64 ", "
595 " 'offset': %d, 'whence': '%s' } }",
596 id, 0, "set");
597 ret = qmp_fd(fixture->fd, cmd);
598 qmp_assert_no_error(ret);
599 val = qdict_get_qdict(ret, "return");
600 count = qdict_get_int(val, "position");
601 eof = qdict_get_bool(val, "eof");
602 g_assert_cmpint(count, ==, 0);
603 g_assert(!eof);
604 QDECREF(ret);
605 g_free(cmd);
607 /* read */
608 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
609 " 'arguments': { 'handle': %" PRId64 "} }",
610 id);
611 ret = qmp_fd(fixture->fd, cmd);
612 val = qdict_get_qdict(ret, "return");
613 count = qdict_get_int(val, "count");
614 eof = qdict_get_bool(val, "eof");
615 b64 = qdict_get_str(val, "buf-b64");
616 g_assert_cmpint(count, ==, sizeof(helloworld));
617 g_assert(eof);
618 g_assert_cmpstr(b64, ==, enc);
619 QDECREF(ret);
620 g_free(cmd);
621 g_free(enc);
623 /* close */
624 cmd = g_strdup_printf("{'execute': 'guest-file-close',"
625 " 'arguments': {'handle': %" PRId64 "} }",
626 id);
627 ret = qmp_fd(fixture->fd, cmd);
628 QDECREF(ret);
629 g_free(cmd);
632 static void test_qga_get_time(gconstpointer fix)
634 const TestFixture *fixture = fix;
635 QDict *ret;
636 int64_t time;
638 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
639 g_assert_nonnull(ret);
640 qmp_assert_no_error(ret);
642 time = qdict_get_int(ret, "return");
643 g_assert_cmpint(time, >, 0);
645 QDECREF(ret);
648 static void test_qga_blacklist(gconstpointer data)
650 TestFixture fix;
651 QDict *ret, *error;
652 const gchar *class, *desc;
654 fixture_setup(&fix, "-b guest-ping,guest-get-time", NULL);
656 /* check blacklist */
657 ret = qmp_fd(fix.fd, "{'execute': 'guest-ping'}");
658 g_assert_nonnull(ret);
659 error = qdict_get_qdict(ret, "error");
660 class = qdict_get_try_str(error, "class");
661 desc = qdict_get_try_str(error, "desc");
662 g_assert_cmpstr(class, ==, "GenericError");
663 g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
664 QDECREF(ret);
666 ret = qmp_fd(fix.fd, "{'execute': 'guest-get-time'}");
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, ==, "GenericError");
672 g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
673 QDECREF(ret);
675 /* check something work */
676 ret = qmp_fd(fix.fd, "{'execute': 'guest-get-fsinfo'}");
677 qmp_assert_no_error(ret);
678 QDECREF(ret);
680 fixture_tear_down(&fix, NULL);
683 static void test_qga_config(gconstpointer data)
685 GError *error = NULL;
686 char *cwd, *cmd, *out, *err, *str, **strv, **argv = NULL;
687 char *env[2];
688 int status;
689 gsize n;
690 GKeyFile *kf;
692 cwd = g_get_current_dir();
693 cmd = g_strdup_printf("%s%cqemu-ga -D",
694 cwd, G_DIR_SEPARATOR);
695 g_free(cwd);
696 g_shell_parse_argv(cmd, NULL, &argv, &error);
697 g_free(cmd);
698 g_assert_no_error(error);
700 env[0] = g_strdup_printf("QGA_CONF=tests%cdata%ctest-qga-config",
701 G_DIR_SEPARATOR, G_DIR_SEPARATOR);
702 env[1] = NULL;
703 g_spawn_sync(NULL, argv, env, 0,
704 NULL, NULL, &out, &err, &status, &error);
705 g_strfreev(argv);
707 g_assert_no_error(error);
708 g_assert_cmpstr(err, ==, "");
709 g_assert_cmpint(status, ==, 0);
711 kf = g_key_file_new();
712 g_key_file_load_from_data(kf, out, -1, G_KEY_FILE_NONE, &error);
713 g_assert_no_error(error);
715 str = g_key_file_get_start_group(kf);
716 g_assert_cmpstr(str, ==, "general");
717 g_free(str);
719 g_assert_false(g_key_file_get_boolean(kf, "general", "daemon", &error));
720 g_assert_no_error(error);
722 str = g_key_file_get_string(kf, "general", "method", &error);
723 g_assert_no_error(error);
724 g_assert_cmpstr(str, ==, "virtio-serial");
725 g_free(str);
727 str = g_key_file_get_string(kf, "general", "path", &error);
728 g_assert_no_error(error);
729 g_assert_cmpstr(str, ==, "/path/to/org.qemu.guest_agent.0");
730 g_free(str);
732 str = g_key_file_get_string(kf, "general", "pidfile", &error);
733 g_assert_no_error(error);
734 g_assert_cmpstr(str, ==, "/var/foo/qemu-ga.pid");
735 g_free(str);
737 str = g_key_file_get_string(kf, "general", "statedir", &error);
738 g_assert_no_error(error);
739 g_assert_cmpstr(str, ==, "/var/state");
740 g_free(str);
742 g_assert_true(g_key_file_get_boolean(kf, "general", "verbose", &error));
743 g_assert_no_error(error);
745 strv = g_key_file_get_string_list(kf, "general", "blacklist", &n, &error);
746 g_assert_cmpint(n, ==, 2);
747 #if GLIB_CHECK_VERSION(2, 44, 0)
748 g_assert_true(g_strv_contains((const char * const *)strv,
749 "guest-ping"));
750 g_assert_true(g_strv_contains((const char * const *)strv,
751 "guest-get-time"));
752 #endif
753 g_assert_no_error(error);
754 g_strfreev(strv);
756 g_free(out);
757 g_free(err);
758 g_free(env[0]);
759 g_key_file_free(kf);
762 static void test_qga_fsfreeze_status(gconstpointer fix)
764 const TestFixture *fixture = fix;
765 QDict *ret;
766 const gchar *status;
768 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-status'}");
769 g_assert_nonnull(ret);
770 qmp_assert_no_error(ret);
772 status = qdict_get_try_str(ret, "return");
773 g_assert_cmpstr(status, ==, "thawed");
775 QDECREF(ret);
778 static void test_qga_guest_exec(gconstpointer fix)
780 const TestFixture *fixture = fix;
781 QDict *ret, *val;
782 const gchar *out;
783 guchar *decoded;
784 int64_t pid, now, exitcode;
785 gsize len;
786 bool exited;
787 char *cmd;
789 /* exec 'echo foo bar' */
790 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
791 " 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ],"
792 " 'capture-output': true } }");
793 g_assert_nonnull(ret);
794 qmp_assert_no_error(ret);
795 val = qdict_get_qdict(ret, "return");
796 pid = qdict_get_int(val, "pid");
797 g_assert_cmpint(pid, >, 0);
798 QDECREF(ret);
800 /* wait for completion */
801 now = g_get_monotonic_time();
802 cmd = g_strdup_printf("{'execute': 'guest-exec-status',"
803 " 'arguments': { 'pid': %" PRId64 " } }", pid);
804 do {
805 ret = qmp_fd(fixture->fd, cmd);
806 g_assert_nonnull(ret);
807 val = qdict_get_qdict(ret, "return");
808 exited = qdict_get_bool(val, "exited");
809 if (!exited) {
810 QDECREF(ret);
812 } while (!exited &&
813 g_get_monotonic_time() < now + 5 * G_TIME_SPAN_SECOND);
814 g_assert(exited);
815 g_free(cmd);
817 /* check stdout */
818 exitcode = qdict_get_int(val, "exitcode");
819 g_assert_cmpint(exitcode, ==, 0);
820 out = qdict_get_str(val, "out-data");
821 decoded = g_base64_decode(out, &len);
822 g_assert_cmpint(len, ==, 12);
823 g_assert_cmpstr((char *)decoded, ==, "\" test_str \"");
824 g_free(decoded);
825 QDECREF(ret);
828 static void test_qga_guest_exec_invalid(gconstpointer fix)
830 const TestFixture *fixture = fix;
831 QDict *ret, *error;
832 const gchar *class, *desc;
834 /* invalid command */
835 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
836 " 'path': '/bin/invalid-cmd42' } }");
837 g_assert_nonnull(ret);
838 error = qdict_get_qdict(ret, "error");
839 g_assert_nonnull(error);
840 class = qdict_get_str(error, "class");
841 desc = qdict_get_str(error, "desc");
842 g_assert_cmpstr(class, ==, "GenericError");
843 g_assert_cmpint(strlen(desc), >, 0);
844 QDECREF(ret);
846 /* invalid pid */
847 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec-status',"
848 " 'arguments': { 'pid': 0 } }");
849 g_assert_nonnull(ret);
850 error = qdict_get_qdict(ret, "error");
851 g_assert_nonnull(error);
852 class = qdict_get_str(error, "class");
853 desc = qdict_get_str(error, "desc");
854 g_assert_cmpstr(class, ==, "GenericError");
855 g_assert_cmpint(strlen(desc), >, 0);
856 QDECREF(ret);
859 static void test_qga_guest_get_osinfo(gconstpointer data)
861 TestFixture fixture;
862 const gchar *str;
863 gchar *cwd, *env[2];
864 QDict *ret, *val;
866 cwd = g_get_current_dir();
867 env[0] = g_strdup_printf(
868 "QGA_OS_RELEASE=%s%ctests%cdata%ctest-qga-os-release",
869 cwd, G_DIR_SEPARATOR, G_DIR_SEPARATOR, G_DIR_SEPARATOR);
870 env[1] = NULL;
871 g_free(cwd);
872 fixture_setup(&fixture, NULL, env);
874 ret = qmp_fd(fixture.fd, "{'execute': 'guest-get-osinfo'}");
875 g_assert_nonnull(ret);
876 qmp_assert_no_error(ret);
878 val = qdict_get_qdict(ret, "return");
880 str = qdict_get_try_str(val, "id");
881 g_assert_nonnull(str);
882 g_assert_cmpstr(str, ==, "qemu-ga-test");
884 str = qdict_get_try_str(val, "name");
885 g_assert_nonnull(str);
886 g_assert_cmpstr(str, ==, "QEMU-GA");
888 str = qdict_get_try_str(val, "pretty-name");
889 g_assert_nonnull(str);
890 g_assert_cmpstr(str, ==, "QEMU Guest Agent test");
892 str = qdict_get_try_str(val, "version");
893 g_assert_nonnull(str);
894 g_assert_cmpstr(str, ==, "Test 1");
896 str = qdict_get_try_str(val, "version-id");
897 g_assert_nonnull(str);
898 g_assert_cmpstr(str, ==, "1");
900 str = qdict_get_try_str(val, "variant");
901 g_assert_nonnull(str);
902 g_assert_cmpstr(str, ==, "Unit test \"'$`\\ and \\\\ etc.");
904 str = qdict_get_try_str(val, "variant-id");
905 g_assert_nonnull(str);
906 g_assert_cmpstr(str, ==, "unit-test");
908 QDECREF(ret);
909 g_free(env[0]);
910 fixture_tear_down(&fixture, NULL);
913 int main(int argc, char **argv)
915 TestFixture fix;
916 int ret;
918 setlocale (LC_ALL, "");
919 g_test_init(&argc, &argv, NULL);
920 fixture_setup(&fix, NULL, NULL);
922 g_test_add_data_func("/qga/sync-delimited", &fix, test_qga_sync_delimited);
923 g_test_add_data_func("/qga/sync", &fix, test_qga_sync);
924 g_test_add_data_func("/qga/ping", &fix, test_qga_ping);
925 g_test_add_data_func("/qga/info", &fix, test_qga_info);
926 g_test_add_data_func("/qga/network-get-interfaces", &fix,
927 test_qga_network_get_interfaces);
928 if (!access("/sys/devices/system/cpu/cpu0", F_OK)) {
929 g_test_add_data_func("/qga/get-vcpus", &fix, test_qga_get_vcpus);
931 g_test_add_data_func("/qga/get-fsinfo", &fix, test_qga_get_fsinfo);
932 g_test_add_data_func("/qga/get-memory-block-info", &fix,
933 test_qga_get_memory_block_info);
934 g_test_add_data_func("/qga/get-memory-blocks", &fix,
935 test_qga_get_memory_blocks);
936 g_test_add_data_func("/qga/file-ops", &fix, test_qga_file_ops);
937 g_test_add_data_func("/qga/file-write-read", &fix, test_qga_file_write_read);
938 g_test_add_data_func("/qga/get-time", &fix, test_qga_get_time);
939 g_test_add_data_func("/qga/invalid-cmd", &fix, test_qga_invalid_cmd);
940 g_test_add_data_func("/qga/invalid-args", &fix, test_qga_invalid_args);
941 g_test_add_data_func("/qga/fsfreeze-status", &fix,
942 test_qga_fsfreeze_status);
944 g_test_add_data_func("/qga/blacklist", NULL, test_qga_blacklist);
945 g_test_add_data_func("/qga/config", NULL, test_qga_config);
946 g_test_add_data_func("/qga/guest-exec", &fix, test_qga_guest_exec);
947 g_test_add_data_func("/qga/guest-exec-invalid", &fix,
948 test_qga_guest_exec_invalid);
949 g_test_add_data_func("/qga/guest-get-osinfo", &fix,
950 test_qga_guest_get_osinfo);
952 ret = g_test_run();
954 fixture_tear_down(&fix, NULL);
956 return ret;