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