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