]> git.meshlink.io Git - meshlink-tiny/blob - test/basicpp.cpp
Add a metering test.
[meshlink-tiny] / test / basicpp.cpp
1 #include <cstring>
2 #include <iostream>
3 #include <unistd.h>
4 #include <cerrno>
5 #include <cassert>
6 #include <dirent.h>
7
8 #include "meshlink-tiny++.h"
9
10 using namespace std;
11
12 int main(void) {
13         assert(meshlink::destroy("basicpp_conf"));
14
15         // Open a new meshlink instance.
16
17         {
18                 meshlink::mesh mesh("basicpp_conf", "foo", "basicpp", DEV_CLASS_BACKBONE);
19                 assert(mesh.isOpen());
20
21                 // Check that our own node exists.
22
23                 meshlink::node *self = mesh.get_self();
24                 assert(self);
25                 assert(!strcmp(self->name, "foo"));
26
27                 // Start and stop the mesh.
28
29                 assert(mesh.start());
30                 mesh.stop();
31
32                 // Make sure we can start and stop the mesh again.
33
34                 assert(mesh.start());
35                 mesh.stop();
36
37                 // Close the mesh and open it again, now with a different name parameter.
38
39                 mesh.close();
40                 assert(!mesh.open("basicpp_conf", "bar", "basicpp", DEV_CLASS_BACKBONE));
41
42                 // Open it without giving a name.
43
44                 assert(mesh.open("basicpp_conf", nullptr, "basicpp", DEV_CLASS_BACKBONE));
45
46                 // Check that the name is ignored now, and that we still are "foo".
47
48                 self = mesh.get_self();
49                 assert(self);
50                 assert(!strcmp(self->name, "foo"));
51
52                 // Start and stop the mesh.
53
54                 assert(mesh.start());
55                 mesh.stop();
56         }
57
58         // Destroy the mesh.
59
60         assert(meshlink::destroy("basicpp_conf"));
61
62         DIR *dir = opendir("basicpp_conf");
63         assert(dir);
64         struct dirent *ent;
65         while((ent = readdir(dir))) {
66                 assert(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."));
67         }
68         closedir(dir);
69
70         return 0;
71 }