]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_channel_set_poll_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_poll_cb.c
1 /*
2     test_cases_channel_set_poll_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 "test_cases_channel_set_poll_cb.h"
26 #include "../common/containers.h"
27 #include "../common/test_step.h"
28 #include "../common/common_handlers.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_channel_set_poll_cb_01(void **state);
43 static bool test_steps_channel_set_poll_cb_01(void);
44 static void test_case_channel_set_poll_cb_02(void **state);
45 static bool test_steps_channel_set_poll_cb_02(void);
46 static void test_case_channel_set_poll_cb_03(void **state);
47 static bool test_steps_channel_set_poll_cb_03(void);
48 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len);
49
50 static black_box_state_t test_case_channel_set_poll_cb_01_state = {
51         .test_case_name = "test_case_channel_set_poll_cb_01",
52 };
53 static black_box_state_t test_case_channel_set_poll_cb_02_state = {
54         .test_case_name = "test_case_channel_set_poll_cb_02",
55 };
56 static black_box_state_t test_case_channel_set_poll_cb_03_state = {
57         .test_case_name = "test_case_channel_set_poll_cb_03",
58 };
59
60 static bool polled;
61 static bool reachable;
62
63 static pthread_mutex_t poll_lock = PTHREAD_MUTEX_INITIALIZER;
64 static pthread_cond_t poll_cond = PTHREAD_COND_INITIALIZER;
65 static pthread_mutex_t reachable_lock = PTHREAD_MUTEX_INITIALIZER;
66 static pthread_cond_t reachable_cond = PTHREAD_COND_INITIALIZER;
67
68 /* channel accept callback */
69 static bool channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
70         (void)mesh;
71         (void)channel;
72         (void)port;
73         (void)data;
74         (void)len;
75         return false;
76 }
77
78 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
79         (void)len;
80         meshlink_set_channel_poll_cb(mesh, channel, NULL);
81         pthread_mutex_lock(&poll_lock);
82         polled = true;
83         assert(!pthread_cond_broadcast(&poll_cond));
84         pthread_mutex_unlock(&poll_lock);
85 }
86
87 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *source, bool reach) {
88         (void)mesh;
89         (void)source;
90
91         if(!reach) {
92                 return;
93         }
94
95         pthread_mutex_lock(&reachable_lock);
96         reachable = true;
97         assert(!pthread_cond_broadcast(&reachable_cond));
98         pthread_mutex_unlock(&reachable_lock);
99 }
100
101 /* Execute meshlink_channel_set_poll_cb Test Case # 1 */
102 static void test_case_channel_set_poll_cb_01(void **state) {
103         execute_test(test_steps_channel_set_poll_cb_01, state);
104 }
105 /* Test Steps for meshlink_channel_set_poll_cb Test Case # 1
106
107     Test Steps:
108     1. Run NUT
109     2. Open channel of the NUT itself
110
111     Expected Result:
112     Opens a channel and also invokes poll callback.
113 */
114 static bool test_steps_channel_set_poll_cb_01(void) {
115         /* deleting the confbase if already exists */
116         struct timespec timeout = {0};
117         assert(meshlink_destroy("pollconf1"));
118         assert(meshlink_destroy("pollconf2"));
119         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
120
121         /* Create meshlink instances */
122         meshlink_handle_t *mesh1 = meshlink_open("pollconf1", "nut", "chat", DEV_CLASS_STATIONARY);
123         assert(mesh1 != NULL);
124         meshlink_handle_t *mesh2 = meshlink_open("pollconf2", "bar", "chat", DEV_CLASS_STATIONARY);
125         assert(mesh2 != NULL);
126         meshlink_set_log_cb(mesh1, MESHLINK_INFO, meshlink_callback_logger);
127         meshlink_set_log_cb(mesh2, MESHLINK_INFO, meshlink_callback_logger);
128         meshlink_set_node_status_cb(mesh1, node_status_cb);
129         meshlink_set_channel_accept_cb(mesh1, channel_accept_cb);
130
131         /* Export and Import on both sides */
132         reachable = false;
133         char *exp1 = meshlink_export(mesh1);
134         assert(exp1 != NULL);
135         char *exp2 = meshlink_export(mesh2);
136         assert(exp2 != NULL);
137         assert(meshlink_import(mesh1, exp2));
138         assert(meshlink_import(mesh2, exp1));
139
140         assert(meshlink_start(mesh1));
141         assert(meshlink_start(mesh2));
142
143         timeout.tv_sec = time(NULL) + 10;
144         pthread_mutex_lock(&reachable_lock);
145
146         while(reachable == false) {
147                 assert(!pthread_cond_timedwait(&reachable_cond, &reachable_lock, &timeout));
148         }
149
150         pthread_mutex_unlock(&reachable_lock);
151
152         meshlink_node_t *destination = meshlink_get_node(mesh2, "nut");
153         assert(destination != NULL);
154
155         /* Open channel for nut node from bar node which should be accepted */
156         polled = false;
157         meshlink_channel_t *channel = meshlink_channel_open(mesh2, destination, PORT, NULL, NULL, 0);
158         assert(channel);
159         meshlink_set_channel_poll_cb(mesh2, channel, poll_cb);
160
161         timeout.tv_sec = time(NULL) + 10;
162         pthread_mutex_lock(&poll_lock);
163
164         while(polled == false) {
165                 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
166         }
167
168         pthread_mutex_unlock(&poll_lock);
169
170         /* closing channel, meshes and destroying confbase */
171         meshlink_close(mesh1);
172         meshlink_close(mesh2);
173         assert(meshlink_destroy("pollconf1"));
174         assert(meshlink_destroy("pollconf2"));
175
176         return true;
177 }
178
179 /* Execute meshlink_channel_set_poll_cb Test Case # 2 */
180 static void test_case_channel_set_poll_cb_02(void **state) {
181         execute_test(test_steps_channel_set_poll_cb_02, state);
182 }
183 /* Test Steps for meshlink_channel_set_poll_cb Test Case # 2
184
185     Test Steps:
186     1. Run NUT
187     2. Open channel of the NUT itself
188     3. Pass NULL as mesh handle argument for meshlink_set_channel_poll_cb API
189
190     Expected Result:
191     Reports error accordingly by returning NULL
192 */
193 static bool test_steps_channel_set_poll_cb_02(void) {
194         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
195
196         /* Create meshlink instance */
197         meshlink_handle_t *mesh_handle = meshlink_open("channelpollconf3", "nut", "node_sim", 1);
198         assert(mesh_handle);
199         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
200
201         assert(meshlink_start(mesh_handle));
202
203         meshlink_node_t *node = meshlink_get_self(mesh_handle);
204         assert(node != NULL);
205
206         /* Opening channel */
207         meshlink_channel_t *channel = meshlink_channel_open(mesh_handle, node, PORT, NULL, NULL, 0);
208         assert(channel != NULL);
209
210         /* Setting poll cb with NULL as mesh handler */
211         meshlink_set_channel_poll_cb(NULL, channel, poll_cb);
212         assert_int_not_equal(meshlink_errno, 0);
213
214         meshlink_close(mesh_handle);
215         assert(meshlink_destroy("channelpollconf3"));
216         return true;
217 }
218
219 /* Execute meshlink_channel_set_poll_cb Test Case # 3 */
220 static void test_case_channel_set_poll_cb_03(void **state) {
221         execute_test(test_steps_channel_set_poll_cb_03, state);
222 }
223 /* Test Steps for meshlink_channel_set_poll_cb Test Case # 3
224
225     Test Steps:
226     1. Run NUT
227     2. Open channel of the NUT itself
228     3. Pass NULL as channel handle argument for meshlink_set_channel_poll_cb API
229
230     Expected Result:
231     Reports error accordingly by returning NULL
232 */
233 static bool test_steps_channel_set_poll_cb_03(void) {
234         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
235
236         /* Create meshlink instance */
237         meshlink_handle_t *mesh_handle = meshlink_open("channelpollconf4", "nut", "node_sim", 1);
238         assert(mesh_handle);
239         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
240
241         assert(meshlink_start(mesh_handle));
242
243         /* Setting poll cb with NULL as channel handler */
244         meshlink_set_channel_poll_cb(mesh_handle, NULL, poll_cb);
245         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
246
247         meshlink_close(mesh_handle);
248         assert(meshlink_destroy("channelpollconf4"));
249         return true;
250 }
251
252
253 int test_meshlink_set_channel_poll_cb(void) {
254         const struct CMUnitTest blackbox_channel_set_poll_cb_tests[] = {
255                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_set_poll_cb_01, NULL, NULL,
256                                 (void *)&test_case_channel_set_poll_cb_01_state),
257                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_set_poll_cb_02, NULL, NULL,
258                                 (void *)&test_case_channel_set_poll_cb_02_state),
259                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_set_poll_cb_03, NULL, NULL,
260                                 (void *)&test_case_channel_set_poll_cb_03_state)
261         };
262         total_tests += sizeof(blackbox_channel_set_poll_cb_tests) / sizeof(blackbox_channel_set_poll_cb_tests[0]);
263
264         return cmocka_run_group_tests(blackbox_channel_set_poll_cb_tests, NULL, NULL);
265 }