Long-standing UDP sockets bug on OS/2
[p5sagit/p5-mst-13.2.git] / t / lib / io_udp.t
1 #!./perl
2
3 BEGIN {
4     unless(grep /blib/, @INC) {
5         chdir 't' if -d 't';
6         unshift @INC, '../lib' if -d '../lib';
7     }
8 }
9
10 use Config;
11
12 BEGIN {
13     if(-d "lib" && -f "TEST") {
14         my $reason;
15
16         if ($Config{'extensions'} !~ /\bSocket\b/) {
17           $reason = 'Socket was not built';
18         }
19         elsif ($Config{'extensions'} !~ /\bIO\b/) {
20           $reason = 'IO was not built';
21         }
22         elsif ($^O eq 'apollo') {
23           $reason = "unknown *FIXME*";
24         }
25         undef $reason if $^O eq 'VMS' and $Config{d_socket};
26         if ($reason) {
27             print "1..0 # Skip: $reason\n";
28             exit 0;
29         }
30     }
31 }
32
33 sub compare_addr {
34     my $a = shift;
35     my $b = shift;
36     if (length($a) != length $b) {
37         my $min = (length($a) < length $b) ? length($a) : length $b;
38         if ($min and substr($a, 0, $min) eq substr($b, 0, $min)) {
39             printf "# Apparently: %d bytes junk at the end of %s\n# %s\n",
40                 abs(length($a) - length ($b)),
41                 $_[length($a) < length ($b) ? 1 : 0],
42                 "consider decreasing bufsize of recfrom.";
43             substr($a, $min) = "";
44             substr($b, $min) = "";
45         }
46         return 0;
47     }
48     my @a = unpack_sockaddr_in($a);
49     my @b = unpack_sockaddr_in($b);
50     "$a[0]$a[1]" eq "$b[0]$b[1]";
51 }
52
53 $| = 1;
54 print "1..7\n";
55
56 use Socket;
57 use IO::Socket qw(AF_INET SOCK_DGRAM INADDR_ANY);
58
59     # This can fail if localhost is undefined or the
60     # special 'loopback' address 127.0.0.1 is not configured
61     # on your system. (/etc/rc.config.d/netconfig on HP-UX.)
62     # As a shortcut (not recommended) you could change 'localhost'
63     # here to be the name of this machine eg 'myhost.mycompany.com'.
64
65 $udpa = IO::Socket::INET->new(Proto => 'udp', LocalAddr => 'localhost')
66     or die "$! (maybe your system does not have the 'localhost' address defined)";
67
68 print "ok 1\n";
69
70 $udpb = IO::Socket::INET->new(Proto => 'udp', LocalAddr => 'localhost')
71     or die "$! (maybe your system does not have the 'localhost' address defined)";
72
73 print "ok 2\n";
74
75 $udpa->send("ok 4\n",0,$udpb->sockname);
76
77 print "not "
78   unless compare_addr($udpa->peername,$udpb->sockname, 'peername', 'sockname');
79 print "ok 3\n";
80
81 my $where = $udpb->recv($buf="",5);
82 print $buf;
83
84 my @xtra = ();
85
86 unless(compare_addr($where,$udpa->sockname, 'recv name', 'sockname')) {
87     print "not ";
88     @xtra = (0,$udpa->sockname);
89 }
90 print "ok 5\n";
91
92 $udpb->send("ok 6\n",@xtra);
93 $udpa->recv($buf="",5);
94 print $buf;
95
96 print "not " if $udpa->connected;
97 print "ok 7\n";