]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_set_port.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_set_port.c
1 /*
2     test_cases_set_port.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_destroy.h"
26 #include "../common/containers.h"
27 #include "../common/test_step.h"
28 #include "../common/common_handlers.h"
29 #include "test_cases_set_port.h"
30 #include <assert.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <setjmp.h>
35 #include <cmocka.h>
36
37
38 /* Modify this to change the logging level of Meshlink */
39 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
40
41 static void test_case_set_port_01(void **state);
42 static bool test_set_port_01(void);
43 static void test_case_set_port_02(void **state);
44 static bool test_set_port_02(void);
45 static void test_case_set_port_03(void **state);
46 static bool test_set_port_03(void);
47
48 /* State structure for set port API Test Case #1 */
49 static black_box_state_t test_case_set_port_01_state = {
50         .test_case_name = "test_case_set_port_01",
51 };
52 /* State structure for set port API Test Case #2 */
53 static black_box_state_t test_case_set_port_02_state = {
54         .test_case_name = "test_case_set_port_02",
55 };
56 /* State structure for set port API Test Case #3 */
57 static black_box_state_t test_case_set_port_03_state = {
58         .test_case_name = "test_case_set_port_03",
59 };
60
61
62 /* Execute meshlink_set_port Test Case # 1 - valid case*/
63 static void test_case_set_port_01(void **state) {
64         execute_test(test_set_port_01, state);
65 }
66 /* Test Steps for meshlink_set_port Test Case # 1 - Valid case
67
68     Test Steps:
69     1. Open NUT(Node Under Test)
70     2. Set Port for NUT
71
72     Expected Result:
73     Set the new port to the NUT.
74 */
75 static bool test_set_port_01(void) {
76         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
77
78         // Create meshlink instance
79
80         mesh_handle = meshlink_open("setportconf", "nut", "test", 1);
81         assert(mesh_handle);
82         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
83
84         // Get old port and set a new port number
85
86         int port;
87         port = meshlink_get_port(mesh_handle);
88         assert(port > 0);
89         bool ret = meshlink_set_port(mesh_handle, 8000);
90         port = meshlink_get_port(mesh_handle);
91
92         assert_int_equal(port, 8000);
93         assert_int_equal(ret, true);
94
95         // Clean up
96
97         meshlink_close(mesh_handle);
98         assert(meshlink_destroy("setportconf"));
99         return true;
100 }
101
102 /* Execute meshlink_set_port Test Case # 2 - Invalid case*/
103 static void test_case_set_port_02(void **state) {
104         execute_test(test_set_port_02, state);
105 }
106
107 /* Test Steps for meshlink_set_port Test Case # 2 - Invalid case
108
109     Test Steps:
110     1. Pass NULL as mesh handle argument for meshlink_set_port
111
112     Expected Result:
113     Report false indicating error.
114 */
115 static bool test_set_port_02(void) {
116         // meshlink_set_port called using NULL as mesh handle
117
118         bool ret = meshlink_set_port(NULL, 8000);
119         assert_int_equal(meshlink_errno, 0);
120         assert_int_equal(ret, false);
121
122         return false;
123 }
124
125
126 /* Execute meshlink_set_port Test Case # 3 - Setting port after starting mesh*/
127 static void test_case_set_port_03(void **state) {
128         execute_test(test_set_port_03, state);
129 }
130
131 /* Test Steps for meshlink_set_port Test Case # 2 - functionality test
132
133     Test Steps:
134     1. Open and start NUT and then try to set new port number
135
136     Expected Result:
137     New port number cannot be set while mesh is running.
138 */
139 static bool test_set_port_03(void) {
140         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
141
142         // Create meshlink instance
143
144         mesh_handle = meshlink_open("getportconf", "nut", "test", 1);
145         assert(mesh_handle);
146         meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
147
148         // Setting port after starting NUT
149         bool ret = meshlink_set_port(mesh_handle, 50000);
150         assert_int_equal(meshlink_errno, 0);
151         assert_int_equal(ret, false);
152
153         // Clean up
154
155         meshlink_close(mesh_handle);
156         assert(meshlink_destroy("getportconf"));
157
158         return false;
159 }
160
161
162 int test_meshlink_set_port(void) {
163         const struct CMUnitTest blackbox_set_port_tests[] = {
164                 cmocka_unit_test_prestate_setup_teardown(test_case_set_port_01, NULL, NULL,
165                                 (void *)&test_case_set_port_01_state),
166                 cmocka_unit_test_prestate_setup_teardown(test_case_set_port_02, NULL, NULL,
167                                 (void *)&test_case_set_port_02_state),
168                 cmocka_unit_test_prestate_setup_teardown(test_case_set_port_03, NULL, NULL,
169                                 (void *)&test_case_set_port_03_state)
170         };
171         total_tests += sizeof(blackbox_set_port_tests) / sizeof(blackbox_set_port_tests[0]);
172         return cmocka_run_group_tests(blackbox_set_port_tests, NULL, NULL);
173 }