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