]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_send.c
Fix compiler warnings in the test suites.
[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 /* Execute meshlink_send Test Case # 1 */
55 static void test_case_mesh_send_01(void **state) {
56         execute_test(test_steps_mesh_send_01, state);
57 }
58
59 static bool receive_data = false;
60 static void receive(meshlink_handle_t *mesh, meshlink_node_t *dest_node, const void *data, size_t len) {
61         (void)mesh;
62         (void)dest_node;
63
64         assert(len);
65
66         if(!memcmp(data, "test", 5)) {
67                 receive_data = true;
68         }
69 }
70
71 /* Test Steps for meshlink_send Test Case # 1
72
73     Test Steps:
74     1. Open instance of foo node
75     2. Run and send data to itself
76
77     Expected Result:
78     Node should receive data sent to itself
79 */
80 static bool test_steps_mesh_send_01(void) {
81         bool result = false;
82
83         meshlink_handle_t *mesh = meshlink_open("send_conf", "foo", "test", DEV_CLASS_STATIONARY);
84         assert(mesh != NULL);
85         meshlink_set_receive_cb(mesh, receive);
86         assert(meshlink_start(mesh));
87         sleep(1);
88         meshlink_node_t *dest_node = meshlink_get_self(mesh);
89         assert(dest_node);
90
91         receive_data = false;
92         result = meshlink_send(mesh, dest_node, "test", 5);
93         assert_int_equal(result, true);
94         sleep(1);
95         assert_int_equal(receive_data, true);
96
97         meshlink_close(mesh);
98         meshlink_destroy("send_conf");
99         return result;
100 }
101
102 /* Execute meshlink_send Test Case # 2
103
104     Test Steps:
105     1. Open instance of foo node
106     2. meshlink_send with NULL as mesh handle
107
108     Expected Result:
109     meshlink_send returns false because of NULL handle
110 */
111 static void test_case_mesh_send_02(void **state) {
112         execute_test(test_steps_mesh_send_02, state);
113 }
114
115 /* Test Steps for meshlink_send Test Case # 2*/
116 static bool test_steps_mesh_send_02(void) {
117         meshlink_handle_t *mesh = meshlink_open("send_conf", "foo", "chat", DEV_CLASS_STATIONARY);
118         assert(mesh != NULL);
119         meshlink_set_receive_cb(mesh, receive);
120
121         assert(meshlink_start(mesh));
122         meshlink_node_t *dest_node = meshlink_get_self(mesh);
123         assert(dest_node);
124
125         bool ret = meshlink_send(NULL, dest_node, "test", 5);
126         assert_int_equal(ret, false);
127
128         meshlink_close(mesh);
129         meshlink_destroy("send_conf");
130         return true;
131 }
132
133 /* Execute meshlink_send Test Case # 3
134
135     Test Steps:
136     1. Open instance of foo node
137     2. meshlink_send with NULL as node handle
138
139     Expected Result:
140     meshlink_send returns false because of NULL handle
141 */
142 static void test_case_mesh_send_03(void **state) {
143         execute_test(test_steps_mesh_send_03, state);
144 }
145
146 /* Test Steps for meshlink_send Test Case # 3*/
147 static bool test_steps_mesh_send_03(void) {
148         meshlink_handle_t *mesh = meshlink_open("send_conf", "foo", "chat", DEV_CLASS_STATIONARY);
149         assert(mesh != NULL);
150         meshlink_set_receive_cb(mesh, receive);
151
152         assert(meshlink_start(mesh));
153
154         bool ret = meshlink_send(mesh, NULL, "test", 5);
155         assert_int_equal(ret, false);
156
157         meshlink_close(mesh);
158         meshlink_destroy("send_conf");
159         return true;
160 }
161
162 int test_meshlink_send(void) {
163         const struct CMUnitTest blackbox_send_tests[] = {
164                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_send_01, NULL, NULL,
165                                 (void *)&test_mesh_send_01_state),
166                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_send_02, NULL, NULL,
167                                 (void *)&test_mesh_send_02_state),
168                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_send_03, NULL, NULL,
169                                 (void *)&test_mesh_send_03_state)
170         };
171
172         total_tests += sizeof(blackbox_send_tests) / sizeof(blackbox_send_tests[0]);
173
174         return cmocka_run_group_tests(blackbox_send_tests, NULL, NULL);
175 }