+#ifdef NDEBUG
+#undef NDEBUG
+#endif
+
#include <stdio.h>
#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/time.h>
+#include <assert.h>
+#include <dirent.h>
#include "meshlink.h"
+#include "utils.h"
+
+int main() {
+ meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
-int main(int argc, char *argv[]) {
// Open a new meshlink instance.
- meshlink_handle_t *mesh = meshlink_open("basic_conf", "foo", "basic");
- if(!mesh) {
- fprintf(stderr, "Could not initialize configuration for foo\n");
- return 1;
- }
+ assert(meshlink_destroy("basic_conf"));
+ meshlink_handle_t *mesh = meshlink_open("basic_conf", "foo", "basic", DEV_CLASS_BACKBONE);
+ assert(mesh);
+
+ // Check that we can't open a second instance of the same node.
+
+ meshlink_handle_t *mesh2 = meshlink_open("basic_conf", "foo", "basic", DEV_CLASS_BACKBONE);
+ assert(!mesh2);
+
+ // Check that we cannot destroy an instance that is in use.
+
+ assert(!meshlink_destroy("basic_conf"));
// Check that our own node exists.
- meshlink_node_t *self = meshlink_get_node(mesh, "foo");
- if(!self) {
- fprintf(stderr, "Foo does not know about itself\n");
- return 1;
- }
- if(strcmp(self->name, "foo")) {
- fprintf(stderr, "Foo thinks its name is %s\n", self->name);
- return 1;
- }
+ meshlink_node_t *self = meshlink_get_self(mesh);
+ assert(self);
+ assert(!strcmp(self->name, "foo"));
+
+ // Check that we are not reachable.
+
+ time_t last_reachable;
+ time_t last_unreachable;
+ assert(!meshlink_get_node_reachability(mesh, self, &last_reachable, &last_unreachable));
+ assert(!last_reachable);
+ assert(!last_unreachable);
// Start and stop the mesh.
- if(!meshlink_start(mesh)) {
- fprintf(stderr, "Foo could not start\n");
- return 1;
- }
+ assert(meshlink_start(mesh));
+
+ // Check that we are now reachable
+
+ assert(meshlink_get_node_reachability(mesh, self, &last_reachable, &last_unreachable));
+ assert(last_reachable);
+ assert(!last_unreachable);
+
meshlink_stop(mesh);
+ // Check that we are no longer reachable.
+
+ assert(!meshlink_get_node_reachability(mesh, self, &last_reachable, &last_unreachable));
+ assert(last_reachable);
+ assert(last_unreachable);
+
// Make sure we can start and stop the mesh again.
- if(!meshlink_start(mesh)) {
- fprintf(stderr, "Foo could not start twice\n");
- return 1;
- }
+ assert(meshlink_start(mesh));
+ assert(meshlink_start(mesh));
+ meshlink_stop(mesh);
meshlink_stop(mesh);
// Close the mesh and open it again, now with a different name parameter.
meshlink_close(mesh);
+ mesh = meshlink_open("basic_conf", "bar", "basic", DEV_CLASS_BACKBONE);
+ assert(mesh);
- // Check that the name is ignored now, and that we still are "foo".
+ self = meshlink_get_self(mesh);
+ assert(self);
- mesh = meshlink_open("basic_conf", "bar", "basic");
- if(!mesh) {
- fprintf(stderr, "Could not open configuration for foo a second time\n");
- return 1;
- }
+ // Check that we remembered we were reachable
- if(meshlink_get_node(mesh, "bar")) {
- fprintf(stderr, "Foo knows about bar, it shouldn't\n");
- return 1;
- }
+ assert(!meshlink_get_node_reachability(mesh, self, &last_reachable, &last_unreachable));
+ assert(last_reachable);
+ assert(last_unreachable);
- self = meshlink_get_node(mesh, "foo");
- if(!self) {
- fprintf(stderr, "Foo doesn't know about itself the second time\n");
- return 1;
- }
- if(strcmp(self->name, "foo")) {
- fprintf(stderr, "Foo thinks its name is %s the second time\n", self->name);
- return 1;
- }
+ // Check that the name is ignored now, and that we still are "foo".
+
+ assert(!meshlink_get_node(mesh, "bar"));
+ self = meshlink_get_self(mesh);
+ assert(self);
+ assert(!strcmp(self->name, "foo"));
// Start and stop the mesh.
- if(!meshlink_start(mesh)) {
- fprintf(stderr, "Foo could not start a third time\n");
- return 1;
- }
+ assert(meshlink_start(mesh));
meshlink_stop(mesh);
+ meshlink_close(mesh);
- // That's it.
+ // Check that messing with the config directory will create a new instance.
+ assert(unlink("basic_conf/current/meshlink.conf") == 0);
+ mesh = meshlink_open("basic_conf", "bar", "basic", DEV_CLASS_BACKBONE);
+ assert(mesh);
+ assert(!meshlink_get_node(mesh, "foo"));
+ self = meshlink_get_self(mesh);
+ assert(self);
+ assert(!strcmp(self->name, "bar"));
+ assert(access("basic_conf/new", X_OK) == -1 && errno == ENOENT);
meshlink_close(mesh);
- return 0;
+ assert(rename("basic_conf/current", "basic_conf/new") == 0);
+ mesh = meshlink_open("basic_conf", "baz", "basic", DEV_CLASS_BACKBONE);
+ assert(mesh);
+ assert(!meshlink_get_node(mesh, "bar"));
+ self = meshlink_get_self(mesh);
+ assert(self);
+ assert(!strcmp(self->name, "baz"));
+ assert(access("basic_conf/new", X_OK) == -1 && errno == ENOENT);
+ meshlink_close(mesh);
+
+ // Destroy the mesh.
+
+ assert(meshlink_destroy("basic_conf"));
+
+ // Check that the configuration directory is completely empty.
+
+ DIR *dir = opendir("basic_conf");
+ assert(dir);
+ struct dirent *ent;
+
+ while((ent = readdir(dir))) {
+ assert(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."));
+ }
+
+ closedir(dir);
+
+ // Check that we can destroy it again.
+
+ assert(meshlink_destroy("basic_conf"));
}