Commit | Line | Data |
37120919 |
1 | #!./perl |
2 | |
3 | BEGIN { |
37120919 |
4 | require Config; import Config; |
c07a80fd |
5 | if ($Config{'extensions'} !~ /\bSocket\b/ && |
3149255e |
6 | !(($^O eq 'VMS') && $Config{d_socket})) { |
2af9ad4a |
7 | print "1..0\n"; |
37120919 |
8 | exit 0; |
9 | } |
ae054b0e |
10 | $has_alarm = $Config{d_alarm}; |
37120919 |
11 | } |
12 | |
38f5dce2 |
13 | use Socket qw(:all); |
37120919 |
14 | |
99f13d4c |
15 | print "1..21\n"; |
37120919 |
16 | |
5d604bee |
17 | $has_echo = $^O ne 'MSWin32'; |
ae054b0e |
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 | |
38f5dce2 |
23 | if (socket(T, PF_INET, SOCK_STREAM, IPPROTO_TCP)) { |
37120919 |
24 | print "ok 1\n"; |
ae054b0e |
25 | |
26 | arm(5); |
ef5a6dd7 |
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))){ |
ae054b0e |
32 | arm(0); |
33 | |
37120919 |
34 | print "ok 2\n"; |
35 | |
31f187ae |
36 | print "# Connected to " . |
37 | inet_ntoa((unpack_sockaddr_in(getpeername(T)))[1])."\n"; |
37120919 |
38 | |
ae054b0e |
39 | arm(5); |
37120919 |
40 | syswrite(T,"hello",5); |
ae054b0e |
41 | arm(0); |
42 | |
43 | arm(5); |
962dce22 |
44 | $read = sysread(T,$buff,10); # Connection may be granted, then closed! |
ae054b0e |
45 | arm(0); |
46 | |
d6831368 |
47 | while ($read > 0 && length($buff) < 5) { |
48 | # adjust for fact that TCP doesn't guarantee size of reads/writes |
ae054b0e |
49 | arm(5); |
d6831368 |
50 | $read = sysread(T,$buff,10,length($buff)); |
ae054b0e |
51 | arm(0); |
d6831368 |
52 | } |
962dce22 |
53 | print(($read == 0 || $buff eq "hello") ? "ok 3\n" : "not ok 3\n"); |
37120919 |
54 | } |
55 | else { |
dc26df50 |
56 | print "# You're allowed to fail tests 2 and 3 if\n"; |
e0975c9c |
57 | print "# the echo service has been disabled or if your\n"; |
ef5a6dd7 |
58 | print "# gethostbyname() cannot resolve your localhost.\n"; |
59 | print "# 'Connection refused' indicates disabled echo service.\n"; |
ae054b0e |
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"; |
37120919 |
64 | } |
65 | } |
66 | else { |
ae054b0e |
67 | print "# Error: $!\n"; |
37120919 |
68 | print "not ok 1\n"; |
69 | } |
70 | |
38f5dce2 |
71 | if( socket(S, PF_INET,SOCK_STREAM, IPPROTO_TCP) ){ |
37120919 |
72 | print "ok 4\n"; |
73 | |
ae054b0e |
74 | arm(5); |
5d604bee |
75 | if ($has_echo && connect(S,pack_sockaddr_in(7,INADDR_LOOPBACK))){ |
ae054b0e |
76 | arm(0); |
77 | |
37120919 |
78 | print "ok 5\n"; |
79 | |
31f187ae |
80 | print "# Connected to " . |
81 | inet_ntoa((unpack_sockaddr_in(getpeername(S)))[1])."\n"; |
37120919 |
82 | |
ae054b0e |
83 | arm(5); |
37120919 |
84 | syswrite(S,"olleh",5); |
ae054b0e |
85 | arm(0); |
86 | |
87 | arm(5); |
962dce22 |
88 | $read = sysread(S,$buff,10); # Connection may be granted, then closed! |
ae054b0e |
89 | arm(0); |
90 | |
d6831368 |
91 | while ($read > 0 && length($buff) < 5) { |
92 | # adjust for fact that TCP doesn't guarantee size of reads/writes |
ae054b0e |
93 | arm(5); |
d6831368 |
94 | $read = sysread(S,$buff,10,length($buff)); |
ae054b0e |
95 | arm(0); |
d6831368 |
96 | } |
962dce22 |
97 | print(($read == 0 || $buff eq "olleh") ? "ok 6\n" : "not ok 6\n"); |
37120919 |
98 | } |
99 | else { |
dc26df50 |
100 | print "# You're allowed to fail tests 5 and 6 if\n"; |
101 | print "# the echo service has been disabled.\n"; |
ae054b0e |
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"; |
37120919 |
106 | } |
107 | } |
108 | else { |
ae054b0e |
109 | print "# Error: $!\n"; |
37120919 |
110 | print "not ok 4\n"; |
111 | } |
d3a7d8c7 |
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") ; |
e245830c |
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 | } |
2528d3bc |
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"); |
d09712d8 |
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"); |
2528d3bc |
136 | print ((inet_ntoa($addr) eq "10.10.10.10") ? "ok 13\n" : "not ok 13\n"); |
d09712d8 |
137 | } |
138 | |
b2d93eb8 |
139 | eval { inet_ntoa(v10.20.30.400) }; |
140 | print (($@ =~ /^Wide character in Socket::inet_ntoa at/) ? "ok 14\n" : "not ok 14\n"); |
2a84dff3 |
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"); |
7513c55b |
150 | |
151 | if ($^O eq 'linux') { |
152 | # see if we can handle abstract sockets |
89904c08 |
153 | my $test_abstract_socket = chr(0) . '/org/perl/hello'. chr(0) . 'world'; |
7513c55b |
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 | } |
99f13d4c |
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 | |
7513c55b |
173 | } else { |
174 | # doesn't have abstract socket support |
175 | print "ok 17 - skipped on this platform\n"; |
99f13d4c |
176 | print "ok 18 - skipped on this platform\n"; |
7513c55b |
177 | } |
036d8bd4 |
178 | |
179 | if($Config{d_inetntop} && $Config{d_inetaton}){ |
99f13d4c |
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"); |
036d8bd4 |
183 | } else { |
184 | # no IPv6 |
99f13d4c |
185 | print "ok $_ - skipped on this platform\n" for 19 .. 21; |
036d8bd4 |
186 | } |