about summary refs log tree commit homepage
path: root/lib/PublicInbox/TestCommon.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2021-02-07 23:05:13 -1000
committerEric Wong <e@80x24.org>2021-02-08 22:07:43 +0000
commit860169adcd29341142b7c4a369c09b4ac492bd1e (patch)
treef08dcf8bd04efaeb0904a6cec40665c107482cac /lib/PublicInbox/TestCommon.pm
parentfb6aa4da304125a3fb7932e1335be85ff260e031 (diff)
downloadpublic-inbox-860169adcd29341142b7c4a369c09b4ac492bd1e.tar.gz
IPv4 gets plenty of real-world coverage, and apparently there's
Debian buildd hosts which lack IPv4(*).  So ensure everything
can work on IPv6 and not cause problems for odd setups.

(*) https://bugs.debian.org/979432
Diffstat (limited to 'lib/PublicInbox/TestCommon.pm')
-rw-r--r--lib/PublicInbox/TestCommon.pm30
1 files changed, 24 insertions, 6 deletions
diff --git a/lib/PublicInbox/TestCommon.pm b/lib/PublicInbox/TestCommon.pm
index 5cce44e4..2f4ca622 100644
--- a/lib/PublicInbox/TestCommon.pm
+++ b/lib/PublicInbox/TestCommon.pm
@@ -14,7 +14,7 @@ BEGIN {
         @EXPORT = qw(tmpdir tcp_server tcp_connect require_git require_mods
                 run_script start_script key2sub xsys xsys_e xqx eml_load tick
                 have_xapian_compact json_utf8 setup_public_inboxes
-                test_lei $lei $lei_out $lei_err $lei_opt);
+                tcp_host_port test_lei $lei $lei_out $lei_err $lei_opt);
         require Test::More;
         my @methods = grep(!/\W/, @Test::More::EXPORT);
         eval(join('', map { "*$_=\\&Test::More::$_;" } @methods));
@@ -40,20 +40,38 @@ sub tmpdir (;$) {
 }
 
 sub tcp_server () {
-        IO::Socket::INET->new(
-                LocalAddr => '127.0.0.1',
+        my %opt = (
                 ReuseAddr => 1,
                 Proto => 'tcp',
                 Type => Socket::SOCK_STREAM(),
                 Listen => 1024,
                 Blocking => 0,
-        ) or BAIL_OUT "failed to create TCP server: $!";
+        );
+        eval {
+                die 'IPv4-only' if $ENV{TEST_IPV4_ONLY};
+                require IO::Socket::INET6;
+                IO::Socket::INET6->new(%opt, LocalAddr => '[::1]')
+        } || eval {
+                die 'IPv6-only' if $ENV{TEST_IPV6_ONLY};
+                IO::Socket::INET->new(%opt, LocalAddr => '127.0.0.1')
+        } || BAIL_OUT "failed to create TCP server: $! ($@)";
+}
+
+sub tcp_host_port ($) {
+        my ($s) = @_;
+        my ($h, $p) = ($s->sockhost, $s->sockport);
+        my $ipv4 = $s->sockdomain == Socket::AF_INET();
+        if (wantarray) {
+                $ipv4 ? ($h, $p) : ("[$h]", $p);
+        } else {
+                $ipv4 ? "$h:$p" : "[$h]:$p";
+        }
 }
 
 sub tcp_connect {
         my ($dest, %opt) = @_;
-        my $addr = $dest->sockhost . ':' . $dest->sockport;
-        my $s = IO::Socket::INET->new(
+        my $addr = tcp_host_port($dest);
+        my $s = ref($dest)->new(
                 Proto => 'tcp',
                 Type => Socket::SOCK_STREAM(),
                 PeerAddr => $addr,