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 meshlink_handle_t *relay = NULL;
142 meshlink_destroy("relay_conf");
144 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
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);
150 meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message);
151 meshlink_enable_discovery(relay, false);
153 assert_true(meshlink_start(relay));
157 meshlink_stop(relay);
158 fprintf(stderr, "Meshlink stop returned\n");
160 assert_int_equal(meshlink_set_port(relay, 10000), false);
161 fprintf(stderr, "Meshlink set port returned\n");
169 meshlink_close(relay);
170 meshlink_destroy("relay_conf");
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);
180 /* Test Steps for meshlink random port bindings Test Case # 2
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.
188 The meshlink_set_port() API should fail.
190 bool test_steps_mesh_random_port_bindings_02(void) {
191 meshlink_handle_t *relay = NULL;
192 meshlink_destroy("relay_conf");
194 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
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);
200 meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message);
201 meshlink_enable_discovery(relay, false);
203 assert_true(meshlink_start(relay));
207 devtool_trybind_probe = occupy_trybind_port;
208 meshlink_stop(relay);
210 assert_int_equal(meshlink_set_port(relay, 10000), false);
218 meshlink_close(relay);
219 meshlink_destroy("relay_conf");
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);
228 /* Test Steps for meshlink random port bindings Test Case # 3
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.
237 The meshlink instance should start with a new random port different to
238 previous port number.
240 bool test_steps_mesh_random_port_bindings_03(void) {
242 meshlink_handle_t *relay = NULL;
243 meshlink_destroy("relay_conf");
245 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
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);
251 meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message);
252 meshlink_enable_discovery(relay, false);
254 assert_true(meshlink_start(relay));
255 port = meshlink_get_port(relay);
257 meshlink_close(relay);
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);
265 meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message);
266 meshlink_enable_discovery(relay, false);
268 assert_true(meshlink_start(relay));
270 new_port = meshlink_get_port(relay);
272 assert_int_not_equal(port, new_port);
280 meshlink_close(relay);
281 meshlink_destroy("relay_conf");
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)
295 total_tests += sizeof(blackbox_random_port_bindings_tests) / sizeof(blackbox_random_port_bindings_tests[0]);
297 return cmocka_run_group_tests(blackbox_random_port_bindings_tests, NULL, NULL);