]> git.meshlink.io Git - meshlink/commitdiff
Add a test for the various storage policies.
authorGuus Sliepen <guus@meshlink.io>
Wed, 3 Feb 2021 22:31:18 +0000 (23:31 +0100)
committerGuus Sliepen <guus@meshlink.io>
Fri, 5 Feb 2021 08:38:05 +0000 (09:38 +0100)
test/Makefile.am
test/storage-policy.c [new file with mode: 0644]

index e398eeb3bf8a3a5208fc053f1f44dad3ce832533..0dd561e875d38eb7ac8157bf42eaba95bbc468e8 100644 (file)
@@ -20,6 +20,7 @@ TESTS = \
        invite-join \
        meta-connections \
        sign-verify \
+       storage-policy \
        trio \
        trio2 \
        utcp-benchmark \
@@ -61,6 +62,7 @@ check_PROGRAMS = \
        invite-join \
        meta-connections \
        sign-verify \
+       storage-policy \
        stream \
        trio \
        trio2
@@ -138,6 +140,9 @@ meta_connections_LDADD = $(top_builddir)/src/libmeshlink.la
 sign_verify_SOURCES = sign-verify.c utils.c utils.h
 sign_verify_LDADD = $(top_builddir)/src/libmeshlink.la
 
+storage_policy_SOURCES = storage-policy.c utils.c utils.h
+storage_policy_LDADD = $(top_builddir)/src/libmeshlink.la
+
 trio_SOURCES = trio.c utils.c utils.h
 trio_LDADD = $(top_builddir)/src/libmeshlink.la
 
