]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_status_cb.c
Add the blackbox container based test suite.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_status_cb.c
1 /*
2     test_cases_status_cb.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_status_cb.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 #include <pthread.h>
32
33 /* Modify this to change the logging level of Meshlink */
34 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
35
36 static void test_case_set_status_cb_01(void **state);
37 static bool test_set_status_cb_01(void);
38 static void test_case_set_status_cb_02(void **state);
39 static bool test_set_status_cb_02(void);
40
41 /* status variable gives access to the status callback to know whether invoked or not */
42 static bool status;
43
44 /* State structure for status callback Test Case #1 */
45 static black_box_state_t test_case_set_status_cb_01_state = {
46         .test_case_name = "test_case_set_status_cb_01",
47 };
48
49 /* State structure for status callback Test Case #2 */
50 static black_box_state_t test_case_set_status_cb_02_state = {
51         .test_case_name = "test_case_set_status_cb_02",
52 };
53
54
55 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *source, bool reach) {
56         fprintf(stderr, "In status callback\n");
57
58         if(reach) {
59                 fprintf(stderr, "[ %s ] node reachable\n", source->name);
60         } else {
61                 fprintf(stderr, "[ %s ] node not reachable\n", source->name) ;
62         }
63
64         status = reach;
65 }
66
67 /* Execute status callback Test Case # 1 - valid case */
68 static void test_case_set_status_cb_01(void **state) {
69         execute_test(test_set_status_cb_01, state);
70 }
71
72 /* Test Steps for meshlink_set_status_cb Test Case # 1
73
74     Test Steps:
75     1. Run bar and nut node instances
76     2. Set status callback for the NUT and Start NUT
77
78     Expected Result:
79     status callback should be invoked when NUT connects/disconnects with 'relay' node.
80 */
81 static bool test_set_status_cb_01(void) {
82         meshlink_destroy("set_status_cb_conf.1");
83         meshlink_destroy("set_status_cb_conf.2");
84
85         // Opening NUT and bar nodes
86         meshlink_handle_t *mesh1 = meshlink_open("set_status_cb_conf.1", "nut", "test", DEV_CLASS_STATIONARY);
87         assert(mesh1 != NULL);
88         meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
89         meshlink_handle_t *mesh2 = meshlink_open("set_status_cb_conf.2", "bar", "test", DEV_CLASS_STATIONARY);
90         assert(mesh2 != NULL);
91         meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
92
93         // Set up callback for node status
94         meshlink_set_node_status_cb(mesh1, status_cb);
95
96         // Exporting and Importing mutually
97         char *exp1 = meshlink_export(mesh1);
98         assert(exp1 != NULL);
99         char *exp2 = meshlink_export(mesh2);
100         assert(exp2 != NULL);
101         bool imp1 = meshlink_import(mesh1, exp2);
102         bool imp2 = meshlink_import(mesh2, exp1);
103
104         assert(meshlink_start(mesh1));
105         assert(meshlink_start(mesh2));
106         sleep(1);
107
108         // Test for status from status callback
109         assert_int_equal(status, true);
110
111         meshlink_close(mesh2);
112         sleep(1);
113
114         // Test for status from status callback
115         assert_int_equal(status, false);
116
117         free(exp1);
118         free(exp2);
119         meshlink_close(mesh1);
120         meshlink_destroy("set_status_cb_conf.1");
121         meshlink_destroy("set_status_cb_conf.2");
122
123         return true;
124 }
125
126 /* Execute status callback Test Case # 2 - Invalid case */
127 static void test_case_set_status_cb_02(void **state) {
128         execute_test(test_set_status_cb_02, state);
129 }
130
131 /* Test Steps for meshlink_set_status_cb Test Case # 2
132
133     Test Steps:
134     1. Calling meshlink_set_status_cb with NULL as mesh handle argument.
135
136     Expected Result:
137     set poll callback handles the invalid parameter when called by giving proper error number.
138 */
139 static bool test_set_status_cb_02(void) {
140
141         // Create meshlink instance
142
143         meshlink_destroy("set_status_cb_conf.3");
144         meshlink_handle_t *mesh_handle = meshlink_open("set_status_cb_conf.3", "nut", "node_sim", 1);
145         assert(mesh_handle);
146
147         // Pass NULL as meshlink_set_node_status_cb's argument
148
149         meshlink_set_node_status_cb(NULL, status_cb);
150         meshlink_errno_t meshlink_errno_buff = meshlink_errno;
151         assert_int_equal(meshlink_errno_buff, MESHLINK_EINVAL);
152
153         // Clean up
154
155         meshlink_close(mesh_handle);
156         meshlink_destroy("set_status_cb_conf.3");
157         return true;
158 }
159
160
161 int test_meshlink_set_status_cb(void) {
162         const struct CMUnitTest blackbox_status_tests[] = {
163                 cmocka_unit_test_prestate_setup_teardown(test_case_set_status_cb_01, NULL, NULL,
164                                 (void *)&test_case_set_status_cb_01_state),
165                 cmocka_unit_test_prestate_setup_teardown(test_case_set_status_cb_02, NULL, NULL,
166                                 (void *)&test_case_set_status_cb_02_state)
167         };
168         total_tests += sizeof(blackbox_status_tests) / sizeof(blackbox_status_tests[0]);
169
170         int failed = cmocka_run_group_tests(blackbox_status_tests, NULL, NULL);
171
172         return failed;
173 }