2 test_cases_send.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.
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"
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);
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",
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",
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",
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",
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",
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",
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);
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;
80 if(!memcmp(data, "test", 5)) {
85 /* Test Steps for meshlink_send Test Case # 1
88 1. Open instance of foo node
89 2. Run and send data to itself
92 Node should receive data sent to itself
94 static bool test_steps_mesh_send_01(void) {
97 meshlink_handle_t *mesh = meshlink_open("send_conf", "foo", "test", DEV_CLASS_STATIONARY);
99 meshlink_set_receive_cb(mesh, receive);
100 assert(meshlink_start(mesh));
102 meshlink_node_t *dest_node = meshlink_get_self(mesh);
105 receive_data = false;
106 result = meshlink_send(mesh, dest_node, "test", 5);
107 assert_int_equal(result, true);
109 assert_int_equal(receive_data, true);
111 meshlink_close(mesh);
112 meshlink_destroy("send_conf");
116 /* Execute meshlink_send Test Case # 2
119 1. Open instance of foo node
120 2. meshlink_send with NULL as mesh handle
123 meshlink_send returns false because of NULL handle
125 static void test_case_mesh_send_02(void **state) {
126 execute_test(test_steps_mesh_send_02, state);
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);
135 assert(meshlink_start(mesh));
136 meshlink_node_t *dest_node = meshlink_get_self(mesh);
139 bool ret = meshlink_send(NULL, dest_node, "test", 5);
140 assert_int_equal(ret, false);
142 meshlink_close(mesh);
143 meshlink_destroy("send_conf");
147 /* Execute meshlink_send Test Case # 3
150 1. Open instance of foo node
151 2. meshlink_send with NULL as node handle
154 meshlink_send returns false because of NULL handle
156 static void test_case_mesh_send_03(void **state) {
157 execute_test(test_steps_mesh_send_03, state);
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);
166 assert(meshlink_start(mesh));
168 bool ret = meshlink_send(mesh, NULL, "test", 5);
169 assert_int_equal(ret, false);
171 meshlink_close(mesh);
172 meshlink_destroy("send_conf");
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)
186 total_tests += sizeof(blackbox_send_tests) / sizeof(blackbox_send_tests[0]);
188 return cmocka_run_group_tests(blackbox_send_tests, NULL, NULL);