Lite/t/netaddr.t

filter: Statementstmt 100% 71 / 71 Branchbran 70.4% 31 / 44 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 87.7% 122 / 139 CC 17 SCAR 18.4
CC
17
Cov
88%
list2NetAddr
9.3
matchNetAddr
8.0
END
2.1
CRAP 6.3
« blib/lib/NetAddr/IP/InetBase.pm ↑ Summary blib/lib/NetAddr/IP/Util.pm »
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 1 ▶BEGIN { $| = 1; print "1..35\n"; }
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶END {print "not ok 1\n" unless $loaded;}
Subroutine END: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded 0 1 1
10  
11 #use diagnostics;
12 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use NetAddr::IP::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 $loaded = 1;
16 1 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 1 ▶sub list2NetAddr {
Subroutine list2NetAddr: called CC=9 SCAR=22.3
48 1 my($inref,$outref) = @_;
49 return undef
50 1 ▶ unless ref $inref eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $inref eq "ARRAY" and ref $outref eq "ARRAY" 0 1 1
Condition33%1/3
ref $inref eq "ARRAY" and ref $outref eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
ref $inref eq "ARRAY" and ref $outref eq "ARRAY"
ref $inref eq "ARRAY" ref $outref eq "ARRAY"
Truth table33%1/3
A and B
A= ref $inref eq "ARRAY"B= ref $outref eq "ARRAY"
ABresult
0X0
100
111
51 && ref $outref eq 'ARRAY';
52 1 ▶ unless ($SKIP_NetAddrIP) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($SKIP_NetAddrIP) 1 0 1
53 1 require NetAddr::IP::Lite;
54 1 $SKIP_NetAddrIP = 1;
55 }
56 1 @$outref = ();
57 1 my $IP;
58 1 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 1 foreach $IP (@$inref) {
60 6 $IP =~ s/\s//g;
61 # 11.22.33.44
62 6 ▶ if ($IP =~ /^\d+\.\d+\.\d+\.\d+$/o) {
Branch: 4 branches
BranchTrueFalseTotal
if ($IP =~ /^\d+\.\d+\.\d+\.\d+$/o) { } 1 5 6
elsif ($IP =~ /^(\d+\.\d+\.\d+\.\d+)\s*\-\s*(\d+\.\d+\.\d+\.\d+)$/o) { } 2 3 5
elsif ($IP =~ m[^\d+\.\d+\.\d+\.\d+/\d+$]) { } 2 1 3
elsif ($IP =~ m[^\d+\.\d+\.\d+\.\d+/\d+\.\d+\.\d+\.\d+$]o) { } 1 0 1
63 1 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 2 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 2 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 1 push @$outref, NetAddr::IP::Lite->new($IP), 0;
76 }
77 # ignore un-matched IP patterns
78 }
79 1 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 33 ▶sub matchNetAddr {
Subroutine matchNetAddr: called CC=8 SCAR=20.8
95 33 my($ip,$naref) = @_;
96 33 ▶ return 0 unless $ip && $ip =~ /\d+\.\d+\.\d+\.\d+/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip and $ip =~ /\d+\.\d+\.\d+\.\d+/ 3 30 33
Condition100%3/3
$ip and $ip =~ /\d+\.\d+\.\d+\.\d+/
!ll&&!rl&&r
1230
MC/DC100%2/2
$ip and $ip =~ /\d+\.\d+\.\d+\.\d+/
$ip $ip =~ /\d+\.\d+\.\d+\.\d+/
Truth table100%3/3
A and B
A= $ipB= $ip =~ /\d+\.\d+\.\d+\.\d+/
ABresult
0X0
100
111
97 30 $ip =~ s/\s//g;
98 30 $ip = new NetAddr::IP::Lite($ip);
99 30 my $i;
100 30 ▶ for($i=0; $i <= $#{$naref}; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$naref;}) 135 12 147
101 135 my $beg = $naref->[$i];
102 135 my $end = $naref->[$i+1];
103 135 ▶ if ($end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end) { } 51 84 135
104 51 ▶ return 1 if $ip >= $beg && $ip <= $end;
Branch: 1 branch
BranchTrueFalseTotal
if $ip >= $beg and $ip <= $end 6 45 51
Condition100%3/3
$ip >= $beg and $ip <= $end
!ll&&!rl&&r
28176
MC/DC100%2/2
$ip >= $beg and $ip <= $end
$ip >= $beg $ip <= $end
Truth table100%3/3
A and B
A= $ip >= $begB= $ip <= $end
ABresult
0X0
100
111
105 } else {
106 84 ▶ return 1 if $ip->within($beg);
Branch: 1 branch
BranchTrueFalseTotal
if $ip->within($beg) 12 72 84
107 }
108 }
109 12 return 0;
110 }
111  
112  
113  
114 1 $test = 2;
115  
116 34 ▶sub ok {
Subroutine ok: called CC=1 SCAR=0.0
117 34 print "ok $test\n";
118 34 ++$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 1 @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 1 my @NAobject;
158 1 my $rv = list2NetAddr(\@tstrng,\@NAobject);
159 1 ▶print "wrong number of NA objects\ngot: $rv, exp: 6\nnot "
Branch: 1 branch
BranchTrueFalseTotal
unless $rv == 6 0 1 1
160 unless $rv == 6;
161 1 &ok;
162  
163 ## test 3 check disallowed terms
164 1 ▶print "accepted null parameter\nnot "
Branch: 1 branch
BranchTrueFalseTotal
if matchNetAddr() 0 1 1
165 if matchNetAddr();
166 1 &ok;
167  
168 ## test 4 check disallowed parm
169 1 ▶print "accepted non-numeric parameter\nnot "
Branch: 1 branch
BranchTrueFalseTotal
if matchNetAddr("junk") 0 1 1
170 if matchNetAddr('junk');
171 1 &ok;
172  
173 ##test 5 check non-ip short
174 1 ▶print "accepted short ip segment\nnot "
Branch: 1 branch
BranchTrueFalseTotal
if matchNetAddr("1.2.3") 0 1 1
175 if matchNetAddr('1.2.3');
176 1 &ok;
177  
178 # yeah, it will accept a long one, but that's tough!
179  
180 ## test 6-35 bracket NA objects
181 #
182 1 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 1 ▶for(my $i=0;$i <= $#chkary; $i+=5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#chkary) 6 1 7
193 6 ▶ print "accepted outside left bound $chkary[$i]\nnot "
Branch: 1 branch
BranchTrueFalseTotal
if matchNetAddr($chkary[$i], \@NAobject) 0 6 6
194 if matchNetAddr($chkary[$i],\@NAobject);
195 6 &ok;
196 6 ▶ print "rejected inside left bound $chkary[$i+1]\nnot "
Branch: 1 branch
BranchTrueFalseTotal
unless matchNetAddr($chkary[$i + 1], \@NAobject) 0 6 6
197 unless matchNetAddr($chkary[$i+1],\@NAobject);
198 6 &ok;
199 6 ▶ print "rejected inside middle bound $chkary[$i+2]\nnot "
Branch: 1 branch
BranchTrueFalseTotal
unless matchNetAddr($chkary[$i + 2], \@NAobject) 0 6 6
200 unless matchNetAddr($chkary[$i+2],\@NAobject);
201 6 &ok;
202 6 ▶ print "rejected inside right bound $chkary[$i+3]\nnot "
Branch: 1 branch
BranchTrueFalseTotal
unless matchNetAddr($chkary[$i + 3], \@NAobject) 0 6 6
203 unless matchNetAddr($chkary[$i+3],\@NAobject);
204 6 &ok;
205 6 ▶ print "accepted outside right bound $chkary[$i+4]\nnot "
Branch: 1 branch
BranchTrueFalseTotal
if matchNetAddr($chkary[$i + 4], \@NAobject) 0 6 6
206 if matchNetAddr($chkary[$i+4],\@NAobject);
207 6 &ok;
208 }
« blib/lib/NetAddr/IP/InetBase.pm ↑ Summary blib/lib/NetAddr/IP/Util.pm »