File: test_tcp_accept_filter.cpp

package info (click to toggle)
zeromq3 4.3.5-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,548 kB
  • sloc: cpp: 56,475; ansic: 4,968; makefile: 1,607; sh: 1,400; xml: 196; python: 40
file content (191 lines) | stat: -rw-r--r-- 5,752 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* SPDX-License-Identifier: MPL-2.0 */

#include "testutil.hpp"
#include "testutil_unity.hpp"

#include <cstring>

SETUP_TEARDOWN_TESTCONTEXT

void test_reconnect_ivl_against_pair_socket (const char *my_endpoint_,
                                             void *sb_)
{
    void *sc = test_context_socket (ZMQ_PAIR);
    int interval = -1;
    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (sc, ZMQ_RECONNECT_IVL, &interval, sizeof (int)));
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_));

    bounce (sb_, sc);

    TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb_, my_endpoint_));

    expect_bounce_fail (sb_, sc);

    TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb_, my_endpoint_));

    expect_bounce_fail (sb_, sc);

    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint_));

    bounce (sb_, sc);

    test_context_socket_close (sc);
}

void test_reconnect_ivl_tcp (bind_function_t bind_function_)
{
    char my_endpoint[MAX_SOCKET_STRING];

    void *sb = test_context_socket (ZMQ_PAIR);
    bind_function_ (sb, my_endpoint, sizeof my_endpoint);

    test_reconnect_ivl_against_pair_socket (my_endpoint, sb);
    test_context_socket_close (sb);
}

void test_bad_filter_string (const char *const filter_)
{
    void *socket = test_context_socket (ZMQ_PAIR);

    TEST_ASSERT_FAILURE_ERRNO (EINVAL,
                               zmq_setsockopt (socket, ZMQ_TCP_ACCEPT_FILTER,
                                               filter_, strlen (filter_)));

    test_context_socket_close (socket);
}

#define TEST_BAD_FILTER_STRING(case, filter)                                   \
    void test_bad_filter_string_##case ()                                      \
    {                                                                          \
        test_bad_filter_string (filter);                                       \
    }

TEST_BAD_FILTER_STRING (foo, "foo")
TEST_BAD_FILTER_STRING (zeros_foo, "0.0.0.0foo")
TEST_BAD_FILTER_STRING (zeros_foo_mask, "0.0.0.0/foo")
TEST_BAD_FILTER_STRING (zeros_empty_mask, "0.0.0.0/")
TEST_BAD_FILTER_STRING (zeros_negative_mask, "0.0.0.0/-1")
TEST_BAD_FILTER_STRING (zeros_too_large_mask, "0.0.0.0/33")

void test_clear ()
{
    void *bind_socket = test_context_socket (ZMQ_PAIR);

    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (bind_socket, ZMQ_TCP_ACCEPT_FILTER, NULL, 0));

#if 0
    // XXX Shouldn't this work as well?
    const char empty_filter[] = "";
    TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
      socket, ZMQ_TCP_ACCEPT_FILTER, empty_filter, strlen (empty_filter)));
#endif

    char endpoint[MAX_SOCKET_STRING];
    bind_loopback_ipv4 (bind_socket, endpoint, sizeof (endpoint));

    void *connect_socket = test_context_socket (ZMQ_PAIR);
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, endpoint));

    bounce (bind_socket, connect_socket);

    test_context_socket_close (connect_socket);
    test_context_socket_close (bind_socket);
}

const char non_matching_filter[] = "127.0.0.255/32";

void test_set_non_matching_and_clear ()
{
    void *bind_socket = test_context_socket (ZMQ_PAIR);

    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (bind_socket, ZMQ_TCP_ACCEPT_FILTER, non_matching_filter,
                      strlen (non_matching_filter)));

    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (bind_socket, ZMQ_TCP_ACCEPT_FILTER, NULL, 0));

    char endpoint[MAX_SOCKET_STRING];
    bind_loopback_ipv4 (bind_socket, endpoint, sizeof (endpoint));

    void *connect_socket = test_context_socket (ZMQ_PAIR);
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, endpoint));

    bounce (bind_socket, connect_socket);

    test_context_socket_close (connect_socket);
    test_context_socket_close (bind_socket);
}

void test_set_matching (const char *const filter_)
{
    void *bind_socket = test_context_socket (ZMQ_PAIR);

    TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (
      bind_socket, ZMQ_TCP_ACCEPT_FILTER, filter_, strlen (filter_)));

    char endpoint[MAX_SOCKET_STRING];
    bind_loopback_ipv4 (bind_socket, endpoint, sizeof (endpoint));

    void *connect_socket = test_context_socket (ZMQ_PAIR);
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, endpoint));

    bounce (bind_socket, connect_socket);

    test_context_socket_close (connect_socket);
    test_context_socket_close (bind_socket);
}

void test_set_matching_1 ()
{
    test_set_matching ("127.0.0.1/32");
}

void test_set_matching_2 ()
{
    test_set_matching ("0.0.0.0/0");
}

void test_set_non_matching ()
{
    void *bind_socket = test_context_socket (ZMQ_PAIR);

    TEST_ASSERT_SUCCESS_ERRNO (
      zmq_setsockopt (bind_socket, ZMQ_TCP_ACCEPT_FILTER, non_matching_filter,
                      strlen (non_matching_filter)));

    char endpoint[MAX_SOCKET_STRING];
    bind_loopback_ipv4 (bind_socket, endpoint, sizeof (endpoint));

    void *connect_socket = test_context_socket (ZMQ_PAIR);
    TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (connect_socket, endpoint));

    expect_bounce_fail (bind_socket, connect_socket);

    test_context_socket_close_zero_linger (connect_socket);
    test_context_socket_close_zero_linger (bind_socket);
}

int main ()
{
    setup_test_environment ();

    UNITY_BEGIN ();
    RUN_TEST (test_bad_filter_string_foo);
    RUN_TEST (test_bad_filter_string_zeros_foo);
    RUN_TEST (test_bad_filter_string_zeros_foo_mask);
    RUN_TEST (test_bad_filter_string_zeros_empty_mask);
    RUN_TEST (test_bad_filter_string_zeros_negative_mask);
    RUN_TEST (test_bad_filter_string_zeros_too_large_mask);

    RUN_TEST (test_clear);
    RUN_TEST (test_set_non_matching_and_clear);
    RUN_TEST (test_set_matching_1);
    RUN_TEST (test_set_matching_2);

    RUN_TEST (test_set_non_matching);

    return UNITY_END ();
}