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