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