qemu/tests/test-char.c
<<
>>
Prefs
   1#include "qemu/osdep.h"
   2#include <glib/gstdio.h>
   3
   4#include "qemu/config-file.h"
   5#include "qemu/option.h"
   6#include "qemu/sockets.h"
   7#include "chardev/char-fe.h"
   8#include "chardev/char-mux.h"
   9#include "sysemu/sysemu.h"
  10#include "qapi/error.h"
  11#include "qapi/qapi-commands-char.h"
  12#include "qapi/qmp/qdict.h"
  13#include "qom/qom-qobject.h"
  14#include "io/channel-socket.h"
  15#include "qapi/qobject-input-visitor.h"
  16#include "qapi/qapi-visit-sockets.h"
  17
  18static bool quit;
  19
  20typedef struct FeHandler {
  21    int read_count;
  22    bool is_open;
  23    int openclose_count;
  24    bool openclose_mismatch;
  25    int last_event;
  26    char read_buf[128];
  27} FeHandler;
  28
  29static void main_loop(void)
  30{
  31    quit = false;
  32    do {
  33        main_loop_wait(false);
  34    } while (!quit);
  35}
  36
  37static int fe_can_read(void *opaque)
  38{
  39    FeHandler *h = opaque;
  40
  41    return sizeof(h->read_buf) - h->read_count;
  42}
  43
  44static void fe_read(void *opaque, const uint8_t *buf, int size)
  45{
  46    FeHandler *h = opaque;
  47
  48    g_assert_cmpint(size, <=, fe_can_read(opaque));
  49
  50    memcpy(h->read_buf + h->read_count, buf, size);
  51    h->read_count += size;
  52    quit = true;
  53}
  54
  55static void fe_event(void *opaque, int event)
  56{
  57    FeHandler *h = opaque;
  58    bool new_open_state;
  59
  60    h->last_event = event;
  61    switch (event) {
  62    case CHR_EVENT_BREAK:
  63        break;
  64    case CHR_EVENT_OPENED:
  65    case CHR_EVENT_CLOSED:
  66        h->openclose_count++;
  67        new_open_state = (event == CHR_EVENT_OPENED);
  68        if (h->is_open == new_open_state) {
  69            h->openclose_mismatch = true;
  70        }
  71        h->is_open = new_open_state;
  72        /* no break */
  73    default:
  74        quit = true;
  75        break;
  76    }
  77}
  78
  79#ifdef _WIN32
  80static void char_console_test_subprocess(void)
  81{
  82    QemuOpts *opts;
  83    Chardev *chr;
  84
  85    opts = qemu_opts_create(qemu_find_opts("chardev"), "console-label",
  86                            1, &error_abort);
  87    qemu_opt_set(opts, "backend", "console", &error_abort);
  88
  89    chr = qemu_chr_new_from_opts(opts, NULL, NULL);
  90    g_assert_nonnull(chr);
  91
  92    qemu_chr_write_all(chr, (const uint8_t *)"CONSOLE", 7);
  93
  94    qemu_opts_del(opts);
  95    object_unparent(OBJECT(chr));
  96}
  97
  98static void char_console_test(void)
  99{
 100    g_test_trap_subprocess("/char/console/subprocess", 0, 0);
 101    g_test_trap_assert_passed();
 102    g_test_trap_assert_stdout("CONSOLE");
 103}
 104#endif
 105static void char_stdio_test_subprocess(void)
 106{
 107    Chardev *chr;
 108    CharBackend be;
 109    int ret;
 110
 111    chr = qemu_chr_new("label", "stdio", NULL);
 112    g_assert_nonnull(chr);
 113
 114    qemu_chr_fe_init(&be, chr, &error_abort);
 115    qemu_chr_fe_set_open(&be, true);
 116    ret = qemu_chr_fe_write(&be, (void *)"buf", 4);
 117    g_assert_cmpint(ret, ==, 4);
 118
 119    qemu_chr_fe_deinit(&be, true);
 120}
 121
 122static void char_stdio_test(void)
 123{
 124    g_test_trap_subprocess("/char/stdio/subprocess", 0, 0);
 125    g_test_trap_assert_passed();
 126    g_test_trap_assert_stdout("buf");
 127}
 128
 129static void char_ringbuf_test(void)
 130{
 131    QemuOpts *opts;
 132    Chardev *chr;
 133    CharBackend be;
 134    char *data;
 135    int ret;
 136
 137    opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
 138                            1, &error_abort);
 139    qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
 140
 141    qemu_opt_set(opts, "size", "5", &error_abort);
 142    chr = qemu_chr_new_from_opts(opts, NULL, NULL);
 143    g_assert_null(chr);
 144    qemu_opts_del(opts);
 145
 146    opts = qemu_opts_create(qemu_find_opts("chardev"), "ringbuf-label",
 147                            1, &error_abort);
 148    qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
 149    qemu_opt_set(opts, "size", "2", &error_abort);
 150    chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
 151    g_assert_nonnull(chr);
 152    qemu_opts_del(opts);
 153
 154    qemu_chr_fe_init(&be, chr, &error_abort);
 155    ret = qemu_chr_fe_write(&be, (void *)"buff", 4);
 156    g_assert_cmpint(ret, ==, 4);
 157
 158    data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort);
 159    g_assert_cmpstr(data, ==, "ff");
 160    g_free(data);
 161
 162    data = qmp_ringbuf_read("ringbuf-label", 4, false, 0, &error_abort);
 163    g_assert_cmpstr(data, ==, "");
 164    g_free(data);
 165
 166    qemu_chr_fe_deinit(&be, true);
 167
 168    /* check alias */
 169    opts = qemu_opts_create(qemu_find_opts("chardev"), "memory-label",
 170                            1, &error_abort);
 171    qemu_opt_set(opts, "backend", "memory", &error_abort);
 172    qemu_opt_set(opts, "size", "2", &error_abort);
 173    chr = qemu_chr_new_from_opts(opts, NULL, NULL);
 174    g_assert_nonnull(chr);
 175    object_unparent(OBJECT(chr));
 176    qemu_opts_del(opts);
 177}
 178
 179static void char_mux_test(void)
 180{
 181    QemuOpts *opts;
 182    Chardev *chr, *base;
 183    char *data;
 184    FeHandler h1 = { 0, false, 0, false, }, h2 = { 0, false, 0, false, };
 185    CharBackend chr_be1, chr_be2;
 186
 187    opts = qemu_opts_create(qemu_find_opts("chardev"), "mux-label",
 188                            1, &error_abort);
 189    qemu_opt_set(opts, "backend", "ringbuf", &error_abort);
 190    qemu_opt_set(opts, "size", "128", &error_abort);
 191    qemu_opt_set(opts, "mux", "on", &error_abort);
 192    chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
 193    g_assert_nonnull(chr);
 194    qemu_opts_del(opts);
 195
 196    qemu_chr_fe_init(&chr_be1, chr, &error_abort);
 197    qemu_chr_fe_set_handlers(&chr_be1,
 198                             fe_can_read,
 199                             fe_read,
 200                             fe_event,
 201                             NULL,
 202                             &h1,
 203                             NULL, true);
 204
 205    qemu_chr_fe_init(&chr_be2, chr, &error_abort);
 206    qemu_chr_fe_set_handlers(&chr_be2,
 207                             fe_can_read,
 208                             fe_read,
 209                             fe_event,
 210                             NULL,
 211                             &h2,
 212                             NULL, true);
 213    qemu_chr_fe_take_focus(&chr_be2);
 214
 215    base = qemu_chr_find("mux-label-base");
 216    g_assert_cmpint(qemu_chr_be_can_write(base), !=, 0);
 217
 218    qemu_chr_be_write(base, (void *)"hello", 6);
 219    g_assert_cmpint(h1.read_count, ==, 0);
 220    g_assert_cmpint(h2.read_count, ==, 6);
 221    g_assert_cmpstr(h2.read_buf, ==, "hello");
 222    h2.read_count = 0;
 223
 224    g_assert_cmpint(h1.last_event, !=, 42); /* should be MUX_OUT or OPENED */
 225    g_assert_cmpint(h2.last_event, !=, 42); /* should be MUX_IN or OPENED */
 226    /* sending event on the base broadcast to all fe, historical reasons? */
 227    qemu_chr_be_event(base, 42);
 228    g_assert_cmpint(h1.last_event, ==, 42);
 229    g_assert_cmpint(h2.last_event, ==, 42);
 230    qemu_chr_be_event(chr, -1);
 231    g_assert_cmpint(h1.last_event, ==, 42);
 232    g_assert_cmpint(h2.last_event, ==, -1);
 233
 234    /* switch focus */
 235    qemu_chr_be_write(base, (void *)"\1b", 2);
 236    g_assert_cmpint(h1.last_event, ==, 42);
 237    g_assert_cmpint(h2.last_event, ==, CHR_EVENT_BREAK);
 238
 239    qemu_chr_be_write(base, (void *)"\1c", 2);
 240    g_assert_cmpint(h1.last_event, ==, CHR_EVENT_MUX_IN);
 241    g_assert_cmpint(h2.last_event, ==, CHR_EVENT_MUX_OUT);
 242    qemu_chr_be_event(chr, -1);
 243    g_assert_cmpint(h1.last_event, ==, -1);
 244    g_assert_cmpint(h2.last_event, ==, CHR_EVENT_MUX_OUT);
 245
 246    qemu_chr_be_write(base, (void *)"hello", 6);
 247    g_assert_cmpint(h2.read_count, ==, 0);
 248    g_assert_cmpint(h1.read_count, ==, 6);
 249    g_assert_cmpstr(h1.read_buf, ==, "hello");
 250    h1.read_count = 0;
 251
 252    qemu_chr_be_write(base, (void *)"\1b", 2);
 253    g_assert_cmpint(h1.last_event, ==, CHR_EVENT_BREAK);
 254    g_assert_cmpint(h2.last_event, ==, CHR_EVENT_MUX_OUT);
 255
 256    /* open/close state and corresponding events */
 257    g_assert_true(qemu_chr_fe_backend_open(&chr_be1));
 258    g_assert_true(qemu_chr_fe_backend_open(&chr_be2));
 259    g_assert_true(h1.is_open);
 260    g_assert_false(h1.openclose_mismatch);
 261    g_assert_true(h2.is_open);
 262    g_assert_false(h2.openclose_mismatch);
 263
 264    h1.openclose_count = h2.openclose_count = 0;
 265
 266    qemu_chr_fe_set_handlers(&chr_be1, NULL, NULL, NULL, NULL,
 267                             NULL, NULL, false);
 268    qemu_chr_fe_set_handlers(&chr_be2, NULL, NULL, NULL, NULL,
 269                             NULL, NULL, false);
 270    g_assert_cmpint(h1.openclose_count, ==, 0);
 271    g_assert_cmpint(h2.openclose_count, ==, 0);
 272
 273    h1.is_open = h2.is_open = false;
 274    qemu_chr_fe_set_handlers(&chr_be1,
 275                             NULL,
 276                             NULL,
 277                             fe_event,
 278                             NULL,
 279                             &h1,
 280                             NULL, false);
 281    qemu_chr_fe_set_handlers(&chr_be2,
 282                             NULL,
 283                             NULL,
 284                             fe_event,
 285                             NULL,
 286                             &h2,
 287                             NULL, false);
 288    g_assert_cmpint(h1.openclose_count, ==, 1);
 289    g_assert_false(h1.openclose_mismatch);
 290    g_assert_cmpint(h2.openclose_count, ==, 1);
 291    g_assert_false(h2.openclose_mismatch);
 292
 293    qemu_chr_be_event(base, CHR_EVENT_CLOSED);
 294    qemu_chr_be_event(base, CHR_EVENT_OPENED);
 295    g_assert_cmpint(h1.openclose_count, ==, 3);
 296    g_assert_false(h1.openclose_mismatch);
 297    g_assert_cmpint(h2.openclose_count, ==, 3);
 298    g_assert_false(h2.openclose_mismatch);
 299
 300    qemu_chr_fe_set_handlers(&chr_be2,
 301                             fe_can_read,
 302                             fe_read,
 303                             fe_event,
 304                             NULL,
 305                             &h2,
 306                             NULL, false);
 307    qemu_chr_fe_set_handlers(&chr_be1,
 308                             fe_can_read,
 309                             fe_read,
 310                             fe_event,
 311                             NULL,
 312                             &h1,
 313                             NULL, false);
 314
 315    /* remove first handler */
 316    qemu_chr_fe_set_handlers(&chr_be1, NULL, NULL, NULL, NULL,
 317                             NULL, NULL, true);
 318    qemu_chr_be_write(base, (void *)"hello", 6);
 319    g_assert_cmpint(h1.read_count, ==, 0);
 320    g_assert_cmpint(h2.read_count, ==, 0);
 321
 322    qemu_chr_be_write(base, (void *)"\1c", 2);
 323    qemu_chr_be_write(base, (void *)"hello", 6);
 324    g_assert_cmpint(h1.read_count, ==, 0);
 325    g_assert_cmpint(h2.read_count, ==, 6);
 326    g_assert_cmpstr(h2.read_buf, ==, "hello");
 327    h2.read_count = 0;
 328
 329    /* print help */
 330    qemu_chr_be_write(base, (void *)"\1?", 2);
 331    data = qmp_ringbuf_read("mux-label-base", 128, false, 0, &error_abort);
 332    g_assert_cmpint(strlen(data), !=, 0);
 333    g_free(data);
 334
 335    qemu_chr_fe_deinit(&chr_be1, false);
 336    qemu_chr_fe_deinit(&chr_be2, true);
 337}
 338
 339
 340static void websock_server_read(void *opaque, const uint8_t *buf, int size)
 341{
 342    g_assert_cmpint(size, ==, 5);
 343    g_assert(memcmp(buf, "world", size) == 0);
 344    quit = true;
 345}
 346
 347
 348static int websock_server_can_read(void *opaque)
 349{
 350    return 10;
 351}
 352
 353
 354static bool websock_check_http_headers(char *buf, int size)
 355{
 356    int i;
 357    const char *ans[] = { "HTTP/1.1 101 Switching Protocols\r\n",
 358                          "Server: QEMU VNC\r\n",
 359                          "Upgrade: websocket\r\n",
 360                          "Connection: Upgrade\r\n",
 361                          "Sec-WebSocket-Accept:",
 362                          "Sec-WebSocket-Protocol: binary\r\n" };
 363
 364    for (i = 0; i < 6; i++) {
 365        if (g_strstr_len(buf, size, ans[i]) == NULL) {
 366            return false;
 367        }
 368    }
 369
 370    return true;
 371}
 372
 373
 374static void websock_client_read(void *opaque, const uint8_t *buf, int size)
 375{
 376    const uint8_t ping[] = { 0x89, 0x85,                  /* Ping header */
 377                             0x07, 0x77, 0x9e, 0xf9,      /* Masking key */
 378                             0x6f, 0x12, 0xf2, 0x95, 0x68 /* "hello" */ };
 379
 380    const uint8_t binary[] = { 0x82, 0x85,                  /* Binary header */
 381                               0x74, 0x90, 0xb9, 0xdf,      /* Masking key */
 382                               0x03, 0xff, 0xcb, 0xb3, 0x10 /* "world" */ };
 383    Chardev *chr_client = opaque;
 384
 385    if (websock_check_http_headers((char *) buf, size)) {
 386        qemu_chr_fe_write(chr_client->be, ping, sizeof(ping));
 387    } else if (buf[0] == 0x8a && buf[1] == 0x05) {
 388        g_assert(strncmp((char *) buf + 2, "hello", 5) == 0);
 389        qemu_chr_fe_write(chr_client->be, binary, sizeof(binary));
 390    } else {
 391        g_assert(buf[0] == 0x88 && buf[1] == 0x16);
 392        g_assert(strncmp((char *) buf + 4, "peer requested close", 10) == 0);
 393        quit = true;
 394    }
 395}
 396
 397
 398static int websock_client_can_read(void *opaque)
 399{
 400    return 4096;
 401}
 402
 403
 404static void char_websock_test(void)
 405{
 406    QObject *addr;
 407    QDict *qdict;
 408    const char *port;
 409    char *tmp;
 410    char *handshake_port;
 411    CharBackend be;
 412    CharBackend client_be;
 413    Chardev *chr_client;
 414    Chardev *chr = qemu_chr_new("server",
 415                                "websocket:127.0.0.1:0,server,nowait", NULL);
 416    const char handshake[] = "GET / HTTP/1.1\r\n"
 417                             "Upgrade: websocket\r\n"
 418                             "Connection: Upgrade\r\n"
 419                             "Host: localhost:%s\r\n"
 420                             "Origin: http://localhost:%s\r\n"
 421                             "Sec-WebSocket-Key: o9JHNiS3/0/0zYE1wa3yIw==\r\n"
 422                             "Sec-WebSocket-Version: 13\r\n"
 423                             "Sec-WebSocket-Protocol: binary\r\n\r\n";
 424    const uint8_t close[] = { 0x88, 0x82,             /* Close header */
 425                              0xef, 0xaa, 0xc5, 0x97, /* Masking key */
 426                              0xec, 0x42              /* Status code */ };
 427
 428    addr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort);
 429    qdict = qobject_to(QDict, addr);
 430    port = qdict_get_str(qdict, "port");
 431    tmp = g_strdup_printf("tcp:127.0.0.1:%s", port);
 432    handshake_port = g_strdup_printf(handshake, port, port);
 433    qobject_unref(qdict);
 434
 435    qemu_chr_fe_init(&be, chr, &error_abort);
 436    qemu_chr_fe_set_handlers(&be, websock_server_can_read, websock_server_read,
 437                             NULL, NULL, chr, NULL, true);
 438
 439    chr_client = qemu_chr_new("client", tmp, NULL);
 440    qemu_chr_fe_init(&client_be, chr_client, &error_abort);
 441    qemu_chr_fe_set_handlers(&client_be, websock_client_can_read,
 442                             websock_client_read,
 443                             NULL, NULL, chr_client, NULL, true);
 444    g_free(tmp);
 445
 446    qemu_chr_write_all(chr_client,
 447                       (uint8_t *) handshake_port,
 448                       strlen(handshake_port));
 449    g_free(handshake_port);
 450    main_loop();
 451
 452    g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort));
 453    g_assert(object_property_get_bool(OBJECT(chr_client),
 454                                      "connected", &error_abort));
 455
 456    qemu_chr_write_all(chr_client, close, sizeof(close));
 457    main_loop();
 458
 459    object_unparent(OBJECT(chr_client));
 460    object_unparent(OBJECT(chr));
 461}
 462
 463
 464#ifndef _WIN32
 465static void char_pipe_test(void)
 466{
 467    gchar *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
 468    gchar *tmp, *in, *out, *pipe = g_build_filename(tmp_path, "pipe", NULL);
 469    Chardev *chr;
 470    CharBackend be;
 471    int ret, fd;
 472    char buf[10];
 473    FeHandler fe = { 0, };
 474
 475    in = g_strdup_printf("%s.in", pipe);
 476    if (mkfifo(in, 0600) < 0) {
 477        abort();
 478    }
 479    out = g_strdup_printf("%s.out", pipe);
 480    if (mkfifo(out, 0600) < 0) {
 481        abort();
 482    }
 483
 484    tmp = g_strdup_printf("pipe:%s", pipe);
 485    chr = qemu_chr_new("pipe", tmp, NULL);
 486    g_assert_nonnull(chr);
 487    g_free(tmp);
 488
 489    qemu_chr_fe_init(&be, chr, &error_abort);
 490
 491    ret = qemu_chr_fe_write(&be, (void *)"pipe-out", 9);
 492    g_assert_cmpint(ret, ==, 9);
 493
 494    fd = open(out, O_RDWR);
 495    ret = read(fd, buf, sizeof(buf));
 496    g_assert_cmpint(ret, ==, 9);
 497    g_assert_cmpstr(buf, ==, "pipe-out");
 498    close(fd);
 499
 500    fd = open(in, O_WRONLY);
 501    ret = write(fd, "pipe-in", 8);
 502    g_assert_cmpint(ret, ==, 8);
 503    close(fd);
 504
 505    qemu_chr_fe_set_handlers(&be,
 506                             fe_can_read,
 507                             fe_read,
 508                             fe_event,
 509                             NULL,
 510                             &fe,
 511                             NULL, true);
 512
 513    main_loop();
 514
 515    g_assert_cmpint(fe.read_count, ==, 8);
 516    g_assert_cmpstr(fe.read_buf, ==, "pipe-in");
 517
 518    qemu_chr_fe_deinit(&be, true);
 519
 520    g_assert(g_unlink(in) == 0);
 521    g_assert(g_unlink(out) == 0);
 522    g_assert(g_rmdir(tmp_path) == 0);
 523    g_free(in);
 524    g_free(out);
 525    g_free(tmp_path);
 526    g_free(pipe);
 527}
 528#endif
 529
 530typedef struct SocketIdleData {
 531    GMainLoop *loop;
 532    Chardev *chr;
 533    bool conn_expected;
 534    CharBackend *be;
 535    CharBackend *client_be;
 536} SocketIdleData;
 537
 538
 539static void socket_read_hello(void *opaque, const uint8_t *buf, int size)
 540{
 541    g_assert_cmpint(size, ==, 5);
 542    g_assert(strncmp((char *)buf, "hello", 5) == 0);
 543
 544    quit = true;
 545}
 546
 547static int socket_can_read_hello(void *opaque)
 548{
 549    return 10;
 550}
 551
 552static int make_udp_socket(int *port)
 553{
 554    struct sockaddr_in addr = { 0, };
 555    socklen_t alen = sizeof(addr);
 556    int ret, sock = qemu_socket(PF_INET, SOCK_DGRAM, 0);
 557
 558    g_assert_cmpint(sock, >, 0);
 559    addr.sin_family = AF_INET ;
 560    addr.sin_addr.s_addr = htonl(INADDR_ANY);
 561    addr.sin_port = 0;
 562    ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
 563    g_assert_cmpint(ret, ==, 0);
 564    ret = getsockname(sock, (struct sockaddr *)&addr, &alen);
 565    g_assert_cmpint(ret, ==, 0);
 566
 567    *port = ntohs(addr.sin_port);
 568    return sock;
 569}
 570
 571static void char_udp_test_internal(Chardev *reuse_chr, int sock)
 572{
 573    struct sockaddr_in other;
 574    SocketIdleData d = { 0, };
 575    Chardev *chr;
 576    CharBackend *be;
 577    socklen_t alen = sizeof(other);
 578    int ret;
 579    char buf[10];
 580    char *tmp = NULL;
 581
 582    if (reuse_chr) {
 583        chr = reuse_chr;
 584        be = chr->be;
 585    } else {
 586        int port;
 587        sock = make_udp_socket(&port);
 588        tmp = g_strdup_printf("udp:127.0.0.1:%d", port);
 589        chr = qemu_chr_new("client", tmp, NULL);
 590        g_assert_nonnull(chr);
 591
 592        be = g_alloca(sizeof(CharBackend));
 593        qemu_chr_fe_init(be, chr, &error_abort);
 594    }
 595
 596    d.chr = chr;
 597    qemu_chr_fe_set_handlers(be, socket_can_read_hello, socket_read_hello,
 598                             NULL, NULL, &d, NULL, true);
 599    ret = qemu_chr_write_all(chr, (uint8_t *)"hello", 5);
 600    g_assert_cmpint(ret, ==, 5);
 601
 602    ret = recvfrom(sock, buf, sizeof(buf), 0,
 603                   (struct sockaddr *)&other, &alen);
 604    g_assert_cmpint(ret, ==, 5);
 605    ret = sendto(sock, buf, 5, 0, (struct sockaddr *)&other, alen);
 606    g_assert_cmpint(ret, ==, 5);
 607
 608    main_loop();
 609
 610    if (!reuse_chr) {
 611        close(sock);
 612        qemu_chr_fe_deinit(be, true);
 613    }
 614    g_free(tmp);
 615}
 616
 617static void char_udp_test(void)
 618{
 619    char_udp_test_internal(NULL, 0);
 620}
 621
 622
 623typedef struct {
 624    int event;
 625    bool got_pong;
 626} CharSocketTestData;
 627
 628
 629#define SOCKET_PING "Hello"
 630#define SOCKET_PONG "World"
 631
 632
 633static void
 634char_socket_event(void *opaque, int event)
 635{
 636    CharSocketTestData *data = opaque;
 637    data->event = event;
 638}
 639
 640
 641static void
 642char_socket_read(void *opaque, const uint8_t *buf, int size)
 643{
 644    CharSocketTestData *data = opaque;
 645    g_assert_cmpint(size, ==, sizeof(SOCKET_PONG));
 646    g_assert(memcmp(buf, SOCKET_PONG, size) == 0);
 647    data->got_pong = true;
 648}
 649
 650
 651static int
 652char_socket_can_read(void *opaque)
 653{
 654    return sizeof(SOCKET_PONG);
 655}
 656
 657
 658static char *
 659char_socket_addr_to_opt_str(SocketAddress *addr, bool fd_pass,
 660                            const char *reconnect, bool is_listen)
 661{
 662    if (fd_pass) {
 663        QIOChannelSocket *ioc = qio_channel_socket_new();
 664        int fd;
 665        char *optstr;
 666        g_assert(!reconnect);
 667        if (is_listen) {
 668            qio_channel_socket_listen_sync(ioc, addr, &error_abort);
 669        } else {
 670            qio_channel_socket_connect_sync(ioc, addr, &error_abort);
 671        }
 672        fd = ioc->fd;
 673        ioc->fd = -1;
 674        optstr = g_strdup_printf("socket,id=cdev0,fd=%d%s",
 675                                 fd, is_listen ? ",server,nowait" : "");
 676        object_unref(OBJECT(ioc));
 677        return optstr;
 678    } else {
 679        switch (addr->type) {
 680        case SOCKET_ADDRESS_TYPE_INET:
 681            return g_strdup_printf("socket,id=cdev0,host=%s,port=%s%s%s",
 682                                   addr->u.inet.host,
 683                                   addr->u.inet.port,
 684                                   reconnect ? reconnect : "",
 685                                   is_listen ? ",server,nowait" : "");
 686
 687        case SOCKET_ADDRESS_TYPE_UNIX:
 688            return g_strdup_printf("socket,id=cdev0,path=%s%s%s",
 689                                   addr->u.q_unix.path,
 690                                   reconnect ? reconnect : "",
 691                                   is_listen ? ",server,nowait" : "");
 692
 693        default:
 694            g_assert_not_reached();
 695        }
 696    }
 697}
 698
 699
 700static void
 701char_socket_ping_pong(QIOChannel *ioc)
 702{
 703    char greeting[sizeof(SOCKET_PING)];
 704    const char *response = SOCKET_PONG;
 705
 706    qio_channel_read_all(ioc, greeting, sizeof(greeting), &error_abort);
 707
 708    g_assert(memcmp(greeting, SOCKET_PING, sizeof(greeting)) == 0);
 709
 710    qio_channel_write_all(ioc, response, sizeof(SOCKET_PONG), &error_abort);
 711
 712    object_unref(OBJECT(ioc));
 713}
 714
 715
 716static gpointer
 717char_socket_server_client_thread(gpointer data)
 718{
 719    SocketAddress *addr = data;
 720    QIOChannelSocket *ioc = qio_channel_socket_new();
 721
 722    qio_channel_socket_connect_sync(ioc, addr, &error_abort);
 723
 724    char_socket_ping_pong(QIO_CHANNEL(ioc));
 725
 726    return NULL;
 727}
 728
 729
 730typedef struct {
 731    SocketAddress *addr;
 732    bool wait_connected;
 733    bool fd_pass;
 734} CharSocketServerTestConfig;
 735
 736
 737static void char_socket_server_test(gconstpointer opaque)
 738{
 739    const CharSocketServerTestConfig *config = opaque;
 740    Chardev *chr;
 741    CharBackend be = {0};
 742    CharSocketTestData data = {0};
 743    QObject *qaddr;
 744    SocketAddress *addr;
 745    Visitor *v;
 746    QemuThread thread;
 747    int ret;
 748    bool reconnected = false;
 749    char *optstr;
 750    QemuOpts *opts;
 751
 752    g_setenv("QTEST_SILENT_ERRORS", "1", 1);
 753    /*
 754     * We rely on config->addr containing "nowait", otherwise
 755     * qemu_chr_new() will block until a client connects. We
 756     * can't spawn our client thread though, because until
 757     * qemu_chr_new() returns we don't know what TCP port was
 758     * allocated by the OS
 759     */
 760    optstr = char_socket_addr_to_opt_str(config->addr,
 761                                         config->fd_pass,
 762                                         NULL,
 763                                         true);
 764    opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
 765                                   optstr, true);
 766    g_assert_nonnull(opts);
 767    chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
 768    qemu_opts_del(opts);
 769    g_assert_nonnull(chr);
 770    g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
 771
 772    qaddr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort);
 773    g_assert_nonnull(qaddr);
 774
 775    v = qobject_input_visitor_new(qaddr);
 776    visit_type_SocketAddress(v, "addr", &addr, &error_abort);
 777    visit_free(v);
 778    qobject_unref(qaddr);
 779
 780    qemu_chr_fe_init(&be, chr, &error_abort);
 781
 782 reconnect:
 783    data.event = -1;
 784    qemu_chr_fe_set_handlers(&be, NULL, NULL,
 785                             char_socket_event, NULL,
 786                             &data, NULL, true);
 787    g_assert(data.event == -1);
 788
 789    /*
 790     * Kick off a thread to act as the "remote" client
 791     * which just plays ping-pong with us
 792     */
 793    qemu_thread_create(&thread, "client",
 794                       char_socket_server_client_thread,
 795                       addr, QEMU_THREAD_JOINABLE);
 796    g_assert(data.event == -1);
 797
 798    if (config->wait_connected) {
 799        /* Synchronously accept a connection */
 800        qemu_chr_wait_connected(chr, &error_abort);
 801    } else {
 802        /*
 803         * Asynchronously accept a connection when the evnt
 804         * loop reports the listener socket as readable
 805         */
 806        while (data.event == -1) {
 807            main_loop_wait(false);
 808        }
 809    }
 810    g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort));
 811    g_assert(data.event == CHR_EVENT_OPENED);
 812    data.event = -1;
 813
 814    /* Send a greeting to the client */
 815    ret = qemu_chr_fe_write_all(&be, (const uint8_t *)SOCKET_PING,
 816                                sizeof(SOCKET_PING));
 817    g_assert_cmpint(ret, ==, sizeof(SOCKET_PING));
 818    g_assert(data.event == -1);
 819
 820    /* Setup a callback to receive the reply to our greeting */
 821    qemu_chr_fe_set_handlers(&be, char_socket_can_read,
 822                             char_socket_read,
 823                             char_socket_event, NULL,
 824                             &data, NULL, true);
 825    g_assert(data.event == CHR_EVENT_OPENED);
 826    data.event = -1;
 827
 828    /* Wait for the client to go away */
 829    while (data.event == -1) {
 830        main_loop_wait(false);
 831    }
 832    g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
 833    g_assert(data.event == CHR_EVENT_CLOSED);
 834    g_assert(data.got_pong);
 835
 836    qemu_thread_join(&thread);
 837
 838    if (!reconnected) {
 839        reconnected = true;
 840        goto reconnect;
 841    }
 842
 843    qapi_free_SocketAddress(addr);
 844    object_unparent(OBJECT(chr));
 845    g_free(optstr);
 846    g_unsetenv("QTEST_SILENT_ERRORS");
 847}
 848
 849
 850static gpointer
 851char_socket_client_server_thread(gpointer data)
 852{
 853    QIOChannelSocket *ioc = data;
 854    QIOChannelSocket *cioc;
 855
 856    cioc = qio_channel_socket_accept(ioc, &error_abort);
 857    g_assert_nonnull(cioc);
 858
 859    char_socket_ping_pong(QIO_CHANNEL(cioc));
 860
 861    return NULL;
 862}
 863
 864
 865typedef struct {
 866    SocketAddress *addr;
 867    const char *reconnect;
 868    bool wait_connected;
 869    bool fd_pass;
 870} CharSocketClientTestConfig;
 871
 872
 873static void char_socket_client_test(gconstpointer opaque)
 874{
 875    const CharSocketClientTestConfig *config = opaque;
 876    QIOChannelSocket *ioc;
 877    char *optstr;
 878    Chardev *chr;
 879    CharBackend be = {0};
 880    CharSocketTestData data = {0};
 881    SocketAddress *addr;
 882    QemuThread thread;
 883    int ret;
 884    bool reconnected = false;
 885    QemuOpts *opts;
 886
 887    /*
 888     * Setup a listener socket and determine get its address
 889     * so we know the TCP port for the client later
 890     */
 891    ioc = qio_channel_socket_new();
 892    g_assert_nonnull(ioc);
 893    qio_channel_socket_listen_sync(ioc, config->addr, &error_abort);
 894    addr = qio_channel_socket_get_local_address(ioc, &error_abort);
 895    g_assert_nonnull(addr);
 896
 897    /*
 898     * Kick off a thread to act as the "remote" client
 899     * which just plays ping-pong with us
 900     */
 901    qemu_thread_create(&thread, "client",
 902                       char_socket_client_server_thread,
 903                       ioc, QEMU_THREAD_JOINABLE);
 904
 905    /*
 906     * Populate the chardev address based on what the server
 907     * is actually listening on
 908     */
 909    optstr = char_socket_addr_to_opt_str(addr,
 910                                         config->fd_pass,
 911                                         config->reconnect,
 912                                         false);
 913
 914    opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
 915                                   optstr, true);
 916    g_assert_nonnull(opts);
 917    chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
 918    qemu_opts_del(opts);
 919    g_assert_nonnull(chr);
 920
 921    if (config->reconnect) {
 922        /*
 923         * If reconnect is set, the connection will be
 924         * established in a background thread and we won't
 925         * see the "connected" status updated until we
 926         * run the main event loop, or call qemu_chr_wait_connected
 927         */
 928        g_assert(!object_property_get_bool(OBJECT(chr), "connected",
 929                                           &error_abort));
 930    } else {
 931        g_assert(object_property_get_bool(OBJECT(chr), "connected",
 932                                          &error_abort));
 933    }
 934
 935    qemu_chr_fe_init(&be, chr, &error_abort);
 936
 937 reconnect:
 938    data.event = -1;
 939    qemu_chr_fe_set_handlers(&be, NULL, NULL,
 940                             char_socket_event, NULL,
 941                             &data, NULL, true);
 942    if (config->reconnect) {
 943        g_assert(data.event == -1);
 944    } else {
 945        g_assert(data.event == CHR_EVENT_OPENED);
 946    }
 947
 948    if (config->wait_connected) {
 949        /*
 950         * Synchronously wait for the connection to complete
 951         * This should be a no-op if reconnect is not set.
 952         */
 953        qemu_chr_wait_connected(chr, &error_abort);
 954    } else {
 955        /*
 956         * Asynchronously wait for the connection to be reported
 957         * as complete when the background thread reports its
 958         * status.
 959         * The loop will short-circuit if reconnect was set
 960         */
 961        while (data.event == -1) {
 962            main_loop_wait(false);
 963        }
 964    }
 965    g_assert(data.event == CHR_EVENT_OPENED);
 966    data.event = -1;
 967    g_assert(object_property_get_bool(OBJECT(chr), "connected", &error_abort));
 968
 969    /* Send a greeting to the server */
 970    ret = qemu_chr_fe_write_all(&be, (const uint8_t *)SOCKET_PING,
 971                                sizeof(SOCKET_PING));
 972    g_assert_cmpint(ret, ==, sizeof(SOCKET_PING));
 973    g_assert(data.event == -1);
 974
 975    /* Setup a callback to receive the reply to our greeting */
 976    qemu_chr_fe_set_handlers(&be, char_socket_can_read,
 977                             char_socket_read,
 978                             char_socket_event, NULL,
 979                             &data, NULL, true);
 980    g_assert(data.event == CHR_EVENT_OPENED);
 981    data.event = -1;
 982
 983    /* Wait for the server to go away */
 984    while (data.event == -1) {
 985        main_loop_wait(false);
 986    }
 987    g_assert(data.event == CHR_EVENT_CLOSED);
 988    g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
 989    g_assert(data.got_pong);
 990    qemu_thread_join(&thread);
 991
 992    if (config->reconnect && !reconnected) {
 993        reconnected = true;
 994        qemu_thread_create(&thread, "client",
 995                           char_socket_client_server_thread,
 996                           ioc, QEMU_THREAD_JOINABLE);
 997        goto reconnect;
 998    }
 999
