17 static struct sync_flag baz_reachable;
18 static struct sync_flag seven_reachable;
19 static struct sync_flag commits_first_flag;
21 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
24 if(reachable && !strcmp(node->name, "baz")) {
25 set_sync_flag(&baz_reachable, true);
28 if(reachable && !strcmp(node->name, "seven")) {
29 set_sync_flag(&seven_reachable, true);
33 static void invitee_commits_first_cb(bool inviter_first) {
34 // Check that eight has committed foo's host config file, but foo hasn't committed eight's
35 assert(access("invite_join_conf.8/current/hosts/foo", F_OK) == 0);
36 assert(access("invite_join_conf.1/current/hosts/eight", F_OK) == -1 && errno == ENOENT);
37 set_sync_flag(&commits_first_flag, !inviter_first);
40 static void inviter_commits_first_cb(bool inviter_first) {
41 // Check that foo has committed nine's host config file, but nine hasn't committed foo's
42 assert(access("invite_join_conf.1/current/hosts/nine", F_OK) == 0);
43 assert(access("invite_join_conf.9/current/hosts/foo", F_OK) == -1 && errno == ENOENT);
44 set_sync_flag(&commits_first_flag, inviter_first);
48 init_sync_flag(&baz_reachable);
49 init_sync_flag(&seven_reachable);
50 init_sync_flag(&commits_first_flag);
52 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
54 assert(meshlink_destroy("invite_join_conf.1"));
55 assert(meshlink_destroy("invite_join_conf.2"));
56 assert(meshlink_destroy("invite_join_conf.3"));
57 assert(meshlink_destroy("invite_join_conf.4"));
58 assert(meshlink_destroy("invite_join_conf.5"));
59 assert(meshlink_destroy("invite_join_conf.6"));
60 assert(meshlink_destroy("invite_join_conf.7"));
61 assert(meshlink_destroy("invite_join_conf.8"));
62 assert(meshlink_destroy("invite_join_conf.9"));
64 // Open thee new meshlink instance.
66 meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo", "invite-join", DEV_CLASS_BACKBONE);
69 meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar", "invite-join", DEV_CLASS_BACKBONE);
72 meshlink_handle_t *mesh3 = meshlink_open("invite_join_conf.3", "quux", "invite-join", DEV_CLASS_BACKBONE);
75 // Disable local discovery.
77 meshlink_enable_discovery(mesh1, false);
78 meshlink_enable_discovery(mesh2, false);
79 meshlink_enable_discovery(mesh3, false);
81 // Have the first instance generate invitations.
83 meshlink_set_node_status_cb(mesh1, status_cb);
85 assert(meshlink_set_canonical_address(mesh1, meshlink_get_self(mesh1), "localhost", NULL));
87 char *baz_url = meshlink_invite(mesh1, NULL, "baz");
90 char *quux_url = meshlink_invite(mesh1, NULL, "quux");
93 // Check that the second instances cannot join if it is already started
95 assert(meshlink_start(mesh1));
96 assert(meshlink_start(mesh2));
97 meshlink_errno = MESHLINK_OK;
98 assert(!meshlink_join(mesh2, baz_url));
99 assert(meshlink_errno = MESHLINK_EINVAL);
101 // Have the second instance join the first.
103 meshlink_stop(mesh2);
104 assert(meshlink_join(mesh2, baz_url));
105 assert(meshlink_start(mesh2));
107 // Wait for the two to connect.
109 assert(wait_sync_flag(&baz_reachable, 20));
111 // Wait for UDP communication to become possible.
113 int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
115 for(int i = 0; i < 10 && !pmtu; i++) {
117 pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
122 // Check that an invitation cannot be used twice
124 assert(!meshlink_join(mesh3, baz_url));
127 // Check that nodes cannot join with expired invitations
129 meshlink_set_invitation_timeout(mesh1, 0);
131 assert(!meshlink_join(mesh3, quux_url));
134 // Check that existing nodes cannot join another mesh
136 char *corge_url = meshlink_invite(mesh3, NULL, "corge");
139 assert(meshlink_start(mesh3));
141 meshlink_stop(mesh2);
143 assert(!meshlink_join(mesh2, corge_url));
146 // Check that invitations work correctly after changing ports
148 meshlink_set_invitation_timeout(mesh1, 86400);
149 meshlink_stop(mesh1);
150 meshlink_stop(mesh3);
152 int oldport = meshlink_get_port(mesh1);
153 bool success = false;
155 for(int i = 0; !success && i < 100; i++) {
156 success = meshlink_set_port(mesh1, 0x9000 + rand() % 0x1000);
160 int newport = meshlink_get_port(mesh1);
161 assert(oldport != newport);
163 assert(meshlink_set_canonical_address(mesh1, meshlink_get_self(mesh1), "localhost", NULL));
165 assert(meshlink_start(mesh1));
166 quux_url = meshlink_invite(mesh1, NULL, "quux");
169 // The old port should not be in the invitation URL
172 snprintf(portstr, sizeof(portstr), ":%d", oldport);
173 assert(!strstr(quux_url, portstr));
175 // The new port should be in the invitation URL
177 snprintf(portstr, sizeof(portstr), ":%d", newport);
178 assert(strstr(quux_url, portstr));
180 // The invitation should work
182 assert(meshlink_join(mesh3, quux_url));
185 // Check that adding duplicate addresses get removed correctly
187 assert(meshlink_add_invitation_address(mesh1, "localhost", portstr + 1));
188 corge_url = meshlink_invite(mesh1, NULL, "corge");
190 char *localhost = strstr(corge_url, "localhost");
192 assert(!strstr(localhost + 1, "localhost"));
195 // Check that resetting and adding multiple, different invitation address works
197 meshlink_clear_invitation_addresses(mesh1);
198 assert(meshlink_add_invitation_address(mesh1, "1.invalid.", "12345"));
199 assert(meshlink_add_invitation_address(mesh1, "2.invalid.", NULL));
200 assert(meshlink_add_invitation_address(mesh1, "3.invalid.", NULL));
201 assert(meshlink_add_invitation_address(mesh1, "4.invalid.", NULL));
202 assert(meshlink_add_invitation_address(mesh1, "5.invalid.", NULL));
203 char *grault_url = meshlink_invite(mesh1, NULL, "grault");
205 localhost = strstr(grault_url, "localhost");
207 char *invalid1 = strstr(grault_url, "1.invalid.:12345");
209 char *invalid5 = strstr(grault_url, "5.invalid.");
212 // Check that explicitly added invitation addresses come before others, in the order they were specified.
214 assert(invalid1 < invalid5);
215 assert(invalid5 < localhost);
218 // Check inviting nodes into a submesh
220 assert(!meshlink_get_node_submesh(mesh1, meshlink_get_self(mesh1)));
222 meshlink_handle_t *mesh4 = meshlink_open("invite_join_conf.4", "four", "invite-join", DEV_CLASS_BACKBONE);
223 meshlink_handle_t *mesh5 = meshlink_open("invite_join_conf.5", "five", "invite-join", DEV_CLASS_BACKBONE);
224 meshlink_handle_t *mesh6 = meshlink_open("invite_join_conf.6", "six", "invite-join", DEV_CLASS_BACKBONE);
229 meshlink_enable_discovery(mesh4, false);
230 meshlink_enable_discovery(mesh5, false);
231 meshlink_enable_discovery(mesh6, false);
233 meshlink_submesh_t *submesh1 = meshlink_submesh_open(mesh1, "submesh1");
234 meshlink_submesh_t *submesh2 = meshlink_submesh_open(mesh1, "submesh2");
238 char *four_url = meshlink_invite(mesh1, submesh1, mesh4->name);
239 char *five_url = meshlink_invite(mesh1, submesh1, mesh5->name);
240 char *six_url = meshlink_invite(mesh1, submesh2, mesh6->name);
245 assert(meshlink_join(mesh4, four_url));
246 assert(meshlink_join(mesh5, five_url));
247 assert(meshlink_join(mesh6, six_url));
253 assert(meshlink_start(mesh2));
254 assert(meshlink_start(mesh4));
255 assert(meshlink_start(mesh5));
256 assert(meshlink_start(mesh6));
258 // Check that each node knows in which submesh it is
260 meshlink_submesh_t *mesh4_submesh = meshlink_get_node_submesh(mesh4, meshlink_get_self(mesh4));
261 meshlink_submesh_t *mesh5_submesh = meshlink_get_node_submesh(mesh4, meshlink_get_self(mesh5));
262 meshlink_submesh_t *mesh6_submesh = meshlink_get_node_submesh(mesh6, meshlink_get_self(mesh6));
263 assert(mesh4_submesh);
264 assert(mesh5_submesh);
265 assert(mesh6_submesh);
266 assert(!strcmp(mesh4_submesh->name, "submesh1"));
267 assert(!strcmp(mesh5_submesh->name, "submesh1"));
268 assert(!strcmp(mesh6_submesh->name, "submesh2"));
270 // Wait for nodes to connect, and check that foo sees the right submeshes
273 meshlink_node_t *mesh1_four = meshlink_get_node(mesh1, mesh4->name);
274 meshlink_node_t *mesh1_six = meshlink_get_node(mesh1, mesh6->name);
275 assert(meshlink_get_node_submesh(mesh1, meshlink_get_self(mesh1)) == NULL);
276 assert(meshlink_get_node_submesh(mesh1, mesh1_four) == submesh1);
277 assert(meshlink_get_node_submesh(mesh1, mesh1_six) == submesh2);
279 // Check that the new invitees still have the right submesh information
281 meshlink_node_t *mesh4_four = meshlink_get_node(mesh4, mesh4->name);
282 meshlink_node_t *mesh4_five = meshlink_get_node(mesh4, mesh5->name);
283 meshlink_node_t *mesh6_six = meshlink_get_node(mesh6, mesh6->name);
284 assert(meshlink_get_node_submesh(mesh4, mesh4_four) == mesh4_submesh);
285 assert(meshlink_get_node_submesh(mesh4, mesh4_five) == mesh4_submesh);
286 assert(meshlink_get_node_submesh(mesh6, mesh6_six) == mesh6_submesh);
288 // Check that bar can see all the nodes in submeshes and vice versa
290 assert(meshlink_get_node(mesh2, mesh4->name));
291 assert(meshlink_get_node(mesh2, mesh5->name));
292 assert(meshlink_get_node(mesh2, mesh6->name));
293 assert(meshlink_get_node(mesh4, mesh2->name));
294 assert(meshlink_get_node(mesh5, mesh2->name));
295 assert(meshlink_get_node(mesh6, mesh2->name));
297 // Check that four and five can see each other
299 assert(meshlink_get_node(mesh4, mesh5->name));
300 assert(meshlink_get_node(mesh5, mesh4->name));
302 // Check that the nodes in different submeshes cannot see each other
304 assert(!meshlink_get_node(mesh4, mesh6->name));
305 assert(!meshlink_get_node(mesh5, mesh6->name));
306 assert(!meshlink_get_node(mesh6, mesh4->name));
307 assert(!meshlink_get_node(mesh6, mesh5->name));
309 // Check that bar sees the right submesh information for the nodes in submeshes
311 meshlink_submesh_t *mesh2_four_submesh = meshlink_get_node_submesh(mesh2, meshlink_get_node(mesh2, mesh4->name));
312 meshlink_submesh_t *mesh2_five_submesh = meshlink_get_node_submesh(mesh2, meshlink_get_node(mesh2, mesh5->name));
313 meshlink_submesh_t *mesh2_six_submesh = meshlink_get_node_submesh(mesh2, meshlink_get_node(mesh2, mesh6->name));
314 assert(mesh2_four_submesh);
315 assert(mesh2_five_submesh);
316 assert(mesh2_six_submesh);
317 assert(!strcmp(mesh2_four_submesh->name, "submesh1"));
318 assert(!strcmp(mesh2_five_submesh->name, "submesh1"));
319 assert(!strcmp(mesh2_six_submesh->name, "submesh2"));
321 // Test case #2: check invalid parameters
323 meshlink_handle_t *mesh7 = meshlink_open("invite_join_conf.7", "seven", "invite-join", DEV_CLASS_BACKBONE);
325 meshlink_enable_discovery(mesh7, false);
326 char *seven_url = meshlink_invite(mesh1, NULL, "seven");
329 meshlink_errno = MESHLINK_OK;
330 assert(!meshlink_invite(NULL, NULL, "seven"));
331 assert(meshlink_errno == MESHLINK_EINVAL);
333 meshlink_errno = MESHLINK_OK;
334 assert(!meshlink_invite(mesh1, NULL, NULL));
335 assert(meshlink_errno == MESHLINK_EINVAL);
337 meshlink_errno = MESHLINK_OK;
338 assert(!meshlink_invite(mesh1, NULL, ""));
339 assert(meshlink_errno == MESHLINK_EINVAL);
341 meshlink_errno = MESHLINK_OK;
342 assert(!meshlink_join(NULL, seven_url));
343 assert(meshlink_errno == MESHLINK_EINVAL);
345 meshlink_errno = MESHLINK_OK;
346 assert(!meshlink_join(mesh7, NULL));
347 assert(meshlink_errno == MESHLINK_EINVAL);
349 meshlink_errno = MESHLINK_OK;
350 assert(!meshlink_join(mesh7, ""));
351 assert(meshlink_errno == MESHLINK_EINVAL);
353 // Test case #3 and #4: check persistence of inviter and invitee
355 assert(meshlink_join(mesh7, seven_url));
357 meshlink_close(mesh1);
358 meshlink_stop(mesh2);
359 meshlink_stop(mesh3);
360 meshlink_stop(mesh4);
361 meshlink_stop(mesh5);
362 meshlink_stop(mesh6);
363 meshlink_close(mesh7);
364 mesh1 = meshlink_open("invite_join_conf.1", "foo", "invite-join", DEV_CLASS_BACKBONE);
365 mesh7 = meshlink_open("invite_join_conf.7", "seven", "invite-join", DEV_CLASS_BACKBONE);
368 meshlink_enable_discovery(mesh1, false);
369 meshlink_enable_discovery(mesh7, false);
370 meshlink_set_node_status_cb(mesh1, status_cb);
371 assert(meshlink_start(mesh1));
372 assert(meshlink_start(mesh7));
373 assert(wait_sync_flag(&seven_reachable, 5));
374 meshlink_stop(mesh7);
376 // Test case #6 and #7: check invalid inviter_commits_first combinations
378 meshlink_handle_t *mesh8 = meshlink_open("invite_join_conf.8", "eight", "invite-join", DEV_CLASS_BACKBONE);
380 meshlink_enable_discovery(mesh8, false);
381 char *eight_url = meshlink_invite(mesh1, NULL, "eight");
383 meshlink_set_inviter_commits_first(mesh1, true);
384 meshlink_set_inviter_commits_first(mesh8, false);
385 assert(!meshlink_join(mesh8, eight_url));
388 eight_url = meshlink_invite(mesh1, NULL, "eight");
389 meshlink_set_inviter_commits_first(mesh1, false);
390 meshlink_set_inviter_commits_first(mesh8, true);
391 assert(!meshlink_join(mesh8, eight_url));
394 // Test case #5: test invitee committing first scenario
396 eight_url = meshlink_invite(mesh1, NULL, "eight");
397 meshlink_set_inviter_commits_first(mesh1, false);
398 meshlink_set_inviter_commits_first(mesh8, false);
399 devtool_set_inviter_commits_first = invitee_commits_first_cb;
400 assert(meshlink_join(mesh8, eight_url));
402 assert(wait_sync_flag(&commits_first_flag, 5));
404 // Test case #6: test inviter committing first scenario
406 meshlink_handle_t *mesh9 = meshlink_open("invite_join_conf.9", "nine", "invite-join", DEV_CLASS_BACKBONE);
408 meshlink_enable_discovery(mesh9, false);
409 char *nine_url = meshlink_invite(mesh1, NULL, "nine");
410 meshlink_set_inviter_commits_first(mesh1, true);
411 meshlink_set_inviter_commits_first(mesh9, true);
412 devtool_set_inviter_commits_first = inviter_commits_first_cb;
413 reset_sync_flag(&commits_first_flag);
414 assert(meshlink_join(mesh9, nine_url));
416 assert(wait_sync_flag(&commits_first_flag, 5));
420 meshlink_close(mesh9);
421 meshlink_close(mesh8);
422 meshlink_close(mesh7);
423 meshlink_close(mesh6);
424 meshlink_close(mesh5);
425 meshlink_close(mesh4);
426 meshlink_close(mesh3);
427 meshlink_close(mesh2);
428 meshlink_close(mesh1);