From 668664d0ea90dc81670cccd7b7d56b36b8360eaa Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Mon, 2 Oct 2017 23:07:34 +0200 Subject: [PATCH] Format the examples and test suite. --- Makefile.am | 2 +- examples/channels.c | 75 ++++++++++++++------ examples/chat.c | 68 +++++++++++++------ examples/chatpp.cc | 64 +++++++++++------ examples/manynodes.c | 132 ++++++++++++++++++++++++------------ examples/meshlinkapp.c | 1 + test/basic.c | 9 +++ test/channels-cornercases.c | 26 ++++--- test/channels-fork.c | 52 ++++++++++---- test/channels.c | 47 ++++++++++--- test/echo-fork.c | 52 ++++++++++---- test/import-export.c | 12 +++- test/invite-join.c | 11 ++- test/sign-verify.c | 8 +++ test/trio.c | 24 +++++-- test/utils.c | 3 +- test/utils.h | 20 +++--- 17 files changed, 434 insertions(+), 172 deletions(-) diff --git a/Makefile.am b/Makefile.am index 42146893..26d555c7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -12,4 +12,4 @@ ChangeLog: git log > ChangeLog astyle: - astyle --options=.astylerc -nQ src/*.[ch] src/ed25519/e*.[ch] + astyle --options=.astylerc -nQ src/*.[ch] src/ed25519/e*.[ch] examples/*.[ch] examples/*.cc test/*.[ch] diff --git a/examples/channels.c b/examples/channels.c index a869f513..e56d9d16 100644 --- a/examples/channels.c +++ b/examples/channels.c @@ -19,10 +19,11 @@ static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, con static void channel_receive(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { if(!len) { - if(meshlink_errno) + if(meshlink_errno) { fprintf(stderr, "Error while reading data from %s: %s\n", channel->node->name, meshlink_strerror(meshlink_errno)); - else + } else { fprintf(stderr, "Chat connection closed by %s\n", channel->node->name); + } channel->node->priv = NULL; meshlink_channel_close(mesh, channel); @@ -61,10 +62,11 @@ static void channel_poll(meshlink_handle_t *mesh, meshlink_channel_t *channel, s } static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { - if(reachable) + if(reachable) { printf("%s joined.\n", node->name); - else + } else { printf("%s left.\n", node->name); + } } static meshlink_node_t **nodes; @@ -72,8 +74,10 @@ static size_t nnodes; static void parse_command(meshlink_handle_t *mesh, char *buf) { char *arg = strchr(buf, ' '); - if(arg) + + if(arg) { *arg++ = 0; + } if(!strcasecmp(buf, "invite")) { char *invitation; @@ -84,6 +88,7 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { } invitation = meshlink_invite(mesh, arg); + if(!invitation) { fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); return; @@ -96,11 +101,15 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { fprintf(stderr, "/join requires an argument!\n"); return; } + meshlink_stop(mesh); - if(!meshlink_join(mesh, arg)) + + if(!meshlink_join(mesh, arg)) { fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno)); - else + } else { fprintf(stderr, "Invitation accepted!\n"); + } + if(!meshlink_start(mesh)) { fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno)); exit(1); @@ -112,6 +121,7 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { } meshlink_node_t *node = meshlink_get_node(mesh, arg); + if(!node) { fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); return; @@ -123,20 +133,26 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { } else if(!strcasecmp(buf, "who")) { if(!arg) { nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes); - if(!nnodes) + + if(!nnodes) { fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno)); - else { + } else { printf("%zu known nodes:", nnodes); - for(int i = 0; i < nnodes; i++) + + for(int i = 0; i < nnodes; i++) { printf(" %s", nodes[i]->name); + } + printf("\n"); } } else { meshlink_node_t *node = meshlink_get_node(mesh, arg); - if(!node) + + if(!node) { fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); - else + } else { printf("Node %s found\n", arg); + } } } else if(!strcasecmp(buf, "quit")) { printf("Bye!\n"); @@ -151,36 +167,42 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { "/who [] List all nodes or show information about the given node.\n" "/quit Exit this program.\n" ); - } else + } else { fprintf(stderr, "Unknown command '/%s'\n", buf); + } } static void parse_input(meshlink_handle_t *mesh, char *buf) { static meshlink_node_t *destination; size_t len; - if(!buf) + if(!buf) { return; + } // Remove newline. len = strlen(buf); - if(len && buf[len - 1] == '\n') + if(len && buf[len - 1] == '\n') { buf[--len] = 0; + } - if(len && buf[len - 1] == '\r') + if(len && buf[len - 1] == '\r') { buf[--len] = 0; + } // Ignore empty lines. - if(!len) + if(!len) { return; + } // Commands start with '/' - if(*buf == '/') + if(*buf == '/') { return parse_command(mesh, buf + 1); + } // Lines in the form "name: message..." set the destination node. @@ -190,10 +212,13 @@ static void parse_input(meshlink_handle_t *mesh, char *buf) { if(colon) { *colon = 0; msg = colon + 1; - if(*msg == ' ') + + if(*msg == ' ') { msg++; + } destination = meshlink_get_node(mesh, buf); + if(!destination) { fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno)); return; @@ -212,10 +237,12 @@ static void parse_input(meshlink_handle_t *mesh, char *buf) { if(!channel) { fprintf(stderr, "Opening chat channel to '%s'\n", destination->name); channel = meshlink_channel_open(mesh, destination, CHAT_PORT, channel_receive, NULL, 0); + if(!channel) { fprintf(stderr, "Could not create channel to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno)); return; } + destination->priv = channel; meshlink_set_channel_poll_cb(mesh, channel, channel_poll); } @@ -233,15 +260,18 @@ int main(int argc, char *argv[]) { const char *nick = NULL; char buf[1024]; - if(argc > 1) + if(argc > 1) { confbase = argv[1]; + } - if(argc > 2) + if(argc > 2) { nick = argv[2]; + } meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message); meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY); + if(!mesh) { fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno)); return 1; @@ -261,8 +291,9 @@ int main(int argc, char *argv[]) { printf("Chat started.\nType /help for a list of commands.\n"); - while(fgets(buf, sizeof(buf), stdin)) + while(fgets(buf, sizeof(buf), stdin)) { parse_input(mesh, buf); + } printf("Chat stopping.\n"); diff --git a/examples/chat.c b/examples/chat.c index f5f41c93..7cf5a480 100644 --- a/examples/chat.c +++ b/examples/chat.c @@ -27,10 +27,11 @@ static void receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void } static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { - if(reachable) + if(reachable) { printf("%s joined.\n", node->name); - else + } else { printf("%s left.\n", node->name); + } } static meshlink_node_t **nodes; @@ -38,8 +39,10 @@ static size_t nnodes; static void parse_command(meshlink_handle_t *mesh, char *buf) { char *arg = strchr(buf, ' '); - if(arg) + + if(arg) { *arg++ = 0; + } if(!strcasecmp(buf, "invite")) { char *invitation; @@ -50,6 +53,7 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { } invitation = meshlink_invite(mesh, arg); + if(!invitation) { fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); return; @@ -62,11 +66,15 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { fprintf(stderr, "/join requires an argument!\n"); return; } + meshlink_stop(mesh); - if(!meshlink_join(mesh, arg)) + + if(!meshlink_join(mesh, arg)) { fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno)); - else + } else { fprintf(stderr, "Invitation accepted!\n"); + } + if(!meshlink_start(mesh)) { fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno)); exit(1); @@ -78,6 +86,7 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { } meshlink_node_t *node = meshlink_get_node(mesh, arg); + if(!node) { fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); return; @@ -89,20 +98,26 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { } else if(!strcasecmp(buf, "who")) { if(!arg) { nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes); - if(!nnodes) + + if(!nnodes) { fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno)); - else { + } else { printf("%zu known nodes:", nnodes); - for(int i = 0; i < nnodes; i++) + + for(int i = 0; i < nnodes; i++) { printf(" %s", nodes[i]->name); + } + printf("\n"); } } else { meshlink_node_t *node = meshlink_get_node(mesh, arg); - if(!node) + + if(!node) { fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); - else + } else { printf("Node %s found\n", arg); + } } } else if(!strcasecmp(buf, "quit")) { printf("Bye!\n"); @@ -117,36 +132,42 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { "/who [] List all nodes or show information about the given node.\n" "/quit Exit this program.\n" ); - } else + } else { fprintf(stderr, "Unknown command '/%s'\n", buf); + } } static void parse_input(meshlink_handle_t *mesh, char *buf) { static meshlink_node_t *destination; size_t len; - if(!buf) + if(!buf) { return; + } // Remove newline. len = strlen(buf); - if(len && buf[len - 1] == '\n') + if(len && buf[len - 1] == '\n') { buf[--len] = 0; + } - if(len && buf[len - 1] == '\r') + if(len && buf[len - 1] == '\r') { buf[--len] = 0; + } // Ignore empty lines. - if(!len) + if(!len) { return; + } // Commands start with '/' - if(*buf == '/') + if(*buf == '/') { return parse_command(mesh, buf + 1); + } // Lines in the form "name: message..." set the destination node. @@ -156,10 +177,13 @@ static void parse_input(meshlink_handle_t *mesh, char *buf) { if(colon) { *colon = 0; msg = colon + 1; - if(*msg == ' ') + + if(*msg == ' ') { msg++; + } destination = meshlink_get_node(mesh, buf); + if(!destination) { fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno)); return; @@ -184,15 +208,18 @@ int main(int argc, char *argv[]) { const char *nick = NULL; char buf[1024]; - if(argc > 1) + if(argc > 1) { confbase = argv[1]; + } - if(argc > 2) + if(argc > 2) { nick = argv[2]; + } meshlink_set_log_cb(NULL, MESHLINK_INFO, log_message); meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY); + if(!mesh) { fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno)); return 1; @@ -209,8 +236,9 @@ int main(int argc, char *argv[]) { printf("Chat started.\nType /help for a list of commands.\n"); - while(fgets(buf, sizeof(buf), stdin)) + while(fgets(buf, sizeof(buf), stdin)) { parse_input(mesh, buf); + } printf("Chat stopping.\n"); diff --git a/examples/chatpp.cc b/examples/chatpp.cc index b5fe44e6..10870ef5 100644 --- a/examples/chatpp.cc +++ b/examples/chatpp.cc @@ -23,10 +23,11 @@ public: } void node_status(meshlink::node *node, bool reachable) { - if(reachable) + if(reachable) { printf("%s joined.\n", node->name); - else + } else { printf("%s left.\n", node->name); + } } }; @@ -35,8 +36,10 @@ static size_t nnodes; static void parse_command(meshlink::mesh *mesh, char *buf) { char *arg = strchr(buf, ' '); - if(arg) + + if(arg) { *arg++ = 0; + } if(!strcasecmp(buf, "invite")) { char *invitation; @@ -47,6 +50,7 @@ static void parse_command(meshlink::mesh *mesh, char *buf) { } invitation = mesh->invite(arg); + if(!invitation) { fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink::strerror()); return; @@ -62,10 +66,11 @@ static void parse_command(meshlink::mesh *mesh, char *buf) { mesh->stop(); - if(!mesh->join(arg)) + if(!mesh->join(arg)) { fprintf(stderr, "Could not join using invitation: %s\n", meshlink::strerror()); - else + } else { fprintf(stderr, "Invitation accepted!\n"); + } if(!mesh->start()) { fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink::strerror()); @@ -78,6 +83,7 @@ static void parse_command(meshlink::mesh *mesh, char *buf) { } meshlink::node *node = mesh->get_node(arg); + if(!node) { fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror()); return; @@ -89,20 +95,26 @@ static void parse_command(meshlink::mesh *mesh, char *buf) { } else if(!strcasecmp(buf, "who")) { if(!arg) { nodes = mesh->get_all_nodes(nodes, &nnodes); - if(!nodes) + + if(!nodes) { fprintf(stderr, "Could not get list of nodes: %s\n", meshlink::strerror()); - else { + } else { printf("%zu known nodes:", nnodes); - for(size_t i = 0; i < nnodes; i++) + + for(size_t i = 0; i < nnodes; i++) { printf(" %s", nodes[i]->name); + } + printf("\n"); } } else { meshlink::node *node = mesh->get_node(arg); - if(!node) + + if(!node) { fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror()); - else + } else { printf("Node %s found\n", arg); + } } } else if(!strcasecmp(buf, "quit")) { printf("Bye!\n"); @@ -117,36 +129,42 @@ static void parse_command(meshlink::mesh *mesh, char *buf) { "/who [] List all nodes or show information about the given node.\n" "/quit Exit this program.\n" ); - } else + } else { fprintf(stderr, "Unknown command '/%s'\n", buf); + } } static void parse_input(meshlink::mesh *mesh, char *buf) { static meshlink::node *destination; size_t len; - if(!buf) + if(!buf) { return; + } // Remove newline. len = strlen(buf); - if(len && buf[len - 1] == '\n') + if(len && buf[len - 1] == '\n') { buf[--len] = 0; + } - if(len && buf[len - 1] == '\r') + if(len && buf[len - 1] == '\r') { buf[--len] = 0; + } // Ignore empty lines. - if(!len) + if(!len) { return; + } // Commands start with '/' - if(*buf == '/') + if(*buf == '/') { return parse_command(mesh, buf + 1); + } // Lines in the form "name: message..." set the destination node. @@ -156,10 +174,13 @@ static void parse_input(meshlink::mesh *mesh, char *buf) { if(colon) { *colon = 0; msg = colon + 1; - if(*msg == ' ') + + if(*msg == ' ') { msg++; + } destination = mesh->get_node(buf); + if(!destination) { fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink::strerror()); return; @@ -185,11 +206,13 @@ int main(int argc, char *argv[]) { const char *nick = NULL; char buf[1024]; - if(argc > 1) + if(argc > 1) { confbase = argv[1]; + } - if(argc > 2) + if(argc > 2) { nick = argv[2]; + } ChatMesh mesh; mesh.open(confbase, nick, "chatpp", DEV_CLASS_STATIONARY); @@ -206,8 +229,9 @@ int main(int argc, char *argv[]) { printf("Chat started.\nType /help for a list of commands.\n"); - while(fgets(buf, sizeof(buf), stdin)) + while(fgets(buf, sizeof(buf), stdin)) { parse_input(&mesh, buf); + } printf("Chat stopping.\n"); diff --git a/examples/manynodes.c b/examples/manynodes.c index 61b5ece5..2cfe8180 100644 --- a/examples/manynodes.c +++ b/examples/manynodes.c @@ -47,14 +47,17 @@ static void testmesh() { for(int nindex = 0; nindex < n; nindex++) { nodes = meshlink_get_all_nodes(mesh[nindex], nodes, &nnodes); - if(!nodes) + + if(!nodes) { fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno)); - else { + } else { printf("%zu known nodes:\n", nnodes); + for(int i = 0; i < nnodes; i++) { //printf(" %s\n", nodes[i]->name); - if(!meshlink_send(mesh[nindex], nodes[i], "magic", strlen("magic") + 1)) + if(!meshlink_send(mesh[nindex], nodes[i], "magic", strlen("magic") + 1)) { fprintf(stderr, "Could not send message to '%s': %s\n", nodes[i]->name, meshlink_strerror(meshlink_errno)); + } } } @@ -84,10 +87,11 @@ static bool exportmeshgraph(const char *path) { int psr = stat(path, &ps); if(psr == 0 || errno != ENOENT) { - if(psr == -1) + if(psr == -1) { perror("stat"); - else + } else { fprintf(stderr, "%s exists already\n", path); + } return false; } @@ -124,10 +128,12 @@ void exportmeshgraph_timer(int signum) { static bool exportmeshgraph_started = false; static bool exportmeshgraph_end(const char *none) { - if(!exportmeshgraph_started) + if(!exportmeshgraph_started) { return false; + } struct itimerval zero_timer = { 0 }; + setitimer(ITIMER_REAL, &zero_timer, NULL); exportmeshgraph_started = false; @@ -136,19 +142,22 @@ static bool exportmeshgraph_end(const char *none) { } static bool exportmeshgraph_begin(const char *timeout_str) { - if(!timeout_str) + if(!timeout_str) { return false; + } if(exportmeshgraph_started) { - if(!exportmeshgraph_end(NULL)) + if(!exportmeshgraph_end(NULL)) { return false; + } } // get timeout int timeout = atoi(timeout_str); - if(timeout < 100) + if(timeout < 100) { timeout = 100; + } int timeout_sec = timeout / 1000; int timeout_msec = timeout % 1000; @@ -184,8 +193,10 @@ static bool exportmeshgraph_begin(const char *timeout_str) { static void parse_command(char *buf) { char *arg = strchr(buf, ' '); - if(arg) + + if(arg) { *arg++ = 0; + } if(!strcasecmp(buf, "invite")) { char *invitation; @@ -196,6 +207,7 @@ static void parse_command(char *buf) { } invitation = meshlink_invite(mesh[nodeindex], arg); + if(!invitation) { fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); return; @@ -208,11 +220,15 @@ static void parse_command(char *buf) { fprintf(stderr, "/join requires an argument!\n"); return; } + meshlink_stop(mesh[nodeindex]); - if(!meshlink_join(mesh[nodeindex], arg)) + + if(!meshlink_join(mesh[nodeindex], arg)) { fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno)); - else + } else { fprintf(stderr, "Invitation accepted!\n"); + } + if(!meshlink_start(mesh[nodeindex])) { fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno)); exit(1); @@ -224,6 +240,7 @@ static void parse_command(char *buf) { } meshlink_node_t *node = meshlink_get_node(mesh[nodeindex], arg); + if(!node) { fprintf(stderr, "Unknown node '%s'\n", arg); return; @@ -235,41 +252,48 @@ static void parse_command(char *buf) { } else if(!strcasecmp(buf, "who")) { if(!arg) { nodes = meshlink_get_all_nodes(mesh[nodeindex], nodes, &nnodes); - if(!nodes) + + if(!nodes) { fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno)); - else { + } else { printf("%zu known nodes:", nnodes); - for(int i = 0; i < nnodes; i++) + + for(int i = 0; i < nnodes; i++) { printf(" %s", nodes[i]->name); + } + printf("\n"); } } else { meshlink_node_t *node = meshlink_get_node(mesh[nodeindex], arg); - if(!node) + + if(!node) { fprintf(stderr, "Unknown node '%s'\n", arg); - else + } else { printf("Node %s found, pmtu %zd\n", arg, meshlink_get_pmtu(mesh[nodeindex], node)); + } } - } else if(!strcasecmp(buf, "link")) + } else if(!strcasecmp(buf, "link")) { linkmesh(); - else if(!strcasecmp(buf, "eg")) + } else if(!strcasecmp(buf, "eg")) { exportmeshgraph(arg); - else if(!strcasecmp(buf, "egb")) + } else if(!strcasecmp(buf, "egb")) { exportmeshgraph_begin(arg); - else if(!strcasecmp(buf, "ege")) + } else if(!strcasecmp(buf, "ege")) { exportmeshgraph_end(NULL); - else if(!strcasecmp(buf, "test")) + } else if(!strcasecmp(buf, "test")) { testmesh(); - else if(!strcasecmp(buf, "select")) { + } else if(!strcasecmp(buf, "select")) { if(!arg) { fprintf(stderr, "/select requires an argument!\n"); return; } + nodeindex = atoi(arg); printf("Index is now %d\n", nodeindex); - } else if(!strcasecmp(buf, "stop")) + } else if(!strcasecmp(buf, "stop")) { meshlink_stop(mesh[nodeindex]); - else if(!strcasecmp(buf, "quit")) { + } else if(!strcasecmp(buf, "quit")) { printf("Bye!\n"); fclose(stdin); } else if(!strcasecmp(buf, "help")) { @@ -287,36 +311,42 @@ static void parse_command(char *buf) { "/stop Call meshlink_stop, use /select first to select which node to stop\n" "/quit Exit this program.\n" ); - } else + } else { fprintf(stderr, "Unknown command '/%s'\n", buf); + } } static void parse_input(char *buf) { static meshlink_node_t *destination; size_t len; - if(!buf) + if(!buf) { return; + } // Remove newline. len = strlen(buf); - if(len && buf[len - 1] == '\n') + if(len && buf[len - 1] == '\n') { buf[--len] = 0; + } - if(len && buf[len - 1] == '\r') + if(len && buf[len - 1] == '\r') { buf[--len] = 0; + } // Ignore empty lines. - if(!len) + if(!len) { return; + } // Commands start with '/' - if(*buf == '/') + if(*buf == '/') { return parse_command(buf + 1); + } // Lines in the form "name: message..." set the destination node. @@ -326,10 +356,13 @@ static void parse_input(char *buf) { if(colon) { *colon = 0; msg = colon + 1; - if(*msg == ' ') + + if(*msg == ' ') { msg++; + } destination = meshlink_get_node(mesh[nodeindex], buf); + if(!destination) { fprintf(stderr, "Unknown node '%s'\n", buf); return; @@ -354,22 +387,26 @@ int main(int argc, char *argv[]) { const char *graphexporttimeout = NULL; char buf[1024]; - if(argc > 1) + if(argc > 1) { n = atoi(argv[1]); + } if(n < 1) { fprintf(stderr, "Usage: %s [number of local nodes] [confbase] [prefixnodenames] [graphexport timeout]\n", argv[0]); return 1; } - if(argc > 2) + if(argc > 2) { basebase = argv[2]; + } - if(argc > 3) + if(argc > 3) { namesprefix = argv[3]; + } - if(argc > 4) + if(argc > 4) { graphexporttimeout = argv[4]; + } mesh = calloc(n, sizeof(*mesh)); @@ -382,14 +419,19 @@ int main(int argc, char *argv[]) { char filename[PATH_MAX]; char nodename[100]; + for(int i = 0; i < n; i++) { snprintf(nodename, sizeof(nodename), "%snode%d", namesprefix, i); snprintf(filename, sizeof(filename), "%s/%s", basebase, nodename); - if(n / (i + 1) > n / 4) + + if(n / (i + 1) > n / 4) { mesh[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_BACKBONE); - else + } else { mesh[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_PORTABLE); + } + meshlink_set_log_cb(mesh[i], MESHLINK_DEBUG, log_message); + if(!mesh[i]) { fprintf(stderr, "errno is: %d\n", meshlink_errno); fprintf(stderr, "Could not open %s: %s\n", filename, meshlink_strerror(meshlink_errno)); @@ -400,10 +442,11 @@ int main(int argc, char *argv[]) { int started = 0; for(int i = 0; i < n; i++) { - if(!meshlink_start(mesh[i])) + if(!meshlink_start(mesh[i])) { fprintf(stderr, "Could not start node %d: %s\n", i, meshlink_strerror(meshlink_errno)); - else + } else { started++; + } } if(!started) { @@ -411,21 +454,24 @@ int main(int argc, char *argv[]) { return 1; } - if(graphexporttimeout) + if(graphexporttimeout) { exportmeshgraph_begin(graphexporttimeout); + } printf("%d nodes started.\nType /help for a list of commands.\n", started); // handle input - while(fgets(buf, sizeof(buf), stdin)) + while(fgets(buf, sizeof(buf), stdin)) { parse_input(buf); + } exportmeshgraph_end(NULL); printf("Nodes stopping.\n"); - for(int i = 0; i < n; i++) + for(int i = 0; i < n; i++) { meshlink_close(mesh[i]); + } return 0; } diff --git a/examples/meshlinkapp.c b/examples/meshlinkapp.c index 972544e1..9796cba3 100644 --- a/examples/meshlinkapp.c +++ b/examples/meshlinkapp.c @@ -25,6 +25,7 @@ int main(int argc, char **argv) { sleep(10); meshlink_node_t *remotenode = meshlink_get_node(myhandle, remotename); + if(!remotenode) { fprintf(stderr, "Node %s not known yet.\n", remotename); continue; diff --git a/test/basic.c b/test/basic.c index d3ac1373..30a81bcb 100644 --- a/test/basic.c +++ b/test/basic.c @@ -9,6 +9,7 @@ int main(int argc, char *argv[]) { // Open a new meshlink instance. meshlink_handle_t *mesh = meshlink_open("basic_conf", "foo", "basic", DEV_CLASS_BACKBONE); + if(!mesh) { fprintf(stderr, "Could not initialize configuration for foo\n"); return 1; @@ -17,10 +18,12 @@ int main(int argc, char *argv[]) { // Check that our own node exists. meshlink_node_t *self = meshlink_get_self(mesh); + if(!self) { fprintf(stderr, "Foo does not know about itself\n"); return 1; } + if(strcmp(self->name, "foo")) { fprintf(stderr, "Foo thinks its name is %s\n", self->name); return 1; @@ -32,6 +35,7 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Foo could not start\n"); return 1; } + meshlink_stop(mesh); // Make sure we can start and stop the mesh again. @@ -40,6 +44,7 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Foo could not start twice\n"); return 1; } + meshlink_stop(mesh); // Close the mesh and open it again, now with a different name parameter. @@ -49,6 +54,7 @@ int main(int argc, char *argv[]) { // Check that the name is ignored now, and that we still are "foo". mesh = meshlink_open("basic_conf", "bar", "basic", DEV_CLASS_BACKBONE); + if(!mesh) { fprintf(stderr, "Could not open configuration for foo a second time\n"); return 1; @@ -60,10 +66,12 @@ int main(int argc, char *argv[]) { } self = meshlink_get_self(mesh); + if(!self) { fprintf(stderr, "Foo doesn't know about itself the second time\n"); return 1; } + if(strcmp(self->name, "foo")) { fprintf(stderr, "Foo thinks its name is %s the second time\n", self->name); return 1; @@ -75,6 +83,7 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Foo could not start a third time\n"); return 1; } + meshlink_stop(mesh); // That's it. diff --git a/test/channels-cornercases.c b/test/channels-cornercases.c index e76f735f..d4e02e28 100644 --- a/test/channels-cornercases.c +++ b/test/channels-cornercases.c @@ -19,27 +19,33 @@ void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *tex static struct timeval tv0; struct timeval tv; - if(tv0.tv_sec == 0) + if(tv0.tv_sec == 0) { gettimeofday(&tv0, NULL); + } + gettimeofday(&tv, NULL); fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000); - if(mesh) + if(mesh) { fprintf(stderr, "(%s) ", mesh->name); + } + fprintf(stderr, "[%d] %s\n", level, text); } void a_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { - if(len == 5 && !memcmp(data, "Hello", 5)) + if(len == 5 && !memcmp(data, "Hello", 5)) { b_responded = true; - else if(len == 0) + } else if(len == 0) { b_closed = true; + } } void b_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { // Send one message back, then close the channel. - if(len) + if(len) { meshlink_channel_send(mesh, channel, data, len); + } meshlink_channel_close(mesh, channel); } @@ -51,8 +57,11 @@ bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t po bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) { meshlink_set_channel_accept_cb(mesh, NULL); meshlink_set_channel_receive_cb(mesh, channel, b_receive_cb); - if(data) + + if(data) { b_receive_cb(mesh, channel, data, len); + } + return true; } @@ -62,8 +71,9 @@ void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) { } void poll_cb2(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) { - if(len) + if(len) { a_nonzero_poll_cb = true; + } } int main(int argc, char *argv[]) { @@ -132,7 +142,7 @@ int main(int argc, char *argv[]) { assert(b_closed); // Try to create a second channel - + meshlink_channel_t *channel2 = meshlink_channel_open(a, nb, 7, a_receive_cb, NULL, 0); assert(channel2); meshlink_set_channel_poll_cb(a, channel2, poll_cb2); diff --git a/test/channels-fork.c b/test/channels-fork.c index faa16c6e..db2a033f 100644 --- a/test/channels-fork.c +++ b/test/channels-fork.c @@ -10,19 +10,23 @@ volatile bool bar_reachable = false; volatile bool bar_responded = false; void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) { - if(mesh) + if(mesh) { fprintf(stderr, "(%s) ", mesh->name); + } + fprintf(stderr, "[%d] %s\n", level, text); } void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { - if(!strcmp(node->name, "bar")) + if(!strcmp(node->name, "bar")) { bar_reachable = reachable; + } } void foo_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { - if(len == 5 && !memcmp(data, "Hello", 5)) + if(len == 5 && !memcmp(data, "Hello", 5)) { bar_responded = true; + } } void bar_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { @@ -35,24 +39,32 @@ bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t po } bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) { - if(port != 7) + if(port != 7) { return false; + } + meshlink_set_channel_receive_cb(mesh, channel, bar_receive_cb); - if(data) + + if(data) { bar_receive_cb(mesh, channel, data, len); + } + return true; } void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) { meshlink_set_channel_poll_cb(mesh, channel, NULL); - if(meshlink_channel_send(mesh, channel, "Hello", 5) != 5) + + if(meshlink_channel_send(mesh, channel, "Hello", 5) != 5) { fprintf(stderr, "Could not send whole message\n"); + } } int main1(int rfd, int wfd) { meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); meshlink_handle_t *mesh1 = meshlink_open("channels_conf.1", "foo", "channels", DEV_CLASS_BACKBONE); + if(!mesh1) { fprintf(stderr, "Could not initialize configuration for foo\n"); return 1; @@ -63,6 +75,7 @@ int main1(int rfd, int wfd) { meshlink_add_address(mesh1, "localhost"); char *data = meshlink_export(mesh1); + if(!data) { fprintf(stderr, "Foo could not export its configuration\n"); return 1; @@ -92,8 +105,10 @@ int main1(int rfd, int wfd) { for(int i = 0; i < 20; i++) { sleep(1); - if(bar_reachable) + + if(bar_reachable) { break; + } } if(!bar_reachable) { @@ -104,6 +119,7 @@ int main1(int rfd, int wfd) { // Open a channel from foo to bar. meshlink_node_t *bar = meshlink_get_node(mesh1, "bar"); + if(!bar) { fprintf(stderr, "Foo could not find bar\n"); return 1; @@ -114,8 +130,10 @@ int main1(int rfd, int wfd) { for(int i = 0; i < 5; i++) { sleep(1); - if(bar_responded) + + if(bar_responded) { break; + } } if(!bar_responded) { @@ -139,6 +157,7 @@ int main2(int rfd, int wfd) { meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); meshlink_handle_t *mesh2 = meshlink_open("channels_conf.2", "bar", "channels", DEV_CLASS_BACKBONE); + if(!mesh2) { fprintf(stderr, "Could not initialize configuration for bar\n"); return 1; @@ -147,14 +166,22 @@ int main2(int rfd, int wfd) { meshlink_enable_discovery(mesh2, false); char *data = meshlink_export(mesh2); + if(!data) { fprintf(stderr, "Bar could not export its configuration\n"); return 1; } size_t len = strlen(data); - if(write(wfd, &len, sizeof(len)) <= 0) abort(); - if(write(wfd, data, len) <= 0) abort(); + + if(write(wfd, &len, sizeof(len)) <= 0) { + abort(); + } + + if(write(wfd, data, len) <= 0) { + abort(); + } + free(data); read(rfd, &len, sizeof(len)); @@ -189,8 +216,9 @@ int main(int argc, char *argv[]) { pipe2(fda, 0); pipe2(fdb, 0); - if(fork()) + if(fork()) { return main1(fda[0], fdb[1]); - else + } else { return main2(fdb[0], fda[1]); + } } diff --git a/test/channels.c b/test/channels.c index cf1ff891..68512eec 100644 --- a/test/channels.c +++ b/test/channels.c @@ -14,28 +14,36 @@ void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *tex static struct timeval tv0; struct timeval tv; - if(tv0.tv_sec == 0) + if(tv0.tv_sec == 0) { gettimeofday(&tv0, NULL); + } + gettimeofday(&tv, NULL); fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000); - if(mesh) + if(mesh) { fprintf(stderr, "(%s) ", mesh->name); + } + fprintf(stderr, "[%d] %s\n", level, text); } void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { printf("status_cb: %s %sreachable\n", node->name, reachable ? "" : "un"); - if(!strcmp(node->name, "bar")) + + if(!strcmp(node->name, "bar")) { bar_reachable = reachable; + } } void foo_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { printf("foo_receive_cb %zu: ", len); fwrite(data, 1, len, stdout); printf("\n"); - if(len == 5 && !memcmp(data, "Hello", 5)) + + if(len == 5 && !memcmp(data, "Hello", 5)) { bar_responded = true; + } } void bar_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { @@ -52,22 +60,32 @@ bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t po bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) { printf("accept_cb: (from %s on port %u) ", channel->node->name, (unsigned int)port); - if(data) + + if(data) { fwrite(data, 1, len, stdout); + } + printf("\n"); - if(port != 7) + if(port != 7) { return false; + } + meshlink_set_channel_receive_cb(mesh, channel, bar_receive_cb); - if(data) + + if(data) { bar_receive_cb(mesh, channel, data, len); + } + return true; } void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) { meshlink_set_channel_poll_cb(mesh, channel, NULL); - if(meshlink_channel_send(mesh, channel, "Hello", 5) != 5) + + if(meshlink_channel_send(mesh, channel, "Hello", 5) != 5) { fprintf(stderr, "Could not send whole message\n"); + } } int main(int argc, char *argv[]) { @@ -76,12 +94,14 @@ int main(int argc, char *argv[]) { // Open two new meshlink instance. meshlink_handle_t *mesh1 = meshlink_open("channels_conf.1", "foo", "channels", DEV_CLASS_BACKBONE); + if(!mesh1) { fprintf(stderr, "Could not initialize configuration for foo\n"); return 1; } meshlink_handle_t *mesh2 = meshlink_open("channels_conf.2", "bar", "channels", DEV_CLASS_BACKBONE); + if(!mesh2) { fprintf(stderr, "Could not initialize configuration for bar\n"); return 1; @@ -97,6 +117,7 @@ int main(int argc, char *argv[]) { meshlink_add_address(mesh1, "localhost"); char *data = meshlink_export(mesh1); + if(!data) { fprintf(stderr, "Foo could not export its configuration\n"); return 1; @@ -110,6 +131,7 @@ int main(int argc, char *argv[]) { free(data); data = meshlink_export(mesh2); + if(!data) { fprintf(stderr, "Bar could not export its configuration\n"); return 1; @@ -145,8 +167,10 @@ int main(int argc, char *argv[]) { for(int i = 0; i < 20; i++) { sleep(1); - if(bar_reachable) + + if(bar_reachable) { break; + } } if(!bar_reachable) { @@ -157,6 +181,7 @@ int main(int argc, char *argv[]) { // Open a channel from foo to bar. meshlink_node_t *bar = meshlink_get_node(mesh1, "bar"); + if(!bar) { fprintf(stderr, "Foo could not find bar\n"); return 1; @@ -167,8 +192,10 @@ int main(int argc, char *argv[]) { for(int i = 0; i < 20; i++) { sleep(1); - if(bar_responded) + + if(bar_responded) { break; + } } if(!bar_responded) { diff --git a/test/echo-fork.c b/test/echo-fork.c index bd8da445..f5aa04e3 100644 --- a/test/echo-fork.c +++ b/test/echo-fork.c @@ -16,17 +16,20 @@ volatile bool foo_closed = false; int debug_level; void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) { - if(mesh) + if(mesh) { fprintf(stderr, "(%s) ", mesh->name); + } + fprintf(stderr, "[%d] %s\n", level, text); } void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { - if(!strcmp(node->name, "bar")) + if(!strcmp(node->name, "bar")) { bar_reachable = reachable; - else if(!strcmp(node->name, "foo")) - if(!reachable) + } else if(!strcmp(node->name, "foo")) + if(!reachable) { foo_closed = true; + } } void foo_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { @@ -39,6 +42,7 @@ void bar_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const foo_closed = true; return; } + // Write data to stdout. write(1, data, len); } @@ -48,11 +52,16 @@ bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t po } bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) { - if(port != 7) + if(port != 7) { return false; + } + meshlink_set_channel_receive_cb(mesh, channel, bar_receive_cb); - if(data) + + if(data) { bar_receive_cb(mesh, channel, data, len); + } + return true; } @@ -67,6 +76,7 @@ int main1(void) { meshlink_set_log_cb(NULL, debug_level, log_cb); meshlink_handle_t *mesh1 = meshlink_open("echo-fork_conf.1", "foo", "echo-fork", DEV_CLASS_BACKBONE); + if(!mesh1) { fprintf(stderr, "Could not initialize configuration for foo\n"); return 1; @@ -83,8 +93,10 @@ int main1(void) { for(int i = 0; i < 20; i++) { sleep(1); - if(bar_reachable) + + if(bar_reachable) { break; + } } if(!bar_reachable) { @@ -95,6 +107,7 @@ int main1(void) { // Open a channel from foo to bar. meshlink_node_t *bar = meshlink_get_node(mesh1, "bar"); + if(!bar) { fprintf(stderr, "Foo could not find bar\n"); return 1; @@ -109,8 +122,10 @@ int main1(void) { for(int i = 0; i < 5; i++) { sleep(1); - if(bar_responded) + + if(bar_responded) { break; + } } if(!bar_responded) { @@ -121,18 +136,24 @@ int main1(void) { do { //fprintf(stderr, ":"); ssize_t len = read(0, buffer, BUF_SIZE); - if(len <= 0) + + if(len <= 0) { break; + } + char *p = buffer; + while(len > 0) { ssize_t sent = meshlink_channel_send(mesh1, channel, p, len); + if(sent < 0) { fprintf(stderr, "Sending message failed\n"); return 1; } - if(!sent) + + if(!sent) { usleep(100000); - else { + } else { len -= sent; p += sent; } @@ -160,6 +181,7 @@ int main2(void) { meshlink_set_log_cb(NULL, debug_level, log_cb); meshlink_handle_t *mesh2 = meshlink_open("echo-fork_conf.2", "bar", "echo-fork", DEV_CLASS_BACKBONE); + if(!mesh2) { fprintf(stderr, "Could not initialize configuration for bar\n"); return 1; @@ -174,8 +196,9 @@ int main2(void) { return 1; } - while(!foo_closed) + while(!foo_closed) { sleep(1); + } // Clean up. @@ -198,8 +221,9 @@ int main(int argc, char *argv[]) { meshlink_close(foo); meshlink_close(bar); - if(fork()) + if(fork()) { return main1(); - else + } else { return main2(); + } } diff --git a/test/import-export.c b/test/import-export.c index 411a7a21..8330f05f 100644 --- a/test/import-export.c +++ b/test/import-export.c @@ -8,20 +8,23 @@ volatile bool bar_reachable = false; void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { - if(!strcmp(node->name, "bar")) + if(!strcmp(node->name, "bar")) { bar_reachable = reachable; + } } int main(int argc, char *argv[]) { // Open two new meshlink instance. meshlink_handle_t *mesh1 = meshlink_open("import_export_conf.1", "foo", "import-export", DEV_CLASS_BACKBONE); + if(!mesh1) { fprintf(stderr, "Could not initialize configuration for foo\n"); return 1; } meshlink_handle_t *mesh2 = meshlink_open("import_export_conf.2", "bar", "import-export", DEV_CLASS_BACKBONE); + if(!mesh2) { fprintf(stderr, "Could not initialize configuration for bar\n"); return 1; @@ -38,6 +41,7 @@ int main(int argc, char *argv[]) { meshlink_add_address(mesh2, "localhost"); char *data = meshlink_export(mesh1); + if(!data) { fprintf(stderr, "Foo could not export its configuration\n"); return 1; @@ -51,6 +55,7 @@ int main(int argc, char *argv[]) { free(data); data = meshlink_export(mesh2); + if(!data) { fprintf(stderr, "Bar could not export its configuration\n"); return 1; @@ -82,8 +87,10 @@ int main(int argc, char *argv[]) { for(int i = 0; i < 20; i++) { sleep(1); - if(bar_reachable) + + if(bar_reachable) { break; + } } if(!bar_reachable) { @@ -92,6 +99,7 @@ int main(int argc, char *argv[]) { } int pmtu = meshlink_get_pmtu(mesh2, meshlink_get_node(mesh2, "bar")); + for(int i = 0; i < 10 && !pmtu; i++) { sleep(1); pmtu = meshlink_get_pmtu(mesh2, meshlink_get_node(mesh2, "bar")); diff --git a/test/invite-join.c b/test/invite-join.c index d20ad37d..9f5cc806 100644 --- a/test/invite-join.c +++ b/test/invite-join.c @@ -8,20 +8,23 @@ volatile bool baz_reachable = false; void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { - if(!strcmp(node->name, "baz")) + if(!strcmp(node->name, "baz")) { baz_reachable = reachable; + } } int main(int argc, char *argv[]) { // Open two new meshlink instance. meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo", "invite-join", DEV_CLASS_BACKBONE); + if(!mesh1) { fprintf(stderr, "Could not initialize configuration for foo\n"); return 1; } meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar", "invite-join", DEV_CLASS_BACKBONE); + if(!mesh2) { fprintf(stderr, "Could not initialize configuration for bar\n"); return 1; @@ -43,6 +46,7 @@ int main(int argc, char *argv[]) { meshlink_add_address(mesh1, "localhost"); char *url = meshlink_invite(mesh1, "baz"); + if(!url) { fprintf(stderr, "Foo could not generate an invitation for baz\n"); return 1; @@ -66,8 +70,10 @@ int main(int argc, char *argv[]) { for(int i = 0; i < 60; i++) { sleep(1); - if(baz_reachable) + + if(baz_reachable) { break; + } } if(!baz_reachable) { @@ -76,6 +82,7 @@ int main(int argc, char *argv[]) { } int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz")); + for(int i = 0; i < 10 && !pmtu; i++) { sleep(1); pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz")); diff --git a/test/sign-verify.c b/test/sign-verify.c index bd05c112..a9e68278 100644 --- a/test/sign-verify.c +++ b/test/sign-verify.c @@ -9,12 +9,14 @@ int main(int argc, char *argv[]) { // 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; @@ -55,18 +57,21 @@ int main(int argc, char *argv[]) { 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"); + if(!foo) { fprintf(stderr, "Bar did not know about node foo\n"); return 1; } meshlink_node_t *bar = meshlink_get_node(mesh2, "bar"); + if(!bar) { fprintf(stderr, "Bar did not know about node bar\n"); return 1; @@ -83,14 +88,17 @@ int main(int argc, char *argv[]) { 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; diff --git a/test/trio.c b/test/trio.c index fbf7255c..bfeb4f6e 100644 --- a/test/trio.c +++ b/test/trio.c @@ -15,13 +15,17 @@ static void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const ch static struct timeval tv0; struct timeval tv; - if(tv0.tv_sec == 0) + if(tv0.tv_sec == 0) { gettimeofday(&tv0, NULL); + } + gettimeofday(&tv, NULL); fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000); - if(mesh) + if(mesh) { fprintf(stderr, "(%s) ", mesh->name); + } + fprintf(stderr, "[%d] %s\n", level, text); } @@ -29,8 +33,10 @@ static bool received = false; static void receive_cb(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) { fprintf(stderr, "RECEIVED SOMETHING\n"); - if(len == 5 && !memcmp(data, "Hello", 5)) + + if(len == 5 && !memcmp(data, "Hello", 5)) { received = true; + } } int main(int argc, char *argv[]) { @@ -69,8 +75,9 @@ int main(int argc, char *argv[]) { // start the nodes - for(int i = 0; i < 3; i++) + for(int i = 0; i < 3; i++) { meshlink_start(mesh[i]); + } // the nodes should now learn about each other @@ -99,8 +106,9 @@ int main(int argc, char *argv[]) { // Stop the other nodes - for(int i = 1; i < 3; i++) + for(int i = 1; i < 3; i++) { meshlink_stop(mesh[i]); + } sleep(1); @@ -108,8 +116,9 @@ int main(int argc, char *argv[]) { meshlink_set_log_cb(mesh[1], MESHLINK_DEBUG, log_cb); - for(int i = 1; i < 3; i++) + for(int i = 1; i < 3; i++) { meshlink_start(mesh[i]); + } assert(meshlink_get_node(mesh[1], name[2])); assert(meshlink_get_node(mesh[2], name[1])); @@ -121,6 +130,7 @@ int main(int argc, char *argv[]) { // Clean up. - for(int i = 0; i < 3; i++) + for(int i = 0; i < 3; i++) { meshlink_close(mesh[i]); + } } diff --git a/test/utils.c b/test/utils.c index d7505ced..4b2b2715 100644 --- a/test/utils.c +++ b/test/utils.c @@ -21,8 +21,9 @@ bool wait_sync_flag(struct sync_flag *s, int seconds) { timeout.tv_sec += seconds; while(!s->flag) - if(!pthread_cond_timedwait(&s->cond, &s->mutex, &timeout) || errno != EINTR) + if(!pthread_cond_timedwait(&s->cond, &s->mutex, &timeout) || errno != EINTR) { break; + } return s->flag; } diff --git a/test/utils.h b/test/utils.h index 488721dd..9fb9b8d2 100644 --- a/test/utils.h +++ b/test/utils.h @@ -25,14 +25,14 @@ extern void stop_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b); /// Stop and cleanup a pair of meshlink instances. extern void close_meshlink_pair(meshlink_handle_t *a, meshlink_handle_t *b, const char *prefix); -#define assert_after(cond, timeout) do {\ - for(int i = 0; i++ <= timeout;) {\ - if(cond)\ - break;\ - if(i == timeout)\ - assert(cond);\ - sleep(1);\ - }\ -} while(0); - +#define assert_after(cond, timeout)\ + do {\ + for(int i = 0; i++ <= timeout;) {\ + if(cond)\ + break;\ + if(i == timeout)\ + assert(cond);\ + sleep(1);\ + }\ + } while(0) #endif -- 2.39.5