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