1*0Sstevel@tonic-gate#!./perl 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateBEGIN { 4*0Sstevel@tonic-gate chdir 't' if -d 't'; 5*0Sstevel@tonic-gate @INC = '../lib'; 6*0Sstevel@tonic-gate require Config; import Config; 7*0Sstevel@tonic-gate if ($Config{'extensions'} !~ /\bSocket\b/ && 8*0Sstevel@tonic-gate !(($^O eq 'VMS') && $Config{d_socket})) { 9*0Sstevel@tonic-gate print "1..0\n"; 10*0Sstevel@tonic-gate exit 0; 11*0Sstevel@tonic-gate } 12*0Sstevel@tonic-gate $has_alarm = $Config{d_alarm}; 13*0Sstevel@tonic-gate} 14*0Sstevel@tonic-gate 15*0Sstevel@tonic-gateuse Socket; 16*0Sstevel@tonic-gate 17*0Sstevel@tonic-gateprint "1..17\n"; 18*0Sstevel@tonic-gate 19*0Sstevel@tonic-gate$has_echo = $^O ne 'MSWin32'; 20*0Sstevel@tonic-gate$alarmed = 0; 21*0Sstevel@tonic-gatesub arm { $alarmed = 0; alarm(shift) if $has_alarm } 22*0Sstevel@tonic-gatesub alarmed { $alarmed = 1 } 23*0Sstevel@tonic-gate$SIG{ALRM} = 'alarmed' if $has_alarm; 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gateif (socket(T,PF_INET,SOCK_STREAM,6)) { 26*0Sstevel@tonic-gate print "ok 1\n"; 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate arm(5); 29*0Sstevel@tonic-gate my $host = $^O eq 'MacOS' || ($^O eq 'irix' && $Config{osvers} == 5) ? 30*0Sstevel@tonic-gate '127.0.0.1' : 'localhost'; 31*0Sstevel@tonic-gate my $localhost = inet_aton($host); 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate if ($has_echo && defined $localhost && connect(T,pack_sockaddr_in(7,$localhost))){ 34*0Sstevel@tonic-gate arm(0); 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate print "ok 2\n"; 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate print "# Connected to " . 39*0Sstevel@tonic-gate inet_ntoa((unpack_sockaddr_in(getpeername(T)))[1])."\n"; 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate arm(5); 42*0Sstevel@tonic-gate syswrite(T,"hello",5); 43*0Sstevel@tonic-gate arm(0); 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate arm(5); 46*0Sstevel@tonic-gate $read = sysread(T,$buff,10); # Connection may be granted, then closed! 47*0Sstevel@tonic-gate arm(0); 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate while ($read > 0 && length($buff) < 5) { 50*0Sstevel@tonic-gate # adjust for fact that TCP doesn't guarantee size of reads/writes 51*0Sstevel@tonic-gate arm(5); 52*0Sstevel@tonic-gate $read = sysread(T,$buff,10,length($buff)); 53*0Sstevel@tonic-gate arm(0); 54*0Sstevel@tonic-gate } 55*0Sstevel@tonic-gate print(($read == 0 || $buff eq "hello") ? "ok 3\n" : "not ok 3\n"); 56*0Sstevel@tonic-gate } 57*0Sstevel@tonic-gate else { 58*0Sstevel@tonic-gate print "# You're allowed to fail tests 2 and 3 if\n"; 59*0Sstevel@tonic-gate print "# the echo service has been disabled or if your\n"; 60*0Sstevel@tonic-gate print "# gethostbyname() cannot resolve your localhost.\n"; 61*0Sstevel@tonic-gate print "# 'Connection refused' indicates disabled echo service.\n"; 62*0Sstevel@tonic-gate print "# 'Interrupted system call' indicates a hanging echo service.\n"; 63*0Sstevel@tonic-gate print "# Error: $!\n"; 64*0Sstevel@tonic-gate print "ok 2 - skipped\n"; 65*0Sstevel@tonic-gate print "ok 3 - skipped\n"; 66*0Sstevel@tonic-gate } 67*0Sstevel@tonic-gate} 68*0Sstevel@tonic-gateelse { 69*0Sstevel@tonic-gate print "# Error: $!\n"; 70*0Sstevel@tonic-gate print "not ok 1\n"; 71*0Sstevel@tonic-gate} 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gateif( socket(S,PF_INET,SOCK_STREAM,6) ){ 74*0Sstevel@tonic-gate print "ok 4\n"; 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate arm(5); 77*0Sstevel@tonic-gate if ($has_echo && connect(S,pack_sockaddr_in(7,INADDR_LOOPBACK))){ 78*0Sstevel@tonic-gate arm(0); 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate print "ok 5\n"; 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate print "# Connected to " . 83*0Sstevel@tonic-gate inet_ntoa((unpack_sockaddr_in(getpeername(S)))[1])."\n"; 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate arm(5); 86*0Sstevel@tonic-gate syswrite(S,"olleh",5); 87*0Sstevel@tonic-gate arm(0); 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate arm(5); 90*0Sstevel@tonic-gate $read = sysread(S,$buff,10); # Connection may be granted, then closed! 91*0Sstevel@tonic-gate arm(0); 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate while ($read > 0 && length($buff) < 5) { 94*0Sstevel@tonic-gate # adjust for fact that TCP doesn't guarantee size of reads/writes 95*0Sstevel@tonic-gate arm(5); 96*0Sstevel@tonic-gate $read = sysread(S,$buff,10,length($buff)); 97*0Sstevel@tonic-gate arm(0); 98*0Sstevel@tonic-gate } 99*0Sstevel@tonic-gate print(($read == 0 || $buff eq "olleh") ? "ok 6\n" : "not ok 6\n"); 100*0Sstevel@tonic-gate } 101*0Sstevel@tonic-gate else { 102*0Sstevel@tonic-gate print "# You're allowed to fail tests 5 and 6 if\n"; 103*0Sstevel@tonic-gate print "# the echo service has been disabled.\n"; 104*0Sstevel@tonic-gate print "# 'Interrupted system call' indicates a hanging echo service.\n"; 105*0Sstevel@tonic-gate print "# Error: $!\n"; 106*0Sstevel@tonic-gate print "ok 5 - skipped\n"; 107*0Sstevel@tonic-gate print "ok 6 - skipped\n"; 108*0Sstevel@tonic-gate } 109*0Sstevel@tonic-gate} 110*0Sstevel@tonic-gateelse { 111*0Sstevel@tonic-gate print "# Error: $!\n"; 112*0Sstevel@tonic-gate print "not ok 4\n"; 113*0Sstevel@tonic-gate} 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate# warnings 116*0Sstevel@tonic-gate$SIG{__WARN__} = sub { 117*0Sstevel@tonic-gate ++ $w if $_[0] =~ /^6-ARG sockaddr_in call is deprecated/ ; 118*0Sstevel@tonic-gate} ; 119*0Sstevel@tonic-gate$w = 0 ; 120*0Sstevel@tonic-gatesockaddr_in(1,2,3,4,5,6) ; 121*0Sstevel@tonic-gateprint ($w == 1 ? "not ok 7\n" : "ok 7\n") ; 122*0Sstevel@tonic-gateuse warnings 'Socket' ; 123*0Sstevel@tonic-gatesockaddr_in(1,2,3,4,5,6) ; 124*0Sstevel@tonic-gateprint ($w == 1 ? "ok 8\n" : "not ok 8\n") ; 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate# Thest that whatever we give into pack/unpack_sockaddr retains 127*0Sstevel@tonic-gate# the value thru the entire chain. 128*0Sstevel@tonic-gateif((inet_ntoa((unpack_sockaddr_in(pack_sockaddr_in(100,inet_aton("10.250.230.10"))))[1])) eq '10.250.230.10') { 129*0Sstevel@tonic-gate print "ok 9\n"; 130*0Sstevel@tonic-gate} else { 131*0Sstevel@tonic-gate print "not ok 9\n"; 132*0Sstevel@tonic-gate} 133*0Sstevel@tonic-gateprint ((inet_ntoa(inet_aton("10.20.30.40")) eq "10.20.30.40") ? "ok 10\n" : "not ok 10\n"); 134*0Sstevel@tonic-gateprint ((inet_ntoa(v10.20.30.40) eq "10.20.30.40") ? "ok 11\n" : "not ok 11\n"); 135*0Sstevel@tonic-gate{ 136*0Sstevel@tonic-gate my ($port,$addr) = unpack_sockaddr_in(pack_sockaddr_in(100,v10.10.10.10)); 137*0Sstevel@tonic-gate print (($port == 100) ? "ok 12\n" : "not ok 12\n"); 138*0Sstevel@tonic-gate print ((inet_ntoa($addr) eq "10.10.10.10") ? "ok 13\n" : "not ok 13\n"); 139*0Sstevel@tonic-gate} 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gateeval { inet_ntoa(v10.20.30.400) }; 142*0Sstevel@tonic-gateprint (($@ =~ /^Wide character in Socket::inet_ntoa at/) ? "ok 14\n" : "not ok 14\n"); 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gateif (sockaddr_family(pack_sockaddr_in(100,inet_aton("10.250.230.10"))) == AF_INET) { 145*0Sstevel@tonic-gate print "ok 15\n"; 146*0Sstevel@tonic-gate} else { 147*0Sstevel@tonic-gate print "not ok 15\n"; 148*0Sstevel@tonic-gate} 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gateeval { sockaddr_family("") }; 151*0Sstevel@tonic-gateprint (($@ =~ /^Bad arg length for Socket::sockaddr_family, length is 0, should be at least \d+/) ? "ok 16\n" : "not ok 16\n"); 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gateif ($^O eq 'linux') { 154*0Sstevel@tonic-gate # see if we can handle abstract sockets 155*0Sstevel@tonic-gate my $test_abstract_socket = chr(0) . '/tmp/test-perl-socket'; 156*0Sstevel@tonic-gate my $addr = sockaddr_un ($test_abstract_socket); 157*0Sstevel@tonic-gate my ($path) = sockaddr_un ($addr); 158*0Sstevel@tonic-gate if ($test_abstract_socket eq $path) { 159*0Sstevel@tonic-gate print "ok 17\n"; 160*0Sstevel@tonic-gate } 161*0Sstevel@tonic-gate else { 162*0Sstevel@tonic-gate $path =~ s/\0/\\0/g; 163*0Sstevel@tonic-gate print "# got <$path>\n"; 164*0Sstevel@tonic-gate print "not ok 17\n"; 165*0Sstevel@tonic-gate } 166*0Sstevel@tonic-gate} else { 167*0Sstevel@tonic-gate # doesn't have abstract socket support 168*0Sstevel@tonic-gate print "ok 17 - skipped on this platform\n"; 169*0Sstevel@tonic-gate} 170