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