xref: /qemu/tests/qtest/test-filter-mirror.c (revision 73b49878)
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 "qapi/qmp/qdict.h"
14 #include "qemu/iov.h"
15 #include "qemu/sockets.h"
16 #include "qemu/error-report.h"
17 #include "qemu/main-loop.h"
18 
19 static void test_mirror(void)
20 {
21     int send_sock[2], recv_sock[2];
22     uint32_t ret = 0, len = 0;
23     char send_buf[] = "Hello! filter-mirror~";
24     char *recv_buf;
25     uint32_t size = sizeof(send_buf);
26     size = htonl(size);
27     QTestState *qts;
28 
29     ret = socketpair(PF_UNIX, SOCK_STREAM, 0, send_sock);
30     g_assert_cmpint(ret, !=, -1);
31 
32     ret = socketpair(PF_UNIX, SOCK_STREAM, 0, recv_sock);
33     g_assert_cmpint(ret, !=, -1);
34 
35     qts = qtest_initf(
36         "-nic socket,id=qtest-bn0,fd=%d "
37         "-chardev socket,id=mirror0,fd=%d "
38         "-object filter-mirror,id=qtest-f0,netdev=qtest-bn0,queue=tx,outdev=mirror0 "
39         , send_sock[1], recv_sock[1]);
40 
41     struct iovec iov[] = {
42         {
43             .iov_base = &size,
44             .iov_len = sizeof(size),
45         }, {
46             .iov_base = send_buf,
47             .iov_len = sizeof(send_buf),
48         },
49     };
50 
51     /* send a qmp command to guarantee that 'connected' is setting to true. */
52     qtest_qmp_assert_success(qts, "{ 'execute' : 'query-status'}");
53     ret = iov_send(send_sock[0], iov, 2, 0, sizeof(size) + sizeof(send_buf));
54     g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
55     close(send_sock[0]);
56 
57     ret = recv(recv_sock[0], &len, sizeof(len), 0);
58     g_assert_cmpint(ret, ==, sizeof(len));
59     len = ntohl(len);
60 
61     g_assert_cmpint(len, ==, sizeof(send_buf));
62     recv_buf = g_malloc(len);
63     ret = recv(recv_sock[0], recv_buf, len, 0);
64     g_assert_cmpint(ret, ==, len);
65     g_assert_cmpstr(recv_buf, ==, send_buf);
66 
67     g_free(recv_buf);
68     close(send_sock[0]);
69     close(send_sock[1]);
70     close(recv_sock[0]);
71     close(recv_sock[1]);
72     qtest_quit(qts);
73 }
74 
75 int main(int argc, char **argv)
76 {
77     g_test_init(&argc, &argv, NULL);
78 
79     qtest_add_func("/netfilter/mirror", test_mirror);
80     return g_test_run();
81 }
82