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