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