]> git.meshlink.io Git - meshlink/blob - test/basic.c
Add meshlink_get_node_reachability().
[meshlink] / test / basic.c
1 #ifdef NDEBUG
2 #undef NDEBUG
3 #endif
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <sys/time.h>
10 #include <assert.h>
11 #include <dirent.h>
12
13 #include "meshlink.h"
14 #include "utils.h"
15
16 int main() {
17         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
18
19         // Open a new meshlink instance.
20
21         assert(meshlink_destroy("basic_conf"));
22         meshlink_handle_t *mesh = meshlink_open("basic_conf", "foo", "basic", DEV_CLASS_BACKBONE);
23         assert(mesh);
24
25         // Check that we can't open a second instance of the same node.
26
27         meshlink_handle_t *mesh2 = meshlink_open("basic_conf", "foo", "basic", DEV_CLASS_BACKBONE);
28         assert(!mesh2);
29
30         // Check that we cannot destroy an instance that is in use.
31
32         assert(!meshlink_destroy("basic_conf"));
33
34         // Check that our own node exists.
35
36         meshlink_node_t *self = meshlink_get_self(mesh);
37         assert(self);
38         assert(!strcmp(self->name, "foo"));
39
40         // Check that we are reachable.
41
42         assert(meshlink_get_node_reachability(mesh, self, NULL, NULL));
43
44         // Start and stop the mesh.
45
46         assert(meshlink_start(mesh));
47         meshlink_stop(mesh);
48
49         // Check that we are still reachable.
50
51         assert(meshlink_get_node_reachability(mesh, self, NULL, NULL));
52
53         // Make sure we can start and stop the mesh again.
54
55         assert(meshlink_start(mesh));
56         assert(meshlink_start(mesh));
57         meshlink_stop(mesh);
58         meshlink_stop(mesh);
59
60         // Close the mesh and open it again, now with a different name parameter.
61
62         meshlink_close(mesh);
63         mesh = meshlink_open("basic_conf", "bar", "basic", DEV_CLASS_BACKBONE);
64         assert(mesh);
65
66         // Check that the name is ignored now, and that we still are "foo".
67
68         assert(!meshlink_get_node(mesh, "bar"));
69         self = meshlink_get_self(mesh);
70         assert(self);
71         assert(!strcmp(self->name, "foo"));
72
73         // Start and stop the mesh.
74
75         assert(meshlink_start(mesh));
76         meshlink_stop(mesh);
77         meshlink_close(mesh);
78
79         // Check that messing with the config directory will create a new instance.
80
81         assert(unlink("basic_conf/current/meshlink.conf") == 0);
82         mesh = meshlink_open("basic_conf", "bar", "basic", DEV_CLASS_BACKBONE);
83         assert(mesh);
84         assert(!meshlink_get_node(mesh, "foo"));
85         self = meshlink_get_self(mesh);
86         assert(self);
87         assert(!strcmp(self->name, "bar"));
88         assert(access("basic_conf/new", X_OK) == -1 && errno == ENOENT);
89         meshlink_close(mesh);
90
91         assert(rename("basic_conf/current", "basic_conf/new") == 0);
92         mesh = meshlink_open("basic_conf", "baz", "basic", DEV_CLASS_BACKBONE);
93         assert(mesh);
94         assert(!meshlink_get_node(mesh, "bar"));
95         self = meshlink_get_self(mesh);
96         assert(self);
97         assert(!strcmp(self->name, "baz"));
98         assert(access("basic_conf/new", X_OK) == -1 && errno == ENOENT);
99         meshlink_close(mesh);
100
101         // Destroy the mesh.
102
103         assert(meshlink_destroy("basic_conf"));
104
105         // Check that the configuration directory is completely empty.
106
107         DIR *dir = opendir("basic_conf");
108         assert(dir);
109         struct dirent *ent;
110
111         while((ent = readdir(dir))) {
112                 assert(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."));
113         }
114
115         closedir(dir);
116
117         // Check that we can destroy it again.
118
119         assert(meshlink_destroy("basic_conf"));
120 }