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.
20 #include "execute_tests.h"
21 #include "test_cases_destroy.h"
22 #include "../common/containers.h"
23 #include "../common/test_step.h"
24 #include "../common/common_handlers.h"
25 #include "test_cases_set_port.h"
34 /* Modify this to change the logging level of Meshlink */
35 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
37 static void test_case_set_port_01(void **state);
38 static bool test_set_port_01(void);
39 static void test_case_set_port_02(void **state);
40 static bool test_set_port_02(void);
41 static void test_case_set_port_03(void **state);
42 static bool test_set_port_03(void);
44 /* State structure for set port API Test Case #1 */
45 static black_box_state_t test_case_set_port_01_state = {
46 .test_case_name = "test_case_set_port_01",
48 /* State structure for set port API Test Case #2 */
49 static black_box_state_t test_case_set_port_02_state = {
50 .test_case_name = "test_case_set_port_02",
52 /* State structure for set port API Test Case #3 */
53 static black_box_state_t test_case_set_port_03_state = {
54 .test_case_name = "test_case_set_port_03",
58 /* Execute meshlink_set_port Test Case # 1 - valid case*/
59 static void test_case_set_port_01(void **state) {
60 execute_test(test_set_port_01, state);
62 /* Test Steps for meshlink_set_port Test Case # 1 - Valid case
65 1. Open NUT(Node Under Test)
69 Set the new port to the NUT.
71 static bool test_set_port_01(void) {
72 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
74 // Create meshlink instance
76 mesh_handle = meshlink_open("setportconf", "nut", "test", 1);
78 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
80 // Get old port and set a new port number
83 port = meshlink_get_port(mesh_handle);
85 bool ret = meshlink_set_port(mesh_handle, 8000);
86 port = meshlink_get_port(mesh_handle);
88 assert_int_equal(port, 8000);
89 assert_int_equal(ret, true);
93 meshlink_close(mesh_handle);
94 meshlink_destroy("setportconf");
98 /* Execute meshlink_set_port Test Case # 2 - Invalid case*/
99 static void test_case_set_port_02(void **state) {
100 execute_test(test_set_port_02, state);
103 /* Test Steps for meshlink_set_port Test Case # 2 - Invalid case
106 1. Pass NULL as mesh handle argument for meshlink_set_port
109 Report false indicating error.
111 static bool test_set_port_02(void) {
112 // meshlink_set_port called using NULL as mesh handle
114 bool ret = meshlink_set_port(NULL, 8000);
115 assert_int_equal(meshlink_errno, 0);
116 assert_int_equal(ret, false);
122 /* Execute meshlink_set_port Test Case # 3 - Setting port after starting mesh*/
123 static void test_case_set_port_03(void **state) {
124 execute_test(test_set_port_03, state);
127 /* Test Steps for meshlink_set_port Test Case # 2 - functionality test
130 1. Open and start NUT and then try to set new port number
133 New port number cannot be set while mesh is running.
135 static bool test_set_port_03(void) {
136 meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
138 // Create meshlink instance
140 mesh_handle = meshlink_open("getportconf", "nut", "test", 1);
142 meshlink_set_log_cb(mesh_handle, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
144 // Setting port after starting NUT
145 bool ret = meshlink_set_port(mesh_handle, 50000);
146 assert_int_equal(meshlink_errno, 0);
147 assert_int_equal(ret, false);
151 meshlink_close(mesh_handle);
152 meshlink_destroy("getportconf");
156 int test_meshlink_set_port(void) {
157 const struct CMUnitTest blackbox_set_port_tests[] = {
158 cmocka_unit_test_prestate_setup_teardown(test_case_set_port_01, NULL, NULL,
159 (void *)&test_case_set_port_01_state),
160 cmocka_unit_test_prestate_setup_teardown(test_case_set_port_02, NULL, NULL,
161 (void *)&test_case_set_port_02_state),
162 cmocka_unit_test_prestate_setup_teardown(test_case_set_port_03, NULL, NULL,
163 (void *)&test_case_set_port_03_state)
165 total_tests += sizeof(blackbox_set_port_tests) / sizeof(blackbox_set_port_tests[0]);
166 return cmocka_run_group_tests(blackbox_set_port_tests, NULL, NULL);