1#!/usr/bin/perl 2 3use v5.14; 4use warnings; 5 6use Test::More; 7 8use IO::Socket::IP; 9 10sub arguments_is { 11 my ($arg, $exp, $name) = @_; 12 13 $arg = [$arg] 14 unless ref $arg; 15 16 $name ||= join ' ', map { defined $_ ? $_ : 'undef' } @$arg; 17 18 my $got = do { 19 no warnings 'redefine'; 20 my $args; 21 22 local *IO::Socket::IP::_io_socket_ip__configure = sub { 23 $args = $_[1]; 24 return $_[0]; 25 }; 26 27 IO::Socket::IP->new(@$arg); 28 29 $args; 30 }; 31 32 is_deeply($got, $exp, $name); 33} 34 35my @tests = ( 36 [ [ '[::1]:80' ], { PeerHost => '::1', PeerService => '80' } ], 37 [ [ '[::1]:http' ], { PeerHost => '::1', PeerService => 'http' } ], 38 [ [ '[::1]' ], { PeerHost => '::1', } ], 39 [ [ '[::1]:' ], { PeerHost => '::1', } ], 40 [ [ '127.0.0.1:80' ], { PeerHost => '127.0.0.1', PeerService => '80' } ], 41 [ [ '127.0.0.1:http' ], { PeerHost => '127.0.0.1', PeerService => 'http' } ], 42 [ [ '127.0.0.1' ], { PeerHost => '127.0.0.1', } ], 43 [ [ '127.0.0.1:' ], { PeerHost => '127.0.0.1', } ], 44 [ [ 'localhost:80' ], { PeerHost => 'localhost', PeerService => '80' } ], 45 [ [ 'localhost:http' ], { PeerHost => 'localhost', PeerService => 'http' } ], 46 [ [ PeerHost => '[::1]:80' ], { PeerHost => '::1', PeerService => '80' } ], 47 [ [ PeerHost => '[::1]' ], { PeerHost => '::1' } ], 48 [ [ LocalHost => '[::1]:80' ], { LocalHost => '::1', LocalService => '80' } ], 49 [ [ LocalHost => undef ], { LocalHost => undef } ], 50 51 # IO::Socket::INET is happy to take port from the *Host argument even if a *Port argument 52 # exists 53 [ [ PeerHost => '127.0.0.1:80', PeerPort => '80' ], { PeerHost => '127.0.0.1', PeerService => '80' } ], 54 # *Host argument should take precedence over *Service if both exist 55 [ [ PeerHost => '127.0.0.1:443', PeerPort => '80' ], { PeerHost => '127.0.0.1', PeerService => '443' } ], 56); 57 58is_deeply( [ IO::Socket::IP->split_addr( "hostname:http" ) ], 59 [ "hostname", "http" ], 60 "split_addr hostname:http" ); 61 62is_deeply( [ IO::Socket::IP->split_addr( "192.0.2.1:80" ) ], 63 [ "192.0.2.1", "80" ], 64 "split_addr 192.0.2.1:80" ); 65 66is_deeply( [ IO::Socket::IP->split_addr( "[2001:db8::1]:80" ) ], 67 [ "2001:db8::1", "80" ], 68 "split_addr [2001:db8::1]:80" ); 69 70is_deeply( [ IO::Socket::IP->split_addr( "something.else" ) ], 71 [ "something.else", undef ], 72 "split_addr something.else" ); 73 74is( IO::Socket::IP->join_addr( "hostname", "http" ), 75 "hostname:http", 76 'join_addr hostname:http' ); 77 78is( IO::Socket::IP->join_addr( "192.0.2.1", 80 ), 79 "192.0.2.1:80", 80 'join_addr 192.0.2.1:80' ); 81 82is( IO::Socket::IP->join_addr( "2001:db8::1", 80 ), 83 "[2001:db8::1]:80", 84 'join_addr [2001:db8::1]:80' ); 85 86is( IO::Socket::IP->join_addr( "something.else", undef ), 87 "something.else", 88 'join_addr something.else' ); 89 90arguments_is(@$_) for @tests; 91 92done_testing; 93