]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_join.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_join.c
1 /*
2     test_cases_join.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_join.h"
26 #include "../common/containers.h"
27 #include "../common/test_step.h"
28 #include "../common/common_handlers.h"
29 #include <assert.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <setjmp.h>
34 #include <cmocka.h>
35 #include <pthread.h>
36
37 /* Modify this to change the logging level of Meshlink */
38 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
39
40 static void test_case_meshlink_join_01(void **state);
41 static bool test_meshlink_join_01(void);
42 static void test_case_meshlink_join_02(void **state);
43 static bool test_meshlink_join_02(void);
44 static void test_case_meshlink_join_03(void **state);
45 static bool test_meshlink_join_03(void);
46
47 /* State structure for join Test Case #1 */
48 static black_box_state_t test_case_join_01_state = {
49         .test_case_name = "test_case_join_01",
50 };
51
52 /* State structure for join Test Case #1 */
53 static black_box_state_t test_case_join_02_state = {
54         .test_case_name = "test_case_join_02",
55 };
56
57 /* State structure for join Test Case #1 */
58 static black_box_state_t test_case_join_03_state = {
59         .test_case_name = "test_case_join_03",
60 };
61
62 static bool join_status;
63
64 /* status callback */
65 static void status_callback(meshlink_handle_t *mesh, meshlink_node_t *source, bool reach) {
66         (void)mesh;
67
68         if(!strcmp(source->name, "relay")) {
69                 join_status = reach;
70         }
71 }
72
73 /* Execute join Test Case # 1 - valid case*/
74 static void test_case_meshlink_join_01(void **state) {
75         execute_test(test_meshlink_join_01, state);
76 }
77
78 /* Test Steps for meshlink_join Test Case # 1 - Valid case
79
80     Test Steps:
81     1. Generate invite in relay container and run 'relay' node
82     2. Run NUT
83     3. Join NUT with relay using invitation generated.
84
85     Expected Result:
86     NUT joins relay using the invitation generated.
87 */
88 static bool test_meshlink_join_01(void) {
89         assert(meshlink_destroy("join_conf.1"));
90         assert(meshlink_destroy("join_conf.2"));
91
92         // Create node instances
93         meshlink_handle_t *mesh1 = meshlink_open("join_conf.1", "nut", "test", DEV_CLASS_STATIONARY);
94         assert(mesh1 != NULL);
95         meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
96         meshlink_handle_t *mesh2 = meshlink_open("join_conf.2", "relay", "test", DEV_CLASS_STATIONARY);
97         assert(mesh2 != NULL);
98         meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
99
100         // Setting node status callback
101         meshlink_set_node_status_cb(mesh1, status_callback);
102
103         // Inviting nut
104         assert(meshlink_start(mesh2));
105         char *invitation = meshlink_invite(mesh2, NULL, "nut");
106         assert(invitation);
107
108         // Joining Node-Under-Test with relay
109         bool ret = meshlink_join(mesh1, invitation);
110         assert_int_equal(ret, true);
111         assert(meshlink_start(mesh1));
112         sleep(1);
113
114         assert_int_equal(join_status, true);
115
116         free(invitation);
117         meshlink_close(mesh1);
118         meshlink_close(mesh2);
119         assert(meshlink_destroy("join_conf.1"));
120         assert(meshlink_destroy("join_conf.2"));
121
122         return true;
123 }
124
125 /* Execute join Test Case # 2 - Invalid case*/
126 static void test_case_meshlink_join_02(void **state) {
127         execute_test(test_meshlink_join_02, state);
128 }
129
130 /* Test Steps for meshlink_join Test Case # 2 - Invalid case
131
132     Test Steps:
133     1. Call meshlink_join with NULL as mesh handler argument.
134
135     Expected Result:
136     report error accordingly when NULL is passed as mesh handle argument
137 */
138 static bool test_meshlink_join_02(void) {
139         assert(meshlink_destroy("join_conf.3"));
140
141         // Create node instances
142         meshlink_handle_t *mesh1 = meshlink_open("join_conf.3", "nut", "test", DEV_CLASS_STATIONARY);
143         assert(mesh1 != NULL);
144         meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
145
146         char *invitation = meshlink_invite(mesh1, NULL, "nodex");
147
148         /* meshlink_join called with NULL as mesh handle and with valid invitation */
149         bool ret = meshlink_join(NULL, invitation);
150         assert_int_equal(ret, false);
151
152         free(invitation);
153         meshlink_close(mesh1);
154         assert(meshlink_destroy("join_conf.3"));
155
156         return true;
157 }
158
159 /* Execute join Test Case # 3- Invalid case*/
160 static void test_case_meshlink_join_03(void **state) {
161         execute_test(test_meshlink_join_03, state);
162 }
163
164 /* Test Steps for meshlink_join Test Case # 3 - Invalid case
165
166     Test Steps:
167     1. Run NUT
168     1. Call meshlink_join with NULL as invitation argument.
169
170     Expected Result:
171     Report error accordingly when NULL is passed as invite argument
172 */
173 static bool test_meshlink_join_03(void) {
174         assert(meshlink_destroy("joinconf.4"));
175         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
176
177         /* Create meshlink instance */
178         mesh_handle = meshlink_open("joinconf.4", "nut", "node_sim", 1);
179         assert(mesh_handle);
180         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
181
182         /* Passing NULL as invitation to join API*/
183         bool ret  = meshlink_join(mesh_handle, NULL);
184         assert_int_equal(ret, false);
185
186         meshlink_close(mesh_handle);
187         assert(meshlink_destroy("joinconf.4"));
188         return true;
189 }
190
191 int test_meshlink_join(void) {
192         const struct CMUnitTest blackbox_join_tests[] = {
193                 cmocka_unit_test_prestate_setup_teardown(test_case_meshlink_join_01, NULL, NULL,
194                                 (void *)&test_case_join_01_state),
195                 cmocka_unit_test_prestate_setup_teardown(test_case_meshlink_join_02, NULL, NULL,
196                                 (void *)&test_case_join_02_state),
197                 cmocka_unit_test_prestate_setup_teardown(test_case_meshlink_join_03, NULL, NULL,
198                                 (void *)&test_case_join_03_state)
199         };
200         total_tests += sizeof(blackbox_join_tests) / sizeof(blackbox_join_tests[0]);
201
202         int failed = cmocka_run_group_tests(blackbox_join_tests, NULL, NULL);
203
204         return failed;
205 }
206