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