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.
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"
32 #include <sys/socket.h>
33 #include <netinet/in.h>
35 #include <sys/types.h>
36 #include <arpa/inet.h>
41 /* Modify this to change the logging level of Meshlink */
42 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
44 /* Port number used in the structure */
47 /* hint address used in the socket structure */
48 #define ADDR "10.1.1.1"
50 static void test_case_hint_address_01(void **state);
51 static bool test_steps_hint_address_01(void);
53 static black_box_state_t test_case_hint_address_01_state = {
54 .test_case_name = "test_case_hint_address_01",
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);
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);
68 // Create meshlink instance for the nodes
69 meshlink_handle_t *mesh1 = meshlink_open("hintconf1", "nut", "test", DEV_CLASS_STATIONARY);
71 meshlink_handle_t *mesh2 = meshlink_open("hintconf2", "bar", "test", DEV_CLASS_STATIONARY);
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);
76 // importing and exporting mesh meta data
77 char *exp1 = meshlink_export(mesh1);
79 char *exp2 = meshlink_export(mesh2);
81 assert(meshlink_import(mesh1, exp2));
82 assert(meshlink_import(mesh2, exp1));
86 // Nodes should learn about each other
90 assert(meshlink_start(mesh1));
91 assert(meshlink_start(mesh2));
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));
99 // Getting node handle for the NUT itself
100 meshlink_node_t *node = meshlink_get_node(mesh1, "bar");
101 assert(node != NULL);
103 meshlink_hint_address(mesh_handle, node, (struct sockaddr *)&hint);
106 fp = open("./hintconf1/hosts/bar", O_RDONLY);
108 off_t fsize = lseek(fp, 0, SEEK_END);
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);
115 assert(close(fp) != -1);
117 assert_int_not_equal(strstr(buff, ADDR), NULL);
120 meshlink_close(mesh1);
121 meshlink_close(mesh2);
122 meshlink_destroy("hintconf1");
123 meshlink_destroy("hintconf2");
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)
135 total_tests += sizeof(blackbox_hint_address_tests) / sizeof(blackbox_hint_address_tests[0]);
137 return cmocka_run_group_tests(blackbox_hint_address_tests, NULL, NULL);