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