Newer
Older
NetAddr-IP / Lite / Util / t / croak.t
  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..31\n"; }
  9. END {print "not ok 1\n" unless $loaded;}
  10.  
  11. use NetAddr::IP::Util qw(
  12. bcd2bin
  13. bin2bcd
  14. hasbits
  15. isIPv4
  16. add128
  17. sub128
  18. shiftleft
  19. comp128
  20. bcdn2txt
  21. bin2bcdn
  22. bcdn2bin
  23. simple_pack
  24. );
  25.  
  26. $loaded = 1;
  27. print "ok 1\n";
  28. ######################### End of black magic.
  29.  
  30. # Insert your test code below (better if it prints "ok 13"
  31. # (correspondingly "not ok 13") depending on the success of chunk 13
  32. # of the test code):
  33.  
  34. $test = 2;
  35.  
  36. sub ok {
  37. print "ok $test\n";
  38. ++$test;
  39. }
  40.  
  41. ## tests 2 - 9 simple_pack
  42.  
  43. foreach(
  44. '1234/',
  45. '1234:',
  46. 'a1234',
  47. '&1234',
  48. ) {
  49. my $rv;
  50. eval { $rv = simple_pack($_) };
  51. if (defined $rv) {
  52. $rv = unpack("H40",$rv);
  53. print "got: $rv, exp: 'die'\nnot ";
  54. }
  55. &ok;
  56.  
  57. print "expected a die from bad character input\nnot "
  58. unless $@ && $@ =~ /Bad/;
  59. &ok;
  60. }
  61.  
  62. ## tests 10 - 17 bcd2bin
  63.  
  64. foreach(
  65. '1234/',
  66. '1234:',
  67. 'a1234',
  68. '&1234',
  69. ) {
  70. my $rv;
  71. eval { $rv = bcd2bin($_) };
  72. if (defined $rv) {
  73. $rv = unpack("H40",$rv);
  74. print "got: $rv, exp: 'die'\nnot ";
  75. }
  76. &ok;
  77.  
  78. print "expected a die from bad character input\nnot "
  79. unless $@ && $@ =~ /Bad/;
  80. &ok;
  81. }
  82.  
  83. ## test 18 bcdn2bin
  84. eval { bcdn2bin('123456789012345678901') };
  85. print "expected a die from bad vector string length\nnot "
  86. unless $@ && $@ =~ /Bad/;
  87. &ok;
  88.  
  89. ## test 19 bcdn2bin
  90. eval { bcdn2bin('12345678901234567890') };
  91. print "expected a die from missing length specifier\nnot "
  92. unless $@ && $@ =~ /Bad/;
  93. &ok;
  94.  
  95. ## test 20 bin2bcd
  96. eval { bin2bcd('123') };
  97. print "expected a die from bad vector string length\nnot "
  98. unless $@ && $@ =~ /Bad/;
  99. &ok;
  100.  
  101. ## test 21 bin2bcdn
  102. eval { bin2bcdn('123') };
  103. print "expected a die from bad vector string length\nnot "
  104. unless $@ && $@ =~ /Bad/;
  105. &ok;
  106.  
  107. ## test 22 bcdn2txt
  108. eval { bcdn2txt('123456789012345678901') };
  109. print "expected a die from bad vector string length\nnot "
  110. unless $@ && $@ =~ /Bad/;
  111. &ok;
  112.  
  113. ## test 23 bcdn2txt
  114. my $rv;
  115. my $exp = '3132333435363738393031323334353637383930';
  116. $rv = bcdn2txt('12345678901234567890');
  117. print "got: $rv\nexp: $exp\nnot "
  118. unless $rv eq $exp;
  119. &ok;
  120.  
  121. ## test 24 hasbits
  122. eval { hasbits('123') };
  123. print "expected a die from bad vector string length\nnot "
  124. unless $@ && $@ =~ /Bad/;
  125. &ok;
  126.  
  127. ## test 25 isIPv4
  128. eval { isIPv4('12345678901234567') };
  129. print "expected a die from bad vector string length\nnot "
  130. unless $@ && $@ =~ /Bad/;
  131. &ok;
  132.  
  133. ## test 26 add128
  134. eval { add128('123','1234567890123456') };
  135. print "expected a die from bad vector string length\nnot "
  136. unless $@ && $@ =~ /Bad/;
  137. &ok;
  138.  
  139. ## test 27 sub128
  140. eval { sub128('1234567890123456','12345678901234567') };
  141. print "expected a die from bad vector string length\nnot "
  142. unless $@ && $@ =~ /Bad/;
  143. &ok;
  144.  
  145. ## test 28 comp128
  146. eval { comp128('123') };
  147. print "expected a die from bad vector string length\nnot "
  148. unless $@ && $@ =~ /Bad/;
  149. &ok;
  150.  
  151. ## test 29 shiftleft
  152. eval { shiftleft ('12345678901234567') };
  153. print "expected a die from bad vector string length\nnot "
  154. unless $@ && $@ =~ /Bad/;
  155. &ok;
  156.  
  157. ## test 30 shiftleft
  158. eval { shiftleft ('1234567890123456',-1) };
  159. print "expected a die from bad shift count specifier\nnot "
  160. unless $@ && $@ =~ /Bad/;
  161. &ok;
  162.  
  163. ## test 31 shiftleft
  164. eval { shiftleft ('1234567890123456',129) };
  165. print "expected a die from bad shift count specifier\nnot "
  166. unless $@ && $@ =~ /Bad/;
  167. &ok;
  168.