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.
24 /* Modify this to change the logging level of Meshlink */
25 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
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"
44 #include <netinet/in.h>
45 #include <sys/socket.h>
46 #include <sys/types.h>
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);
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",
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",
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",
70 static int sockfd = -1, ipv6_fd = -1;
72 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
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",
82 fprintf(stderr, "%s:\x1b[0m %s\n", levelstr[level], text);
85 static void occupy_port(int port) {
88 struct sockaddr_in servaddr;
89 struct sockaddr_in6 ipv6addr;
91 sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
92 assert_int_not_equal(sockfd, -1);
93 memset(&servaddr, 0, sizeof(servaddr));
95 servaddr.sin_family = AF_INET;
96 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
97 servaddr.sin_port = htons(port);
99 assert_int_equal(bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)), 0);
101 ipv6_fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
102 assert_int_not_equal(ipv6_fd, -1);
105 setsockopt(ipv6_fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&mode, sizeof(mode));
107 memset(&ipv6addr, 0, sizeof(ipv6addr));
109 ipv6addr.sin6_family = AF_INET6;
110 ipv6addr.sin6_addr = in6addr_any;
111 ipv6addr.sin6_port = htons(port);
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));
123 static void occupy_trybind_port(void) {
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);
133 /* Test Steps for meshlink random port bindings Test Case # 1
136 1. Open a node instance
137 2. Bind a Socket on port 10000
138 3. Call meshlink_set_port() with same port 10000
141 The meshlink_set_port() API should fail and the Listening Port
142 of the instance should be unchanged.
144 bool test_steps_mesh_random_port_bindings_01(void) {
145 meshlink_handle_t *relay = NULL;
146 assert(meshlink_destroy("relay_conf"));
148 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
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);
154 meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message);
155 meshlink_enable_discovery(relay, false);
157 assert_true(meshlink_start(relay));
161 meshlink_stop(relay);
162 fprintf(stderr, "Meshlink stop returned\n");
164 assert_int_equal(meshlink_set_port(relay, 10000), false);
165 fprintf(stderr, "Meshlink set port returned\n");
173 meshlink_close(relay);
174 assert(meshlink_destroy("relay_conf"));
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);
184 /* Test Steps for meshlink random port bindings Test Case # 2
187 1. Open a node and start the instance.
188 2. Call meshlink_set_port() with port 10000
189 3. When try bind succeeds block the port using devtool_trybind_probe() callback.
192 The meshlink_set_port() API should fail.
194 bool test_steps_mesh_random_port_bindings_02(void) {
195 meshlink_handle_t *relay = NULL;
196 assert(meshlink_destroy("relay_conf"));
198 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
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);
204 meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message);
205 meshlink_enable_discovery(relay, false);
207 assert_true(meshlink_start(relay));
211 devtool_trybind_probe = occupy_trybind_port;
212 meshlink_stop(relay);
214 assert_int_equal(meshlink_set_port(relay, 10000), false);
222 meshlink_close(relay);
223 assert(meshlink_destroy("relay_conf"));
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);
232 /* Test Steps for meshlink random port bindings Test Case # 3
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.
241 The meshlink instance should start with a new random port different to
242 previous port number.
244 bool test_steps_mesh_random_port_bindings_03(void) {
246 meshlink_handle_t *relay = NULL;
247 assert(meshlink_destroy("relay_conf"));
249 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
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);
255 meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message);
256 meshlink_enable_discovery(relay, false);
258 assert_true(meshlink_start(relay));
259 port = meshlink_get_port(relay);
261 meshlink_close(relay);
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);
269 meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message);
270 meshlink_enable_discovery(relay, false);
272 assert_true(meshlink_start(relay));
274 new_port = meshlink_get_port(relay);
276 assert_int_not_equal(port, new_port);
284 meshlink_close(relay);
285 assert(meshlink_destroy("relay_conf"));
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)
299 total_tests += sizeof(blackbox_random_port_bindings_tests) / sizeof(blackbox_random_port_bindings_tests[0]);
301 return cmocka_run_group_tests(blackbox_random_port_bindings_tests, NULL, NULL);