]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_random_port_bindings01.c
Fix compiler warnings in the test suites.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_random_port_bindings01.c
1 /*
2     test_cases_random_port_bindings01.c -- Execution of specific meshlink black box test cases
3     Copyright (C) 2018  Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 /* Modify this to change the logging level of Meshlink */
21 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
22
23 #include "execute_tests.h"
24 #include "test_cases_random_port_bindings01.h"
25 #include "../../../src/meshlink.h"
26 #include "../../../src/devtools.h"
27 #include "../common/containers.h"
28 #include "../common/test_step.h"
29 #include "../common/common_handlers.h"
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <setjmp.h>
33 #include <cmocka.h>
34 #include <assert.h>
35 #include <string.h>
36
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <netdb.h>
40 #include <netinet/in.h>
41 #include <sys/socket.h>
42 #include <sys/types.h>
43
44 static void test_case_mesh_random_port_bindings_01(void **state);
45 static bool test_steps_mesh_random_port_bindings_01(void);
46 static void test_case_mesh_random_port_bindings_02(void **state);
47 static bool test_steps_mesh_random_port_bindings_02(void);
48 static void test_case_mesh_random_port_bindings_03(void **state);
49 static bool test_steps_mesh_random_port_bindings_03(void);
50
51 /* State structure for meshlink_random_port_bindings Test Case #1 */
52 static black_box_state_t test_mesh_random_port_bindings_01_state = {
53         .test_case_name = "test_case_mesh_random_port_bindings_01",
54 };
55
56 /* State structure for meshlink_random_port_bindings Test Case #2 */
57 static black_box_state_t test_mesh_random_port_bindings_02_state = {
58         .test_case_name = "test_case_mesh_random_port_bindings_02",
59 };
60
61 /* State structure for meshlink_random_port_bindings Test Case #3 */
62 static black_box_state_t test_mesh_random_port_bindings_03_state = {
63         .test_case_name = "test_case_mesh_random_port_bindings_03",
64 };
65
66 static int sockfd = -1, ipv6_fd = -1;
67
68 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
69         (void) mesh;
70
71         static const char *levelstr[] = {
72                 [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
73                 [MESHLINK_INFO] = "\x1b[32mINFO",
74                 [MESHLINK_WARNING] = "\x1b[33mWARNING",
75                 [MESHLINK_ERROR] = "\x1b[31mERROR",
76                 [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
77         };
78         fprintf(stderr, "%s:\x1b[0m %s\n", levelstr[level], text);
79 }
80
81 static void occupy_port(int port) {
82         int ret_val;
83         int mode = 1;
84         struct sockaddr_in servaddr;
85         struct sockaddr_in6 ipv6addr;
86
87         sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
88         assert_int_not_equal(sockfd, -1);
89         memset(&servaddr, 0, sizeof(servaddr));
90
91         servaddr.sin_family = AF_INET;
92         servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
93         servaddr.sin_port = htons(port);
94
95         assert_int_equal(bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)), 0);
96
97         ipv6_fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
98         assert_int_not_equal(ipv6_fd, -1);
99
100         mode = 1;
101         setsockopt(ipv6_fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&mode, sizeof(mode));
102
103         memset(&ipv6addr, 0, sizeof(ipv6addr));
104
105         ipv6addr.sin6_family = AF_INET6;
106         ipv6addr.sin6_addr   = in6addr_any;
107         ipv6addr.sin6_port   = htons(port);
108
109         if((ret_val = bind(ipv6_fd, (const struct sockaddr *)&ipv6addr, sizeof(ipv6addr))) < 0) {
110                 fprintf(stderr, "Bind to ipv6 failed due to %s\n", strerror(errno));
111                 assert(false);
112         }
113
114         listen(ipv6_fd, 5);
115
116         return;
117 }
118
119 static void occupy_trybind_port(void) {
120         occupy_port(10000);
121         return;
122 }
123
124 /* Execute meshlink_random_port_bindings Test Case # 1*/
125 void test_case_mesh_random_port_bindings_01(void **state) {
126         execute_test(test_steps_mesh_random_port_bindings_01, state);
127 }
128
129 /* Test Steps for meshlink random port bindings Test Case # 1
130
131     Test Steps:
132     1. Open a node instance
133     2. Bind a Socket on port 10000
134     3. Call meshlink_set_port() with same port 10000
135
136     Expected Result:
137     The meshlink_set_port() API should fail and the Listening Port
138     of the instance should be unchanged.
139 */
140 bool test_steps_mesh_random_port_bindings_01(void) {
141         meshlink_handle_t *relay = NULL;
142         meshlink_destroy("relay_conf");
143
144         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
145
146         relay = meshlink_open("relay_conf", "relay", "test", DEV_CLASS_BACKBONE);
147         fprintf(stderr, "Got mesh handle %p\n", (void *)relay);
148         assert_non_null(relay);
149
150         meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message);
151         meshlink_enable_discovery(relay, false);
152
153         assert_true(meshlink_start(relay));
154
155         occupy_port(10000);
156
157         meshlink_stop(relay);
158         fprintf(stderr, "Meshlink stop returned\n");
159
160         assert_int_equal(meshlink_set_port(relay, 10000), false);
161         fprintf(stderr, "Meshlink set port returned\n");
162
163         close(sockfd);
164         close(ipv6_fd);
165
166         sockfd = -1;
167         ipv6_fd = -1;
168
169         meshlink_close(relay);
170         meshlink_destroy("relay_conf");
171
172         return true;
173 }
174
175 /* Execute meshlink_blacklist Test Case # 2*/
176 void test_case_mesh_random_port_bindings_02(void **state) {
177         execute_test(test_steps_mesh_random_port_bindings_02, state);
178 }
179
180 /* Test Steps for meshlink random port bindings Test Case # 2
181
182     Test Steps:
183     1. Open a node and start the instance.
184     2. Call meshlink_set_port() with port 10000
185     3. When try bind succeds block the port using devtool_trybind_probe() callback.
186
187     Expected Result:
188     The meshlink_set_port() API should fail.
189 */
190 bool test_steps_mesh_random_port_bindings_02(void) {
191         meshlink_handle_t *relay = NULL;
192         meshlink_destroy("relay_conf");
193
194         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
195
196         relay = meshlink_open("relay_conf", "relay", "test", DEV_CLASS_BACKBONE);
197         fprintf(stderr, "Got mesh handle %p\n", (void *)relay);
198         assert_non_null(relay);
199
200         meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message);
201         meshlink_enable_discovery(relay, false);
202
203         assert_true(meshlink_start(relay));
204
205         sleep(1);
206
207         devtool_trybind_probe = occupy_trybind_port;
208         meshlink_stop(relay);
209
210         assert_int_equal(meshlink_set_port(relay, 10000), false);
211
212         close(sockfd);
213         close(ipv6_fd);
214
215         sockfd = -1;
216         ipv6_fd = -1;
217
218         meshlink_close(relay);
219         meshlink_destroy("relay_conf");
220         return true;
221 }
222
223 /* Execute meshlink_blacklist Test Case # 3*/
224 void test_case_mesh_random_port_bindings_03(void **state) {
225         execute_test(test_steps_mesh_random_port_bindings_03, state);
226 }
227
228 /* Test Steps for meshlink random port bindings Test Case # 3
229
230     Test Steps:
231     1. Open a node and start the instance.
232     2. Retrieve the port number of current instance using meshlink_get_port().
233     3. Close the instance and try to occupy the meshlink instance port.
234     4. Start the instance again with same confdir.
235
236     Expected Result:
237     The meshlink instance should start with a new random port different to
238     previous port number.
239 */
240 bool test_steps_mesh_random_port_bindings_03(void) {
241         int port, new_port;
242         meshlink_handle_t *relay = NULL;
243         meshlink_destroy("relay_conf");
244
245         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
246
247         relay = meshlink_open("relay_conf", "relay", "test", DEV_CLASS_BACKBONE);
248         fprintf(stderr, "Got mesh handle %p\n", (void *)relay);
249         assert_non_null(relay);
250
251         meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message);
252         meshlink_enable_discovery(relay, false);
253
254         assert_true(meshlink_start(relay));
255         port = meshlink_get_port(relay);
256
257         meshlink_close(relay);
258
259         occupy_port(port);
260
261         relay = meshlink_open("relay_conf", "relay", "test", DEV_CLASS_BACKBONE);
262         fprintf(stderr, "Got mesh handle %p\n", (void *)relay);
263         assert_non_null(relay);
264
265         meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message);
266         meshlink_enable_discovery(relay, false);
267
268         assert_true(meshlink_start(relay));
269
270         new_port = meshlink_get_port(relay);
271
272         assert_int_not_equal(port, new_port);
273
274         close(sockfd);
275         close(ipv6_fd);
276
277         sockfd = -1;
278         ipv6_fd = -1;
279
280         meshlink_close(relay);
281         meshlink_destroy("relay_conf");
282         return true;
283 }
284
285 int test_meshlink_random_port_bindings01(void) {
286         const struct CMUnitTest blackbox_random_port_bindings_tests[] = {
287                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_random_port_bindings_01, NULL, NULL,
288                                 (void *)&test_mesh_random_port_bindings_01_state),
289                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_random_port_bindings_02, NULL, NULL,
290                                 (void *)&test_mesh_random_port_bindings_02_state),
291                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_random_port_bindings_03, NULL, NULL,
292                                 (void *)&test_mesh_random_port_bindings_03_state)
293         };
294
295         total_tests += sizeof(blackbox_random_port_bindings_tests) / sizeof(blackbox_random_port_bindings_tests[0]);
296
297         return cmocka_run_group_tests(blackbox_random_port_bindings_tests, NULL, NULL);
298 }