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