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