SDK/Storage/Blobs/Serializer.pm

filter: Statementstmt 100% 67 / 67 Branchbran 86.6% 26 / 30 Conditioncond 52.3% 11 / 21 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 100% 16 / 16 Podpod 100% 2 / 2 totaltotal 84.5% 126 / 149 CC 24 SCAR 14.9
CC
24
Cov
88%
parse_error
8.2
_parse
6.5
__ANON__
4.3
CRAP 4.4
« SDK/Core/Headers.pm ↑ Summary SDK/Storage/Queues/Serializer.pm »
1 3 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶no warnings 'experimental::class';
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 3 class Azure::SDK::Storage::Blobs::Serializer {
6 3 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶ use bytes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶ use XML::LibXML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶ use Azure::SDK::Storage::Blobs::BlobItem;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶ use Azure::SDK::Storage::Blobs::BlobPage;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶ use Azure::SDK::Storage::Blobs::Uri;
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 # Bounds parser memory on a hostile or corrupted body (CWE-770, CWE-409).
14 3 ▶ use constant DEFAULT_MAX_BYTES => 8 * 1024 * 1024;
Subroutine BEGIN: called CC=1 SCAR=0.0
15
16 3 field $max_bytes :param :reader = DEFAULT_MAX_BYTES;
17
18 3 field $parser = undef;
19
20 30 ▶ ADJUST {
Subroutine __ANON__: called CC=4 SCAR=14.6
21 30 ▶ croak 'Azure::SDK::Storage::Blobs::Serializer max_bytes must be a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max_bytes and $max_bytes =~ /\A[0-9]+\z/u and $max_bytes > 0 1 29 30
Condition50%3/6
defined $max_bytes and $max_bytes =~ /\A[0-9]+\z/u and $max_bytes > 0
!ll&&!rl&&r
0129
defined $max_bytes and $max_bytes =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0030
MC/DC33%1/3
defined $max_bytes and $max_bytes =~ /\A[0-9]+\z/u and $max_bytes > 0
defined $max_bytes $max_bytes =~ /\A[0-9]+\z/u $max_bytes > 0
Truth table50%2/4
A and B and C
A= defined $max_bytesB= $max_bytes =~ /\A[0-9]+\z/uC= $max_bytes > 0
ABCresult
0XX0
10X0
1100
1111
22 unless defined $max_bytes && $max_bytes =~ /\A[0-9]+\z/ && $max_bytes > 0;
23
24 29 $parser = XML::LibXML->new(
25 # No network fetches, no external DTD, no entity expansion
26 # (CWE-611, CWE-776).
27 no_network => 1,
28 load_ext_dtd => 0,
29 expand_entities => 0,
30 huge => 0,
31 recover => 0,
32 no_blanks => 1,
33 );
34 $parser->set_option( ext_ent_handler => sub {
35 croak 'Refusing to resolve an external entity in a Blob response';
36 29 } );
37 }
38
39 21 ▶ method parse_blob_list ($xml) {
Subroutine parse_blob_list: called CC=4 SCAR=13.9
Pod documented
40 21 my $document = $self->_parse($xml);
41 17 my ($root) = $document->findnodes('/*[local-name()="EnumerationResults"]');
42
43 17 ▶ croak 'Blob listing response is not an EnumerationResults document'
Branch: 1 branch
BranchTrueFalseTotal
unless $root 1 16 17
44 unless $root;
45
46 16 my @items;
47 16 for my $node ( $root->findnodes('.//*[local-name()="Blobs"]/*[local-name()="Blob"]') ) {
48 16 push @items, Azure::SDK::Storage::Blobs::BlobItem->new(
49 name => _blob_name($node),
50 etag => _child_text( $node, 'Etag' ),
51 last_modified => _child_text( $node, 'Last-Modified' ),
52 content_length => _child_text( $node, 'Content-Length' ),
53 content_type => _child_text( $node, 'Content-Type' ),
54 );
55 }
56
57 14 ▶ return Azure::SDK::Storage::Blobs::BlobPage->new(
Branch: 1 branch
BranchTrueFalseTotal
$root->hasAttribute('ContainerName') ? : 14 0 14
58 items => \@items,
59 continuation_token => _child_text( $root, 'NextMarker' ),
60 container_name => $root->hasAttribute('ContainerName')
61 ? $root->getAttribute('ContainerName')
62 : undef,
63 prefix => _child_text( $root, 'Prefix' ),
64 );
65 }
66
67 # Returns undef, not a croak, when the body is not an error envelope: an
68 # empty body is a normal outcome the caller handles via the status code.
69 9 ▶ method parse_error ($xml) {
Subroutine parse_error: called CC=8 SCAR=21.1
Pod documented
70 9 ▶ return undef unless defined $xml && $xml =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xml and $xml =~ /\S/u 2 7 9
Condition66%2/3
defined $xml and $xml =~ /\S/u
!ll&&!rl&&r
027
MC/DC50%1/2
defined $xml and $xml =~ /\S/u
defined $xml $xml =~ /\S/u
Truth table66%2/3
A and B
A= defined $xmlB= $xml =~ /\S/u
ABresult
0X0
100
111
71 7 ▶ return undef if bytes::length($xml) > $max_bytes;
Branch: 1 branch
BranchTrueFalseTotal
if &bytes::length($xml) > $max_bytes 1 6 7
72
73 6 ▶ my $document = eval { $self->_parse($xml) } or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $document = eval { do { $self->_parse($xml) } } or return undef 5 1 6
74
75 5 my ($root) = $document->findnodes('/*[local-name()="Error"]');
76 5 ▶ return undef unless $root;
Branch: 1 branch
BranchTrueFalseTotal
unless $root 1 4 5
77
78 4 my $code = _child_text( $root, 'Code' );
79 4 my $message = _child_text( $root, 'Message' );
80 4 ▶ return undef unless defined $code || defined $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code or defined $message 0 4 4
Condition33%1/3
defined $code or defined $message
l!l&&r!l&&!r
400
MC/DC0%0/2
defined $code or defined $message
defined $code defined $message
Truth table33%1/3
A or B
A= defined $codeB= defined $message
ABresult
1X1
011
000
81
82 return {
83 4 code => $code,
84 message => $message,
85 };
86 }
87
88 27 ▶ method _parse ($xml) {
Subroutine _parse: called CC=6 SCAR=18.7
89 27 ▶ croak 'Blob response body is empty'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xml and length $xml 0 27 27
Condition33%1/3
defined $xml and length $xml
!ll&&!rl&&r
0027
MC/DC0%0/2
defined $xml and length $xml
defined $xml length $xml
Truth table33%1/3
A and B
A= defined $xmlB= length $xml
ABresult
0X0
100
111
90 unless defined $xml && length $xml;
91
92 # bytes::length, not length: length counts characters, so a decoded body
93 # could carry several times this many octets while appearing to fit.
94 27 ▶ croak 'Blob response body exceeds ' . $max_bytes . ' bytes'
Branch: 1 branch
BranchTrueFalseTotal
if &bytes::length($xml) > $max_bytes 2 25 27
95 if bytes::length($xml) > $max_bytes;
96
97 25 my $document = $parser->load_xml( string => $xml );
98
99 # A Blob response never carries a DTD, so rejecting one outright removes
100 # the entity-expansion class rather than relying only on parser options.
101 24 ▶ croak 'Blob response body must not contain a document type declaration'
Branch: 1 branch
BranchTrueFalseTotal
if defined $document->internalSubset or defined $document->externalSubset 2 22 24
Condition66%2/3
defined $document->internalSubset or defined $document->externalSubset
l!l&&r!l&&!r
2022
MC/DC50%1/2
defined $document->internalSubset or defined $document->externalSubset
defined $document->internalSubset defined $document->externalSubset
Truth table66%2/3
A or B
A= defined $document->internalSubsetB= defined $document->externalSubset
ABresult
1X1
011
000
102 if defined $document->internalSubset || defined $document->externalSubset;
103
104 22 return $document;
105 }
106
107 # Decodes through the URI builder's strict decoder, not a local copy: a name
108 # read here is fed straight into the next request, so a malformed escape
109 # accepted now would be re-encoded into a request for a different blob.
110 16 ▶ sub _blob_name ($node) {
Subroutine _blob_name: called CC=4 SCAR=14.0
111 16 my ($name_node) = $node->findnodes('./*[local-name()="Name"]');
112 16 ▶ return undef unless $name_node;
Branch: 1 branch
BranchTrueFalseTotal
unless $name_node 0 16 16
113
114 16 my $value = $name_node->textContent;
115 16 ▶ return $value
Branch: 1 branch
BranchTrueFalseTotal
unless $name_node->hasAttribute("Encoded") and lc $name_node->getAttribute("Encoded") eq "true" 11 5 16
Condition66%2/3
$name_node->hasAttribute("Encoded") and lc $name_node->getAttribute("Encoded") eq "true"
!ll&&!rl&&r
1105
MC/DC50%1/2
$name_node->hasAttribute("Encoded") and lc $name_node->getAttribute("Encoded") eq "true"
$name_node->hasAttribute("Encoded") lc $name_node->getAttribute("Encoded") eq "true"
Truth table66%2/3
A and B
A= $name_node->hasAttribute("Encoded")B= lc $name_node->getAttribute("Encoded") eq "true"
ABresult
0X0
100
111
116 unless $name_node->hasAttribute('Encoded')
117 && lc $name_node->getAttribute('Encoded') eq 'true';
118
119 5 return Azure::SDK::Storage::Blobs::Uri->decode_component($value);
120 }
121
122 92 ▶ sub _child_text ( $node, $name ) {
Subroutine _child_text: called CC=3 SCAR=11.0
123 92 my ($child) = $node->findnodes( './/*[local-name()="' . $name . '"]' );
124 92 ▶ return undef unless $child;
Branch: 1 branch
BranchTrueFalseTotal
unless $child 24 68 92
125
126 68 my $text = $child->textContent;
127 68 ▶ return length $text ? $text : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $text ? : 56 12 68
128 }
129 }
130
131 3 1;
132
133 __END__
« SDK/Core/Headers.pm ↑ Summary SDK/Storage/Queues/Serializer.pm »