]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_send.c
Add the blackbox container based test suite.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_send.c
1 /*
2     test_cases_send.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 #include "execute_tests.h"
21 #include "test_cases_send.h"
22 #include "../common/containers.h"
23 #include "../common/test_step.h"
24 #include "../common/common_handlers.h"
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <setjmp.h>
28 #include <cmocka.h>
29 #include <assert.h>
30 #include <string.h>
31
32 static void test_case_mesh_send_01(void **state);
33 static bool test_steps_mesh_send_01(void);
34 static void test_case_mesh_send_02(void **state);
35 static bool test_steps_mesh_send_02(void);
36 static void test_case_mesh_send_03(void **state);
37 static bool test_steps_mesh_send_03(void);
38
39 /* State structure for meshlink_send Test Case #1 */
40 static black_box_state_t test_mesh_send_01_state = {
41         .test_case_name = "test_case_mesh_send_01",
42 };
43
44 /* State structure for meshlink_send Test Case #2 */
45 static black_box_state_t test_mesh_send_02_state = {
46         .test_case_name = "test_case_mesh_send_02",
47 };
48
49 /* State structure for meshlink_send Test Case #3 */
50 static black_box_state_t test_mesh_send_03_state = {
51         .test_case_name = "test_case_mesh_send_03",
52 };
53
54 /* State structure for meshlink_send Test Case #4 */
55 static black_box_state_t test_mesh_send_04_state = {
56         .test_case_name = "test_case_mesh_send_04",
57 };
58
59 /* State structure for meshlink_send Test Case #5 */
60 static black_box_state_t test_mesh_send_05_state = {
61         .test_case_name = "test_case_mesh_send_05",
62 };
63
64 /* State structure for meshlink_send Test Case #6 */
65 static black_box_state_t test_mesh_send_06_state = {
66         .test_case_name = "test_case_mesh_send_06",
67 };
68
69 /* Execute meshlink_send Test Case # 1 */
70 static void test_case_mesh_send_01(void **state) {
71         execute_test(test_steps_mesh_send_01, state);
72 }
73
74 static bool receive_data = false;
75 static void receive(meshlink_handle_t *mesh, meshlink_node_t *dest_node, const void *data, size_t len) {
76         const char *msg = data;
77
78         assert(len);
79
80         if(!memcmp(data, "test", 5)) {
81                 receive_data = true;
82         }
83 }
84
85 /* Test Steps for meshlink_send Test Case # 1
86
87     Test Steps:
88     1. Open instance of foo node
89     2. Run and send data to itself
90
91     Expected Result:
92     Node should receive data sent to itself
93 */
94 static bool test_steps_mesh_send_01(void) {
95         bool result = false;
96
97         meshlink_handle_t *mesh = meshlink_open("send_conf", "foo", "test", DEV_CLASS_STATIONARY);
98         assert(mesh != NULL);
99         meshlink_set_receive_cb(mesh, receive);
100         assert(meshlink_start(mesh));
101         sleep(1);
102         meshlink_node_t *dest_node = meshlink_get_self(mesh);
103         assert(dest_node);
104
105         receive_data = false;
106         result = meshlink_send(mesh, dest_node, "test", 5);
107         assert_int_equal(result, true);
108         sleep(1);
109         assert_int_equal(receive_data, true);
110
111         meshlink_close(mesh);
112         meshlink_destroy("send_conf");
113         return result;
114 }
115
116 /* Execute meshlink_send Test Case # 2
117
118     Test Steps:
119     1. Open instance of foo node
120     2. meshlink_send with NULL as mesh handle
121
122     Expected Result:
123     meshlink_send returns false because of NULL handle
124 */
125 static void test_case_mesh_send_02(void **state) {
126         execute_test(test_steps_mesh_send_02, state);
127 }
128
129 /* Test Steps for meshlink_send Test Case # 2*/
130 static bool test_steps_mesh_send_02(void) {
131         meshlink_handle_t *mesh = meshlink_open("send_conf", "foo", "chat", DEV_CLASS_STATIONARY);
132         assert(mesh != NULL);
133         meshlink_set_receive_cb(mesh, receive);
134
135         assert(meshlink_start(mesh));
136         meshlink_node_t *dest_node = meshlink_get_self(mesh);
137         assert(dest_node);
138
139         bool ret = meshlink_send(NULL, dest_node, "test", 5);
140         assert_int_equal(ret, false);
141
142         meshlink_close(mesh);
143         meshlink_destroy("send_conf");
144         return true;
145 }
146
147 /* Execute meshlink_send Test Case # 3
148
149     Test Steps:
150     1. Open instance of foo node
151     2. meshlink_send with NULL as node handle
152
153     Expected Result:
154     meshlink_send returns false because of NULL handle
155 */
156 static void test_case_mesh_send_03(void **state) {
157         execute_test(test_steps_mesh_send_03, state);
158 }
159
160 /* Test Steps for meshlink_send Test Case # 3*/
161 static bool test_steps_mesh_send_03(void) {
162         meshlink_handle_t *mesh = meshlink_open("send_conf", "foo", "chat", DEV_CLASS_STATIONARY);
163         assert(mesh != NULL);
164         meshlink_set_receive_cb(mesh, receive);
165
166         assert(meshlink_start(mesh));
167
168         bool ret = meshlink_send(mesh, NULL, "test", 5);
169         assert_int_equal(ret, false);
170
171         meshlink_close(mesh);
172         meshlink_destroy("send_conf");
173         return true;
174 }
175
176 int test_meshlink_send(void) {
177         const struct CMUnitTest blackbox_send_tests[] = {
178                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_send_01, NULL, NULL,
179                                 (void *)&test_mesh_send_01_state),
180                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_send_02, NULL, NULL,
181                                 (void *)&test_mesh_send_02_state),
182                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_send_03, NULL, NULL,
183                                 (void *)&test_mesh_send_03_state)
184         };
185
186         total_tests += sizeof(blackbox_send_tests) / sizeof(blackbox_send_tests[0]);
187
188         return cmocka_run_group_tests(blackbox_send_tests, NULL, NULL);
189 }