]> git.meshlink.io Git - meshlink/blob - test/port.c
Add a test for changing ports and handling of port conflicts.
[meshlink] / test / port.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 #include <errno.h>
12
13 #include "meshlink.h"
14 #include "devtools.h"
15 #include "utils.h"
16
17
18 int main(void) {
19         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
20
21         meshlink_handle_t *mesh1;
22         meshlink_handle_t *mesh2;
23
24         // Open two instances
25
26         assert(meshlink_destroy("port_conf.1"));
27         assert(meshlink_destroy("port_conf.2"));
28
29         mesh1 = meshlink_open("port_conf.1", "foo", "port", DEV_CLASS_BACKBONE);
30         mesh2 = meshlink_open("port_conf.2", "bar", "port", DEV_CLASS_BACKBONE);
31
32         assert(mesh1);
33         assert(mesh2);
34
35         meshlink_enable_discovery(mesh1, false);
36         meshlink_enable_discovery(mesh2, false);
37
38         int port1 = meshlink_get_port(mesh1);
39         int port2 = meshlink_get_port(mesh2);
40         assert(port1);
41         assert(port2);
42         assert(port1 != port2);
43
44         // bar cannot take foo's port if foo is still open
45         assert(!meshlink_set_port(mesh2, port1));
46
47         // bar can take foo's port of foo is closed
48         meshlink_close(mesh1);
49
50         assert(meshlink_set_port(mesh2, port1));
51         assert(meshlink_get_port(mesh2) == port1);
52
53         // foo can open but will now use a different port
54         mesh1 = meshlink_open("port_conf.1", "foo", "port", DEV_CLASS_BACKBONE);
55         assert(mesh1);
56         int port1b = meshlink_get_port(mesh1);
57         assert(port1b);
58         assert(port1b != port1);
59
60         assert(!meshlink_set_port(mesh1, port1));
61
62         // foo can take over bar's old port
63         assert(meshlink_set_port(mesh1, port2));
64
65         meshlink_close(mesh1);
66         meshlink_close(mesh2);
67 }