]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-qga.c
Merge remote-tracking branch 'remotes/kraxel/tags/pull-input-20151020-1' into staging
[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 /* check there is at least a fs */
277 list = qdict_get_qlist(ret, "return");
278 entry = qlist_first(list);
279 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "name"));
280 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "mountpoint"));
281 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "type"));
282 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "disk"));
283
284 QDECREF(ret);
285 }
286
287 static void test_qga_get_memory_block_info(gconstpointer fix)
288 {
289 const TestFixture *fixture = fix;
290 QDict *ret, *val;
291 int64_t size;
292
293 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-block-info'}");
294 g_assert_nonnull(ret);
295
296 /* some systems might not expose memory block info in sysfs */
297 if (!qdict_haskey(ret, "error")) {
298 /* check there is at least some memory */
299 val = qdict_get_qdict(ret, "return");
300 size = qdict_get_int(val, "size");
301 g_assert_cmpint(size, >, 0);
302 }
303
304 QDECREF(ret);
305 }
306
307 static void test_qga_get_memory_blocks(gconstpointer fix)
308 {
309 const TestFixture *fixture = fix;
310 QDict *ret;
311 QList *list;
312 const QListEntry *entry;
313
314 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-blocks'}");
315 g_assert_nonnull(ret);
316
317 /* some systems might not expose memory block info in sysfs */
318 if (!qdict_haskey(ret, "error")) {
319 list = qdict_get_qlist(ret, "return");
320 entry = qlist_first(list);
321 /* newer versions of qga may return empty list without error */
322 if (entry) {
323 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "phys-index"));
324 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "online"));
325 }
326 }
327
328 QDECREF(ret);
329 }
330
331 static void test_qga_network_get_interfaces(gconstpointer fix)
332 {
333 const TestFixture *fixture = fix;
334 QDict *ret;
335 QList *list;
336 const QListEntry *entry;
337
338 ret = qmp_fd(fixture->fd, "{'execute': 'guest-network-get-interfaces'}");
339 g_assert_nonnull(ret);
340 qmp_assert_no_error(ret);
341
342 /* check there is at least an interface */
343 list = qdict_get_qlist(ret, "return");
344 entry = qlist_first(list);
345 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "name"));
346
347 QDECREF(ret);
348 }
349
350 static void test_qga_file_ops(gconstpointer fix)
351 {
352 const TestFixture *fixture = fix;
353 const guchar helloworld[] = "Hello World!\n";
354 const char *b64;
355 gchar *cmd, *path, *enc;
356 guchar *dec;
357 QDict *ret, *val;
358 int64_t id, eof;
359 gsize count;
360 FILE *f;
361 char tmp[100];
362
363 /* open */
364 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
365 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
366 g_assert_nonnull(ret);
367 qmp_assert_no_error(ret);
368 id = qdict_get_int(ret, "return");
369 QDECREF(ret);
370
371 enc = g_base64_encode(helloworld, sizeof(helloworld));
372 /* write */
373 cmd = g_strdup_printf("{'execute': 'guest-file-write',"
374 " 'arguments': { 'handle': %" PRId64 ","
375 " 'buf-b64': '%s' } }", id, enc);
376 ret = qmp_fd(fixture->fd, cmd);
377 g_assert_nonnull(ret);
378 qmp_assert_no_error(ret);
379
380 val = qdict_get_qdict(ret, "return");
381 count = qdict_get_int(val, "count");
382 eof = qdict_get_bool(val, "eof");
383 g_assert_cmpint(count, ==, sizeof(helloworld));
384 g_assert_cmpint(eof, ==, 0);
385 QDECREF(ret);
386 g_free(cmd);
387
388 /* flush */
389 cmd = g_strdup_printf("{'execute': 'guest-file-flush',"
390 " 'arguments': {'handle': %" PRId64 "} }",
391 id);
392 ret = qmp_fd(fixture->fd, cmd);
393 QDECREF(ret);
394 g_free(cmd);
395
396 /* close */
397 cmd = g_strdup_printf("{'execute': 'guest-file-close',"
398 " 'arguments': {'handle': %" PRId64 "} }",
399 id);
400 ret = qmp_fd(fixture->fd, cmd);
401 QDECREF(ret);
402 g_free(cmd);
403
404 /* check content */
405 path = g_build_filename(fixture->test_dir, "foo", NULL);
406 f = fopen(path, "r");
407 g_assert_nonnull(f);
408 count = fread(tmp, 1, sizeof(tmp), f);
409 g_assert_cmpint(count, ==, sizeof(helloworld));
410 tmp[count] = 0;
411 g_assert_cmpstr(tmp, ==, (char *)helloworld);
412 fclose(f);
413
414 /* open */
415 ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
416 " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
417 g_assert_nonnull(ret);
418 qmp_assert_no_error(ret);
419 id = qdict_get_int(ret, "return");
420 QDECREF(ret);
421
422 /* read */
423 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
424 " 'arguments': { 'handle': %" PRId64 "} }",
425 id);
426 ret = qmp_fd(fixture->fd, cmd);
427 val = qdict_get_qdict(ret, "return");
428 count = qdict_get_int(val, "count");
429 eof = qdict_get_bool(val, "eof");
430 b64 = qdict_get_str(val, "buf-b64");
431 g_assert_cmpint(count, ==, sizeof(helloworld));
432 g_assert(eof);
433 g_assert_cmpstr(b64, ==, enc);
434
435 QDECREF(ret);
436 g_free(cmd);
437 g_free(enc);
438
439 /* read eof */
440 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
441 " 'arguments': { 'handle': %" PRId64 "} }",
442 id);
443 ret = qmp_fd(fixture->fd, cmd);
444 val = qdict_get_qdict(ret, "return");
445 count = qdict_get_int(val, "count");
446 eof = qdict_get_bool(val, "eof");
447 b64 = qdict_get_str(val, "buf-b64");
448 g_assert_cmpint(count, ==, 0);
449 g_assert(eof);
450 g_assert_cmpstr(b64, ==, "");
451 QDECREF(ret);
452 g_free(cmd);
453
454 /* seek */
455 cmd = g_strdup_printf("{'execute': 'guest-file-seek',"
456 " 'arguments': { 'handle': %" PRId64 ", "
457 " 'offset': %d, 'whence': %d } }",
458 id, 6, SEEK_SET);
459 ret = qmp_fd(fixture->fd, cmd);
460 qmp_assert_no_error(ret);
461 val = qdict_get_qdict(ret, "return");
462 count = qdict_get_int(val, "position");
463 eof = qdict_get_bool(val, "eof");
464 g_assert_cmpint(count, ==, 6);
465 g_assert(!eof);
466 QDECREF(ret);
467 g_free(cmd);
468
469 /* partial read */
470 cmd = g_strdup_printf("{'execute': 'guest-file-read',"
471 " 'arguments': { 'handle': %" PRId64 "} }",
472 id);
473 ret = qmp_fd(fixture->fd, cmd);
474 val = qdict_get_qdict(ret, "return");
475 count = qdict_get_int(val, "count");
476 eof = qdict_get_bool(val, "eof");
477 b64 = qdict_get_str(val, "buf-b64");
478 g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
479 g_assert(eof);
480 dec = g_base64_decode(b64, &count);
481 g_assert_cmpint(count, ==, sizeof(helloworld) - 6);
482 g_assert_cmpmem(dec, count, helloworld + 6, sizeof(helloworld) - 6);
483 g_free(dec);
484
485 QDECREF(ret);
486 g_free(cmd);
487
488 /* close */
489 cmd = g_strdup_printf("{'execute': 'guest-file-close',"
490 " 'arguments': {'handle': %" PRId64 "} }",
491 id);
492 ret = qmp_fd(fixture->fd, cmd);
493 QDECREF(ret);
494 g_free(cmd);
495 }
496
497 static void test_qga_get_time(gconstpointer fix)
498 {
499 const TestFixture *fixture = fix;
500 QDict *ret;
501 int64_t time;
502
503 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
504 g_assert_nonnull(ret);
505 qmp_assert_no_error(ret);
506
507 time = qdict_get_int(ret, "return");
508 g_assert_cmpint(time, >, 0);
509
510 QDECREF(ret);
511 }
512
513 static void test_qga_set_time(gconstpointer fix)
514 {
515 const TestFixture *fixture = fix;
516 QDict *ret;
517 int64_t current, time;
518 gchar *cmd;
519
520 /* get current time */
521 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
522 g_assert_nonnull(ret);
523 qmp_assert_no_error(ret);
524 current = qdict_get_int(ret, "return");
525 g_assert_cmpint(current, >, 0);
526 QDECREF(ret);
527
528 /* set some old time */
529 ret = qmp_fd(fixture->fd, "{'execute': 'guest-set-time',"
530 " 'arguments': { 'time': 1000 } }");
531 g_assert_nonnull(ret);
532 qmp_assert_no_error(ret);
533 QDECREF(ret);
534
535 /* check old time */
536 ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
537 g_assert_nonnull(ret);
538 qmp_assert_no_error(ret);
539 time = qdict_get_int(ret, "return");
540 g_assert_cmpint(time / 1000, <, G_USEC_PER_SEC * 10);
541 QDECREF(ret);
542
543 /* set back current time */
544 cmd = g_strdup_printf("{'execute': 'guest-set-time',"
545 " 'arguments': { 'time': %" PRId64 " } }",
546 current + time * 1000);
547 ret = qmp_fd(fixture->fd, cmd);
548 g_free(cmd);
549 g_assert_nonnull(ret);
550 qmp_assert_no_error(ret);
551 QDECREF(ret);
552 }
553
554 static void test_qga_fstrim(gconstpointer fix)
555 {
556 const TestFixture *fixture = fix;
557 QDict *ret;
558 QList *list;
559 const QListEntry *entry;
560
561 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fstrim',"
562 " arguments: { minimum: 4194304 } }");
563 g_assert_nonnull(ret);
564 qmp_assert_no_error(ret);
565 list = qdict_get_qlist(ret, "return");
566 entry = qlist_first(list);
567 g_assert(qdict_haskey(qobject_to_qdict(entry->value), "paths"));
568
569 QDECREF(ret);
570 }
571
572 static void test_qga_blacklist(gconstpointer data)
573 {
574 TestFixture fix;
575 QDict *ret, *error;
576 const gchar *class, *desc;
577
578 fixture_setup(&fix, "-b guest-ping,guest-get-time");
579
580 /* check blacklist */
581 ret = qmp_fd(fix.fd, "{'execute': 'guest-ping'}");
582 g_assert_nonnull(ret);
583 error = qdict_get_qdict(ret, "error");
584 class = qdict_get_try_str(error, "class");
585 desc = qdict_get_try_str(error, "desc");
586 g_assert_cmpstr(class, ==, "GenericError");
587 g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
588 QDECREF(ret);
589
590 ret = qmp_fd(fix.fd, "{'execute': 'guest-get-time'}");
591 g_assert_nonnull(ret);
592 error = qdict_get_qdict(ret, "error");
593 class = qdict_get_try_str(error, "class");
594 desc = qdict_get_try_str(error, "desc");
595 g_assert_cmpstr(class, ==, "GenericError");
596 g_assert_nonnull(g_strstr_len(desc, -1, "has been disabled"));
597 QDECREF(ret);
598
599 /* check something work */
600 ret = qmp_fd(fix.fd, "{'execute': 'guest-get-fsinfo'}");
601 qmp_assert_no_error(ret);
602 QDECREF(ret);
603
604 fixture_tear_down(&fix, NULL);
605 }
606
607 static void test_qga_config(gconstpointer data)
608 {
609 GError *error = NULL;
610 char *cwd, *cmd, *out, *err, *str, **strv, *conf, **argv = NULL;
611 char *env[2];
612 int status, tmp;
613 gsize n;
614 GKeyFile *kf;
615 const char *qga_config =
616 "[general]\n"
617 "daemon=false\n"
618 "method=virtio-serial\n"
619 "path=/path/to/org.qemu.guest_agent.0\n"
620 "pidfile=/var/foo/qemu-ga.pid\n"
621 "statedir=/var/state\n"
622 "verbose=true\n"
623 "blacklist=guest-ping;guest-get-time\n";
624
625 tmp = g_file_open_tmp(NULL, &conf, &error);
626 g_assert_no_error(error);
627 g_assert_cmpint(tmp, >=, 0);
628 g_assert_cmpstr(conf, !=, "");
629
630 g_file_set_contents(conf, qga_config, -1, &error);
631 g_assert_no_error(error);
632
633 cwd = g_get_current_dir();
634 cmd = g_strdup_printf("%s%cqemu-ga -D",
635 cwd, G_DIR_SEPARATOR);
636 g_shell_parse_argv(cmd, NULL, &argv, &error);
637 g_assert_no_error(error);
638
639 env[0] = g_strdup_printf("QGA_CONF=%s", conf);
640 env[1] = NULL;
641 g_spawn_sync(NULL, argv, env, 0,
642 NULL, NULL, &out, &err, &status, &error);
643 g_assert_no_error(error);
644 g_assert_cmpstr(err, ==, "");
645 g_assert_cmpint(status, ==, 0);
646
647 kf = g_key_file_new();
648 g_key_file_load_from_data(kf, out, -1, G_KEY_FILE_NONE, &error);
649 g_assert_no_error(error);
650
651 str = g_key_file_get_start_group(kf);
652 g_assert_cmpstr(str, ==, "general");
653 g_free(str);
654
655 g_assert_false(g_key_file_get_boolean(kf, "general", "daemon", &error));
656 g_assert_no_error(error);
657
658 str = g_key_file_get_string(kf, "general", "method", &error);
659 g_assert_no_error(error);
660 g_assert_cmpstr(str, ==, "virtio-serial");
661 g_free(str);
662
663 str = g_key_file_get_string(kf, "general", "path", &error);
664 g_assert_no_error(error);
665 g_assert_cmpstr(str, ==, "/path/to/org.qemu.guest_agent.0");
666 g_free(str);
667
668 str = g_key_file_get_string(kf, "general", "pidfile", &error);
669 g_assert_no_error(error);
670 g_assert_cmpstr(str, ==, "/var/foo/qemu-ga.pid");
671 g_free(str);
672
673 str = g_key_file_get_string(kf, "general", "statedir", &error);
674 g_assert_no_error(error);
675 g_assert_cmpstr(str, ==, "/var/state");
676 g_free(str);
677
678 g_assert_true(g_key_file_get_boolean(kf, "general", "verbose", &error));
679 g_assert_no_error(error);
680
681 strv = g_key_file_get_string_list(kf, "general", "blacklist", &n, &error);
682 g_assert_cmpint(n, ==, 2);
683 #if GLIB_CHECK_VERSION(2, 44, 0)
684 g_assert_true(g_strv_contains((const char * const *)strv,
685 "guest-ping"));
686 g_assert_true(g_strv_contains((const char * const *)strv,
687 "guest-get-time"));
688 #endif
689 g_assert_no_error(error);
690 g_strfreev(strv);
691
692 g_free(out);
693 g_free(err);
694 g_free(conf);
695 g_free(env[0]);
696 g_key_file_free(kf);
697
698 close(tmp);
699 }
700
701 static void test_qga_fsfreeze_status(gconstpointer fix)
702 {
703 const TestFixture *fixture = fix;
704 QDict *ret;
705 const gchar *status;
706
707 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-status'}");
708 g_assert_nonnull(ret);
709 qmp_assert_no_error(ret);
710
711 status = qdict_get_try_str(ret, "return");
712 g_assert_cmpstr(status, ==, "thawed");
713
714 QDECREF(ret);
715 }
716
717 static void test_qga_fsfreeze_and_thaw(gconstpointer fix)
718 {
719 const TestFixture *fixture = fix;
720 QDict *ret;
721 const gchar *status;
722
723 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-freeze'}");
724 g_assert_nonnull(ret);
725 qmp_assert_no_error(ret);
726 QDECREF(ret);
727
728 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-status'}");
729 g_assert_nonnull(ret);
730 qmp_assert_no_error(ret);
731 status = qdict_get_try_str(ret, "return");
732 g_assert_cmpstr(status, ==, "frozen");
733 QDECREF(ret);
734
735 ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-thaw'}");
736 g_assert_nonnull(ret);
737 qmp_assert_no_error(ret);
738 QDECREF(ret);
739 }
740
741 int main(int argc, char **argv)
742 {
743 TestFixture fix;
744 int ret;
745
746 setlocale (LC_ALL, "");
747 g_test_init(&argc, &argv, NULL);
748 fixture_setup(&fix, NULL);
749
750 g_test_add_data_func("/qga/sync-delimited", &fix, test_qga_sync_delimited);
751 g_test_add_data_func("/qga/sync", &fix, test_qga_sync);
752 g_test_add_data_func("/qga/ping", &fix, test_qga_ping);
753 g_test_add_data_func("/qga/info", &fix, test_qga_info);
754 g_test_add_data_func("/qga/network-get-interfaces", &fix,
755 test_qga_network_get_interfaces);
756 g_test_add_data_func("/qga/get-vcpus", &fix, test_qga_get_vcpus);
757 g_test_add_data_func("/qga/get-fsinfo", &fix, test_qga_get_fsinfo);
758 g_test_add_data_func("/qga/get-memory-block-info", &fix,
759 test_qga_get_memory_block_info);
760 g_test_add_data_func("/qga/get-memory-blocks", &fix,
761 test_qga_get_memory_blocks);
762 g_test_add_data_func("/qga/file-ops", &fix, test_qga_file_ops);
763 g_test_add_data_func("/qga/get-time", &fix, test_qga_get_time);
764 g_test_add_data_func("/qga/invalid-cmd", &fix, test_qga_invalid_cmd);
765 g_test_add_data_func("/qga/fsfreeze-status", &fix,
766 test_qga_fsfreeze_status);
767
768 g_test_add_data_func("/qga/blacklist", NULL, test_qga_blacklist);
769 g_test_add_data_func("/qga/config", NULL, test_qga_config);
770
771 if (g_getenv("QGA_TEST_SIDE_EFFECTING")) {
772 g_test_add_data_func("/qga/fsfreeze-and-thaw", &fix,
773 test_qga_fsfreeze_and_thaw);
774 g_test_add_data_func("/qga/set-time", &fix, test_qga_set_time);
775 g_test_add_data_func("/qga/fstrim", &fix, test_qga_fstrim);
776 }
777
778 ret = g_test_run();
779
780 fixture_tear_down(&fix, NULL);
781
782 return ret;
783 }