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