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