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