]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_rec_cb.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_rec_cb.c
1 /*
2     test_cases_rec_cb.c -- Execution of specific meshlink black box test cases
3     Copyright (C) 2018  Guus Sliepen <guus@meshlink.io>
4
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.
9
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.
14
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.
18 */
19
20 #ifdef NDEBUG
21 #undef NDEBUG
22 #endif
23
24 #include "execute_tests.h"
25 #include "test_cases.h"
26 #include "../common/containers.h"
27 #include "../common/test_step.h"
28 #include "../common/common_handlers.h"
29 #include "test_cases_rec_cb.h"
30 #include <assert.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <setjmp.h>
35 #include <cmocka.h>
36 #include <pthread.h>
37
38
39 /* Modify this to change the logging level of Meshlink */
40 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
41
42 static void test_case_set_rec_cb_01(void **state);
43 static bool test_set_rec_cb_01(void);
44 static void test_case_set_rec_cb_02(void **state);
45 static bool test_set_rec_cb_02(void);
46 static void test_case_set_rec_cb_03(void **state);
47 static bool test_set_rec_cb_03(void);
48
49 /* Test Steps for meshlink_set_receive_cb Test Case #1 */
50 static black_box_state_t test_case_set_rec_cb_01_state = {
51         .test_case_name = "test_case_set_rec_cb_01",
52 };
53
54 /* Test Steps for meshlink_set_receive_cb Test Case #2 */
55 static black_box_state_t test_case_set_rec_cb_02_state = {
56         .test_case_name = "test_case_set_rec_cb_02",
57 };
58
59 /* Test Steps for meshlink_set_receive_cb Test Case #3 */
60 static black_box_state_t test_case_set_rec_cb_03_state = {
61         .test_case_name = "test_case_set_rec_cb_03",
62 };
63
64 static bool received;
65
66 /* mutex for the common variable */
67 pthread_mutex_t lock;
68
69 /* receive callback function */
70 static void rec_cb(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
71         (void)mesh;
72         (void)source;
73
74         assert(len);
75
76         pthread_mutex_lock(&lock);
77
78         if(len == 5 && !memcmp(data, "test", 5)) {
79                 received = true;
80         }
81
82         pthread_mutex_unlock(&lock);
83 }
84
85 /* Execute meshlink_set_receive_cb Test Case # 1 - Valid case */
86 static void test_case_set_rec_cb_01(void **state) {
87         execute_test(test_set_rec_cb_01, state);
88 }
89 /* Test Steps for meshlink_set_receive_cb Test Case # 1
90
91     Test Steps:
92     1. Open NUT
93     2. Set receive callback for the NUT
94     3. Echo NUT with some data.
95
96     Expected Result:
97     Receive callback should be invoked when NUT echoes or sends data for itself.
98 */
99 static bool test_set_rec_cb_01(void) {
100         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
101
102         /* Create meshlink instance */
103         meshlink_handle_t *mesh_handle = meshlink_open("set_receive_cb_conf", "nut", "test", 1);
104         assert(mesh_handle);
105         meshlink_set_receive_cb(mesh_handle, rec_cb);
106         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
107
108         assert(meshlink_start(mesh_handle));
109         sleep(1);
110
111         pthread_mutex_lock(&lock);
112         received = false;
113         pthread_mutex_unlock(&lock);
114         meshlink_node_t *node_handle = meshlink_get_self(mesh_handle);
115         assert(node_handle);
116         assert(meshlink_send(mesh_handle, node_handle, "test", 5));
117         sleep(1);
118
119         pthread_mutex_lock(&lock);
120         assert_int_equal(received, true);
121         pthread_mutex_unlock(&lock);
122
123         meshlink_close(mesh_handle);
124         assert(meshlink_destroy("set_receive_cb_conf"));
125         return true;
126 }
127
128
129 /* Execute meshlink_set_receive_cb Test Case # 2 - Invalid case */
130 static void test_case_set_rec_cb_02(void **state) {
131         execute_test(test_set_rec_cb_02, state);
132 }
133 /* Test Steps for meshlink_set_receive_cb Test Case # 2
134
135     Test Steps:
136     1. Call meshlink_set_receive_cb with NULL as mesh handle argument
137
138     Expected Result:
139     meshlink_set_receive_cb API reports proper error accordingly.
140 */
141 static bool test_set_rec_cb_02(void) {
142         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
143
144         // Setting receive callback with NULL as mesh handle
145         meshlink_set_receive_cb(NULL, rec_cb);
146         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
147
148         return true;
149 }
150
151 /* Execute meshlink_set_receive_cb Test Case # 3 - Functionality Test, Trying to set receive call back after
152       starting the mesh */
153 static void test_case_set_rec_cb_03(void **state) {
154         execute_test(test_set_rec_cb_03, state);
155 }
156 /* Test Steps for meshlink_set_receive_cb Test Case # 3
157
158     Test Steps:
159     1. Open NUT
160     2. Starting mesh
161     2. Set receive callback for the NUT
162     3. Echo NUT with some data.
163
164     Expected Result:
165     Receive callback can be invoked when NUT echoes or sends data for itself
166 */
167 static bool test_set_rec_cb_03(void) {
168         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
169
170         /* Create meshlink instance */
171         meshlink_handle_t *mesh_handle = meshlink_open("set_receive_cb_conf", "nut", "test", 1);
172         assert(mesh_handle);
173         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
174
175         assert(meshlink_start(mesh_handle));
176         sleep(1);
177         meshlink_set_receive_cb(mesh_handle, rec_cb);
178
179         pthread_mutex_lock(&lock);
180         received = false;
181         pthread_mutex_unlock(&lock);
182         meshlink_node_t *node_handle = meshlink_get_self(mesh_handle);
183         assert(node_handle);
184         assert(meshlink_send(mesh_handle, node_handle, "test", 5));
185         sleep(1);
186
187         pthread_mutex_lock(&lock);
188         assert_int_equal(received, true);
189         pthread_mutex_unlock(&lock);
190
191         meshlink_close(mesh_handle);
192         assert(meshlink_destroy("set_receive_cb_conf"));
193         return true;
194 }
195
196 int test_meshlink_set_receive_cb(void) {
197         const struct CMUnitTest blackbox_receive_tests[] = {
198                 cmocka_unit_test_prestate_setup_teardown(test_case_set_rec_cb_01, NULL, NULL,
199                                 (void *)&test_case_set_rec_cb_01_state),
200                 cmocka_unit_test_prestate_setup_teardown(test_case_set_rec_cb_02, NULL, NULL,
201                                 (void *)&test_case_set_rec_cb_02_state),
202                 cmocka_unit_test_prestate_setup_teardown(test_case_set_rec_cb_03, NULL, NULL,
203                                 (void *)&test_case_set_rec_cb_03_state)
204         };
205         total_tests += sizeof(blackbox_receive_tests) / sizeof(blackbox_receive_tests[0]);
206
207         assert(pthread_mutex_init(&lock, NULL) == 0);
208         int failed = cmocka_run_group_tests(blackbox_receive_tests, NULL, NULL);
209         assert(pthread_mutex_destroy(&lock) == 0);
210
211         return failed;
212 }