Newer
Older
NetAddr-IP / Lite / t / netaddr.t
@Michael Robinton Michael Robinton on 21 Oct 2014 5 KB Import of MIKER/NetAddr-IP-4.020 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..35\n"; }
  9. END {print "not ok 1\n" unless $loaded;}
  10.  
  11. #use diagnostics;
  12. use Data::Dumper;
  13. use NetAddr::IP::Lite;
  14.  
  15. $loaded = 1;
  16. print "ok 1\n";
  17. ######################### End of black magic.
  18.  
  19. # Insert your test code below (better if it prints "ok 13"
  20. # (correspondingly "not ok 13") depending on the success of chunk 13
  21. # of the test code):
  22.  
  23. =pod
  24.  
  25. $rv=list2NetAddr(\@inlist,\@NAobject);
  26.  
  27. Build of NetAddr object structure from a list of IPv4 addresses or address
  28. ranges. This object is passed to B<matchNetAddr> to check if a given IP
  29. address is contained in the list.
  30.  
  31. input: array reference pointer
  32. to a list of addresses
  33.  
  34. i.e. 11.22.33.44
  35. 11.22.33.0/24
  36. 11.22.33.0/255.255.255.0
  37. 11.22.33.20-11.22.33.46
  38. 11.22.33.20 - 11.22.33.46
  39.  
  40. output: Number of objects created
  41. or undef on error
  42.  
  43. The NAobject array is filled with NetAddr::IP::Lite object references.
  44.  
  45. =cut
  46.  
  47. sub list2NetAddr {
  48. my($inref,$outref) = @_;
  49. return undef
  50. unless ref $inref eq 'ARRAY'
  51. && ref $outref eq 'ARRAY';
  52. unless ($SKIP_NetAddrIP) {
  53. require NetAddr::IP::Lite;
  54. $SKIP_NetAddrIP = 1;
  55. }
  56. @$outref = ();
  57. my $IP;
  58. no strict;
  59. foreach $IP (@$inref) {
  60. $IP =~ s/\s//g;
  61. # 11.22.33.44
  62. if ($IP =~ /^\d+\.\d+\.\d+\.\d+$/o) {
  63. push @$outref, NetAddr::IP::Lite->new($IP), 0;
  64. }
  65. # 11.22.33.44 - 11.22.33.49
  66. elsif ($IP =~ /^(\d+\.\d+\.\d+\.\d+)\s*\-\s*(\d+\.\d+\.\d+\.\d+)$/o) {
  67. push @$outref, NetAddr::IP::Lite->new($1), NetAddr::IP::Lite->new($2);
  68. }
  69. # 11.22.33.44/63
  70. elsif ($IP =~ m|^\d+\.\d+\.\d+\.\d+/\d+$|) {
  71. push @$outref, NetAddr::IP::Lite->new($IP), 0;
  72. }
  73. # 11.22.33.44/255.255.255.224
  74. elsif ($IP =~ m|^\d+\.\d+\.\d+\.\d+/\d+\.\d+\.\d+\.\d+$|o) {
  75. push @$outref, NetAddr::IP::Lite->new($IP), 0;
  76. }
  77. # ignore un-matched IP patterns
  78. }
  79. return (scalar @$outref)/2;
  80. }
  81.  
  82. =pod
  83.  
  84. $rv = matchNetAddr($ip,\@NAobject);
  85.  
  86. Check if an IP address appears in a list of NetAddr objects.
  87.  
  88. input: dot quad IP address,
  89. reference to NetAddr objects
  90. output: true if match else false
  91.  
  92. =cut
  93.  
  94. sub matchNetAddr {
  95. my($ip,$naref) = @_;
  96. return 0 unless $ip && $ip =~ /\d+\.\d+\.\d+\.\d+/;
  97. $ip =~ s/\s//g;
  98. $ip = new NetAddr::IP::Lite($ip);
  99. my $i;
  100. for($i=0; $i <= $#{$naref}; $i += 2) {
  101. my $beg = $naref->[$i];
  102. my $end = $naref->[$i+1];
  103. if ($end) {
  104. return 1 if $ip >= $beg && $ip <= $end;
  105. } else {
  106. return 1 if $ip->within($beg);
  107. }
  108. }
  109. return 0;
  110. }
  111.  
  112.  
  113.  
  114. $test = 2;
  115.  
  116. sub ok {
  117. print "ok $test\n";
  118. ++$test;
  119. }
  120.  
  121. ## test 2 instantiate netaddr array
  122. #
  123. # A multi-formated array of IP address that will never be tarpitted.
  124. #
  125. # WARNING: if you are using a private network, then you should include the
  126. # address description for the net/subnets that you are using or you might
  127. # find your DMZ or internal mail servers blocked since many DNSBLS list the
  128. # private network addresses as BLACKLISTED
  129. #
  130. # 127./8, 10./8, 172.16/12, 192.168/16
  131. #
  132. # class A xxx.0.0.0/8
  133. # class B xxx.xxx.0.0/16
  134. # class C xxx.xxx.xxx.0/24 0
  135. # 128 subnet xxx.xxx.xxx.xxx/25 128
  136. # 64 subnet xxx.xxx.xxx.xxx/26 192
  137. # 32 subnet xxx.xxx.xxx.xxx/27 224
  138. # 16 subnet xxx.xxx.xxx.xxx/28 240
  139. # 8 subnet xxx.xxx.xxx.xxx/29 248
  140. # 4 subnet xxx.xxx.xxx.xxx/30 252
  141. # 2 subnet xxx.xxx.xxx.xxx/31 254
  142. # single address xxx.xxx.xxx.xxx/32 255
  143. #
  144. @tstrng = (
  145. # a single address
  146. '11.22.33.44',
  147. # a range of ip's, ONLY VALID WITHIN THE SAME CLASS 'C'
  148. '22.33.44.55 - 22.33.44.65',
  149. '45.67.89.10-45.67.89.32',
  150. # a CIDR range
  151. '5.6.7.16/28',
  152. # a range specified with a netmask
  153. '7.8.9.128/255.255.255.240',
  154. # this should ALWAYS be here
  155. '127.0.0.0/8', # ignore all test entries and localhost
  156. );
  157. my @NAobject;
  158. my $rv = list2NetAddr(\@tstrng,\@NAobject);
  159. print "wrong number of NA objects\ngot: $rv, exp: 6\nnot "
  160. unless $rv == 6;
  161. &ok;
  162.  
  163. ## test 3 check disallowed terms
  164. print "accepted null parameter\nnot "
  165. if matchNetAddr();
  166. &ok;
  167.  
  168. ## test 4 check disallowed parm
  169. print "accepted non-numeric parameter\nnot "
  170. if matchNetAddr('junk');
  171. &ok;
  172.  
  173. ##test 5 check non-ip short
  174. print "accepted short ip segment\nnot "
  175. if matchNetAddr('1.2.3');
  176. &ok;
  177.  
  178. # yeah, it will accept a long one, but that's tough!
  179.  
  180. ## test 6-35 bracket NA objects
  181. #
  182. my @chkary = # 5 x 6 tests
  183. # out left in left middle in right out right
  184. qw( 11.22.33.43 11.22.33.44 11.22.33.44 11.22.33.44 11.22.33.45
  185. 22.33.44.54 22.33.44.55 22.33.44.60 22.33.44.65 22.33.44.66
  186. 45.67.89.9 45.67.89.10 45.67.89.20 45.67.89.32 45.67.89.33
  187. 5.6.7.15 5.6.7.16 5.6.7.20 5.6.7.31 5.6.7.32
  188. 7.8.9.127 7.8.9.128 7.8.9.138 7.8.9.143 7.8.9.144
  189. 126.255.255.255 127.0.0.0 127.128.128.128 127.255.255.255 128.0.0.0
  190. );
  191.  
  192. for(my $i=0;$i <= $#chkary; $i+=5) {
  193. print "accepted outside left bound $chkary[$i]\nnot "
  194. if matchNetAddr($chkary[$i],\@NAobject);
  195. &ok;
  196. print "rejected inside left bound $chkary[$i+1]\nnot "
  197. unless matchNetAddr($chkary[$i+1],\@NAobject);
  198. &ok;
  199. print "rejected inside middle bound $chkary[$i+2]\nnot "
  200. unless matchNetAddr($chkary[$i+2],\@NAobject);
  201. &ok;
  202. print "rejected inside right bound $chkary[$i+3]\nnot "
  203. unless matchNetAddr($chkary[$i+3],\@NAobject);
  204. &ok;
  205. print "accepted outside right bound $chkary[$i+4]\nnot "
  206. if matchNetAddr($chkary[$i+4],\@NAobject);
  207. &ok;
  208. }