]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_channel_close.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_channel_close.c
1 /*
2     test_cases_channel_close.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_close.h"
26 #include "../common/containers.h"
27 #include "../common/test_step.h"
28 #include "../common/common_handlers.h"
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <setjmp.h>
32 #include <cmocka.h>
33 #include <assert.h>
34 #include <string.h>
35
36 static void test_case_mesh_channel_close_01(void **state);
37 static bool test_steps_mesh_channel_close_01(void);
38 static void test_case_mesh_channel_close_02(void **state);
39 static bool test_steps_mesh_channel_close_02(void);
40
41 /* State structure for meshlink_channel_close Test Case #1 */
42 static black_box_state_t test_mesh_channel_close_01_state = {
43         .test_case_name = "test_case_mesh_channel_close_01",
44 };
45
46 /* State structure for meshlink_channel_close Test Case #2 */
47 static black_box_state_t test_mesh_channel_close_02_state = {
48         .test_case_name = "test_case_mesh_channel_close_02",
49 };
50
51 /* Execute meshlink_channel_close Test Case # 1*/
52 static void test_case_mesh_channel_close_01(void **state) {
53         execute_test(test_steps_mesh_channel_close_01, state);
54         return;
55 }
56
57 /* Test Steps for meshlink_channel_close Test Case # 1*/
58 static bool test_steps_mesh_channel_close_01(void) {
59         assert(meshlink_destroy("chan_close_conf.3"));
60         assert(meshlink_destroy("chan_close_conf.4"));
61
62         // Open two new meshlink instance.
63         meshlink_handle_t *mesh1 = meshlink_open("chan_close_conf.3", "foo", "channels", DEV_CLASS_BACKBONE);
64         assert(mesh1 != NULL);
65
66         meshlink_handle_t *mesh2 = meshlink_open("chan_close_conf.4", "bar", "channels", DEV_CLASS_BACKBONE);
67         assert(mesh2 != NULL);
68
69         if(!mesh2) {
70                 fprintf(stderr, "Could not initialize configuration for bar\n");
71                 return false;
72         }
73
74         char *exp = meshlink_export(mesh1);
75         assert(exp != NULL);
76         assert(meshlink_import(mesh2, exp));
77         free(exp);
78         exp = meshlink_export(mesh2);
79         assert(exp != NULL);
80         assert(meshlink_import(mesh1, exp));
81         free(exp);
82
83         // Start both instances
84         assert(meshlink_start(mesh1));
85         assert(meshlink_start(mesh2));
86         sleep(2);
87
88         // Open a channel from foo to bar.
89
90         meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
91         assert(bar != NULL);
92         meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7, NULL, NULL, 0);
93         assert(channel != NULL);
94
95         meshlink_channel_close(NULL, channel);
96         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
97
98
99         // Clean up.
100
101         meshlink_close(mesh2);
102         meshlink_close(mesh1);
103         assert(meshlink_destroy("chan_close_conf.3"));
104         assert(meshlink_destroy("chan_close_conf.4"));
105         return true;
106 }
107
108 /* Execute meshlink_channel_close Test Case # 2*/
109 static void test_case_mesh_channel_close_02(void **state) {
110         execute_test(test_steps_mesh_channel_close_02, state);
111         return;
112 }
113
114 /* Test Steps for meshlink_channel_close Test Case # 2*/
115 static bool test_steps_mesh_channel_close_02(void) {
116         assert(meshlink_destroy("chan_close_conf.5"));
117         // Open two new meshlink instance.
118
119         meshlink_handle_t *mesh = meshlink_open("chan_close_conf.5", "foo", "channels", DEV_CLASS_BACKBONE);
120         assert(mesh != NULL);
121
122         // Start both instances
123         assert(meshlink_start(mesh));
124
125         // Pass NULL as mesh handle
126         meshlink_channel_close(mesh, NULL);
127         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
128
129         // Clean up.
130
131         meshlink_close(mesh);
132         assert(meshlink_destroy("chan_close_conf.5"));
133         return true;
134 }
135
136 int test_meshlink_channel_close(void) {
137         const struct CMUnitTest blackbox_channel_close_tests[] = {
138                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_channel_close_01, NULL, NULL,
139                                 (void *)&test_mesh_channel_close_01_state),
140                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_channel_close_02, NULL, NULL,
141                                 (void *)&test_mesh_channel_close_02_state)
142         };
143
144         total_tests += sizeof(blackbox_channel_close_tests) / sizeof(blackbox_channel_close_tests[0]);
145
146         return cmocka_run_group_tests(blackbox_channel_close_tests, NULL, NULL);
147 }