Commit | Line | Data |
37120919 |
1 | #!./perl |
2 | |
3 | BEGIN { |
4 | chdir 't' if -d 't'; |
20822f61 |
5 | @INC = '../lib'; |
37120919 |
6 | require Config; import Config; |
c07a80fd |
7 | if ($Config{'extensions'} !~ /\bSocket\b/ && |
3149255e |
8 | !(($^O eq 'VMS') && $Config{d_socket})) { |
2af9ad4a |
9 | print "1..0\n"; |
37120919 |
10 | exit 0; |
11 | } |
ae054b0e |
12 | $has_alarm = $Config{d_alarm}; |
37120919 |
13 | } |
14 | |
15 | use Socket; |
16 | |
2a84dff3 |
17 | print "1..16\n"; |
37120919 |
18 | |
5d604bee |
19 | $has_echo = $^O ne 'MSWin32'; |
ae054b0e |
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 | |
37120919 |
25 | if (socket(T,PF_INET,SOCK_STREAM,6)) { |
26 | print "ok 1\n"; |
ae054b0e |
27 | |
28 | arm(5); |
e69a2255 |
29 | my $host = $^O eq 'MacOS' ? '127.0.0.1' : 'localhost'; |
30 | if ($has_echo && connect(T,pack_sockaddr_in(7,inet_aton($host)))){ |
ae054b0e |
31 | arm(0); |
32 | |
37120919 |
33 | print "ok 2\n"; |
34 | |
31f187ae |
35 | print "# Connected to " . |
36 | inet_ntoa((unpack_sockaddr_in(getpeername(T)))[1])."\n"; |
37120919 |
37 | |
ae054b0e |
38 | arm(5); |
37120919 |
39 | syswrite(T,"hello",5); |
ae054b0e |
40 | arm(0); |
41 | |
42 | arm(5); |
962dce22 |
43 | $read = sysread(T,$buff,10); # Connection may be granted, then closed! |
ae054b0e |
44 | arm(0); |
45 | |
d6831368 |
46 | while ($read > 0 && length($buff) < 5) { |
47 | # adjust for fact that TCP doesn't guarantee size of reads/writes |
ae054b0e |
48 | arm(5); |
d6831368 |
49 | $read = sysread(T,$buff,10,length($buff)); |
ae054b0e |
50 | arm(0); |
d6831368 |
51 | } |
962dce22 |
52 | print(($read == 0 || $buff eq "hello") ? "ok 3\n" : "not ok 3\n"); |
37120919 |
53 | } |
54 | else { |
dc26df50 |
55 | print "# You're allowed to fail tests 2 and 3 if\n"; |
56 | print "# the echo service has been disabled.\n"; |
ae054b0e |
57 | print "# 'Interrupted system call' indicates a hanging echo service.\n"; |
58 | print "# Error: $!\n"; |
59 | print "ok 2 - skipped\n"; |
60 | print "ok 3 - skipped\n"; |
37120919 |
61 | } |
62 | } |
63 | else { |
ae054b0e |
64 | print "# Error: $!\n"; |
37120919 |
65 | print "not ok 1\n"; |
66 | } |
67 | |
68 | if( socket(S,PF_INET,SOCK_STREAM,6) ){ |
69 | print "ok 4\n"; |
70 | |
ae054b0e |
71 | arm(5); |
5d604bee |
72 | if ($has_echo && connect(S,pack_sockaddr_in(7,INADDR_LOOPBACK))){ |
ae054b0e |
73 | arm(0); |
74 | |
37120919 |
75 | print "ok 5\n"; |
76 | |
31f187ae |
77 | print "# Connected to " . |
78 | inet_ntoa((unpack_sockaddr_in(getpeername(S)))[1])."\n"; |
37120919 |
79 | |
ae054b0e |
80 | arm(5); |
37120919 |
81 | syswrite(S,"olleh",5); |
ae054b0e |
82 | arm(0); |
83 | |
84 | arm(5); |
962dce22 |
85 | $read = sysread(S,$buff,10); # Connection may be granted, then closed! |
ae054b0e |
86 | arm(0); |
87 | |
d6831368 |
88 | while ($read > 0 && length($buff) < 5) { |
89 | # adjust for fact that TCP doesn't guarantee size of reads/writes |
ae054b0e |
90 | arm(5); |
d6831368 |
91 | $read = sysread(S,$buff,10,length($buff)); |
ae054b0e |
92 | arm(0); |
d6831368 |
93 | } |
962dce22 |
94 | print(($read == 0 || $buff eq "olleh") ? "ok 6\n" : "not ok 6\n"); |
37120919 |
95 | } |
96 | else { |
dc26df50 |
97 | print "# You're allowed to fail tests 5 and 6 if\n"; |
98 | print "# the echo service has been disabled.\n"; |
ae054b0e |
99 | print "# 'Interrupted system call' indicates a hanging echo service.\n"; |
100 | print "# Error: $!\n"; |
101 | print "ok 5 - skipped\n"; |
102 | print "ok 6 - skipped\n"; |
37120919 |
103 | } |
104 | } |
105 | else { |
ae054b0e |
106 | print "# Error: $!\n"; |
37120919 |
107 | print "not ok 4\n"; |
108 | } |
d3a7d8c7 |
109 | |
110 | # warnings |
111 | $SIG{__WARN__} = sub { |
112 | ++ $w if $_[0] =~ /^6-ARG sockaddr_in call is deprecated/ ; |
113 | } ; |
114 | $w = 0 ; |
115 | sockaddr_in(1,2,3,4,5,6) ; |
116 | print ($w == 1 ? "not ok 7\n" : "ok 7\n") ; |
117 | use warnings 'Socket' ; |
118 | sockaddr_in(1,2,3,4,5,6) ; |
119 | print ($w == 1 ? "ok 8\n" : "not ok 8\n") ; |
e245830c |
120 | |
121 | # Thest that whatever we give into pack/unpack_sockaddr retains |
122 | # the value thru the entire chain. |
123 | if((inet_ntoa((unpack_sockaddr_in(pack_sockaddr_in(100,inet_aton("10.250.230.10"))))[1])) eq '10.250.230.10') { |
124 | print "ok 9\n"; |
125 | } else { |
126 | print "not ok 9\n"; |
127 | } |
2528d3bc |
128 | print ((inet_ntoa(inet_aton("10.20.30.40")) eq "10.20.30.40") ? "ok 10\n" : "not ok 10\n"); |
129 | print ((inet_ntoa(v10.20.30.40) eq "10.20.30.40") ? "ok 11\n" : "not ok 11\n"); |
d09712d8 |
130 | { |
131 | my ($port,$addr) = unpack_sockaddr_in(pack_sockaddr_in(100,v10.10.10.10)); |
132 | print (($port == 100) ? "ok 12\n" : "not ok 12\n"); |
2528d3bc |
133 | print ((inet_ntoa($addr) eq "10.10.10.10") ? "ok 13\n" : "not ok 13\n"); |
d09712d8 |
134 | } |
135 | |
b2d93eb8 |
136 | eval { inet_ntoa(v10.20.30.400) }; |
137 | print (($@ =~ /^Wide character in Socket::inet_ntoa at/) ? "ok 14\n" : "not ok 14\n"); |
2a84dff3 |
138 | |
139 | if (sockaddr_family(pack_sockaddr_in(100,inet_aton("10.250.230.10"))) == AF_INET) { |
140 | print "ok 15\n"; |
141 | } else { |
142 | print "not ok 15\n"; |
143 | } |
144 | |
145 | eval { sockaddr_family("") }; |
146 | print (($@ =~ /^Bad arg length for Socket::sockaddr_family, length is 0, should be at least \d+/) ? "ok 16\n" : "not ok 16\n"); |