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) {
83 pthread_mutex_lock(&reachable_lock);
85 assert(!pthread_cond_broadcast(&reachable_cond));
86 pthread_mutex_unlock(&reachable_lock);
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);
93 /* Test Steps for meshlink_channel_set_poll_cb Test Case # 1
97 2. Open channel of the NUT itself
100 Opens a channel and also invokes poll callback.
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);
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);
119 /* Export and Import on both sides */
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));
128 assert(meshlink_start(mesh1));
129 assert(meshlink_start(mesh2));
131 timeout.tv_sec = time(NULL) + 10;
132 pthread_mutex_lock(&reachable_lock);
134 while(reachable == false) {
135 assert(!pthread_cond_timedwait(&reachable_cond, &reachable_lock, &timeout));
138 pthread_mutex_unlock(&reachable_lock);
140 meshlink_node_t *destination = meshlink_get_node(mesh2, "nut");
141 assert(destination != NULL);
143 /* Open channel for nut node from bar node which should be accepted */
145 meshlink_channel_t *channel = meshlink_channel_open(mesh2, destination, PORT, NULL, NULL, 0);
147 meshlink_set_channel_poll_cb(mesh2, channel, poll_cb);
149 timeout.tv_sec = time(NULL) + 10;
150 pthread_mutex_lock(&poll_lock);
152 while(polled == false) {
153 assert(!pthread_cond_timedwait(&poll_cond, &poll_lock, &timeout));
156 pthread_mutex_unlock(&poll_lock);
158 /* closing channel, meshes and destroying confbase */
159 meshlink_close(mesh1);
160 meshlink_close(mesh2);
161 meshlink_destroy("pollconf1");
162 meshlink_destroy("pollconf2");
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);
171 /* Test Steps for meshlink_channel_set_poll_cb Test Case # 2
175 2. Open channel of the NUT itself
176 3. Pass NULL as mesh handle argument for meshlink_set_channel_poll_cb API
179 Reports error accordingly by returning NULL
181 static bool test_steps_channel_set_poll_cb_02(void) {
182 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
184 /* Create meshlink instance */
185 meshlink_handle_t *mesh_handle = meshlink_open("channelpollconf3", "nut", "node_sim", 1);
187 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
189 assert(meshlink_start(mesh_handle));
191 meshlink_node_t *node = meshlink_get_self(mesh_handle);
192 assert(node != NULL);
194 /* Opening channel */
195 meshlink_channel_t *channel = meshlink_channel_open(mesh_handle, node, PORT, NULL, NULL, 0);
196 assert(channel != NULL);
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);
202 meshlink_close(mesh_handle);
203 meshlink_destroy("channelpollconf3");
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);
211 /* Test Steps for meshlink_channel_set_poll_cb Test Case # 3
215 2. Open channel of the NUT itself
216 3. Pass NULL as channel handle argument for meshlink_set_channel_poll_cb API
219 Reports error accordingly by returning NULL
221 static bool test_steps_channel_set_poll_cb_03(void) {
222 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
224 /* Create meshlink instance */
225 meshlink_handle_t *mesh_handle = meshlink_open("channelpollconf4", "nut", "node_sim", 1);
227 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
229 assert(meshlink_start(mesh_handle));
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);
235 meshlink_close(mesh_handle);
236 meshlink_destroy("channelpollconf4");
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)
250 total_tests += sizeof(blackbox_channel_set_poll_cb_tests) / sizeof(blackbox_channel_set_poll_cb_tests[0]);
252 return cmocka_run_group_tests(blackbox_channel_set_poll_cb_tests, NULL, NULL);