]> git.meshlink.io Git - meshlink/blob - test/invite-join.c
1dd6c26da654bc525bef2b446e7b5fb25e4eafce
[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_add_address(mesh1, "localhost"));
54         char *baz_url = meshlink_invite(mesh1, NULL, "baz");
55         assert(baz_url);
56
57         char *quux_url = meshlink_invite(mesh1, NULL, "quux");
58         assert(quux_url);
59
60         // Have the second instance join the first.
61
62         assert(meshlink_start(mesh1));
63
64         assert(meshlink_join(mesh2, baz_url));
65         assert(meshlink_start(mesh2));
66
67         // Wait for the two to connect.
68
69         assert(wait_sync_flag(&baz_reachable, 20));
70
71         // Wait for UDP communication to become possible.
72
73         int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
74
75         for(int i = 0; i < 10 && !pmtu; i++) {
76                 sleep(1);
77                 pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
78         }
79
80         assert(pmtu);
81
82         // Check that an invitation cannot be used twice
83
84         assert(!meshlink_join(mesh3, baz_url));
85         free(baz_url);
86
87         // Check that nodes cannot join with expired invitations
88
89         meshlink_set_invitation_timeout(mesh1, 0);
90
91         assert(!meshlink_join(mesh3, quux_url));
92         free(quux_url);
93
94         // Check that existing nodes cannot join another mesh
95
96         char *corge_url = meshlink_invite(mesh3, NULL, "corge");
97         assert(corge_url);
98
99         assert(meshlink_start(mesh3));
100
101         meshlink_stop(mesh2);
102
103         assert(!meshlink_join(mesh2, corge_url));
104         free(corge_url);
105
106         // Check that invitations work correctly after changing ports
107
108         meshlink_set_invitation_timeout(mesh1, 86400);
109         meshlink_stop(mesh1);
110         meshlink_stop(mesh3);
111
112         int oldport = meshlink_get_port(mesh1);
113         bool success = false;
114
115         for(int i = 0; !success && i < 100; i++) {
116                 success = meshlink_set_port(mesh1, 0x9000 + rand() % 0x1000);
117         }
118
119         assert(success);
120         int newport = meshlink_get_port(mesh1);
121         assert(oldport != newport);
122
123         assert(meshlink_start(mesh1));
124         quux_url = meshlink_invite(mesh1, NULL, "quux");
125         assert(quux_url);
126
127         // The old port should not be in the invitation URL
128
129         char portstr[10];
130         snprintf(portstr, sizeof(portstr), ":%d", oldport);
131         assert(!strstr(quux_url, portstr));
132
133         // The new port should be in the invitation URL
134
135         snprintf(portstr, sizeof(portstr), ":%d", newport);
136         assert(strstr(quux_url, portstr));
137
138         // The invitation should work
139
140         assert(meshlink_join(mesh3, quux_url));
141         free(quux_url);
142
143         // Clean up.
144
145         meshlink_close(mesh3);
146         meshlink_close(mesh2);
147         meshlink_close(mesh1);
148 }