t/common.perl | 15 ++++++++++++++- t/git-http-backend.t | 1 - t/httpd-corner.t | 9 ++------- t/httpd-https.t | 9 ++++----- t/httpd.t | 8 ++------ t/nntpd-tls.t | 7 +------ t/nntpd.t | 12 ++---------- t/v2writable.t | 1 - t/www_listing.t | 1 - diff --git a/t/common.perl b/t/common.perl index 91d65c5fa380a41b47420993198a57dfada4bc6d..ccc7be46099aaa7708cb65126d3400b98102a87a 100644 --- a/t/common.perl +++ b/t/common.perl @@ -1,10 +1,11 @@ -# Copyright (C) 2015-2018 all contributors +# Copyright (C) 2015-2019 all contributors # License: AGPL-3.0+ use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD); use POSIX qw(dup2); use strict; use warnings; +use IO::Socket::INET; sub stream_to_string { my ($res) = @_; @@ -35,6 +36,18 @@ Type => Socket::SOCK_STREAM(), Local => $_[0], ); $s->blocking(0); + $s; +} + +sub tcp_connect { + my ($dest, %opt) = @_; + my $s = IO::Socket::INET->new( + Proto => 'tcp', + Type => Socket::SOCK_STREAM(), + PeerAddr => $dest->sockhost . ':' . $dest->sockport, + %opt, + ); + $s->autoflush(1); $s; } diff --git a/t/git-http-backend.t b/t/git-http-backend.t index 946cd86aa36fa09a497d609e23b7c1d202264d3c..3623d47a4b6dea0a11cfc94790833ea838ff5dd5 100644 --- a/t/git-http-backend.t +++ b/t/git-http-backend.t @@ -7,7 +7,6 @@ use strict; use warnings; use Test::More; use File::Temp qw/tempdir/; -use IO::Socket::INET; use POSIX qw(setsid); my $git_dir = $ENV{GIANT_GIT_DIR}; diff --git a/t/httpd-corner.t b/t/httpd-corner.t index 35318b501f6dfd3f237f796d7e5f5ec031dc020f..c72bc9c6368cc2b3f6fb2f49045cb374ffe96a56 100644 --- a/t/httpd-corner.t +++ b/t/httpd-corner.t @@ -157,14 +157,9 @@ 'set REMOTE_ADDR and REMOTE_PORT for Unix socket'); } sub conn_for { - my ($sock, $msg) = @_; - my $conn = IO::Socket::INET->new( - PeerAddr => $sock->sockhost, - PeerPort => $sock->sockport, - Proto => 'tcp', - Type => SOCK_STREAM); + my ($dest, $msg) = @_; + my $conn = tcp_connect($dest); ok($conn, "connected for $msg"); - $conn->autoflush(1); setsockopt($conn, IPPROTO_TCP, TCP_NODELAY, 1); return $conn; } diff --git a/t/httpd-https.t b/t/httpd-https.t index 939669493254ef990fd2348fc2ec9c30a6a18cb4..410ae658325596196128326ddee7d435dd207fbc 100644 --- a/t/httpd-https.t +++ b/t/httpd-https.t @@ -32,7 +32,6 @@ kill 'TERM', $_ if defined $_; } }; my $https_addr = $https->sockhost . ':' . $https->sockport; -my %opt = ( Proto => 'tcp', PeerAddr => $https_addr, Type => SOCK_STREAM ); for my $args ( [ "-lhttps://$https_addr/?key=$key,cert=$cert" ], @@ -56,7 +55,7 @@ SSL_verify_mode => SSL_VERIFY_PEER(), SSL_ca_file => 'certs/test-ca.pem', ); # start negotiating a slow TLS connection - my $slow = IO::Socket::INET->new(%opt, Blocking => 0); + my $slow = tcp_connect($https, Blocking => 0); $slow = IO::Socket::SSL->start_SSL($slow, SSL_startHandshake => 0, %o); my @poll = (fileno($slow)); my $slow_done = $slow->connect_SSL; @@ -68,7 +67,7 @@ push @poll, PublicInbox::TLS::epollbit(); } # normal HTTPS - my $c = IO::Socket::INET->new(%opt); + my $c = tcp_connect($https); IO::Socket::SSL->start_SSL($c, %o); ok($c->print("GET /empty HTTP/1.1\r\n\r\nHost: example.com\r\n\r\n"), 'wrote HTTP request'); @@ -77,13 +76,13 @@ sysread($c, $buf, 2007, length($buf)) until $buf =~ /\r\n\r\n/; like($buf, qr!\AHTTP/1\.1 200!, 'read HTTP response'); # HTTPS with bad hostname - $c = IO::Socket::INET->new(%opt); + $c = tcp_connect($https); $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.fail'; $c = IO::Socket::SSL->start_SSL($c, %o); is($c, undef, 'HTTPS fails with bad hostname'); $o{SSL_hostname} = $o{SSL_verifycn_name} = 'server.local'; - $c = IO::Socket::INET->new(%opt); + $c = tcp_connect($https); IO::Socket::SSL->start_SSL($c, %o); ok($c, 'HTTPS succeeds again with valid hostname'); diff --git a/t/httpd.t b/t/httpd.t index e0a2bf44164a0f79d6be8b6a34471337645352b0..47ba7acc0c532af992ee3da36de4eca913925b8e 100644 --- a/t/httpd.t +++ b/t/httpd.t @@ -9,8 +9,7 @@ eval "require $mod"; plan skip_all => "$mod missing for httpd.t" if $@; } use File::Temp qw/tempdir/; -use IO::Socket::INET; -use Socket qw(IPPROTO_TCP); +use Socket qw(IPPROTO_TCP SOL_SOCKET); require './t/common.perl'; # FIXME: too much setup @@ -58,10 +57,7 @@ my $cmd = [ $httpd, "--stdout=$out", "--stderr=$err" ]; $pid = spawn_listener(undef, $cmd, [$sock]); my $host = $sock->sockhost; my $port = $sock->sockport; - my $conn = IO::Socket::INET->new(PeerAddr => $host, - PeerPort => $port, - Proto => 'tcp', - Type => SOCK_STREAM); + my $conn = tcp_connect($sock); ok($conn, 'connected'); ok($conn->write("GET / HTTP/1.0\r\n\r\n"), 'wrote data to socket'); { diff --git a/t/nntpd-tls.t b/t/nntpd-tls.t index 84d6e3c08f8bbc499e7698b33e81a7fb9f39092d..e961965bfc0b5a12ec2a173ad8199a9be91ec221 100644 --- a/t/nntpd-tls.t +++ b/t/nntpd-tls.t @@ -117,12 +117,7 @@ ); my $expect = { $group => [qw(1 1 n)] }; # start negotiating a slow TLS connection - my $slow = IO::Socket::INET->new( - Proto => 'tcp', - PeerAddr => $nntps_addr, - Type => SOCK_STREAM, - Blocking => 0, - ); + my $slow = tcp_connect($nntps, Blocking => 0); $slow = IO::Socket::SSL->start_SSL($slow, SSL_startHandshake => 0, %o); my $slow_done = $slow->connect_SSL; diag('W: connect_SSL early OK, slow client test invalid') if $slow_done; diff --git a/t/nntpd.t b/t/nntpd.t index b47cf7db33a512bcd204a05b6b0fd0ad7ea2e3b4..aa686e9c5a22435bb778d59286e831f538be2119 100644 --- a/t/nntpd.t +++ b/t/nntpd.t @@ -119,12 +119,6 @@ ok(!$n->starttls, 'STARTTLS fails when unconfigured'); is($n->code, 580, 'got 580 code on server w/o TLS'); }; - %opts = ( - PeerAddr => $host_port, - Proto => 'tcp', - Type => SOCK_STREAM, - Timeout => 1, - ); my $mid = ''; my %xhdr = ( 'message-id' => $mid, @@ -137,22 +131,20 @@ 'xref' => hostname . " $group:1", 'references' => '', ); - my $s = IO::Socket::INET->new(%opts); + my $s = tcp_connect($sock); sysread($s, my $buf, 4096); is($buf, "201 " . hostname . " ready - post via email\r\n", 'got greeting'); - $s->autoflush(1); ok(syswrite($s, " \r\n"), 'wrote spaces'); ok(syswrite($s, "\r\n"), 'wrote nothing'); syswrite($s, "NEWGROUPS\t19990424 000000 \033GMT\007\r\n"); is(0, sysread($s, $buf, 4096), 'GOT EOF on cntrl'); - $s = IO::Socket::INET->new(%opts); + $s = tcp_connect($sock); sysread($s, $buf, 4096); is($buf, "201 " . hostname . " ready - post via email\r\n", 'got greeting'); - $s->autoflush(1); syswrite($s, "CAPABILITIES\r\n"); $buf = read_til_dot($s); diff --git a/t/v2writable.t b/t/v2writable.t index 5406fd1b49e35f56c4cc24e72f91ab207ceff008..8e96ff00d106ef03ac695a458098620454d93b70 100644 --- a/t/v2writable.t +++ b/t/v2writable.t @@ -131,7 +131,6 @@ } { use Net::NNTP; - use IO::Socket::INET; my $err = "$mainrepo/stderr.log"; my $out = "$mainrepo/stdout.log"; my $group = 'inbox.comp.test.v2writable'; diff --git a/t/www_listing.t b/t/www_listing.t index a5d81f38c8f5975de39bd7ea4ab22adfb9c4f5df..990233c8ba203a5ce53b4c866c24901c402a7697 100644 --- a/t/www_listing.t +++ b/t/www_listing.t @@ -72,7 +72,6 @@ my $alt = "$tmpdir/alt.git"; my $cfgfile = "$tmpdir/config"; my $v2 = "$tmpdir/v2"; my $httpd = 'blib/script/public-inbox-httpd'; - use IO::Socket::INET; my $sock = tcp_server(); ok($sock, 'sock created'); my ($host, $port) = ($sock->sockhost, $sock->sockport);