]> git.meshlink.io Git - meshlink/blob - test/basic.c
Allow meshlink_open() to be called with a NULL name.
[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         // Check that the first time we need to supply a name
20
21         assert(meshlink_destroy("basic_conf"));
22
23         meshlink_handle_t *mesh = meshlink_open("basic_conf", NULL, "basic", DEV_CLASS_BACKBONE);
24         assert(!mesh);
25
26         // Open a new meshlink instance.
27
28         mesh = meshlink_open("basic_conf", "foo", "basic", DEV_CLASS_BACKBONE);
29         assert(mesh);
30
31         // Check that we can't open a second instance of the same node.
32
33         meshlink_handle_t *mesh2 = meshlink_open("basic_conf", "foo", "basic", DEV_CLASS_BACKBONE);
34         assert(!mesh2);
35
36         // Check that we cannot destroy an instance that is in use.
37
38         assert(!meshlink_destroy("basic_conf"));
39
40         // Check that our own node exists.
41
42         meshlink_node_t *self = meshlink_get_self(mesh);
43         assert(self);
44         assert(!strcmp(self->name, "foo"));
45
46         // Check that we are not reachable.
47
48         time_t last_reachable;
49         time_t last_unreachable;
50         assert(!meshlink_get_node_reachability(mesh, self, &last_reachable, &last_unreachable));
51         assert(!last_reachable);
52         assert(!last_unreachable);
53
54         // Start and stop the mesh.
55
56         assert(meshlink_start(mesh));
57
58         // Check that we are now reachable
59
60         assert(meshlink_get_node_reachability(mesh, self, &last_reachable, &last_unreachable));
61         assert(last_reachable);
62         assert(!last_unreachable);
63
64         meshlink_stop(mesh);
65
66         // Check that we are no longer reachable.
67
68         assert(!meshlink_get_node_reachability(mesh, self, &last_reachable, &last_unreachable));
69         assert(last_reachable);
70         assert(last_unreachable);
71
72         // Make sure we can start and stop the mesh again.
73
74         assert(meshlink_start(mesh));
75         assert(meshlink_start(mesh));
76         meshlink_stop(mesh);
77         meshlink_stop(mesh);
78
79         // Close the mesh and open it again, now with a different name parameter.
80
81         meshlink_close(mesh);
82         mesh = meshlink_open("basic_conf", "bar", "basic", DEV_CLASS_BACKBONE);
83         assert(!mesh);
84
85         // Open it without providing a name
86
87         mesh = meshlink_open("basic_conf", NULL, "basic", DEV_CLASS_BACKBONE);
88         assert(mesh);
89
90         self = meshlink_get_self(mesh);
91         assert(self);
92         assert(!strcmp(mesh->name, "foo"));
93         assert(!strcmp(self->name, "foo"));
94
95         // Check that we remembered we were reachable
96
97         assert(!meshlink_get_node_reachability(mesh, self, &last_reachable, &last_unreachable));
98         assert(last_reachable);
99         assert(last_unreachable);
100
101         // Check that the name is ignored now, and that we still are "foo".
102
103         assert(!meshlink_get_node(mesh, "bar"));
104         self = meshlink_get_self(mesh);
105         assert(self);
106         assert(!strcmp(self->name, "foo"));
107
108         // Start and stop the mesh.
109
110         assert(meshlink_start(mesh));
111         meshlink_stop(mesh);
112         meshlink_close(mesh);
113
114         // Check that messing with the config directory will create a new instance.
115
116         assert(unlink("basic_conf/current/meshlink.conf") == 0);
117         mesh = meshlink_open("basic_conf", "bar", "basic", DEV_CLASS_BACKBONE);
118         assert(mesh);
119         assert(!meshlink_get_node(mesh, "foo"));
120         self = meshlink_get_self(mesh);
121         assert(self);
122         assert(!strcmp(self->name, "bar"));
123         assert(access("basic_conf/new", X_OK) == -1 && errno == ENOENT);
124         meshlink_close(mesh);
125
126         assert(rename("basic_conf/current", "basic_conf/new") == 0);
127         mesh = meshlink_open("basic_conf", "baz", "basic", DEV_CLASS_BACKBONE);
128         assert(mesh);
129         assert(!meshlink_get_node(mesh, "bar"));
130         self = meshlink_get_self(mesh);
131         assert(self);
132         assert(!strcmp(self->name, "baz"));
133         assert(access("basic_conf/new", X_OK) == -1 && errno == ENOENT);
134         meshlink_close(mesh);
135
136         // Destroy the mesh.
137
138         assert(meshlink_destroy("basic_conf"));
139
140         // Check that the configuration directory is completely empty.
141
142         DIR *dir = opendir("basic_conf");
143         assert(dir);
144         struct dirent *ent;
145
146         while((ent = readdir(dir))) {
147                 assert(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."));
148         }
149
150         closedir(dir);
151
152         // Check that we can destroy it again.
153
154         assert(meshlink_destroy("basic_conf"));
155 }