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>
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.
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.
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.
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"
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 */
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);
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);
45 static bool channel_acc;
49 /* mutex for the common variable */
50 static pthread_mutex_t accept_lock = PTHREAD_MUTEX_INITIALIZER;
51 static pthread_mutex_t poll_lock = PTHREAD_MUTEX_INITIALIZER;
52 static pthread_mutex_t lock_receive = PTHREAD_MUTEX_INITIALIZER;
54 static pthread_cond_t accept_cond = PTHREAD_COND_INITIALIZER;
55 static pthread_cond_t poll_cond = PTHREAD_COND_INITIALIZER;
56 static pthread_cond_t receive_cond = PTHREAD_COND_INITIALIZER;
58 static black_box_state_t test_case_channel_set_accept_cb_01_state = {
59 .test_case_name = "test_case_channel_set_accept_cb_01",
61 static black_box_state_t test_case_channel_set_accept_cb_02_state = {
62 .test_case_name = "test_case_channel_set_accept_cb_02",
65 /* channel receive callback */
66 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
73 pthread_mutex_lock(&lock_receive);
75 assert(!pthread_cond_broadcast(&receive_cond));
76 pthread_mutex_unlock(&lock_receive);
81 /* channel reject callback */
82 static bool channel_reject(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
91 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
97 assert_int_equal(port, PORT);
99 pthread_mutex_lock(&accept_lock);
101 assert(!pthread_cond_broadcast(&accept_cond));
102 pthread_mutex_unlock(&accept_lock);
107 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
109 meshlink_set_channel_poll_cb(mesh, channel, NULL);
110 pthread_mutex_lock(&poll_lock);
112 assert(!pthread_cond_broadcast(&poll_cond));
113 pthread_mutex_unlock(&poll_lock);
116 /* Execute meshlink_channel_set_accept_cb Test Case # 1 - Valid case*/
117 static void test_case_set_channel_accept_cb_01(void **state) {
118 execute_test(test_steps_set_channel_accept_cb_01, state);
120 /* Test Steps for meshlink_channel_set_accept_cb Test Case # 1 - Valid case
123 1. Open NUT(Node Under Test) & bar meshes.
124 2. Set channel_accept callback for NUT's meshlink_set_channel_accept_cb API.
125 3. Export and Import nodes.
126 4. Open a channel with NUT from bar to invoke channel accept callback
127 5. Open a channel with bar from NUT to invoke channel accept callback
130 Opens a channel by invoking accept callback, when accept callback rejects the channel
131 it should invoke the other node's receive callback with length = 0 and no error.
133 static bool test_steps_set_channel_accept_cb_01(void) {
134 /* deleting the confbase if already exists */
135 struct timespec timeout = {0};
136 meshlink_destroy("acceptconf1");
137 meshlink_destroy("acceptconf2");
138 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
140 /* Create meshlink instances */
141 meshlink_handle_t *mesh1 = meshlink_open("acceptconf1", "nut", "chat", DEV_CLASS_STATIONARY);
142 assert(mesh1 != NULL);
143 meshlink_handle_t *mesh2 = meshlink_open("acceptconf2", "bar", "chat", DEV_CLASS_STATIONARY);
144 assert(mesh2 != NULL);
145 meshlink_set_log_cb(mesh1, MESHLINK_INFO, meshlink_callback_logger);
146 meshlink_set_log_cb(mesh2, MESHLINK_INFO, meshlink_callback_logger);
148 meshlink_set_channel_accept_cb(mesh2, channel_reject);
149 meshlink_set_channel_accept_cb(mesh1, channel_accept);
151 /* Export and Import on both sides */
152 char *exp1 = meshlink_export(mesh1);
153 assert(exp1 != NULL);
154 char *exp2 = meshlink_export(mesh2);
155 assert(exp2 != NULL);
156 assert(meshlink_import(mesh1, exp2));
157 assert(meshlink_import(mesh2, exp1));
159 assert(meshlink_start(mesh1));
160 assert(meshlink_start(mesh2));
163 meshlink_node_t *destination = meshlink_get_node(mesh2, "nut");
164 assert(destination != NULL);
166 /* Open channel for nut node from bar node which should be accepted */
169 meshlink_channel_t *channel2 = meshlink_channel_open(mesh2, destination, PORT, NULL, NULL, 0);
171 meshlink_set_channel_poll_cb(mesh2, channel2, poll_cb);
173 timeout.tv_sec = time(NULL) + 10;
174 pthread_mutex_lock(&poll_lock);
176 while(polled == false) {
177 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
180 pthread_mutex_unlock(&poll_lock);
182 timeout.tv_sec = time(NULL) + 10;
183 pthread_mutex_lock(&accept_lock);
185 while(channel_acc == false) {
186 assert(!pthread_cond_timedwait(&accept_cond, &accept_lock, &timeout));
189 pthread_mutex_unlock(&accept_lock);
191 /* Open channel for bar node from nut node which should be rejected */
195 destination = meshlink_get_node(mesh1, "bar");
196 assert(destination != NULL);
198 meshlink_channel_t *channel1 = meshlink_channel_open(mesh1, destination, PORT, channel_receive_cb, NULL, 0);
200 meshlink_set_channel_poll_cb(mesh1, channel1, poll_cb);
202 timeout.tv_sec = time(NULL) + 10;
203 pthread_mutex_lock(&poll_lock);
205 while(polled == false) {
206 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
209 pthread_mutex_unlock(&poll_lock);
211 timeout.tv_sec = time(NULL) + 10;
212 pthread_mutex_lock(&lock_receive);
214 while(rejected == false) {
215 assert(!pthread_cond_timedwait(&receive_cond, &lock_receive, &timeout));
218 pthread_mutex_unlock(&lock_receive);
220 /* closing channel, meshes and destroying confbase */
221 meshlink_close(mesh1);
222 meshlink_close(mesh2);
223 meshlink_destroy("acceptconf1");
224 meshlink_destroy("acceptconf2");
229 /* Execute meshlink_channel_set_accept_cb Test Case # 2 - Invalid case*/
230 static void test_case_set_channel_accept_cb_02(void **state) {
231 execute_test(test_steps_set_channel_accept_cb_02, state);
233 /* Test Steps for meshlink_channel_set_accept_cb Test Case # 2 - Invalid case
236 1. Passing NULL as mesh handle argument for channel accept callback.
239 meshlink_channel_set_accept_cb returning proper meshlink_errno.
241 static bool test_steps_set_channel_accept_cb_02(void) {
242 /* setting channel accept cb with NULL as mesh handle and valid callback */
243 meshlink_set_channel_accept_cb(NULL, channel_accept);
244 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
250 int test_meshlink_set_channel_accept_cb(void) {
251 const struct CMUnitTest blackbox_channel_set_accept_cb_tests[] = {
252 cmocka_unit_test_prestate_setup_teardown(test_case_set_channel_accept_cb_01, NULL, NULL,
253 (void *)&test_case_channel_set_accept_cb_01_state),
254 cmocka_unit_test_prestate_setup_teardown(test_case_set_channel_accept_cb_02, NULL, NULL,
255 (void *)&test_case_channel_set_accept_cb_02_state)
258 total_tests += sizeof(blackbox_channel_set_accept_cb_tests) / sizeof(blackbox_channel_set_accept_cb_tests[0]);
260 int failed = cmocka_run_group_tests(blackbox_channel_set_accept_cb_tests, NULL, NULL);