]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_invite.c
Update invite API in blackbox test cases
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_invite.c
1 /*
2     test_cases_invite.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_invite.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_invite_01(void **state);
36 static bool test_invite_01(void);
37 static void test_case_invite_02(void **state);
38 static bool test_invite_02(void);
39 static void test_case_invite_03(void **state);
40 static bool test_invite_03(void);
41 static void test_case_invite_04(void **state);
42 static bool test_invite_04(void);
43
44 /* State structure for invite API Test Case #1 */
45 static black_box_state_t test_case_invite_01_state = {
46         .test_case_name = "test_case_invite_01",
47 };
48
49 /* State structure for invite API Test Case #2 */
50 static black_box_state_t test_case_invite_02_state = {
51         .test_case_name = "test_case_invite_02",
52 };
53
54 /* State structure for invite API Test Case #3 */
55 static black_box_state_t test_case_invite_03_state = {
56         .test_case_name = "test_case_invite_03",
57 };
58
59 /* State structure for invite API Test Case #4 */
60 static black_box_state_t test_case_invite_04_state = {
61         .test_case_name = "test_case_invite_04",
62 };
63
64 /* Execute invite Test Case # 1 - valid case*/
65 static void test_case_invite_01(void **state) {
66         execute_test(test_invite_01, state);
67 }
68 /*Test Steps for meshlink_invite Test Case # 1 - Valid case
69     Test Steps:
70     1. Run NUT
71     2. Invite 'new' node
72
73     Expected Result:
74     Generates an invitation
75 */
76 static bool test_invite_01(void) {
77         meshlink_destroy("inviteconf");
78         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
79
80         // Create meshlink instance
81         meshlink_handle_t *mesh_handle = meshlink_open("inviteconf", "nut", "node_sim", 1);
82         assert(mesh_handle);
83         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
84
85         char *invitation = meshlink_invite(mesh_handle, NULL, "new");
86         assert_int_equal(invitation, NULL);
87
88         free(invitation);
89         meshlink_close(mesh_handle);
90         meshlink_destroy("inviteconf");
91         return true;
92 }
93
94 /* Execute invite Test Case # 2 - Invalid case*/
95 static void test_case_invite_02(void **state) {
96         execute_test(test_invite_02, state);
97 }
98 /*Test Steps for meshlink_invite Test Case # 2 - Invalid case
99     Test Steps:
100     1. Calling meshlink_invite API with NULL as mesh handle argument
101
102     Expected Result:
103     Reports appropriate error by returning NULL
104 */
105 static bool test_invite_02(void) {
106         // Trying to generate INVITATION by passing NULL as mesh link handle
107         char *invitation = meshlink_invite(NULL, NULL, "nut");
108         assert_int_equal(invitation, NULL);
109
110         return true;
111 }
112
113 /* Execute invite Test Case # 3 - Invalid case*/
114 static void test_case_invite_03(void **state) {
115         execute_test(test_invite_03, state);
116 }
117 /*Test Steps for meshlink_invite Test Case # 3 - Invalid case
118     Test Steps:
119     1. Run NUT
120     2. Call meshlink_invite with NULL node name argument
121
122     Expected Result:
123     Reports appropriate error by returning NULL
124 */
125 static bool test_invite_03(void) {
126         meshlink_destroy("inviteconf");
127         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
128
129         // Create meshlink instance
130         meshlink_handle_t *mesh_handle = meshlink_open("inviteconf", "nut", "node_sim", 1);
131         assert(mesh_handle);
132         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
133
134         // Trying to generate INVITATION by passing NULL as mesh link handle
135         char *invitation = meshlink_invite(mesh_handle, NULL, NULL);
136         assert_int_equal(invitation, NULL);
137
138         meshlink_close(mesh_handle);
139         meshlink_destroy("inviteconf");
140         return true;
141 }
142
143 /* Execute invite Test Case # 4 - Functionality test*/
144 static void test_case_invite_04(void **state) {
145         execute_test(test_invite_04, state);
146 }
147 /*Test Steps for meshlink_invite Test Case # 4 - Functionality test
148
149     Test Steps:
150     1. Create node instance
151     2. Add a new address to the mesh and invite a node
152     3. Add another new address and invite a node
153
154     Expected Result:
155     Newly added address should be there in the invitation.
156 */
157 static bool test_invite_04(void) {
158         meshlink_destroy("inviteconf");
159         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
160
161         // Create meshlink instance
162         meshlink_handle_t *mesh_handle = meshlink_open("inviteconf", "nut", "test", 1);
163         assert(mesh_handle);
164         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
165
166         char *hostname1 = "127.1.1.1";
167         bool ret = meshlink_add_address(mesh_handle, hostname1);
168         char *invitation = meshlink_invite(mesh_handle, NULL, "foo");
169         assert_int_not_equal(strstr(invitation, hostname1), NULL);
170
171         char *hostname2 = "127.1.2.3";
172         ret = meshlink_add_address(mesh_handle, hostname2);
173         invitation = meshlink_invite(mesh_handle, NULL, "bar");
174
175         // Verify we have both the added addresses
176         assert_int_not_equal(strstr(invitation, hostname1), NULL);
177         assert_int_not_equal(strstr(invitation, hostname2), NULL);
178
179         meshlink_close(mesh_handle);
180         meshlink_destroy("inviteconf");
181
182         return true;
183 }
184
185 int test_meshlink_invite(void) {
186         const struct CMUnitTest blackbox_invite_tests[] = {
187                 cmocka_unit_test_prestate_setup_teardown(test_case_invite_01, NULL, NULL,
188                                 (void *)&test_case_invite_01_state),
189                 cmocka_unit_test_prestate_setup_teardown(test_case_invite_02, NULL, NULL,
190                                 (void *)&test_case_invite_02_state),
191                 cmocka_unit_test_prestate_setup_teardown(test_case_invite_03, NULL, NULL,
192                                 (void *)&test_case_invite_03_state),
193                 cmocka_unit_test_prestate_setup_teardown(test_case_invite_04, NULL, NULL,
194                                 (void *)&test_case_invite_04_state)
195         };
196
197         total_tests += sizeof(blackbox_invite_tests) / sizeof(blackbox_invite_tests[0]);
198
199         return cmocka_run_group_tests(blackbox_invite_tests, NULL, NULL);
200 }