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