blib/lib/Net/Amazon/S3/Policy.pm

filter: Statementstmt 86.5% 116 / 134 Branchbran 56.8% 25 / 44 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 90.0% 27 / 30 Podpod 100% 14 / 14 totaltotal 80.0% 184 / 230 CC 29 SCAR 16.4
CC
29
Cov
78%
remove
30.0
new
6.0
expiration
5.3
CRAP 5.1
↑ Summary
1 package Net::Amazon::S3::Policy;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use version; our $VERSION = qv('0.1.6');
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use MIME::Base64 qw< decode_base64 >;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 our @ISA = qw( Exporter );
15 2 our @EXPORT_OK = qw( exact starts_with range );
16 2 our %EXPORT_TAGS = (all => \@EXPORT_OK,);
17  
18 # Module implementation here
19 3 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
20 3 my $class = shift;
21 3 ▶ my %args = ref($_[0]) ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 2 3
22 3 my $self = bless {}, $class;
23  
24 3 ▶ if ($args{json}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'json'}) { } 2 1 3
25 2 $self->parse($args{json});
26 }
27 else {
28 1 ▶ $self->expiration($args{expiration}) if defined $args{expiration};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"expiration"} 1 0 1
29 1 $self->conditions([]);
30 1 ▶ $self->add($_) for @{$args{conditions} || []};
Condition50%1/2
$args{'conditions'} || []
l!l
01
MC/DC0%0/1
$args{'conditions'} || []
$args{'conditions'}
Truth table50%1/2
A
A= $args{'conditions'}
Aresult
11
00
31 }
32  
33 3 return $self;
34 } ## end sub new
35  
36 # Accessors
37 4 ▶sub expiration {
Subroutine expiration: called CC=5 SCAR=16.6
Pod documented
38 4 my $self = shift;
39 4 my $previous = $self->{expiration};
40 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 3 4
41 1 my $time = shift;
42 1 ▶ if ($time && $time =~ /\A \d+ \z/mxs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time and $time =~ /\A \d+ \z/msx) 1 0 1
Condition33%1/3
$time and $time =~ /\A \d+ \z/msx
!ll&&!rl&&r
001
MC/DC0%0/2
$time and $time =~ /\A \d+ \z/msx
$time $time =~ /\A \d+ \z/msx
Truth table33%1/3
A and B
A= $timeB= $time =~ /\A \d+ \z/msx
ABresult
0X0
100
111
43 1 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
44 gmtime($time);
45 1 $time = sprintf "%04d-%02d-%02dT%02d:%02d:%02d.000Z",
46 $year + 1900,
47 $mon + 1, $mday, $hour, $min, $sec;
48 } ## end if ($time && $time =~ ...
49 1 ▶ $time ? ($self->{expiration} = $time) : delete $self->{expiration};
Branch: 1 branch
BranchTrueFalseTotal
$time ? : 1 0 1
50 } ## end if (@_)
51 4 return $previous;
52 } ## end sub expiration
53  
54 10 ▶sub conditions {
Subroutine conditions: called CC=3 SCAR=11.0
Pod documented
55 10 my $self = shift;
56 10 my $previous = $self->{conditions};
57  
58 10 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 9 10
59 1 ▶ $self->{conditions} = (scalar(@_) == 1) ? shift : [@_];
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 ? : 1 0 1
60 }
61  
62 10 return $previous;
63 } ## end sub conditions
64  
65 { # try to understand rules
66  
67 5 ▶ sub _prepend_dollar {
Subroutine _prepend_dollar: called CC=2 SCAR=7.6
68 5 ▶ return substr($_[0], 0, 1) eq '$' ? $_[0] : '$' . $_[0];
Branch: 1 branch
BranchTrueFalseTotal
substr($_[0], 0, 1) eq '$' ? : 0 5 5
69 }
70 my @DWIMs = (
71 1 ▶ qr{\A\s* (\S+?) \s* \* \s*\z}mxs => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 1 my $target = _prepend_dollar(shift);
73 1 return starts_with($target, '');
74 },
75 1 ▶ qr{\A\s* (\S+) \s+ eq \s+ (.*?) \s*\z}mxs => sub{
Subroutine __ANON__: called CC=2 SCAR=7.1
76 1 my $target = _prepend_dollar(shift);
77 1 my $value = shift;
78 1 ▶ return $value eq '*' ? starts_with($target, '') : exact($target, $value);
Branch: 1 branch
BranchTrueFalseTotal
$value eq '*' ? : 0 1 1
79 },
80 3 ▶ qr{\A\s* (\S+) \s+ (?: ^ | starts[_-]?with) \s+ (.*?) \s*\z}mxs => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
81 3 my $target = _prepend_dollar(shift);
82 3 my $prefix = shift;
83 3 return starts_with($target, $prefix);
84 },
85 1 ▶ qr{\A\s* (\d+) \s*<=\s* (\S+) \s*<=\s* (\d+) \s*\z}mxs => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
86 1 my ($min, $value, $max) = @_;
87 1 s{_}{}g for $min, $max;
88  
89 # no "_prepend_dollar" for range conditions
90 1 return range($value, $min, $max);
91 },
92 2 );
93  
94 6 ▶ sub _resolve_rule {
Subroutine _resolve_rule: called CC=4 SCAR=14.1
95 6 my ($string) = @_;
96  
97 6 for my $i (0 .. (@DWIMs - 1) / 2) {
98 16 my ($regex, $callback) = @DWIMs[$i * 2, $i * 2 + 1];
99 16 ▶ if (my @captures = $string =~ /$regex/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@captures) = $string =~ /$regex/) 6 10 16
100 6 my $result = $callback->(@captures);
101 6 ▶ return $result if defined $result;
Branch: 1 branch
BranchTrueFalseTotal
if defined $result 6 0 6
102 }
103 }
104  
105 0 croak "could not understand '$_', bailing out";
106 } ## end sub _resolve_rule
107 }
108  
109 2 ▶sub add {
Subroutine add: called CC=2 SCAR=7.0
Pod documented
110 6 my ($self, $condition) = @_;
111 6 ▶ push @{$self->conditions()},
Branch: 1 branch
BranchTrueFalseTotal
ref $condition ? : 0 6 6
112 ref($condition) ? $condition : _resolve_rule($condition);
113 6 return;
114 }
115  
116 0 ▶sub remove {
Subroutine remove: not called CC=5 SCAR=34.0
Pod documented
117 0 my ($self, $condition) = @_;
118 0 ▶ $condition = _resolve_rule($condition) unless ref $condition;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $condition 0 0 0
119 0 my $conditions = $self->conditions();
120 my @filtered = grep {
121 0 my $keep;
122 0 ▶ if (@$condition != @$_) { # different lengths => different
Branch: 1 branch
BranchTrueFalseTotal
if (@$condition != @$_) { } 0 0 0
123 0 $keep = 1;
124 }
125 else {
126 0 for my $i (0 .. $#$condition) {
127 0 ▶ last if $keep = $condition->[$i] ne $_->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $keep = $condition->[$i] ne $_->[$i] 0 0 0
128 }
129 }
130 0 $keep;
131 } @$conditions;
132 0 $self->conditions(\@filtered);
133 0 return;
134 } ## end sub remove
135  
136 1 ▶sub exact {
Subroutine exact: called CC=2 SCAR=7.1
Pod documented
137 1 ▶ shift if ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 1 1
138 1 my ($target, $value) = @_;
139 1 return ['eq', $target, $value];
140 }
141  
142 4 ▶sub starts_with {
Subroutine starts_with: called CC=2 SCAR=7.1
Pod documented
143 4 ▶ shift if ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 4 4
144 4 my ($target, $value) = @_;
145 4 return ['starts-with', $target, $value];
146 }
147  
148 1 ▶sub range {
Subroutine range: called CC=2 SCAR=7.1
Pod documented
149 1 ▶ shift if ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 1 1
150 1 my ($target, $min, $max) = @_;
151 1 return [$target, $min, $max];
152 }
153  
154 2 ▶sub json {
Subroutine json: called CC=2 SCAR=7.0
Pod documented
155 2 my ($self, $args) = @_;
156 2 my %params = %$self;
157 2 ▶ delete $params{expiration} unless defined $params{expiration};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"expiration"} 0 2 2
158 2 return to_json(\%params, $args);
159 } ## end sub json
160  
161 1 ▶sub base64 {
Subroutine base64: called CC=1 SCAR=0.0
Pod documented
162 1 my $self = shift;
163 1 return encode_base64(Encode::encode('utf-8', $self->json(@_)));
164 }
165  
166 {
167 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
168 2 *stringify = \&json;
169 2 *json_base64 = \&base64;
170 2 *stringify_base64 = \&base64;
171 }
172  
173 2 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.0
Pod documented
174 2 my ($self, $json) = @_;
175  
176 2 ▶ $json = decode_base64($json)
Branch: 1 branch
BranchTrueFalseTotal
unless substr($json, 0, 1) eq "{" 0 2 2
177 unless substr($json, 0, 1) eq '{';
178  
179 2 my %decoded = %{from_json($json)};
180 $self->{conditions} = [
181 map {
182 14 ▶ if (ref($_) eq 'ARRAY') { $_; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'ARRAY') { } 12 2 14
183 else {
184 2 my ($name, $value) = %$_;
185 2 ['eq', '$' . $name, $value];
186 }
187 2 } @{$decoded{conditions}}
188 ];
189 2 $self->{expiration} = $decoded{expiration};
190  
191 2 return $self;
192 } ## end sub parse
193  
194 0 ▶sub signature {
Subroutine signature: not called CC=1 SCAR=6.9
Pod documented
195 0 my ($self, $key) = @_;
196 0 require Digest::HMAC_SHA1;
197 0 return Digest::HMAC_SHA1::hmac_sha1($self->base64(), $key);
198 }
199  
200 0 ▶sub signature_base64 {
Subroutine signature_base64: not called CC=1 SCAR=6.9
Pod documented
201 0 my ($self, $key) = @_;
202 0 return encode_base64($self->signature($key));
203 }
204  
205 # Wrapper around base64 encoder, ensuring that there's no newline
206 # to make AWS S3 happy
207 1 ▶sub encode_base64 {
Subroutine encode_base64: called CC=1 SCAR=0.0
Pod documented
208 1 return MIME::Base64::encode_base64($_[0], '');
209 }
210  
211 2 1; # Magic true value required at end of module
212 __END__
↑ Summary