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