lib/Mogura/ResultStream.pm

filter: Statementstmt 98.7% 77 / 78 Branchbran 78.2% 61 / 78 Conditioncond 84.2% 64 / 76 MC/DCmcdc 57.5% 19 / 33 Subroutinesub 100% 6 / 6 Podpod 33.3% 1 / 3 totaltotal 83.2% 228 / 274 CC 68 SCAR 34.1
CC
68
Cov
87%
on_any
39.6
valid_artifact
27.7
read
12.3
CRAP 30.4
« lib/Mogura/Scanner/Client.pm ↑ Summary lib/Mogura/Server/API.pm »
1 package Mogura::ResultStream;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use parent 'JSON::SL::Tuba';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # Retain the envelope and one result at a time. Tool metadata outside the
7 # descriptor is validated by the parser but never materialized in Perl.
8 48 ▶sub read {
Subroutine read: called CC=12 SCAR=25.1
Pod undocumented
9 48 my ($class, $file, $kind, $consume, $digest) = @_;
10 48 my %arrays = (syft => 'artifacts', grype => 'matches', cdx => 'components');
11 48 ▶ die 'Unknown result kind' unless $arrays{$kind};
Branch: 1 branch
BranchTrueFalseTotal
unless $arrays{$kind} 0 48 48
12 48 my $parser = $class->new(utf8 => 1, cb_unified => 1);
13 48 $parser->{array} = $arrays{$kind};
14 48 $parser->{consume} = $consume;
15 48 $parser->{stack} = [];
16 48 ▶ open my $handle, '<:raw', $file or die "Open result: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<:raw", $file or die "Open result: $!" 48 0 48
17  
18 48 while (1) {
19 110 my $size = CORE::read($handle, my $chunk, 65536);
20 110 ▶ die "Read result: $!" unless defined $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size 0 110 110
21 110 ▶ last unless $size;
Branch: 1 branch
BranchTrueFalseTotal
unless $size 45 65 110
22 65 ▶ $digest->add($chunk) if $digest;
Branch: 1 branch
BranchTrueFalseTotal
if $digest 10 55 65
23 65 $parser->{offset} += $size;
24  
25 # Tuba accumulates scalar tokens across chunks. Bound unfinished tokens
26 # and projected records before accepting another chunk.
27 65 ▶ my $start = $parser->{collect_start} // $parser->{last_event} // 0;
Condition100%5/5
($parser->{"collect_start"} // $parser->{"last_event"}) // 0
l!l
1748
$parser->{"collect_start"} // $parser->{"last_event"}
l!l&&r!l&&!r
41348
MC/DC100%2/2
($parser->{"collect_start"} // $parser->{"last_event"}) // 0
$parser->{"collect_start"} $parser->{"last_event"}
Truth table100%3/3
A // B
A= $parser->{"collect_start"}B= $parser->{"last_event"}
ABresult
1X1
011
000
28 65 ▶ die 'Result record or token exceeds 8 MiB' if $parser->{offset} - $start > 8 * 1024 * 1024;
Branch: 1 branch
BranchTrueFalseTotal
if $parser->{"offset"} - $start > 8388608 0 65 65
29 65 $parser->parse($chunk);
30 }
31 45 ▶ close $handle or die "Close result: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $handle or die "Close result: $!" 45 0 45
32 45 ▶ die 'Incomplete JSON result' unless $parser->{finished} && !@{$parser->{stack}};
Branch: 1 branch
BranchTrueFalseTotal
unless $parser->{"finished"} and not @{$parser->{"stack"};} 3 42 45
Condition66%2/3
$parser->{"finished"} and not @{$parser->{"stack"};}
!ll&&!rl&&r
3042
MC/DC50%1/2
$parser->{"finished"} and not @{$parser->{"stack"};}
$parser->{"finished"} not @{$parser->{"stack"};}
Truth table66%2/3
A and not(B)
A= $parser->{"finished"}B= not @{$parser->{"stack"};}
ABresult
0X0
100
111
33 42 return $parser->{envelope};
34 }
35  
36 6172 ▶sub on_any {
Subroutine on_any: called CC=39 SCAR=36.8
Pod documented
37 6172 my ($self, $info, $data) = @_;
38 6172 $self->{last_event} = $self->{offset};
39 6172 my ($type, $mode) = ("$info->{Type}", "$info->{Mode}");
40 6172 ▶ if ($type eq 'D') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "D") 91 6081 6172
41 91 ▶ if ($mode eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq '+') { } 48 43 91
42 48 ▶ die 'Multiple JSON documents' if $self->{started}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"started"}++ 0 48 48
43 }
44 else {
45 43 $self->{finished} = 1;
46 }
47 91 return;
48 }
49 6081 my $stack = $self->{stack};
50 6081 ▶ if ($mode eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "-") 2039 4042 6081
51 2039 my $frame = pop @$stack;
52 2039 ▶ delete $self->{collect_start} if $frame->{bounded};
Branch: 1 branch
BranchTrueFalseTotal
if $frame->{"bounded"} 280 1759 2039
53 2039 ▶ $self->{consume}->($frame->{value}) if $frame->{item} && $self->{consume};
Branch: 1 branch
BranchTrueFalseTotal
if $frame->{"item"} and $self->{"consume"} 252 1787 2039
Condition100%3/3
$frame->{"item"} and $self->{"consume"}
!ll&&!rl&&r
1659128252
MC/DC100%2/2
$frame->{"item"} and $self->{"consume"}
$frame->{"item"} $self->{"consume"}
Truth table100%3/3
A and B
A= $frame->{"item"}B= $self->{"consume"}
ABresult
0X0
100
111
54 2039 return;
55 }
56 4042 my $depth = @$stack;
57 4042 my $key = $info->{Key};
58 4042 ▶ my $item = $depth == 2 && ($stack->[1]{key} // '') eq $self->{array};
Condition80%4/5
$depth == 2 && ($stack->[1]{'key'} // '') eq $self->{'array'}
!ll&&!rl&&r
358478380
$stack->[1]{"key"} // ""
l!l
4580
MC/DC50%1/2
$depth == 2 && ($stack->[1]{'key'} // '') eq $self->{'array'}
$depth == 2 $stack->[1]{"key"}
Truth table66%2/3
A && B
A= $depth == 2B= $stack->[1]{"key"}
ABresult
0X0
100
111
59 my $collect =
60 !$depth
61 || ($depth == 1 && ($key // '') eq 'descriptor')
62 || ($depth > 1 && $stack->[-1]{collect})
63 4042 ▶ || ($item && $self->{consume});
Condition95%19/20
!$depth || $depth == 1 && ($key // '') eq 'descriptor' || $depth > 1 && $stack->[-1]{'collect'} || $item && $self->{'consume'}
l!l&&r!l&&!r
24302521360
not $depth or $depth == 1 and ($key // "") eq "descriptor" or $depth > 1 and $stack->[-1]{"collect"}
l!l&&r!l&&!r
7623541612
not $depth or $depth == 1 and ($key // "") eq "descriptor"
l!l&&r!l&&!r
48283966
$depth == 1 and ($key // "") eq "descriptor"
!ll&&!rl&&r
38798728
$key // ""
l!l
1150
$depth > 1 and $stack->[-1]{"collect"}
!ll&&!rl&&r
8715252354
$item and $self->{"consume"}
!ll&&!rl&&r
1232128252
MC/DC71%5/7
!$depth || $depth == 1 && ($key // '') eq 'descriptor' || $depth > 1 && $stack->[-1]{'collect'} || $item && $self->{'consume'}
not $depth $depth == 1 $key $depth > 1 $stack->[-1]{"collect"} $item $self->{"consume"}
Truth table37%6/16
not(A) or B and C or D and E || F and G
A= not $depthB= $depth == 1C= $keyD= $depth > 1E= $stack->[-1]{"collect"}F= $itemG= $self->{"consume"}
ABCDEFGresult
1XXXXXX1
011XXXX1
00X11XX1
01011XX1
00X0X111
00X10111
0100X111
01010111
00X0X0X0
00X0X100
00X100X0
00X10100
0100X0X0
0100X100
010100X0
01010100
64 4042 ▶ my $container = $type eq '{' || $type eq '[';
Condition100%3/3
$type eq '{' || $type eq '['
l!l&&r!l&&!r
16214231998
MC/DC100%2/2
$type eq '{' || $type eq '['
$type eq '{' $type eq '['
Truth table100%3/3
A || B
A= $type eq '{'B= $type eq '['
ABresult
1X1
011
000
65 4042 ▶ my $value = $container ? ($type eq '{' ? {} : []) : ($type eq '?' ? ($data ? 1 : 0) : $data);
Branch: 4 branches
BranchTrueFalseTotal
$container ? : 2044 1998 4042
$type eq '{' ? : 1621 423 2044
$type eq '?' ? : 9 1989 1998
$data ? : 8 1 9
66 4042 ▶ if (!$depth) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $depth) { } 48 3994 4042
elsif (not $item and $depth == 1 && ($key // '') =~ /\A(?:artifacts|matches|components|descriptor|source|bomFormat|specVersion)\z/ || $depth > 1 && $stack->[-1]{'collect'}) { } 2466 1911 4377
67 48 ▶ die 'Invalid JSON result envelope' unless $type eq '{';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "{" 1 47 48
68 47 $self->{envelope} = $value;
69 }
70 ▶ elsif (
Condition92%13/14
not $item and $depth == 1 && ($key // "") =~ /\A(?:artifacts|matches|components|descriptor|source|bomFormat|specVersion)\z/ || $depth > 1 && $stack->[-1]{"collect"}
!ll&&!rl&&r
38011482466
$depth == 1 and ($key // "") =~ /\A(?:artifacts|matches|components|descriptor|source|bomFormat|specVersion)\z/ or $depth > 1 and $stack->[-1]{"collect"}
l!l&&r!l&&!r
11223541148
$depth == 1 and ($key // "") =~ /\A(?:artifacts|matches|components|descriptor|source|bomFormat|specVersion)\z/
!ll&&!rl&&r
34993112
$key // ""
l!l
1150
$depth > 1 and $stack->[-1]{"collect"}
!ll&&!rl&&r
311452354
MC/DC60%3/5
not $item and $depth == 1 && ($key // "") =~ /\A(?:artifacts|matches|components|descriptor|source|bomFormat|specVersion)\z/ || $depth > 1 && $stack->[-1]{"collect"}
not $item $depth == 1 $key $depth > 1 $stack->[-1]{"collect"}
Truth table50%4/8
not(A) and B and C or D and E
A= not $itemB= $depth == 1C= $keyD= $depth > 1E= $stack->[-1]{"collect"}
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
71 !$item
72 && (
73 (
74 $depth == 1
75 && ($key // '') =~
76 /\A(?:artifacts|matches|components|descriptor|source|bomFormat|specVersion)\z/
77 )
78 || ($depth > 1 && $stack->[-1]{collect})
79 )
80 )
81 {
82 2466 my $parent = $stack->[-1]{value};
83 2466 ▶ if (ref $parent eq 'HASH') { $parent->{$key} = $value; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parent eq 'HASH') { } 2216 250 2466
84 250 else { push @$parent, $value; }
85 }
86 4041 ▶ if ($container) {
Branch: 2 branches
BranchTrueFalseTotal
if ($container) { } 2043 1998 4041
elsif ($item and $self->{'consume'}) { } 0 3996 3996
87 2043 ▶ my $bounded = ($depth == 1 && ($key // '') eq 'descriptor') || ($item && $self->{consume});
Condition90%10/11
$depth == 1 && ($key // '') eq 'descriptor' || $item && $self->{'consume'}
l!l&&r!l&&!r
282521763
$depth == 1 and ($key // "") eq "descriptor"
!ll&&!rl&&r
19526328
$key // ""
l!l
910
$item and $self->{"consume"}
!ll&&!rl&&r
1635128252
MC/DC75%3/4
$depth == 1 && ($key // '') eq 'descriptor' || $item && $self->{'consume'}
$depth == 1 $key $item $self->{"consume"}
Truth table57%4/7
A and B || C and D
A= $depth == 1B= $keyC= $itemD= $self->{"consume"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
88 2043 ▶ $self->{collect_start} = $self->{offset} if $bounded;
Branch: 1 branch
BranchTrueFalseTotal
if $bounded 280 1763 2043
89 2043 push @$stack,
90 {bounded => $bounded, key => $key, collect => $collect, item => $item, value => $value};
91 }
92 ▶ elsif ($item && $self->{consume}) {
Condition33%1/3
$item and $self->{"consume"}
!ll&&!rl&&r
199800
MC/DC0%0/2
$item and $self->{"consume"}
$item $self->{"consume"}
Truth table33%1/3
A and B
A= $itemB= $self->{"consume"}
ABresult
0X0
100
111
93 0 $self->{consume}->($value);
94 }
95 }
96  
97 43 ▶sub valid_artifact {
Subroutine valid_artifact: called CC=19 SCAR=33.2
Pod undocumented
98 43 my ($self, $file, $kind) = @_;
99  
100 43 my $data = $self->read($file, $kind);
101 38 ▶ die "Invalid $kind JSON" unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 38 38
102  
103 # Validate the envelope before publishing or using an output as another
104 # tool's input. A syntactically valid JSON document alone is insufficient.
105 38 ▶ if ($kind eq 'syft') {
Branch: 3 branches
BranchTrueFalseTotal
if ($kind eq 'syft') { } 14 24 38
elsif ($kind eq 'cdx') { } 12 12 24
elsif ($kind eq 'grype') { } 12 0 12
106 14 ▶ die 'Invalid Syft SBOM' unless ref($data->{artifacts}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->{"artifacts"} eq "ARRAY" 0 14 14
107 14 ▶ die 'Invalid Syft SBOM' unless ref($data->{descriptor}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->{"descriptor"} eq "HASH" 0 14 14
108 14 ▶ die 'Invalid Syft SBOM' unless ($data->{descriptor}{name} // '') eq 'syft';
Branch: 1 branch
BranchTrueFalseTotal
unless ($data->{"descriptor"}{"name"} // "") eq "syft" 0 14 14
Condition50%1/2
$data->{"descriptor"}{"name"} // ""
l!l
140
MC/DC0%0/1
$data->{"descriptor"}{"name"} // ""
$data->{"descriptor"}{"name"}
Truth table50%1/2
A
A= $data->{"descriptor"}{"name"}
Aresult
11
00
109 14 ▶ die 'Invalid Syft SBOM' unless ref($data->{source}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->{"source"} eq "HASH" 0 14 14
110 }
111 elsif ($kind eq 'cdx') {
112 12 ▶ die 'Invalid CycloneDX SBOM' unless ($data->{bomFormat} // '') eq 'CycloneDX';
Branch: 1 branch
BranchTrueFalseTotal
unless ($data->{"bomFormat"} // "") eq "CycloneDX" 0 12 12
Condition50%1/2
$data->{"bomFormat"} // ""
l!l
120
MC/DC0%0/1
$data->{"bomFormat"} // ""
$data->{"bomFormat"}
Truth table50%1/2
A
A= $data->{"bomFormat"}
Aresult
11
00
113 12 ▶ die 'Invalid CycloneDX SBOM' unless $data->{specVersion};
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"specVersion"} 0 12 12
114 die 'Invalid CycloneDX SBOM'
115 12 ▶ if exists $data->{components} && ref($data->{components}) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"components"} and ref $data->{"components"} ne "ARRAY" 0 12 12
Condition33%1/3
exists $data->{"components"} and ref $data->{"components"} ne "ARRAY"
!ll&&!rl&&r
0120
MC/DC0%0/2
exists $data->{"components"} and ref $data->{"components"} ne "ARRAY"
exists $data->{"components"} ref $data->{"components"} ne "ARRAY"
Truth table33%1/3
A and B
A= exists $data->{"components"}B= ref $data->{"components"} ne "ARRAY"
ABresult
0X0
100
111
116 }
117 elsif ($kind eq 'grype') {
118 12 ▶ die 'Invalid Grype result' unless ref($data->{matches}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->{"matches"} eq "ARRAY" 1 11 12
119 11 ▶ die 'Invalid Grype result' unless ref($data->{descriptor}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data->{"descriptor"} eq "HASH" 0 11 11
120 11 ▶ die 'Invalid Grype result' unless ($data->{descriptor}{name} // '') eq 'grype';
Branch: 1 branch
BranchTrueFalseTotal
unless ($data->{"descriptor"}{"name"} // "") eq "grype" 1 10 11
Condition50%1/2
$data->{"descriptor"}{"name"} // ""
l!l
110
MC/DC0%0/1
$data->{"descriptor"}{"name"} // ""
$data->{"descriptor"}{"name"}
Truth table50%1/2
A
A= $data->{"descriptor"}{"name"}
Aresult
11
00
121 }
122 36 return $data;
123 }
124  
125 8 1;
« lib/Mogura/Scanner/Client.pm ↑ Summary lib/Mogura/Server/API.pm »