]> git.meshlink.io Git - meshlink-tiny/blob - test/basic.c
Add a metering test.
[meshlink-tiny] / 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-tiny.h"
14 #include "utils.h"
15
16 int main(void) {
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         // Start and stop the mesh.
47
48         assert(meshlink_start(mesh));
49
50         meshlink_stop(mesh);
51
52         // Make sure we can start and stop the mesh again.
53
54         assert(meshlink_start(mesh));
55         assert(meshlink_start(mesh));
56         meshlink_stop(mesh);
57         meshlink_stop(mesh);
58
59         // Close the mesh and open it again, now with a different name parameter.
60
61         meshlink_close(mesh);
62         mesh = meshlink_open("basic_conf", "bar", "basic", DEV_CLASS_BACKBONE);
63         assert(!mesh);
64
65         // Open it without providing a name
66
67         mesh = meshlink_open("basic_conf", NULL, "basic", DEV_CLASS_BACKBONE);
68         assert(mesh);
69
70         self = meshlink_get_self(mesh);
71         assert(self);
72         assert(!strcmp(mesh->name, "foo"));
73         assert(!strcmp(self->name, "foo"));
74
75         // Check that the name is ignored now, and that we still are "foo".
76
77         assert(!meshlink_get_node(mesh, "bar"));
78         self = meshlink_get_self(mesh);
79         assert(self);
80         assert(!strcmp(self->name, "foo"));
81
82         // Start and stop the mesh.
83
84         assert(meshlink_start(mesh));
85         meshlink_stop(mesh);
86         meshlink_close(mesh);
87
88         // Check that messing with the config directory will create a new instance.
89
90         assert(unlink("basic_conf/current/meshlink.conf") == 0);
91         mesh = meshlink_open("basic_conf", "bar", "basic", DEV_CLASS_BACKBONE);
92         assert(mesh);
93         assert(!meshlink_get_node(mesh, "foo"));
94         self = meshlink_get_self(mesh);
95         assert(self);
96         assert(!strcmp(self->name, "bar"));
97         assert(access("basic_conf/new", X_OK) == -1 && errno == ENOENT);
98         meshlink_close(mesh);
99
100         assert(rename("basic_conf/current", "basic_conf/new") == 0);
101         mesh = meshlink_open("basic_conf", "baz", "basic", DEV_CLASS_BACKBONE);
102         assert(mesh);
103         assert(!meshlink_get_node(mesh, "bar"));
104         self = meshlink_get_self(mesh);
105         assert(self);
106         assert(!strcmp(self->name, "baz"));
107         assert(access("basic_conf/new", X_OK) == -1 && errno == ENOENT);
108         meshlink_close(mesh);
109
110         // Destroy the mesh.
111
112         assert(meshlink_destroy("basic_conf"));
113
114         // Check that the configuration directory is completely empty.
115
116         DIR *dir = opendir("basic_conf");
117         assert(dir);
118         struct dirent *ent;
119
120         while((ent = readdir(dir))) {
121                 assert(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."));
122         }
123
124         closedir(dir);
125
126         // Check that we can destroy it again.
127
128         assert(meshlink_destroy("basic_conf"));
129 }