diff --git a/test/storage-policy.c b/test/storage-policy.c
new file mode 100644 (file)
index 0000000..f9593c8
--- /dev/null
@@ -0,0 +1,154 @@
+#ifdef NDEBUG
+#undef NDEBUG
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/time.h>
+#include <assert.h>
+
+#include "meshlink.h"
+#include "utils.h"
+
+int main(void) {
+       meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
+
+       meshlink_handle_t *mesh1;
+       meshlink_handle_t *mesh2;
+
+       // Open two instances
+
+       assert(meshlink_destroy("storage-policy_conf.1"));
+       assert(meshlink_destroy("storage-policy_conf.2"));
+
+       mesh1 = meshlink_open("storage-policy_conf.1", "foo", "storage-policy", DEV_CLASS_BACKBONE);
+       mesh2 = meshlink_open("storage-policy_conf.2", "bar", "storage-policy", DEV_CLASS_BACKBONE);
+       assert(mesh1);
+       assert(mesh2);
+       meshlink_enable_discovery(mesh1, false);
+       meshlink_enable_discovery(mesh2, false);
+       meshlink_set_storage_policy(mesh1, MESHLINK_STORAGE_DISABLED);
+       meshlink_set_storage_policy(mesh2, MESHLINK_STORAGE_DISABLED);
+
+       // Exchange data
+
+       char *export1 = meshlink_export(mesh1);
+       char *export2 = meshlink_export(mesh2);
+
+       assert(export1);
+       assert(export2);
+
+       assert(meshlink_import(mesh1, export2));
+       assert(meshlink_import(mesh2, export1));
+
+       // Check that they know each other
+
+       assert(meshlink_get_node(mesh1, "bar"));
+       assert(meshlink_get_node(mesh2, "foo"));
+
+       start_meshlink_pair(mesh1, mesh2);
+
+       // Close the instances and reopen them.
+
+       close_meshlink_pair(mesh1, mesh2);
+
+       mesh1 = meshlink_open("storage-policy_conf.1", "foo", "storage-policy", DEV_CLASS_BACKBONE);
+       mesh2 = meshlink_open("storage-policy_conf.2", "bar", "storage-policy", DEV_CLASS_BACKBONE);
+       assert(mesh1);
+       assert(mesh2);
+       meshlink_enable_discovery(mesh1, false);
+       meshlink_enable_discovery(mesh2, false);
+       meshlink_set_storage_policy(mesh1, MESHLINK_STORAGE_KEYS_ONLY);
+       meshlink_set_storage_policy(mesh2, MESHLINK_STORAGE_KEYS_ONLY);
+
+       // Check that the nodes no longer know each other
+
+       assert(!meshlink_get_node(mesh1, "bar"));
+       assert(!meshlink_get_node(mesh2, "foo"));
+
+       // Exchange data again
+
+       assert(meshlink_import(mesh1, export2));
+       assert(meshlink_import(mesh2, export1));
+
+       free(export1);
+       free(export2);
+
+       // Close the instances and reopen them.
+
+       close_meshlink_pair(mesh1, mesh2);
+
+       mesh1 = meshlink_open("storage-policy_conf.1", "foo", "storage-policy", DEV_CLASS_BACKBONE);
+       mesh2 = meshlink_open("storage-policy_conf.2", "bar", "storage-policy", DEV_CLASS_BACKBONE);
+       assert(mesh1);
+       assert(mesh2);
+       meshlink_enable_discovery(mesh1, false);
+       meshlink_enable_discovery(mesh2, false);
+       meshlink_set_storage_policy(mesh1, MESHLINK_STORAGE_KEYS_ONLY);
+       meshlink_set_storage_policy(mesh2, MESHLINK_STORAGE_KEYS_ONLY);
+
+       // Check that the nodes know each other
+
+       assert(meshlink_get_node(mesh1, "bar"));
+       assert(meshlink_get_node(mesh2, "foo"));
+
+       // Check that we update reachability
+
+       time_t last_reachable;
+       time_t last_unreachable;
+       assert(!meshlink_get_node_reachability(mesh1, meshlink_get_node(mesh1, "bar"), &last_reachable, &last_unreachable));
+       assert(!last_reachable);
+       assert(!last_unreachable);
+
+       start_meshlink_pair(mesh1, mesh2);
+       stop_meshlink_pair(mesh1, mesh2);
+
+       assert(!meshlink_get_node_reachability(mesh1, meshlink_get_node(mesh1, "bar"), &last_reachable, &last_unreachable));
+       assert(last_reachable);
+       assert(last_unreachable);
+
+       // But have not stored it
+
+       close_meshlink_pair(mesh1, mesh2);
+
+       mesh1 = meshlink_open("storage-policy_conf.1", "foo", "storage-policy", DEV_CLASS_BACKBONE);
+       mesh2 = meshlink_open("storage-policy_conf.2", "bar", "storage-policy", DEV_CLASS_BACKBONE);
+       assert(mesh1);
+       assert(mesh2);
+       meshlink_enable_discovery(mesh1, false);
+       meshlink_enable_discovery(mesh2, false);
+       meshlink_set_storage_policy(mesh1, MESHLINK_STORAGE_KEYS_ONLY);
+       meshlink_set_storage_policy(mesh2, MESHLINK_STORAGE_KEYS_ONLY);
+
+       assert(meshlink_get_node(mesh1, "bar"));
+       assert(meshlink_get_node(mesh2, "foo"));
+
+       assert(!meshlink_get_node_reachability(mesh1, meshlink_get_node(mesh1, "bar"), &last_reachable, &last_unreachable));
+       assert(!last_reachable);
+       assert(!last_unreachable);
+
+       // Check that if we change back to STORAGE_ENABLED right before closing, pending changes are still saved
+
+       start_meshlink_pair(mesh1, mesh2);
+       stop_meshlink_pair(mesh1, mesh2);
+
+       meshlink_set_storage_policy(mesh1, MESHLINK_STORAGE_ENABLED);
+       meshlink_set_storage_policy(mesh2, MESHLINK_STORAGE_ENABLED);
+
+       close_meshlink_pair(mesh1, mesh2);
+
+       mesh1 = meshlink_open("storage-policy_conf.1", "foo", "storage-policy", DEV_CLASS_BACKBONE);
+       mesh2 = meshlink_open("storage-policy_conf.2", "bar", "storage-policy", DEV_CLASS_BACKBONE);
+       assert(mesh1);
+       assert(mesh2);
+       meshlink_enable_discovery(mesh1, false);
+       meshlink_enable_discovery(mesh2, false);
+
+       assert(!meshlink_get_node_reachability(mesh1, meshlink_get_node(mesh1, "bar"), &last_reachable, &last_unreachable));
+       assert(last_reachable);
+       assert(last_unreachable);
+
+       // That's it.
+
+       close_meshlink_pair(mesh1, mesh2);
+}