]> Sergey Matveev's repositories - public-inbox.git/commitdiff
tests: add tcp_connect() helper
authorEric Wong <e@80x24.org>
Mon, 2 Sep 2019 04:51:31 +0000 (04:51 +0000)
committerEric Wong <e@80x24.org>
Mon, 9 Sep 2019 01:59:23 +0000 (01:59 +0000)
IO::Socket::INET->new is rather verbose with the options hash,
extract it into a standalone sub

t/common.perl
t/git-http-backend.t
t/httpd-corner.t
t/httpd-https.t
t/httpd.t
t/nntpd-tls.t
t/nntpd.t
t/v2writable.t
t/www_listing.t

index 91d65c5fa380a41b47420993198a57dfada4bc6d..ccc7be46099aaa7708cb65126d3400b98102a87a 100644 (file)
@@ -1,10 +1,11 @@
-# Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
+# Copyright (C) 2015-2019 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 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) = @_;
@@ -38,6 +39,18 @@ sub unix_server ($) {
        $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;
+}
+
 sub spawn_listener {
        my ($env, $cmd, $socks) = @_;
        my $pid = fork;
index 946cd86aa36fa09a497d609e23b7c1d202264d3c..3623d47a4b6dea0a11cfc94790833ea838ff5dd5 100644 (file)
@@ -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};
index 35318b501f6dfd3f237f796d7e5f5ec031dc020f..c72bc9c6368cc2b3f6fb2f49045cb374ffe96a56 100644 (file)
@@ -157,14 +157,9 @@ SKIP: {
 }
 
 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;
 }
index 939669493254ef990fd2348fc2ec9c30a6a18cb4..410ae658325596196128326ddee7d435dd207fbc 100644 (file)
@@ -32,7 +32,6 @@ END {
        }
 };
 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 @@ for my $args (
                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 @@ for my $args (
        }
 
        # 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 @@ for my $args (
        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');
 
index e0a2bf44164a0f79d6be8b6a34471337645352b0..47ba7acc0c532af992ee3da36de4eca913925b8e 100644 (file)
--- a/t/httpd.t
+++ b/t/httpd.t
@@ -9,8 +9,7 @@ foreach my $mod (qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status)) {
        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 @@ EOF
        $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');
        {
index 84d6e3c08f8bbc499e7698b33e81a7fb9f39092d..e961965bfc0b5a12ec2a173ad8199a9be91ec221 100644 (file)
@@ -117,12 +117,7 @@ for my $args (
        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;
index b47cf7db33a512bcd204a05b6b0fd0ad7ea2e3b4..aa686e9c5a22435bb778d59286e831f538be2119 100644 (file)
--- a/t/nntpd.t
+++ b/t/nntpd.t
@@ -119,12 +119,6 @@ EOF
                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 = '<nntp@example.com>';
        my %xhdr = (
                'message-id' => $mid,
@@ -137,22 +131,20 @@ EOF
                'references' => '<reftabsqueezed>',
        );
 
-       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);
index 5406fd1b49e35f56c4cc24e72f91ab207ceff008..8e96ff00d106ef03ac695a458098620454d93b70 100644 (file)
@@ -131,7 +131,6 @@ if ('ensure git configs are correct') {
 
 {
        use Net::NNTP;
-       use IO::Socket::INET;
        my $err = "$mainrepo/stderr.log";
        my $out = "$mainrepo/stdout.log";
        my $group = 'inbox.comp.test.v2writable';
index a5d81f38c8f5975de39bd7ea4ab22adfb9c4f5df..990233c8ba203a5ce53b4c866c24901c402a7697 100644 (file)
@@ -72,7 +72,6 @@ SKIP: {
        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);