]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_verify.c
Fix compiler warnings in the test suites.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_verify.c
1 /*
2     test_cases_verify.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_verify.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
32 /* Modify this to change the logging level of Meshlink */
33 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
34
35 static void test_case_verify_01(void **state);
36 static bool test_verify_01(void);
37 static void test_case_verify_02(void **state);
38 static bool test_verify_02(void);
39 static void test_case_verify_03(void **state);
40 static bool test_verify_03(void);
41 static void test_case_verify_04(void **state);
42 static bool test_verify_04(void);
43 static void test_case_verify_05(void **state);
44 static bool test_verify_05(void);
45 static void test_case_verify_06(void **state);
46 static bool test_verify_06(void);
47
48 /* State structure for verify API Test Case #1 */
49 static black_box_state_t test_case_verify_01_state = {
50         .test_case_name = "test_case_verify_01",
51 };
52
53 /* State structure for verify API Test Case #2 */
54 static black_box_state_t test_case_verify_02_state = {
55         .test_case_name = "test_case_verify_02",
56 };
57
58 /* State structure for verify API Test Case #3 */
59 static black_box_state_t test_case_verify_03_state = {
60         .test_case_name = "test_case_verify_03",
61 };
62
63 /* State structure for verify API Test Case #4 */
64 static black_box_state_t test_case_verify_04_state = {
65         .test_case_name = "test_case_verify_04",
66 };
67
68 /* State structure for verify API Test Case #5 */
69 static black_box_state_t test_case_verify_05_state = {
70         .test_case_name = "test_case_verify_05",
71 };
72
73 /* State structure for verify API Test Case #6 */
74 static black_box_state_t test_case_verify_06_state = {
75         .test_case_name = "test_case_verify_06",
76 };
77
78
79
80 /* Execute meshlink_verify Test Case # 1 - Valid case - verify a data successfully*/
81 void test_case_verify_01(void **state) {
82         execute_test(test_verify_01, state);
83 }
84
85 /* Test Steps for meshlink_sign Test Case # 1 - Valid case
86
87     Test Steps:
88     1. Run NUT(Node Under Test)
89     2. Sign data with meshlink_sign
90     3. Verify data with the sign buffer used while signing
91
92     Expected Result:
93     Verifies data successfully with the apt signature
94 */
95 bool test_verify_01(void) {
96         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
97         meshlink_handle_t *mesh_handle = meshlink_open("verifyconf", "nut", "node_sim", DEV_CLASS_BACKBONE);
98         assert(mesh_handle);
99         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
100         assert(meshlink_start(mesh_handle));
101
102         char *data = "Test";
103         char sig[MESHLINK_SIGLEN];
104         size_t ssize = MESHLINK_SIGLEN;
105         bool ret = meshlink_sign(mesh_handle, data, strlen(data) + 1, sig, &ssize);
106         assert(ret);
107
108         meshlink_node_t *source = meshlink_get_node(mesh_handle, "nut");
109         assert(source);
110         ret = meshlink_verify(mesh_handle, source, data, strlen(data) + 1, sig, ssize);
111         meshlink_close(mesh_handle);
112         meshlink_destroy("verifyconf");
113
114         if(!ret) {
115                 PRINT_TEST_CASE_MSG("meshlink_verify FAILED to verify data\n");
116                 return false;
117         }
118
119         PRINT_TEST_CASE_MSG("meshlink_verify Successfully verified data\n");
120         return true;
121 }
122
123
124 /* Execute verify_data Test Case # 2 - Invalid case - meshlink_verify passing NULL args*/
125 void test_case_verify_02(void **state) {
126         execute_test(test_verify_02, state);
127 }
128
129 /* Test Steps for meshlink_sign Test Case # 2 - Invalid case
130
131     Test Steps:
132     1. Run NUT(Node Under Test)
133     2. Sign data with meshlink_sign
134     3. Trying to pass NULL as mesh handle argument
135         and other arguments being valid
136
137     Expected Result:
138     Reports error accordingly by returning false
139 */
140 bool test_verify_02(void) {
141         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
142         meshlink_handle_t *mesh_handle = meshlink_open("verifyconf", "nut", "node_sim", DEV_CLASS_BACKBONE);
143         assert(mesh_handle);
144         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
145         assert(meshlink_start(mesh_handle));
146
147         char *data = "Test";
148         char sig[MESHLINK_SIGLEN];
149         size_t ssize = MESHLINK_SIGLEN;
150         bool sret = meshlink_sign(mesh_handle, data, strlen(data) + 1, sig, &ssize);
151         assert(sret);
152
153         meshlink_node_t *source = meshlink_get_node(mesh_handle, "nut");
154         assert(source != NULL);
155         bool ret = meshlink_verify(NULL, source, data, strlen(data) + 1, sig, ssize);
156         meshlink_close(mesh_handle);
157         meshlink_destroy("verifyconf");
158
159         if(!ret) {
160                 PRINT_TEST_CASE_MSG("meshlink_sign Successfully reported error on passing NULL as mesh_handle arg\n");
161                 return true;
162         }
163
164         PRINT_TEST_CASE_MSG("meshlink_sign FAILED to report error on passing NULL as mesh_handle arg\n");
165         return false;
166 }
167
168
169 /* Execute verify_data Test Case # 3 - Invalid case - meshlink_verify passing NULL args*/
170 void test_case_verify_03(void **state) {
171         execute_test(test_verify_03, state);
172 }
173
174 /* Test Steps for meshlink_sign Test Case # 3 - Invalid case
175
176     Test Steps:
177     1. Run NUT(Node Under Test)
178     2. Sign data with meshlink_sign
179     3. Trying to pass NULL as source node handle argument
180         and other arguments being valid
181
182     Expected Result:
183     Reports error accordingly by returning false
184 */
185 bool test_verify_03(void) {
186         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
187         meshlink_handle_t *mesh_handle = meshlink_open("verifyconf", "nut", "node_sim", DEV_CLASS_BACKBONE);
188         assert(mesh_handle);
189         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
190         assert(meshlink_start(mesh_handle));
191
192         char *data = "Test";
193         char sig[MESHLINK_SIGLEN];
194         size_t ssize = MESHLINK_SIGLEN;
195         bool ret = meshlink_sign(mesh_handle, data, strlen(data) + 1, sig, &ssize);
196         assert(ret);
197         ret = meshlink_verify(mesh_handle, NULL, data, strlen(data) + 1, sig, ssize);
198         meshlink_close(mesh_handle);
199         meshlink_destroy("verifyconf");
200
201         if(!ret) {
202                 PRINT_TEST_CASE_MSG("meshlink_verify successfully reported NULL as node_handle arg\n");
203                 return true;
204         }
205
206         PRINT_TEST_CASE_MSG("meshlink_verify FAILED to report NULL as node_handle arg\n");
207         return false;
208 }
209
210 /* Execute verify_data Test Case # 4 - Invalid case - meshlink_verify passing NULL args*/
211 void test_case_verify_04(void **state) {
212         execute_test(test_verify_04, state);
213 }
214
215 /* Test Steps for meshlink_sign Test Case # 4 - Invalid case
216
217     Test Steps:
218     1. Run NUT(Node Under Test)
219     2. Sign data with meshlink_sign
220     3. Trying to pass NULL as signed data argument
221         and other arguments being valid
222
223     Expected Result:
224     Reports error accordingly by returning false
225 */
226 bool test_verify_04(void) {
227         meshlink_destroy("verifyconf");
228         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
229         meshlink_handle_t *mesh_handle = meshlink_open("verifyconf", "nut", "node_sim", DEV_CLASS_BACKBONE);
230         assert(mesh_handle);
231         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
232         assert(meshlink_start(mesh_handle));
233
234         char *data = "Test";
235         char sig[MESHLINK_SIGLEN];
236         size_t ssize = MESHLINK_SIGLEN;
237         bool ret = meshlink_sign(mesh_handle, data, strlen(data) + 1, sig, &ssize);
238         assert(ret);
239         meshlink_node_t *source = meshlink_get_node(mesh_handle, "nut");
240         assert(source != NULL);
241         ret = meshlink_verify(mesh_handle, source, NULL, strlen(data) + 1, sig, ssize);
242         meshlink_stop(mesh_handle);
243         meshlink_close(mesh_handle);
244         meshlink_destroy("verifyconf");
245
246         if(!ret) {
247                 PRINT_TEST_CASE_MSG("meshlink_verify successfully reported NULL as data arg\n");
248                 return true;
249         }
250
251         PRINT_TEST_CASE_MSG("meshlink_verify FAILED to report NULL as data arg\n");
252         return false;
253 }
254
255
256 /* Execute verify_data Test Case # 5 - Invalid case - meshlink_verify passing NULL args*/
257 void test_case_verify_05(void **state) {
258         execute_test(test_verify_05, state);
259 }
260
261 /* Test Steps for meshlink_sign Test Case # 5 - Invalid case
262
263     Test Steps:
264     1. Run NUT(Node Under Test)
265     2. Sign data with meshlink_sign
266     3. Trying to pass NULL as signature buffer argument
267         and other arguments being valid
268
269     Expected Result:
270     Reports error accordingly by returning false
271 */
272 bool test_verify_05(void) {
273         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
274         meshlink_handle_t *mesh_handle = meshlink_open("verifyconf", "nut", "node_sim", 1);
275         assert(mesh_handle);
276         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
277         assert(meshlink_start(mesh_handle));
278
279         char *data = "Test";
280         char sig[MESHLINK_SIGLEN];
281         size_t ssize = MESHLINK_SIGLEN;
282         bool ret = meshlink_sign(mesh_handle, data, strlen(data) + 1, sig, &ssize);
283         assert(ret);
284         meshlink_node_t *source = meshlink_get_node(mesh_handle, "nut");
285         assert(source != NULL);
286
287         ret = meshlink_verify(mesh_handle, source, data, strlen(data) + 1, NULL, ssize);
288         meshlink_stop(mesh_handle);
289         meshlink_close(mesh_handle);
290         meshlink_destroy("verifyconf");
291
292         if(!ret) {
293                 PRINT_TEST_CASE_MSG("meshlink_verify successfully NULL as sign arg\n");
294                 return true;
295         }
296
297         PRINT_TEST_CASE_MSG("meshlink_verify FAILED to report NULL as sign arg\n");
298         return false;
299 }
300
301 /* Execute verify_data Test Case # 6 - Functionality test, when a wrong source node is mentioned to verify
302       the signed data */
303 void test_case_verify_06(void **state) {
304         execute_test(test_verify_06, state);
305 }
306
307 /* Test Steps for meshlink_verify Test Case # 6 - Functionality Test
308
309     Test Steps:
310     1. Run NUT(Node Under Test) and peer
311     2. Sign using peer as source node.
312     3. Verify with NUT but passing NUT as source node rather than
313         'peer' as source node
314
315     Expected Result:
316     API returns false when it detects the wrong source node
317 */
318 bool test_verify_06(void) {
319         /* deleting the confbase if already exists */
320         meshlink_destroy("verifyconf1");
321         meshlink_destroy("verifyconf2");
322         /* Set up logging for Meshlink */
323         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
324         meshlink_handle_t *mesh1 = meshlink_open("verifyconf1", "nut", "chat", DEV_CLASS_STATIONARY);
325         assert(mesh1);
326         meshlink_handle_t *mesh2 = meshlink_open("verifyconf2", "bar", "chat", DEV_CLASS_STATIONARY);
327         assert(mesh2);
328
329         char *exp1 = meshlink_export(mesh1);
330         assert(exp1 != NULL);
331         char *exp2 = meshlink_export(mesh2);
332         assert(exp2 != NULL);
333         assert(meshlink_import(mesh1, exp2));
334         assert(meshlink_import(mesh2, exp1));
335
336         /* signing done by peer node  */
337         char *data = "Test";
338         char sig[MESHLINK_SIGLEN];
339         size_t ssize = MESHLINK_SIGLEN;
340         bool ret = meshlink_sign(mesh2, data, strlen(data) + 1, sig, &ssize);
341         assert(ret);
342
343         meshlink_node_t *source_nut = meshlink_get_self(mesh1);
344         assert(source_nut);
345         ret = meshlink_verify(mesh_handle, source_nut, data, strlen(data) + 1, sig, ssize);
346         meshlink_close(mesh1);
347         meshlink_close(mesh2);
348         meshlink_destroy("verifyconf1");
349         meshlink_destroy("verifyconf2");
350
351         if(!ret) {
352                 PRINT_TEST_CASE_MSG("meshlink_verify successfully returned 'false' when a wrong source node used to verify the data\n");
353                 return true;
354         }
355
356         PRINT_TEST_CASE_MSG("meshlink_verify FAILED to report error when a wrong source is mentioned\n");
357         return false;
358 }
359
360
361 int test_meshlink_verify(void) {
362         const struct CMUnitTest blackbox_verify_tests[] = {
363                 cmocka_unit_test_prestate_setup_teardown(test_case_verify_01, NULL, NULL,
364                                 (void *)&test_case_verify_01_state),
365                 cmocka_unit_test_prestate_setup_teardown(test_case_verify_02, NULL, NULL,
366                                 (void *)&test_case_verify_02_state),
367                 cmocka_unit_test_prestate_setup_teardown(test_case_verify_03, NULL, NULL,
368                                 (void *)&test_case_verify_03_state),
369                 cmocka_unit_test_prestate_setup_teardown(test_case_verify_04, NULL, NULL,
370                                 (void *)&test_case_verify_04_state),
371                 cmocka_unit_test_prestate_setup_teardown(test_case_verify_05, NULL, NULL,
372                                 (void *)&test_case_verify_05_state),
373                 cmocka_unit_test_prestate_setup_teardown(test_case_verify_06, NULL, NULL,
374                                 (void *)&test_case_verify_06_state)
375         };
376
377         total_tests += sizeof(blackbox_verify_tests) / sizeof(blackbox_verify_tests[0]);
378
379         return cmocka_run_group_tests(blackbox_verify_tests, NULL, NULL);
380 }