]> git.meshlink.io Git - meshlink/blob - test/basic.c
Avoid allocating packet buffers unnecessarily.
[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 not reachable.
41
42         time_t last_reachable;
43         time_t last_unreachable;
44         assert(!meshlink_get_node_reachability(mesh, self, &last_reachable, &last_unreachable));
45         assert(!last_reachable);
46         assert(!last_unreachable);
47
48         // Start and stop the mesh.
49
50         assert(meshlink_start(mesh));
51
52         // Check that we are now reachable
53
54         assert(meshlink_get_node_reachability(mesh, self, &last_reachable, &last_unreachable));
55         assert(last_reachable);
56         assert(!last_unreachable);
57
58         meshlink_stop(mesh);
59
60         // Check that we are no longer reachable.
61
62         assert(!meshlink_get_node_reachability(mesh, self, &last_reachable, &last_unreachable));
63         assert(last_reachable);
64         assert(last_unreachable);
65
66         // Make sure we can start and stop the mesh again.
67
68         assert(meshlink_start(mesh));
69         assert(meshlink_start(mesh));
70         meshlink_stop(mesh);
71         meshlink_stop(mesh);
72
73         // Close the mesh and open it again, now with a different name parameter.
74
75         meshlink_close(mesh);
76         mesh = meshlink_open("basic_conf", "bar", "basic", DEV_CLASS_BACKBONE);
77         assert(mesh);
78
79         self = meshlink_get_self(mesh);
80         assert(self);
81
82         // Check that we remembered we were reachable
83
84         assert(!meshlink_get_node_reachability(mesh, self, &last_reachable, &last_unreachable));
85         assert(last_reachable);
86         assert(last_unreachable);
87
88         // Check that the name is ignored now, and that we still are "foo".
89
90         assert(!meshlink_get_node(mesh, "bar"));
91         self = meshlink_get_self(mesh);
92         assert(self);
93         assert(!strcmp(self->name, "foo"));
94
95         // Start and stop the mesh.
96
97         assert(meshlink_start(mesh));
98         meshlink_stop(mesh);
99         meshlink_close(mesh);
100
101         // Check that messing with the config directory will create a new instance.
102
103         assert(unlink("basic_conf/current/meshlink.conf") == 0);
104         mesh = meshlink_open("basic_conf", "bar", "basic", DEV_CLASS_BACKBONE);
105         assert(mesh);
106         assert(!meshlink_get_node(mesh, "foo"));
107         self = meshlink_get_self(mesh);
108         assert(self);
109         assert(!strcmp(self->name, "bar"));
110         assert(access("basic_conf/new", X_OK) == -1 && errno == ENOENT);
111         meshlink_close(mesh);
112
113         assert(rename("basic_conf/current", "basic_conf/new") == 0);
114         mesh = meshlink_open("basic_conf", "baz", "basic", DEV_CLASS_BACKBONE);
115         assert(mesh);
116         assert(!meshlink_get_node(mesh, "bar"));
117         self = meshlink_get_self(mesh);
118         assert(self);
119         assert(!strcmp(self->name, "baz"));
120         assert(access("basic_conf/new", X_OK) == -1 && errno == ENOENT);
121         meshlink_close(mesh);
122
123         // Destroy the mesh.
124
125         assert(meshlink_destroy("basic_conf"));
126
127         // Check that the configuration directory is completely empty.
128
129         DIR *dir = opendir("basic_conf");
130         assert(dir);
131         struct dirent *ent;
132
133         while((ent = readdir(dir))) {
134                 assert(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."));
135         }
136
137         closedir(dir);
138
139         // Check that we can destroy it again.
140
141         assert(meshlink_destroy("basic_conf"));
142 }