Newer
Older
NetAddr-IP / Lite / t / v4-aton.t
@Michael Robinton Michael Robinton on 21 Oct 2014 1 KB Import of MIKER/NetAddr-IP-4.020 from CPAN.
  1. use Test::More tests => 19;
  2.  
  3. my @addr = (
  4. [ 'localhost', '127.0.0.1' ],
  5. [ 'broadcast', '255.255.255.255' ],
  6. [ '254.254.0.1', '254.254.0.1' ],
  7. [ 'default', '0.0.0.0' ],
  8. [ '10.0.0.1', '10.0.0.1' ],
  9.  
  10. );
  11. my %addr = (
  12. localhost => pack('N',0x7f000001),
  13. broadcast => pack('N',0xffffffff),
  14. '254.254.0.1' => pack('N',0xfefe0001),
  15. default => pack('N',0),
  16. '10.0.0.1' => pack('N',0x0a000001),
  17. '127.0.0.1' => pack('N',0x7f000001),
  18. '255.255.255.255' => pack('N',0xffffffff),
  19. '0.0.0.0' => pack('N',0),
  20. );
  21.  
  22. # local inet_aton, don't use perl's Socket
  23.  
  24. sub l_inet_aton {
  25. my $rv = (exists $addr{$_[0]}) ? $addr{$_[0]} : undef;
  26. }
  27.  
  28.  
  29. # Verify that Accept_Binary_IP works...
  30.  
  31. my $x;
  32.  
  33. SKIP:
  34. {
  35. skip "Failed to load NetAddr::IP::Lite", 17
  36. unless use_ok('NetAddr::IP::Lite');
  37.  
  38. ok(! defined NetAddr::IP::Lite->new("\1\1\1\1"),
  39. "binary unrecognized by default ". ($x ? $x->addr :''));
  40.  
  41. # This mimicks the actual use with :aton
  42. NetAddr::IP::Lite::import(':aton');
  43.  
  44. ok(defined ($x = NetAddr::IP::Lite->new("\1\1\1\1")),
  45. "...but can be recognized ". $x->addr);
  46.  
  47. ok(!defined ($x = NetAddr::IP::Lite->new('bad rfc-952 characters')),
  48. "bad rfc-952 characters ". ($x ? $x->addr :''));
  49.  
  50. is(NetAddr::IP::Lite->new($_->[0])->aton, l_inet_aton($_->[1]), "->aton($_->[0])")
  51. for @addr;
  52.  
  53. ok(defined NetAddr::IP::Lite->new(l_inet_aton($_->[1])), "->new aton($_->[1])")
  54. for @addr;
  55.  
  56. is(NetAddr::IP::Lite->new(l_inet_aton($_->[1]))->addr, $_->[1],
  57. "->new aton($_->[1])")
  58. for @addr;
  59. };