2 test_cases_hint_address.c -- Execution of specific meshlink black box test cases
3 Copyright (C) 2018 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
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"
36 #include <sys/socket.h>
37 #include <netinet/in.h>
39 #include <sys/types.h>
40 #include <arpa/inet.h>
45 /* Modify this to change the logging level of Meshlink */
46 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
48 /* Port number used in the structure */
51 /* hint address used in the socket structure */
52 #define ADDR "10.1.1.1"
54 static void test_case_hint_address_01(void **state);
55 static bool test_steps_hint_address_01(void);
57 static black_box_state_t test_case_hint_address_01_state = {
58 .test_case_name = "test_case_hint_address_01",
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);
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);
72 // Create meshlink instance for the nodes
73 meshlink_handle_t *mesh1 = meshlink_open("hintconf1", "nut", "test", DEV_CLASS_STATIONARY);
75 meshlink_handle_t *mesh2 = meshlink_open("hintconf2", "bar", "test", DEV_CLASS_STATIONARY);
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);
80 // importing and exporting mesh meta data
81 char *exp1 = meshlink_export(mesh1);
83 char *exp2 = meshlink_export(mesh2);
85 assert(meshlink_import(mesh1, exp2));
86 assert(meshlink_import(mesh2, exp1));
90 // Nodes should learn about each other
94 assert(meshlink_start(mesh1));
95 assert(meshlink_start(mesh2));
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));
103 // Getting node handle for the NUT itself
104 meshlink_node_t *node = meshlink_get_node(mesh1, "bar");
105 assert(node != NULL);
107 meshlink_hint_address(mesh_handle, node, (struct sockaddr *)&hint);
110 fp = open("./hintconf1/hosts/bar", O_RDONLY);
112 off_t fsize = lseek(fp, 0, SEEK_END);
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);
119 assert(close(fp) != -1);
121 assert_int_not_equal(strstr(buff, ADDR), NULL);
124 meshlink_close(mesh1);
125 meshlink_close(mesh2);
126 assert(meshlink_destroy("hintconf1"));
127 assert(meshlink_destroy("hintconf2"));
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)
139 total_tests += sizeof(blackbox_hint_address_tests) / sizeof(blackbox_hint_address_tests[0]);
141 return cmocka_run_group_tests(blackbox_hint_address_tests, NULL, NULL);