]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_channel_set_accept_cb.c
Fix compiler warnings in the test suites.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_channel_set_accept_cb.c
1 /*
2     test_cases_channel_set_accept_cb.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 #include "execute_tests.h"
20 #include "../common/containers.h"
21 #include "../common/test_step.h"
22 #include "../common/common_handlers.h"
23 #include "test_cases_channel_set_accept_cb.h"
24 #include <assert.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <setjmp.h>
29 #include <cmocka.h>
30 #include <pthread.h>
31
32 /* Modify this to change the logging level of Meshlink */
33 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
34 /* Modify this to change the port number */
35 #define PORT 8000
36
37 static void test_case_set_channel_accept_cb_01(void **state);
38 static bool test_steps_set_channel_accept_cb_01(void);
39 static void test_case_set_channel_accept_cb_02(void **state);
40 static bool test_steps_set_channel_accept_cb_02(void);
41
42 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len);
43 static bool channel_reject(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len);
44
45 static bool channel_acc;
46 static bool polled;
47 static bool rejected;
48
49 /* mutex for the common variable */
50 static pthread_mutex_t accept_lock = PTHREAD_MUTEX_INITIALIZER;
51 static pthread_mutex_t poll_lock = PTHREAD_MUTEX_INITIALIZER;
52 static pthread_mutex_t lock_receive = PTHREAD_MUTEX_INITIALIZER;
53
54 static pthread_cond_t accept_cond = PTHREAD_COND_INITIALIZER;
55 static pthread_cond_t poll_cond = PTHREAD_COND_INITIALIZER;
56 static pthread_cond_t receive_cond = PTHREAD_COND_INITIALIZER;
57
58 static black_box_state_t test_case_channel_set_accept_cb_01_state = {
59         .test_case_name = "test_case_channel_set_accept_cb_01",
60 };
61 static black_box_state_t test_case_channel_set_accept_cb_02_state = {
62         .test_case_name = "test_case_channel_set_accept_cb_02",
63 };
64
65 /* channel receive callback */
66 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
67         (void)mesh;
68         (void)channel;
69         (void)dat;
70
71         if(!len) {
72                 if(!meshlink_errno) {
73                         pthread_mutex_lock(&lock_receive);
74                         rejected = true;
75                         assert(!pthread_cond_broadcast(&receive_cond));
76                         pthread_mutex_unlock(&lock_receive);
77                 }
78         }
79 }
80
81 /* channel reject callback */
82 static bool channel_reject(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
83         (void)mesh;
84         (void)channel;
85         (void)port;
86         (void)data;
87         (void)len;
88         return false;
89 }
90
91 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
92         (void)mesh;
93         (void)channel;
94         (void)dat;
95         (void)len;
96
97         assert_int_equal(port, PORT);
98
99         pthread_mutex_lock(&accept_lock);
100         channel_acc = true;
101         assert(!pthread_cond_broadcast(&accept_cond));
102         pthread_mutex_unlock(&accept_lock);
103
104         return true;
105 }
106
107 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
108         (void)len;
109         meshlink_set_channel_poll_cb(mesh, channel, NULL);
110         pthread_mutex_lock(&poll_lock);
111         polled = true;
112         assert(!pthread_cond_broadcast(&poll_cond));
113         pthread_mutex_unlock(&poll_lock);
114 }
115
116 /* Execute meshlink_channel_set_accept_cb Test Case # 1 - Valid case*/
117 static void test_case_set_channel_accept_cb_01(void **state) {
118         execute_test(test_steps_set_channel_accept_cb_01, state);
119 }
120 /* Test Steps for meshlink_channel_set_accept_cb Test Case # 1 - Valid case
121
122     Test Steps:
123     1. Open NUT(Node Under Test) & bar meshes.
124     2. Set channel_accept callback for NUT's meshlink_set_channel_accept_cb API.
125     3. Export and Import nodes.
126     4. Open a channel with NUT from bar to invoke channel accept callback
127     5. Open a channel with bar from NUT to invoke channel accept callback
128
129     Expected Result:
130     Opens a channel by invoking accept callback, when accept callback rejects the channel
131     it should invoke the other node's receive callback with length = 0 and no error.
132 */
133 static bool test_steps_set_channel_accept_cb_01(void) {
134         /* deleting the confbase if already exists */
135         struct timespec timeout = {0};
136         meshlink_destroy("acceptconf1");
137         meshlink_destroy("acceptconf2");
138         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
139
140         /* Create meshlink instances */
141         meshlink_handle_t *mesh1 = meshlink_open("acceptconf1", "nut", "chat", DEV_CLASS_STATIONARY);
142         assert(mesh1 != NULL);
143         meshlink_handle_t *mesh2 = meshlink_open("acceptconf2", "bar", "chat", DEV_CLASS_STATIONARY);
144         assert(mesh2 != NULL);
145         meshlink_set_log_cb(mesh1, MESHLINK_INFO, meshlink_callback_logger);
146         meshlink_set_log_cb(mesh2, MESHLINK_INFO, meshlink_callback_logger);
147
148         meshlink_set_channel_accept_cb(mesh2, channel_reject);
149         meshlink_set_channel_accept_cb(mesh1, channel_accept);
150
151         /* Export and Import on both sides */
152         char *exp1 = meshlink_export(mesh1);
153         assert(exp1 != NULL);
154         char *exp2 = meshlink_export(mesh2);
155         assert(exp2 != NULL);
156         assert(meshlink_import(mesh1, exp2));
157         assert(meshlink_import(mesh2, exp1));
158
159         assert(meshlink_start(mesh1));
160         assert(meshlink_start(mesh2));
161         sleep(1);
162
163         meshlink_node_t *destination = meshlink_get_node(mesh2, "nut");
164         assert(destination != NULL);
165
166         /* Open channel for nut node from bar node which should be accepted */
167         polled = false;
168         channel_acc = false;
169         meshlink_channel_t *channel2 = meshlink_channel_open(mesh2, destination, PORT, NULL, NULL, 0);
170         assert(channel2);
171         meshlink_set_channel_poll_cb(mesh2, channel2, poll_cb);
172
173         timeout.tv_sec = time(NULL) + 10;
174         pthread_mutex_lock(&poll_lock);
175
176         while(polled == false) {
177                 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
178         }
179
180         pthread_mutex_unlock(&poll_lock);
181
182         timeout.tv_sec = time(NULL) + 10;
183         pthread_mutex_lock(&accept_lock);
184
185         while(channel_acc == false) {
186                 assert(!pthread_cond_timedwait(&accept_cond, &accept_lock, &timeout));
187         }
188
189         pthread_mutex_unlock(&accept_lock);
190
191         /* Open channel for bar node from nut node which should be rejected */
192         polled = false;
193         rejected = false;
194         channel_acc = false;
195         destination = meshlink_get_node(mesh1, "bar");
196         assert(destination != NULL);
197
198         meshlink_channel_t *channel1 = meshlink_channel_open(mesh1, destination, PORT, channel_receive_cb, NULL, 0);
199         assert(channel1);
200         meshlink_set_channel_poll_cb(mesh1, channel1, poll_cb);
201
202         timeout.tv_sec = time(NULL) + 10;
203         pthread_mutex_lock(&poll_lock);
204
205         while(polled == false) {
206                 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
207         }
208
209         pthread_mutex_unlock(&poll_lock);
210
211         timeout.tv_sec = time(NULL) + 10;
212         pthread_mutex_lock(&lock_receive);
213
214         while(rejected == false) {
215                 assert(!pthread_cond_timedwait(&receive_cond, &lock_receive, &timeout));
216         }
217
218         pthread_mutex_unlock(&lock_receive);
219
220         /* closing channel, meshes and destroying confbase */
221         meshlink_close(mesh1);
222         meshlink_close(mesh2);
223         meshlink_destroy("acceptconf1");
224         meshlink_destroy("acceptconf2");
225
226         return true;
227 }
228
229 /* Execute meshlink_channel_set_accept_cb Test Case # 2 - Invalid case*/
230 static void test_case_set_channel_accept_cb_02(void **state) {
231         execute_test(test_steps_set_channel_accept_cb_02, state);
232 }
233 /* Test Steps for meshlink_channel_set_accept_cb Test Case # 2 - Invalid case
234
235     Test Steps:
236     1. Passing NULL as mesh handle argument for channel accept callback.
237
238     Expected Result:
239     meshlink_channel_set_accept_cb returning proper meshlink_errno.
240 */
241 static bool test_steps_set_channel_accept_cb_02(void) {
242         /* setting channel accept cb with NULL as mesh handle and valid callback */
243         meshlink_set_channel_accept_cb(NULL, channel_accept);
244         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
245
246         return true;
247 }
248
249
250 int test_meshlink_set_channel_accept_cb(void) {
251         const struct CMUnitTest blackbox_channel_set_accept_cb_tests[] = {
252                 cmocka_unit_test_prestate_setup_teardown(test_case_set_channel_accept_cb_01, NULL, NULL,
253                                 (void *)&test_case_channel_set_accept_cb_01_state),
254                 cmocka_unit_test_prestate_setup_teardown(test_case_set_channel_accept_cb_02, NULL, NULL,
255                                 (void *)&test_case_channel_set_accept_cb_02_state)
256         };
257
258         total_tests += sizeof(blackbox_channel_set_accept_cb_tests) / sizeof(blackbox_channel_set_accept_cb_tests[0]);
259
260         int failed = cmocka_run_group_tests(blackbox_channel_set_accept_cb_tests, NULL, NULL);
261
262         return failed;
263 }
264
265
266