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