Ignore:
Timestamp:
11/02/09 17:49:41 (2 years ago)
Author:
adamk
Message:

Cleaning

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/IO-Socket-Multicast/t/03_multicast.t

    r9008 r9021  
    77 
    88use Test::More tests => 13; 
    9  
    109use IO::Socket::Multicast; 
    1110 
    12 sub test { 
    13   my ($flag,$test) = @_; 
    14   print $flag ? "ok $test\n" : "not ok $test ($!)\n"; 
     11# Simple constructor 
     12my $s = IO::Socket::Multicast->new; 
     13isa_ok( $s, 'IO::Socket::Multicast' ); 
     14 
     15# Platform compatibility 
     16my $WIN32        = $^O eq 'MSWin32'; 
     17my $LINUX        = $WIN32 ? 0 : (`uname -sr` =~ /^Linux (\d+\.\d+)/)[0]; 
     18my $OS_OK        = ( $LINUX and $LINUX >= 2.2 ); 
     19my $IO_INTERFACE = eval "use IO::Interface ':flags'; 1;"; 
     20my $INTERFACE    = $IO_INTERFACE && find_a_mcast_if($s); 
     21 
     22# Some basics 
     23ok($s->mcast_add('225.0.1.1'), 'Add socket to Multicast Group' ); 
     24ok($s->mcast_drop(inet_aton('225.0.1.1')),'Drop Multicast Group' ); 
     25SKIP: { 
     26    # What the hell ? Dropping an unsubscribed mcast group on win32 fails to fail? 
     27    skip("Doesn't work on Win32??", 1) if $WIN32; 
     28    ok( ! $s->mcast_drop('225.0.1.1'), 'Drop unsubscribed group returns false' ); 
    1529} 
    1630 
    17 my $s = IO::Socket::Multicast->new; 
    18  
    19 # dumb tests for incompatibilities, etc. 
    20 my $io_interface_avail = eval "use IO::Interface ':flags'; 1;"; 
    21 my $mcast_if = $io_interface_avail && find_a_mcast_if($s); 
    22 my ($linux_version) = `uname -sr` =~ /^Linux (\d+\.\d+)/; 
    23 my $os_ok = $linux_version && ($linux_version >= 2.2); 
    24 my $win32 = $^O =~ /^MSWin/; 
    25  
    26 ok($s->mcast_add('225.0.1.1'), 'Add socket to Multicast Group' ); 
    27 ok($s->mcast_drop(inet_aton('225.0.1.1')),'Drop Multicast Group' ); 
    28 if ($win32) { 
    29   print "ok # Skip. Doesn't work on Win32??\n"; 
    30   # What the hell ? Dropping an unsubscribed mcast group on win32 fails to fail? 
    31 } else { 
    32   ok(!$s->mcast_drop('225.0.1.1'), 'Drop unsubscribed group returns false'  ); 
     31# More subtle control 
     32SKIP: { 
     33    skip("Needs Linux >= 2.2", 6) unless $OS_OK; 
     34    ok($s->mcast_ttl         == 1,  'Get socket TTL default is one'); 
     35    ok($s->mcast_ttl(10)     == 1,  'Set TTL returns previous value'); 
     36    ok($s->mcast_ttl         == 10, 'Get TTL post-set returns correct TTL'); 
     37    ok($s->mcast_loopback    == 1,  'Multicast loopback defaults to true'); 
     38    ok($s->mcast_loopback(0) == 1,  'Loopback set returns previous value' ); 
     39    ok($s->mcast_loopback    == 0,  'Loopback get' ); 
    3340} 
    3441 
    3542SKIP: { 
    36 if ($os_ok) { 
    37   ok($s->mcast_ttl         == 1,     'Get socket TTL default is one'); 
    38   ok($s->mcast_ttl(10)     == 1,     'Set TTL returns previous value'); 
    39   ok($s->mcast_ttl         == 10,    'Get TTL post-set returns correct TTL'); 
    40   ok($s->mcast_loopback    == 1,     'Multicast loopback defaults to true'); 
    41   ok($s->mcast_loopback(0) == 1,     'Loopback set returns previous value' ); 
    42   ok($s->mcast_loopback    == 0,   'Loopback get' ); 
    43 } else { 
    44   skip  "Needs Linux >= 2.2\n", 6; 
    45   } 
    46 } 
    47  
    48 if ($io_interface_avail && $mcast_if && $os_ok) { 
    49   ok ($s->mcast_if  eq 'any'    ,    'Default interface "any"'); 
    50   ok ($s->mcast_if($mcast_if) eq 'any', 'Multicast interface set returns previous value'); 
    51   ok ($s->mcast_if eq $mcast_if       , 'Multicast interface set'); 
    52   ok ($s->mcast_add('225.0.1.1',$mcast_if)  , 'Multicast add GROUP,if'); 
    53 } else { 
    54   my $explanation = 'IO::Interface not available' if !$io_interface_avail; 
    55   $explanation ||= 'No multicast interface available'  if !$mcast_if; 
    56   $explanation ||= 'Needs Linux >= 2.2'        if !$os_ok; 
    57   skip $explanation , 4; 
     43    skip('IO::Interface not available', 4)      unless $IO_INTERFACE; 
     44    skip('No multicast interface available', 4) unless $INTERFACE; 
     45    skip('Needs Linux >= 2.2', 4)               unless $OS_OK; 
     46    ok ($s->mcast_if  eq 'any' ,    'Default interface "any"'); 
     47    ok ($s->mcast_if($INTERFACE) eq 'any', 'Multicast interface set returns previous value'); 
     48    ok ($s->mcast_if eq $INTERFACE , 'Multicast interface set'); 
     49    ok ($s->mcast_add('225.0.1.1',$INTERFACE), 'Multicast add GROUP,if'); 
    5850} 
    5951 
    6052sub find_a_mcast_if { 
    61   my $s = shift; 
    62   my @ifs = $s->if_list; 
    63   foreach (reverse @ifs) { 
    64      
    65     next unless $s->if_flags($_) & IFF_MULTICAST(); 
    66     next unless $s->if_flags($_) & IFF_RUNNING(); 
    67     next unless $s->if_addr($_);  
    68     return $_; 
    69   } 
     53    my $s   = shift; 
     54    my @ifs = $s->if_list; 
     55    foreach ( reverse @ifs ) { 
     56        next unless $s->if_flags($_) & IFF_MULTICAST(); 
     57        next unless $s->if_flags($_) & IFF_RUNNING(); 
     58        next unless $s->if_addr($_);  
     59        return $_; 
     60    } 
    7061} 
Note: See TracChangeset for help on using the changeset viewer.