]> git.meshlink.io Git - meshlink/blob - test/basic.c
Add support for encrypted storage.
[meshlink] / test / basic.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <errno.h>
5
6 #include "meshlink.h"
7
8 void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
9         static struct timeval tv0;
10         struct timeval tv;
11
12         if(tv0.tv_sec == 0) {
13                 gettimeofday(&tv0, NULL);
14         }
15
16         gettimeofday(&tv, NULL);
17         fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
18
19         if(mesh) {
20                 fprintf(stderr, "(%s) ", mesh->name);
21         }
22
23         fprintf(stderr, "[%d] %s\n", level, text);
24 }
25
26 int main() {
27         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
28
29         // Open a new meshlink instance.
30
31         meshlink_handle_t *mesh = meshlink_open("basic_conf", "foo", "basic", DEV_CLASS_BACKBONE);
32
33         if(!mesh) {
34                 fprintf(stderr, "Could not initialize configuration for foo\n");
35                 return 1;
36         }
37
38         meshlink_set_log_cb(mesh, MESHLINK_DEBUG, log_cb);
39
40         // Check that our own node exists.
41
42         meshlink_node_t *self = meshlink_get_self(mesh);
43
44         if(!self) {
45                 fprintf(stderr, "Foo does not know about itself\n");
46                 return 1;
47         }
48
49         if(strcmp(self->name, "foo")) {
50                 fprintf(stderr, "Foo thinks its name is %s\n", self->name);
51                 return 1;
52         }
53
54         // Start and stop the mesh.
55
56         if(!meshlink_start(mesh)) {
57                 fprintf(stderr, "Foo could not start\n");
58                 return 1;
59         }
60
61         meshlink_stop(mesh);
62
63         // Make sure we can start and stop the mesh again.
64
65         if(!meshlink_start(mesh)) {
66                 fprintf(stderr, "Foo could not start twice\n");
67                 return 1;
68         }
69
70         meshlink_stop(mesh);
71
72         // Close the mesh and open it again, now with a different name parameter.
73
74         meshlink_close(mesh);
75
76         // Check that the name is ignored now, and that we still are "foo".
77
78         mesh = meshlink_open("basic_conf", "bar", "basic", DEV_CLASS_BACKBONE);
79
80         if(!mesh) {
81                 fprintf(stderr, "Could not open configuration for foo a second time\n");
82                 return 1;
83         }
84
85         meshlink_set_log_cb(mesh, MESHLINK_DEBUG, log_cb);
86
87         if(meshlink_get_node(mesh, "bar")) {
88                 fprintf(stderr, "Foo knows about bar, it shouldn't\n");
89                 return 1;
90         }
91
92         self = meshlink_get_self(mesh);
93
94         if(!self) {
95                 fprintf(stderr, "Foo doesn't know about itself the second time\n");
96                 return 1;
97         }
98
99         if(strcmp(self->name, "foo")) {
100                 fprintf(stderr, "Foo thinks its name is %s the second time\n", self->name);
101                 return 1;
102         }
103
104         // Start and stop the mesh.
105
106         if(!meshlink_start(mesh)) {
107                 fprintf(stderr, "Foo could not start a third time\n");
108                 return 1;
109         }
110
111         meshlink_stop(mesh);
112
113         // That's it.
114
115         meshlink_close(mesh);
116
117         // Destroy the mesh.
118
119         if(!meshlink_destroy("basic_conf")) {
120                 fprintf(stderr, "Could not destroy configuration\n");
121                 return 1;
122         }
123
124         if(!access("basic_conf", F_OK) || errno != ENOENT) {
125                 fprintf(stderr, "Configuration not fully destroyed\n");
126                 return 1;
127         }
128
129         return 0;
130 }