]> git.meshlink.io Git - meshlink-tiny/blob - test/storage-policy.c
Add a metering test.
[meshlink-tiny] / test / storage-policy.c
1 #ifdef NDEBUG
2 #undef NDEBUG
3 #endif
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <sys/time.h>
8 #include <assert.h>
9
10 #include "full.h"
11 #include "meshlink-tiny.h"
12 #include "utils.h"
13
14 int main(void) {
15         init_full();
16
17         full_meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
18         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
19
20         meshlink_handle_t *mesh1;
21         meshlink_handle_t *mesh2;
22
23         // Open two instances
24
25         assert(full_meshlink_destroy("storage-policy_conf.1"));
26         assert(meshlink_destroy("storage-policy_conf.2"));
27
28         mesh1 = full_meshlink_open("storage-policy_conf.1", "foo", "storage-policy", DEV_CLASS_BACKBONE);
29         mesh2 = meshlink_open("storage-policy_conf.2", "bar", "storage-policy", DEV_CLASS_BACKBONE);
30         assert(mesh1);
31         assert(mesh2);
32         full_meshlink_set_storage_policy(mesh1, MESHLINK_STORAGE_DISABLED);
33         meshlink_set_storage_policy(mesh2, MESHLINK_STORAGE_DISABLED);
34
35         // Exchange data
36
37         char *export1 = full_meshlink_export(mesh1);
38         char *export2 = meshlink_export(mesh2);
39
40         assert(export1);
41         assert(export2);
42
43         assert(full_meshlink_import(mesh1, export2));
44         assert(meshlink_import(mesh2, export1));
45
46         // Check that they know each other
47
48         assert(full_meshlink_get_node(mesh1, "bar"));
49         assert(meshlink_get_node(mesh2, "foo"));
50
51         start_full_tiny_pair(mesh1, mesh2);
52
53         // Close the instances and reopen them.
54
55         close_full_tiny_pair(mesh1, mesh2);
56
57         mesh1 = full_meshlink_open("storage-policy_conf.1", "foo", "storage-policy", DEV_CLASS_BACKBONE);
58         mesh2 = meshlink_open("storage-policy_conf.2", "bar", "storage-policy", DEV_CLASS_BACKBONE);
59         assert(mesh1);
60         assert(mesh2);
61         full_meshlink_set_storage_policy(mesh1, MESHLINK_STORAGE_KEYS_ONLY);
62         meshlink_set_storage_policy(mesh2, MESHLINK_STORAGE_KEYS_ONLY);
63
64         // Check that the nodes no longer know each other
65
66         assert(!full_meshlink_get_node(mesh1, "bar"));
67         assert(!meshlink_get_node(mesh2, "foo"));
68
69         // Exchange data again
70
71         assert(full_meshlink_import(mesh1, export2));
72         assert(meshlink_import(mesh2, export1));
73
74         free(export1);
75         free(export2);
76
77         // Close the instances and reopen them.
78
79         close_full_tiny_pair(mesh1, mesh2);
80
81         mesh1 = full_meshlink_open("storage-policy_conf.1", "foo", "storage-policy", DEV_CLASS_BACKBONE);
82         mesh2 = meshlink_open("storage-policy_conf.2", "bar", "storage-policy", DEV_CLASS_BACKBONE);
83         assert(mesh1);
84         assert(mesh2);
85         full_meshlink_set_storage_policy(mesh1, MESHLINK_STORAGE_KEYS_ONLY);
86         meshlink_set_storage_policy(mesh2, MESHLINK_STORAGE_KEYS_ONLY);
87
88         // Check that the nodes know each other
89
90         assert(full_meshlink_get_node(mesh1, "bar"));
91         assert(meshlink_get_node(mesh2, "foo"));
92
93         // Check that if we change back to STORAGE_ENABLED right before closing, pending changes are still saved
94
95         start_full_tiny_pair(mesh1, mesh2);
96         stop_full_tiny_pair(mesh1, mesh2);
97
98         full_meshlink_set_storage_policy(mesh1, MESHLINK_STORAGE_ENABLED);
99         meshlink_set_storage_policy(mesh2, MESHLINK_STORAGE_ENABLED);
100
101         close_full_tiny_pair(mesh1, mesh2);
102
103         mesh1 = full_meshlink_open("storage-policy_conf.1", "foo", "storage-policy", DEV_CLASS_BACKBONE);
104         mesh2 = meshlink_open("storage-policy_conf.2", "bar", "storage-policy", DEV_CLASS_BACKBONE);
105         assert(mesh1);
106         assert(mesh2);
107
108         // Close the instances and reopen them.
109
110         close_full_tiny_pair(mesh1, mesh2);
111
112         mesh1 = full_meshlink_open("storage-policy_conf.1", "foo", "storage-policy", DEV_CLASS_BACKBONE);
113         mesh2 = meshlink_open("storage-policy_conf.2", "bar", "storage-policy", DEV_CLASS_BACKBONE);
114         assert(mesh1);
115         assert(mesh2);
116         full_meshlink_set_storage_policy(mesh1, MESHLINK_STORAGE_KEYS_ONLY);
117         meshlink_set_storage_policy(mesh2, MESHLINK_STORAGE_KEYS_ONLY);
118
119         // Check that the nodes know each other
120
121         assert(full_meshlink_get_node(mesh1, "bar"));
122         assert(meshlink_get_node(mesh2, "foo"));
123
124         // Done.
125
126         close_full_tiny_pair(mesh1, mesh2);
127 }