1000    object_unref(OBJECT(ioc));
1001    object_unparent(OBJECT(chr));
1002    qapi_free_SocketAddress(addr);
1003    g_free(optstr);
1004}
1005
1006static void
1007count_closed_event(void *opaque, int event)
1008{
1009    int *count = opaque;
1010    if (event == CHR_EVENT_CLOSED) {
1011        (*count)++;
1012    }
1013}
1014
1015static void
1016char_socket_discard_read(void *opaque, const uint8_t *buf, int size)
1017{
1018}
1019
1020static void char_socket_server_two_clients_test(gconstpointer opaque)
1021{
1022    SocketAddress *incoming_addr = (gpointer) opaque;
1023    Chardev *chr;
1024    CharBackend be = {0};
1025    QObject *qaddr;
1026    SocketAddress *addr;
1027    Visitor *v;
1028    char *optstr;
1029    QemuOpts *opts;
1030    QIOChannelSocket *ioc1, *ioc2;
1031    int closed = 0;
1032
1033    g_setenv("QTEST_SILENT_ERRORS", "1", 1);
1034    /*
1035     * We rely on addr containing "nowait", otherwise
1036     * qemu_chr_new() will block until a client connects. We
1037     * can't spawn our client thread though, because until
1038     * qemu_chr_new() returns we don't know what TCP port was
1039     * allocated by the OS
1040     */
1041    optstr = char_socket_addr_to_opt_str(incoming_addr,
1042                                         false,
1043                                         NULL,
1044                                         true);
1045    opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
1046                                   optstr, true);
1047    g_assert_nonnull(opts);
1048    chr = qemu_chr_new_from_opts(opts, NULL, &error_abort);
1049    qemu_opts_del(opts);
1050    g_assert_nonnull(chr);
1051    g_assert(!object_property_get_bool(OBJECT(chr), "connected", &error_abort));
1052
1053    qaddr = object_property_get_qobject(OBJECT(chr), "addr", &error_abort);
1054    g_assert_nonnull(qaddr);
1055
1056    v = qobject_input_visitor_new(qaddr);
1057    visit_type_SocketAddress(v, "addr", &addr, &error_abort);
1058    visit_free(v);
1059    qobject_unref(qaddr);
1060
1061    qemu_chr_fe_init(&be, chr, &error_abort);
1062
1063    qemu_chr_fe_set_handlers(&be, char_socket_can_read, char_socket_discard_read,
1064                             count_closed_event, NULL,
1065                             &closed, NULL, true);
1066
1067    ioc1 = qio_channel_socket_new();
1068    qio_channel_socket_connect_sync(ioc1, addr, &error_abort);
1069    qemu_chr_wait_connected(chr, &error_abort);
1070
1071    /* switch the chardev to another context */
1072    GMainContext *ctx = g_main_context_new();
1073    qemu_chr_fe_set_handlers(&be, char_socket_can_read, char_socket_discard_read,
1074                             count_closed_event, NULL,
1075                             &closed, ctx, true);
1076
1077    /* Start a second connection while the first is still connected.
1078     * It will be placed in the listen() backlog, and connect() will
1079     * succeed immediately.
1080     */
1081    ioc2 = qio_channel_socket_new();
1082    qio_channel_socket_connect_sync(ioc2, addr, &error_abort);
1083
1084    object_unref(OBJECT(ioc1));
1085    /* The two connections should now be processed serially.  */
1086    while (g_main_context_iteration(ctx, TRUE)) {
1087        if (closed == 1 && ioc2) {
1088            object_unref(OBJECT(ioc2));
1089            ioc2 = NULL;
1090        }
1091        if (closed == 2) {
1092            break;
1093        }
1094    }
1095
1096    qapi_free_SocketAddress(addr);
1097    object_unparent(OBJECT(chr));
1098    g_main_context_unref(ctx);
1099    g_free(optstr);
1100    g_unsetenv("QTEST_SILENT_ERRORS");
1101}
1102
1103
1104#ifdef HAVE_CHARDEV_SERIAL
1105static void char_serial_test(void)
1106{
1107    QemuOpts *opts;
1108    Chardev *chr;
1109
1110    opts = qemu_opts_create(qemu_find_opts("chardev"), "serial-id",
1111                            1, &error_abort);
1112    qemu_opt_set(opts, "backend", "serial", &error_abort);
1113    qemu_opt_set(opts, "path", "/dev/null", &error_abort);
1114
1115    chr = qemu_chr_new_from_opts(opts, NULL, NULL);
1116    g_assert_nonnull(chr);
1117    /* TODO: add more tests with a pty */
1118    object_unparent(OBJECT(chr));
1119
1120    /* test tty alias */
1121    qemu_opt_set(opts, "backend", "tty", &error_abort);
1122    chr = qemu_chr_new_from_opts(opts, NULL, NULL);
1123    g_assert_nonnull(chr);
1124    object_unparent(OBJECT(chr));
1125
1126    qemu_opts_del(opts);
1127}
1128#endif
1129
1130#ifndef _WIN32
1131static void char_file_fifo_test(void)
1132{
1133    Chardev *chr;
1134    CharBackend be;
1135    char *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
1136    char *fifo = g_build_filename(tmp_path, "fifo", NULL);
1137    char *out = g_build_filename(tmp_path, "out", NULL);
1138    ChardevFile file = { .in = fifo,
1139                         .has_in = true,
1140                         .out = out };
1141    ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE,
1142                               .u.file.data = &file };
1143    FeHandler fe = { 0, };
1144    int fd, ret;
1145
1146    if (mkfifo(fifo, 0600) < 0) {
1147        abort();
1148    }
1149
1150    fd = open(fifo, O_RDWR);
1151    ret = write(fd, "fifo-in", 8);
1152    g_assert_cmpint(ret, ==, 8);
1153
1154    chr = qemu_chardev_new("label-file", TYPE_CHARDEV_FILE, &backend,
1155                           NULL, &error_abort);
1156
1157    qemu_chr_fe_init(&be, chr, &error_abort);
1158    qemu_chr_fe_set_handlers(&be,
1159                             fe_can_read,
1160                             fe_read,
1161                             fe_event,
1162                             NULL,
1163                             &fe, NULL, true);
1164
1165    g_assert_cmpint(fe.last_event, !=, CHR_EVENT_BREAK);
1166    qmp_chardev_send_break("label-foo", NULL);
1167    g_assert_cmpint(fe.last_event, !=, CHR_EVENT_BREAK);
1168    qmp_chardev_send_break("label-file", NULL);
1169    g_assert_cmpint(fe.last_event, ==, CHR_EVENT_BREAK);
1170
1171    main_loop();
1172
1173    close(fd);
1174
1175    g_assert_cmpint(fe.read_count, ==, 8);
1176    g_assert_cmpstr(fe.read_buf, ==, "fifo-in");
1177
1178    qemu_chr_fe_deinit(&be, true);
1179
1180    g_unlink(fifo);
1181    g_free(fifo);
1182    g_unlink(out);
1183    g_free(out);
1184    g_rmdir(tmp_path);
1185    g_free(tmp_path);
1186}
1187#endif
1188
1189static void char_file_test_internal(Chardev *ext_chr, const char *filepath)
1190{
1191    char *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
1192    char *out;
1193    Chardev *chr;
1194    char *contents = NULL;
1195    ChardevFile file = {};
1196    ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE,
1197                               .u.file.data = &file };
1198    gsize length;
1199    int ret;
1200
1201    if (ext_chr) {
1202        chr = ext_chr;
1203        out = g_strdup(filepath);
1204        file.out = out;
1205    } else {
1206        out = g_build_filename(tmp_path, "out", NULL);
1207        file.out = out;
1208        chr = qemu_chardev_new(NULL, TYPE_CHARDEV_FILE, &backend,
1209                               NULL, &error_abort);
1210    }
1211    ret = qemu_chr_write_all(chr, (uint8_t *)"hello!", 6);
1212    g_assert_cmpint(ret, ==, 6);
1213
1214    ret = g_file_get_contents(out, &contents, &length, NULL);
1215    g_assert(ret == TRUE);
1216    g_assert_cmpint(length, ==, 6);
1217    g_assert(strncmp(contents, "hello!", 6) == 0);
1218
1219    if (!ext_chr) {
1220        object_unref(OBJECT(chr));
1221        g_unlink(out);
1222    }
1223    g_free(contents);
1224    g_rmdir(tmp_path);
1225    g_free(tmp_path);
1226    g_free(out);
1227}
1228
1229static void char_file_test(void)
1230{
1231    char_file_test_internal(NULL, NULL);
1232}
1233
1234static void char_null_test(void)
1235{
1236    Error *err = NULL;
1237    Chardev *chr;
1238    CharBackend be;
1239    int ret;
1240
1241    chr = qemu_chr_find("label-null");
1242    g_assert_null(chr);
1243
1244    chr = qemu_chr_new("label-null", "null", NULL);
1245    chr = qemu_chr_find("label-null");
1246    g_assert_nonnull(chr);
1247
1248    g_assert(qemu_chr_has_feature(chr,
1249                 QEMU_CHAR_FEATURE_FD_PASS) == false);
1250    g_assert(qemu_chr_has_feature(chr,
1251                 QEMU_CHAR_FEATURE_RECONNECTABLE) == false);
1252
1253    /* check max avail */
1254    qemu_chr_fe_init(&be, chr, &error_abort);
1255    qemu_chr_fe_init(&be, chr, &err);
1256    error_free_or_abort(&err);
1257
1258    /* deinit & reinit */
1259    qemu_chr_fe_deinit(&be, false);
1260    qemu_chr_fe_init(&be, chr, &error_abort);
1261
1262    qemu_chr_fe_set_open(&be, true);
1263
1264    qemu_chr_fe_set_handlers(&be,
1265                             fe_can_read,
1266                             fe_read,
1267                             fe_event,
1268                             NULL,
1269                             NULL, NULL, true);
1270
1271    ret = qemu_chr_fe_write(&be, (void *)"buf", 4);
1272    g_assert_cmpint(ret, ==, 4);
1273
1274    qemu_chr_fe_deinit(&be, true);
1275}
1276
1277static void char_invalid_test(void)
1278{
1279    Chardev *chr;
1280    g_setenv("QTEST_SILENT_ERRORS", "1", 1);
1281    chr = qemu_chr_new("label-invalid", "invalid", NULL);
1282    g_assert_null(chr);
1283    g_unsetenv("QTEST_SILENT_ERRORS");
1284}
1285
1286static int chardev_change(void *opaque)
1287{
1288    return 0;
1289}
1290
1291static int chardev_change_denied(void *opaque)
1292{
1293    return -1;
1294}
1295
1296static void char_hotswap_test(void)
1297{
1298    char *chr_args;
1299    Chardev *chr;
1300    CharBackend be;
1301
1302    gchar *tmp_path = g_dir_make_tmp("qemu-test-char.XXXXXX", NULL);
1303    char *filename = g_build_filename(tmp_path, "file", NULL);
1304    ChardevFile file = { .out = filename };
1305    ChardevBackend backend = { .type = CHARDEV_BACKEND_KIND_FILE,
1306                               .u.file.data = &file };
1307    ChardevReturn *ret;
1308
1309    int port;
1310    int sock = make_udp_socket(&port);
1311    g_assert_cmpint(sock, >, 0);
1312
1313    chr_args = g_strdup_printf("udp:127.0.0.1:%d", port);
1314
1315    chr = qemu_chr_new("chardev", chr_args, NULL);
1316    qemu_chr_fe_init(&be, chr, &error_abort);
1317
1318    /* check that chardev operates correctly */
1319    char_udp_test_internal(chr, sock);
1320
1321    /* set the handler that denies the hotswap */
1322    qemu_chr_fe_set_handlers(&be, NULL, NULL,
1323                             NULL, chardev_change_denied, NULL, NULL, true);
1324
1325    /* now, change is denied and has to keep the old backend operating */
1326    ret = qmp_chardev_change("chardev", &backend, NULL);
1327    g_assert(!ret);
1328    g_assert(be.chr == chr);
1329
1330    char_udp_test_internal(chr, sock);
1331
1332    /* now allow the change */
1333    qemu_chr_fe_set_handlers(&be, NULL, NULL,
1334                             NULL, chardev_change, NULL, NULL, true);
1335
1336    /* has to succeed now */
1337    ret = qmp_chardev_change("chardev", &backend, &error_abort);
1338    g_assert(be.chr != chr);
1339
1340    close(sock);
1341    chr = be.chr;
1342
1343    /* run the file chardev test */
1344    char_file_test_internal(chr, filename);
1345
1346    object_unparent(OBJECT(chr));
1347
1348    qapi_free_ChardevReturn(ret);
1349    g_unlink(filename);
1350    g_free(filename);
1351    g_rmdir(tmp_path);
1352    g_free(tmp_path);
1353    g_free(chr_args);
1354}
1355
1356int main(int argc, char **argv)
1357{
1358    qemu_init_main_loop(&error_abort);
1359    socket_init();
1360
1361    g_test_init(&argc, &argv, NULL);
1362
1363    module_call_init(MODULE_INIT_QOM);
1364    qemu_add_opts(&qemu_chardev_opts);
1365
1366    g_test_add_func("/char/null", char_null_test);
1367    g_test_add_func("/char/invalid", char_invalid_test);
1368    g_test_add_func("/char/ringbuf", char_ringbuf_test);
1369    g_test_add_func("/char/mux", char_mux_test);
1370#ifdef _WIN32
1371    g_test_add_func("/char/console/subprocess", char_console_test_subprocess);
1372    g_test_add_func("/char/console", char_console_test);
1373#endif
1374    g_test_add_func("/char/stdio/subprocess", char_stdio_test_subprocess);
1375    g_test_add_func("/char/stdio", char_stdio_test);
1376#ifndef _WIN32
1377    g_test_add_func("/char/pipe", char_pipe_test);
1378#endif
1379    g_test_add_func("/char/file", char_file_test);
1380#ifndef _WIN32
1381    g_test_add_func("/char/file-fifo", char_file_fifo_test);
1382#endif
1383
1384    SocketAddress tcpaddr = {
1385        .type = SOCKET_ADDRESS_TYPE_INET,
1386        .u.inet.host = (char *)"127.0.0.1",
1387        .u.inet.port = (char *)"0",
1388    };
1389#ifndef WIN32
1390    SocketAddress unixaddr = {
1391        .type = SOCKET_ADDRESS_TYPE_UNIX,
1392        .u.q_unix.path = (char *)"test-char.sock",
1393    };
1394#endif
1395
1396#define SOCKET_SERVER_TEST(name, addr)                                  \
1397    CharSocketServerTestConfig server1 ## name =                        \
1398        { addr, false, false };                                         \
1399    CharSocketServerTestConfig server2 ## name =                        \
1400        { addr, true, false };                                          \
1401    CharSocketServerTestConfig server3 ## name =                        \
1402        { addr, false, true };                                          \
1403    CharSocketServerTestConfig server4 ## name =                        \
1404        { addr, true, true };                                           \
1405    g_test_add_data_func("/char/socket/server/mainloop/" # name,        \
1406                         &server1 ##name, char_socket_server_test);     \
1407    g_test_add_data_func("/char/socket/server/wait-conn/" # name,       \
1408                         &server2 ##name, char_socket_server_test);     \
1409    g_test_add_data_func("/char/socket/server/mainloop-fdpass/" # name, \
1410                         &server3 ##name, char_socket_server_test);     \
1411    g_test_add_data_func("/char/socket/server/wait-conn-fdpass/" # name, \
1412                         &server4 ##name, char_socket_server_test)
1413
1414#define SOCKET_CLIENT_TEST(name, addr)                                  \
1415    CharSocketClientTestConfig client1 ## name =                        \
1416        { addr, NULL, false, false };                                   \
1417    CharSocketClientTestConfig client2 ## name =                        \
1418        { addr, NULL, true, false };                                    \
1419    CharSocketClientTestConfig client3 ## name =                        \
1420        { addr, ",reconnect=1", false };                                \
1421    CharSocketClientTestConfig client4 ## name =                        \
1422        { addr, ",reconnect=1", true };                                 \
1423    CharSocketClientTestConfig client5 ## name =                        \
1424        { addr, NULL, false, true };                                    \
1425    CharSocketClientTestConfig client6 ## name =                        \
1426        { addr, NULL, true, true };                                     \
1427    g_test_add_data_func("/char/socket/client/mainloop/" # name,        \
1428                         &client1 ##name, char_socket_client_test);     \
1429    g_test_add_data_func("/char/socket/client/wait-conn/" # name,       \
1430                         &client2 ##name, char_socket_client_test);     \
1431    g_test_add_data_func("/char/socket/client/mainloop-reconnect/" # name, \
1432                         &client3 ##name, char_socket_client_test);     \
1433    g_test_add_data_func("/char/socket/client/wait-conn-reconnect/" # name, \
1434                         &client4 ##name, char_socket_client_test);     \
1435    g_test_add_data_func("/char/socket/client/mainloop-fdpass/" # name, \
1436                         &client5 ##name, char_socket_client_test);     \
1437    g_test_add_data_func("/char/socket/client/wait-conn-fdpass/" # name, \
1438                         &client6 ##name, char_socket_client_test)
1439
1440    SOCKET_SERVER_TEST(tcp, &tcpaddr);
1441    SOCKET_CLIENT_TEST(tcp, &tcpaddr);
1442    g_test_add_data_func("/char/socket/server/two-clients/tcp", &tcpaddr,
1443                         char_socket_server_two_clients_test);
1444#ifndef WIN32
1445    SOCKET_SERVER_TEST(unix, &unixaddr);
1446    SOCKET_CLIENT_TEST(unix, &unixaddr);
1447    g_test_add_data_func("/char/socket/server/two-clients/unix", &unixaddr,
1448                         char_socket_server_two_clients_test);
1449#endif
1450
1451    g_test_add_func("/char/udp", char_udp_test);
1452#ifdef HAVE_CHARDEV_SERIAL
1453    g_test_add_func("/char/serial", char_serial_test);
1454#endif
1455    g_test_add_func("/char/hotswap", char_hotswap_test);
1456    g_test_add_func("/char/websocket", char_websock_test);
1457
1458    return g_test_run();
1459}
1460