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