]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_channel_get_flags.c
Add the blackbox container based test suite.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_channel_get_flags.c
1 /*
2     test_cases_channel_get_flags.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_channel_get_flags.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 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
33 /* Modify this to change the port number */
34 #define PORT 8000
35
36 static void test_case_channel_get_flags_01(void **state);
37 static bool test_steps_channel_get_flags_01(void);
38 static void test_case_channel_get_flags_02(void **state);
39 static bool test_steps_channel_get_flags_02(void);
40 static void test_case_channel_get_flags_03(void **state);
41 static bool test_steps_channel_get_flags_03(void);
42
43 static black_box_state_t test_case_channel_get_flags_01_state = {
44         .test_case_name = "test_case_channel_get_flags_01",
45 };
46 static black_box_state_t test_case_channel_get_flags_02_state = {
47         .test_case_name = "test_case_channel_get_flags_02",
48 };
49 static black_box_state_t test_case_channel_get_flags_03_state = {
50         .test_case_name = "test_case_channel_get_flags_03",
51 };
52 static black_box_state_t test_case_channel_get_flags_04_state = {
53         .test_case_name = "test_case_channel_get_flags_04",
54 };
55
56
57 /* Execute meshlink_channel_get_flags Test Case # 1 - Valid case*/
58 static void test_case_channel_get_flags_01(void **state) {
59         execute_test(test_steps_channel_get_flags_01, state);
60 }
61 /* Test Steps for meshlink_channel_get_flags Test Case # 1
62
63     Test Steps:
64     1. Run NUT(Node Under Test)
65     2. Open channel to ourself (with TCP semantic here)
66     3. Get flag(s) of that channel
67
68     Expected Result:
69     API returning exact flag that has been assigned while opening (here TCP)
70 */
71 static bool test_steps_channel_get_flags_01(void) {
72         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
73
74         /* Create meshlink instance */
75         meshlink_handle_t *mesh_handle = meshlink_open("getflagsconf", "nut", "node_sim", 1);
76         assert(mesh_handle);
77         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
78         meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
79         meshlink_set_channel_accept_cb(mesh_handle, NULL);
80
81         assert(meshlink_start(mesh_handle));
82
83         meshlink_node_t *node = meshlink_get_self(mesh_handle);
84         assert(node != NULL);
85         sleep(1);
86
87         /* Passing all valid arguments for meshlink_channel_open_ex */
88         meshlink_channel_t *channel;
89         channel = meshlink_channel_open_ex(mesh_handle, node, PORT, NULL, NULL, 0, MESHLINK_CHANNEL_TCP);
90         assert(channel != NULL);
91
92         // Obtaining channel flags using meshlink_channel_get_flags
93         uint32_t flags = meshlink_channel_get_flags(mesh_handle, channel);
94         assert_int_equal(flags, MESHLINK_CHANNEL_TCP);
95
96         meshlink_close(mesh_handle);
97         meshlink_destroy("getflagsconf");
98
99         return true;
100 }
101
102 /* Execute meshlink_channel_get_flags Test Case # 2 - Invalid case*/
103 static void test_case_channel_get_flags_02(void **state) {
104         execute_test(test_steps_channel_get_flags_02, state);
105 }
106 /* Test Steps for meshlink_channel_get_flags Test Case # 2
107
108     Test Steps:
109     1. Run NUT(Node Under Test)
110     2. Open channel to ourself (with TCP semantic here)
111     3. Call meshlink_channel_get_flags by passing NULL as mesh handle argument
112
113     Expected Result:
114     API reporting error accordingly.
115 */
116 static bool test_steps_channel_get_flags_02(void) {
117         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
118
119         /* Create meshlink instance */
120         meshlink_handle_t *mesh_handle = meshlink_open("getflagsconf", "nut", "node_sim", 1);
121         assert(mesh_handle);
122
123         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
124         meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
125         meshlink_set_channel_accept_cb(mesh_handle, NULL);
126
127         assert(meshlink_start(mesh_handle));
128
129         /* Getting node handle for itself */
130         meshlink_node_t *node = meshlink_get_self(mesh_handle);
131         assert(node != NULL);
132         sleep(1);
133
134         /* Passing all valid arguments for meshlink_channel_open_ex */
135         meshlink_channel_t *channel;
136         channel = meshlink_channel_open_ex(mesh_handle, node, PORT, NULL, NULL, 0, MESHLINK_CHANNEL_TCP);
137         assert(channel != NULL);
138
139         // passing NULL as mesh handle argument for meshlink_channel_get_flags
140         uint32_t flags = meshlink_channel_get_flags(NULL, channel);
141
142         assert_int_equal((int32_t)flags, -1);
143         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
144
145         meshlink_close(mesh_handle);
146         meshlink_destroy("getflagsconf");
147         return true;
148 }
149
150 /* Execute meshlink_channel_get flags Test Case # 3 - Invalid case*/
151 static void test_case_channel_get_flags_03(void **state) {
152         execute_test(test_steps_channel_get_flags_03, state);
153 }
154 /* Test Steps for meshlink_channel_get_flags Test Case # 3
155
156     Test Steps:
157     1. Run NUT(Node Under Test)
158     3. Call meshlink_channel_get_flags by passing NULL as channel handle argument
159
160     Expected Result:
161     API reporting error accordingly.
162 */
163 static bool test_steps_channel_get_flags_03(void) {
164         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
165
166         /* Create meshlink instance */
167         meshlink_handle_t *mesh_handle = meshlink_open("getflagsconf", "nut", "node_sim", 1);
168         assert(mesh_handle);
169         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
170         meshlink_set_node_status_cb(mesh_handle, meshlink_callback_node_status);
171         meshlink_set_channel_accept_cb(mesh_handle, NULL);
172
173         assert(meshlink_start(mesh_handle));
174
175         // passing NULL as channel handle argument for meshlink_channel_get_flags
176         uint32_t flags = meshlink_channel_get_flags(mesh_handle, NULL);
177
178         assert_int_equal((int32_t)flags, -1);
179         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
180
181         meshlink_close(mesh_handle);
182         meshlink_destroy("getflagsconf");
183         return true;
184 }
185
186
187 int test_meshlink_channel_get_flags(void) {
188         const struct CMUnitTest blackbox_channel_get_flags_tests[] = {
189                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_get_flags_01, NULL, NULL,
190                                 (void *)&test_case_channel_get_flags_01_state),
191                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_get_flags_02, NULL, NULL,
192                                 (void *)&test_case_channel_get_flags_02_state),
193                 cmocka_unit_test_prestate_setup_teardown(test_case_channel_get_flags_03, NULL, NULL,
194                                 (void *)&test_case_channel_get_flags_03_state)
195         };
196
197         total_tests += sizeof(blackbox_channel_get_flags_tests) / sizeof(blackbox_channel_get_flags_tests[0]);
198
199         return cmocka_run_group_tests(blackbox_channel_get_flags_tests, NULL, NULL);
200 }