2 test_cases_rec_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.h"
21 #include "../common/containers.h"
22 #include "../common/test_step.h"
23 #include "../common/common_handlers.h"
24 #include "test_cases_rec_cb.h"
34 /* Modify this to change the logging level of Meshlink */
35 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
37 static void test_case_set_rec_cb_01(void **state);
38 static bool test_set_rec_cb_01(void);
39 static void test_case_set_rec_cb_02(void **state);
40 static bool test_set_rec_cb_02(void);
41 static void test_case_set_rec_cb_03(void **state);
42 static bool test_set_rec_cb_03(void);
44 /* Test Steps for meshlink_set_receive_cb Test Case #1 */
45 static black_box_state_t test_case_set_rec_cb_01_state = {
46 .test_case_name = "test_case_set_rec_cb_01",
49 /* Test Steps for meshlink_set_receive_cb Test Case #2 */
50 static black_box_state_t test_case_set_rec_cb_02_state = {
51 .test_case_name = "test_case_set_rec_cb_02",
54 /* Test Steps for meshlink_set_receive_cb Test Case #3 */
55 static black_box_state_t test_case_set_rec_cb_03_state = {
56 .test_case_name = "test_case_set_rec_cb_03",
61 /* mutex for the common variable */
64 /* receive callback function */
65 static void rec_cb(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
71 pthread_mutex_lock(&lock);
73 if(len == 5 && !memcmp(data, "test", 5)) {
77 pthread_mutex_unlock(&lock);
80 /* Execute meshlink_set_receive_cb Test Case # 1 - Valid case */
81 static void test_case_set_rec_cb_01(void **state) {
82 execute_test(test_set_rec_cb_01, state);
84 /* Test Steps for meshlink_set_receive_cb Test Case # 1
88 2. Set receive callback for the NUT
89 3. Echo NUT with some data.
92 Receive callback should be invoked when NUT echoes or sends data for itself.
94 static bool test_set_rec_cb_01(void) {
95 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
97 /* Create meshlink instance */
98 meshlink_handle_t *mesh_handle = meshlink_open("set_receive_cb_conf", "nut", "test", 1);
100 meshlink_set_receive_cb(mesh_handle, rec_cb);
101 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
103 assert(meshlink_start(mesh_handle));
106 pthread_mutex_lock(&lock);
108 pthread_mutex_unlock(&lock);
109 meshlink_node_t *node_handle = meshlink_get_self(mesh_handle);
111 assert(meshlink_send(mesh_handle, node_handle, "test", 5));
114 pthread_mutex_lock(&lock);
115 assert_int_equal(received, true);
116 pthread_mutex_unlock(&lock);
118 meshlink_close(mesh_handle);
119 meshlink_destroy("set_receive_cb_conf");
124 /* Execute meshlink_set_receive_cb Test Case # 2 - Invalid case */
125 static void test_case_set_rec_cb_02(void **state) {
126 execute_test(test_set_rec_cb_02, state);
128 /* Test Steps for meshlink_set_receive_cb Test Case # 2
131 1. Call meshlink_set_receive_cb with NULL as mesh handle argument
134 meshlink_set_receive_cb API reports proper error accordingly.
136 static bool test_set_rec_cb_02(void) {
137 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
139 // Setting receive callback with NULL as mesh handle
140 meshlink_set_receive_cb(NULL, rec_cb);
141 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
146 /* Execute meshlink_set_receive_cb Test Case # 3 - Functionality Test, Trying to set receive call back after
148 static void test_case_set_rec_cb_03(void **state) {
149 execute_test(test_set_rec_cb_03, state);
151 /* Test Steps for meshlink_set_receive_cb Test Case # 3
156 2. Set receive callback for the NUT
157 3. Echo NUT with some data.
160 Receive callback can be invoked when NUT echoes or sends data for itself
162 static bool test_set_rec_cb_03(void) {
163 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
165 /* Create meshlink instance */
166 meshlink_handle_t *mesh_handle = meshlink_open("set_receive_cb_conf", "nut", "test", 1);
168 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
170 assert(meshlink_start(mesh_handle));
172 meshlink_set_receive_cb(mesh_handle, rec_cb);
174 pthread_mutex_lock(&lock);
176 pthread_mutex_unlock(&lock);
177 meshlink_node_t *node_handle = meshlink_get_self(mesh_handle);
179 assert(meshlink_send(mesh_handle, node_handle, "test", 5));
182 pthread_mutex_lock(&lock);
183 assert_int_equal(received, true);
184 pthread_mutex_unlock(&lock);
186 meshlink_close(mesh_handle);
187 meshlink_destroy("set_receive_cb_conf");
191 int test_meshlink_set_receive_cb(void) {
192 const struct CMUnitTest blackbox_receive_tests[] = {
193 cmocka_unit_test_prestate_setup_teardown(test_case_set_rec_cb_01, NULL, NULL,
194 (void *)&test_case_set_rec_cb_01_state),
195 cmocka_unit_test_prestate_setup_teardown(test_case_set_rec_cb_02, NULL, NULL,
196 (void *)&test_case_set_rec_cb_02_state),
197 cmocka_unit_test_prestate_setup_teardown(test_case_set_rec_cb_03, NULL, NULL,
198 (void *)&test_case_set_rec_cb_03_state)
200 total_tests += sizeof(blackbox_receive_tests) / sizeof(blackbox_receive_tests[0]);
202 assert(pthread_mutex_init(&lock, NULL) == 0);
203 int failed = cmocka_run_group_tests(blackbox_receive_tests, NULL, NULL);
204 assert(pthread_mutex_destroy(&lock) == 0);