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