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