]> git.meshlink.io Git - meshlink/blob - test/invite-join.c
Allow meshlink_open() to be called with a NULL name.
[meshlink] / test / invite-join.c
1 #ifdef NDEBUG
2 #undef NDEBUG
3 #endif
4
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/time.h>
10 #include <assert.h>
11
12 #include "meshlink.h"
13 #include "utils.h"
14
15 static struct sync_flag baz_reachable;
16
17 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
18         (void)mesh;
19
20         if(reachable && !strcmp(node->name, "baz")) {
21                 set_sync_flag(&baz_reachable, true);
22         }
23 }
24
25 int main() {
26         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
27
28         assert(meshlink_destroy("invite_join_conf.1"));
29         assert(meshlink_destroy("invite_join_conf.2"));
30         assert(meshlink_destroy("invite_join_conf.3"));
31
32         // Open thee new meshlink instance.
33
34         meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo", "invite-join", DEV_CLASS_BACKBONE);
35         assert(mesh1);
36
37         meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar", "invite-join", DEV_CLASS_BACKBONE);
38         assert(mesh2);
39
40         meshlink_handle_t *mesh3 = meshlink_open("invite_join_conf.3", "quux", "invite-join", DEV_CLASS_BACKBONE);
41         assert(mesh3);
42
43         // Disable local discovery.
44
45         meshlink_enable_discovery(mesh1, false);
46         meshlink_enable_discovery(mesh2, false);
47         meshlink_enable_discovery(mesh3, false);
48
49         // Have the first instance generate invitations.
50
51         meshlink_set_node_status_cb(mesh1, status_cb);
52
53         assert(meshlink_set_canonical_address(mesh1, meshlink_get_self(mesh1), "localhost", NULL));
54
55         char *baz_url = meshlink_invite(mesh1, NULL, "baz");
56         assert(baz_url);
57
58         char *quux_url = meshlink_invite(mesh1, NULL, "quux");
59         assert(quux_url);
60
61         // Have the second instance join the first.
62
63         assert(meshlink_start(mesh1));
64
65         assert(meshlink_join(mesh2, baz_url));
66         assert(meshlink_start(mesh2));
67
68         // Wait for the two to connect.
69
70         assert(wait_sync_flag(&baz_reachable, 20));
71
72         // Wait for UDP communication to become possible.
73
74         int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
75
76         for(int i = 0; i < 10 && !pmtu; i++) {
77                 sleep(1);
78                 pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
79         }
80
81         assert(pmtu);
82
83         // Check that an invitation cannot be used twice
84
85         assert(!meshlink_join(mesh3, baz_url));
86         free(baz_url);
87
88         // Check that nodes cannot join with expired invitations
89
90         meshlink_set_invitation_timeout(mesh1, 0);
91
92         assert(!meshlink_join(mesh3, quux_url));
93         free(quux_url);
94
95         // Check that existing nodes cannot join another mesh
96
97         char *corge_url = meshlink_invite(mesh3, NULL, "corge");
98         assert(corge_url);
99
100         assert(meshlink_start(mesh3));
101
102         meshlink_stop(mesh2);
103
104         assert(!meshlink_join(mesh2, corge_url));
105         free(corge_url);
106
107         // Check that invitations work correctly after changing ports
108
109         meshlink_set_invitation_timeout(mesh1, 86400);
110         meshlink_stop(mesh1);
111         meshlink_stop(mesh3);
112
113         int oldport = meshlink_get_port(mesh1);
114         bool success = false;
115
116         for(int i = 0; !success && i < 100; i++) {
117                 success = meshlink_set_port(mesh1, 0x9000 + rand() % 0x1000);
118         }
119
120         assert(success);
121         int newport = meshlink_get_port(mesh1);
122         assert(oldport != newport);
123
124         assert(meshlink_start(mesh1));
125         quux_url = meshlink_invite(mesh1, NULL, "quux");
126         assert(quux_url);
127
128         // The old port should not be in the invitation URL
129
130         char portstr[10];
131         snprintf(portstr, sizeof(portstr), ":%d", oldport);
132         assert(!strstr(quux_url, portstr));
133
134         // The new port should be in the invitation URL
135
136         snprintf(portstr, sizeof(portstr), ":%d", newport);
137         assert(strstr(quux_url, portstr));
138
139         // The invitation should work
140
141         assert(meshlink_join(mesh3, quux_url));
142         free(quux_url);
143
144         // Check that adding duplicate addresses get removed correctly
145
146         assert(meshlink_add_invitation_address(mesh1, "localhost", portstr + 1));
147         corge_url = meshlink_invite(mesh1, NULL, "corge");
148         assert(corge_url);
149         char *localhost = strstr(corge_url, "localhost");
150         assert(localhost);
151         assert(!strstr(localhost + 1, "localhost"));
152         free(corge_url);
153
154         // Check that resetting and adding multiple, different invitation address works
155
156         meshlink_clear_invitation_addresses(mesh1);
157         assert(meshlink_add_invitation_address(mesh1, "1.invalid.", "12345"));
158         assert(meshlink_add_invitation_address(mesh1, "2.invalid.", NULL));
159         assert(meshlink_add_invitation_address(mesh1, "3.invalid.", NULL));
160         assert(meshlink_add_invitation_address(mesh1, "4.invalid.", NULL));
161         assert(meshlink_add_invitation_address(mesh1, "5.invalid.", NULL));
162         char *grault_url = meshlink_invite(mesh1, NULL, "grault");
163         assert(grault_url);
164         localhost = strstr(grault_url, "localhost");
165         assert(localhost);
166         char *invalid1 = strstr(grault_url, "1.invalid.:12345");
167         assert(invalid1);
168         char *invalid5 = strstr(grault_url, "5.invalid.");
169         assert(invalid5);
170
171         // Check that explicitly added invitation addresses come before others, in the order they were specified.
172
173         assert(invalid1 < invalid5);
174         assert(invalid5 < localhost);
175         free(grault_url);
176
177         // Clean up.
178
179         meshlink_close(mesh3);
180         meshlink_close(mesh2);
181         meshlink_close(mesh1);
182 }