qemu/tests/test-filter-mirror.c
<<
>>
Prefs
   1/*
   2 * QTest testcase for filter-mirror
   3 *
   4 * Copyright (c) 2016 FUJITSU LIMITED
   5 * Author: Zhang Chen <zhangchen.fnst@cn.fujitsu.com>
   6 *
   7 * This work is licensed under the terms of the GNU GPL, version 2 or
   8 * later.  See the COPYING file in the top-level directory.
   9 */
  10
  11#include "qemu/osdep.h"
  12#include "libqtest.h"
  13#include "qemu/iov.h"
  14#include "qemu/sockets.h"
  15#include "qemu/error-report.h"
  16#include "qemu/main-loop.h"
  17
  18static void test_mirror(void)
  19{
  20#ifndef _WIN32
  21/* socketpair(PF_UNIX) which does not exist on windows */
  22
  23    int send_sock[2], recv_sock;
  24    char *cmdline;
  25    uint32_t ret = 0, len = 0;
  26    char send_buf[] = "Hello! filter-mirror~";
  27    char sock_path[] = "filter-mirror.XXXXXX";
  28    char *recv_buf;
  29    uint32_t size = sizeof(send_buf);
  30    size = htonl(size);
  31
  32    ret = socketpair(PF_UNIX, SOCK_STREAM, 0, send_sock);
  33    g_assert_cmpint(ret, !=, -1);
  34
  35    ret = mkstemp(sock_path);
  36    g_assert_cmpint(ret, !=, -1);
  37
  38    cmdline = g_strdup_printf("-netdev socket,id=qtest-bn0,fd=%d "
  39                 "-device e1000,netdev=qtest-bn0,id=qtest-e0 "
  40                 "-chardev socket,id=mirror0,path=%s,server,nowait "
  41                 "-object filter-mirror,id=qtest-f0,netdev=qtest-bn0,queue=tx,outdev=mirror0 "
  42                 , send_sock[1], sock_path);
  43    qtest_start(cmdline);
  44    g_free(cmdline);
  45
  46    recv_sock = unix_connect(sock_path, NULL);
  47    g_assert_cmpint(recv_sock, !=, -1);
  48
  49    struct iovec iov[] = {
  50        {
  51            .iov_base = &size,
  52            .iov_len = sizeof(size),
  53        }, {
  54            .iov_base = send_buf,
  55            .iov_len = sizeof(send_buf),
  56        },
  57    };
  58
  59    /* send a qmp command to guarantee that 'connected' is setting to true. */
  60    qmp_discard_response("{ 'execute' : 'query-status'}");
  61    ret = iov_send(send_sock[0], iov, 2, 0, sizeof(size) + sizeof(send_buf));
  62    g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
  63    close(send_sock[0]);
  64
  65    ret = qemu_recv(recv_sock, &len, sizeof(len), 0);
  66    g_assert_cmpint(ret, ==, sizeof(len));
  67    len = ntohl(len);
  68
  69    g_assert_cmpint(len, ==, sizeof(send_buf));
  70    recv_buf = g_malloc(len);
  71    ret = qemu_recv(recv_sock, recv_buf, len, 0);
  72    g_assert_cmpstr(recv_buf, ==, send_buf);
  73
  74    g_free(recv_buf);
  75    close(recv_sock);
  76    unlink(sock_path);
  77
  78#endif
  79}
  80
  81int main(int argc, char **argv)
  82{
  83    int ret;
  84
  85    g_test_init(&argc, &argv, NULL);
  86
  87    qtest_add_func("/netfilter/mirror", test_mirror);
  88    ret = g_test_run();
  89    qtest_end();
  90
  91    return ret;
  92}
  93