hw/arm/virt: Set minimum_page_bits to 12
[qemu/ar7.git] / tests / test-qga.c
blob40af64987a365151e528f1b5711a52aec1e089d7
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"
9 typedef struct {
10 char *test_dir;
11 GMainLoop *loop;
12 int fd;
13 GPid pid;
14 } TestFixture;
16 static int connect_qga(char *path)
18 int s, ret, len, i = 0;
19 struct sockaddr_un remote;
21 s = socket(AF_UNIX, SOCK_STREAM, 0);
22 g_assert(s != -1);
24 remote.sun_family = AF_UNIX;
25 do {
26 strcpy(remote.sun_path, path);
27 len = strlen(remote.sun_path) + sizeof(remote.sun_family);
28 ret = connect(s, (struct sockaddr *)&remote, len);
29 if (ret == -1) {
30 g_usleep(G_USEC_PER_SEC);
32 if (i++ == 10) {
33 return -1;
35 } while (ret == -1);
37 return s;
40 static void qga_watch(GPid pid, gint status, gpointer user_data)
42 TestFixture *fixture = user_data;
44 g_assert_cmpint(status, ==, 0);
45 g_main_loop_quit(fixture->loop);
48 static void
49 fixture_setup(TestFixture *fixture, gconstpointer data)
51 const gchar *extra_arg = data;
52 GError *error = NULL;
53 gchar *cwd, *path, *cmd, **argv = NULL;
55 fixture->loop = g_main_loop_new(NULL, FALSE);
57 fixture->test_dir = g_strdup("/tmp/qgatest.XXXXXX");
58 g_assert_nonnull(mkdtemp(fixture->test_dir));
60 path = g_build_filename(fixture->test_dir, "sock", NULL);
61 cwd = g_get_current_dir();
62 cmd = g_strdup_printf("%s%cqemu-ga -m unix-listen -t %s -p %s %s %s",
63 cwd, G_DIR_SEPARATOR,
64 fixture->test_dir, path,
65 getenv("QTEST_LOG") ? "-v" : "",
66 extra_arg ?: "");
67 g_shell_parse_argv(cmd, NULL, &argv, &error);
68 g_assert_no_error(error);
70 g_spawn_async(fixture->test_dir, argv, NULL,
71 G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
72 NULL, NULL, &fixture->pid, &error);
73 g_assert_no_error(error);
75 g_child_watch_add(fixture->pid, qga_watch, fixture);
77 fixture->fd = connect_qga(path);
78 g_assert_cmpint(fixture->fd, !=, -1);
80 g_strfreev(argv);
81 g_free(cmd);
82 g_free(cwd);
83 g_free(path);
86 static void
87 fixture_tear_down(TestFixture *fixture, gconstpointer data)
89 gchar *tmp;
91 kill(fixture->pid, SIGTERM);
93 g_main_loop_run(fixture->loop);
94 g_main_loop_unref(fixture->loop);
96 g_spawn_close_pid(fixture->pid);
98 tmp = g_build_filename(fixture->test_dir, "foo", NULL);
99 g_unlink(tmp);
100 g_free(tmp);
102 tmp = g_build_filename(fixture->test_dir, "qga.state", NULL);
103 g_unlink(tmp);
104 g_free(tmp);
106 tmp = g_build_filename(fixture->test_dir, "sock", NULL);
107 g_unlink(tmp);
108 g_free(tmp);
110 g_rmdir(fixture->test_dir);
111 g_free(fixture->test_dir);
114 static void qmp_assertion_message_error(const char *domain,
115 const char *file,
116 int line,
117 const char *func,
118 const char *expr,
119 QDict *dict)
121 const char *class, *desc;
122 char *s;
123 QDict *error;
125 error = qdict_get_qdict(dict, "error");
126 class = qdict_get_try_str(error, "class");
127 desc = qdict_get_try_str(error, "desc");
129 s = g_strdup_printf("assertion failed %s: %s %s", expr, class, desc);
130 g_assertion_message(domain, file, line, func, s);
131 g_free(s);
134 #define qmp_assert_no_error(err) do { \
135 if (qdict_haskey(err, "error")) { \
136 qmp_assertion_message_error(G_LOG_DOMAIN, __FILE__, __LINE__, \
137 G_STRFUNC, #err, err); \
139 } while (0)
141 static void test_qga_sync_delimited(gconstpointer fix)
143 const TestFixture *fixture = fix;
144 guint32 v, r = g_random_int();
145 unsigned char c;
146 QDict *ret;
147 gchar *cmd;
149 cmd = g_strdup_printf("%c{'execute': 'guest-sync-delimited',"
150 " 'arguments': {'id': %u } }", 0xff, r);
151 qmp_fd_send(fixture->fd, cmd);
152 g_free(cmd);
154 v = read(fixture->fd, &c, 1);
155 g_assert_cmpint(v, ==, 1);
156 g_assert_cmpint(c, ==, 0xff);
158 ret = qmp_fd_receive(fixture->fd);
159 g_assert_nonnull(ret);
160 qmp_assert_no_error(ret);
162 v = qdict_get_int(ret, "return");
163 g_assert_cmpint(r, ==, v);
165 QDECREF(ret);
168 static void test_qga_sync(gconstpointer fix)
170 const TestFixture *fixture = fix;
171 guint32 v, r = g_random_int();
172 QDict *ret;
173 gchar *cmd;
175 cmd = g_strdup_printf("%c{'execute': 'guest-sync',"
176 " 'arguments': {'id': %u } }", 0xff, r);
177 ret = qmp_fd(fixture->fd, cmd);
178 g_free(cmd);
180 g_assert_nonnull(ret);
181 qmp_assert_no_error(ret);
183 v = qdict_get_int(ret, "return");
184 g_assert_cmpint(r, ==, v);
186 QDECREF(ret);
189 static void test_qga_ping(gconstpointer fix)
191 const TestFixture *fixture = fix;
192 QDict *ret;
194 ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping'}");
195 g_assert_nonnull(ret);
196 qmp_assert_no_error(ret);
198 QDECREF(ret);
201 static void test_qga_invalid_args(gconstpointer fix)
203 const TestFixture *fixture = fix;
204 QDict *ret, *error;
205 const gchar *class, *desc;
207 ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping', "
208 "'arguments': {'foo': 42 }}");
209 g_assert_nonnull(ret);
211 error = qdict_get_qdict(ret, "error");
212 class = qdict_get_try_str(error, "class");
213 desc = qdict_get_try_str(error, "desc");
215 g_assert_cmpstr(class, ==, "GenericError");
216 g_assert_cmpstr(desc, ==, "QMP input object member 'foo' is unexpected");
218 QDECREF(ret);
221 static void test_qga_invalid_cmd(gconstpointer fix)
223 const TestFixture *fixture = fix;
224 QDict *ret, *error;
225 const gchar *class, *desc;
227 ret = qmp_fd(fixture->fd, "{'execute': 'guest-invalid-cmd'}");
228 g_assert_nonnull(ret);
230 error = qdict_get_qdict(ret, "error");
231 class = qdict_get_try_str(error, "class");
232 desc = qdict_get_try_str(error, "desc");
234 g_assert_cmpstr(class, ==, "CommandNotFound");
235 g_assert_cmpint(strlen(desc), >, 0);
237 QDECREF(ret);
240 static void test_qga_info(gconstpointer fix)
242 const TestFixture *fixture = fix;
243 QDict *ret, *val;
244 const gchar *version;
246 ret = qmp_fd(fixture->fd, "{'execute': 'guest-info'}");
247 g_assert_nonnull(ret);
248 qmp_assert_no_error(ret);
250 val = qdict_get_qdict(ret, "return");
251 version = qdict_get_try_str(val, "version");
252 g_assert_cmpstr(version, ==, QEMU_VERSION);
254 QDECREF(ret);
257 static void test_qga_get_vcpus(gconstpointer fix)
259 const TestFixture *fixture = fix;
260 QDict *ret;
261 QList *list;
262 const QListEntry *entry;
264 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-vcpus'}");
265 g_assert_nonnull(ret);
266 qmp_assert_no_error(ret);
268 /* check there is at least a cpu */
269 list = qdict_get_qlist(ret, "return");
270 entry = qlist_first(list);
271 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "online"));
272 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "logical-id"));
274 QDECREF(ret);
277 static void test_qga_get_fsinfo(gconstpointer fix)
279 const TestFixture *fixture = fix;
280 QDict *ret;
281 QList *list;
282 const QListEntry *entry;
284 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-fsinfo'}");
285 g_assert_nonnull(ret);
286 qmp_assert_no_error(ret);
288 /* sanity-check the response if there are any filesystems */
289 list = qdict_get_qlist(ret, "return");
290 entry = qlist_first(list);
291 if (entry) {
292 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "name"));
293 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "mountpoint"));
294 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "type"));
295 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "disk"));
298 QDECREF(ret);
301 static void test_qga_get_memory_block_info(gconstpointer fix)
303 const TestFixture *fixture = fix;
304 QDict *ret, *val;
305 int64_t size;
307 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-block-info'}");
308 g_assert_nonnull(ret);
310 /* some systems might not expose memory block info in sysfs */
311 if (!qdict_haskey(ret, "error")) {
312 /* check there is at least some memory */
313 val = qdict_get_qdict(ret, "return");
314 size = qdict_get_int(val, "size");
315 g_assert_cmpint(size, >, 0);
318 QDECREF(ret);
321 static void test_qga_get_memory_blocks(gconstpointer fix)
323 const TestFixture *fixture = fix;
324 QDict *ret;
325 QList *list;
326 const QListEntry *entry;
328 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-blocks'}");
329 g_assert_nonnull(ret);
331 /* some systems might not expose memory block info in sysfs */
332 if (!qdict_haskey(ret, "error")) {
333 list = qdict_get_qlist(ret, "return");
334 entry = qlist_first(list);
335 /* newer versions of qga may return empty list without error */
336 if (entry) {
337 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "phys-index"));
338 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "online"));
342 QDECREF(ret);
345 static void test_qga_network_get_interfaces(gconstpointer fix)
347 const TestFixture *fixture = fix;
348 QDict *ret;
349 QList *list;
350 const QListEntry *entry;
352 ret = qmp_fd(fixture->fd, "{'execute': 'guest-network-get-interfaces'}");
353 g_assert_nonnull(ret);
354 qmp_assert_no_error(ret);
356 /* check there is at least an interface */
357 list = qdict_get_qlist(ret, "return");
358 entry = qlist_first(list);
359 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "name"));
361 QDECREF(ret);
364 static void test_qga_file_ops(gconstpointer fix)
366 const TestFixture *fixture = fix;
367 const unsigned char helloworld[] = "Hello World!\n";
368 const char *b64;
369 gchar *cmd, *path, *enc;
370 unsigned char *dec;
371 QDict *ret, *val;
372 int64_t id, eof;
373 gsize count;
374 FILE *f;
375 char tmp[100];
377 /* open */
378 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
379 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
380 g_assert_nonnull(ret);
381 qmp_assert_no_error(ret);
382 id = qdict_get_int(ret, "return");
383 QDECREF(ret);
385 enc = g_base64_encode(helloworld, sizeof(helloworld));
386 /* write */
387 cmd = g_strdup_printf("{'execute': 'guest-file-write',"
388 " 'arguments': { 'handle': %" PRId64 ","
389 " 'buf-b64': '%s' } }", id, enc);
390 ret = qmp_fd(fixture->fd, cmd);
391 g_assert_nonnull(ret);
392 qmp_assert_no_error(ret);
394 val = qdict_get_qdict(ret, "return");
395 count = qdict_get_int(val, "count");
396 eof = qdict_get_bool(val, "eof");
397 g_assert_cmpint(count, ==, sizeof(helloworld));
398 g_assert_cmpint(eof, ==, 0);
399 QDECREF(ret);
400 g_free(cmd);
402 /* flush */
403 cmd = g_strdup_printf("{'execute': 'guest-file-flush',"
404 " 'arguments': {'handle': %" PRId64 "} }",
405 id);
406 ret = qmp_fd(fixture->fd, cmd);
407 QDECREF(ret);
408 g_free(cmd);
410 /* close */
411 cmd = g_strdup_printf("{'execute': 'guest-file-close',"
412 " 'arguments': {'handle': %" PRId64 "} }",
413 id);
414 ret = qmp_fd(fixture->fd, cmd);
415 QDECREF(ret);
416 g_free(cmd);
418 /* check content */
419 path = g_build_filename(fixture->test_dir, "foo", NULL);
420 f = fopen(path, "r");
421 g_free(path);
422 g_assert_nonnull(f);
423 count = fread(tmp, 1, sizeof(tmp), f);
424 g_assert_cmpint(count, ==, sizeof(helloworld));
425 tmp[count] = 0;
426 g_assert_cmpstr(tmp, ==, (char *)helloworld);
427 fclose(f);
429 /* open */
430 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
431 " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
432 g_assert_nonnull(ret);
433 qmp_assert_no_error(ret);
434 id = qdict_get_int(ret, "return");
435 QDECREF(ret);
437 /* read */
438 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
439 " 'arguments': { 'handle': %" PRId64 "} }",
440 id);
441 ret = qmp_fd(fixture->fd, cmd);
442 val = qdict_get_qdict(ret, "return");
443 count = qdict_get_int(val, "count");
444 eof = qdict_get_bool(val, "eof");
445 b64 = qdict_get_str(val, "buf-b64");
446 g_assert_cmpint(count, ==, sizeof(helloworld));
447 g_assert(eof);
448 g_assert_cmpstr(b64, ==, enc);
450 QDECREF(ret);
451 g_free(cmd);
452 g_free(enc);
454 /* read eof */
455 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
456 " 'arguments': { 'handle': %" PRId64 "} }",
457 id);
458 ret = qmp_fd(fixture->fd, cmd);
459 val = qdict_get_qdict(ret, "return");
460 count = qdict_get_int(val, "count");
461 eof = qdict_get_bool(val, "eof");
462 b64 = qdict_get_str(val, "buf-b64");
463 g_assert_cmpint(count, ==, 0);
464 g_assert(eof);
465 g_assert_cmpstr(b64, ==, "");
466 QDECREF(ret);
467 g_free(cmd);
469 /* seek */
470 cmd = g_strdup_printf("{'execute': 'guest-file-seek',"
471 " 'arguments': { 'handle': %" PRId64 ", "
472 " 'offset': %d, 'whence': '%s' } }",
473 id, 6, "set");
474 ret = qmp_fd(fixture->fd, cmd);
475 qmp_assert_no_error(ret);
476 val = qdict_get_qdict(ret, "return");
477 count = qdict_get_int(val, "position");
478 eof = qdict_get_bool(val, "eof");
479 g_assert_cmpint(count, ==, 6);
480 g_assert(!eof);
481 QDECREF(ret);
482 g_free(cmd);
484 /* partial read */
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, ==, sizeof(helloworld) - 6);
494 g_assert(eof);
495 dec = g_base64_decode(b64, &count);
496 g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
497 g_assert_cmpmem(dec, count, helloworld + 6, sizeof(helloworld) - 6);
498 g_free(dec);
500 QDECREF(ret);
501 g_free(cmd);
503 /* close */
504 cmd = g_strdup_printf("{'execute': 'guest-file-close',"
505 " 'arguments': {'handle': %" PRId64 "} }",
506 id);
507 ret = qmp_fd(fixture->fd, cmd);
508 QDECREF(ret);
509 g_free(cmd);
512 static void test_qga_file_write_read(gconstpointer fix)
514 const TestFixture *fixture = fix;
515 const unsigned char helloworld[] = "Hello World!\n";
516 const char *b64;
517 gchar *cmd, *enc;
518 QDict *ret, *val;
519 int64_t id, eof;
520 gsize count;
522 /* open */
523 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
524 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
525 g_assert_nonnull(ret);
526 qmp_assert_no_error(ret);
527 id = qdict_get_int(ret, "return");
528 QDECREF(ret);
530 enc = g_base64_encode(helloworld, sizeof(helloworld));
531 /* write */
532 cmd = g_strdup_printf("{'execute': 'guest-file-write',"
533 " 'arguments': { 'handle': %" PRId64 ","
534 " 'buf-b64': '%s' } }", id, enc);
535 ret = qmp_fd(fixture->fd, cmd);
536 g_assert_nonnull(ret);
537 qmp_assert_no_error(ret);
539 val = qdict_get_qdict(ret, "return");
540 count = qdict_get_int(val, "count");
541 eof = qdict_get_bool(val, "eof");
542 g_assert_cmpint(count, ==, sizeof(helloworld));
543 g_assert_cmpint(eof, ==, 0);
544 QDECREF(ret);
545 g_free(cmd);
547 /* read (check implicit flush) */
548 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
549 " 'arguments': { 'handle': %" PRId64 "} }",
550 id);
551 ret = qmp_fd(fixture->fd, cmd);
552 val = qdict_get_qdict(ret, "return");
553 count = qdict_get_int(val, "count");
554 eof = qdict_get_bool(val, "eof");
555 b64 = qdict_get_str(val, "buf-b64");
556 g_assert_cmpint(count, ==, 0);
557 g_assert(eof);
558 g_assert_cmpstr(b64, ==, "");
559 QDECREF(ret);
560 g_free(cmd);
562 /* seek to 0 */
563 cmd = g_strdup_printf("{'execute': 'guest-file-seek',"
564 " 'arguments': { 'handle': %" PRId64 ", "
565 " 'offset': %d, 'whence': '%s' } }",
566 id, 0, "set");
567 ret = qmp_fd(fixture->fd, cmd);
568 qmp_assert_no_error(ret);
569 val = qdict_get_qdict(ret, "return");
570 count = qdict_get_int(val, "position");
571 eof = qdict_get_bool(val, "eof");
572 g_assert_cmpint(count, ==, 0);
573 g_assert(!eof);
574 QDECREF(ret);
575 g_free(cmd);
577 /* read */
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, ==, sizeof(helloworld));
587 g_assert(eof);
588 g_assert_cmpstr(b64, ==, enc);
589 QDECREF(ret);
590 g_free(cmd);
591 g_free(enc);
593 /* close */
594 cmd = g_strdup_printf("{'execute': 'guest-file-close',"
595 " 'arguments': {'handle': %" PRId64 "} }",
596 id);
597 ret = qmp_fd(fixture->fd, cmd);
598 QDECREF(ret);
599 g_free(cmd);
602 static void test_qga_get_time(gconstpointer fix)
604 const TestFixture *fixture = fix;
605 QDict *ret;
606 int64_t time;
608 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
609 g_assert_nonnull(ret);
610 qmp_assert_no_error(ret);
612 time = qdict_get_int(ret, "return");
613 g_assert_cmpint(time, >, 0);
615 QDECREF(ret);
618 static void test_qga_set_time(gconstpointer fix)
620 const TestFixture *fixture = fix;
621 QDict *ret;
622 int64_t current, time;
623 gchar *cmd;
625 /* get current time */
626 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
627 g_assert_nonnull(ret);
628 qmp_assert_no_error(ret);
629 current = qdict_get_int(ret, "return");
630 g_assert_cmpint(current, >, 0);
631 QDECREF(ret);
633 /* set some old time */
634 ret = qmp_fd(fixture->fd, "{'execute': 'guest-set-time',"
635 " 'arguments': { 'time': 1000 } }");
636 g_assert_nonnull(ret);
637 qmp_assert_no_error(ret);
638 QDECREF(ret);
640 /* check old time */
641 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
642 g_assert_nonnull(ret);
643 qmp_assert_no_error(ret);
644 time = qdict_get_int(ret, "return");
645 g_assert_cmpint(time / 1000, <, G_USEC_PER_SEC * 10);
646 QDECREF(ret);
648 /* set back current time */
649 cmd = g_strdup_printf("{'execute': 'guest-set-time',"
650 " 'arguments': { 'time': %" PRId64 " } }",
651 current + time * 1000);
652 ret = qmp_fd(fixture->fd, cmd);
653 g_free(cmd);
654 g_assert_nonnull(ret);
655 qmp_assert_no_error(ret);
656 QDECREF(ret);
659 static void test_qga_fstrim(gconstpointer fix)
661 const TestFixture *fixture = fix;
662 QDict *ret;
663 QList *list;
664 const QListEntry *entry;
666 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fstrim',"
667 " arguments: { minimum: 4194304 } }");
668 g_assert_nonnull(ret);
669 qmp_assert_no_error(ret);
670 list = qdict_get_qlist(ret, "return");
671 entry = qlist_first(list);
672 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "paths"));
674 QDECREF(ret);
677 static void test_qga_blacklist(gconstpointer data)
679 TestFixture fix;
680 QDict *ret, *error;
681 const gchar *class, *desc;
683 fixture_setup(&fix, "-b guest-ping,guest-get-time");
685 /* check blacklist */
686 ret = qmp_fd(fix.fd, "{'execute': 'guest-ping'}");
687 g_assert_nonnull(ret);
688 error = qdict_get_qdict(ret, "error");
689 class = qdict_get_try_str(error, "class");
690 desc = qdict_get_try_str(error, "desc");
691 g_assert_cmpstr(class, ==, "GenericError");
692 g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
693 QDECREF(ret);
695 ret = qmp_fd(fix.fd, "{'execute': 'guest-get-time'}");
696 g_assert_nonnull(ret);
697 error = qdict_get_qdict(ret, "error");
698 class = qdict_get_try_str(error, "class");
699 desc = qdict_get_try_str(error, "desc");
700 g_assert_cmpstr(class, ==, "GenericError");
701 g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
702 QDECREF(ret);
704 /* check something work */
705 ret = qmp_fd(fix.fd, "{'execute': 'guest-get-fsinfo'}");
706 qmp_assert_no_error(ret);
707 QDECREF(ret);
709 fixture_tear_down(&fix, NULL);
712 static void test_qga_config(gconstpointer data)
714 GError *error = NULL;
715 char *cwd, *cmd, *out, *err, *str, **strv, **argv = NULL;
716 char *env[2];
717 int status;
718 gsize n;
719 GKeyFile *kf;
721 cwd = g_get_current_dir();
722 cmd = g_strdup_printf("%s%cqemu-ga -D",
723 cwd, G_DIR_SEPARATOR);
724 g_free(cwd);
725 g_shell_parse_argv(cmd, NULL, &argv, &error);
726 g_free(cmd);
727 g_assert_no_error(error);
729 env[0] = g_strdup_printf("QGA_CONF=tests%cdata%ctest-qga-config",
730 G_DIR_SEPARATOR, G_DIR_SEPARATOR);
731 env[1] = NULL;
732 g_spawn_sync(NULL, argv, env, 0,
733 NULL, NULL, &out, &err, &status, &error);
734 g_strfreev(argv);
736 g_assert_no_error(error);
737 g_assert_cmpstr(err, ==, "");
738 g_assert_cmpint(status, ==, 0);
740 kf = g_key_file_new();
741 g_key_file_load_from_data(kf, out, -1, G_KEY_FILE_NONE, &error);
742 g_assert_no_error(error);
744 str = g_key_file_get_start_group(kf);
745 g_assert_cmpstr(str, ==, "general");
746 g_free(str);
748 g_assert_false(g_key_file_get_boolean(kf, "general", "daemon", &error));
749 g_assert_no_error(error);
751 str = g_key_file_get_string(kf, "general", "method", &error);
752 g_assert_no_error(error);
753 g_assert_cmpstr(str, ==, "virtio-serial");
754 g_free(str);
756 str = g_key_file_get_string(kf, "general", "path", &error);
757 g_assert_no_error(error);
758 g_assert_cmpstr(str, ==, "/path/to/org.qemu.guest_agent.0");
759 g_free(str);
761 str = g_key_file_get_string(kf, "general", "pidfile", &error);
762 g_assert_no_error(error);
763 g_assert_cmpstr(str, ==, "/var/foo/qemu-ga.pid");
764 g_free(str);
766 str = g_key_file_get_string(kf, "general", "statedir", &error);
767 g_assert_no_error(error);
768 g_assert_cmpstr(str, ==, "/var/state");
769 g_free(str);
771 g_assert_true(g_key_file_get_boolean(kf, "general", "verbose", &error));
772 g_assert_no_error(error);
774 strv = g_key_file_get_string_list(kf, "general", "blacklist", &n, &error);
775 g_assert_cmpint(n, ==, 2);
776 #if GLIB_CHECK_VERSION(2, 44, 0)
777 g_assert_true(g_strv_contains((const char * const *)strv,
778 "guest-ping"));
779 g_assert_true(g_strv_contains((const char * const *)strv,
780 "guest-get-time"));
781 #endif
782 g_assert_no_error(error);
783 g_strfreev(strv);
785 g_free(out);
786 g_free(err);
787 g_free(env[0]);
788 g_key_file_free(kf);
791 static void test_qga_fsfreeze_status(gconstpointer fix)
793 const TestFixture *fixture = fix;
794 QDict *ret;
795 const gchar *status;
797 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-status'}");
798 g_assert_nonnull(ret);
799 qmp_assert_no_error(ret);
801 status = qdict_get_try_str(ret, "return");
802 g_assert_cmpstr(status, ==, "thawed");
804 QDECREF(ret);
807 static void test_qga_fsfreeze_and_thaw(gconstpointer fix)
809 const TestFixture *fixture = fix;
810 QDict *ret;
811 const gchar *status;
813 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-freeze'}");
814 g_assert_nonnull(ret);
815 qmp_assert_no_error(ret);
816 QDECREF(ret);
818 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-status'}");
819 g_assert_nonnull(ret);
820 qmp_assert_no_error(ret);
821 status = qdict_get_try_str(ret, "return");
822 g_assert_cmpstr(status, ==, "frozen");
823 QDECREF(ret);
825 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-thaw'}");
826 g_assert_nonnull(ret);
827 qmp_assert_no_error(ret);
828 QDECREF(ret);
831 static void test_qga_guest_exec(gconstpointer fix)
833 const TestFixture *fixture = fix;
834 QDict *ret, *val;
835 const gchar *out;
836 guchar *decoded;
837 int64_t pid, now, exitcode;
838 gsize len;
839 bool exited;
841 /* exec 'echo foo bar' */
842 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
843 " 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ],"
844 " 'capture-output': true } }");
845 g_assert_nonnull(ret);
846 qmp_assert_no_error(ret);
847 val = qdict_get_qdict(ret, "return");
848 pid = qdict_get_int(val, "pid");
849 g_assert_cmpint(pid, >, 0);
850 QDECREF(ret);
852 /* wait for completion */
853 now = g_get_monotonic_time();
854 do {
855 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec-status',"
856 " 'arguments': { 'pid': %" PRId64 " } }", pid);
857 g_assert_nonnull(ret);
858 val = qdict_get_qdict(ret, "return");
859 exited = qdict_get_bool(val, "exited");
860 if (!exited) {
861 QDECREF(ret);
863 } while (!exited &&
864 g_get_monotonic_time() < now + 5 * G_TIME_SPAN_SECOND);
865 g_assert(exited);
867 /* check stdout */
868 exitcode = qdict_get_int(val, "exitcode");
869 g_assert_cmpint(exitcode, ==, 0);
870 out = qdict_get_str(val, "out-data");
871 decoded = g_base64_decode(out, &len);
872 g_assert_cmpint(len, ==, 12);
873 g_assert_cmpstr((char *)decoded, ==, "\" test_str \"");
874 g_free(decoded);
875 QDECREF(ret);
878 static void test_qga_guest_exec_invalid(gconstpointer fix)
880 const TestFixture *fixture = fix;
881 QDict *ret, *error;
882 const gchar *class, *desc;
884 /* invalid command */
885 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
886 " 'path': '/bin/invalid-cmd42' } }");
887 g_assert_nonnull(ret);
888 error = qdict_get_qdict(ret, "error");
889 g_assert_nonnull(error);
890 class = qdict_get_str(error, "class");
891 desc = qdict_get_str(error, "desc");
892 g_assert_cmpstr(class, ==, "GenericError");
893 g_assert_cmpint(strlen(desc), >, 0);
894 QDECREF(ret);
896 /* invalid pid */
897 ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec-status',"
898 " 'arguments': { 'pid': 0 } }");
899 g_assert_nonnull(ret);
900 error = qdict_get_qdict(ret, "error");
901 g_assert_nonnull(error);
902 class = qdict_get_str(error, "class");
903 desc = qdict_get_str(error, "desc");
904 g_assert_cmpstr(class, ==, "GenericError");
905 g_assert_cmpint(strlen(desc), >, 0);
906 QDECREF(ret);
909 int main(int argc, char **argv)
911 TestFixture fix;
912 int ret;
914 setlocale (LC_ALL, "");
915 g_test_init(&argc, &argv, NULL);
916 fixture_setup(&fix, NULL);
918 g_test_add_data_func("/qga/sync-delimited", &fix, test_qga_sync_delimited);
919 g_test_add_data_func("/qga/sync", &fix, test_qga_sync);
920 g_test_add_data_func("/qga/ping", &fix, test_qga_ping);
921 g_test_add_data_func("/qga/info", &fix, test_qga_info);
922 g_test_add_data_func("/qga/network-get-interfaces", &fix,
923 test_qga_network_get_interfaces);
924 g_test_add_data_func("/qga/get-vcpus", &fix, test_qga_get_vcpus);
925 g_test_add_data_func("/qga/get-fsinfo", &fix, test_qga_get_fsinfo);
926 g_test_add_data_func("/qga/get-memory-block-info", &fix,
927 test_qga_get_memory_block_info);
928 g_test_add_data_func("/qga/get-memory-blocks", &fix,
929 test_qga_get_memory_blocks);
930 g_test_add_data_func("/qga/file-ops", &fix, test_qga_file_ops);
931 g_test_add_data_func("/qga/file-write-read", &fix, test_qga_file_write_read);
932 g_test_add_data_func("/qga/get-time", &fix, test_qga_get_time);
933 g_test_add_data_func("/qga/invalid-cmd", &fix, test_qga_invalid_cmd);
934 g_test_add_data_func("/qga/invalid-args", &fix, test_qga_invalid_args);
935 g_test_add_data_func("/qga/fsfreeze-status", &fix,
936 test_qga_fsfreeze_status);
938 g_test_add_data_func("/qga/blacklist", NULL, test_qga_blacklist);
939 g_test_add_data_func("/qga/config", NULL, test_qga_config);
940 g_test_add_data_func("/qga/guest-exec", &fix, test_qga_guest_exec);
941 g_test_add_data_func("/qga/guest-exec-invalid", &fix,
942 test_qga_guest_exec_invalid);
944 if (g_getenv("QGA_TEST_SIDE_EFFECTING")) {
945 g_test_add_data_func("/qga/fsfreeze-and-thaw", &fix,
946 test_qga_fsfreeze_and_thaw);
947 g_test_add_data_func("/qga/set-time", &fix, test_qga_set_time);
948 g_test_add_data_func("/qga/fstrim", &fix, test_qga_fstrim);
951 ret = g_test_run();
953 fixture_tear_down(&fix, NULL);
955 return ret;