File: test-listener-manager.c

package info (click to toggle)
mptcpd 0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,612 kB
  • sloc: ansic: 9,472; sh: 5,154; makefile: 467; cpp: 61
file content (251 lines) | stat: -rw-r--r-- 7,330 bytes parent folder | download | duplicates (2)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// SPDX-License-Identifier: BSD-3-Clause
/**
 * @file test-listener-manager.c
 *
 * @brief mptcpd listener manager test.
 *
 * Copyright (c) 2022, Intel Corporation
 */

#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/un.h>

#include <ell/ell.h>

#include <mptcpd/private/listener_manager.h>
#include <mptcpd/listener_manager.h>

#include "test-util.h"

#undef NDEBUG
#include <assert.h>


struct ipv4_listen_case
{
        struct sockaddr_in addr;
        char const *const desc;
};

struct ipv6_listen_case
{
        struct sockaddr_in6 addr;
        char const *const desc;
};

/**
 * @brief Initialize test case to listen on IPv4 loopback address.
 *
 * @param[in] port TCP port to listen on - host byte order.
 */
#define INIT_IPV4_TEST_CASE(port)                                       \
        {                                                               \
                .addr = {                                               \
                        .sin_family = AF_INET,                          \
                        .sin_port   = htons(port),                      \
                        .sin_addr   = {                                 \
                                .s_addr = htonl(INADDR_LOOPBACK)        \
                        }                                               \
                },                                                      \
                .desc = "listen - IPv4 on port " L_STRINGIFY(port)      \
        }

/**
 * @brief Initialize test case to listen on IPv6 loopback address.
 *
 * @param[in] port TCP port to listen on - host byte order.
 */
#define INIT_IPV6_TEST_CASE(port)                                       \
        {                                                               \
                .addr = {                                               \
                        .sin6_family = AF_INET6,                        \
                        .sin6_port   = htons(port),                     \
                        .sin6_addr   = { .s6_addr = { [15] = 0x01 } }   \
                },                                                      \
                .desc = "listen - IPv6 on port " L_STRINGIFY(port)      \
        }

// -----------------------------------------------------------------------

static struct mptcpd_lm *_lm;

static in_port_t get_port(struct sockaddr const *sa)
{
        return sa->sa_family == AF_INET
                ? ((struct sockaddr_in const *) sa)->sin_port
                : ((struct sockaddr_in6 const *) sa)->sin6_port;
}


static void test_create(void const *test_data)
{
        (void) test_data;

        _lm = mptcpd_lm_create();

        assert(_lm != NULL);
}

static void test_listen(void const *test_data)
{
        struct sockaddr *const sa = (struct sockaddr *) test_data;

        if (sa == NULL) {
                assert(mptcpd_lm_listen(_lm, sa) != 0);
                return;
        }

        in_port_t const original_port = get_port(sa);

        assert(mptcpd_lm_listen(_lm, sa) == 0);

        in_port_t port = get_port(sa);

        assert(port != 0);

        if (original_port != 0)
                assert(original_port == port);

        port = ntohs(port);

        l_info("Listening on port 0x%x (%u)", port, port);
}

static void test_listen_bad_address(void const *test_data)
{
        struct sockaddr *const sa = (struct sockaddr *) test_data;

        assert(mptcpd_lm_listen(_lm, sa) != 0);
}

static void test_close(void const *test_data)
{
        struct sockaddr const *const sa = test_data;

        if (sa == NULL) {
                assert(mptcpd_lm_close(_lm, sa) != 0);
                return;
        }

        in_port_t const port = get_port(sa);

        if (port == 0)
                assert(mptcpd_lm_close(_lm, sa) != 0);
        else
                assert(mptcpd_lm_close(_lm, sa) == 0);
}

static void test_destroy(void const *test_data)
{
        (void) test_data;

        mptcpd_lm_destroy(_lm);
        mptcpd_lm_destroy(NULL);
}

int main(int argc, char *argv[])
{
        // Skip this test if the kernel is not MPTCP capable.
        tests_skip_if_no_mptcp();

        l_log_set_stderr();
        //l_debug_enable("*");

        l_test_init(&argc, &argv);

        l_test_add("create lm", test_create, NULL);

        /*
          Listen on the IPv4 and IPv6 loopback addresses since we need
          an address backed by a network interface so that the
          underlying bind() call can succeed.
        */
        struct ipv4_listen_case ipv4_cases[] = {
                INIT_IPV4_TEST_CASE(0x3456),
                INIT_IPV4_TEST_CASE(0x3457),
                INIT_IPV4_TEST_CASE(0x3456),  // Same port as above.
                INIT_IPV4_TEST_CASE(0)
        };

        struct ipv6_listen_case ipv6_cases[] = {
                INIT_IPV6_TEST_CASE(0x4567),
                INIT_IPV6_TEST_CASE(0x4578),
                INIT_IPV6_TEST_CASE(0x4567),  // Same port as above.
                INIT_IPV6_TEST_CASE(0)
        };

        for (size_t i = 0; i < L_ARRAY_SIZE(ipv4_cases); ++i) {
                char const *const desc = ipv4_cases[i].desc;

                l_test_add(desc, test_listen, &ipv4_cases[i].addr);
        }

        for (size_t i = 0; i < L_ARRAY_SIZE(ipv6_cases); ++i) {
                char const *const desc = ipv6_cases[i].desc;

                l_test_add(desc, test_listen, &ipv6_cases[i].addr);
        }

        // Test listen failure with "bad" (unbound) addresses.
        struct sockaddr_in ipv4_bad_cases[] = {
                {
                        .sin_family = AF_INET,
                        .sin_addr = { .s_addr = INADDR_ANY }
                },
                {
                        .sin_family = AF_INET,
                        .sin_addr = { .s_addr = INADDR_BROADCAST }
                }
        };

        for (size_t i = 0; i < L_ARRAY_SIZE(ipv4_bad_cases); ++i) {
                l_test_add("listen - bad IPv4 address",
                           test_listen_bad_address,
                           &ipv4_bad_cases[i]);
        }

        struct sockaddr_in6 const ipv6_bad_case = {
                .sin6_family = AF_INET6,
                .sin6_addr = IN6ADDR_ANY_INIT
        };

        l_test_add("listen - bad IPv6 address",
                   test_listen_bad_address,
                   &ipv6_bad_case);

        struct sockaddr const unix_sa = { .sa_family = AF_UNIX };
        l_test_add("listen - bad non-INET address",
                   test_listen_bad_address,
                   &unix_sa);

        l_test_add("listen - bad NULL address",
                   test_listen_bad_address,
                   NULL);

        // Listener close test cases.
        l_test_add("close  - IPv4", test_close, &ipv4_cases[0]);
        l_test_add("close  - IPv6", test_close, &ipv6_cases[0]);

        struct sockaddr_in const zero_port_case = {
                .sin_family = AF_INET,
                .sin_addr   = { .s_addr = htonl(INADDR_LOOPBACK) }
        };

        l_test_add("close  - IPv4 - zero port",
                   test_close,
                   &zero_port_case);

        l_test_add("close  - NULL", test_close, NULL);

        l_test_add("destroy lm",    test_destroy, NULL);

        return l_test_run();
}


/*
  Local Variables:
  c-file-style: "linux"
  End:
*/