]> git.meshlink.io Git - meshlink/blob - test/encrypted.c
Add support for encrypted storage.
[meshlink] / test / encrypted.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <errno.h>
5 #include <sys/time.h>
6
7 #include "meshlink.h"
8
9 void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
10         static struct timeval tv0;
11         struct timeval tv;
12
13         if(tv0.tv_sec == 0) {
14                 gettimeofday(&tv0, NULL);
15         }
16
17         gettimeofday(&tv, NULL);
18         fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
19
20         if(mesh) {
21                 fprintf(stderr, "(%s) ", mesh->name);
22         }
23
24         fprintf(stderr, "[%d] %s\n", level, text);
25 }
26
27 int main() {
28         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
29
30         // Open a new meshlink instance.
31
32         meshlink_handle_t *mesh = meshlink_open_encrypted("encrypted_conf", "foo", "encrypted", DEV_CLASS_BACKBONE, "right", 5);
33
34         if(!mesh) {
35                 fprintf(stderr, "Could not initialize configuration for foo\n");
36                 return 1;
37         }
38
39         // Close the mesh and open it again, now with a different key.
40
41         meshlink_close(mesh);
42
43         mesh = meshlink_open_encrypted("encrypted_conf", "foo", "encrypted", DEV_CLASS_BACKBONE, "wrong", 5);
44
45         if(mesh) {
46                 fprintf(stderr, "Could open mesh with the wrong key\n");
47                 return 1;
48         }
49
50         // Open it again, now with the right key.
51
52         mesh = meshlink_open_encrypted("encrypted_conf", "foo", "encrypted", DEV_CLASS_BACKBONE, "right", 5);
53
54         if(!mesh) {
55                 fprintf(stderr, "Could not open mesh with the right key\n");
56                 return 1;
57         }
58
59         // That's it.
60
61         meshlink_close(mesh);
62
63         // Destroy the mesh.
64
65         if(!meshlink_destroy("encrypted_conf")) {
66                 fprintf(stderr, "Could not destroy configuration\n");
67                 return 1;
68         }
69
70         if(!access("encrypted_conf", F_OK) || errno != ENOENT) {
71                 fprintf(stderr, "Configuration not fully destroyed\n");
72                 return 1;
73         }
74
75         return 0;
76 }