]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_set_connection_try_cb.c
Add meta-connection attempt callback feature
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_set_connection_try_cb.c
1 /*
2     test_cases_set_connection_try_cb.c -- Execution of specific meshlink black box test cases
3     Copyright (C) 2019  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_set_connection_try_cb.h"
22 #include "../common/test_step.h"
23 #include "../common/common_handlers.h"
24 #include "../../utils.h"
25 #include <assert.h>
26 #include <setjmp.h>
27 #include <cmocka.h>
28 #include <pthread.h>
29
30 static void test_case_set_connection_try_cb_01(void **state);
31 static bool test_set_connection_try_cb_01(void);
32
33 static bool bar_reachable;
34 static int connection_attempts;
35 static struct sync_flag status_changed_cond = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
36 static struct sync_flag connection_attempt_cond = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
37
38 static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *source, bool reachable) {
39         if(!strcmp(mesh->name, "foo") && !strcmp(source->name, "bar")) {
40                 bar_reachable = reachable;
41                 set_sync_flag(&status_changed_cond, true);
42         }
43 }
44
45 /* Meta-connection try callback handler */
46 static void connection_try_cb(meshlink_handle_t *mesh, meshlink_node_t *source) {
47         if(!strcmp(mesh->name, "foo")) {
48                 ++connection_attempts;
49
50                 if(connection_attempts > 3) {
51                         set_sync_flag(&connection_attempt_cond, true);
52                 }
53         }
54 }
55
56 /* Execute set meta connection try callback Test Case # 1 */
57 static void test_case_set_connection_try_cb_01(void **state) {
58         execute_test(test_set_connection_try_cb_01, state);
59 }
60
61 /* Test Steps for meshlink_set_connection_try_cb Test Case # 1
62
63     Test Steps:
64     1. Run foo and bar nodes after exporting and importing node's keys and addresses mutually.
65     2. Close bar node. Wait for connection attempts and cleanup.
66
67     Expected Result:
68     Connection try callback should be invoked initially when foo and bar forms a meta-connection.
69     After closing bar node it should invoke 3 connection try callbacks in span of about 30 seconds.
70 */
71 static bool test_set_connection_try_cb_01(void) {
72         meshlink_destroy("meshlink_conf.1");
73         meshlink_destroy("meshlink_conf.2");
74
75         // Opening foo and bar nodes
76         meshlink_handle_t *mesh1 = meshlink_open("meshlink_conf.1", "foo", "test", DEV_CLASS_STATIONARY);
77         assert(mesh1 != NULL);
78         meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, meshlink_callback_logger);
79         meshlink_enable_discovery(mesh1, false);
80         meshlink_handle_t *mesh2 = meshlink_open("meshlink_conf.2", "bar", "test", DEV_CLASS_STATIONARY);
81         assert(mesh2 != NULL);
82
83         // Set up callback for node status
84         meshlink_set_node_status_cb(mesh1, node_status_cb);
85         meshlink_set_connection_try_cb(mesh1, connection_try_cb);
86
87         // Exporting and Importing mutually
88         char *exp1 = meshlink_export(mesh1);
89         assert(exp1 != NULL);
90         char *exp2 = meshlink_export(mesh2);
91         assert(exp2 != NULL);
92         bool imp1 = meshlink_import(mesh1, exp2);
93         bool imp2 = meshlink_import(mesh2, exp1);
94         free(exp1);
95         free(exp2);
96
97         assert(meshlink_start(mesh1));
98         assert(meshlink_start(mesh2));
99
100         // Wait for foo and bar nodes to join
101         assert(wait_sync_flag(&status_changed_cond, 5));
102         assert(bar_reachable);
103
104         // Joining should in this case raise one connection try callback
105         assert_int_equal(connection_attempts, 1);
106
107         // Close the bar node
108         set_sync_flag(&status_changed_cond, false);
109         meshlink_close(mesh2);
110         assert(wait_sync_flag(&status_changed_cond, 5));
111         assert(!bar_reachable);
112
113         // Wait for additional 3 connection try callbacks
114         time_t attempt_time_start = time(NULL);
115         assert(attempt_time_start != -1);
116         assert_int_equal(wait_sync_flag(&connection_attempt_cond, 60), true);
117
118         // Close bar node and assert on number of callbacks invoked and the time taken.
119         meshlink_close(mesh1);
120         time_t attempt_time_stop = time(NULL);
121         assert(attempt_time_stop != -1);
122         assert_int_equal(connection_attempts, 4);
123         assert_in_range(attempt_time_stop - attempt_time_start, 25, 45);
124
125         // Cleanup
126         meshlink_destroy("meshlink_conf.1");
127         meshlink_destroy("meshlink_conf.2");
128
129         return true;
130 }
131
132 int test_cases_connection_try(void) {
133         black_box_state_t test_case_set_connection_try_cb_01_state = {
134                 .test_case_name = "test_case_set_connection_try_cb_01",
135         };
136
137         const struct CMUnitTest blackbox_connection_try_tests[] = {
138                 cmocka_unit_test_prestate_setup_teardown(test_case_set_connection_try_cb_01, NULL, NULL,
139                                 (void *)&test_case_set_connection_try_cb_01_state),
140         };
141         total_tests += sizeof(blackbox_connection_try_tests) / sizeof(blackbox_connection_try_tests[0]);
142
143         int failed = cmocka_run_group_tests(blackbox_connection_try_tests, NULL, NULL);
144
145         return failed;
146 }