]> git.meshlink.io Git - meshlink/blob - test/basic.c
Add a function to destroy a MeshLink instance.
[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 int main(int argc, char *argv[]) {
9         // Open a new meshlink instance.
10
11         meshlink_handle_t *mesh = meshlink_open("basic_conf", "foo", "basic", DEV_CLASS_BACKBONE);
12         if(!mesh) {
13                 fprintf(stderr, "Could not initialize configuration for foo\n");
14                 return 1;
15         }
16
17         // Check that our own node exists.
18
19         meshlink_node_t *self = meshlink_get_node(mesh, "foo");
20         if(!self) {
21                 fprintf(stderr, "Foo does not know about itself\n");
22                 return 1;
23         }
24         if(strcmp(self->name, "foo")) {
25                 fprintf(stderr, "Foo thinks its name is %s\n", self->name);
26                 return 1;
27         }
28
29         // Start and stop the mesh.
30
31         if(!meshlink_start(mesh)) {
32                 fprintf(stderr, "Foo could not start\n");
33                 return 1;
34         }
35         meshlink_stop(mesh);
36
37         // Make sure we can start and stop the mesh again.
38
39         if(!meshlink_start(mesh)) {
40                 fprintf(stderr, "Foo could not start twice\n");
41                 return 1;
42         }
43         meshlink_stop(mesh);
44
45         // Close the mesh and open it again, now with a different name parameter.
46
47         meshlink_close(mesh);
48
49         // Check that the name is ignored now, and that we still are "foo".
50
51         mesh = meshlink_open("basic_conf", "bar", "basic", DEV_CLASS_BACKBONE);
52         if(!mesh) {
53                 fprintf(stderr, "Could not open configuration for foo a second time\n");
54                 return 1;
55         }
56
57         if(meshlink_get_node(mesh, "bar")) {
58                 fprintf(stderr, "Foo knows about bar, it shouldn't\n");
59                 return 1;
60         }
61
62         self = meshlink_get_node(mesh, "foo");
63         if(!self) {
64                 fprintf(stderr, "Foo doesn't know about itself the second time\n");
65                 return 1;
66         }
67         if(strcmp(self->name, "foo")) {
68                 fprintf(stderr, "Foo thinks its name is %s the second time\n", self->name);
69                 return 1;
70         }
71
72         // Start and stop the mesh.
73
74         if(!meshlink_start(mesh)) {
75                 fprintf(stderr, "Foo could not start a third time\n");
76                 return 1;
77         }
78         meshlink_stop(mesh);
79
80         // That's it.
81
82         meshlink_close(mesh);
83
84         // Destroy the mesh.
85
86         if(!meshlink_destroy("basic_conf")) {
87                 fprintf(stderr, "Could not destroy configuration\n");
88                 return 1;
89         }
90
91         if(!access("basic_conf", F_OK) || errno != ENOENT) {
92                 fprintf(stderr, "Configuration not fully destroyed\n");
93                 return 1;
94         }
95
96         return 0;
97 }