qemu/tests/test-qga.c
<<
>>
Prefs
   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
  11typedef struct {
  12    char *test_dir;
  13    GMainLoop *loop;
  14    int fd;
  15    GPid pid;
  16} TestFixture;
  17
  18static 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
  42static 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
  50static void
  51fixture_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
  88static void
  89fixture_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
 116static 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
 143static 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
 150    qmp_fd_send_raw(fixture->fd, "\xff");
 151    qmp_fd_send(fixture->fd,
 152                "{'execute': 'guest-sync-delimited',"
 153                " 'arguments': {'id': %u } }",
 154                r);
 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
 186static void test_qga_sync(gconstpointer fix)
 187{
 188    const TestFixture *fixture = fix;
 189    guint32 v, r = g_random_int();
 190    QDict *ret;
 191
 192    /*
 193     * TODO guest-sync is inherently limited: we cannot distinguish
 194     * failure caused by reacting to garbage on the wire prior to this
 195     * command, from failure of this actual command. Clients are
 196     * supposed to be able to send a raw '\xff' byte to at least
 197     * re-synchronize the server's parser prior to this command, but
 198     * we are not in a position to test that here because (at least
 199     * for now) it causes the server to issue an error message about
 200     * invalid JSON. Testing of '\xff' handling is done in
 201     * guest-sync-delimited instead.
 202     */
 203    ret = qmp_fd(fixture->fd,
 204                 "{'execute': 'guest-sync', 'arguments': {'id': %u } }",
 205                 r);
 206
 207    g_assert_nonnull(ret);
 208    qmp_assert_no_error(ret);
 209
 210    v = qdict_get_int(ret, "return");
 211    g_assert_cmpint(r, ==, v);
 212
 213    qobject_unref(ret);
 214}
 215
 216static void test_qga_ping(gconstpointer fix)
 217{
 218    const TestFixture *fixture = fix;
 219    QDict *ret;
 220
 221    ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping'}");
 222    g_assert_nonnull(ret);
 223    qmp_assert_no_error(ret);
 224
 225    qobject_unref(ret);
 226}
 227
 228static void test_qga_id(gconstpointer fix)
 229{
 230    const TestFixture *fixture = fix;
 231    QDict *ret;
 232
 233    ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping', 'id': 1}");
 234    g_assert_nonnull(ret);
 235    qmp_assert_no_error(ret);
 236    g_assert_cmpint(qdict_get_int(ret, "id"), ==, 1);
 237
 238    qobject_unref(ret);
 239}
 240
 241static void test_qga_invalid_oob(gconstpointer fix)
 242{
 243    const TestFixture *fixture = fix;
 244    QDict *ret;
 245
 246    ret = qmp_fd(fixture->fd, "{'exec-oob': 'guest-ping'}");
 247    g_assert_nonnull(ret);
 248
 249    qmp_assert_error_class(ret, "GenericError");
 250}
 251
 252static void test_qga_invalid_args(gconstpointer fix)
 253{
 254    const TestFixture *fixture = fix;
 255    QDict *ret, *error;
 256    const gchar *class, *desc;
 257
 258    ret = qmp_fd(fixture->fd, "{'execute': 'guest-ping', "
 259                 "'arguments': {'foo': 42 }}");
 260    g_assert_nonnull(ret);
 261
 262    error = qdict_get_qdict(ret, "error");
 263    class = qdict_get_try_str(error, "class");
 264    desc = qdict_get_try_str(error, "desc");
 265
 266    g_assert_cmpstr(class, ==, "GenericError");
 267    g_assert_cmpstr(desc, ==, "Parameter 'foo' is unexpected");
 268
 269    qobject_unref(ret);
 270}
 271
 272static void test_qga_invalid_cmd(gconstpointer fix)
 273{
 274    const TestFixture *fixture = fix;
 275    QDict *ret, *error;
 276    const gchar *class, *desc;
 277
 278    ret = qmp_fd(fixture->fd, "{'execute': 'guest-invalid-cmd'}");
 279    g_assert_nonnull(ret);
 280
 281    error = qdict_get_qdict(ret, "error");
 282    class = qdict_get_try_str(error, "class");
 283    desc = qdict_get_try_str(error, "desc");
 284
 285    g_assert_cmpstr(class, ==, "CommandNotFound");
 286    g_assert_cmpint(strlen(desc), >, 0);
 287
 288    qobject_unref(ret);
 289}
 290
 291static void test_qga_info(gconstpointer fix)
 292{
 293    const TestFixture *fixture = fix;
 294    QDict *ret, *val;
 295    const gchar *version;
 296
 297    ret = qmp_fd(fixture->fd, "{'execute': 'guest-info'}");
 298    g_assert_nonnull(ret);
 299    qmp_assert_no_error(ret);
 300
 301    val = qdict_get_qdict(ret, "return");
 302    version = qdict_get_try_str(val, "version");
 303    g_assert_cmpstr(version, ==, QEMU_VERSION);
 304
 305    qobject_unref(ret);
 306}
 307
 308static void test_qga_get_vcpus(gconstpointer fix)
 309{
 310    const TestFixture *fixture = fix;
 311    QDict *ret;
 312    QList *list;
 313    const QListEntry *entry;
 314
 315    ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-vcpus'}");
 316    g_assert_nonnull(ret);
 317    qmp_assert_no_error(ret);
 318
 319    /* check there is at least a cpu */
 320    list = qdict_get_qlist(ret, "return");
 321    entry = qlist_first(list);
 322    g_assert(qdict_haskey(qobject_to(QDict, entry->value), "online"));
 323    g_assert(qdict_haskey(qobject_to(QDict, entry->value), "logical-id"));
 324
 325    qobject_unref(ret);
 326}
 327
 328static void test_qga_get_fsinfo(gconstpointer fix)
 329{
 330    const TestFixture *fixture = fix;
 331    QDict *ret;
 332    QList *list;
 333    const QListEntry *entry;
 334
 335    ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-fsinfo'}");
 336    g_assert_nonnull(ret);
 337    qmp_assert_no_error(ret);
 338
 339    /* sanity-check the response if there are any filesystems */
 340    list = qdict_get_qlist(ret, "return");
 341    entry = qlist_first(list);
 342    if (entry) {
 343        g_assert(qdict_haskey(qobject_to(QDict, entry->value), "name"));
 344        g_assert(qdict_haskey(qobject_to(QDict, entry->value), "mountpoint"));
 345        g_assert(qdict_haskey(qobject_to(QDict, entry->value), "type"));
 346        g_assert(qdict_haskey(qobject_to(QDict, entry->value), "disk"));
 347    }
 348
 349    qobject_unref(ret);
 350}
 351
 352static void test_qga_get_memory_block_info(gconstpointer fix)
 353{
 354    const TestFixture *fixture = fix;
 355    QDict *ret, *val;
 356    int64_t size;
 357
 358    ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-block-info'}");
 359    g_assert_nonnull(ret);
 360
 361    /* some systems might not expose memory block info in sysfs */
 362    if (!qdict_haskey(ret, "error")) {
 363        /* check there is at least some memory */
 364        val = qdict_get_qdict(ret, "return");
 365        size = qdict_get_int(val, "size");
 366        g_assert_cmpint(size, >, 0);
 367    }
 368
 369    qobject_unref(ret);
 370}
 371
 372static void test_qga_get_memory_blocks(gconstpointer fix)
 373{
 374    const TestFixture *fixture = fix;
 375    QDict *ret;
 376    QList *list;
 377    const QListEntry *entry;
 378
 379    ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-memory-blocks'}");
 380    g_assert_nonnull(ret);
 381
 382    /* some systems might not expose memory block info in sysfs */
 383    if (!qdict_haskey(ret, "error")) {
 384        list = qdict_get_qlist(ret, "return");
 385        entry = qlist_first(list);
 386        /* newer versions of qga may return empty list without error */
 387        if (entry) {
 388            g_assert(qdict_haskey(qobject_to(QDict, entry->value),
 389                                  "phys-index"));
 390            g_assert(qdict_haskey(qobject_to(QDict, entry->value), "online"));
 391        }
 392    }
 393
 394    qobject_unref(ret);
 395}
 396
 397static void test_qga_network_get_interfaces(gconstpointer fix)
 398{
 399    const TestFixture *fixture = fix;
 400    QDict *ret;
 401    QList *list;
 402    const QListEntry *entry;
 403
 404    ret = qmp_fd(fixture->fd, "{'execute': 'guest-network-get-interfaces'}");
 405    g_assert_nonnull(ret);
 406    qmp_assert_no_error(ret);
 407
 408    /* check there is at least an interface */
 409    list = qdict_get_qlist(ret, "return");
 410    entry = qlist_first(list);
 411    g_assert(qdict_haskey(qobject_to(QDict, entry->value), "name"));
 412
 413    qobject_unref(ret);
 414}
 415
 416static void test_qga_file_ops(gconstpointer fix)
 417{
 418    const TestFixture *fixture = fix;
 419    const unsigned char helloworld[] = "Hello World!\n";
 420    const char *b64;
 421    gchar *path, *enc;
 422    unsigned char *dec;
 423    QDict *ret, *val;
 424    int64_t id, eof;
 425    gsize count;
 426    FILE *f;
 427    char tmp[100];
 428
 429    /* open */
 430    ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
 431                 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
 432    g_assert_nonnull(ret);
 433    qmp_assert_no_error(ret);
 434    id = qdict_get_int(ret, "return");
 435    qobject_unref(ret);
 436
 437    enc = g_base64_encode(helloworld, sizeof(helloworld));
 438    /* write */
 439    ret = qmp_fd(fixture->fd,
 440                 "{'execute': 'guest-file-write',"
 441                 " 'arguments': { 'handle': %" PRId64 ", 'buf-b64': %s } }",
 442                 id, enc);
 443    g_assert_nonnull(ret);
 444    qmp_assert_no_error(ret);
 445
 446    val = qdict_get_qdict(ret, "return");
 447    count = qdict_get_int(val, "count");
 448    eof = qdict_get_bool(val, "eof");
 449    g_assert_cmpint(count, ==, sizeof(helloworld));
 450    g_assert_cmpint(eof, ==, 0);
 451    qobject_unref(ret);
 452
 453    /* flush */
 454    ret = qmp_fd(fixture->fd,
 455                 "{'execute': 'guest-file-flush',"
 456                 " 'arguments': {'handle': %" PRId64 "} }",
 457                 id);
 458    qobject_unref(ret);
 459
 460    /* close */
 461    ret = qmp_fd(fixture->fd,
 462                 "{'execute': 'guest-file-close',"
 463                 " 'arguments': {'handle': %" PRId64 "} }",
 464                 id);
 465    qobject_unref(ret);
 466
 467    /* check content */
 468    path = g_build_filename(fixture->test_dir, "foo", NULL);
 469    f = fopen(path, "r");
 470    g_free(path);
 471    g_assert_nonnull(f);
 472    count = fread(tmp, 1, sizeof(tmp), f);
 473    g_assert_cmpint(count, ==, sizeof(helloworld));
 474    tmp[count] = 0;
 475    g_assert_cmpstr(tmp, ==, (char *)helloworld);
 476    fclose(f);
 477
 478    /* open */
 479    ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
 480                 " 'arguments': { 'path': 'foo', 'mode': 'r' } }");
 481    g_assert_nonnull(ret);
 482    qmp_assert_no_error(ret);
 483    id = qdict_get_int(ret, "return");
 484    qobject_unref(ret);
 485
 486    /* read */
 487    ret = qmp_fd(fixture->fd,
 488                 "{'execute': 'guest-file-read',"
 489                 " 'arguments': { 'handle': %" PRId64 "} }",
 490                 id);
 491    val = qdict_get_qdict(ret, "return");
 492    count = qdict_get_int(val, "count");
 493    eof = qdict_get_bool(val, "eof");
 494    b64 = qdict_get_str(val, "buf-b64");
 495    g_assert_cmpint(count, ==, sizeof(helloworld));
 496    g_assert(eof);
 497    g_assert_cmpstr(b64, ==, enc);
 498
 499    qobject_unref(ret);
 500    g_free(enc);
 501
 502    /* read eof */
 503    ret = qmp_fd(fixture->fd,
 504                 "{'execute': 'guest-file-read',"
 505                 " 'arguments': { 'handle': %" PRId64 "} }",
 506                 id);
 507    val = qdict_get_qdict(ret, "return");
 508    count = qdict_get_int(val, "count");
 509    eof = qdict_get_bool(val, "eof");
 510    b64 = qdict_get_str(val, "buf-b64");
 511    g_assert_cmpint(count, ==, 0);
 512    g_assert(eof);
 513    g_assert_cmpstr(b64, ==, "");
 514    qobject_unref(ret);
 515
 516    /* seek */
 517    ret = qmp_fd(fixture->fd,
 518                 "{'execute': 'guest-file-seek',"
 519                 " 'arguments': { 'handle': %" PRId64 ", "
 520                 " 'offset': %d, 'whence': %s } }",
 521                 id, 6, "set");
 522    qmp_assert_no_error(ret);
 523    val = qdict_get_qdict(ret, "return");
 524    count = qdict_get_int(val, "position");
 525    eof = qdict_get_bool(val, "eof");
 526    g_assert_cmpint(count, ==, 6);
 527    g_assert(!eof);
 528    qobject_unref(ret);
 529
 530    /* partial read */
 531    ret = qmp_fd(fixture->fd,
 532                 "{'execute': 'guest-file-read',"
 533                 " 'arguments': { 'handle': %" PRId64 "} }",
 534                 id);
 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
 548    /* close */
 549    ret = qmp_fd(fixture->fd,
 550                 "{'execute': 'guest-file-close',"
 551                 " 'arguments': {'handle': %" PRId64 "} }",
 552                 id);
 553    qobject_unref(ret);
 554}
 555
 556static void test_qga_file_write_read(gconstpointer fix)
 557{
 558    const TestFixture *fixture = fix;
 559    const unsigned char helloworld[] = "Hello World!\n";
 560    const char *b64;
 561    gchar *enc;
 562    QDict *ret, *val;
 563    int64_t id, eof;
 564    gsize count;
 565
 566    /* open */
 567    ret = qmp_fd(fixture->fd, "{'execute': 'guest-file-open',"
 568                 " 'arguments': { 'path': 'foo', 'mode': 'w+' } }");
 569    g_assert_nonnull(ret);
 570    qmp_assert_no_error(ret);
 571    id = qdict_get_int(ret, "return");
 572    qobject_unref(ret);
 573
 574    enc = g_base64_encode(helloworld, sizeof(helloworld));
 575    /* write */
 576    ret = qmp_fd(fixture->fd,
 577                 "{'execute': 'guest-file-write',"
 578                 " 'arguments': { 'handle': %" PRId64 ","
 579                 " 'buf-b64': %s } }", id, enc);
 580    g_assert_nonnull(ret);
 581    qmp_assert_no_error(ret);
 582
 583    val = qdict_get_qdict(ret, "return");
 584    count = qdict_get_int(val, "count");
 585    eof = qdict_get_bool(val, "eof");
 586    g_assert_cmpint(count, ==, sizeof(helloworld));
 587    g_assert_cmpint(eof, ==, 0);
 588    qobject_unref(ret);
 589
 590    /* read (check implicit flush) */
 591    ret = qmp_fd(fixture->fd,
 592                 "{'execute': 'guest-file-read',"
 593                 " 'arguments': { 'handle': %" PRId64 "} }",
 594                 id);
 595    val = qdict_get_qdict(ret, "return");
 596    count = qdict_get_int(val, "count");
 597    eof = qdict_get_bool(val, "eof");
 598    b64 = qdict_get_str(val, "buf-b64");
 599    g_assert_cmpint(count, ==, 0);
 600    g_assert(eof);
 601    g_assert_cmpstr(b64, ==, "");
 602    qobject_unref(ret);
 603
 604    /* seek to 0 */
 605    ret = qmp_fd(fixture->fd,
 606                 "{'execute': 'guest-file-seek',"
 607                 " 'arguments': { 'handle': %" PRId64 ", "
 608                 " 'offset': %d, 'whence': %s } }",
 609                 id, 0, "set");
 610    qmp_assert_no_error(ret);
 611    val = qdict_get_qdict(ret, "return");
 612    count = qdict_get_int(val, "position");
 613    eof = qdict_get_bool(val, "eof");
 614    g_assert_cmpint(count, ==, 0);
 615    g_assert(!eof);
 616    qobject_unref(ret);
 617
 618    /* read */
 619    ret = qmp_fd(fixture->fd,
 620                 "{'execute': 'guest-file-read',"
 621                 " 'arguments': { 'handle': %" PRId64 "} }",
 622                 id);
 623    val = qdict_get_qdict(ret, "return");
 624    count = qdict_get_int(val, "count");
 625    eof = qdict_get_bool(val, "eof");
 626    b64 = qdict_get_str(val, "buf-b64");
 627    g_assert_cmpint(count, ==, sizeof(helloworld));
 628    g_assert(eof);
 629    g_assert_cmpstr(b64, ==, enc);
 630    qobject_unref(ret);
 631    g_free(enc);
 632
 633    /* close */
 634    ret = qmp_fd(fixture->fd,
 635                 "{'execute': 'guest-file-close',"
 636                 " 'arguments': {'handle': %" PRId64 "} }",
 637                 id);
 638    qobject_unref(ret);
 639}
 640
 641static void test_qga_get_time(gconstpointer fix)
 642{
 643    const TestFixture *fixture = fix;
 644    QDict *ret;
 645    int64_t time;
 646
 647    ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}");
 648    g_assert_nonnull(ret);
 649    qmp_assert_no_error(ret);
 650
 651    time = qdict_get_int(ret, "return");
 652    g_assert_cmpint(time, >, 0);
 653
 654    qobject_unref(ret);
 655}
 656
 657static 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", NULL);
 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    qobject_unref(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    qobject_unref(ret);
 683
 684    /* check something work */
 685    ret = qmp_fd(fix.fd, "{'execute': 'guest-get-fsinfo'}");
 686    qmp_assert_no_error(ret);
 687    qobject_unref(ret);
 688
 689    fixture_tear_down(&fix, NULL);
 690}
 691
 692static 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    g_assert_true(g_strv_contains((const char * const *)strv,
 757                                  "guest-ping"));
 758    g_assert_true(g_strv_contains((const char * const *)strv,
 759                                  "guest-get-time"));
 760    g_assert_no_error(error);
 761    g_strfreev(strv);
 762
 763    g_free(out);
 764    g_free(err);
 765    g_free(env[0]);
 766    g_key_file_free(kf);
 767}
 768
 769static void test_qga_fsfreeze_status(gconstpointer fix)
 770{
 771    const TestFixture *fixture = fix;
 772    QDict *ret;
 773    const gchar *status;
 774
 775    ret = qmp_fd(fixture->fd, "{'execute': 'guest-fsfreeze-status'}");
 776    g_assert_nonnull(ret);
 777    qmp_assert_no_error(ret);
 778
 779    status = qdict_get_try_str(ret, "return");
 780    g_assert_cmpstr(status, ==, "thawed");
 781
 782    qobject_unref(ret);
 783}
 784
 785static void test_qga_guest_exec(gconstpointer fix)
 786{
 787    const TestFixture *fixture = fix;
 788    QDict *ret, *val;
 789    const gchar *out;
 790    guchar *decoded;
 791    int64_t pid, now, exitcode;
 792    gsize len;
 793    bool exited;
 794
 795    /* exec 'echo foo bar' */
 796    ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
 797                 " 'path': '/bin/echo', 'arg': [ '-n', '\" test_str \"' ],"
 798                 " 'capture-output': true } }");
 799    g_assert_nonnull(ret);
 800    qmp_assert_no_error(ret);
 801    val = qdict_get_qdict(ret, "return");
 802    pid = qdict_get_int(val, "pid");
 803    g_assert_cmpint(pid, >, 0);
 804    qobject_unref(ret);
 805
 806    /* wait for completion */
 807    now = g_get_monotonic_time();
 808    do {
 809        ret = qmp_fd(fixture->fd,
 810                     "{'execute': 'guest-exec-status',"
 811                     " 'arguments': { 'pid': %" PRId64 " } }", pid);
 812        g_assert_nonnull(ret);
 813        val = qdict_get_qdict(ret, "return");
 814        exited = qdict_get_bool(val, "exited");
 815        if (!exited) {
 816            qobject_unref(ret);
 817        }
 818    } while (!exited &&
 819             g_get_monotonic_time() < now + 5 * G_TIME_SPAN_SECOND);
 820    g_assert(exited);
 821
 822    /* check stdout */
 823    exitcode = qdict_get_int(val, "exitcode");
 824    g_assert_cmpint(exitcode, ==, 0);
 825    out = qdict_get_str(val, "out-data");
 826    decoded = g_base64_decode(out, &len);
 827    g_assert_cmpint(len, ==, 12);
 828    g_assert_cmpstr((char *)decoded, ==, "\" test_str \"");
 829    g_free(decoded);
 830    qobject_unref(ret);
 831}
 832
 833static void test_qga_guest_exec_invalid(gconstpointer fix)
 834{
 835    const TestFixture *fixture = fix;
 836    QDict *ret, *error;
 837    const gchar *class, *desc;
 838
 839    /* invalid command */
 840    ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {"
 841                 " 'path': '/bin/invalid-cmd42' } }");
 842    g_assert_nonnull(ret);
 843    error = qdict_get_qdict(ret, "error");
 844    g_assert_nonnull(error);
 845    class = qdict_get_str(error, "class");
 846    desc = qdict_get_str(error, "desc");
 847    g_assert_cmpstr(class, ==, "GenericError");
 848    g_assert_cmpint(strlen(desc), >, 0);
 849    qobject_unref(ret);
 850
 851    /* invalid pid */
 852    ret = qmp_fd(fixture->fd, "{'execute': 'guest-exec-status',"
 853                 " 'arguments': { 'pid': 0 } }");
 854    g_assert_nonnull(ret);
 855    error = qdict_get_qdict(ret, "error");
 856    g_assert_nonnull(error);
 857    class = qdict_get_str(error, "class");
 858    desc = qdict_get_str(error, "desc");
 859    g_assert_cmpstr(class, ==, "GenericError");
 860    g_assert_cmpint(strlen(desc), >, 0);
 861    qobject_unref(ret);
 862}
 863
 864static void test_qga_guest_get_host_name(gconstpointer fix)
 865{
 866    const TestFixture *fixture = fix;
 867    QDict *ret, *val;
 868
 869    ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-host-name'}");
 870    g_assert_nonnull(ret);
 871    qmp_assert_no_error(ret);
 872
 873    val = qdict_get_qdict(ret, "return");
 874    g_assert(qdict_haskey(val, "host-name"));
 875
 876    qobject_unref(ret);
 877}
 878
 879static void test_qga_guest_get_timezone(gconstpointer fix)
 880{
 881    const TestFixture *fixture = fix;
 882    QDict *ret, *val;
 883
 884    ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-timezone'}");
 885    g_assert_nonnull(ret);
 886    qmp_assert_no_error(ret);
 887
 888    /* Make sure there's at least offset */
 889    val = qdict_get_qdict(ret, "return");
 890    g_assert(qdict_haskey(val, "offset"));
 891
 892    qobject_unref(ret);
 893}
 894
 895static void test_qga_guest_get_users(gconstpointer fix)
 896{
 897    const TestFixture *fixture = fix;
 898    QDict *ret;
 899    QList *val;
 900
 901    ret = qmp_fd(fixture->fd, "{'execute': 'guest-get-users'}");
 902    g_assert_nonnull(ret);
 903    qmp_assert_no_error(ret);
 904
 905    /* There is not much to test here */
 906    val = qdict_get_qlist(ret, "return");
 907    g_assert_nonnull(val);
 908
 909    qobject_unref(ret);
 910}
 911
 912static void test_qga_guest_get_osinfo(gconstpointer data)
 913{
 914    TestFixture fixture;
 915    const gchar *str;
 916    gchar *cwd, *env[2];
 917    QDict *ret, *val;
 918
 919    cwd = g_get_current_dir();
 920    env[0] = g_strdup_printf(
 921        "QGA_OS_RELEASE=%s%ctests%cdata%ctest-qga-os-release",
 922        cwd, G_DIR_SEPARATOR, G_DIR_SEPARATOR, G_DIR_SEPARATOR);
 923    env[1] = NULL;
 924    g_free(cwd);
 925    fixture_setup(&fixture, NULL, env);
 926
 927    ret = qmp_fd(fixture.fd, "{'execute': 'guest-get-osinfo'}");
 928    g_assert_nonnull(ret);
 929    qmp_assert_no_error(ret);
 930
 931    val = qdict_get_qdict(ret, "return");
 932
 933    str = qdict_get_try_str(val, "id");
 934    g_assert_nonnull(str);
 935    g_assert_cmpstr(str, ==, "qemu-ga-test");
 936
 937    str = qdict_get_try_str(val, "name");
 938    g_assert_nonnull(str);
 939    g_assert_cmpstr(str, ==, "QEMU-GA");
 940
 941    str = qdict_get_try_str(val, "pretty-name");
 942    g_assert_nonnull(str);
 943    g_assert_cmpstr(str, ==, "QEMU Guest Agent test");
 944
 945    str = qdict_get_try_str(val, "version");
 946    g_assert_nonnull(str);
 947    g_assert_cmpstr(str, ==, "Test 1");
 948
 949    str = qdict_get_try_str(val, "version-id");
 950    g_assert_nonnull(str);
 951    g_assert_cmpstr(str, ==, "1");
 952
 953    str = qdict_get_try_str(val, "variant");
 954    g_assert_nonnull(str);
 955    g_assert_cmpstr(str, ==, "Unit test \"'$`\\ and \\\\ etc.");
 956
 957    str = qdict_get_try_str(val, "variant-id");
 958    g_assert_nonnull(str);
 959    g_assert_cmpstr(str, ==, "unit-test");
 960
 961    qobject_unref(ret);
 962    g_free(env[0]);
 963    fixture_tear_down(&fixture, NULL);
 964}
 965
 966int main(int argc, char **argv)
 967{
 968    TestFixture fix;
 969    int ret;
 970
 971    setlocale (LC_ALL, "");
 972    g_test_init(&argc, &argv, NULL);
 973    fixture_setup(&fix, NULL, NULL);
 974
 975    g_test_add_data_func("/qga/sync-delimited", &fix, test_qga_sync_delimited);
 976    g_test_add_data_func("/qga/sync", &fix, test_qga_sync);
 977    g_test_add_data_func("/qga/ping", &fix, test_qga_ping);
 978    g_test_add_data_func("/qga/info", &fix, test_qga_info);
 979    g_test_add_data_func("/qga/network-get-interfaces", &fix,
 980                         test_qga_network_get_interfaces);
 981    if (!access("/sys/devices/system/cpu/cpu0", F_OK)) {
 982        g_test_add_data_func("/qga/get-vcpus", &fix, test_qga_get_vcpus);
 983    }
 984    g_test_add_data_func("/qga/get-fsinfo", &fix, test_qga_get_fsinfo);
 985    g_test_add_data_func("/qga/get-memory-block-info", &fix,
 986                         test_qga_get_memory_block_info);
 987    g_test_add_data_func("/qga/get-memory-blocks", &fix,
 988                         test_qga_get_memory_blocks);
 989    g_test_add_data_func("/qga/file-ops", &fix, test_qga_file_ops);
 990    g_test_add_data_func("/qga/file-write-read", &fix, test_qga_file_write_read);
 991    g_test_add_data_func("/qga/get-time", &fix, test_qga_get_time);
 992    g_test_add_data_func("/qga/id", &fix, test_qga_id);
 993    g_test_add_data_func("/qga/invalid-oob", &fix, test_qga_invalid_oob);
 994    g_test_add_data_func("/qga/invalid-cmd", &fix, test_qga_invalid_cmd);
 995    g_test_add_data_func("/qga/invalid-args", &fix, test_qga_invalid_args);
 996    g_test_add_data_func("/qga/fsfreeze-status", &fix,
 997                         test_qga_fsfreeze_status);
 998
 999    g_test_add_data_func("/qga/blacklist", NULL, test_qga_blacklist);
1000    g_test_add_data_func("/qga/config", NULL, test_qga_config);
1001    g_test_add_data_func("/qga/guest-exec", &fix, test_qga_guest_exec);
1002    g_test_add_data_func("/qga/guest-exec-invalid", &fix,
1003                         test_qga_guest_exec_invalid);
1004    g_test_add_data_func("/qga/guest-get-osinfo", &fix,
1005                         test_qga_guest_get_osinfo);
1006    g_test_add_data_func("/qga/guest-get-host-name", &fix,
1007                         test_qga_guest_get_host_name);
1008    g_test_add_data_func("/qga/guest-get-timezone", &fix,
1009                         test_qga_guest_get_timezone);
1010    g_test_add_data_func("/qga/guest-get-users", &fix,
1011                         test_qga_guest_get_users);
1012
1013    ret = g_test_run();
1014
1015    fixture_tear_down(&fix, NULL);
1016
1017    return ret;
1018}
1019