blib/lib/Tie/InSecureHash.pm

filter: Statementstmt 54.5% 96 / 176 Branchbran 47.6% 40 / 84 Conditioncond 20.5% 7 / 34 MC/DCmcdc 5.0% 1 / 20 Subroutinesub 77.2% 17 / 22 Podpod 0.0% 0 / 6 totaltotal 47.0% 161 / 342 CC 60 SCAR 38.6
CC
60
Cov
49%
debug
182.0
new
65.2
TIEHASH
23.8
CRAP 47.3
↑ Summary
1 package Tie::InSecureHash;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw($VERSION $strict $fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 $VERSION = '1.07';
8  
9 sub import
10 1 ▶{
Subroutine import: called CC=6 SCAR=28.7
11 1 my $pkg = shift;
12 0 ▶ foreach (@_) { $strict ||= /strict/; $fast ||= /fast/ }
Condition0%0/6
$Tie::InSecureHash::strict ||= /strict/
l!l&&r!l&&!r
000
$Tie::InSecureHash::fast ||= /fast/
l!l&&r!l&&!r
000
MC/DC0%0/2
$Tie::InSecureHash::strict ||= /strict/
$Tie::InSecureHash::strict /strict/
MC/DC0%0/2
$Tie::InSecureHash::fast ||= /fast/
$Tie::InSecureHash::fast /fast/
Truth table0%0/3
A ||= B
A= $Tie::InSecureHash::strictB= /strict/
ABresult
1X1
011
000
Truth table0%0/3
A ||= B
A= $Tie::InSecureHash::fastB= /fast/
ABresult
1X1
011
000
13 1 ▶ croak qq{$pkg can't be both "strict" and "fast"} if $strict && $fast;
Branch: 1 branch
BranchTrueFalseTotal
if $Tie::InSecureHash::strict and $Tie::InSecureHash::fast 0 1 1
Condition33%1/3
$Tie::InSecureHash::strict and $Tie::InSecureHash::fast
!ll&&!rl&&r
100
MC/DC0%0/2
$Tie::InSecureHash::strict and $Tie::InSecureHash::fast
$Tie::InSecureHash::strict $Tie::InSecureHash::fast
Truth table33%1/3
A and B
A= $Tie::InSecureHash::strictB= $Tie::InSecureHash::fast
ABresult
0X0
100
111
14 }
15  
16 # TAKE A LIST OF POSSIBLE CLASSES FOR AN IMPLICIT KEY AND REMOVE NON-CONTENDERS
17  
18 sub _winnow
19 109 ▶{
Subroutine _winnow: called CC=1 SCAR=0.0
20 109 my ($caller, $nonpublic, @classes) = @_;
21  
22 # REMOVE CLASSES NOT IN HIERARCHY FOR NON-PUBLIC KEY
23  
24 # @classes = grep { $caller->isa($_) } @classes if $nonpublic;
25  
26 # COMPARE REMAINING KEYS PAIRWISE, ELIMINATING "SHADOWED" KEYS...
27  
28  
29 109 return @classes;
30 };
31  
32 # DETERMINE IF A KEY IS ACCESSIBLE
33  
34 sub _access # ($self,$key,$caller)
35 464 ▶{
Subroutine _access: called CC=17 SCAR=28.4
36 464 my ($self, $key, $caller, $file, $delete) = @_;
37  
38 # EXPLICIT KEYS...
39  
40 464 ▶ if ($key =~ /\A([\w:]*)::((_{0,2})[^:]+)\Z/)
Branch: 3 branches
BranchTrueFalseTotal
if ($key =~ /\A([\w:]*)::((_{0,2})[^:]+)\Z/) { } 308 156 464
elsif ($key =~ /\A(__[^:]+)\Z/) { } 21 135 156
elsif ($key =~ /\A((_?)[^:]+)\Z/) { } 135 0 135
41 {
42 308 my ($classname, $shortkey, $mode) = ($1,$2,$3);
43 308 ▶ unless ($classname)
Branch: 1 branch
BranchTrueFalseTotal
unless ($classname) 18 290 308
44 {
45 18 $classname = 'main';
46 18 $key = $classname.$key;
47 }
48 308 ▶ if ($mode eq '__') # PRIVATE
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq '__') { } 108 200 308
elsif ($mode eq '_') { } 96 104 200
49 {
50 108 ▶ if (exists $self->{fullkeys}{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'fullkeys'}{$key}) { } 99 9 108
51 {
52 }
53 else
54 {
55 0 ▶ if ($delete) { delete $self->{file}{$key} }
Branch: 1 branch
BranchTrueFalseTotal
if ($delete) { } 0 9 9
56 9 else { $self->{file}{$key} = $file }
57 }
58 }
59 elsif ($mode eq '_') # PROTECTED
60 {
61 }
62  
63 308 ▶ if (!exists $self->{fullkeys}{$key})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"fullkeys"}{$key}) 49 259 308
64 {
65 49 ▶ if ($delete)
Branch: 1 branch
BranchTrueFalseTotal
if ($delete) { } 2 47 49
66 {
67 2 @{$self->{keylist}{$shortkey}} =
68 0 grep { $_ !~ /$classname/ }
69 2 @{$self->{keylist}{$shortkey}}
70 }
71 else
72 {
73 47 push @{$self->{keylist}{$shortkey}}, $classname;
74 }
75 }
76 }
77  
78 # IMPLICIT PRIVATE KEY (MUST BE IN CALLING CLASS)
79 elsif ($key =~ /\A(__[^:]+)\Z/)
80 {
81 21 ▶ if (!exists $self->{fullkeys}{"${caller}::$key"})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"fullkeys"}{"${caller}::$key"}) 11 10 21
82 {
83 }
84 21 $key = "${caller}::$key";
85 21 ▶ if (exists $self->{fullkeys}{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"fullkeys"}{$key}) 10 11 21
86 {
87 }
88 }
89  
90 # IMPLICIT PROTECTED OR PUBLIC KEY
91 # (PROTECTED KEY MUST BE IN ANCESTRAL HIERARCHY OF CALLING CLASS)
92 elsif ($key =~ /\A((_?)[^:]+)\Z/)
93 {
94 135 my $fullkey = "${caller}::$key";
95 135 ▶ if (exists $self->{fullkeys}{$fullkey})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'fullkeys'}{$fullkey}) { } 26 109 135
96 {
97 26 $key = $fullkey;
98 }
99 else
100 {
101 my @classes = _winnow($caller, $2,
102 109 ▶ @{$self->{keylist}{$key}||[]});
Condition100%2/2
$self->{'keylist'}{$key} || []
l!l
2386
MC/DC100%1/1
$self->{'keylist'}{$key} || []
$self->{'keylist'}{$key}
Truth table100%2/2
A
A= $self->{'keylist'}{$key}
Aresult
11
00
103
104 109 ▶ if (@classes)
Branch: 1 branch
BranchTrueFalseTotal
if (@classes) { } 22 87 109
105 {
106 # TOO MANY CHOICES
107 22 $key = $classes[0]."::$key";
108 }
109 else # NOT ENOUGH CHOICES
110 {
111 }
112 }
113 }
114 else # INVALID KEY
115 {
116 }
117  
118 464 ▶ if ($delete) { return delete $self->{fullkeys}{$key}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($delete) 3 461 464
119 461 return \$self->{fullkeys}{$key};
120  
121 };
122  
123  
124 # NOTE THAT NEW MAY TIE AND BLESS INTO THE SAME CLASS
125 # IF NOTHING MORE APPROPRIATE IS SPECIFIED
126  
127 sub new
128 1 ▶{
Subroutine new: called CC=12 SCAR=41.8
Pod undocumented
129 1 my %self = ();
130 1 ▶ my $class = ref($_[0])||$_[0];
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
131 1 ▶ my $blessclass = ref($_[1])||$_[1]||$class;
Condition33%2/6
ref $_[1] || $_[1] || $class
l!l&&r!l&&!r
010
ref $_[1] or $_[1]
l!l&&r!l&&!r
001
MC/DC0%0/3
ref $_[1] || $_[1] || $class
ref $_[1] $_[1] $class
Truth table25%1/4
A or B || C
A= ref $_[1]B= $_[1]C= $class
ABCresult
1XX1
01X1
0011
0000
132 1 ▶ my $impl = tie %self, $class unless $fast;
Branch: 1 branch
BranchTrueFalseTotal
unless $Tie::InSecureHash::fast 1 0 1
133 1 my $self = bless \%self, $blessclass;
134 1 splice(@_,0,2);
135 1 ▶ if (@_) # INITIALIZATION ARGUMENTS PRESENT
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
136 {
137 0 my ($ancestor, $file);
138 0 my $i = 0;
139 0 ▶ while ( ($ancestor,$file) = caller($i++) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = caller $i++) 0 0 0
140 {
141 0 ▶ last if $ancestor eq $blessclass;
Branch: 1 branch
BranchTrueFalseTotal
if $ancestor eq $blessclass 0 0 0
142 }
143 0 my ($key, $value);
144 0 ▶ while (($key,$value) = splice(@_,0,2))
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 0 0
145 {
146 0 ▶ my $fullkey = $key=~/::/ ? $key : "${blessclass}::$key";
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /::/ ? : 0 0 0
147 0 ▶ if ($fast)
Branch: 1 branch
BranchTrueFalseTotal
if ($Tie::InSecureHash::fast) { } 0 0 0
148 {
149 0 $self->{$fullkey} = $value;
150 }
151 else
152 {
153 0 $impl->{fullkeys}{$fullkey} = $value;
154 0 push @{$impl->{keylist}{$key}}, $blessclass;
155 0 ▶ $impl->{file}{$fullkey} = $file
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /\A__/ 0 0 0
156 if $key =~ /\A__/;
157 }
158 }
159 }
160  
161 1 return $self;
162 }
163  
164 # USEFUL METHODS TO DUMP INFORMATION
165  
166 sub debug
167 0 ▶{
Subroutine debug: not called CC=13 SCAR=52.0
Pod undocumented
168 0 my $self = tied %{$_[0]};
169 0 ▶ my ($caller, $file, $line, $sub) = (caller,(caller(1))[3]||"(none)");
Condition0%0/2
(caller 1)[3] || '(none)'
l!l
00
MC/DC0%0/1
(caller 1)[3] || '(none)'
(caller 1)[3]
Truth table0%0/2
A
A= (caller 1)[3]
Aresult
11
00
170 0 ▶ return _simple_debug($_[0],$caller, $file, $line, $sub) unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
171 0 my ($key, $val);
172 0 my %sorted = ();
173 0 ▶ while ($key = each %{$self->{fullkeys}})
Branch: 1 branch
BranchTrueFalseTotal
if (defined($key = CORE::each %{$$self{"fullkeys"};})) 0 0 0
174 {
175 0 $key =~ m/\A(.*?)([^:]*)\Z/;
176 0 push @{$sorted{$1}}, $key;
177 }
178  
179 0 print STDERR "\nIn subroutine '$sub' called from package '$caller' ($file, line $line):\n";
180 0 foreach my $class (keys %sorted)
181 {
182 0 print STDERR "\n\t$class\n";
183 0 foreach $key ( @{$sorted{$class}} )
184 {
185 0 print STDERR "\t\t";
186 0 my ($shortkey) = $key =~ /.*::(.*)/;
187 0 my $explanation = "";
188 0 ▶ if (eval { _access($self,$shortkey,$caller, $file); 1 })
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { _access($self, $shortkey, $caller, $file); 1 } }) { } 0 0 0
elsif ($@ =~ /\AAmbiguous key/) { } 0 0 0
189 {
190 0 print STDERR '(+)';
191 }
192 elsif ($@ =~ /\AAmbiguous key/)
193 {
194 0 print STDERR '(?)';
195 0 ($explanation = $@) =~ s/.*\n//;
196 0 $explanation =~ s/.*\n\Z//;
197 0 $explanation =~ s/\ACould/Ambiguous unless fully qualified. Could/;
198 0 $explanation =~ s/^(?!\Z)/\t\t\t>>> /gm;
199 }
200 else
201 {
202 0 print STDERR '(-)';
203 0 ▶ if ($shortkey =~ /\A__/ && $@ =~ /file/)
Branch: 2 branches
BranchTrueFalseTotal
if ($shortkey =~ /\A__/ and $@ =~ /file/) { } 0 0 0
elsif ($shortkey =~ /\A__/) { } 0 0 0
Condition0%0/3
$shortkey =~ /\A__/ and $@ =~ /file/
!ll&&!rl&&r
000
MC/DC0%0/2
$shortkey =~ /\A__/ and $@ =~ /file/
$shortkey =~ /\A__/ $@ =~ /file/
Truth table0%0/3
A and B
A= $shortkey =~ /\A__/B= $@ =~ /file/
ABresult
0X0
100
111
204 {
205 0 $explanation = "\t\t\t>>> Private entry of $class\n\t\t\t>>> declared in file $self->{file}{$key}\n\t\t\t>>> is inaccessable from file $file.\n"
206 }
207 elsif ($shortkey =~ /\A__/)
208 {
209 0 $explanation = "\t\t\t>>> Private entry of $class\n\t\t\t>>> is inaccessable from package $caller.\n"
210 }
211 else
212 {
213 0 $explanation = "\t\t\t>>> Protected entry of $class\n\t\t\t>>> is inaccessible outside its hierarchy (i.e. from $caller).\n"
214 }
215
216 }
217 0 my $val = $self->{fullkeys}{$key};
218 0 ▶ if (defined $val) { $val = "'$val'" }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
219 0 else { $val = "undef" }
220 0 print STDERR " '$shortkey'\t=> $val";
221 0 ▶ print STDERR "\n$explanation" if $explanation;
Branch: 1 branch
BranchTrueFalseTotal
if $explanation 0 0 0
222 0 print STDERR "\n";
223 }
224 }
225 }
226  
227 sub _simple_debug
228 0 ▶{
Subroutine _simple_debug: not called CC=4 SCAR=30.0
229 0 my ($self,$caller, $file, $line, $sub) = @_;
230 0 my ($key, $val);
231 0 my %sorted = ();
232 0 ▶ while ($key = each %{$self})
Branch: 1 branch
BranchTrueFalseTotal
if (defined($key = CORE::each %{$self;})) 0 0 0
233 {
234 0 $key =~ m/\A(.*?)([^:]*)\Z/;
235 0 push @{$sorted{$1}}, $key;
236 }
237  
238 0 print "\nIn subroutine '$sub' called from package '$caller' ($file, line $line):\n";
239 0 foreach my $class (keys %sorted)
240 {
241 0 print "\n\t$class\n";
242 0 foreach $key ( @{$sorted{$class}} )
243 {
244 0 print "\t\t";
245 0 print " '$key'\t=> '$self->{$key}'\n";
246 }
247 }
248 }
249  
250  
251 17 ▶sub each { each %{$_[0]} }
Subroutine each: called CC=1 SCAR=0.0
Pod undocumented
252 0 ▶sub keys { keys %{$_[0]} }
Subroutine keys: not called CC=1 SCAR=6.9
Pod undocumented
253 1 ▶sub values { values %{$_[0]} }
Subroutine values: called CC=1 SCAR=0.0
Pod undocumented
254 0 ▶sub exists { exists $_[0]->{$_[1]} }
Subroutine exists: not called CC=1 SCAR=6.9
Pod undocumented
255  
256 sub TIEHASH # ($class, @args)
257 1 ▶{
Subroutine TIEHASH: called CC=7 SCAR=31.7
258 1 ▶ my $class = ref($_[0]) || $_[0];
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
259 1 ▶ if ($strict)
Branch: 2 branches
BranchTrueFalseTotal
if ($Tie::InSecureHash::strict) { } 0 1 1
elsif ($Tie::InSecureHash::fast) { } 0 1 1
260 {
261 0 ▶ carp qq{Tie'ing a securehash directly will be unsafe in 'fast' mode.\n}.
Branch: 1 branch
BranchTrueFalseTotal
unless (caller 1)[3] =~ /\A(.*?)::([^:]*)\Z/ and $2 eq "new" and $1->isa("Tie::SecureHash") 0 0 0
Condition0%0/6
(caller 1)[3] =~ /\A(.*?)::([^:]*)\Z/ and $2 eq "new" and $1->isa("Tie::SecureHash")
!ll&&!rl&&r
000
(caller 1)[3] =~ /\A(.*?)::([^:]*)\Z/ and $2 eq "new"
!ll&&!rl&&r
000
MC/DC0%0/3
(caller 1)[3] =~ /\A(.*?)::([^:]*)\Z/ and $2 eq "new" and $1->isa("Tie::SecureHash")
(caller 1)[3] =~ /\A(.*?)::([^:]*)\Z/ $2 eq "new" $1->isa("Tie::SecureHash")
Truth table0%0/4
A and B and C
A= (caller 1)[3] =~ /\A(.*?)::([^:]*)\Z/B= $2 eq "new"C= $1->isa("Tie::SecureHash")
ABCresult
0XX0
10X0
1100
1111
262 qq{Use Tie::SecureHash::new instead}
263 unless (caller 1)[3] =~ /\A(.*?)::([^:]*)\Z/
264 && $2 eq "new"
265 && $1->isa('Tie::SecureHash');
266 }
267 elsif ($fast)
268 {
269 0 carp qq{Tie'ing a securehash directly should never happen in 'fast' mode.\n}.
270 qq{Use Tie::SecureHash::new instead}
271 }
272 1 bless {}, $class;
273 }
274  
275 sub FETCH # ($self, $key)
276 205 ▶{
Subroutine FETCH: called CC=2 SCAR=7.6
277 205 my ($self, $key) = @_;
278 205 my $entry = _access($self,$key,(caller)[0..1]);
279 205 ▶ return $$entry if $entry;
Branch: 1 branch
BranchTrueFalseTotal
if $entry 205 0 205
280 0 return;
281 }
282  
283 sub STORE # ($self, $key, $value)
284 54 ▶{
Subroutine STORE: called CC=2 SCAR=7.6
285 54 my ($self, $key, $value) = @_;
286 54 my $entry = _access($self,$key,(caller)[0..1]);
287 54 ▶ return $$entry = $value if $entry;
Branch: 1 branch
BranchTrueFalseTotal
if $entry 54 0 54
288 0 return;
289 }
290  
291 sub DELETE # ($self, $key)
292 3 ▶{
Subroutine DELETE: called CC=1 SCAR=0.0
293 3 my ($self, $key) = @_;
294 3 return _access($self,$key,(caller)[0..1],'DELETE');
295 }
296  
297 sub CLEAR # ($self)
298 2 ▶{
Subroutine CLEAR: called CC=2 SCAR=7.0
299 2 my ($self) = @_;
300 2 my ($caller, $file) = caller;
301 my @inaccessibles =
302 56 grep { ! eval { _access($self,$_,$caller,$file); 1 } }
303 2 keys %{$self->{fullkeys}};
304 croak "Unable to assign to securehash because the following existing keys\nare inaccessible from package $caller and cannot be deleted:\n" .
305 0 ▶ join("\n", map {"\t$_"} @inaccessibles) . "\n "
Branch: 1 branch
BranchTrueFalseTotal
if @inaccessibles 0 2 2
306 if @inaccessibles;
307 2 %{$self} = ();
308 }
309  
310 sub EXISTS # ($self, $key)
311 50 ▶{
Subroutine EXISTS: called CC=2 SCAR=7.0
312 50 my ($self, $key) = @_;
313 50 my @context = (caller)[0..1];
314 50 ▶ eval { _access($self,$key,@context); 1 } ? 1 : '';
Branch: 1 branch
BranchTrueFalseTotal
eval { do { _access($self, $key, @context); 1 } } ? : 50 0 50
315 }
316  
317 sub FIRSTKEY # ($self)
318 6 ▶{
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
319 6 my ($self) = @_;
320 6 keys %{$self->{fullkeys}};
321 6 goto &NEXTKEY;
322 }
323  
324 sub NEXTKEY # ($self)
325 102 ▶{
Subroutine NEXTKEY: called CC=3 SCAR=11.0
326 102 my $self = $_[0];
327 102 my $key;
328 102 my @context = (caller)[0..1];
329 102 ▶ while (defined($key = each %{$self->{fullkeys}}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($key = CORE::each %{$$self{"fullkeys"};})) 96 6 102
330 {
331 96 ▶ last if eval { _access($self,$key,@context) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { _access($self, $key, @context) } } 96 0 96
332 }
333 102 return $key;
334 }
335  
336 sub DESTROY # ($self)
337 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=0.0
338 # NOTHING TO DO
339 # (BE CAREFUL SINCE IT DOES DOUBLE DUTY FOR tie AND bless)
340 }
341  
342  
343 1 1;
344 __END__
↑ Summary