+#ifdef NDEBUG
+#undef NDEBUG
+#endif
+
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
+#include <assert.h>
#include "meshlink.h"
+#include "utils.h"
int main() {
- // Open two new meshlink instance.
-
- meshlink_handle_t *mesh1 = meshlink_open("sign_verify_conf.1", "foo", "sign-verify", DEV_CLASS_BACKBONE);
-
- if(!mesh1) {
- fprintf(stderr, "Could not initialize configuration for foo\n");
- return 1;
- }
-
- meshlink_handle_t *mesh2 = meshlink_open("sign_verify_conf.2", "bar", "sign-verify", DEV_CLASS_BACKBONE);
-
- if(!mesh2) {
- fprintf(stderr, "Could not initialize configuration for bar\n");
- return 1;
- }
-
- // Import and export both side's data
-
- meshlink_add_address(mesh1, "localhost");
- meshlink_add_address(mesh2, "localhost");
-
- char *data = meshlink_export(mesh1);
-
- if(!meshlink_import(mesh2, data)) {
- fprintf(stderr, "Bar could not import data from foo\n");
- return 1;
- }
+ meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
- free(data);
-
- data = meshlink_export(mesh2);
-
- if(!meshlink_import(mesh1, data)) {
- fprintf(stderr, "Foo could not import data from bar\n");
- return 1;
- }
+ // Open two new meshlink instance.
- free(data);
+ meshlink_handle_t *mesh_a, *mesh_b;
+ open_meshlink_pair(&mesh_a, &mesh_b, "sign_verify");
// Verify that a signature made on one node can be verified by its peer.
char sig[MESHLINK_SIGLEN * 2];
size_t siglen = sizeof(sig) * 2;
- if(!meshlink_sign(mesh1, testdata1, sizeof(testdata1), sig, &siglen)) {
- fprintf(stderr, "Signing failed\n");
- return 1;
- }
-
- if(siglen != MESHLINK_SIGLEN) {
- fprintf(stderr, "Signature has unexpected length %zu != %zu\n", siglen, MESHLINK_SIGLEN);
- return 1;
- }
-
- meshlink_node_t *foo = meshlink_get_node(mesh2, "foo");
+ assert(meshlink_sign(mesh_a, testdata1, sizeof(testdata1), sig, &siglen));
+ assert(siglen == MESHLINK_SIGLEN);
- if(!foo) {
- fprintf(stderr, "Bar did not know about node foo\n");
- return 1;
- }
+ meshlink_node_t *a = meshlink_get_node(mesh_b, "a");
+ assert(a);
- meshlink_node_t *bar = meshlink_get_node(mesh2, "bar");
+ meshlink_node_t *b = meshlink_get_node(mesh_b, "b");
+ assert(b);
- if(!bar) {
- fprintf(stderr, "Bar did not know about node bar\n");
- return 1;
- }
-
- if(!meshlink_verify(mesh2, foo, testdata1, sizeof(testdata1), sig, siglen)) {
- fprintf(stderr, "False negative verification\n");
- return 1;
- }
+ assert(meshlink_verify(mesh_b, a, testdata1, sizeof(testdata1), sig, siglen));
// Check that bad signatures are revoked
- if(meshlink_verify(mesh2, foo, testdata1, sizeof(testdata1), sig, siglen / 2)) {
- fprintf(stderr, "False positive verification with half sized signature\n");
- return 1;
- }
-
- if(meshlink_verify(mesh2, foo, testdata1, sizeof(testdata1), sig, siglen * 2)) {
- fprintf(stderr, "False positive verification with double sized signature\n");
- return 1;
- }
-
- if(meshlink_verify(mesh2, foo, testdata2, sizeof(testdata2), sig, siglen)) {
- fprintf(stderr, "False positive verification with wrong data\n");
- return 1;
- }
-
- if(meshlink_verify(mesh2, bar, testdata1, sizeof(testdata1), sig, siglen)) {
- fprintf(stderr, "False positive verification with wrong signer\n");
- return 1;
- }
+ assert(!meshlink_verify(mesh_b, a, testdata1, sizeof(testdata1), sig, siglen / 2));
+ assert(!meshlink_verify(mesh_b, a, testdata1, sizeof(testdata1), sig, siglen * 2));
+ assert(!meshlink_verify(mesh_b, a, testdata2, sizeof(testdata2), sig, siglen));
+ assert(!meshlink_verify(mesh_b, b, testdata1, sizeof(testdata1), sig, siglen));
// Clean up.
- meshlink_close(mesh2);
- meshlink_close(mesh1);
-
- return 0;
+ close_meshlink_pair(mesh_a, mesh_b);
}