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