]> Git Repo - qemu.git/blob - tests/test-qga.c
Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[qemu.git] / tests / test-qga.c
1 #include "qemu/osdep.h"
2 #include <locale.h>
3 #include <glib/gstdio.h>
4 #include <sys/socket.h>
5 #include <sys/un.h>
6
7 #include "libqtest.h"
8 #include "qapi/qmp/qdict.h"
9 #include "qapi/qmp/qlist.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, gchar **envp)
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, envp,
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("\xff{'execute': 'guest-sync-delimited',"
152                           " 'arguments': {'id': %u } }", r);
153     qmp_fd_send(fixture->fd, cmd);
154     g_free(cmd);
155
156     /*
157      * Read and ignore garbage until resynchronized.
158      *
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.
165      *
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.
170      */
171     do {
172         v = read(fixture->fd, &c, 1);
173         g_assert_cmpint(v, ==, 1);
174     } while (c != 0xff);
175
176     ret = qmp_fd_receive(fixture->fd);
177     g_assert_nonnull(ret);
178     qmp_assert_no_error(ret);
179
180     v = qdict_get_int(ret, "return");
181     g_assert_cmpint(r, ==, v);
182
183     QDECREF(ret);
184 }
185
186 static void test_qga_sync(gconstpointer fix)
187 {
188     const TestFixture *fixture = fix;
189     guint32 v, r = g_random_int();
190     QDict *ret;
191     gchar *cmd;
192
193     /*
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.
203      */
204     cmd = g_strdup_printf("{'execute': 'guest-sync',"
205                           " 'arguments': {'id': %u } }", r);
206     ret = qmp_fd(fixture->fd, cmd);
207     g_free(cmd);
208
209     g_assert_nonnull(ret);
210     qmp_assert_no_error(ret);
211
212     v = qdict_get_int(ret, "return");
213     g_assert_cmpint(r, ==, v);
214
215     QDECREF(ret);
216 }
217
218 static void test_qga_ping(gconstpointer fix)
219 {
220     const TestFixture *fixture = fix;
221     QDict *ret;
222
223     ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping'}");
224     g_assert_nonnull(ret);
225     qmp_assert_no_error(ret);
226
227     QDECREF(ret);
228 }
229
230 static void test_qga_invalid_args(gconstpointer fix)
231 {
232     const TestFixture *fixture = fix;
233     QDict *ret, *error;
234     const gchar *class, *desc;
235
236     ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping', "
237                  "'arguments': {'foo': 42 }}");
238     g_assert_nonnull(ret);
239
240     error = qdict_get_qdict(ret, "error");
241     class = qdict_get_try_str(error, "class");
242     desc = qdict_get_try_str(error, "desc");
243
244     g_assert_cmpstr(class, ==, "GenericError");
245     g_assert_cmpstr(desc, ==, "Parameter 'foo' is unexpected");
246
247     QDECREF(ret);
248 }
249
250 static void test_qga_invalid_cmd(gconstpointer fix)
251 {
252     const TestFixture *fixture = fix;
253     QDict *ret, *error;
254     const gchar *class, *desc;
255
256     ret = qmp_fd(fixture->fd, "{'execute': 'guest-invalid-cmd'}");
257     g_assert_nonnull(ret);
258
259     error = qdict_get_qdict(ret, "error");
260     class = qdict_get_try_str(error, "class");
261     desc = qdict_get_try_str(error, "desc");
262
263     g_assert_cmpstr(class, ==, "CommandNotFound");
264     g_assert_cmpint(strlen(desc), >, 0);
265
266     QDECREF(ret);
267 }
268
269 static void test_qga_info(gconstpointer fix)
270 {
271     const TestFixture *fixture = fix;
272     QDict *ret, *val;
273     const gchar *version;
274
275     ret = qmp_fd(fixture->fd, "{'execute': 'guest-info'}");
276     g_assert_nonnull(ret);
277     qmp_assert_no_error(ret);
278
279     val = qdict_get_qdict(ret, "return");
280     version = qdict_get_try_str(val, "version");
281     g_assert_cmpstr(version, ==, QEMU_VERSION);
282
283     QDECREF(ret);
284 }
285
286 static void test_qga_get_vcpus(gconstpointer fix)
287 {
288     const TestFixture *fixture = fix;
289     QDict *ret;
290     QList *list;
291     const QListEntry *entry;
292
293     ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-vcpus'}");
294     g_assert_nonnull(ret);
295     qmp_assert_no_error(ret);
296
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"));
302
303     QDECREF(ret);
304 }
305
306 static void test_qga_get_fsinfo(gconstpointer fix)
307 {
308     const TestFixture *fixture = fix;
309     QDict *ret;
310     QList *list;
311     const QListEntry *entry;
312
313     ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-fsinfo'}");
314     g_assert_nonnull(ret);
315     qmp_assert_no_error(ret);
316
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"));
325     }
326
327     QDECREF(ret);
328 }
329
330 static void test_qga_get_memory_block_info(gconstpointer fix)
331 {
332     const TestFixture *fixture = fix;
333     QDict *ret, *val;
334     int64_t size;
335
336     ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-block-info'}");
337     g_assert_nonnull(ret);
338
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);
345     }
346
347     QDECREF(ret);
348 }
349
350 static void test_qga_get_memory_blocks(gconstpointer fix)
351 {
352     const TestFixture *fixture = fix;
353     QDict *ret;
354     QList *list;
355     const QListEntry *entry;
356
357     ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-blocks'}");
358     g_assert_nonnull(ret);
359
360     /* some systems might not expose memory block info in sysfs */
361     if (!qdict_haskey(ret, "error")) {
362         list = qdict_get_qlist(ret, "return");
363         entry = qlist_first(list);
364         /* newer versions of qga may return empty list without error */
365         if (entry) {
366             g_assert(qdict_haskey(qobject_to(QDict, entry->value),
367                                   "phys-index"));
368             g_assert(qdict_haskey(qobject_to(QDict, entry->value), "online"));
369         }
370     }
371
372     QDECREF(ret);
373 }
374
375 static void test_qga_network_get_interfaces(gconstpointer fix)
376 {
377     const TestFixture *fixture = fix;
378     QDict *ret;
379     QList *list;
380     const QListEntry *entry;
381
382     ret = qmp_fd(fixture->fd, "{'execute': 'guest-network-get-interfaces'}");
383     g_assert_nonnull(ret);
384     qmp_assert_no_error(ret);
385
386     /* check there is at least an interface */
387     list = qdict_get_qlist(ret, "return");
388     entry = qlist_first(list);
389     g_assert(qdict_haskey(qobject_to(QDict, entry->value), "name"));
390
391     QDECREF(ret);
392 }
393
394 static void test_qga_file_ops(gconstpointer fix)
395 {
396     const TestFixture *fixture = fix;
397     const unsigned char helloworld[] = "Hello World!\n";
398     const char *b64;
399     gchar *cmd, *path, *enc;
400     unsigned char *dec;
401     QDict *ret, *val;
402     int64_t id, eof;
403     gsize count;
404     FILE *f;
405     char tmp[100];
406
407     /* open */
408     ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
409                  " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
410     g_assert_nonnull(ret);
411     qmp_assert_no_error(ret);
412     id = qdict_get_int(ret, "return");
413     QDECREF(ret);
414
415     enc = g_base64_encode(helloworld, sizeof(helloworld));
416     /* write */
417     cmd = g_strdup_printf("{'execute': 'guest-file-write',"
418                           " 'arguments': { 'handle': %" PRId64 ","
419                           " 'buf-b64': '%s' } }", id, enc);
420     ret = qmp_fd(fixture->fd, cmd);
421     g_assert_nonnull(ret);
422     qmp_assert_no_error(ret);
423
424     val = qdict_get_qdict(ret, "return");
425     count = qdict_get_int(val, "count");
426     eof = qdict_get_bool(val, "eof");
427     g_assert_cmpint(count, ==, sizeof(helloworld));
428     g_assert_cmpint(eof, ==, 0);
429     QDECREF(ret);
430     g_free(cmd);
431
432     /* flush */
433     cmd = g_strdup_printf("{'execute': 'guest-file-flush',"
434                           " 'arguments': {'handle': %" PRId64 "} }",
435                           id);
436     ret = qmp_fd(fixture->fd, cmd);
437     QDECREF(ret);
438     g_free(cmd);
439
440     /* close */
441     cmd = g_strdup_printf("{'execute': 'guest-file-close',"
442                           " 'arguments': {'handle': %" PRId64 "} }",
443                           id);
444     ret = qmp_fd(fixture->fd, cmd);
445     QDECREF(ret);
446     g_free(cmd);
447
448     /* check content */
449     path = g_build_filename(fixture->test_dir, "foo", NULL);
450     f = fopen(path, "r");
451     g_free(path);
452     g_assert_nonnull(f);
453     count = fread(tmp, 1, sizeof(tmp), f);
454     g_assert_cmpint(count, ==, sizeof(helloworld));
455     tmp[count] = 0;
456     g_assert_cmpstr(tmp, ==, (char *)helloworld);
457     fclose(f);
458
459     /* open */
460     ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
461                  " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
462     g_assert_nonnull(ret);
463     qmp_assert_no_error(ret);
464     id = qdict_get_int(ret, "return");
465     QDECREF(ret);
466
467     /* read */
468     cmd = g_strdup_printf("{'execute': 'guest-file-read',"
469                           " 'arguments': { 'handle': %" PRId64 "} }",
470                           id);
471     ret = qmp_fd(fixture->fd, cmd);
472     val = qdict_get_qdict(ret, "return");
473     count = qdict_get_int(val, "count");
474     eof = qdict_get_bool(val, "eof");
475     b64 = qdict_get_str(val, "buf-b64");
476     g_assert_cmpint(count, ==, sizeof(helloworld));
477     g_assert(eof);
478     g_assert_cmpstr(b64, ==, enc);
479
480     QDECREF(ret);
481     g_free(cmd);
482     g_free(enc);
483
484     /* read eof */
485     cmd = g_strdup_printf("{'execute': 'guest-file-read',"
486                           " 'arguments': { 'handle': %" PRId64 "} }",
487                           id);
488     ret = qmp_fd(fixture->fd, cmd);
489     val = qdict_get_qdict(ret, "return");
490     count = qdict_get_int(val, "count");
491     eof = qdict_get_bool(val, "eof");
492     b64 = qdict_get_str(val, "buf-b64");
493     g_assert_cmpint(count, ==, 0);
494     g_assert(eof);
495     g_assert_cmpstr(b64, ==, "");
496     QDECREF(ret);
497     g_free(cmd);
498
499     /* seek */
500     cmd = g_strdup_printf("{'execute': 'guest-file-seek',"
501                           " 'arguments': { 'handle': %" PRId64 ", "
502                           " 'offset': %d, 'whence': '%s' } }",
503                           id, 6, "set");
504     ret = qmp_fd(fixture->fd, cmd);
505     qmp_assert_no_error(ret);
506     val = qdict_get_qdict(ret, "return");
507     count = qdict_get_int(val, "position");
508     eof = qdict_get_bool(val, "eof");
509     g_assert_cmpint(count, ==, 6);
510     g_assert(!eof);
511     QDECREF(ret);
512     g_free(cmd);
513
514     /* partial read */
515     cmd = g_strdup_printf("{'execute': 'guest-file-read',"
516                           " 'arguments': { 'handle': %" PRId64 "} }",
517                           id);
518     ret = qmp_fd(fixture->fd, cmd);
519     val = qdict_get_qdict(ret, "return");
520     count = qdict_get_int(val, "count");
521     eof = qdict_get_bool(val, "eof");
522     b64 = qdict_get_str(val, "buf-b64");
523     g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
524     g_assert(eof);
525     dec = g_base64_decode(b64, &count);
526     g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
527     g_assert_cmpmem(dec, count, helloworld + 6, sizeof(helloworld) - 6);
528     g_free(dec);
529
530     QDECREF(ret);
531     g_free(cmd);
532
533     /* close */
534     cmd = g_strdup_printf("{'execute': 'guest-file-close',"
535                           " 'arguments': {'handle': %" PRId64 "} }",
536                           id);
537     ret = qmp_fd(fixture->fd, cmd);
538     QDECREF(ret);
539     g_free(cmd);
540 }
541
542 static void test_qga_file_write_read(gconstpointer fix)
543 {
544     const TestFixture *fixture = fix;
545     const unsigned char helloworld[] = "Hello World!\n";
546     const char *b64;
547     gchar *cmd, *enc;
548     QDict *ret, *val;
549     int64_t id, eof;
550     gsize count;
551
552     /* open */
553     ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
554                  " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
555     g_assert_nonnull(ret);
556     qmp_assert_no_error(ret);
557     id = qdict_get_int(ret, "return");
558     QDECREF(ret);
559
560     enc = g_base64_encode(helloworld, sizeof(helloworld));
561     /* write */
562     cmd = g_strdup_printf("{'execute': 'guest-file-write',"
563                           " 'arguments': { 'handle': %" PRId64 ","
564                           " 'buf-b64': '%s' } }", id, enc);
565     ret = qmp_fd(fixture->fd, cmd);
566     g_assert_nonnull(ret);
567     qmp_assert_no_error(ret);
568
569     val = qdict_get_qdict(ret, "return");
570     count = qdict_get_int(val, "count");
571     eof = qdict_get_bool(val, "eof");
572     g_assert_cmpint(count, ==, sizeof(helloworld));
573     g_assert_cmpint(eof, ==, 0);
574     QDECREF(ret);
575     g_free(cmd);
576
577     /* read (check implicit flush) */
578     cmd = g_strdup_printf("{'execute': 'guest-file-read',"
579                           " 'arguments': { 'handle': %" PRId64 "} }",
580                           id);
581     ret = qmp_fd(fixture->fd, cmd);
582     val = qdict_get_qdict(ret, "return");
583     count = qdict_get_int(val, "count");
584     eof = qdict_get_bool(val, "eof");
585     b64 = qdict_get_str(val, "buf-b64");
586     g_assert_cmpint(count, ==, 0);
587     g_assert(eof);
588     g_assert_cmpstr(b64, ==, "");
589     QDECREF(ret);
590     g_free(cmd);
591
592     /* seek to 0 */
593     cmd = g_strdup_printf("{'execute': 'guest-file-seek',"
594                           " 'arguments': { 'handle': %" PRId64 ", "
595                           " 'offset': %d, 'whence': '%s' } }",
596                           id, 0, "set");
597     ret = qmp_fd(fixture->fd, cmd);
598     qmp_assert_no_error(ret);
599     val = qdict_get_qdict(ret, "return");
600     count = qdict_get_int(val, "position");
601     eof = qdict_get_bool(val, "eof");
602     g_assert_cmpint(count, ==, 0);
603     g_assert(!eof);
604     QDECREF(ret);
605     g_free(cmd);
606
607     /* read */
608     cmd = g_strdup_printf("{'execute': 'guest-file-read',"
609                           " 'arguments': { 'handle': %" PRId64 "} }",
610                           id);
611     ret = qmp_fd(fixture->fd, cmd);
612     val = qdict_get_qdict(ret, "return");
613     count = qdict_get_int(val, "count");
614     eof = qdict_get_bool(val, "eof");
615     b64 = qdict_get_str(val, "buf-b64");
616     g_assert_cmpint(count, ==, sizeof(helloworld));
617     g_assert(eof);
618     g_assert_cmpstr(b64, ==, enc);
619     QDECREF(ret);
620     g_free(cmd);
621     g_free(enc);
622
623     /* close */
624     cmd = g_strdup_printf("{'execute': 'guest-file-close',"
625                           " 'arguments': {'handle': %" PRId64 "} }",
626                           id);
627     ret = qmp_fd(fixture->fd, cmd);
628     QDECREF(ret);
629     g_free(cmd);
630 }
631
632 static void test_qga_get_time(gconstpointer fix)
633 {
634     const TestFixture *fixture = fix;
635     QDict *ret;
636     int64_t time;
637
638     ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
639     g_assert_nonnull(ret);
640     qmp_assert_no_error(ret);
641
642     time = qdict_get_int(ret, "return");
643     g_assert_cmpint(time, >, 0);
644
645     QDECREF(ret);
646 }
647
648 static void test_qga_blacklist(gconstpointer data)
649 {
650     TestFixture fix;
651     QDict *ret, *error;
652     const gchar *class, *desc;
653
654     fixture_setup(&fix, "-b guest-ping,guest-get-time", NULL);
655
656     /* check blacklist */
657     ret = qmp_fd(fix.fd, "{'execute': 'guest-ping'}");
658     g_assert_nonnull(ret);
659     error = qdict_get_qdict(ret, "error");
660     class = qdict_get_try_str(error, "class");
661     desc = qdict_get_try_str(error, "desc");
662     g_assert_cmpstr(class, ==, "GenericError");
663     g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
664     QDECREF(ret);
665
666     ret = qmp_fd(fix.fd, "{'execute': 'guest-get-time'}");
667     g_assert_nonnull(ret);
668     error = qdict_get_qdict(ret, "error");
669     class = qdict_get_try_str(error, "class");
670     desc = qdict_get_try_str(error, "desc");
671     g_assert_cmpstr(class, ==, "GenericError");
672     g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
673     QDECREF(ret);
674
675     /* check something work */
676     ret = qmp_fd(fix.fd, "{'execute': 'guest-get-fsinfo'}");
677     qmp_assert_no_error(ret);
678     QDECREF(ret);
679
680     fixture_tear_down(&fix, NULL);
681 }
682
683 static void test_qga_config(gconstpointer data)
684 {
685     GError *error = NULL;
686     char *cwd, *cmd, *out, *err, *str, **strv, **argv = NULL;
687     char *env[2];
688     int status;
689     gsize n;
690     GKeyFile *kf;
691
692     cwd = g_get_current_dir();
693     cmd = g_strdup_printf("%s%cqemu-ga -D",
694                           cwd, G_DIR_SEPARATOR);
695     g_free(cwd);
696     g_shell_parse_argv(cmd, NULL, &argv, &error);
697     g_free(cmd);
698     g_assert_no_error(error);
699
700     env[0] = g_strdup_printf("QGA_CONF=tests%cdata%ctest-qga-config",
701                              G_DIR_SEPARATOR, G_DIR_SEPARATOR);
702     env[1] = NULL;
703     g_spawn_sync(NULL, argv, env, 0,
704                  NULL, NULL, &out, &err, &status, &error);
705     g_strfreev(argv);
706
707     g_assert_no_error(error);
708     g_assert_cmpstr(err, ==, "");
709     g_assert_cmpint(status, ==, 0);
710
711     kf = g_key_file_new();
712     g_key_file_load_from_data(kf, out, -1, G_KEY_FILE_NONE, &error);
713     g_assert_no_error(error);
714
715     str = g_key_file_get_start_group(kf);
716     g_assert_cmpstr(str, ==, "general");
717     g_free(str);
718
719     g_assert_false(g_key_file_get_boolean(kf, "general", "daemon", &error));
720     g_assert_no_error(error);
721
722     str = g_key_file_get_string(kf, "general", "method", &error);
723     g_assert_no_error(error);
724     g_assert_cmpstr(str, ==, "virtio-serial");
725     g_free(str);
726
727     str = g_key_file_get_string(kf, "general", "path", &error);
728     g_assert_no_error(error);
729     g_assert_cmpstr(str, ==, "/path/to/org.qemu.guest_agent.0");
730     g_free(str);
731
732     str = g_key_file_get_string(kf, "general", "pidfile", &error);
733     g_assert_no_error(error);
734     g_assert_cmpstr(str, ==, "/var/foo/qemu-ga.pid");
735     g_free(str);
736
737     str = g_key_file_get_string(kf, "general", "statedir", &error);
738     g_assert_no_error(error);
739     g_assert_cmpstr(str, ==, "/var/state");
740     g_free(str);
741
742     g_assert_true(g_key_file_get_boolean(kf, "general", "verbose", &error));
743     g_assert_no_error(error);
744
745     strv = g_key_file_get_string_list(kf, "general", "blacklist", &n, &error);
746     g_assert_cmpint(n, ==, 2);
747 #if GLIB_CHECK_VERSION(2, 44, 0)
748     g_assert_true(g_strv_contains((const char * const *)strv,
749                                   "guest-ping"));
750     g_assert_true(g_strv_contains((const char * const *)strv,
751                                   "guest-get-time"));
752 #endif
753     g_assert_no_error(error);
754     g_strfreev(strv);
755
756     g_free(out);
757     g_free(err);
758     g_free(env[0]);
759     g_key_file_free(kf);
760 }
761
762 static void test_qga_fsfreeze_status(gconstpointer fix)
763 {
764     const TestFixture *fixture = fix;
765     QDict *ret;
766     const gchar *status;
767
768     ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-status'}");
769     g_assert_nonnull(ret);
770     qmp_assert_no_error(ret);
771
772     status = qdict_get_try_str(ret, "return");
773     g_assert_cmpstr(status, ==, "thawed");
774
775     QDECREF(ret);
776 }
777
778 static void test_qga_guest_exec(gconstpointer fix)
779 {
780     const TestFixture *fixture = fix;
781     QDict *ret, *val;
782     const gchar *out;
783     guchar *decoded;
784     int64_t pid, now, exitcode;
785     gsize len;
786     bool exited;
787     char *cmd;
788
789     /* exec 'echo foo bar' */
790     ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
791                  " 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ],"
792                  " 'capture-output': true } }");
793     g_assert_nonnull(ret);
794     qmp_assert_no_error(ret);
795     val = qdict_get_qdict(ret, "return");
796     pid = qdict_get_int(val, "pid");
797     g_assert_cmpint(pid, >, 0);
798     QDECREF(ret);
799
800     /* wait for completion */
801     now = g_get_monotonic_time();
802     cmd = g_strdup_printf("{'execute': 'guest-exec-status',"
803                           " 'arguments': { 'pid': %" PRId64 " } }", pid);
804     do {
805         ret = qmp_fd(fixture->fd, cmd);
806         g_assert_nonnull(ret);
807         val = qdict_get_qdict(ret, "return");
808         exited = qdict_get_bool(val, "exited");
809         if (!exited) {
810             QDECREF(ret);
811         }
812     } while (!exited &&
813              g_get_monotonic_time() < now + 5 * G_TIME_SPAN_SECOND);
814     g_assert(exited);
815     g_free(cmd);
816
817     /* check stdout */
818     exitcode = qdict_get_int(val, "exitcode");
819     g_assert_cmpint(exitcode, ==, 0);
820     out = qdict_get_str(val, "out-data");
821     decoded = g_base64_decode(out, &len);
822     g_assert_cmpint(len, ==, 12);
823     g_assert_cmpstr((char *)decoded, ==, "\" test_str \"");
824     g_free(decoded);
825     QDECREF(ret);
826 }
827
828 static void test_qga_guest_exec_invalid(gconstpointer fix)
829 {
830     const TestFixture *fixture = fix;
831     QDict *ret, *error;
832     const gchar *class, *desc;
833
834     /* invalid command */
835     ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
836                  " 'path': '/bin/invalid-cmd42' } }");
837     g_assert_nonnull(ret);
838     error = qdict_get_qdict(ret, "error");
839     g_assert_nonnull(error);
840     class = qdict_get_str(error, "class");
841     desc = qdict_get_str(error, "desc");
842     g_assert_cmpstr(class, ==, "GenericError");
843     g_assert_cmpint(strlen(desc), >, 0);
844     QDECREF(ret);
845
846     /* invalid pid */
847     ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec-status',"
848                  " 'arguments': { 'pid': 0 } }");
849     g_assert_nonnull(ret);
850     error = qdict_get_qdict(ret, "error");
851     g_assert_nonnull(error);
852     class = qdict_get_str(error, "class");
853     desc = qdict_get_str(error, "desc");
854     g_assert_cmpstr(class, ==, "GenericError");
855     g_assert_cmpint(strlen(desc), >, 0);
856     QDECREF(ret);
857 }
858
859 static void test_qga_guest_get_osinfo(gconstpointer data)
860 {
861     TestFixture fixture;
862     const gchar *str;
863     gchar *cwd, *env[2];
864     QDict *ret, *val;
865
866     cwd = g_get_current_dir();
867     env[0] = g_strdup_printf(
868         "QGA_OS_RELEASE=%s%ctests%cdata%ctest-qga-os-release",
869         cwd, G_DIR_SEPARATOR, G_DIR_SEPARATOR, G_DIR_SEPARATOR);
870     env[1] = NULL;
871     g_free(cwd);
872     fixture_setup(&fixture, NULL, env);
873
874     ret = qmp_fd(fixture.fd, "{'execute': 'guest-get-osinfo'}");
875     g_assert_nonnull(ret);
876     qmp_assert_no_error(ret);
877
878     val = qdict_get_qdict(ret, "return");
879
880     str = qdict_get_try_str(val, "id");
881     g_assert_nonnull(str);
882     g_assert_cmpstr(str, ==, "qemu-ga-test");
883
884     str = qdict_get_try_str(val, "name");
885     g_assert_nonnull(str);
886     g_assert_cmpstr(str, ==, "QEMU-GA");
887
888     str = qdict_get_try_str(val, "pretty-name");
889     g_assert_nonnull(str);
890     g_assert_cmpstr(str, ==, "QEMU Guest Agent test");
891
892     str = qdict_get_try_str(val, "version");
893     g_assert_nonnull(str);
894     g_assert_cmpstr(str, ==, "Test 1");
895
896     str = qdict_get_try_str(val, "version-id");
897     g_assert_nonnull(str);
898     g_assert_cmpstr(str, ==, "1");
899
900     str = qdict_get_try_str(val, "variant");
901     g_assert_nonnull(str);
902     g_assert_cmpstr(str, ==, "Unit test \"'$`\\ and \\\\ etc.");
903
904     str = qdict_get_try_str(val, "variant-id");
905     g_assert_nonnull(str);
906     g_assert_cmpstr(str, ==, "unit-test");
907
908     QDECREF(ret);
909     g_free(env[0]);
910     fixture_tear_down(&fixture, NULL);
911 }
912
913 int main(int argc, char **argv)
914 {
915     TestFixture fix;
916     int ret;
917
918     setlocale (LC_ALL, "");
919     g_test_init(&argc, &argv, NULL);
920     fixture_setup(&fix, NULL, NULL);
921
922     g_test_add_data_func("/qga/sync-delimited", &fix, test_qga_sync_delimited);
923     g_test_add_data_func("/qga/sync", &fix, test_qga_sync);
924     g_test_add_data_func("/qga/ping", &fix, test_qga_ping);
925     g_test_add_data_func("/qga/info", &fix, test_qga_info);
926     g_test_add_data_func("/qga/network-get-interfaces", &fix,
927                          test_qga_network_get_interfaces);
928     if (!access("/sys/devices/system/cpu/cpu0", F_OK)) {
929         g_test_add_data_func("/qga/get-vcpus", &fix, test_qga_get_vcpus);
930     }
931     g_test_add_data_func("/qga/get-fsinfo", &fix, test_qga_get_fsinfo);
932     g_test_add_data_func("/qga/get-memory-block-info", &fix,
933                          test_qga_get_memory_block_info);
934     g_test_add_data_func("/qga/get-memory-blocks", &fix,
935                          test_qga_get_memory_blocks);
936     g_test_add_data_func("/qga/file-ops", &fix, test_qga_file_ops);
937     g_test_add_data_func("/qga/file-write-read", &fix, test_qga_file_write_read);
938     g_test_add_data_func("/qga/get-time", &fix, test_qga_get_time);
939     g_test_add_data_func("/qga/invalid-cmd", &fix, test_qga_invalid_cmd);
940     g_test_add_data_func("/qga/invalid-args", &fix, test_qga_invalid_args);
941     g_test_add_data_func("/qga/fsfreeze-status", &fix,
942                          test_qga_fsfreeze_status);
943
944     g_test_add_data_func("/qga/blacklist", NULL, test_qga_blacklist);
945     g_test_add_data_func("/qga/config", NULL, test_qga_config);
946     g_test_add_data_func("/qga/guest-exec", &fix, test_qga_guest_exec);
947     g_test_add_data_func("/qga/guest-exec-invalid", &fix,
948                          test_qga_guest_exec_invalid);
949     g_test_add_data_func("/qga/guest-get-osinfo", &fix,
950                          test_qga_guest_get_osinfo);
951
952     ret = g_test_run();
953
954     fixture_tear_down(&fix, NULL);
955
956     return ret;
957 }
This page took 0.077563 seconds and 4 git commands to generate.