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