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