From: Guus Sliepen Date: Sat, 1 Nov 2014 21:06:56 +0000 (+0100) Subject: Add a version of the channels test that forks. X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=4c9d02d9b204f3300c28331b00baa32b2690a930;p=meshlink Add a version of the channels test that forks. This creates two separate programs that each run one MeshLink instance, instead of one program that runs two instances simultaneously. --- diff --git a/test/Makefile.am b/test/Makefile.am index a95d22ec..3828584c 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,6 +1,8 @@ TESTS = \ basic.test \ basicpp.test \ + channels.test \ + channels-fork.test \ import-export.test \ invite-join.test \ sign-verify.test @@ -9,7 +11,7 @@ dist_check_SCRIPTS = $(TESTS) AM_CPPFLAGS = -I../src -check_PROGRAMS = basic basicpp import-export invite-join sign-verify +check_PROGRAMS = basic basicpp channels channels-fork import-export invite-join sign-verify basic_SOURCES = basic.c basic_LDADD = ../src/libmeshlink.la @@ -17,6 +19,12 @@ basic_LDADD = ../src/libmeshlink.la basicpp_SOURCES = basicpp.cpp basicpp_LDADD = ../src/libmeshlink.la +channels_SOURCES = channels.cpp +channels_LDADD = ../src/libmeshlink.la + +channels_fork_SOURCES = channels-fork.cpp +channels_fork_LDADD = ../src/libmeshlink.la + import_export_SOURCES = import-export.c import_export_LDADD = ../src/libmeshlink.la diff --git a/test/channels-fork.c b/test/channels-fork.c new file mode 100755 index 00000000..3a9615ff --- /dev/null +++ b/test/channels-fork.c @@ -0,0 +1,192 @@ +#define _GNU_SOURCE +#include +#include +#include +#include + +#include "../src/meshlink.h" + +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) + 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")) + 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)) + bar_responded = true; +} + +void bar_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { + // Echo the data back. + meshlink_channel_send(mesh, channel, data, len); +} + +bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) { + return false; +} + +bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) { + if(port != 7) + return false; + meshlink_set_channel_receive_cb(mesh, channel, bar_receive_cb); + 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) + 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; + } + + meshlink_add_address(mesh1, "localhost"); + + char *data = meshlink_export(mesh1); + if(!data) { + fprintf(stderr, "Foo could not export its configuration\n"); + return 1; + } + + size_t len = strlen(data); + write(wfd, &len, sizeof len); + write(wfd, data, len); + free(data); + + read(rfd, &len, sizeof len); + char indata[len + 1]; + read(rfd, indata, len); + indata[len] = 0; + + fprintf(stderr, "Foo exchanged data\n"); + + meshlink_import(mesh1, indata); + + meshlink_set_channel_accept_cb(mesh1, reject_cb); + meshlink_set_node_status_cb(mesh1, status_cb); + + if(!meshlink_start(mesh1)) { + fprintf(stderr, "Foo could not start\n"); + return 1; + } + + for(int i = 0; i < 20; i++) { + sleep(1); + if(bar_reachable) + break; + } + + if(!bar_reachable) { + fprintf(stderr, "Bar not reachable for foo after 20 seconds\n"); + return 1; + } + + // 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; + } + + meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7, foo_receive_cb, NULL, 0); + meshlink_set_channel_poll_cb(mesh1, channel, poll_cb); + + for(int i = 0; i < 5; i++) { + sleep(1); + if(bar_responded) + break; + } + + if(!bar_responded) { + fprintf(stderr, "Bar did not respond to foo's channel message\n"); + return 1; + } + + meshlink_channel_close(mesh1, channel); + + // Clean up. + + meshlink_close(mesh1); + + return 0; +} + + +int main2(int rfd, int wfd) { + sleep(1); + + 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; + } + + 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(); + free(data); + + read(rfd, &len, sizeof len); + char indata[len + 1]; + read(rfd, indata, len); + indata[len] = 0; + + fprintf(stderr, "Bar exchanged data\n"); + + meshlink_import(mesh2, indata); + + meshlink_set_channel_accept_cb(mesh2, accept_cb); + + if(!meshlink_start(mesh2)) { + fprintf(stderr, "Bar could not start\n"); + return 1; + } + + sleep(20); + + // Clean up. + + meshlink_close(mesh2); + + return 0; +} + + +int main(int argc, char *argv[]) { + int fda[2], fdb[2], result; + + pipe2(fda, 0); + pipe2(fdb, 0); + + if(fork()) + return main1(fda[0], fdb[1]); + else + return main2(fdb[0], fda[1]); +} diff --git a/test/channels-fork.test b/test/channels-fork.test new file mode 100755 index 00000000..7a514f9e --- /dev/null +++ b/test/channels-fork.test @@ -0,0 +1,4 @@ +#!/bin/sh + +rm -Rf channels_conf.* +./channels-fork diff --git a/test/channels.c b/test/channels.c index 4a96541f..f572d1b1 100644 --- a/test/channels.c +++ b/test/channels.c @@ -3,11 +3,17 @@ #include #include -#include "meshlink.h" +#include "../src/meshlink.h" 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) + 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")) bar_reachable = reachable; @@ -23,7 +29,11 @@ void bar_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const meshlink_channel_send(mesh, channel, data, len); } -bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_node_t *node, uint16_t port, const void *data, size_t len) { +bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) { + return false; +} + +bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) { if(port != 7) return false; meshlink_set_channel_receive_cb(mesh, channel, bar_receive_cb); @@ -32,7 +42,15 @@ bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_no 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) + fprintf(stderr, "Could not send whole message\n"); +} + int main(int argc, char *argv[]) { + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + // Open two new meshlink instance. meshlink_handle_t *mesh1 = meshlink_open("channels_conf.1", "foo", "channels", DEV_CLASS_BACKBONE); @@ -71,7 +89,6 @@ int main(int argc, char *argv[]) { return 1; } - if(!meshlink_import(mesh1, data)) { fprintf(stderr, "Foo could not import bar's configuration\n"); return 1; @@ -79,19 +96,22 @@ int main(int argc, char *argv[]) { free(data); - // Set the channel accept callback on bar. + // Set the callbacks. + meshlink_set_channel_accept_cb(mesh1, reject_cb); meshlink_set_channel_accept_cb(mesh2, accept_cb); - // Start both instances - meshlink_set_node_status_cb(mesh1, status_cb); + // Start both instances + if(!meshlink_start(mesh1)) { fprintf(stderr, "Foo could not start\n"); return 1; } + usleep(123456); + if(!meshlink_start(mesh2)) { fprintf(stderr, "Bar could not start\n"); return 1; @@ -110,6 +130,8 @@ int main(int argc, char *argv[]) { return 1; } + sleep(1); + // Open a channel from foo to bar. meshlink_node_t *bar = meshlink_get_node(mesh1, "bar"); @@ -118,7 +140,8 @@ int main(int argc, char *argv[]) { return 1; } - meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7, foo_receive_cb, "Hello", 5); + meshlink_channel_t *channel = meshlink_channel_open(mesh1, bar, 7, foo_receive_cb, NULL, 0); + meshlink_set_channel_poll_cb(mesh1, channel, poll_cb); for(int i = 0; i < 5; i++) { sleep(1); diff --git a/test/channels.test b/test/channels.test index 8a46d210..2c6111b9 100755 --- a/test/channels.test +++ b/test/channels.test @@ -1,4 +1,4 @@ #!/bin/sh -rm -Rf channels_conf +rm -Rf channels_conf.* ./channels