diff --git a/Makefile b/Makefile index ee3e80a..a9f618a 100644 --- a/Makefile +++ b/Makefile @@ -67,7 +67,7 @@ test: $(TEST_DLLS) tests/addr.exe tests/bind.exe ./tests/addr.exe cp $(TEST_DLLS) tests/ - cd tests; perl bind.pl + cd tests/; prove bind.t tests/addr.exe: tests/addr.c src/addr.o tests/bind.exe: tests/bind.c diff --git a/manifest.src.txt b/manifest.src.txt index b2ca92e..9c88f28 100644 --- a/manifest.src.txt +++ b/manifest.src.txt @@ -57,5 +57,4 @@ directplay-win64.reg tests/addr.c tests/bind.c -tests/bind.pl -tests/ipxtest.pm +tests/bind.t diff --git a/tests/bind.pl b/tests/bind.pl deleted file mode 100644 index 1fe5d47..0000000 --- a/tests/bind.pl +++ /dev/null @@ -1,98 +0,0 @@ -# IPXWrapper - Tests for bind -# Copyright (C) 2012 Daniel Collins -# -# This program is free software; you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 2 as published by -# the Free Software Foundation. -# -# This program is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for -# more details. -# -# You should have received a copy of the GNU General Public License along with -# this program; if not, write to the Free Software Foundation, Inc., 51 -# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -use strict; -use warnings; - -use ipxtest; - -my @tests = ( - # Single process, unique socket numbers. - - [ "bind.exe 0 1234 0 1235" => "OK" ], - [ "bind.exe 0 1234 1 1235" => "OK" ], - - # Two processes, unique socket numbers. - - [ - "bind.exe 0 1234" => "OK", - "bind.exe 0 1235" => "OK", - ], - - [ - "bind.exe 0 1234" => "OK", - "bind.exe 1 1235" => "OK", - ], - - # Single process, bind to two random sockets. - - [ "bind.exe 0 0 0 0" => "OK" ], - [ "bind.exe 0 0 1 0" => "OK" ], - - # Two processes, bind to random sockets. - - [ - "bind.exe 0 0" => "OK", - "bind.exe 0 0" => "OK", - ], - - [ - "bind.exe 0 0" => "OK", - "bind.exe 1 0" => "OK", - ], - - # Single process, conflicting addresses. - - [ "bind.exe 0 1234 0 1234" => "FAIL" ], - [ "bind.exe 0 1234 1 1234" => "FAIL" ], - - # Two processes, conflicting addresses. - - [ - "bind.exe 0 1234" => "OK", - "bind.exe 0 1234" => "FAIL" - ], - - [ - "bind.exe 0 1234" => "OK", - "bind.exe 1 1234" => "FAIL" - ], - - # SO_REUSEADDR within one process - - [ "bind.exe --reuse 0 1234 --reuse 0 1234" => "OK" ], - [ "bind.exe --reuse 0 1234 0 1234" => "FAIL" ], - [ "bind.exe 0 1234 --reuse 0 1234" => "FAIL" ], - - # SO_REUSEADDR between processes - - [ - "bind.exe --reuse 0 1234" => "OK", - "bind.exe --reuse 0 1234" => "OK", - ], - - [ - "bind.exe --reuse 0 1234" => "OK", - "bind.exe 0 1234" => "FAIL", - ], - - [ - "bind.exe 0 1234" => "OK", - "bind.exe --reuse 0 1234" => "FAIL", - ], -); - -IPXWrapper::Testing::run_tests(@tests); diff --git a/tests/bind.t b/tests/bind.t new file mode 100644 index 0000000..9be0bfa --- /dev/null +++ b/tests/bind.t @@ -0,0 +1,177 @@ +# IPXWrapper - Tests for bind +# Copyright (C) 2012-2014 Daniel Collins +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License version 2 as published by +# the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., 51 +# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +use strict; +use warnings; + +use Test::More tests => 28; +use IPC::Open2; + +# TODO: Test there are enough interfaces. + +try_binds( + "Single process, single socket", + + "bind.exe 0 1234" => "OK", +); + +try_binds( + "Single process, unique socket numbers, same interface", + + "bind.exe 0 1234 0 1235" => "OK", +); + +try_binds( + "Single process, unique socket numbers, seperate interfaces", + + "bind.exe 0 1234 1 1235" => "OK", +); + +# TODO: Test randomly allocated socket numbers are sensible. + +try_binds( + "Single process, random socket numbers, same interface", + + "bind.exe 0 0 0 0" => "OK", +); + +try_binds( + "Single process, random socket numbers, seperate interfaces", + + "bind.exe 0 0 1 0" => "OK", +); + +try_binds( + "Single process, conflicting socket numbers, same interface", + + "bind.exe 0 1234 0 1234" => "FAIL", +); + +try_binds( + "Single process, conflicting socket numbers, seperate interfaces", + + "bind.exe 0 1234 1 1234" => "FAIL", +); + +try_binds( + "Single process, conflicting socket numbers, both using SO_REUSEADDR", + + "bind.exe --reuse 0 1234 --reuse 0 1234" => "OK", +); + +try_binds( + "Single process, conflicting socket numbers, first using SO_REUSEADDR", + + "bind.exe --reuse 0 1234 0 1234" => "FAIL", +); + +try_binds( + "Single process, conflicting socket numbers, second using SO_REUSEADDR", + + "bind.exe 0 1234 --reuse 0 1234" => "FAIL", +); + +try_binds( + "Two processes, unique socket numbers, same interface", + + "bind.exe 0 1234" => "OK", + "bind.exe 0 1235" => "OK", +); + +try_binds( + "Two processes, unique socket numbers, seperate interfaces", + + "bind.exe 0 1234" => "OK", + "bind.exe 1 1235" => "OK", +); + +try_binds( + "Two processes, random socket numbers, same interface", + + "bind.exe 0 0" => "OK", + "bind.exe 0 0" => "OK", +); + +try_binds( + "Two processes, random socket numbers, seperate interfaces", + + "bind.exe 0 0" => "OK", + "bind.exe 1 0" => "OK", +); + +try_binds( + "Two processes, conflicting socket numbers, same interface", + + "bind.exe 0 1234" => "OK", + "bind.exe 0 1234" => "FAIL", +); + +try_binds( + "Two processes, conflicting socket numbers, seperate interfaces", + + "bind.exe 0 1234" => "OK", + "bind.exe 1 1234" => "FAIL", +); + +try_binds( + "Two processes, conflicting socket numbers, both using SO_REUSEADDR", + + "bind.exe --reuse 0 1234" => "OK", + "bind.exe --reuse 0 1234" => "OK", +); + +try_binds( + "Two processes, conflicting socket numbers, first using SO_REUSEADDR", + + "bind.exe --reuse 0 1234" => "OK", + "bind.exe 0 1234" => "FAIL", +); + +try_binds( + "Two processes, conflicting socket numbers, second using SO_REUSEADDR", + + "bind.exe 0 1234" => "OK", + "bind.exe --reuse 0 1234" => "FAIL", +); + +sub try_binds +{ + my ($desc, @cmds) = @_; + + diag("$desc..."); + + my @stdins = (); + + for(my $i = 0; $i < (scalar @cmds); $i += 2) + { + my $command = $cmds[$i]; + my $expect = $cmds[$i + 1]; + + my $pid = open2(my $stdout, my $stdin, $command); + push(@stdins, $stdin); + + (my $result = <$stdout>) =~ s/[\r\n]//g; + + is($result, $expect, $command); + } + + # Tell any remaining processes to exit. + + foreach my $stdin(@stdins) + { + print {$stdin} "\n"; + } +} diff --git a/tests/ipxtest.pm b/tests/ipxtest.pm deleted file mode 100644 index 6479723..0000000 --- a/tests/ipxtest.pm +++ /dev/null @@ -1,81 +0,0 @@ -# IPXWrapper - Test helpers -# Copyright (C) 2012 Daniel Collins -# -# This program is free software; you can redistribute it and/or modify it -# under the terms of the GNU General Public License version 2 as published by -# the Free Software Foundation. -# -# This program is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for -# more details. -# -# You should have received a copy of the GNU General Public License along with -# this program; if not, write to the Free Software Foundation, Inc., 51 -# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -package IPXWrapper::Testing; - -use strict; -use warnings; - -use IPC::Open2; - -sub run_tests -{ - my (@tests) = @_; - - my $total_tests = 0; - my $passed_tests = 0; - - my $test_n = 1; - - foreach my $test(@tests) - { - my @procs = (); - - for(my $i = 0, my $subtest = 1; $i < scalar @$test; $i += 2, $subtest++) - { - my $command = $test->[$i]; - my $expect = $test->[$i + 1]; - - my $pid = open2(my $stdout, my $stdin, $command); - - push(@procs, $stdin); - - (my $result = <$stdout>) =~ s/[\r\n]//g; - - print "Test $test_n-$subtest: "; - - if($result eq $expect) - { - print "PASSED\n"; - - $passed_tests++; - } - else{ - print "FAILED\n"; - print "\tCommand: $command\n"; - print "\tResult: $result\n"; - print "\tExpected: $expect\n"; - } - - $total_tests++; - } - - # Tell any remaining processes to exit. - - foreach my $stdin(@procs) - { - print {$stdin} "\n"; - } - - $test_n++; - } - - print "\n-- Passed $passed_tests/$total_tests tests\n"; - - exit($total_tests == $passed_tests ? 0 : 1); -} - -1;