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;
46 static bool channel_rej;
50 /* mutex for the common variable */
51 static pthread_mutex_t accept_lock = PTHREAD_MUTEX_INITIALIZER;
52 static pthread_mutex_t reject_lock = PTHREAD_MUTEX_INITIALIZER;
53 static pthread_mutex_t poll_lock = PTHREAD_MUTEX_INITIALIZER;
54 static pthread_mutex_t lock_receive = PTHREAD_MUTEX_INITIALIZER;
56 static pthread_cond_t accept_cond = PTHREAD_COND_INITIALIZER;
57 static pthread_cond_t reject_cond = PTHREAD_COND_INITIALIZER;
58 static pthread_cond_t poll_cond = PTHREAD_COND_INITIALIZER;
59 static pthread_cond_t receive_cond = PTHREAD_COND_INITIALIZER;
61 static black_box_state_t test_case_channel_set_accept_cb_01_state = {
62 .test_case_name = "test_case_channel_set_accept_cb_01",
64 static black_box_state_t test_case_channel_set_accept_cb_02_state = {
65 .test_case_name = "test_case_channel_set_accept_cb_02",
68 /* channel receive callback */
69 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
72 pthread_mutex_lock(& lock_receive);
74 assert(!pthread_cond_broadcast(&receive_cond));
75 pthread_mutex_unlock(& lock_receive);
80 /* channel reject callback */
81 static bool channel_reject(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
90 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
93 char *data = (char *) dat;
94 assert_int_equal(port, PORT);
96 pthread_mutex_lock(&accept_lock);
98 assert(!pthread_cond_broadcast(&accept_cond));
99 pthread_mutex_unlock(&accept_lock);
104 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
106 meshlink_set_channel_poll_cb(mesh, channel, NULL);
107 pthread_mutex_lock(&poll_lock);
109 assert(!pthread_cond_broadcast(&poll_cond));
110 pthread_mutex_unlock(&poll_lock);
113 /* Execute meshlink_channel_set_accept_cb Test Case # 1 - Valid case*/
114 static void test_case_set_channel_accept_cb_01(void **state) {
115 execute_test(test_steps_set_channel_accept_cb_01, state);
117 /* Test Steps for meshlink_channel_set_accept_cb Test Case # 1 - Valid case
120 1. Open NUT(Node Under Test) & bar meshes.
121 2. Set channel_accept callback for NUT's meshlink_set_channel_accept_cb API.
122 3. Export and Import nodes.
123 4. Open a channel with NUT from bar to invoke channel accept callback
124 5. Open a channel with bar from NUT to invoke channel accept callback
127 Opens a channel by invoking accept callback, when accept callback rejects the channel
128 it should invoke the other node's receive callback with length = 0 and no error.
130 static bool test_steps_set_channel_accept_cb_01(void) {
131 /* deleting the confbase if already exists */
132 struct timespec timeout = {0};
133 meshlink_destroy("acceptconf1");
134 meshlink_destroy("acceptconf2");
135 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
137 /* Create meshlink instances */
138 meshlink_handle_t *mesh1 = meshlink_open("acceptconf1", "nut", "chat", DEV_CLASS_STATIONARY);
139 assert(mesh1 != NULL);
140 meshlink_handle_t *mesh2 = meshlink_open("acceptconf2", "bar", "chat", DEV_CLASS_STATIONARY);
141 assert(mesh2 != NULL);
142 meshlink_set_log_cb(mesh1, MESHLINK_INFO, meshlink_callback_logger);
143 meshlink_set_log_cb(mesh2, MESHLINK_INFO, meshlink_callback_logger);
145 meshlink_set_channel_accept_cb(mesh2, channel_reject);
146 meshlink_set_channel_accept_cb(mesh1, channel_accept);
148 /* Export and Import on both sides */
149 char *exp1 = meshlink_export(mesh1);
150 assert(exp1 != NULL);
151 char *exp2 = meshlink_export(mesh2);
152 assert(exp2 != NULL);
153 assert(meshlink_import(mesh1, exp2));
154 assert(meshlink_import(mesh2, exp1));
156 assert(meshlink_start(mesh1));
157 assert(meshlink_start(mesh2));
160 meshlink_node_t *destination = meshlink_get_node(mesh2, "nut");
161 assert(destination != NULL);
163 /* Open channel for nut node from bar node which should be accepted */
166 meshlink_channel_t *channel2 = meshlink_channel_open(mesh2, destination, PORT, NULL, NULL, 0);
168 meshlink_set_channel_poll_cb(mesh2, channel2, poll_cb);
170 timeout.tv_sec = time(NULL) + 10;
171 pthread_mutex_lock(&poll_lock);
173 while(polled == false) {
174 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
177 pthread_mutex_unlock(&poll_lock);
179 timeout.tv_sec = time(NULL) + 10;
180 pthread_mutex_lock(&accept_lock);
182 while(channel_acc == false) {
183 assert(!pthread_cond_timedwait(&accept_cond, &accept_lock, &timeout));
186 pthread_mutex_unlock(&accept_lock);
188 /* Open channel for bar node from nut node which should be rejected */
192 destination = meshlink_get_node(mesh1, "bar");
193 assert(destination != NULL);
195 meshlink_channel_t *channel1 = meshlink_channel_open(mesh1, destination, PORT, channel_receive_cb, NULL, 0);
197 meshlink_set_channel_poll_cb(mesh1, channel1, poll_cb);
199 timeout.tv_sec = time(NULL) + 10;
200 pthread_mutex_lock(&poll_lock);
202 while(polled == false) {
203 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
206 pthread_mutex_unlock(&poll_lock);
208 timeout.tv_sec = time(NULL) + 10;
209 pthread_mutex_lock(&lock_receive);
211 while(rejected == false) {
212 assert(!pthread_cond_timedwait(&receive_cond, &lock_receive, &timeout));
215 pthread_mutex_unlock(&lock_receive);
217 /* closing channel, meshes and destroying confbase */
218 meshlink_close(mesh1);
219 meshlink_close(mesh2);
220 meshlink_destroy("acceptconf1");
221 meshlink_destroy("acceptconf2");
226 /* Execute meshlink_channel_set_accept_cb Test Case # 2 - Invalid case*/
227 static void test_case_set_channel_accept_cb_02(void **state) {
228 execute_test(test_steps_set_channel_accept_cb_02, state);
230 /* Test Steps for meshlink_channel_set_accept_cb Test Case # 2 - Invalid case
233 1. Passing NULL as mesh handle argument for channel accept callback.
236 meshlink_channel_set_accept_cb returning proper meshlink_errno.
238 static bool test_steps_set_channel_accept_cb_02(void) {
239 /* setting channel accept cb with NULL as mesh handle and valid callback */
240 meshlink_set_channel_accept_cb(NULL, channel_accept);
241 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
247 int test_meshlink_set_channel_accept_cb(void) {
248 const struct CMUnitTest blackbox_channel_set_accept_cb_tests[] = {
249 cmocka_unit_test_prestate_setup_teardown(test_case_set_channel_accept_cb_01, NULL, NULL,
250 (void *)&test_case_channel_set_accept_cb_01_state),
251 cmocka_unit_test_prestate_setup_teardown(test_case_set_channel_accept_cb_02, NULL, NULL,
252 (void *)&test_case_channel_set_accept_cb_02_state)
255 total_tests += sizeof(blackbox_channel_set_accept_cb_tests) / sizeof(blackbox_channel_set_accept_cb_tests[0]);
257 int failed = cmocka_run_group_tests(blackbox_channel_set_accept_cb_tests, NULL, NULL);