2 test_cases_sign.c -- Execution of specific meshlink black box test cases
3 Copyright (C) 2018 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
20 #include "execute_tests.h"
21 #include "test_cases_sign.h"
22 #include "../common/containers.h"
23 #include "../common/test_step.h"
24 #include "../common/common_handlers.h"
32 /* Modify this to change the logging level of Meshlink */
33 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
35 static void test_case_sign_01(void **state);
36 static bool test_sign_01(void);
37 static void test_case_sign_02(void **state);
38 static bool test_sign_02(void);
39 static void test_case_sign_03(void **state);
40 static bool test_sign_03(void);
41 static void test_case_sign_04(void **state);
42 static bool test_sign_04(void);
43 static void test_case_sign_05(void **state);
44 static bool test_sign_05(void);
45 static void test_case_sign_06(void **state);
46 static bool test_sign_06(void);
47 static void test_case_sign_07(void **state);
48 static bool test_sign_07(void);
50 /* State structure for sign API Test Case #1 */
51 static black_box_state_t test_case_sign_01_state = {
52 .test_case_name = "test_case_sign_01",
55 /* State structure for sign API Test Case #2 */
56 static black_box_state_t test_case_sign_02_state = {
57 .test_case_name = "test_case_sign_02",
60 /* State structure for sign API Test Case #3 */
61 static black_box_state_t test_case_sign_03_state = {
62 .test_case_name = "test_case_sign_03",
65 /* State structure for sign API Test Case #4 */
66 static black_box_state_t test_case_sign_04_state = {
67 .test_case_name = "test_case_sign_04",
70 /* State structure for sign API Test Case #5 */
71 static black_box_state_t test_case_sign_05_state = {
72 .test_case_name = "test_case_sign_05",
75 /* State structure for sign API Test Case #6 */
76 static black_box_state_t test_case_sign_06_state = {
77 .test_case_name = "test_case_sign_06",
80 /* State structure for sign API Test Case #7 */
81 static black_box_state_t test_case_sign_07_state = {
82 .test_case_name = "test_case_sign_07",
86 /* Execute sign_data Test Case # 1 - Valid case - sign a data successfully*/
87 static void test_case_sign_01(void **state) {
88 execute_test(test_sign_01, state);
91 /* Test Steps for meshlink_sign Test Case # 1 - Valid case
94 1. Run NUT(Node Under Test)
98 Signs data successfully
100 static bool test_sign_01(void) {
101 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
103 // Create meshlink instance
104 meshlink_handle_t *mesh_handle = meshlink_open("signconf", "nut", "node_sim", 1);
106 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
107 assert(meshlink_start(mesh_handle));
112 char sig[MESHLINK_SIGLEN];
113 size_t ssize = MESHLINK_SIGLEN;
114 bool ret = meshlink_sign(mesh_handle, data, strlen(data) + 1, sig, &ssize);
117 meshlink_close(mesh_handle);
118 meshlink_destroy("signconf");
123 /* Execute sign_data Test Case # 2 - Invalid case - meshlink_sign passing NULL as mesh handle argument*/
124 static void test_case_sign_02(void **state) {
125 execute_test(test_sign_02, state);
129 /* Test Steps for meshlink_sign Test Case # 2 - invalid case
132 1. meshlink_sign API called by passing NULL as mesh handle argument
135 API returns false hinting the error.
137 static bool test_sign_02(void) {
139 char sig[MESHLINK_SIGLEN];
140 size_t ssize = MESHLINK_SIGLEN;
142 // Calling meshlink_sign API
143 bool ret = meshlink_sign(NULL, data, strlen(data) + 1, sig, &ssize);
146 PRINT_TEST_CASE_MSG("meshlink_sign Successfully reported error on passing NULL as mesh_handle arg\n");
150 PRINT_TEST_CASE_MSG("meshlink_sign FAILED to report error on passing NULL as mesh_handle arg\n");
154 /* Execute sign_data Test Case # 3 - Invalid case - meshlink_sign passing data to be signed as NULL */
155 static void test_case_sign_03(void **state) {
156 execute_test(test_sign_03, state);
159 /* Test Steps for meshlink_sign Test Case # 3 - invalid case
162 1. Run NUT(Node Under Test)
163 2. meshlink_sign API called by passing NULL as data argument
164 that has to be signed.
167 API returns false hinting the error.
169 static bool test_sign_03(void) {
170 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
172 // Create meshlink instance
173 meshlink_handle_t *mesh_handle = meshlink_open("signconf", "nut", "node_sim", 1);
175 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
176 assert(meshlink_start(mesh_handle));
180 char sig[MESHLINK_SIGLEN];
181 size_t ssize = MESHLINK_SIGLEN;
182 bool ret = meshlink_sign(mesh_handle, NULL, strlen(data) + 1, sig, &ssize);
186 meshlink_close(mesh_handle);
187 meshlink_destroy("signconf");
190 PRINT_TEST_CASE_MSG("meshlink_sign Successfully reported error on passing NULL as data arg\n");
193 PRINT_TEST_CASE_MSG("meshlink_sign FAILED to report error on passing NULL as data arg\n");
198 /* Execute sign_data Test Case # 4 - Invalid case - meshlink_sign passing 0 as size of data
200 static void test_case_sign_04(void **state) {
201 execute_test(test_sign_04, state);
204 /* Test Steps for meshlink_sign Test Case # 3 - invalid case
207 1. Run NUT(Node Under Test)
208 2. meshlink_sign API called by passing 0 as size of data to be signed
211 API returns false hinting the error.
213 static bool test_sign_04(void) {
214 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
216 // Create meshlink instance
218 meshlink_handle_t *mesh_handle = meshlink_open("signconf", "nut", "node_sim", 1);
220 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
221 assert(meshlink_start(mesh_handle));
226 char sig[MESHLINK_SIGLEN];
227 size_t ssize = MESHLINK_SIGLEN;
228 bool ret = meshlink_sign(mesh_handle, data, 0, sig, &ssize);
232 meshlink_close(mesh_handle);
233 meshlink_destroy("signconf");
236 PRINT_TEST_CASE_MSG("meshlink_sign Successfully reported error on passing 0 as size of data arg\n");
240 PRINT_TEST_CASE_MSG("meshlink_sign FAILED to report error on passing 0 as size of data arg\n");
244 /* Execute sign_data Test Case # 5 - Invalid case - meshlink_sign passing NULL as
245 signature buffer argument*/
246 static void test_case_sign_05(void **state) {
247 execute_test(test_sign_05, state);
250 /* Test Steps for meshlink_sign Test Case # 5 - invalid case
253 1. Run NUT(Node Under Test)
254 2. meshlink_sign API called by passing NULL for signature buffer argument
257 API returns false hinting the error.
259 static bool test_sign_05(void) {
260 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
262 // Create meshlink instance
264 meshlink_handle_t *mesh_handle = meshlink_open("signconf", "nut", "node_sim", 1);
266 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
267 assert(meshlink_start(mesh_handle));
272 size_t ssize = MESHLINK_SIGLEN;
273 bool ret = meshlink_sign(mesh_handle, data, strlen(data) + 1, NULL, &ssize);
277 meshlink_close(mesh_handle);
278 meshlink_destroy("signconf");
281 PRINT_TEST_CASE_MSG("meshlink_sign Successfully reported error on passing NULL as sign arg\n");
285 PRINT_TEST_CASE_MSG("meshlink_sign FAILED to report error on passing NULL as sign arg\n");
289 /* Execute sign_data Test Case # 6 - Invalid case - meshlink_sign passing NULL for size of
290 signature argument */
291 static void test_case_sign_06(void **state) {
292 execute_test(test_sign_06, state);
295 /* Test Steps for meshlink_sign Test Case # 6 - invalid case
298 1. Run NUT(Node Under Test)
299 2. meshlink_sign API called by passing NULL for size of signature buffer argument
302 API returns false hinting the error.
304 static bool test_sign_06(void) {
305 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
307 // Create meshlink instance
308 meshlink_handle_t *mesh_handle = meshlink_open("signconf", "nut", "node_sim", 1);
310 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
311 assert(meshlink_start(mesh_handle));
316 char sig[MESHLINK_SIGLEN];
317 bool ret = meshlink_sign(mesh_handle, data, strlen(data) + 1, sig, NULL);
321 meshlink_close(mesh_handle);
322 meshlink_destroy("signconf");
325 PRINT_TEST_CASE_MSG("meshlink_sign Successfully reported error on passing NULL as signsize arg\n");
329 PRINT_TEST_CASE_MSG("meshlink_sign FAILED to report error on passing NULL as signsize arg\n");
333 /* Execute sign_data Test Case # 7 - Invalid case - meshlink_sign passing size of signature < MESHLINK_SIGLEN*/
334 static void test_case_sign_07(void **state) {
335 execute_test(test_sign_07, state);
338 /* Test Steps for meshlink_sign Test Case # 6 - invalid case
341 1. Run NUT(Node Under Test)
342 2. meshlink_sign API called by passing size of signature < MESHLINK_SIGLEN
345 API returns false hinting the error.
347 static bool test_sign_07(void) {
348 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
350 // Create meshlink instance
352 meshlink_handle_t *mesh_handle = meshlink_open("signconf", "nut", "node_sim", 1);
354 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
355 assert(meshlink_start(mesh_handle));
360 char sig[MESHLINK_SIGLEN];
361 size_t ssize = 5; //5 < MESHLINK_SIGLEN
362 bool ret = meshlink_sign(mesh_handle, data, strlen(data) + 1, sig, &ssize);
366 meshlink_stop(mesh_handle);
367 meshlink_close(mesh_handle);
368 meshlink_destroy("signconf");
371 PRINT_TEST_CASE_MSG("meshlink_sign Successfully reported error on passing signsize < MESHLINK_SIGLEN arg\n");
375 PRINT_TEST_CASE_MSG("meshlink_sign FAILED to report error on passing signsize < MESHLINK_SIGLEN arg\n");
380 int test_meshlink_sign(void) {
381 const struct CMUnitTest blackbox_sign_tests[] = {
382 cmocka_unit_test_prestate_setup_teardown(test_case_sign_01, NULL, NULL,
383 (void *)&test_case_sign_01_state),
384 cmocka_unit_test_prestate_setup_teardown(test_case_sign_02, NULL, NULL,
385 (void *)&test_case_sign_02_state),
386 cmocka_unit_test_prestate_setup_teardown(test_case_sign_03, NULL, NULL,
387 (void *)&test_case_sign_03_state),
388 cmocka_unit_test_prestate_setup_teardown(test_case_sign_04, NULL, NULL,
389 (void *)&test_case_sign_04_state),
390 cmocka_unit_test_prestate_setup_teardown(test_case_sign_05, NULL, NULL,
391 (void *)&test_case_sign_05_state),
392 cmocka_unit_test_prestate_setup_teardown(test_case_sign_06, NULL, NULL,
393 (void *)&test_case_sign_06_state),
394 cmocka_unit_test_prestate_setup_teardown(test_case_sign_07, NULL, NULL,
395 (void *)&test_case_sign_07_state)
397 total_tests += sizeof(blackbox_sign_tests) / sizeof(blackbox_sign_tests[0]);
399 return cmocka_run_group_tests(blackbox_sign_tests, NULL, NULL);