Implement IO::Handle::say the same way as the builtin say().
[p5sagit/p5-mst-13.2.git] / ext / IO / t / io_udp.t
1 #!./perl
2
3 BEGIN {
4     unless(grep /blib/, @INC) {
5         chdir 't' if -d 't';
6         @INC = '../lib';
7     }
8
9     require($ENV{PERL_CORE} ? './test.pl' : './t/test.pl');
10
11     use Config;
12     my $reason;
13     if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bSocket\b/) {
14       $reason = 'Socket was not built';
15     }
16     elsif ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bIO\b/) {
17       $reason = 'IO was not built';
18     }
19     elsif ($^O eq 'apollo') {
20       $reason = "unknown *FIXME*";
21     }
22     undef $reason if $^O eq 'VMS' and $Config{d_socket};
23     skip_all($reason) if $reason;
24 }
25
26 sub compare_addr {
27     no utf8;
28     my $a = shift;
29     my $b = shift;
30     if (length($a) != length $b) {
31         my $min = (length($a) < length $b) ? length($a) : length $b;
32         if ($min and substr($a, 0, $min) eq substr($b, 0, $min)) {
33             printf "# Apparently: %d bytes junk at the end of %s\n# %s\n",
34                 abs(length($a) - length ($b)),
35                 $_[length($a) < length ($b) ? 1 : 0],
36                 "consider decreasing bufsize of recfrom.";
37             substr($a, $min) = "";
38             substr($b, $min) = "";
39         }
40         return 0;
41     }
42     my @a = unpack_sockaddr_in($a);
43     my @b = unpack_sockaddr_in($b);
44     "$a[0]$a[1]" eq "$b[0]$b[1]";
45 }
46
47 plan(7);
48 watchdog(15);
49
50 use Socket;
51 use IO::Socket qw(AF_INET SOCK_DGRAM INADDR_ANY);
52
53 $udpa = IO::Socket::INET->new(Proto => 'udp', LocalAddr => 'localhost')
54      || IO::Socket::INET->new(Proto => 'udp', LocalAddr => '127.0.0.1')
55     or die "$! (maybe your system does not have a localhost at all, 'localhost' or 127.0.0.1)";
56 ok(1);
57
58 $udpb = IO::Socket::INET->new(Proto => 'udp', LocalAddr => 'localhost')
59      || IO::Socket::INET->new(Proto => 'udp', LocalAddr => '127.0.0.1')
60     or die "$! (maybe your system does not have a localhost at all, 'localhost' or 127.0.0.1)";
61 ok(1);
62
63 $udpa->send('BORK', 0, $udpb->sockname);
64
65 ok(compare_addr($udpa->peername,$udpb->sockname, 'peername', 'sockname'));
66
67 my $where = $udpb->recv($buf="", 4);
68 is($buf, 'BORK');
69
70 my @xtra = ();
71
72 if (! ok(compare_addr($where,$udpa->sockname, 'recv name', 'sockname'))) {
73     @xtra = (0, $udpa->sockname);
74 }
75
76 $udpb->send('FOObar', @xtra);
77 $udpa->recv($buf="", 6);
78 is($buf, 'FOObar');
79
80 ok(! $udpa->connected);
81
82 exit(0);
83
84 # EOF