Newer
Older
NetAddr-IP / Lite / Util / t / inet_pton.t
@Michael Robinton Michael Robinton on 21 Oct 2014 2 KB Import of MIKER/NetAddr-IP-4.049 from CPAN.
  1. # Before `make install' is performed this script should be runnable with
  2. # `make test'. After `make install' it should work as `perl test.pl'
  3.  
  4. ######################### We start with some black magic to print on failure.
  5. # Change 1..1 below to 1..last_test_to_print .
  6. # (It may become useful if the test is moved to ./t subdirectory.)
  7.  
  8. BEGIN { $| = 1; print "1..32\n"; }
  9. END {print "not ok 1\n" unless $loaded;}
  10.  
  11. #use diagnostics;
  12. use NetAddr::IP::Util qw(
  13. inet_ntop
  14. inet_pton
  15. AF_INET
  16. AF_INET6
  17. );
  18.  
  19. $loaded = 1;
  20. print "ok 1\n";
  21. ######################### End of black magic.
  22.  
  23. # Insert your test code below (better if it prints "ok 13"
  24. # (correspondingly "not ok 13") depending on the success of chunk 13
  25. # of the test code):
  26.  
  27. $test = 2;
  28.  
  29. sub ok {
  30. print "ok $test\n";
  31. ++$test;
  32. }
  33.  
  34. ## test 2 - 13 add stuff to buffer
  35. my @num = # addr
  36. qw(
  37. 0.0.0.0
  38. 255.255.255.255
  39. 1.2.3.4
  40. 10.253.230.9
  41. );
  42.  
  43. foreach (@num) {
  44. my @digs = split(/\./,$_);
  45. my $pkd = pack('C4',@digs);
  46. my $naddr = inet_pton(AF_INET(),$_);
  47. my $addr = join('.',unpack('C4',$naddr));
  48. my $num = inet_ntop(AF_INET(),$pkd);
  49.  
  50. print "bits do not match\nnot "
  51. unless $naddr eq $pkd;
  52. &ok;
  53.  
  54. print "inet_pton: $addr, exp: $_\nnot "
  55. unless $addr eq $_;
  56. &ok;
  57.  
  58. print "inet_ntop: $num, exp: $_\nnot "
  59. unless $num eq $_;
  60. &ok;
  61. }
  62.  
  63. ## test 14 - 31
  64. @num = # in exphex
  65. qw(
  66. :: ::
  67. 43:: 43::
  68. ::21 ::21
  69. ::1:2:3:4:5:6:7 0:1:2:3:4:5:6:7
  70. 1:2:3:4:5:6:7:: 1:2:3:4:5:6:7:0
  71. 1::8 1::8
  72. FF00::FFFF ff00::ffff
  73. FFFF::FFFF:FFFF ffff::ffff:ffff
  74. A1B2:C3D4:E5D6:F7E8:08F9:190A:1.2.3.4 a1b2:c3d4:e5d6:f7e8:8f9:190a:102:304
  75. );
  76.  
  77. for (my $i=0;$i<@num;$i+=2) {
  78. my $bits = inet_pton(AF_INET6(),$num[$i]);
  79. my $len = length($bits);
  80. print "bad len = $len, exp: 16\nnot "
  81. unless $len == 16; # 16 bytes x 8 bits
  82. &ok;
  83. my $ipv6x = inet_ntop(AF_INET6(),$bits);
  84. print "got: $ipv6x\nexp: $num[$i +1]\nnot "
  85. unless $ipv6x eq $num[$i +1];
  86. &ok;
  87. }
  88.  
  89. ## test 32 check bad length ntop
  90. my $try = '1234';
  91. my $notempty = eval {
  92. inet_ntop(AF_INET6(),$try);
  93. };
  94. print "failed bad argument length test for inet_ntop\nnot "
  95. unless $@ && $@ =~ /Bad arg/;
  96. &ok;