These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / tests / test-qga.c
1 #include "qemu/osdep.h"
2 #include <locale.h>
3 #include <glib.h>
4 #include <glib/gstdio.h>
5 #include <sys/socket.h>
6 #include <sys/un.h>
7
8 #include "libqtest.h"
9
10 typedef struct {
11     char *test_dir;
12     GMainLoop *loop;
13     int fd;
14     GPid pid;
15 } TestFixture;
16
17 static int connect_qga(char *path)
18 {
19     int s, ret, len, i = 0;
20     struct sockaddr_un remote;
21
22     s = socket(AF_UNIX, SOCK_STREAM, 0);
23     g_assert(s != -1);
24
25     remote.sun_family = AF_UNIX;
26     do {
27         strcpy(remote.sun_path, path);
28         len = strlen(remote.sun_path) + sizeof(remote.sun_family);
29         ret = connect(s, (struct sockaddr *)&remote, len);
30         if (ret == -1) {
31             g_usleep(G_USEC_PER_SEC);
32         }
33         if (i++ == 10) {
34             return -1;
35         }
36     } while (ret == -1);
37
38     return s;
39 }
40
41 static void qga_watch(GPid pid, gint status, gpointer user_data)
42 {
43     TestFixture *fixture = user_data;
44
45     g_assert_cmpint(status, ==, 0);
46     g_main_loop_quit(fixture->loop);
47 }
48
49 static void
50 fixture_setup(TestFixture *fixture, gconstpointer data)
51 {
52     const gchar *extra_arg = data;
53     GError *error = NULL;
54     gchar *cwd, *path, *cmd, **argv = NULL;
55
56     fixture->loop = g_main_loop_new(NULL, FALSE);
57
58     fixture->test_dir = g_strdup("/tmp/qgatest.XXXXXX");
59     g_assert_nonnull(mkdtemp(fixture->test_dir));
60
61     path = g_build_filename(fixture->test_dir, "sock", NULL);
62     cwd = g_get_current_dir();
63     cmd = g_strdup_printf("%s%cqemu-ga -m unix-listen -t %s -p %s %s %s",
64                           cwd, G_DIR_SEPARATOR,
65                           fixture->test_dir, path,
66                           getenv("QTEST_LOG") ? "-v" : "",
67                           extra_arg ?: "");
68     g_shell_parse_argv(cmd, NULL, &argv, &error);
69     g_assert_no_error(error);
70
71     g_spawn_async(fixture->test_dir, argv, NULL,
72                   G_SPAWN_SEARCH_PATH|G_SPAWN_DO_NOT_REAP_CHILD,
73                   NULL, NULL, &fixture->pid, &error);
74     g_assert_no_error(error);
75
76     g_child_watch_add(fixture->pid, qga_watch, fixture);
77
78     fixture->fd = connect_qga(path);
79     g_assert_cmpint(fixture->fd, !=, -1);
80
81     g_strfreev(argv);
82     g_free(cmd);
83     g_free(cwd);
84     g_free(path);
85 }
86
87 static void
88 fixture_tear_down(TestFixture *fixture, gconstpointer data)
89 {
90     gchar *tmp;
91
92     kill(fixture->pid, SIGTERM);
93
94     g_main_loop_run(fixture->loop);
95     g_main_loop_unref(fixture->loop);
96
97     g_spawn_close_pid(fixture->pid);
98
99     tmp = g_build_filename(fixture->test_dir, "foo", NULL);
100     g_unlink(tmp);
101     g_free(tmp);
102
103     tmp = g_build_filename(fixture->test_dir, "qga.state", NULL);
104     g_unlink(tmp);
105     g_free(tmp);
106
107     tmp = g_build_filename(fixture->test_dir, "sock", NULL);
108     g_unlink(tmp);
109     g_free(tmp);
110
111     g_rmdir(fixture->test_dir);
112     g_free(fixture->test_dir);
113 }
114
115 static void qmp_assertion_message_error(const char     *domain,
116                                         const char     *file,
117                                         int             line,
118                                         const char     *func,
119                                         const char     *expr,
120                                         QDict          *dict)
121 {
122     const char *class, *desc;
123     char *s;
124     QDict *error;
125
126     error = qdict_get_qdict(dict, "error");
127     class = qdict_get_try_str(error, "class");
128     desc = qdict_get_try_str(error, "desc");
129
130     s = g_strdup_printf("assertion failed %s: %s %s", expr, class, desc);
131     g_assertion_message(domain, file, line, func, s);
132     g_free(s);
133 }
134
135 #define qmp_assert_no_error(err) do {                                   \
136     if (qdict_haskey(err, "error")) {                                   \
137         qmp_assertion_message_error(G_LOG_DOMAIN, __FILE__, __LINE__,   \
138                                     G_STRFUNC, #err, err);              \
139     }                                                                   \
140 } while (0)
141
142 static void test_qga_sync_delimited(gconstpointer fix)
143 {
144     const TestFixture *fixture = fix;
145     guint32 v, r = g_random_int();
146     unsigned char c;
147     QDict *ret;
148     gchar *cmd;
149
150     cmd = g_strdup_printf("%c{'execute': 'guest-sync-delimited',"
151                           " 'arguments': {'id': %u } }", 0xff, r);
152     qmp_fd_send(fixture->fd, cmd);
153     g_free(cmd);
154
155     v = read(fixture->fd, &c, 1);
156     g_assert_cmpint(v, ==, 1);
157     g_assert_cmpint(c, ==, 0xff);
158
159     ret = qmp_fd_receive(fixture->fd);
160     g_assert_nonnull(ret);
161     qmp_assert_no_error(ret);
162
163     v = qdict_get_int(ret, "return");
164     g_assert_cmpint(r, ==, v);
165
166     QDECREF(ret);
167 }
168
169 static void test_qga_sync(gconstpointer fix)
170 {
171     const TestFixture *fixture = fix;
172     guint32 v, r = g_random_int();
173     QDict *ret;
174     gchar *cmd;
175
176     cmd = g_strdup_printf("%c{'execute': 'guest-sync',"
177                           " 'arguments': {'id': %u } }", 0xff, r);
178     ret = qmp_fd(fixture->fd, cmd);
179     g_free(cmd);
180
181     g_assert_nonnull(ret);
182     qmp_assert_no_error(ret);
183
184     v = qdict_get_int(ret, "return");
185     g_assert_cmpint(r, ==, v);
186
187     QDECREF(ret);
188 }
189
190 static void test_qga_ping(gconstpointer fix)
191 {
192     const TestFixture *fixture = fix;
193     QDict *ret;
194
195     ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping'}");
196     g_assert_nonnull(ret);
197     qmp_assert_no_error(ret);
198
199     QDECREF(ret);
200 }
201
202 static void test_qga_invalid_cmd(gconstpointer fix)
203 {
204     const TestFixture *fixture = fix;
205     QDict *ret, *error;
206     const gchar *class, *desc;
207
208     ret = qmp_fd(fixture->fd, "{'execute': 'guest-invalid-cmd'}");
209     g_assert_nonnull(ret);
210
211     error = qdict_get_qdict(ret, "error");
212     class = qdict_get_try_str(error, "class");
213     desc = qdict_get_try_str(error, "desc");
214
215     g_assert_cmpstr(class, ==, "CommandNotFound");
216     g_assert_cmpint(strlen(desc), >, 0);
217
218     QDECREF(ret);
219 }
220
221 static void test_qga_info(gconstpointer fix)
222 {
223     const TestFixture *fixture = fix;
224     QDict *ret, *val;
225     const gchar *version;
226
227     ret = qmp_fd(fixture->fd, "{'execute': 'guest-info'}");
228     g_assert_nonnull(ret);
229     qmp_assert_no_error(ret);
230
231     val = qdict_get_qdict(ret, "return");
232     version = qdict_get_try_str(val, "version");
233     g_assert_cmpstr(version, ==, QEMU_VERSION);
234
235     QDECREF(ret);
236 }
237
238 static void test_qga_get_vcpus(gconstpointer fix)
239 {
240     const TestFixture *fixture = fix;
241     QDict *ret;
242     QList *list;
243     const QListEntry *entry;
244
245     ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-vcpus'}");
246     g_assert_nonnull(ret);
247     qmp_assert_no_error(ret);
248
249     /* check there is at least a cpu */
250     list = qdict_get_qlist(ret, "return");
251     entry = qlist_first(list);
252     g_assert(qdict_haskey(qobject_to_qdict(entry->value), "online"));
253     g_assert(qdict_haskey(qobject_to_qdict(entry->value), "logical-id"));
254
255     QDECREF(ret);
256 }
257
258 static void test_qga_get_fsinfo(gconstpointer fix)
259 {
260     const TestFixture *fixture = fix;
261     QDict *ret;
262     QList *list;
263     const QListEntry *entry;
264
265     ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-fsinfo'}");
266     g_assert_nonnull(ret);
267     qmp_assert_no_error(ret);
268
269     /* sanity-check the response if there are any filesystems */
270     list = qdict_get_qlist(ret, "return");
271     entry = qlist_first(list);
272     if (entry) {
273         g_assert(qdict_haskey(qobject_to_qdict(entry->value), "name"));
274         g_assert(qdict_haskey(qobject_to_qdict(entry->value), "mountpoint"));
275         g_assert(qdict_haskey(qobject_to_qdict(entry->value), "type"));
276         g_assert(qdict_haskey(qobject_to_qdict(entry->value), "disk"));
277     }
278
279     QDECREF(ret);
280 }
281
282 static void test_qga_get_memory_block_info(gconstpointer fix)
283 {
284     const TestFixture *fixture = fix;
285     QDict *ret, *val;
286     int64_t size;
287
288     ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-block-info'}");
289     g_assert_nonnull(ret);
290
291     /* some systems might not expose memory block info in sysfs */
292     if (!qdict_haskey(ret, "error")) {
293         /* check there is at least some memory */
294         val = qdict_get_qdict(ret, "return");
295         size = qdict_get_int(val, "size");
296         g_assert_cmpint(size, >, 0);
297     }
298
299     QDECREF(ret);
300 }
301
302 static void test_qga_get_memory_blocks(gconstpointer fix)
303 {
304     const TestFixture *fixture = fix;
305     QDict *ret;
306     QList *list;
307     const QListEntry *entry;
308
309     ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-blocks'}");
310     g_assert_nonnull(ret);
311
312     /* some systems might not expose memory block info in sysfs */
313     if (!qdict_haskey(ret, "error")) {
314         list = qdict_get_qlist(ret, "return");
315         entry = qlist_first(list);
316         /* newer versions of qga may return empty list without error */
317         if (entry) {
318             g_assert(qdict_haskey(qobject_to_qdict(entry->value), "phys-index"));
319             g_assert(qdict_haskey(qobject_to_qdict(entry->value), "online"));
320         }
321     }
322
323     QDECREF(ret);
324 }
325
326 static void test_qga_network_get_interfaces(gconstpointer fix)
327 {
328     const TestFixture *fixture = fix;
329     QDict *ret;
330     QList *list;
331     const QListEntry *entry;
332
333     ret = qmp_fd(fixture->fd, "{'execute': 'guest-network-get-interfaces'}");
334     g_assert_nonnull(ret);
335     qmp_assert_no_error(ret);
336
337     /* check there is at least an interface */
338     list = qdict_get_qlist(ret, "return");
339     entry = qlist_first(list);
340     g_assert(qdict_haskey(qobject_to_qdict(entry->value), "name"));
341
342     QDECREF(ret);
343 }
344
345 static void test_qga_file_ops(gconstpointer fix)
346 {
347     const TestFixture *fixture = fix;
348     const unsigned char helloworld[] = "Hello World!\n";
349     const char *b64;
350     gchar *cmd, *path, *enc;
351     unsigned char *dec;
352     QDict *ret, *val;
353     int64_t id, eof;
354     gsize count;
355     FILE *f;
356     char tmp[100];
357
358     /* open */
359     ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
360                  " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
361     g_assert_nonnull(ret);
362     qmp_assert_no_error(ret);
363     id = qdict_get_int(ret, "return");
364     QDECREF(ret);
365
366     enc = g_base64_encode(helloworld, sizeof(helloworld));
367     /* write */
368     cmd = g_strdup_printf("{'execute': 'guest-file-write',"
369                           " 'arguments': { 'handle': %" PRId64 ","
370                           " 'buf-b64': '%s' } }", id, enc);
371     ret = qmp_fd(fixture->fd, cmd);
372     g_assert_nonnull(ret);
373     qmp_assert_no_error(ret);
374
375     val = qdict_get_qdict(ret, "return");
376     count = qdict_get_int(val, "count");
377     eof = qdict_get_bool(val, "eof");
378     g_assert_cmpint(count, ==, sizeof(helloworld));
379     g_assert_cmpint(eof, ==, 0);
380     QDECREF(ret);
381     g_free(cmd);
382
383     /* flush */
384     cmd = g_strdup_printf("{'execute': 'guest-file-flush',"
385                           " 'arguments': {'handle': %" PRId64 "} }",
386                           id);
387     ret = qmp_fd(fixture->fd, cmd);
388     QDECREF(ret);
389     g_free(cmd);
390
391     /* close */
392     cmd = g_strdup_printf("{'execute': 'guest-file-close',"
393                           " 'arguments': {'handle': %" PRId64 "} }",
394                           id);
395     ret = qmp_fd(fixture->fd, cmd);
396     QDECREF(ret);
397     g_free(cmd);
398
399     /* check content */
400     path = g_build_filename(fixture->test_dir, "foo", NULL);
401     f = fopen(path, "r");
402     g_assert_nonnull(f);
403     count = fread(tmp, 1, sizeof(tmp), f);
404     g_assert_cmpint(count, ==, sizeof(helloworld));
405     tmp[count] = 0;
406     g_assert_cmpstr(tmp, ==, (char *)helloworld);
407     fclose(f);
408
409     /* open */
410     ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
411                  " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
412     g_assert_nonnull(ret);
413     qmp_assert_no_error(ret);
414     id = qdict_get_int(ret, "return");
415     QDECREF(ret);
416
417     /* read */
418     cmd = g_strdup_printf("{'execute': 'guest-file-read',"
419                           " 'arguments': { 'handle': %" PRId64 "} }",
420                           id);
421     ret = qmp_fd(fixture->fd, cmd);
422     val = qdict_get_qdict(ret, "return");
423     count = qdict_get_int(val, "count");
424     eof = qdict_get_bool(val, "eof");
425     b64 = qdict_get_str(val, "buf-b64");
426     g_assert_cmpint(count, ==, sizeof(helloworld));
427     g_assert(eof);
428     g_assert_cmpstr(b64, ==, enc);
429
430     QDECREF(ret);
431     g_free(cmd);
432     g_free(enc);
433
434     /* read eof */
435     cmd = g_strdup_printf("{'execute': 'guest-file-read',"
436                           " 'arguments': { 'handle': %" PRId64 "} }",
437                           id);
438     ret = qmp_fd(fixture->fd, cmd);
439     val = qdict_get_qdict(ret, "return");
440     count = qdict_get_int(val, "count");
441     eof = qdict_get_bool(val, "eof");
442     b64 = qdict_get_str(val, "buf-b64");
443     g_assert_cmpint(count, ==, 0);
444     g_assert(eof);
445     g_assert_cmpstr(b64, ==, "");
446     QDECREF(ret);
447     g_free(cmd);
448
449     /* seek */
450     cmd = g_strdup_printf("{'execute': 'guest-file-seek',"
451                           " 'arguments': { 'handle': %" PRId64 ", "
452                           " 'offset': %d, 'whence': '%s' } }",
453                           id, 6, "set");
454     ret = qmp_fd(fixture->fd, cmd);
455     qmp_assert_no_error(ret);
456     val = qdict_get_qdict(ret, "return");
457     count = qdict_get_int(val, "position");
458     eof = qdict_get_bool(val, "eof");
459     g_assert_cmpint(count, ==, 6);
460     g_assert(!eof);
461     QDECREF(ret);
462     g_free(cmd);
463
464     /* partial read */
465     cmd = g_strdup_printf("{'execute': 'guest-file-read',"
466                           " 'arguments': { 'handle': %" PRId64 "} }",
467                           id);
468     ret = qmp_fd(fixture->fd, cmd);
469     val = qdict_get_qdict(ret, "return");
470     count = qdict_get_int(val, "count");
471     eof = qdict_get_bool(val, "eof");
472     b64 = qdict_get_str(val, "buf-b64");
473     g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
474     g_assert(eof);
475     dec = g_base64_decode(b64, &count);
476     g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
477     g_assert_cmpmem(dec, count, helloworld + 6, sizeof(helloworld) - 6);
478     g_free(dec);
479
480     QDECREF(ret);
481     g_free(cmd);
482
483     /* close */
484     cmd = g_strdup_printf("{'execute': 'guest-file-close',"
485                           " 'arguments': {'handle': %" PRId64 "} }",
486                           id);
487     ret = qmp_fd(fixture->fd, cmd);
488     QDECREF(ret);
489     g_free(cmd);
490 }
491
492 static void test_qga_file_write_read(gconstpointer fix)
493 {
494     const TestFixture *fixture = fix;
495     const unsigned char helloworld[] = "Hello World!\n";
496     const char *b64;
497     gchar *cmd, *enc;
498     QDict *ret, *val;
499     int64_t id, eof;
500     gsize count;
501
502     /* open */
503     ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
504                  " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
505     g_assert_nonnull(ret);
506     qmp_assert_no_error(ret);
507     id = qdict_get_int(ret, "return");
508     QDECREF(ret);
509
510     enc = g_base64_encode(helloworld, sizeof(helloworld));
511     /* write */
512     cmd = g_strdup_printf("{'execute': 'guest-file-write',"
513                           " 'arguments': { 'handle': %" PRId64 ","
514                           " 'buf-b64': '%s' } }", id, enc);
515     ret = qmp_fd(fixture->fd, cmd);
516     g_assert_nonnull(ret);
517     qmp_assert_no_error(ret);
518
519     val = qdict_get_qdict(ret, "return");
520     count = qdict_get_int(val, "count");
521     eof = qdict_get_bool(val, "eof");
522     g_assert_cmpint(count, ==, sizeof(helloworld));
523     g_assert_cmpint(eof, ==, 0);
524     QDECREF(ret);
525     g_free(cmd);
526
527     /* read (check implicit flush) */
528     cmd = g_strdup_printf("{'execute': 'guest-file-read',"
529                           " 'arguments': { 'handle': %" PRId64 "} }",
530                           id);
531     ret = qmp_fd(fixture->fd, cmd);
532     val = qdict_get_qdict(ret, "return");
533     count = qdict_get_int(val, "count");
534     eof = qdict_get_bool(val, "eof");
535     b64 = qdict_get_str(val, "buf-b64");
536     g_assert_cmpint(count, ==, 0);
537     g_assert(eof);
538     g_assert_cmpstr(b64, ==, "");
539     QDECREF(ret);
540     g_free(cmd);
541
542     /* seek to 0 */
543     cmd = g_strdup_printf("{'execute': 'guest-file-seek',"
544                           " 'arguments': { 'handle': %" PRId64 ", "
545                           " 'offset': %d, 'whence': '%s' } }",
546                           id, 0, "set");
547     ret = qmp_fd(fixture->fd, cmd);
548     qmp_assert_no_error(ret);
549     val = qdict_get_qdict(ret, "return");
550     count = qdict_get_int(val, "position");
551     eof = qdict_get_bool(val, "eof");
552     g_assert_cmpint(count, ==, 0);
553     g_assert(!eof);
554     QDECREF(ret);
555     g_free(cmd);
556
557     /* read */
558     cmd = g_strdup_printf("{'execute': 'guest-file-read',"
559                           " 'arguments': { 'handle': %" PRId64 "} }",
560                           id);
561     ret = qmp_fd(fixture->fd, cmd);
562     val = qdict_get_qdict(ret, "return");
563     count = qdict_get_int(val, "count");
564     eof = qdict_get_bool(val, "eof");
565     b64 = qdict_get_str(val, "buf-b64");
566     g_assert_cmpint(count, ==, sizeof(helloworld));
567     g_assert(eof);
568     g_assert_cmpstr(b64, ==, enc);
569     QDECREF(ret);
570     g_free(cmd);
571     g_free(enc);
572
573     /* close */
574     cmd = g_strdup_printf("{'execute': 'guest-file-close',"
575                           " 'arguments': {'handle': %" PRId64 "} }",
576                           id);
577     ret = qmp_fd(fixture->fd, cmd);
578     QDECREF(ret);
579     g_free(cmd);
580 }
581
582 static void test_qga_get_time(gconstpointer fix)
583 {
584     const TestFixture *fixture = fix;
585     QDict *ret;
586     int64_t time;
587
588     ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
589     g_assert_nonnull(ret);
590     qmp_assert_no_error(ret);
591
592     time = qdict_get_int(ret, "return");
593     g_assert_cmpint(time, >, 0);
594
595     QDECREF(ret);
596 }
597
598 static void test_qga_set_time(gconstpointer fix)
599 {
600     const TestFixture *fixture = fix;
601     QDict *ret;
602     int64_t current, time;
603     gchar *cmd;
604
605     /* get current time */
606     ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
607     g_assert_nonnull(ret);
608     qmp_assert_no_error(ret);
609     current = qdict_get_int(ret, "return");
610     g_assert_cmpint(current, >, 0);
611     QDECREF(ret);
612
613     /* set some old time */
614     ret = qmp_fd(fixture->fd, "{'execute': 'guest-set-time',"
615                  " 'arguments': { 'time': 1000 } }");
616     g_assert_nonnull(ret);
617     qmp_assert_no_error(ret);
618     QDECREF(ret);
619
620     /* check old time */
621     ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
622     g_assert_nonnull(ret);
623     qmp_assert_no_error(ret);
624     time = qdict_get_int(ret, "return");
625     g_assert_cmpint(time / 1000, <, G_USEC_PER_SEC * 10);
626     QDECREF(ret);
627
628     /* set back current time */
629     cmd = g_strdup_printf("{'execute': 'guest-set-time',"
630                           " 'arguments': { 'time': %" PRId64 " } }",
631                           current + time * 1000);
632     ret = qmp_fd(fixture->fd, cmd);
633     g_free(cmd);
634     g_assert_nonnull(ret);
635     qmp_assert_no_error(ret);
636     QDECREF(ret);
637 }
638
639 static void test_qga_fstrim(gconstpointer fix)
640 {
641     const TestFixture *fixture = fix;
642     QDict *ret;
643     QList *list;
644     const QListEntry *entry;
645
646     ret = qmp_fd(fixture->fd, "{'execute': 'guest-fstrim',"
647                  " arguments: { minimum: 4194304 } }");
648     g_assert_nonnull(ret);
649     qmp_assert_no_error(ret);
650     list = qdict_get_qlist(ret, "return");
651     entry = qlist_first(list);
652     g_assert(qdict_haskey(qobject_to_qdict(entry->value), "paths"));
653
654     QDECREF(ret);
655 }
656
657 static void test_qga_blacklist(gconstpointer data)
658 {
659     TestFixture fix;
660     QDict *ret, *error;
661     const gchar *class, *desc;
662
663     fixture_setup(&fix, "-b guest-ping,guest-get-time");
664
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, ==, "GenericError");
672     g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
673     QDECREF(ret);
674
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, ==, "GenericError");
681     g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
682     QDECREF(ret);
683
684     /* check something work */
685     ret = qmp_fd(fix.fd, "{'execute': 'guest-get-fsinfo'}");
686     qmp_assert_no_error(ret);
687     QDECREF(ret);
688
689     fixture_tear_down(&fix, NULL);
690 }
691
692 static void test_qga_config(gconstpointer data)
693 {
694     GError *error = NULL;
695     char *cwd, *cmd, *out, *err, *str, **strv, *conf, **argv = NULL;
696     char *env[2];
697     int status, tmp;
698     gsize n;
699     GKeyFile *kf;
700     const char *qga_config =
701         "[general]\n"
702         "daemon=false\n"
703         "method=virtio-serial\n"
704         "path=/path/to/org.qemu.guest_agent.0\n"
705         "pidfile=/var/foo/qemu-ga.pid\n"
706         "statedir=/var/state\n"
707         "verbose=true\n"
708         "blacklist=guest-ping;guest-get-time\n";
709
710     tmp = g_file_open_tmp(NULL, &conf, &error);
711     g_assert_no_error(error);
712     g_assert_cmpint(tmp, >=, 0);
713     g_assert_cmpstr(conf, !=, "");
714
715     g_file_set_contents(conf, qga_config, -1, &error);
716     g_assert_no_error(error);
717
718     cwd = g_get_current_dir();
719     cmd = g_strdup_printf("%s%cqemu-ga -D",
720                           cwd, G_DIR_SEPARATOR);
721     g_shell_parse_argv(cmd, NULL, &argv, &error);
722     g_assert_no_error(error);
723
724     env[0] = g_strdup_printf("QGA_CONF=%s", conf);
725     env[1] = NULL;
726     g_spawn_sync(NULL, argv, env, 0,
727                  NULL, NULL, &out, &err, &status, &error);
728     g_assert_no_error(error);
729     g_assert_cmpstr(err, ==, "");
730     g_assert_cmpint(status, ==, 0);
731
732     kf = g_key_file_new();
733     g_key_file_load_from_data(kf, out, -1, G_KEY_FILE_NONE, &error);
734     g_assert_no_error(error);
735
736     str = g_key_file_get_start_group(kf);
737     g_assert_cmpstr(str, ==, "general");
738     g_free(str);
739
740     g_assert_false(g_key_file_get_boolean(kf, "general", "daemon", &error));
741     g_assert_no_error(error);
742
743     str = g_key_file_get_string(kf, "general", "method", &error);
744     g_assert_no_error(error);
745     g_assert_cmpstr(str, ==, "virtio-serial");
746     g_free(str);
747
748     str = g_key_file_get_string(kf, "general", "path", &error);
749     g_assert_no_error(error);
750     g_assert_cmpstr(str, ==, "/path/to/org.qemu.guest_agent.0");
751     g_free(str);
752
753     str = g_key_file_get_string(kf, "general", "pidfile", &error);
754     g_assert_no_error(error);
755     g_assert_cmpstr(str, ==, "/var/foo/qemu-ga.pid");
756     g_free(str);
757
758     str = g_key_file_get_string(kf, "general", "statedir", &error);
759     g_assert_no_error(error);
760     g_assert_cmpstr(str, ==, "/var/state");
761     g_free(str);
762
763     g_assert_true(g_key_file_get_boolean(kf, "general", "verbose", &error));
764     g_assert_no_error(error);
765
766     strv = g_key_file_get_string_list(kf, "general", "blacklist", &n, &error);
767     g_assert_cmpint(n, ==, 2);
768 #if GLIB_CHECK_VERSION(2, 44, 0)
769     g_assert_true(g_strv_contains((const char * const *)strv,
770                                   "guest-ping"));
771     g_assert_true(g_strv_contains((const char * const *)strv,
772                                   "guest-get-time"));
773 #endif
774     g_assert_no_error(error);
775     g_strfreev(strv);
776
777     g_free(out);
778     g_free(err);
779     g_free(conf);
780     g_free(env[0]);
781     g_key_file_free(kf);
782
783     close(tmp);
784 }
785
786 static void test_qga_fsfreeze_status(gconstpointer fix)
787 {
788     const TestFixture *fixture = fix;
789     QDict *ret;
790     const gchar *status;
791
792     ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-status'}");
793     g_assert_nonnull(ret);
794     qmp_assert_no_error(ret);
795
796     status = qdict_get_try_str(ret, "return");
797     g_assert_cmpstr(status, ==, "thawed");
798
799     QDECREF(ret);
800 }
801
802 static void test_qga_fsfreeze_and_thaw(gconstpointer fix)
803 {
804     const TestFixture *fixture = fix;
805     QDict *ret;
806     const gchar *status;
807
808     ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-freeze'}");
809     g_assert_nonnull(ret);
810     qmp_assert_no_error(ret);
811     QDECREF(ret);
812
813     ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-status'}");
814     g_assert_nonnull(ret);
815     qmp_assert_no_error(ret);
816     status = qdict_get_try_str(ret, "return");
817     g_assert_cmpstr(status, ==, "frozen");
818     QDECREF(ret);
819
820     ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-thaw'}");
821     g_assert_nonnull(ret);
822     qmp_assert_no_error(ret);
823     QDECREF(ret);
824 }
825
826 int main(int argc, char **argv)
827 {
828     TestFixture fix;
829     int ret;
830
831     setlocale (LC_ALL, "");
832     g_test_init(&argc, &argv, NULL);
833     fixture_setup(&fix, NULL);
834
835     g_test_add_data_func("/qga/sync-delimited", &fix, test_qga_sync_delimited);
836     g_test_add_data_func("/qga/sync", &fix, test_qga_sync);
837     g_test_add_data_func("/qga/ping", &fix, test_qga_ping);
838     g_test_add_data_func("/qga/info", &fix, test_qga_info);
839     g_test_add_data_func("/qga/network-get-interfaces", &fix,
840                          test_qga_network_get_interfaces);
841     g_test_add_data_func("/qga/get-vcpus", &fix, test_qga_get_vcpus);
842     g_test_add_data_func("/qga/get-fsinfo", &fix, test_qga_get_fsinfo);
843     g_test_add_data_func("/qga/get-memory-block-info", &fix,
844                          test_qga_get_memory_block_info);
845     g_test_add_data_func("/qga/get-memory-blocks", &fix,
846                          test_qga_get_memory_blocks);
847     g_test_add_data_func("/qga/file-ops", &fix, test_qga_file_ops);
848     g_test_add_data_func("/qga/file-write-read", &fix, test_qga_file_write_read);
849     g_test_add_data_func("/qga/get-time", &fix, test_qga_get_time);
850     g_test_add_data_func("/qga/invalid-cmd", &fix, test_qga_invalid_cmd);
851     g_test_add_data_func("/qga/fsfreeze-status", &fix,
852                          test_qga_fsfreeze_status);
853
854     g_test_add_data_func("/qga/blacklist", NULL, test_qga_blacklist);
855     g_test_add_data_func("/qga/config", NULL, test_qga_config);
856
857     if (g_getenv("QGA_TEST_SIDE_EFFECTING")) {
858         g_test_add_data_func("/qga/fsfreeze-and-thaw", &fix,
859                              test_qga_fsfreeze_and_thaw);
860         g_test_add_data_func("/qga/set-time", &fix, test_qga_set_time);
861         g_test_add_data_func("/qga/fstrim", &fix, test_qga_fstrim);
862     }
863
864     ret = g_test_run();
865
866     fixture_tear_down(&fix, NULL);
867
868     return ret;
869 }