From 753280da5d9ac8e9e2580b4ac6f3034e50cff6c8 Mon Sep 17 00:00:00 2001 From: Daniel Collins Date: Wed, 20 Sep 2017 00:07:50 +0100 Subject: [PATCH] Improved bind tests. Verify processes exiting abnormally don't hold onto addresses and check the error code when bind() fails. --- tests/20-bind.t | 29 +++++++++++++++++++++++++---- tests/lib/IPXWrapper/Tool/Bind.pm | 6 +++--- tools/bind.c | 10 +++++++++- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/tests/20-bind.t b/tests/20-bind.t index 599a1e8..6eaca08 100644 --- a/tests/20-bind.t +++ b/tests/20-bind.t @@ -38,6 +38,7 @@ use constant { NSPROTO_IPX => 1000, NSPROTO_SPX => 1256, NSPROTO_SPXII => 1257, + WSAEADDRINUSE => 10048, }; my ($bind_type_a, $bind_proto_a); @@ -124,7 +125,7 @@ shared_examples_for "bind address selection/reuse for one protocol" => sub cmp_deeply(\@result, [ { net => "00:00:00:01", node => $remote_mac_a, sock => 1 }, - undef, + { errno => WSAEADDRINUSE }, ]); }; @@ -137,7 +138,7 @@ shared_examples_for "bind address selection/reuse for one protocol" => sub cmp_deeply(\@result, [ { net => "00:00:00:01", node => $remote_mac_a, sock => 1 }, - undef, + { errno => WSAEADDRINUSE }, ]); }; @@ -208,7 +209,7 @@ shared_examples_for "bind address selection/reuse for one protocol" => sub ]); cmp_deeply([ $bind_b->result() ], [ - undef, + { errno => WSAEADDRINUSE }, ]); }; @@ -227,7 +228,7 @@ shared_examples_for "bind address selection/reuse for one protocol" => sub ]); cmp_deeply([ $bind_b->result() ], [ - undef, + { errno => WSAEADDRINUSE }, ]); }; @@ -338,6 +339,26 @@ shared_examples_for "bind address selection/reuse for one protocol" => sub { net => "00:00:00:01", node => $remote_mac_a, sock => 1 }, ]); }; + + it "allows address reuse (between processes) if first process exits uncleanly" => sub + { + my $bind_a = IPXWrapper::Tool::Bind->new($remote_ip_a, + $bind_type_a, $bind_proto_a, "00:00:00:00", $remote_mac_a, "1", + "-e", # Forces early _exit() + ); + + my $bind_b = IPXWrapper::Tool::Bind->new($remote_ip_a, + $bind_type_b, $bind_proto_b, "00:00:00:00", $remote_mac_a, "1", + ); + + cmp_deeply([ $bind_a->result() ], [ + { net => "00:00:00:01", node => $remote_mac_a, sock => 1 }, + ]); + + cmp_deeply([ $bind_b->result() ], [ + { net => "00:00:00:01", node => $remote_mac_a, sock => 1 }, + ]); + }; }; describe "bind" => sub diff --git a/tests/lib/IPXWrapper/Tool/Bind.pm b/tests/lib/IPXWrapper/Tool/Bind.pm index 57c5915..9d27e34 100644 --- a/tests/lib/IPXWrapper/Tool/Bind.pm +++ b/tests/lib/IPXWrapper/Tool/Bind.pm @@ -56,11 +56,11 @@ sub new sock => $3, }); } - elsif($line eq "Failed") + elsif($line =~ m/^Failed \(([0-9]+)\)$/) { - push(@{ $self->{sockets} }, undef); + push(@{ $self->{sockets} }, { errno => $1 }); } - elsif($line eq "Ready") + elsif($line eq "Ready" || $line eq "Calling _exit") { return $self; } diff --git a/tools/bind.c b/tools/bind.c index 698c5c9..7f3f3e9 100644 --- a/tools/bind.c +++ b/tools/bind.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -38,6 +39,12 @@ int main(int argc, const char **argv) for(int i = 1; i < argc; i += 5) { + if(strcmp(argv[i], "-e") == 0) + { + printf("Calling _exit\n"); + _exit(0); + } + BOOL reuse = FALSE; if(strcmp(argv[i], "-r") == 0) { @@ -56,6 +63,7 @@ int main(int argc, const char **argv) { fprintf(stderr, "Usage: %s\n" "\t[-r] [-c] \n" + "\t[-e]\n" "\t...\n", argv[0]); return 1; } @@ -83,7 +91,7 @@ int main(int argc, const char **argv) printf("Bound socket to %s\n", bound_addr); } else{ - printf("Failed\n"); + printf("Failed (%u)\n", (unsigned int)(WSAGetLastError())); } if(close)