]> git.meshlink.io Git - meshlink/blob - test/invite-join.c
Refactor the non-blackbox test suite.
[meshlink] / test / invite-join.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/time.h>
6 #include <assert.h>
7
8 #include "meshlink.h"
9 #include "utils.h"
10
11 static struct sync_flag baz_reachable;
12
13 static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
14         (void)mesh;
15
16         if(reachable && !strcmp(node->name, "baz")) {
17                 set_sync_flag(&baz_reachable, true);
18         }
19 }
20
21 int main() {
22         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
23
24         assert(meshlink_destroy("invite_join_conf.1"));
25         assert(meshlink_destroy("invite_join_conf.2"));
26         assert(meshlink_destroy("invite_join_conf.3"));
27
28         // Open thee new meshlink instance.
29
30         meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo", "invite-join", DEV_CLASS_BACKBONE);
31         assert(mesh1);
32
33         meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar", "invite-join", DEV_CLASS_BACKBONE);
34         assert(mesh2);
35
36         meshlink_handle_t *mesh3 = meshlink_open("invite_join_conf.3", "quux", "invite-join", DEV_CLASS_BACKBONE);
37         assert(mesh3);
38
39         // Disable local discovery.
40
41         meshlink_enable_discovery(mesh1, false);
42         meshlink_enable_discovery(mesh2, false);
43         meshlink_enable_discovery(mesh3, false);
44
45         // Have the first instance generate invitations.
46
47         meshlink_set_node_status_cb(mesh1, status_cb);
48
49         meshlink_add_address(mesh1, "localhost");
50         char *baz_url = meshlink_invite(mesh1, NULL, "baz");
51         assert(baz_url);
52
53         char *quux_url = meshlink_invite(mesh1, NULL, "quux");
54         assert(quux_url);
55
56         // Have the second instance join the first.
57
58         assert(meshlink_start(mesh1));
59
60         assert(meshlink_join(mesh2, baz_url));
61         assert(meshlink_start(mesh2));
62
63         // Wait for the two to connect.
64
65         assert(wait_sync_flag(&baz_reachable, 20));
66
67         // Wait for UDP communication to become possible.
68
69         int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
70
71         for(int i = 0; i < 10 && !pmtu; i++) {
72                 sleep(1);
73                 pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz"));
74         }
75
76         assert(pmtu);
77
78         // Check that an invitation cannot be used twice
79
80         assert(!meshlink_join(mesh3, baz_url));
81         free(baz_url);
82
83         // Check that nodes cannot join with expired invitations
84
85         meshlink_set_invitation_timeout(mesh1, 0);
86
87         assert(!meshlink_join(mesh3, quux_url));
88         free(quux_url);
89
90         // Check that existing nodes cannot join another mesh
91
92         char *corge_url = meshlink_invite(mesh3, NULL, "corge");
93         assert(corge_url);
94
95         assert(meshlink_start(mesh3));
96
97         meshlink_stop(mesh2);
98
99         assert(!meshlink_join(mesh2, corge_url));
100         free(corge_url);
101
102         // Check that invitations work correctly after changing ports
103
104         meshlink_set_invitation_timeout(mesh1, 86400);
105         meshlink_stop(mesh1);
106         meshlink_stop(mesh3);
107
108         int oldport = meshlink_get_port(mesh1);
109         bool success = false;
110
111         for(int i = 0; !success && i < 100; i++) {
112                 success = meshlink_set_port(mesh1, 0x9000 + rand() % 0x1000);
113         }
114
115         assert(success);
116         int newport = meshlink_get_port(mesh1);
117         assert(oldport != newport);
118
119         assert(meshlink_start(mesh1));
120         quux_url = meshlink_invite(mesh1, NULL, "quux");
121         assert(quux_url);
122
123         // The old port should not be in the invitation URL
124
125         char portstr[10];
126         snprintf(portstr, sizeof(portstr), ":%d", oldport);
127         assert(!strstr(quux_url, portstr));
128
129         // The new port should be in the invitation URL
130
131         snprintf(portstr, sizeof(portstr), ":%d", newport);
132         assert(strstr(quux_url, portstr));
133
134         // The invitation should work
135
136         assert(meshlink_join(mesh3, quux_url));
137         free(quux_url);
138
139         // Clean up.
140
141         meshlink_close(mesh3);
142         meshlink_close(mesh2);
143         meshlink_close(mesh1);
144 }