blib/lib/Net/DAV/LockManager.pm

filter: Statementstmt 98.2% 112 / 114 Branchbran 98.6% 71 / 72 Conditioncond 97.4% 38 / 39 MC/DCmcdc 92.0% 23 / 25 Subroutinesub 100% 19 / 19 Podpod 100% 7 / 7 totaltotal 97.8% 270 / 276 CC 52 SCAR 21.9
CC
52
Cov
98%
_validate_lock_request
20.0
lock
8.0
list_all_locks
6.0
CRAP 8.9
« blib/lib/Net/DAV/Lock.pm ↑ Summary Build »
1 package Net::DAV::LockManager;
2  
3 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 27 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 27 ▶use Net::DAV::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 27 ▶use Net::DAV::Lock;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 27 our $VERSION = '1.304';
11 27 $VERSION = eval $VERSION;
12  
13 sub new {
14 140 ▶ my ($class, $db) = (shift, shift);
Subroutine new: called CC=1 SCAR=0.0
Pod documented
15 140 my %obj = @_;
16  
17 140 $obj{'db'} = $db;
18  
19 140 return bless \%obj, $class;
20 }
21  
22 sub can_modify {
23 178 ▶ my ($self, $req) = @_;
Subroutine can_modify: called CC=4 SCAR=13.9
Pod documented
24  
25 178 _validate_lock_request( $req, 'user' );
26  
27 159 my ($resource, $token) = @{$req}{qw/path token/};
28 159 ▶ my $lock = $self->_get_lock( $resource ) || $self->_get_indirect_lock( $resource );
Condition100%3/3
$self->_get_lock($resource) || $self->_get_indirect_lock($resource)
l!l&&r!l&&!r
2533101
MC/DC100%2/2
$self->_get_lock($resource) || $self->_get_indirect_lock($resource)
$self->_get_lock($resource) $self->_get_indirect_lock($resource)
Truth table100%3/3
A || B
A= $self->_get_lock($resource)B= $self->_get_indirect_lock($resource)
ABresult
1X1
011
000
29  
30 159 ▶ return 1 unless $lock;
Branch: 1 branch
BranchTrueFalseTotal
unless $lock 101 58 159
31 58 ▶ return 0 unless $token;
Branch: 1 branch
BranchTrueFalseTotal
unless $token 29 29 58
32  
33 29 return _is_permitted( $req, $lock );
34 }
35  
36 sub lock {
37 118 ▶ my ($self, $req) = @_;
Subroutine lock: called CC=8 SCAR=20.8
Pod documented
38  
39 118 _validate_lock_request( $req, 'user', 'owner' );
40  
41 91 my $path = $req->{'path'};
42  
43 91 ▶ return undef unless $self->can_modify( $req ) && !$self->_get_lock( $path );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can_modify($req) and not $self->_get_lock($path) 12 79 91
Condition100%3/3
$self->can_modify($req) and not $self->_get_lock($path)
!ll&&!rl&&r
10279
MC/DC100%2/2
$self->can_modify($req) and not $self->_get_lock($path)
$self->can_modify($req) not $self->_get_lock($path)
Truth table100%3/3
A and not(B)
A= $self->can_modify($req)B= not $self->_get_lock($path)
ABresult
0X0
100
111
44 79 foreach my $lock ( $self->{'db'}->list_descendants( $path ) ) {
45 4 ▶ return undef unless _is_permitted( $req, $lock );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_permitted($req, $lock) 3 1 4
46 }
47  
48 return $self->_add_lock(Net::DAV::Lock->new({
49 'path' => $path,
50 (defined $req->{'timeout'} ? ('expiry' => time() + $req->{'timeout'}) : ()),
51 'creator' => $req->{'user'},
52 'owner' => $req->{'owner'},
53 (defined $req->{'depth'} ? ('depth' => $req->{'depth'}) : ()),
54 76 ▶ (defined $req->{'scope'} ? ('scope' => $req->{'scope'}) : ()),
Branch: 3 branches
BranchTrueFalseTotal
defined $req->{'timeout'} ? : 9 67 76
defined $req->{'depth'} ? : 34 42 76
defined $req->{'scope'} ? : 29 47 76
55 }));
56 }
57  
58 sub refresh_lock {
59 28 ▶ my ($self, $req) = @_;
Subroutine refresh_lock: called CC=4 SCAR=13.9
Pod documented
60 28 _validate_lock_request( $req, 'user', 'token' );
61  
62 9 my $lock = $self->_get_lock( $req->{'path'} );
63 9 ▶ return undef unless $lock;
Branch: 1 branch
BranchTrueFalseTotal
unless $lock 2 7 9
64 7 ▶ return undef unless _is_permitted( $req, $lock );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_permitted($req, $lock) 4 3 7
65  
66 3 ▶ $lock->renew( time() + ($req->{'timeout'} || $Net::DAV::Lock::DEFAULT_LOCK_TIMEOUT) );
Condition66%2/3
$req->{'timeout'} || $Net::DAV::Lock::DEFAULT_LOCK_TIMEOUT
l!l&&r!l&&!r
210
MC/DC0%0/2
$req->{'timeout'} || $Net::DAV::Lock::DEFAULT_LOCK_TIMEOUT
$req->{'timeout'} $Net::DAV::Lock::DEFAULT_LOCK_TIMEOUT
Truth table66%2/3
A || B
A= $req->{'timeout'}B= $Net::DAV::Lock::DEFAULT_LOCK_TIMEOUT
ABresult
1X1
011
000
67  
68 3 return $self->_update_lock( $lock );
69 }
70  
71 sub unlock {
72 37 ▶ my ($self, $req) = @_;
Subroutine unlock: called CC=3 SCAR=11.0
Pod documented
73 37 _validate_lock_request( $req, 'user', 'token' );
74  
75 18 my $lock = $self->_get_lock( $req->{'path'} );
76 18 ▶ return 0 unless $lock;
Branch: 1 branch
BranchTrueFalseTotal
unless $lock 4 14 18
77 14 ▶ return 0 unless _is_permitted( $req, $lock );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_permitted($req, $lock) 3 11 14
78  
79 11 $self->_remove_lock( $lock );
80  
81 11 return 1;
82 }
83  
84 sub find_lock {
85 13 ▶ my ($self, $req) = @_;
Subroutine find_lock: called CC=2 SCAR=6.9
Pod documented
86  
87 13 _validate_lock_request( $req );
88  
89 13 my $path = $req->{'path'};
90  
91 13 ▶ return $self->_get_lock( $path ) || $self->_get_indirect_lock( $path );
Condition100%3/3
$self->_get_lock($path) || $self->_get_indirect_lock($path)
l!l&&r!l&&!r
814
MC/DC100%2/2
$self->_get_lock($path) || $self->_get_indirect_lock($path)
$self->_get_lock($path) $self->_get_indirect_lock($path)
Truth table100%3/3
A || B
A= $self->_get_lock($path)B= $self->_get_indirect_lock($path)
ABresult
1X1
011
000
92 }
93  
94 sub list_all_locks {
95 24 ▶ my ($self, $req) = @_;
Subroutine list_all_locks: called CC=6 SCAR=17.9
Pod documented
96  
97 24 _validate_lock_request( $req );
98  
99 24 my $path = $req->{'path'};
100 24 my @locks;
101 24 my $lock = $self->_get_lock( $path );
102 24 ▶ push @locks, $lock if defined $lock;
Branch: 1 branch
BranchTrueFalseTotal
if defined $lock 10 14 24
103  
104 24 ▶ while ( $path =~ s{/[^/]+$}{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ s[/[^/]+$][]) 29 24 53
105 29 ▶ $path = '/' unless length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless length $path 19 10 29
106  
107 29 my $lock = $self->_get_lock( $path );
108 29 ▶ push @locks, $lock if $lock && $lock->depth eq 'infinity';
Branch: 1 branch
BranchTrueFalseTotal
if $lock and $lock->depth eq "infinity" 4 25 29
Condition100%3/3
$lock and $lock->depth eq "infinity"
!ll&&!rl&&r
2414
MC/DC100%2/2
$lock and $lock->depth eq "infinity"
$lock $lock->depth eq "infinity"
Truth table100%3/3
A and B
A= $lockB= $lock->depth eq "infinity"
ABresult
0X0
100
111
109 }
110  
111 24 return @locks;
112 }
113  
114 #
115 # Retrieve a lock from the lock database, given the path to the lock.
116 # Return undef if none. This method also has the side effect of expiring
117 # any old locks persisted upon fetching.
118 #
119 sub _get_lock {
120 558 ▶ my ($self, $path) = @_;
Subroutine _get_lock: called CC=3 SCAR=11.6
121  
122 558 my $lock = $self->{'db'}->get( $path );
123  
124 558 ▶ return undef unless $lock;
Branch: 1 branch
BranchTrueFalseTotal
unless $lock 446 112 558
125  
126 112 ▶ if (time() >= $lock->expiry) {
Branch: 1 branch
BranchTrueFalseTotal
if (time >= $lock->expiry) 0 112 112
127 0 $self->_remove_lock($lock);
128  
129 0 return undef;
130 }
131  
132 112 return $lock;
133 }
134  
135 #
136 # Add the given lock to the database.
137 #
138 sub _add_lock {
139 76 ▶ my ($self, $lock) = @_;
Subroutine _add_lock: called CC=1 SCAR=0.0
140  
141 76 return $self->{'db'}->add($lock);
142 }
143  
144 #
145 # Update the lock provided.
146 #
147 sub _update_lock {
148 3 ▶ my ($self, $lock) = @_;
Subroutine _update_lock: called CC=1 SCAR=0.0
149  
150 3 return $self->{'db'}->update($lock);
151 }
152  
153 #
154 # Remove the lock object passed from the database.
155 #
156 sub _remove_lock {
157 11 ▶ my ($self, $lock) = @_;
Subroutine _remove_lock: called CC=1 SCAR=0.0
158  
159 11 $self->{'db'}->remove($lock);
160  
161 11 return 1;
162 }
163  
164 #
165 # Get the lock of the nearest ancestor that applies to this resource.
166 # Returns undef if none found.
167 #
168 sub _get_indirect_lock {
169 139 ▶ my ($self, $res) = @_;
Subroutine _get_indirect_lock: called CC=5 SCAR=16.1
170  
171 139 ▶ while ( $res =~ s{/[^/]+$}{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res =~ s[/[^/]+$][]) 225 105 330
172 225 ▶ $res = '/' unless length $res;
Branch: 1 branch
BranchTrueFalseTotal
unless length $res 104 121 225
173  
174 225 my $lock = $self->_get_lock( $res );
175 225 ▶ return $lock if $lock && $lock->depth eq 'infinity';
Branch: 1 branch
BranchTrueFalseTotal
if $lock and $lock->depth eq "infinity" 34 191 225
Condition100%3/3
$lock and $lock->depth eq "infinity"
!ll&&!rl&&r
184734
MC/DC100%2/2
$lock and $lock->depth eq "infinity"
$lock $lock->depth eq "infinity"
Truth table100%3/3
A and B
A= $lockB= $lock->depth eq "infinity"
ABresult
0X0
100
111
176 }
177  
178 105 return;
179 }
180  
181 #
182 # Return true or false depending on whether or not the information reflected
183 # in the request is appropriate for the lock obtained from the database. In
184 # other words, make sure the token and user match the request.
185 #
186 sub _is_permitted {
187 54 ▶ my ($req, $lock) = @_;
Subroutine _is_permitted: called CC=6 SCAR=17.9
188  
189 54 ▶ return 0 unless $req->{'user'} eq $lock->creator;
Branch: 1 branch
BranchTrueFalseTotal
unless $req->{"user"} eq $lock->creator 14 40 54
190 40 ▶ return 0 if !defined $req->{'token'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $req->{"token"} 1 39 40
191 39 ▶ if ( 'ARRAY' eq ref $req->{'token'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('ARRAY' eq ref $req->{'token'}) { } 7 32 39
192 7 ▶ return 0 unless grep { $_ eq $lock->token } @{$req->{'token'}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $lock->token;} @{$$req{"token"};} 3 4 7
193 }
194 else {
195 32 ▶ return 0 unless $req->{'token'} eq $lock->token;
Branch: 1 branch
BranchTrueFalseTotal
unless $req->{"token"} eq $lock->token 8 24 32
196 }
197  
198 28 return 1;
199 }
200  
201 #
202 # Perform general parameter validation.
203 #
204 # The parameter passed in should be a hash reference to be validated. The
205 # optional list that follows are names of required parameters besides the
206 # 'path' and 'user' parameters that are always required.
207 #
208 # Throws exception on failure.
209 #
210 sub _validate_lock_request {
211 411 ▶ my ($req, @required) = @_;
Subroutine _validate_lock_request: called CC=20 SCAR=30.0
212 411 ▶ die "Parameter should be a hash reference.\n" unless 'HASH' eq ref $req;
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $req 8 403 411
213  
214 403 foreach my $arg ( qw/path/, @required ) {
215 916 ▶ die "Missing required '$arg' parameter.\n" unless exists $req->{$arg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $req->{$arg} 15 901 916
216 }
217  
218 388 ▶ die "Not a clean path\n" if $req->{'path'} =~ m{(?:^|/)\.\.?(?:$|/)};
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"path"} =~ m[(?:^|/)\.\.?(?:$|/)] 25 363 388
219 363 ▶ die "Not a clean path\n" if $req->{'path'} !~ m{^/} && !($req->{'path'} =~ s{^https?://[^/]+/}{/});
Branch: 1 branch
BranchTrueFalseTotal
unless $req->{"path"} =~ m[^/] or $req->{"path"} =~ s[^https?://[^/]+/][/] 13 350 363
Condition100%3/3
$req->{"path"} =~ m[^/] or $req->{"path"} =~ s[^https?://[^/]+/][/]
l!l&&r!l&&!r
345513
MC/DC100%2/2
$req->{"path"} =~ m[^/] or $req->{"path"} =~ s[^https?://[^/]+/][/]
$req->{"path"} =~ m[^/] $req->{"path"} =~ s[^https?://[^/]+/][/]
Truth table100%3/3
A or B
A= $req->{"path"} =~ m[^/]B= $req->{"path"} =~ s[^https?://[^/]+/][/]
ABresult
1X1
011
000
220 350 ▶ if( defined $req->{'user'} && $req->{'user'} !~ m{^[0-9a-z_.][-a-z0-9_.]*$}i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $req->{"user"} and not $req->{"user"} =~ /^[0-9a-z_.][-a-z0-9_.]*$/i) 20 330 350
Condition100%3/3
defined $req->{"user"} and not $req->{"user"} =~ /^[0-9a-z_.][-a-z0-9_.]*$/i
!ll&&!rl&&r
2730320
MC/DC100%2/2
defined $req->{"user"} and not $req->{"user"} =~ /^[0-9a-z_.][-a-z0-9_.]*$/i
defined $req->{"user"} not $req->{"user"} =~ /^[0-9a-z_.][-a-z0-9_.]*$/i
Truth table100%3/3
A and not(B)
A= defined $req->{"user"}B= not $req->{"user"} =~ /^[0-9a-z_.][-a-z0-9_.]*$/i
ABresult
0X0
100
111
221 20 die "Not a valid user name.\n"; # May need better validation.
222 }
223  
224 # Validate optional parameters as necessary.
225 330 ▶ if( defined $req->{'scope'} && $Net::DAV::Lock::DEFAULT_SCOPE ne $req->{'scope'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $req->{"scope"} and $Net::DAV::Lock::DEFAULT_SCOPE ne $req->{"scope"}) 2 328 330
Condition100%3/3
defined $req->{"scope"} and $Net::DAV::Lock::DEFAULT_SCOPE ne $req->{"scope"}
!ll&&!rl&&r
264642
MC/DC100%2/2
defined $req->{"scope"} and $Net::DAV::Lock::DEFAULT_SCOPE ne $req->{"scope"}
defined $req->{"scope"} $Net::DAV::Lock::DEFAULT_SCOPE ne $req->{"scope"}
Truth table100%3/3
A and B
A= defined $req->{"scope"}B= $Net::DAV::Lock::DEFAULT_SCOPE ne $req->{"scope"}
ABresult
0X0
100
111
226 2 die "'$req->{'scope'}' is not a supported value for scope.\n";
227 }
228  
229 328 ▶ if( defined $req->{'depth'} && '0' ne $req->{'depth'} && 'infinity' ne $req->{'depth'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $req->{"depth"} and 0 ne $req->{"depth"} and "infinity" ne $req->{"depth"}) 2 326 328
Condition100%6/6
defined $req->{"depth"} and 0 ne $req->{"depth"} and "infinity" ne $req->{"depth"}
!ll&&!rl&&r
2191072
defined $req->{"depth"} and 0 ne $req->{"depth"}
!ll&&!rl&&r
20910109
MC/DC100%3/3
defined $req->{"depth"} and 0 ne $req->{"depth"} and "infinity" ne $req->{"depth"}
defined $req->{"depth"} 0 ne $req->{"depth"} "infinity" ne $req->{"depth"}
Truth table100%4/4
A and B and C
A= defined $req->{"depth"}B= 0 ne $req->{"depth"}C= "infinity" ne $req->{"depth"}
ABCresult
0XX0
10X0
1100
1111
230 2 die "'$req->{'depth'}' is not a supported value for depth.\n";
231 }
232  
233 326 ▶ if( defined $req->{'timeout'} && $req->{'timeout'} =~ /\D/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $req->{"timeout"} and $req->{"timeout"} =~ /\D/) 3 323 326
Condition100%3/3
defined $req->{"timeout"} and $req->{"timeout"} =~ /\D/
!ll&&!rl&&r
294293
MC/DC100%2/2
defined $req->{"timeout"} and $req->{"timeout"} =~ /\D/
defined $req->{"timeout"} $req->{"timeout"} =~ /\D/
Truth table100%3/3
A and B
A= defined $req->{"timeout"}B= $req->{"timeout"} =~ /\D/
ABresult
0X0
100
111
234 3 die "'$req->{'timeout'}' is not a supported value for timeout.\n";
235 }
236  
237 323 ▶ if ( defined $req->{'token'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $req->{"token"}) 74 249 323
238 74 ▶ unless ( !ref $req->{'token'} || 'ARRAY' eq ref $req->{'token'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not ref $req->{"token"} or "ARRAY" eq ref $req->{"token"}) 1 73 74
Condition100%3/3
not ref $req->{"token"} or "ARRAY" eq ref $req->{"token"}
l!l&&r!l&&!r
62111
MC/DC100%2/2
not ref $req->{"token"} or "ARRAY" eq ref $req->{"token"}
not ref $req->{"token"} "ARRAY" eq ref $req->{"token"}
Truth table100%3/3
not(A) or B
A= not ref $req->{"token"}B= "ARRAY" eq ref $req->{"token"}
ABresult
1X1
011
000
239 1 die "Invalid token, not a string or array reference.\n";
240 }
241 }
242  
243 # Remove trailing / from path to make pathnames canonical.
244 322 ▶ $req->{'path'} =~ s{/$}{} unless $req->{'path'} eq '/';
Branch: 1 branch
BranchTrueFalseTotal
unless $req->{"path"} eq "/" 279 43 322
245  
246 322 return;
247 }
248  
249 27 1;
250  
251 __END__
« blib/lib/Net/DAV/Lock.pm ↑ Summary Build »