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