]> git.meshlink.io Git - meshlink/blob - test/sign-verify.c
Refactor the non-blackbox test suite.
[meshlink] / test / sign-verify.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <assert.h>
6
7 #include "meshlink.h"
8 #include "utils.h"
9
10 int main() {
11         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
12
13         // Open two new meshlink instance.
14
15         meshlink_handle_t *mesh_a, *mesh_b;
16         open_meshlink_pair(&mesh_a, &mesh_b, "sign_verify");
17
18         // Verify that a signature made on one node can be verified by its peer.
19
20         static const char testdata1[] = "Test data 1.";
21         static const char testdata2[] = "Test data 2.";
22
23         char sig[MESHLINK_SIGLEN * 2];
24         size_t siglen = sizeof(sig) * 2;
25
26         assert(meshlink_sign(mesh_a, testdata1, sizeof(testdata1), sig, &siglen));
27         assert(siglen == MESHLINK_SIGLEN);
28
29         meshlink_node_t *a = meshlink_get_node(mesh_b, "a");
30         assert(a);
31
32         meshlink_node_t *b = meshlink_get_node(mesh_b, "b");
33         assert(b);
34
35         assert(meshlink_verify(mesh_b, a, testdata1, sizeof(testdata1), sig, siglen));
36
37         // Check that bad signatures are revoked
38
39         assert(!meshlink_verify(mesh_b, a, testdata1, sizeof(testdata1), sig, siglen / 2));
40         assert(!meshlink_verify(mesh_b, a, testdata1, sizeof(testdata1), sig, siglen * 2));
41         assert(!meshlink_verify(mesh_b, a, testdata2, sizeof(testdata2), sig, siglen));
42         assert(!meshlink_verify(mesh_b, b, testdata1, sizeof(testdata1), sig, siglen));
43
44         // Clean up.
45
46         close_meshlink_pair(mesh_a, mesh_b);
47 }