]> git.meshlink.io Git - meshlink/blob - test/encrypted.c
Use a key/value store with configurable storage callbacks.
[meshlink] / test / encrypted.c
1 #ifdef NDEBUG
2 #undef NDEBUG
3 #endif
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <sys/time.h>
10 #include <assert.h>
11 #include <dirent.h>
12
13 #include "meshlink.h"
14 #include "utils.h"
15
16 #include "devtools.h"
17
18 static bool fail_stage1(int stage) {
19         return stage != 1;
20 }
21
22 static bool fail_stage2(int stage) {
23         return stage != 2;
24 }
25
26 int main(void) {
27         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
28
29         // Open a new meshlink instance.
30
31         assert(meshlink_destroy("encrypted_conf"));
32         meshlink_handle_t *mesh = meshlink_open_encrypted("encrypted_conf", "foo", "encrypted", DEV_CLASS_BACKBONE, "right", 5);
33         assert(mesh);
34
35         // Close the mesh and open it again, now with a different key.
36
37         meshlink_close(mesh);
38
39         mesh = meshlink_open_encrypted("encrypted_conf", "foo", "encrypted", DEV_CLASS_BACKBONE, "wrong", 5);
40         assert(!mesh);
41
42         // Open it again, now with the right key.
43
44         mesh = meshlink_open_encrypted("encrypted_conf", "foo", "encrypted", DEV_CLASS_BACKBONE, "right", 5);
45         assert(mesh);
46
47         // Change the encryption key.
48
49         assert(meshlink_encrypted_key_rotate(mesh, "newkey", 6));
50         meshlink_close(mesh);
51
52         // Check that we can only reopen it with the new key
53
54         mesh = meshlink_open_encrypted("encrypted_conf", "foo", "encrypted", DEV_CLASS_BACKBONE, "right", 5);
55         assert(!mesh);
56         mesh = meshlink_open_encrypted("encrypted_conf", "foo", "encrypted", DEV_CLASS_BACKBONE, "newkey", 6);
57         assert(mesh);
58
59         // Simulate a failed rotation, we should only be able to open it with the old key
60
61         devtool_keyrotate_probe = fail_stage1;
62         assert(!meshlink_encrypted_key_rotate(mesh, "newkey2", 7));
63         meshlink_close(mesh);
64         mesh = meshlink_open_encrypted("encrypted_conf", "foo", "encrypted", DEV_CLASS_BACKBONE, "newkey2", 7);
65         assert(!mesh);
66         mesh = meshlink_open_encrypted("encrypted_conf", "foo", "encrypted", DEV_CLASS_BACKBONE, "newkey", 6);
67         assert(mesh);
68
69         // Simulate a succesful rotation that was interrupted before cleaning up old files
70
71         devtool_keyrotate_probe = fail_stage2;
72         assert(meshlink_encrypted_key_rotate(mesh, "newkey3", 7));
73         meshlink_close(mesh);
74         mesh = meshlink_open_encrypted("encrypted_conf", "foo", "encrypted", DEV_CLASS_BACKBONE, "newkey", 6);
75         assert(!mesh);
76         mesh = meshlink_open_encrypted("encrypted_conf", "foo", "encrypted", DEV_CLASS_BACKBONE, "newkey3", 7);
77         assert(mesh);
78
79         // That's it.
80
81         meshlink_close(mesh);
82
83         // Destroy the mesh.
84
85         assert(meshlink_destroy("encrypted_conf"));
86
87         DIR *dir = opendir("encrypted_conf");
88         assert(!dir && errno == ENOENT);
89 }