]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_add_ex_addr.c
bff9dd83c08c45bd6eca1d2320ba76a2be589091
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_add_ex_addr.c
1 /*
2     test_cases_add_ex_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_ex_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_ex_address_01(void **state);
33 static bool test_steps_mesh_add_ex_address_01(void);
34 static void test_case_mesh_add_ex_address_02(void **state);
35 static bool test_steps_mesh_add_ex_address_02(void);
36
37 /* State structure for meshlink_add_external_address Test Case #1 */
38 static black_box_state_t test_mesh_add_ex_address_01_state = {
39         .test_case_name = "test_case_mesh_add_ex_address_01",
40 };
41
42 /* State structure for meshlink_add_external_address Test Case #2 */
43 static black_box_state_t test_mesh_add_ex_address_02_state = {
44         .test_case_name = "test_case_mesh_add_ex_address_01",
45 };
46
47 /* Execute meshlink_add_external_address Test Case # 1 */
48 void test_case_mesh_add_ex_address_01(void **state) {
49         execute_test(test_steps_mesh_add_ex_address_01, state);
50 }
51
52 /* Test Steps for meshlink_add_external_address Test Case # 1
53
54     Test Steps:
55     1. Create node instance
56     2. Get mesh's external address
57     3. Add external address using meshlink_add_external_address API
58     4. Open nodes confbase and read the external address from the list if addresses
59
60     Expected Result:
61     meshlink_add_external_address API adds the new address given to it's confbase
62 */
63 bool test_steps_mesh_add_ex_address_01(void) {
64         meshlink_destroy("addex_conf.1");
65
66         // Create node instance
67         meshlink_handle_t *mesh = meshlink_open("addex_conf.1", "foo", "test", DEV_CLASS_STATIONARY);
68         assert(mesh != NULL);
69
70         char *external_address = meshlink_get_external_address(mesh);
71         assert(external_address);
72
73         bool ret = meshlink_add_external_address(mesh);
74         assert_int_equal(ret, true);
75
76         // Open the foo host file from confbase to verify address being added
77         bool found = false;
78         FILE *fp = fopen("./addex_conf.1/hosts/foo", "r");
79         assert(fp);
80         char line[100];
81
82         while(fgets(line, 100, fp) != NULL) {
83                 if(strcasestr(line, "Address") && strcasestr(line, external_address)) {
84                         found = true;
85                 }
86         }
87
88         assert(!fclose(fp));
89
90         assert_int_equal(found, true);
91
92         meshlink_close(mesh);
93         meshlink_destroy("addex_conf.1");
94         return true;
95 }
96
97 /* Execute meshlink_add_external_address Test Case # 2 */
98 void test_case_mesh_add_ex_address_02(void **state) {
99         execute_test(test_steps_mesh_add_ex_address_02, state);
100 }
101
102 /* Test Steps for meshlink_add_external_address Test Case # 2
103
104     Test Steps:
105     1. Create node instance
106     2. Call meshlink_add_external_address API using NULL as mesh handle argument
107
108     Expected Result:
109     meshlink_add_external_address API returns false by reporting error successfully.
110 */
111 bool test_steps_mesh_add_ex_address_02(void) {
112         bool result = meshlink_add_external_address(NULL);
113         assert_int_equal(result, false);
114
115         return true;
116 }
117
118 int test_meshlink_add_external_address(void) {
119         const struct CMUnitTest blackbox_add_ex_addr_tests[] = {
120                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_add_ex_address_01, NULL, NULL,
121                                 (void *)&test_mesh_add_ex_address_01_state),
122                 cmocka_unit_test_prestate_setup_teardown(test_case_mesh_add_ex_address_02, NULL, NULL,
123                                 (void *)&test_mesh_add_ex_address_02_state)
124         };
125
126         total_tests += sizeof(blackbox_add_ex_addr_tests) / sizeof(blackbox_add_ex_addr_tests[0]);
127
128         return cmocka_run_group_tests(blackbox_add_ex_addr_tests, NULL, NULL);
129 }
130