]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_get_port.c
177540b30b5990709bd8c8678c46c7adcc002dd3
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_get_port.c
1 /*
2     test_cases_get_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_get_port.h"
22 #include "../common/containers.h"
23 #include "../common/test_step.h"
24 #include "../common/common_handlers.h"
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <setjmp.h>
28 #include <cmocka.h>
29 #include <assert.h>
30 #include <string.h>
31
32 static void test_case_mesh_get_port_01(void **state);
33 static bool test_steps_mesh_get_port_01(void);
34 static void test_case_mesh_get_port_02(void **state);
35 static bool test_steps_mesh_get_port_02(void);
36
37 /* State structure for meshlink_get_port Test Case #1 */
38 static black_box_state_t test_mesh_get_port_01_state = {
39         .test_case_name = "test_case_mesh_get_port_01",
40 };
41
42 /* State structure for meshlink_get_port Test Case #2 */
43 static black_box_state_t test_mesh_get_port_02_state = {
44         .test_case_name = "test_case_mesh_get_port_02",
45 };
46
47 /* Execute meshlink_get_port Test Case # 1 */
48 static void test_case_mesh_get_port_01(void **state) {
49         execute_test(test_steps_mesh_get_port_01, state);
50 }
51
52 /* Test Steps for meshlink_get_port Test Case # 1
53
54     Test Steps:
55     1. Open node instance
56     2. Run the node instance
57     3. Obtain port of that mesh using meshlink_get_port API
58
59     Expected Result:
60     API returns valid port number.
61 */
62 static bool test_steps_mesh_get_port_01(void) {
63         meshlink_handle_t *mesh = meshlink_open("port_conf", "foo", "chat", DEV_CLASS_STATIONARY);
64         assert(mesh);
65         assert(meshlink_start(mesh));
66
67         int port = meshlink_get_port(mesh);
68         assert_int_not_equal(port, -1);
69
70         meshlink_close(mesh);
71         meshlink_destroy("port_conf");
72         return true;
73 }
74
75 /* Execute meshlink_get_port Test Case # 2 */
76 static void test_case_mesh_get_port_02(void **state) {
77         execute_test(test_steps_mesh_get_port_02, state);
78 }
79
80 /* Test Steps for meshlink_get_port Test Case # 2 - Invalid case
81
82     Test Steps:
83     1. Pass NULL as mesh handle argument to meshlink_get_port API
84
85     Expected Result:
86     Reports error successfully by returning -1
87 */
88 static bool test_steps_mesh_get_port_02(void) {
89         int port = meshlink_get_port(NULL);
90         assert_int_equal(port, -1);
91
92         return true;
93 }
94
95 int test_meshlink_get_port(void) {
96         const struct CMUnitTest blackbox_get_port_tests[] = {
97                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_get_port_01, NULL, NULL,
98                                 (void *)&test_mesh_get_port_01_state),
99                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_get_port_02, NULL, NULL,
100                                 (void *)&test_mesh_get_port_02_state)
101         };
102
103         total_tests += sizeof(blackbox_get_port_tests) / sizeof(blackbox_get_port_tests[0]);
104
105         return cmocka_run_group_tests(blackbox_get_port_tests, NULL, NULL);
106 }