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