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