2 test_cases_random_port_bindings01.c -- Execution of specific meshlink black box test cases
3 Copyright (C) 2018 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
20 /* Modify this to change the logging level of Meshlink */
21 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
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"
40 #include <netinet/in.h>
41 #include <sys/socket.h>
42 #include <sys/types.h>
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);
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",
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",
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",
66 static int sockfd = -1, ipv6_fd = -1;
68 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
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",
78 fprintf(stderr, "%s:\x1b[0m %s\n", levelstr[level], text);
81 static void occupy_port(int port) {
84 struct sockaddr_in servaddr;
85 struct sockaddr_in6 ipv6addr;
87 sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
88 assert_int_not_equal(sockfd, -1);
89 memset(&servaddr, 0, sizeof(servaddr));
91 servaddr.sin_family = AF_INET;
92 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
93 servaddr.sin_port = htons(port);
95 assert_int_equal(bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)), 0);
97 ipv6_fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
98 assert_int_not_equal(ipv6_fd, -1);
101 setsockopt(ipv6_fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&mode, sizeof(mode));
103 memset(&ipv6addr, 0, sizeof(ipv6addr));
105 ipv6addr.sin6_family = AF_INET6;
106 ipv6addr.sin6_addr = in6addr_any;
107 ipv6addr.sin6_port = htons(port);
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));
119 static void occupy_trybind_port(void) {
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);
129 /* Test Steps for meshlink random port bindings Test Case # 1
132 1. Open a node instance
133 2. Bind a Socket on port 10000
134 3. Call meshlink_set_port() with same port 10000
137 The meshlink_set_port() API should fail and the Listening Port
138 of the instance should be unchanged.
140 bool test_steps_mesh_random_port_bindings_01(void) {
141 struct sockaddr_in servaddr;
142 meshlink_handle_t *relay = NULL;
143 meshlink_destroy("relay_conf");
145 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
147 relay = meshlink_open("relay_conf", "relay", "test", DEV_CLASS_BACKBONE);
148 fprintf(stderr, "Got mesh handle %p\n", (void *)relay);
149 assert_non_null(relay);
151 meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message);
152 meshlink_enable_discovery(relay, false);
154 assert_true(meshlink_start(relay));
158 meshlink_stop(relay);
159 fprintf(stderr, "Meshlink stop returned\n");
161 assert_int_equal(meshlink_set_port(relay, 10000), false);
162 fprintf(stderr, "Meshlink set port returned\n");
170 meshlink_close(relay);
171 meshlink_destroy("relay_conf");
176 /* Execute meshlink_blacklist Test Case # 2*/
177 void test_case_mesh_random_port_bindings_02(void **state) {
178 execute_test(test_steps_mesh_random_port_bindings_02, state);
181 /* Test Steps for meshlink random port bindings Test Case # 2
184 1. Open a node and start the instance.
185 2. Call meshlink_set_port() with port 10000
186 3. When try bind succeds block the port using devtool_trybind_probe() callback.
189 The meshlink_set_port() API should fail.
191 bool test_steps_mesh_random_port_bindings_02(void) {
193 meshlink_handle_t *relay = NULL;
194 meshlink_destroy("relay_conf");
196 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
198 relay = meshlink_open("relay_conf", "relay", "test", DEV_CLASS_BACKBONE);
199 fprintf(stderr, "Got mesh handle %p\n", (void *)relay);
200 assert_non_null(relay);
202 meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message);
203 meshlink_enable_discovery(relay, false);
205 assert_true(meshlink_start(relay));
208 port = meshlink_get_port(relay);
210 devtool_trybind_probe = occupy_trybind_port;
211 meshlink_stop(relay);
213 assert_int_equal(meshlink_set_port(relay, 10000), false);
221 meshlink_close(relay);
222 meshlink_destroy("relay_conf");
226 /* Execute meshlink_blacklist Test Case # 3*/
227 void test_case_mesh_random_port_bindings_03(void **state) {
228 execute_test(test_steps_mesh_random_port_bindings_03, state);
231 /* Test Steps for meshlink random port bindings Test Case # 3
234 1. Open a node and start the instance.
235 2. Retrieve the port number of current instance using meshlink_get_port().
236 3. Close the instance and try to occupy the meshlink instance port.
237 4. Start the instance again with same confdir.
240 The meshlink instance should start with a new random port different to
241 previous port number.
243 bool test_steps_mesh_random_port_bindings_03(void) {
246 struct sockaddr_in servaddr;
247 struct sockaddr_in6 ipv6addr;
248 meshlink_handle_t *relay = NULL;
249 meshlink_destroy("relay_conf");
251 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
253 relay = meshlink_open("relay_conf", "relay", "test", DEV_CLASS_BACKBONE);
254 fprintf(stderr, "Got mesh handle %p\n", (void *)relay);
255 assert_non_null(relay);
257 meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message);
258 meshlink_enable_discovery(relay, false);
260 assert_true(meshlink_start(relay));
261 port = meshlink_get_port(relay);
263 meshlink_close(relay);
267 relay = meshlink_open("relay_conf", "relay", "test", DEV_CLASS_BACKBONE);
268 fprintf(stderr, "Got mesh handle %p\n", (void *)relay);
269 assert_non_null(relay);
271 meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message);
272 meshlink_enable_discovery(relay, false);
274 assert_true(meshlink_start(relay));
276 new_port = meshlink_get_port(relay);
278 assert_int_not_equal(port, new_port);
286 meshlink_close(relay);
287 meshlink_destroy("relay_conf");
291 int test_meshlink_random_port_bindings01(void) {
292 const struct CMUnitTest blackbox_random_port_bindings_tests[] = {
293 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_random_port_bindings_01, NULL, NULL,
294 (void *)&test_mesh_random_port_bindings_01_state),
295 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_random_port_bindings_02, NULL, NULL,
296 (void *)&test_mesh_random_port_bindings_02_state),
297 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_random_port_bindings_03, NULL, NULL,
298 (void *)&test_mesh_random_port_bindings_03_state)
301 total_tests += sizeof(blackbox_random_port_bindings_tests) / sizeof(blackbox_random_port_bindings_tests[0]);
303 return cmocka_run_group_tests(blackbox_random_port_bindings_tests, NULL, NULL);