sdcard: use a more descriptive label 'unimplemented_spi_cmd'
[qemu/ar7.git] / tests / test-qga.c
blob5c5b661f8a365399fd5c16f75cea0e8dad123e14
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), "phys-index"));
367 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "online"));
371 QDECREF(ret);
374 static void test_qga_network_get_interfaces(gconstpointer fix)
376 const TestFixture *fixture = fix;
377 QDict *ret;
378 QList *list;
379 const QListEntry *entry;
381 ret = qmp_fd(fixture->fd, "{'execute': 'guest-network-get-interfaces'}");
382 g_assert_nonnull(ret);
383 qmp_assert_no_error(ret);
385 /* check there is at least an interface */
386 list = qdict_get_qlist(ret, "return");
387 entry = qlist_first(list);
388 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "name"));
390 QDECREF(ret);
393 static void test_qga_file_ops(gconstpointer fix)
395 const TestFixture *fixture = fix;
396 const unsigned char helloworld[] = "Hello World!\n";
397 const char *b64;
398 gchar *cmd, *path, *enc;
399 unsigned char *dec;
400 QDict *ret, *val;
401 int64_t id, eof;
402 gsize count;
403 FILE *f;
404 char tmp[100];
406 /* open */
407 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
408 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
409 g_assert_nonnull(ret);
410 qmp_assert_no_error(ret);
411 id = qdict_get_int(ret, "return");
412 QDECREF(ret);
414 enc = g_base64_encode(helloworld, sizeof(helloworld));
415 /* write */
416 cmd = g_strdup_printf("{'execute': 'guest-file-write',"
417 " 'arguments': { 'handle': %" PRId64 ","
418 " 'buf-b64': '%s' } }", id, enc);
419 ret = qmp_fd(fixture->fd, cmd);
420 g_assert_nonnull(ret);
421 qmp_assert_no_error(ret);
423 val = qdict_get_qdict(ret, "return");
424 count = qdict_get_int(val, "count");
425 eof = qdict_get_bool(val, "eof");
426 g_assert_cmpint(count, ==, sizeof(helloworld));
427 g_assert_cmpint(eof, ==, 0);
428 QDECREF(ret);
429 g_free(cmd);
431 /* flush */
432 cmd = g_strdup_printf("{'execute': 'guest-file-flush',"
433 " 'arguments': {'handle': %" PRId64 "} }",
434 id);
435 ret = qmp_fd(fixture->fd, cmd);
436 QDECREF(ret);
437 g_free(cmd);
439 /* close */
440 cmd = g_strdup_printf("{'execute': 'guest-file-close',"
441 " 'arguments': {'handle': %" PRId64 "} }",
442 id);
443 ret = qmp_fd(fixture->fd, cmd);
444 QDECREF(ret);
445 g_free(cmd);
447 /* check content */
448 path = g_build_filename(fixture->test_dir, "foo", NULL);
449 f = fopen(path, "r");
450 g_free(path);
451 g_assert_nonnull(f);
452 count = fread(tmp, 1, sizeof(tmp), f);
453 g_assert_cmpint(count, ==, sizeof(helloworld));
454 tmp[count] = 0;
455 g_assert_cmpstr(tmp, ==, (char *)helloworld);
456 fclose(f);
458 /* open */
459 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
460 " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
461 g_assert_nonnull(ret);
462 qmp_assert_no_error(ret);
463 id = qdict_get_int(ret, "return");
464 QDECREF(ret);
466 /* read */
467 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
468 " 'arguments': { 'handle': %" PRId64 "} }",
469 id);
470 ret = qmp_fd(fixture->fd, cmd);
471 val = qdict_get_qdict(ret, "return");
472 count = qdict_get_int(val, "count");
473 eof = qdict_get_bool(val, "eof");
474 b64 = qdict_get_str(val, "buf-b64");
475 g_assert_cmpint(count, ==, sizeof(helloworld));
476 g_assert(eof);
477 g_assert_cmpstr(b64, ==, enc);
479 QDECREF(ret);
480 g_free(cmd);
481 g_free(enc);
483 /* read eof */
484 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
485 " 'arguments': { 'handle': %" PRId64 "} }",
486 id);
487 ret = qmp_fd(fixture->fd, cmd);
488 val = qdict_get_qdict(ret, "return");
489 count = qdict_get_int(val, "count");
490 eof = qdict_get_bool(val, "eof");
491 b64 = qdict_get_str(val, "buf-b64");
492 g_assert_cmpint(count, ==, 0);
493 g_assert(eof);
494 g_assert_cmpstr(b64, ==, "");
495 QDECREF(ret);
496 g_free(cmd);
498 /* seek */
499 cmd = g_strdup_printf("{'execute': 'guest-file-seek',"
500 " 'arguments': { 'handle': %" PRId64 ", "
501 " 'offset': %d, 'whence': '%s' } }",
502 id, 6, "set");
503 ret = qmp_fd(fixture->fd, cmd);
504 qmp_assert_no_error(ret);
505 val = qdict_get_qdict(ret, "return");
506 count = qdict_get_int(val, "position");
507 eof = qdict_get_bool(val, "eof");
508 g_assert_cmpint(count, ==, 6);
509 g_assert(!eof);
510 QDECREF(ret);
511 g_free(cmd);
513 /* partial read */
514 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
515 " 'arguments': { 'handle': %" PRId64 "} }",
516 id);
517 ret = qmp_fd(fixture->fd, cmd);
518 val = qdict_get_qdict(ret, "return");
519 count = qdict_get_int(val, "count");
520 eof = qdict_get_bool(val, "eof");
521 b64 = qdict_get_str(val, "buf-b64");
522 g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
523 g_assert(eof);
524 dec = g_base64_decode(b64, &count);
525 g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
526 g_assert_cmpmem(dec, count, helloworld + 6, sizeof(helloworld) - 6);
527 g_free(dec);
529 QDECREF(ret);
530 g_free(cmd);
532 /* close */
533 cmd = g_strdup_printf("{'execute': 'guest-file-close',"
534 " 'arguments': {'handle': %" PRId64 "} }",
535 id);
536 ret = qmp_fd(fixture->fd, cmd);
537 QDECREF(ret);
538 g_free(cmd);
541 static void test_qga_file_write_read(gconstpointer fix)
543 const TestFixture *fixture = fix;
544 const unsigned char helloworld[] = "Hello World!\n";
545 const char *b64;
546 gchar *cmd, *enc;
547 QDict *ret, *val;
548 int64_t id, eof;
549 gsize count;
551 /* open */
552 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
553 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
554 g_assert_nonnull(ret);
555 qmp_assert_no_error(ret);
556 id = qdict_get_int(ret, "return");
557 QDECREF(ret);
559 enc = g_base64_encode(helloworld, sizeof(helloworld));
560 /* write */
561 cmd = g_strdup_printf("{'execute': 'guest-file-write',"
562 " 'arguments': { 'handle': %" PRId64 ","
563 " 'buf-b64': '%s' } }", id, enc);
564 ret = qmp_fd(fixture->fd, cmd);
565 g_assert_nonnull(ret);
566 qmp_assert_no_error(ret);
568 val = qdict_get_qdict(ret, "return");
569 count = qdict_get_int(val, "count");
570 eof = qdict_get_bool(val, "eof");
571 g_assert_cmpint(count, ==, sizeof(helloworld));
572 g_assert_cmpint(eof, ==, 0);
573 QDECREF(ret);
574 g_free(cmd);
576 /* read (check implicit flush) */
577 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
578 " 'arguments': { 'handle': %" PRId64 "} }",
579 id);
580 ret = qmp_fd(fixture->fd, cmd);
581 val = qdict_get_qdict(ret, "return");
582 count = qdict_get_int(val, "count");
583 eof = qdict_get_bool(val, "eof");
584 b64 = qdict_get_str(val, "buf-b64");
585 g_assert_cmpint(count, ==, 0);
586 g_assert(eof);
587 g_assert_cmpstr(b64, ==, "");
588 QDECREF(ret);
589 g_free(cmd);
591 /* seek to 0 */
592 cmd = g_strdup_printf("{'execute': 'guest-file-seek',"
593 " 'arguments': { 'handle': %" PRId64 ", "
594 " 'offset': %d, 'whence': '%s' } }",
595 id, 0, "set");
596 ret = qmp_fd(fixture->fd, cmd);
597 qmp_assert_no_error(ret);
598 val = qdict_get_qdict(ret, "return");
599 count = qdict_get_int(val, "position");
600 eof = qdict_get_bool(val, "eof");
601 g_assert_cmpint(count, ==, 0);
602 g_assert(!eof);
603 QDECREF(ret);
604 g_free(cmd);
606 /* read */
607 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
608 " 'arguments': { 'handle': %" PRId64 "} }",
609 id);
610 ret = qmp_fd(fixture->fd, cmd);
611 val = qdict_get_qdict(ret, "return");
612 count = qdict_get_int(val, "count");
613 eof = qdict_get_bool(val, "eof");
614 b64 = qdict_get_str(val, "buf-b64");
615 g_assert_cmpint(count, ==, sizeof(helloworld));
616 g_assert(eof);
617 g_assert_cmpstr(b64, ==, enc);
618 QDECREF(ret);
619 g_free(cmd);
620 g_free(enc);
622 /* close */
623 cmd = g_strdup_printf("{'execute': 'guest-file-close',"
624 " 'arguments': {'handle': %" PRId64 "} }",
625 id);
626 ret = qmp_fd(fixture->fd, cmd);
627 QDECREF(ret);
628 g_free(cmd);
631 static void test_qga_get_time(gconstpointer fix)
633 const TestFixture *fixture = fix;
634 QDict *ret;
635 int64_t time;
637 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
638 g_assert_nonnull(ret);
639 qmp_assert_no_error(ret);
641 time = qdict_get_int(ret, "return");
642 g_assert_cmpint(time, >, 0);
644 QDECREF(ret);
647 static void test_qga_blacklist(gconstpointer data)
649 TestFixture fix;
650 QDict *ret, *error;
651 const gchar *class, *desc;
653 fixture_setup(&fix, "-b guest-ping,guest-get-time", NULL);
655 /* check blacklist */
656 ret = qmp_fd(fix.fd, "{'execute': 'guest-ping'}");
657 g_assert_nonnull(ret);
658 error = qdict_get_qdict(ret, "error");
659 class = qdict_get_try_str(error, "class");
660 desc = qdict_get_try_str(error, "desc");
661 g_assert_cmpstr(class, ==, "GenericError");
662 g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
663 QDECREF(ret);
665 ret = qmp_fd(fix.fd, "{'execute': 'guest-get-time'}");
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"));
672 QDECREF(ret);
674 /* check something work */
675 ret = qmp_fd(fix.fd, "{'execute': 'guest-get-fsinfo'}");
676 qmp_assert_no_error(ret);
677 QDECREF(ret);
679 fixture_tear_down(&fix, NULL);
682 static void test_qga_config(gconstpointer data)
684 GError *error = NULL;
685 char *cwd, *cmd, *out, *err, *str, **strv, **argv = NULL;
686 char *env[2];
687 int status;
688 gsize n;
689 GKeyFile *kf;
691 cwd = g_get_current_dir();
692 cmd = g_strdup_printf("%s%cqemu-ga -D",
693 cwd, G_DIR_SEPARATOR);
694 g_free(cwd);
695 g_shell_parse_argv(cmd, NULL, &argv, &error);
696 g_free(cmd);
697 g_assert_no_error(error);
699 env[0] = g_strdup_printf("QGA_CONF=tests%cdata%ctest-qga-config",
700 G_DIR_SEPARATOR, G_DIR_SEPARATOR);
701 env[1] = NULL;
702 g_spawn_sync(NULL, argv, env, 0,
703 NULL, NULL, &out, &err, &status, &error);
704 g_strfreev(argv);
706 g_assert_no_error(error);
707 g_assert_cmpstr(err, ==, "");
708 g_assert_cmpint(status, ==, 0);
710 kf = g_key_file_new();
711 g_key_file_load_from_data(kf, out, -1, G_KEY_FILE_NONE, &error);
712 g_assert_no_error(error);
714 str = g_key_file_get_start_group(kf);
715 g_assert_cmpstr(str, ==, "general");
716 g_free(str);
718 g_assert_false(g_key_file_get_boolean(kf, "general", "daemon", &error));
719 g_assert_no_error(error);
721 str = g_key_file_get_string(kf, "general", "method", &error);
722 g_assert_no_error(error);
723 g_assert_cmpstr(str, ==, "virtio-serial");
724 g_free(str);
726 str = g_key_file_get_string(kf, "general", "path", &error);
727 g_assert_no_error(error);
728 g_assert_cmpstr(str, ==, "/path/to/org.qemu.guest_agent.0");
729 g_free(str);
731 str = g_key_file_get_string(kf, "general", "pidfile", &error);
732 g_assert_no_error(error);
733 g_assert_cmpstr(str, ==, "/var/foo/qemu-ga.pid");
734 g_free(str);
736 str = g_key_file_get_string(kf, "general", "statedir", &error);
737 g_assert_no_error(error);
738 g_assert_cmpstr(str, ==, "/var/state");
739 g_free(str);
741 g_assert_true(g_key_file_get_boolean(kf, "general", "verbose", &error));
742 g_assert_no_error(error);
744 strv = g_key_file_get_string_list(kf, "general", "blacklist", &n, &error);
745 g_assert_cmpint(n, ==, 2);
746 #if GLIB_CHECK_VERSION(2, 44, 0)
747 g_assert_true(g_strv_contains((const char * const *)strv,
748 "guest-ping"));
749 g_assert_true(g_strv_contains((const char * const *)strv,
750 "guest-get-time"));
751 #endif
752 g_assert_no_error(error);
753 g_strfreev(strv);
755 g_free(out);
756 g_free(err);
757 g_free(env[0]);
758 g_key_file_free(kf);
761 static void test_qga_fsfreeze_status(gconstpointer fix)
763 const TestFixture *fixture = fix;
764 QDict *ret;
765 const gchar *status;
767 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-status'}");
768 g_assert_nonnull(ret);
769 qmp_assert_no_error(ret);
771 status = qdict_get_try_str(ret, "return");
772 g_assert_cmpstr(status, ==, "thawed");
774 QDECREF(ret);
777 static void test_qga_guest_exec(gconstpointer fix)
779 const TestFixture *fixture = fix;
780 QDict *ret, *val;
781 const gchar *out;
782 guchar *decoded;
783 int64_t pid, now, exitcode;
784 gsize len;
785 bool exited;
786 char *cmd;
788 /* exec 'echo foo bar' */
789 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
790 " 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ],"
791 " 'capture-output': true } }");
792 g_assert_nonnull(ret);
793 qmp_assert_no_error(ret);
794 val = qdict_get_qdict(ret, "return");
795 pid = qdict_get_int(val, "pid");
796 g_assert_cmpint(pid, >, 0);
797 QDECREF(ret);
799 /* wait for completion */
800 now = g_get_monotonic_time();
801 cmd = g_strdup_printf("{'execute': 'guest-exec-status',"
802 " 'arguments': { 'pid': %" PRId64 " } }", pid);
803 do {
804 ret = qmp_fd(fixture->fd, cmd);
805 g_assert_nonnull(ret);
806 val = qdict_get_qdict(ret, "return");
807 exited = qdict_get_bool(val, "exited");
808 if (!exited) {
809 QDECREF(ret);
811 } while (!exited &&
812 g_get_monotonic_time() < now + 5 * G_TIME_SPAN_SECOND);
813 g_assert(exited);
814 g_free(cmd);
816 /* check stdout */
817 exitcode = qdict_get_int(val, "exitcode");
818 g_assert_cmpint(exitcode, ==, 0);
819 out = qdict_get_str(val, "out-data");
820 decoded = g_base64_decode(out, &len);
821 g_assert_cmpint(len, ==, 12);
822 g_assert_cmpstr((char *)decoded, ==, "\" test_str \"");
823 g_free(decoded);
824 QDECREF(ret);
827 static void test_qga_guest_exec_invalid(gconstpointer fix)
829 const TestFixture *fixture = fix;
830 QDict *ret, *error;
831 const gchar *class, *desc;
833 /* invalid command */
834 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
835 " 'path': '/bin/invalid-cmd42' } }");
836 g_assert_nonnull(ret);
837 error = qdict_get_qdict(ret, "error");
838 g_assert_nonnull(error);
839 class = qdict_get_str(error, "class");
840 desc = qdict_get_str(error, "desc");
841 g_assert_cmpstr(class, ==, "GenericError");
842 g_assert_cmpint(strlen(desc), >, 0);
843 QDECREF(ret);
845 /* invalid pid */
846 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec-status',"
847 " 'arguments': { 'pid': 0 } }");
848 g_assert_nonnull(ret);
849 error = qdict_get_qdict(ret, "error");
850 g_assert_nonnull(error);
851 class = qdict_get_str(error, "class");
852 desc = qdict_get_str(error, "desc");
853 g_assert_cmpstr(class, ==, "GenericError");
854 g_assert_cmpint(strlen(desc), >, 0);
855 QDECREF(ret);
858 static void test_qga_guest_get_osinfo(gconstpointer data)
860 TestFixture fixture;
861 const gchar *str;
862 gchar *cwd, *env[2];
863 QDict *ret, *val;
865 cwd = g_get_current_dir();
866 env[0] = g_strdup_printf(
867 "QGA_OS_RELEASE=%s%ctests%cdata%ctest-qga-os-release",
868 cwd, G_DIR_SEPARATOR, G_DIR_SEPARATOR, G_DIR_SEPARATOR);
869 env[1] = NULL;
870 g_free(cwd);
871 fixture_setup(&fixture, NULL, env);
873 ret = qmp_fd(fixture.fd, "{'execute': 'guest-get-osinfo'}");
874 g_assert_nonnull(ret);
875 qmp_assert_no_error(ret);
877 val = qdict_get_qdict(ret, "return");
879 str = qdict_get_try_str(val, "id");
880 g_assert_nonnull(str);
881 g_assert_cmpstr(str, ==, "qemu-ga-test");
883 str = qdict_get_try_str(val, "name");
884 g_assert_nonnull(str);
885 g_assert_cmpstr(str, ==, "QEMU-GA");
887 str = qdict_get_try_str(val, "pretty-name");
888 g_assert_nonnull(str);
889 g_assert_cmpstr(str, ==, "QEMU Guest Agent test");
891 str = qdict_get_try_str(val, "version");
892 g_assert_nonnull(str);
893 g_assert_cmpstr(str, ==, "Test 1");
895 str = qdict_get_try_str(val, "version-id");
896 g_assert_nonnull(str);
897 g_assert_cmpstr(str, ==, "1");
899 str = qdict_get_try_str(val, "variant");
900 g_assert_nonnull(str);
901 g_assert_cmpstr(str, ==, "Unit test \"'$`\\ and \\\\ etc.");
903 str = qdict_get_try_str(val, "variant-id");
904 g_assert_nonnull(str);
905 g_assert_cmpstr(str, ==, "unit-test");
907 QDECREF(ret);
908 g_free(env[0]);
909 fixture_tear_down(&fixture, NULL);
912 int main(int argc, char **argv)
914 TestFixture fix;
915 int ret;
917 setlocale (LC_ALL, "");
918 g_test_init(&argc, &argv, NULL);
919 fixture_setup(&fix, NULL, NULL);
921 g_test_add_data_func("/qga/sync-delimited", &fix, test_qga_sync_delimited);
922 g_test_add_data_func("/qga/sync", &fix, test_qga_sync);
923 g_test_add_data_func("/qga/ping", &fix, test_qga_ping);
924 g_test_add_data_func("/qga/info", &fix, test_qga_info);
925 g_test_add_data_func("/qga/network-get-interfaces", &fix,
926 test_qga_network_get_interfaces);
927 if (!access("/sys/devices/system/cpu/cpu0", F_OK)) {
928 g_test_add_data_func("/qga/get-vcpus", &fix, test_qga_get_vcpus);
930 g_test_add_data_func("/qga/get-fsinfo", &fix, test_qga_get_fsinfo);
931 g_test_add_data_func("/qga/get-memory-block-info", &fix,
932 test_qga_get_memory_block_info);
933 g_test_add_data_func("/qga/get-memory-blocks", &fix,
934 test_qga_get_memory_blocks);
935 g_test_add_data_func("/qga/file-ops", &fix, test_qga_file_ops);
936 g_test_add_data_func("/qga/file-write-read", &fix, test_qga_file_write_read);
937 g_test_add_data_func("/qga/get-time", &fix, test_qga_get_time);
938 g_test_add_data_func("/qga/invalid-cmd", &fix, test_qga_invalid_cmd);
939 g_test_add_data_func("/qga/invalid-args", &fix, test_qga_invalid_args);
940 g_test_add_data_func("/qga/fsfreeze-status", &fix,
941 test_qga_fsfreeze_status);
943 g_test_add_data_func("/qga/blacklist", NULL, test_qga_blacklist);
944 g_test_add_data_func("/qga/config", NULL, test_qga_config);
945 g_test_add_data_func("/qga/guest-exec", &fix, test_qga_guest_exec);
946 g_test_add_data_func("/qga/guest-exec-invalid", &fix,
947 test_qga_guest_exec_invalid);
948 g_test_add_data_func("/qga/guest-get-osinfo", &fix,
949 test_qga_guest_get_osinfo);
951 ret = g_test_run();
953 fixture_tear_down(&fix, NULL);
955 return ret;