user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* [PATCH] tests: add tcp_connect() helper
@ 2019-09-09  2:18  7% Eric Wong
  0 siblings, 0 replies; 1+ results
From: Eric Wong @ 2019-09-09  2:18 UTC (permalink / raw)
  To: meta

IO::Socket::INET->new is rather verbose with the options hash,
extract it into a standalone sub
---
 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 -
 9 files changed, 25 insertions(+), 38 deletions(-)

diff --git a/t/common.perl b/t/common.perl
index 91d65c5f..ccc7be46 100644
--- a/t/common.perl
+++ b/t/common.perl
@@ -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;
diff --git a/t/git-http-backend.t b/t/git-http-backend.t
index 946cd86a..3623d47a 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 35318b50..c72bc9c6 100644
--- a/t/httpd-corner.t
+++ b/t/httpd-corner.t
@@ -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;
 }
diff --git a/t/httpd-https.t b/t/httpd-https.t
index 93966949..410ae658 100644
--- a/t/httpd-https.t
+++ b/t/httpd-https.t
@@ -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');
 
diff --git a/t/httpd.t b/t/httpd.t
index e0a2bf44..47ba7acc 100644
--- 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');
 	{
diff --git a/t/nntpd-tls.t b/t/nntpd-tls.t
index 84d6e3c0..e961965b 100644
--- a/t/nntpd-tls.t
+++ b/t/nntpd-tls.t
@@ -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;
diff --git a/t/nntpd.t b/t/nntpd.t
index b47cf7db..aa686e9c 100644
--- 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);
diff --git a/t/v2writable.t b/t/v2writable.t
index 5406fd1b..8e96ff00 100644
--- a/t/v2writable.t
+++ b/t/v2writable.t
@@ -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';
diff --git a/t/www_listing.t b/t/www_listing.t
index a5d81f38..990233c8 100644
--- a/t/www_listing.t
+++ b/t/www_listing.t
@@ -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);

^ permalink raw reply related	[relevance 7%]

Results 1-1 of 1 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2019-09-09  2:18  7% [PATCH] tests: add tcp_connect() helper Eric Wong

Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).