]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_get_fingerprint.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_get_fingerprint.c
1 /*
2     test_cases_get_fingerprint.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.h"
26 #include "../common/containers.h"
27 #include "../common/test_step.h"
28 #include "../common/common_handlers.h"
29 #include "test_cases_get_fingerprint.h"
30 #include <assert.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <setjmp.h>
35 #include <cmocka.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_get_fingerprint_cb_01(void **state);
41 static bool test_get_fingerprint_cb_01(void);
42 static void test_case_get_fingerprint_cb_02(void **state);
43 static bool test_get_fingerprint_cb_02(void);
44 static void test_case_get_fingerprint_cb_03(void **state);
45 static bool test_get_fingerprint_cb_03(void);
46
47 /* State structure for get_fingerprint Test Case #1 */
48 static black_box_state_t test_case_get_fingerprint_cb_01_state = {
49         .test_case_name = "test_case_get_fingerprint_cb_01",
50 };
51 /* State structure for get_fingerprint Test Case #2 */
52 static black_box_state_t test_case_get_fingerprint_cb_02_state = {
53         .test_case_name = "test_case_get_fingerprint_cb_02",
54 };
55 /* State structure for get_fingerprint Test Case #3 */
56 static black_box_state_t test_case_get_fingerprint_cb_03_state = {
57         .test_case_name = "test_case_get_fingerprint_cb_03",
58 };
59
60 /* Execute get_fingerprint Test Case # 1 - Valid Case of obtaing publickey of NUT */
61 static void test_case_get_fingerprint_cb_01(void **state) {
62         execute_test(test_get_fingerprint_cb_01, state);
63 }
64 /* Test Steps for get_fingerprint Test Case # 1 - Valid case
65
66     Test Steps:
67     1. Run NUT(Node Under Test)
68     2. Get node handle for ourself(for NUT) and obtain fingerprint
69
70     Expected Result:
71     Obtain fingerprint of NUT successfully.
72 */
73 static bool test_get_fingerprint_cb_01(void) {
74         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
75
76         /* Create meshlink instance */
77         meshlink_handle_t *mesh_handle = meshlink_open("getfingerprintconf", "nut", "test", 1);
78         assert(mesh_handle);
79         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
80
81         meshlink_node_t *node = meshlink_get_self(mesh_handle);
82         assert(node != NULL);
83
84         char *fp = meshlink_get_fingerprint(mesh_handle, node);
85         assert_int_not_equal(fp, NULL);
86
87         meshlink_close(mesh_handle);
88         assert(meshlink_destroy("getfingerprintconf"));
89
90         return true;
91 }
92
93 /* Execute get_fingerprint Test Case # 2 - Invalid Case - trying t0 obtain publickey of a node in a
94    mesh by passing NULL as mesh handle argument*/
95 static void test_case_get_fingerprint_cb_02(void **state) {
96         execute_test(test_get_fingerprint_cb_02, state);
97 }
98
99 /* Test Steps for get_fingerprint Test Case # 2 - Invalid case
100
101     Test Steps:
102     1. Run NUT(Node Under Test)
103     2. Get node handle for ourself(for NUT)
104     3. Obtain fingerprint by passing NULL as mesh handle
105
106     Expected Result:
107     Return NULL by reporting error successfully.
108 */
109 static bool test_get_fingerprint_cb_02(void) {
110         /* Set up logging for Meshlink */
111         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
112
113         /* Create meshlink instance */
114         PRINT_TEST_CASE_MSG("Opening NUT\n");
115         meshlink_handle_t *mesh_handle = meshlink_open("getfingerprintconf", "nut", "test", 1);
116         assert(mesh_handle);
117
118         /* Set up logging for Meshlink with the newly acquired Mesh Handle */
119         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
120         /* Getting node handle for itself */
121         meshlink_node_t *node = meshlink_get_self(mesh_handle);
122         assert(node != NULL);
123
124         /* passing NULL as mesh handle for meshlink_get_fingerprint API */
125         char *fp = meshlink_get_fingerprint(NULL, node);
126         assert_int_equal(fp, NULL);
127
128         meshlink_close(mesh_handle);
129         assert(meshlink_destroy("getfingerprintconf"));
130
131         return true;
132 }
133
134 /* Execute get_fingerprint Test Case # 3 - Invalid Case - trying t0 obtain publickey of a node in a
135    mesh by passing NULL as node handle argument */
136 static void test_case_get_fingerprint_cb_03(void **state) {
137         execute_test(test_get_fingerprint_cb_03, state);
138 }
139 /* Test Steps for get_fingerprint Test Case # 3 - Invalid case
140
141     Test Steps:
142     1. Run NUT(Node Under Test)
143     2. Get node handle for ourself(for NUT)
144     3. Obtain fingerprint by passing NULL as node handle
145
146     Expected Result:
147     Return NULL by reporting error successfully.
148 */
149 static bool test_get_fingerprint_cb_03(void) {
150         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
151
152         /* Create meshlink instance */
153         meshlink_handle_t *mesh_handle = meshlink_open("getfingerprintconf", "nut", "test", 1);
154         assert(mesh_handle);
155         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
156
157         char *fp = meshlink_get_fingerprint(mesh_handle, NULL);
158         assert_int_equal(fp, NULL);
159
160         meshlink_close(mesh_handle);
161         assert(meshlink_destroy("getfingerprintconf"));
162
163         return true;
164 }
165
166 int test_meshlink_get_fingerprint(void) {
167         const struct CMUnitTest blackbox_get_fingerprint_tests[] = {
168                 cmocka_unit_test_prestate_setup_teardown(test_case_get_fingerprint_cb_01, NULL, NULL,
169                                 (void *)&test_case_get_fingerprint_cb_01_state),
170                 cmocka_unit_test_prestate_setup_teardown(test_case_get_fingerprint_cb_02, NULL, NULL,
171                                 (void *)&test_case_get_fingerprint_cb_02_state),
172                 cmocka_unit_test_prestate_setup_teardown(test_case_get_fingerprint_cb_03, NULL, NULL,
173                                 (void *)&test_case_get_fingerprint_cb_03_state)
174         };
175
176         total_tests += sizeof(blackbox_get_fingerprint_tests) / sizeof(blackbox_get_fingerprint_tests[0]);
177
178         return cmocka_run_group_tests(blackbox_get_fingerprint_tests, NULL, NULL);
179
180 }