2 test_cases_channel_set_receive_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.
24 #include "execute_tests.h"
25 #include "../common/containers.h"
26 #include "../common/test_step.h"
27 #include "../common/common_handlers.h"
28 #include "test_cases_channel_set_receive_cb.h"
38 /* Modify this to change the logging level of Meshlink */
39 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
41 /* Modify this to change the port number */
44 /* Modify this to change the channel receive callback access buffer */
47 static void test_case_set_channel_receive_cb_01(void **state);
48 static bool test_steps_set_channel_receive_cb_01(void);
49 static void test_case_set_channel_receive_cb_02(void **state);
50 static bool test_steps_set_channel_receive_cb_02(void);
51 static void test_case_set_channel_receive_cb_03(void **state);
52 static bool test_steps_set_channel_receive_cb_03(void);
54 static bool rec_stat = false;
55 static bool accept_stat = false;
57 /* mutex for the receive callback common resources */
58 static pthread_mutex_t lock_accept = PTHREAD_MUTEX_INITIALIZER;
59 static pthread_mutex_t lock_receive = PTHREAD_MUTEX_INITIALIZER;
60 static pthread_cond_t accept_cond = PTHREAD_COND_INITIALIZER;
61 static pthread_cond_t receive_cond = PTHREAD_COND_INITIALIZER;
63 static black_box_state_t test_case_channel_set_receive_cb_01_state = {
64 .test_case_name = "test_case_channel_set_receive_cb_01",
67 static black_box_state_t test_case_channel_set_receive_cb_02_state = {
68 .test_case_name = "test_case_channel_set_receive_cb_02",
71 static black_box_state_t test_case_channel_set_receive_cb_03_state = {
72 .test_case_name = "test_case_channel_set_receive_cb_03",
75 /* channel receive callback */
76 static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
82 pthread_mutex_lock(& lock_receive);
84 assert(!pthread_cond_broadcast(&receive_cond));
85 pthread_mutex_unlock(& lock_receive);
88 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
93 meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
95 pthread_mutex_lock(& lock_accept);
97 assert(!pthread_cond_broadcast(&accept_cond));
98 pthread_mutex_unlock(& lock_accept);
103 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 assert(meshlink_channel_send(mesh, channel, "Hello", 5) >= 0);
110 /* Execute meshlink_channel_set_receive_cb Test Case # 1 */
111 static void test_case_set_channel_receive_cb_01(void **state) {
112 execute_test(test_steps_set_channel_receive_cb_01, state);
114 /* Test Steps for meshlink_channel_set_receive_cb Test Case # 1 - Valid case
117 1. Run NUT and Open channel for itself.
118 2. Set channel receive callback and send data.
121 Opens a channel by invoking channel receive callback when data sent to it.
123 static bool test_steps_set_channel_receive_cb_01(void) {
124 struct timespec timeout = {0};
125 assert(meshlink_destroy("channelreceiveconf"));
126 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
128 /* Create meshlink instance */
129 meshlink_handle_t *mesh_handle = meshlink_open("channelreceiveconf", "nut", "node_sim", 1);
130 assert(mesh_handle != NULL);
131 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
132 meshlink_set_channel_accept_cb(mesh_handle, accept_cb);
134 assert(meshlink_start(mesh_handle));
136 meshlink_node_t *node = meshlink_get_self(mesh_handle);
137 assert(node != NULL);
141 meshlink_channel_t *channel = meshlink_channel_open(mesh_handle, node, 8000, NULL, NULL, 0);
142 meshlink_set_channel_poll_cb(mesh_handle, channel, poll_cb);
144 timeout.tv_sec = time(NULL) + 20;
145 pthread_mutex_lock(& lock_accept);
147 while(accept_stat == false) {
148 assert(!pthread_cond_timedwait(&accept_cond, &lock_accept, &timeout));
151 pthread_mutex_unlock(& lock_accept);
153 timeout.tv_sec = time(NULL) + 20;
154 pthread_mutex_lock(& lock_receive);
156 while(rec_stat == false) {
157 assert(pthread_cond_timedwait(&receive_cond, &lock_receive, &timeout) == 0);
160 pthread_mutex_unlock(& lock_receive);
162 meshlink_close(mesh_handle);
163 assert(meshlink_destroy("channelreceiveconf"));
168 /* Execute meshlink_channel_set_receive_cb Test Case # 2 */
169 static void test_case_set_channel_receive_cb_02(void **state) {
170 execute_test(test_steps_set_channel_receive_cb_02, state);
172 /* Test Steps for meshlink_channel_set_receive_cb Test Case # 2 - Invalid case
175 1. Run NUT and Open channel for itself.
176 2. Set channel receive callback with NULL as mesh handle.
179 meshlink_channel_set_receive_cb returning proper meshlink_errno.
181 static bool test_steps_set_channel_receive_cb_02(void) {
182 assert(meshlink_destroy("channelreceiveconf"));
183 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
185 /* Create meshlink instance */
186 meshlink_handle_t *mesh_handle = meshlink_open("channelreceiveconf", "nut", "node_sim", 1);
187 assert(mesh_handle != NULL);
188 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
189 meshlink_set_channel_accept_cb(mesh_handle, accept_cb);
192 assert(meshlink_start(mesh_handle));
193 meshlink_node_t *node = meshlink_get_self(mesh_handle);
194 assert(node != NULL);
196 meshlink_channel_t *channel = meshlink_channel_open_ex(mesh_handle, node, 8000, NULL, NULL, 0, MESHLINK_CHANNEL_UDP);
197 assert(channel != NULL);
198 meshlink_set_channel_poll_cb(mesh_handle, channel, poll_cb);
200 /* Setting channel for NUT using meshlink_set_channel_receive_cb API with NULL as mesh handle */
201 meshlink_set_channel_receive_cb(NULL, channel, channel_receive_cb);
202 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
204 meshlink_close(mesh_handle);
205 assert(meshlink_destroy("channelreceiveconf"));
210 /* Execute meshlink_channel_set_receive_cb Test Case # 3 */
211 static void test_case_set_channel_receive_cb_03(void **state) {
212 execute_test(test_steps_set_channel_receive_cb_03, state);
214 /* Test Steps for meshlink_channel_set_receive_cb Test Case # 3 - Invalid case
217 1. Run NUT and Open channel for itself.
218 2. Set channel receive callback with NULL as channel handle.
221 meshlink_channel_set_receive_cb returning proper meshlink_errno.
223 static bool test_steps_set_channel_receive_cb_03(void) {
224 assert(meshlink_destroy("channelreceiveconf"));
225 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
227 /* Create meshlink instance */
228 meshlink_handle_t *mesh_handle = meshlink_open("channelreceiveconf", "nut", "node_sim", 1);
229 fprintf(stderr, "meshlink_open status: %s\n", meshlink_strerror(meshlink_errno));
230 assert(mesh_handle != NULL);
231 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
232 meshlink_set_channel_accept_cb(mesh_handle, accept_cb);
235 assert(meshlink_start(mesh_handle));
237 /* Setting channel for NUT using meshlink_set_channel_receive_cb API channel handle as NULL */
238 meshlink_set_channel_receive_cb(mesh_handle, NULL, channel_receive_cb);
240 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
241 meshlink_close(mesh_handle);
242 assert(meshlink_destroy("channelreceiveconf"));
247 int test_meshlink_set_channel_receive_cb(void) {
248 const struct CMUnitTest blackbox_channel_set_receive_cb_tests[] = {
249 cmocka_unit_test_prestate_setup_teardown(test_case_set_channel_receive_cb_01, NULL, NULL,
250 (void *)&test_case_channel_set_receive_cb_01_state),
251 cmocka_unit_test_prestate_setup_teardown(test_case_set_channel_receive_cb_02, NULL, NULL,
252 (void *)&test_case_channel_set_receive_cb_02_state),
253 cmocka_unit_test_prestate_setup_teardown(test_case_set_channel_receive_cb_03, NULL, NULL,
254 (void *)&test_case_channel_set_receive_cb_03_state)
256 total_tests += sizeof(blackbox_channel_set_receive_cb_tests) / sizeof(blackbox_channel_set_receive_cb_tests[0]);
258 int failed = cmocka_run_group_tests(blackbox_channel_set_receive_cb_tests, NULL, NULL);