2 test_cases_set_port.c -- Execution of specific meshlink black box test cases
3 Copyright (C) 2018 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
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"
38 /* Modify this to change the logging level of Meshlink */
39 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
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);
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",
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",
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",
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);
66 /* Test Steps for meshlink_set_port Test Case # 1 - Valid case
69 1. Open NUT(Node Under Test)
73 Set the new port to the NUT.
75 static bool test_set_port_01(void) {
76 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
78 // Create meshlink instance
80 mesh_handle = meshlink_open("setportconf", "nut", "test", 1);
82 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
84 // Get old port and set a new port number
87 port = meshlink_get_port(mesh_handle);
89 bool ret = meshlink_set_port(mesh_handle, 8000);
90 port = meshlink_get_port(mesh_handle);
92 assert_int_equal(port, 8000);
93 assert_int_equal(ret, true);
97 meshlink_close(mesh_handle);
98 assert(meshlink_destroy("setportconf"));
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);
107 /* Test Steps for meshlink_set_port Test Case # 2 - Invalid case
110 1. Pass NULL as mesh handle argument for meshlink_set_port
113 Report false indicating error.
115 static bool test_set_port_02(void) {
116 // meshlink_set_port called using NULL as mesh handle
118 bool ret = meshlink_set_port(NULL, 8000);
119 assert_int_equal(meshlink_errno, 0);
120 assert_int_equal(ret, false);
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);
131 /* Test Steps for meshlink_set_port Test Case # 2 - functionality test
134 1. Open and start NUT and then try to set new port number
137 New port number cannot be set while mesh is running.
139 static bool test_set_port_03(void) {
140 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
142 // Create meshlink instance
144 mesh_handle = meshlink_open("getportconf", "nut", "test", 1);
146 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
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);
155 meshlink_close(mesh_handle);
156 assert(meshlink_destroy("getportconf"));
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)
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);