]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_hint_address.c
aacbbecdbd01acce5536c455e1f9a7b22a0c3165
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_hint_address.c
1 /*
2     test_cases_hint_address.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_hint_address.h"
22 #include "../common/containers.h"
23 #include "../common/test_step.h"
24 #include "../common/common_handlers.h"
25 #include <assert.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <setjmp.h>
30 #include <cmocka.h>
31 #include <pthread.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <arpa/inet.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39
40
41 /* Modify this to change the logging level of Meshlink */
42 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
43
44 /* Port number used in the structure */
45 #define PORT 8000
46
47 /* hint address used in the socket structure */
48 #define ADDR "10.1.1.1"
49
50 static void test_case_hint_address_01(void **state);
51 static bool test_steps_hint_address_01(void);
52
53 static black_box_state_t test_case_hint_address_01_state = {
54         .test_case_name = "test_case_hint_address_01",
55 };
56
57
58 /* Execute meshlink_hint_address Test Case # 1 - Valid Case*/
59 void test_case_hint_address_01(void **state) {
60         execute_test(test_steps_hint_address_01, state);
61 }
62 /* Test Steps for meshlink_hint_address Test Case # 1 - Valid case */
63 bool test_steps_hint_address_01(void) {
64         meshlink_destroy("hintconf1");
65         meshlink_destroy("hintconf2");
66         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
67
68         // Create meshlink instance for the nodes
69         meshlink_handle_t *mesh1 = meshlink_open("hintconf1", "nut", "test", DEV_CLASS_STATIONARY);
70         assert(mesh1);
71         meshlink_handle_t *mesh2 = meshlink_open("hintconf2", "bar", "test", DEV_CLASS_STATIONARY);
72         assert(mesh2);
73         meshlink_set_log_cb(mesh1, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
74         meshlink_set_log_cb(mesh2, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
75
76         // importing and exporting mesh meta data
77         char *exp1 = meshlink_export(mesh1);
78         assert(exp1 != NULL);
79         char *exp2 = meshlink_export(mesh2);
80         assert(exp2 != NULL);
81         assert(meshlink_import(mesh1, exp2));
82         assert(meshlink_import(mesh2, exp1));
83         free(exp1);
84         free(exp2);
85
86         // Nodes should learn about each other
87         sleep(1);
88
89         // Start the nodes
90         assert(meshlink_start(mesh1));
91         assert(meshlink_start(mesh2));
92
93         // socket structure to be hinted
94         struct sockaddr_in hint;
95         hint.sin_family        = AF_INET;
96         hint.sin_port          = htons(PORT);
97         assert(inet_aton(ADDR, &hint.sin_addr));
98
99         // Getting node handle for the NUT itself
100         meshlink_node_t *node = meshlink_get_node(mesh1, "bar");
101         assert(node != NULL);
102
103         meshlink_hint_address(mesh_handle, node, (struct sockaddr *)&hint);
104
105         int fp;
106         fp = open("./hintconf1/hosts/bar", O_RDONLY);
107         assert(fp >= 0);
108         off_t fsize = lseek(fp, 0, SEEK_END);
109         assert(fsize >= 0);
110         char *buff = (char *) calloc(1, fsize + 1);
111         assert(buff != NULL);
112         assert(lseek(fp, 0, SEEK_SET) == 0);
113         assert(read(fp, buff, fsize) >= 0);
114         buff[fsize] = '\0';
115         assert(close(fp) != -1);
116
117         assert_int_not_equal(strstr(buff, ADDR), NULL);
118
119         free(buff);
120         meshlink_close(mesh1);
121         meshlink_close(mesh2);
122         meshlink_destroy("hintconf1");
123         meshlink_destroy("hintconf2");
124
125         return true;
126 }
127
128
129 int test_meshlink_hint_address(void) {
130         const struct CMUnitTest blackbox_hint_address_tests[] = {
131                 cmocka_unit_test_prestate_setup_teardown(test_case_hint_address_01, NULL, NULL,
132                                 (void *)&test_case_hint_address_01_state)
133         };
134
135         total_tests += sizeof(blackbox_hint_address_tests) / sizeof(blackbox_hint_address_tests[0]);
136
137         return cmocka_run_group_tests(blackbox_hint_address_tests, NULL, NULL);
138 }