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