]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_set_port.c
Add missing atomic test cases to the APIs that affects disk writes
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_set_port.c
1 /*
2     test_cases_set_port.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 #include "execute_tests.h"
25 #include "test_cases_destroy.h"
26 #include "../common/containers.h"
27 #include "../common/test_step.h"
28 #include "../common/common_handlers.h"
29 #include "../../utils.h"
30 #include "test_cases_set_port.h"
31 #include <assert.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <stdarg.h>
35 #include <setjmp.h>
36 #include <cmocka.h>
37 #include <signal.h>
38 #include <wait.h>
39 #include <linux/limits.h>
40
41 /* Modify this to change the logging level of Meshlink */
42 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
43
44 #define NUT                         "nut"
45 #define PEER                        "peer"
46 #define TEST_MESHLINK_SET_PORT      "test_set_port"
47 #define create_path(confbase, node_name, test_case_no)   assert(snprintf(confbase, sizeof(confbase), TEST_MESHLINK_SET_PORT "_%ld_%s_%02d", (long) getpid(), node_name, test_case_no) > 0)
48
49 static void test_case_set_port_01(void **state);
50 static bool test_set_port_01(void);
51 static void test_case_set_port_02(void **state);
52 static bool test_set_port_02(void);
53 static void test_case_set_port_03(void **state);
54 static bool test_set_port_03(void);
55 static void test_case_set_port_04(void **state);
56 static bool test_set_port_04(void);
57
58 /* State structure for set port API Test Case #1 */
59 static black_box_state_t test_case_set_port_01_state = {
60         .test_case_name = "test_case_set_port_01",
61 };
62 /* State structure for set port API Test Case #2 */
63 static black_box_state_t test_case_set_port_02_state = {
64         .test_case_name = "test_case_set_port_02",
65 };
66 /* State structure for set port API Test Case #3 */
67 static black_box_state_t test_case_set_port_03_state = {
68         .test_case_name = "test_case_set_port_03",
69 };
70 /* State structure for set port API Test Case #4 */
71 static black_box_state_t test_case_set_port_04_state = {
72         .test_case_name = "test_case_set_port_04",
73 };
74
75 static bool try_bind(int portno) {
76         int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
77         assert_int_not_equal(socket_fd, -1);
78
79         struct sockaddr_in sin;
80         socklen_t len = sizeof(sin);
81         bzero(&sin, len);
82
83         assert_int_not_equal(getsockname(socket_fd, (struct sockaddr *)&sin, &len), -1);
84         sin.sin_addr.s_addr = INADDR_ANY;
85         sin.sin_port = htons(portno);
86
87         errno = 0;
88         int bind_status = bind(socket_fd, (struct sockaddr *)&sin, len);
89
90         // Exempt EADDRINUSE error only
91
92         if(bind_status) {
93                 assert_int_equal(errno, EADDRINUSE);
94         }
95
96         assert_int_not_equal(close(socket_fd), -1);
97
98         return !bind_status;
99 }
100
101 static void wait_for_socket_free(int portno) {
102
103         // Wait upto 20 seconds and poll every second whether the port is freed or not
104
105         for(int i = 0; i < 20; i++) {
106                 if(try_bind(portno)) {
107                         return;
108                 } else {
109                         sleep(1);
110                 }
111         }
112
113         fail();
114 }
115
116 static int get_free_port(void) {
117
118         // Get a free port
119
120         int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
121         assert_int_not_equal(socket_fd, -1);
122
123         struct sockaddr_in sin;
124         socklen_t len = sizeof(sin);
125         bzero(&sin, len);
126
127         assert_int_not_equal(getsockname(socket_fd, (struct sockaddr *)&sin, &len), -1);
128         sin.sin_addr.s_addr = INADDR_ANY;
129         sin.sin_port = 0;
130
131         assert_int_not_equal(bind(socket_fd, (struct sockaddr *)&sin, len), -1);
132
133         assert_int_not_equal(getsockname(socket_fd, (struct sockaddr *)&sin, &len), -1);
134
135         assert_int_not_equal(close(socket_fd), -1);
136
137         return (int) sin.sin_port;
138 }
139
140
141 /* Execute meshlink_set_port Test Case # 1 - valid case*/
142 static void test_case_set_port_01(void **state) {
143         execute_test(test_set_port_01, state);
144 }
145 /* Test Steps for meshlink_set_port Test Case # 1 - Valid case
146
147     Test Steps:
148     1. Open NUT(Node Under Test)
149     2. Set Port for NUT
150
151     Expected Result:
152     Set the new port to the NUT.
153 */
154 static bool test_set_port_01(void) {
155         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
156
157         // Create meshlink instance
158
159         mesh_handle = meshlink_open("setportconf", "nut", "test", 1);
160         assert(mesh_handle);
161         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
162
163         // Get old port and set a new port number
164
165         int port;
166         port = meshlink_get_port(mesh_handle);
167         assert(port > 0);
168         bool ret = meshlink_set_port(mesh_handle, 8000);
169         port = meshlink_get_port(mesh_handle);
170
171         assert_int_equal(port, 8000);
172         assert_int_equal(ret, true);
173
174         // Clean up
175
176         meshlink_close(mesh_handle);
177         assert(meshlink_destroy("setportconf"));
178         return true;
179 }
180
181
182 /* Execute meshlink_set_port Test Case # 2 - Invalid arguments */
183 static void test_case_set_port_02(void **state) {
184         execute_test(test_set_port_02, state);
185 }
186
187 /* Test Steps for meshlink_set_port Test Case # 2 - functionality test
188
189     Test Steps:
190     1. Open and start NUT and then pass invalid arguments to the set port API
191
192     Expected Result:
193     Meshlink set port API should fail and error out when invalid arguments are passed
194 */
195 static bool test_set_port_02(void) {
196         char nut_confbase[PATH_MAX];
197         create_path(nut_confbase, NUT, 2);
198
199         // Create meshlink instance
200
201         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, log_cb);
202         meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_SET_PORT, DEV_CLASS_STATIONARY);
203         meshlink_set_log_cb(mesh, TEST_MESHLINK_LOG_LEVEL, log_cb);
204
205         // meshlink_set_port called using NULL as mesh handle
206
207         meshlink_errno = MESHLINK_OK;
208         assert_false(meshlink_set_port(NULL, 8000));
209         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
210
211         // Setting port after starting NUT
212         meshlink_errno = MESHLINK_OK;
213         assert_false(meshlink_set_port(mesh, -1));
214         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
215
216         meshlink_errno = MESHLINK_OK;
217         assert_false(meshlink_set_port(mesh, 70000));
218         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
219
220         assert_true(meshlink_start(mesh));
221         meshlink_errno = MESHLINK_OK;
222         assert_false(meshlink_set_port(mesh, 8000));
223         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
224
225         // Clean up
226
227         meshlink_close(mesh);
228         assert_true(meshlink_destroy(nut_confbase));
229         return true;
230 }
231
232 /* Execute meshlink_set_port Test Case # 3 - Synchronization testing */
233 static void test_case_set_port_03(void **state) {
234         execute_test(test_set_port_03, state);
235 }
236
237 static bool test_set_port_03(void) {
238         pid_t pid;
239         int pid_status;
240         char nut_confbase[PATH_MAX];
241         create_path(nut_confbase, NUT, 3);
242
243         int new_port = get_free_port();
244
245         // Fork a new process in which NUT opens it's instance, set's the new port and raises SIGINT to terminate.
246
247         pid = fork();
248         assert_int_not_equal(pid, -1);
249
250         if(!pid) {
251                 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
252                 meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_SET_PORT, DEV_CLASS_STATIONARY);
253                 assert(mesh);
254
255                 assert(meshlink_set_port(mesh, new_port));
256                 raise(SIGINT);
257         }
258
259         // Wait for child exit and verify which signal terminated it
260
261         assert_int_not_equal(waitpid(pid, &pid_status, 0), -1);
262         assert_int_equal(WIFSIGNALED(pid_status), true);
263         assert_int_equal(WTERMSIG(pid_status), SIGINT);
264
265         // Wait for the NUT's listening socket to be freed. (i.e, preventing meshlink from binding to a new port
266         // when NUT instance is reopened and the actual port is not freed due EADDRINUSE)
267
268         wait_for_socket_free(new_port);
269
270         // Reopen the NUT instance in the same test suite
271
272         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
273         meshlink_handle_t *mesh = meshlink_open(nut_confbase, NUT, TEST_MESHLINK_SET_PORT, DEV_CLASS_STATIONARY);
274         assert_non_null(mesh);
275
276         assert_false(try_bind(new_port));
277
278         // Validate the new port that's being set in the previous instance persists.
279
280         int get_port = meshlink_get_port(mesh);
281         assert_int_equal(get_port, new_port);
282
283         // Close the mesh instance and verify that the listening port is closed or not
284
285         meshlink_close(mesh);
286
287         wait_for_socket_free(new_port);
288
289         assert_true(meshlink_destroy(nut_confbase));
290         return true;
291 }
292
293
294 int test_meshlink_set_port(void) {
295         const struct CMUnitTest blackbox_set_port_tests[] = {
296                 cmocka_unit_test_prestate_setup_teardown(test_case_set_port_01, NULL, NULL,
297                                 (void *)&test_case_set_port_01_state),
298                 cmocka_unit_test_prestate_setup_teardown(test_case_set_port_02, NULL, NULL,
299                                 (void *)&test_case_set_port_02_state),
300                 cmocka_unit_test_prestate_setup_teardown(test_case_set_port_03, NULL, NULL,
301                                 (void *)&test_case_set_port_03_state)
302         };
303         total_tests += sizeof(blackbox_set_port_tests) / sizeof(blackbox_set_port_tests[0]);
304         return cmocka_run_group_tests(blackbox_set_port_tests, NULL, NULL);
305 }