]> Git Repo - qemu.git/blob - tests/test-filter-mirror.c
iotests: Let 045 be run concurrently
[qemu.git] / tests / test-filter-mirror.c
1 /*
2  * QTest testcase for filter-mirror
3  *
4  * Copyright (c) 2016 FUJITSU LIMITED
5  * Author: Zhang Chen <[email protected]>
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 /* TODO actually test the results and get rid of this */
20 #define qmp_discard_response(qs, ...) qobject_unref(qtest_qmp(qs, __VA_ARGS__))
21
22 static void test_mirror(void)
23 {
24     int send_sock[2], recv_sock[2];
25     uint32_t ret = 0, len = 0;
26     char send_buf[] = "Hello! filter-mirror~";
27     char *recv_buf;
28     uint32_t size = sizeof(send_buf);
29     size = htonl(size);
30     const char *devstr = "e1000";
31     QTestState *qts;
32
33     if (g_str_equal(qtest_get_arch(), "s390x")) {
34         devstr = "virtio-net-ccw";
35     }
36
37     ret = socketpair(PF_UNIX, SOCK_STREAM, 0, send_sock);
38     g_assert_cmpint(ret, !=, -1);
39
40     ret = socketpair(PF_UNIX, SOCK_STREAM, 0, recv_sock);
41     g_assert_cmpint(ret, !=, -1);
42
43     qts = qtest_initf(
44         "-netdev socket,id=qtest-bn0,fd=%d "
45         "-device %s,netdev=qtest-bn0,id=qtest-e0 "
46         "-chardev socket,id=mirror0,fd=%d "
47         "-object filter-mirror,id=qtest-f0,netdev=qtest-bn0,queue=tx,outdev=mirror0 "
48         , send_sock[1], devstr, recv_sock[1]);
49
50     struct iovec iov[] = {
51         {
52             .iov_base = &size,
53             .iov_len = sizeof(size),
54         }, {
55             .iov_base = send_buf,
56             .iov_len = sizeof(send_buf),
57         },
58     };
59
60     /* send a qmp command to guarantee that 'connected' is setting to true. */
61     qmp_discard_response(qts, "{ 'execute' : 'query-status'}");
62     ret = iov_send(send_sock[0], iov, 2, 0, sizeof(size) + sizeof(send_buf));
63     g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
64     close(send_sock[0]);
65
66     ret = qemu_recv(recv_sock[0], &len, sizeof(len), 0);
67     g_assert_cmpint(ret, ==, sizeof(len));
68     len = ntohl(len);
69
70     g_assert_cmpint(len, ==, sizeof(send_buf));
71     recv_buf = g_malloc(len);
72     ret = qemu_recv(recv_sock[0], recv_buf, len, 0);
73     g_assert_cmpstr(recv_buf, ==, send_buf);
74
75     g_free(recv_buf);
76     close(send_sock[0]);
77     close(send_sock[1]);
78     close(recv_sock[0]);
79     close(recv_sock[1]);
80     qtest_quit(qts);
81 }
82
83 int main(int argc, char **argv)
84 {
85     int ret;
86
87     g_test_init(&argc, &argv, NULL);
88
89     qtest_add_func("/netfilter/mirror", test_mirror);
90     ret = g_test_run();
91
92     return ret;
93 }
This page took 0.029934 seconds and 4 git commands to generate.