]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_add_addr.c
Add the blackbox container based test suite.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_add_addr.c
1 /*
2     test_cases_add_addr.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_add_addr.h"
22 #include "../common/containers.h"
23 #include "../common/test_step.h"
24 #include "../common/common_handlers.h"
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <setjmp.h>
29 #include <cmocka.h>
30 #include <assert.h>
31
32 static void test_case_mesh_add_address_01(void **state);
33 static bool test_steps_mesh_add_address_01(void);
34 static void test_case_mesh_add_address_02(void **state);
35 static bool test_steps_mesh_add_address_02(void);
36 static void test_case_mesh_add_address_03(void **state);
37 static bool test_steps_mesh_add_address_03(void);
38
39 /* State structure for meshlink_add_address Test Case #1 */
40 static black_box_state_t test_mesh_add_address_01_state = {
41         .test_case_name = "test_case_mesh_add_address_01",
42 };
43
44 /* State structure for meshlink_add_address Test Case #2 */
45 static black_box_state_t test_mesh_add_address_02_state = {
46         .test_case_name = "test_case_mesh_add_address_02",
47 };
48
49 /* State structure for meshlink_add_address Test Case #3 */
50 static black_box_state_t test_mesh_add_address_03_state = {
51         .test_case_name = "test_case_mesh_add_address_03",
52 };
53
54 /* Execute meshlink_add_address Test Case # 1 */
55 static void test_case_mesh_add_address_01(void **state) {
56         execute_test(test_steps_mesh_add_address_01, state);
57 }
58
59 /* Test Steps for meshlink_add_address Test Case # 1
60
61     Test Steps:
62     1. Create node instance
63     2. Add an address to the host node
64     2. Open host file from confbase & verify address being added
65
66     Expected Result:
67     meshlink_add_address API adds the new address given to it's confbase
68 */
69 static bool test_steps_mesh_add_address_01(void) {
70         char *node = "foo";
71         meshlink_destroy("add_conf.1");
72
73         // Create node instance
74         meshlink_handle_t *mesh = meshlink_open("add_conf.1", node, "chat", DEV_CLASS_STATIONARY);
75         assert(mesh != NULL);
76
77         char *hostname = "localhost";
78         bool ret = meshlink_add_address(mesh, hostname);
79         assert_int_equal(ret, true);
80
81         // Open the foo host file from confbase to verify address being added
82         bool found = false;
83         FILE *fp = fopen("./add_conf.1/hosts/foo", "r");
84         assert(fp);
85         char line[100];
86
87         while(fgets(line, 100, fp) != NULL) {
88                 if(strcasestr(line, "Address") && strcasestr(line, hostname)) {
89                         found = true;
90                 }
91         }
92
93         assert(!fclose(fp));
94
95         assert_int_equal(found, true);
96
97         // Clean up
98         meshlink_close(mesh);
99         meshlink_destroy("add_conf.1");
100         return true;
101 }
102
103 /* Execute meshlink_add_address Test Case # 2 */
104 static void test_case_mesh_add_address_02(void **state) {
105         execute_test(test_steps_mesh_add_address_02, state);
106 }
107
108 /* Test Steps for meshlink_add_address Test Case # 2
109
110     Test Steps:
111     1. Create node instance
112     2. Call meshlink_add_address API using NULL as mesh handle argument
113
114     Expected Result:
115     meshlink_add_address API returns false by reporting error successfully.
116 */
117 static bool test_steps_mesh_add_address_02(void) {
118         // Passing NULL as mesh handle argument to meshlink_add_address API
119         bool result = meshlink_add_address(NULL, "localhost");
120         assert_int_equal(result, false);
121
122         return true;
123 }
124
125 /* Execute meshlink_add_address Test Case # 3 */
126 static void test_case_mesh_add_address_03(void **state) {
127         execute_test(test_steps_mesh_add_address_03, state);
128 }
129
130 /* Test Steps for meshlink_add_address Test Case # 3
131
132     Test Steps:
133     1. Create node instance
134     2. Call meshlink_add_address API using NULL as address argument
135
136     Expected Result:
137     meshlink_add_address API returns false by reporting error successfully.
138 */
139 static bool test_steps_mesh_add_address_03(void) {
140         meshlink_destroy("add_conf.3");
141
142         // Create node instance
143         meshlink_handle_t *mesh = meshlink_open("add_conf.3", "foo", "chat", DEV_CLASS_STATIONARY);
144         assert(mesh != NULL);
145
146         bool result = meshlink_add_address(mesh, NULL);
147         assert_int_equal(result, false);
148
149         meshlink_close(mesh);
150         meshlink_destroy("add_conf.3");
151         return true;
152 }
153
154 int test_meshlink_add_address(void) {
155         const struct CMUnitTest blackbox_add_addr_tests[] = {
156                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_add_address_01, NULL, NULL,
157                                 (void *)&test_mesh_add_address_01_state),
158                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_add_address_02, NULL, NULL,
159                                 (void *)&test_mesh_add_address_02_state),
160                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_add_address_03, NULL, NULL,
161                                 (void *)&test_mesh_add_address_03_state)
162         };
163
164         total_tests += sizeof(blackbox_add_addr_tests) / sizeof(blackbox_add_addr_tests[0]);
165
166         return cmocka_run_group_tests(blackbox_add_addr_tests, NULL, NULL);
167 }
168