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>
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 "test_cases_channel_set_poll_cb.h"
21 #include "../common/containers.h"
22 #include "../common/test_step.h"
23 #include "../common/common_handlers.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_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);
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",
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",
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",
56 static bool reachable;
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;
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) {
73 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
75 meshlink_set_channel_poll_cb(mesh, channel, NULL);
76 pthread_mutex_lock(&poll_lock);
78 assert(!pthread_cond_broadcast(&poll_cond));
79 pthread_mutex_unlock(&poll_lock);
82 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *source, bool reach) {
90 pthread_mutex_lock(&reachable_lock);
92 assert(!pthread_cond_broadcast(&reachable_cond));
93 pthread_mutex_unlock(&reachable_lock);
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);
100 /* Test Steps for meshlink_channel_set_poll_cb Test Case # 1
104 2. Open channel of the NUT itself
107 Opens a channel and also invokes poll callback.
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);
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);
126 /* Export and Import on both sides */
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));
135 assert(meshlink_start(mesh1));
136 assert(meshlink_start(mesh2));
138 timeout.tv_sec = time(NULL) + 10;
139 pthread_mutex_lock(&reachable_lock);
141 while(reachable == false) {
142 assert(!pthread_cond_timedwait(&reachable_cond, &reachable_lock, &timeout));
145 pthread_mutex_unlock(&reachable_lock);
147 meshlink_node_t *destination = meshlink_get_node(mesh2, "nut");
148 assert(destination != NULL);
150 /* Open channel for nut node from bar node which should be accepted */
152 meshlink_channel_t *channel = meshlink_channel_open(mesh2, destination, PORT, NULL, NULL, 0);
154 meshlink_set_channel_poll_cb(mesh2, channel, poll_cb);
156 timeout.tv_sec = time(NULL) + 10;
157 pthread_mutex_lock(&poll_lock);
159 while(polled == false) {
160 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
163 pthread_mutex_unlock(&poll_lock);
165 /* closing channel, meshes and destroying confbase */
166 meshlink_close(mesh1);
167 meshlink_close(mesh2);
168 meshlink_destroy("pollconf1");
169 meshlink_destroy("pollconf2");
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);
178 /* Test Steps for meshlink_channel_set_poll_cb Test Case # 2
182 2. Open channel of the NUT itself
183 3. Pass NULL as mesh handle argument for meshlink_set_channel_poll_cb API
186 Reports error accordingly by returning NULL
188 static bool test_steps_channel_set_poll_cb_02(void) {
189 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
191 /* Create meshlink instance */
192 meshlink_handle_t *mesh_handle = meshlink_open("channelpollconf3", "nut", "node_sim", 1);
194 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
196 assert(meshlink_start(mesh_handle));
198 meshlink_node_t *node = meshlink_get_self(mesh_handle);
199 assert(node != NULL);
201 /* Opening channel */
202 meshlink_channel_t *channel = meshlink_channel_open(mesh_handle, node, PORT, NULL, NULL, 0);
203 assert(channel != NULL);
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);
209 meshlink_close(mesh_handle);
210 meshlink_destroy("channelpollconf3");
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);
218 /* Test Steps for meshlink_channel_set_poll_cb Test Case # 3
222 2. Open channel of the NUT itself
223 3. Pass NULL as channel handle argument for meshlink_set_channel_poll_cb API
226 Reports error accordingly by returning NULL
228 static bool test_steps_channel_set_poll_cb_03(void) {
229 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
231 /* Create meshlink instance */
232 meshlink_handle_t *mesh_handle = meshlink_open("channelpollconf4", "nut", "node_sim", 1);
234 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
236 assert(meshlink_start(mesh_handle));
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);
242 meshlink_close(mesh_handle);
243 meshlink_destroy("channelpollconf4");
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)
257 total_tests += sizeof(blackbox_channel_set_poll_cb_tests) / sizeof(blackbox_channel_set_poll_cb_tests[0]);
259 return cmocka_run_group_tests(blackbox_channel_set_poll_cb_tests, NULL, NULL);