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