CycloneDX.pm

filter: Statementstmt 82.0% 151 / 184 Branchbran 50.0% 23 / 46 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 86.6% 39 / 45 Podpod 100% 7 / 7 totaltotal 75.3% 220 / 292 CC 34 SCAR 22.7
CC
34
Cov
74%
get_component_by_purl
20.0
get_component_by_bom_ref
20.0
get_vulnerabilities_for_bom_ref
20.0
CRAP 9.6
« CycloneDX/License.pm ↑ Summary CycloneDX/Version.pm »
1 package SBOM::CycloneDX;
2  
3 16 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 16 ▶use Types::Standard qw(Str StrMatch Int Num InstanceOf HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Types::TypeTiny qw(ArrayLike);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 16 ▶use SBOM::CycloneDX::Declarations;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 16 ▶use SBOM::CycloneDX::Definitions;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use SBOM::CycloneDX::Dependency;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 16 ▶use SBOM::CycloneDX::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 16 ▶use SBOM::CycloneDX::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 16 ▶use SBOM::CycloneDX::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 16 ▶use SBOM::CycloneDX::Util qw(urn_uuid);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 16 ▶use List::Util qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 16 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 16 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 16 extends 'SBOM::CycloneDX::Base';
25  
26 16 ▶use constant JSON_SCHEMA_1_2 => 'http://cyclonedx.org/schema/bom-1.2b.schema.json';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 16 ▶use constant JSON_SCHEMA_1_3 => 'http://cyclonedx.org/schema/bom-1.3a.schema.json';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 16 ▶use constant JSON_SCHEMA_1_4 => 'http://cyclonedx.org/schema/bom-1.4.schema.json';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 16 ▶use constant JSON_SCHEMA_1_5 => 'http://cyclonedx.org/schema/bom-1.5.schema.json';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 16 ▶use constant JSON_SCHEMA_1_6 => 'http://cyclonedx.org/schema/bom-1.6.schema.json';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 16 ▶use constant JSON_SCHEMA_1_7 => 'http://cyclonedx.org/schema/bom-1.7.schema.json';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 16 our $VERSION = 1.09;
34  
35 16 our %JSON_SCHEMA = (
36 '1.2' => JSON_SCHEMA_1_2,
37 '1.3' => JSON_SCHEMA_1_3,
38 '1.4' => JSON_SCHEMA_1_4,
39 '1.5' => JSON_SCHEMA_1_5,
40 '1.6' => JSON_SCHEMA_1_6,
41 '1.7' => JSON_SCHEMA_1_7
42 );
43  
44 16 has bom_format => (is => 'ro', isa => Str, required => 1, default => 'CycloneDX');
45  
46 57 ▶has spec_version => (is => 'rw', isa => Num->where(sub { defined $JSON_SCHEMA{$_} }), required => 1, default => 1.7);
Subroutine __ANON__: called CC=1 SCAR=0.0
47  
48 has serial_number => (
49 is => 'rw',
50 isa => StrMatch [qr{^urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$}],
51 0 ▶ default => sub {urn_uuid}
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 16 );
53  
54 16 has version => (is => 'rw', isa => Int, default => 1);
55  
56 has metadata =>
57 57 ▶ (is => 'rw', isa => InstanceOf ['SBOM::CycloneDX::Metadata'], default => sub { SBOM::CycloneDX::Metadata->new });
Subroutine __ANON__: called CC=1 SCAR=0.0
58  
59 has components => (
60 is => 'rw',
61 isa => ArrayLike [InstanceOf ['SBOM::CycloneDX::Component']],
62 57 ▶ default => sub { SBOM::CycloneDX::List->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
63 16 );
64  
65 has services => (
66 is => 'rw',
67 isa => ArrayLike [InstanceOf ['SBOM::CycloneDX::Service']],
68 57 ▶ default => sub { SBOM::CycloneDX::List->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
69 16 );
70  
71 has external_references => (
72 is => 'rw',
73 isa => ArrayLike [InstanceOf ['SBOM::CycloneDX::ExternalReference']],
74 57 ▶ default => sub { SBOM::CycloneDX::List->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
75 16 );
76  
77 has dependencies => (
78 is => 'rw',
79 isa => ArrayLike [InstanceOf ['SBOM::CycloneDX::Dependency']],
80 57 ▶ default => sub { SBOM::CycloneDX::List->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
81 16 );
82  
83 has compositions => (
84 is => 'rw',
85 isa => ArrayLike [InstanceOf ['SBOM::CycloneDX::Composition']],
86 57 ▶ default => sub { SBOM::CycloneDX::List->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
87 16 );
88  
89 has vulnerabilities => (
90 is => 'rw',
91 isa => ArrayLike [InstanceOf ['SBOM::CycloneDX::Vulnerability']],
92 57 ▶ default => sub { SBOM::CycloneDX::List->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
93 16 );
94  
95 has annotations => (
96 is => 'rw',
97 isa => ArrayLike [InstanceOf ['SBOM::CycloneDX::Annotations']],
98 57 ▶ default => sub { SBOM::CycloneDX::List->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
99 16 );
100  
101 has formulation => (
102 is => 'rw',
103 isa => ArrayLike [InstanceOf ['SBOM::CycloneDX::Formulation']],
104 57 ▶ default => sub { SBOM::CycloneDX::List->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
105 16 );
106  
107 has declarations => (
108 is => 'rw',
109 isa => InstanceOf ['SBOM::CycloneDX::Declarations'],
110 57 ▶ default => sub { SBOM::CycloneDX::Declarations->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
111 16 );
112  
113 has definitions => (
114 is => 'rw',
115 isa => InstanceOf ['SBOM::CycloneDX::Definitions'],
116 57 ▶ default => sub { SBOM::CycloneDX::Definitions->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
117 16 );
118  
119 has citations => (
120 is => 'rw',
121 isa => ArrayLike [InstanceOf ['SBOM::CycloneDX::Citation']],
122 57 ▶ default => sub { SBOM::CycloneDX::List->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
123 16 );
124  
125 has properties => (
126 is => 'rw',
127 isa => ArrayLike [InstanceOf ['SBOM::CycloneDX::Property']],
128 57 ▶ default => sub { SBOM::CycloneDX::List->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
129 16 );
130  
131 # TODO JSF (JSON Signature Format)
132 57 ▶has signature => (is => 'rw', isa => HashRef, default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
133  
134  
135 0 ▶sub validate { SBOM::CycloneDX::Schema->new(bom => shift)->validate }
Subroutine validate: not called CC=1 SCAR=6.9
Pod documented
136  
137 12 ▶sub add_dependency {
Subroutine add_dependency: called CC=5 SCAR=18.2
Pod documented
138  
139 12 my ($self, $target, $depends_on) = @_;
140  
141 12 my $target_ref = $target->bom_ref;
142 12 my @depends_on_refs = map { $_->bom_ref } @{$depends_on};
143  
144 12 my $exists = 0;
145  
146 12 foreach my $dependency ($self->dependencies->each) {
147  
148 6 ▶ next unless $dependency->ref eq $target_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $dependency->ref eq $target_ref 6 0 6
149  
150 0 $exists = 1;
151  
152 # TODO
153 0 my @old_refs = @{$dependency->depends_on};
154 0 push @old_refs, @depends_on_refs;
155 0 my @new_refs = uniq(@old_refs);
156  
157 0 $dependency->depends_on(\@new_refs);
158  
159 }
160  
161 12 ▶ if (not $exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exists) 12 0 12
162 12 $self->dependencies->push(
163 SBOM::CycloneDX::Dependency->new(ref => $target_ref, depends_on => \@depends_on_refs));
164 }
165  
166 # Add empty dependency entry if not exists "ref"
167 12 $self->add_dependency($_, []) for (@{$depends_on});
168  
169 }
170  
171 0 ▶sub get_component_by_purl {
Subroutine get_component_by_purl: not called CC=4 SCAR=30.0
Pod documented
172  
173 0 my ($self, $purl) = @_;
174  
175 0 foreach my $component (@{$self->components}) {
176 0 ▶ return $component if ($component->purl && $component->purl eq $purl);
Branch: 1 branch
BranchTrueFalseTotal
if $component->purl and $component->purl eq $purl 0 0 0
Condition0%0/3
$component->purl and $component->purl eq $purl
!ll&&!rl&&r
000
MC/DC0%0/2
$component->purl and $component->purl eq $purl
$component->purl $component->purl eq $purl
Truth table0%0/3
A and B
A= $component->purlB= $component->purl eq $purl
ABresult
0X0
100
111
177 }
178  
179 }
180  
181 0 ▶sub get_component_by_bom_ref {
Subroutine get_component_by_bom_ref: not called CC=4 SCAR=30.0
Pod documented
182  
183 0 my ($self, $bom_ref) = @_;
184  
185 0 foreach my $component (@{$self->components}) {
186 0 ▶ return $component if ($component->bom_ref && $component->bom_ref eq $bom_ref);
Branch: 1 branch
BranchTrueFalseTotal
if $component->bom_ref and $component->bom_ref eq $bom_ref 0 0 0
Condition0%0/3
$component->bom_ref and $component->bom_ref eq $bom_ref
!ll&&!rl&&r
000
MC/DC0%0/2
$component->bom_ref and $component->bom_ref eq $bom_ref
$component->bom_ref $component->bom_ref eq $bom_ref
Truth table0%0/3
A and B
A= $component->bom_refB= $component->bom_ref eq $bom_ref
ABresult
0X0
100
111
187 }
188  
189 }
190  
191 0 ▶sub get_vulnerabilities_for_bom_ref {
Subroutine get_vulnerabilities_for_bom_ref: not called CC=4 SCAR=30.0
Pod documented
192  
193 0 my ($self, $bom_ref) = @_;
194  
195 0 my $list = SBOM::CycloneDX::List->new;
196  
197 0 foreach my $vulnerability (@{$self->vulnerabilities}) {
198 0 foreach my $affect (@{$vulnerability->affects}) {
199 0 ▶ $list->add($vulnerability) if $affect->ref eq $bom_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $affect->ref eq $bom_ref 0 0 0
200 }
201 }
202  
203 0 return $list;
204  
205 }
206  
207 0 ▶sub get_affected_components_by_cve {
Subroutine get_affected_components_by_cve: not called CC=4 SCAR=30.0
Pod documented
208  
209 0 my ($self, $cve_id) = @_;
210  
211 0 my $list = SBOM::CycloneDX::List->new;
212  
213 0 foreach my $vulnerability (@{$self->vulnerabilities}) {
214 0 ▶ if ($vulnerability->id eq $cve_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vulnerability->id eq $cve_id) 0 0 0
215 0 foreach my $affect (@{$vulnerability->affects}) {
216 0 $list->add($self->get_component_by_bom_ref($affect->ref));
217 }
218 }
219 }
220  
221 0 return $list;
222  
223 }
224  
225 138 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=18 SCAR=30.0
Pod documented
226  
227 138 my $self = shift;
228  
229 138 my $spec_version = $self->spec_version;
230 138 my $schema = $JSON_SCHEMA{$spec_version};
231  
232 138 my $json = {bomFormat => $self->bom_format, specVersion => "$spec_version"};
233  
234 138 ▶ if ($spec_version > 1.5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec_version > "1.5") 58 80 138
235 58 $json->{'$schema'} = $schema;
236 }
237  
238 138 ▶ $json->{serialNumber} = $self->serial_number if $self->serial_number;
Branch: 1 branch
BranchTrueFalseTotal
if $self->serial_number 138 0 138
239 138 ▶ $json->{version} = $self->version if $self->version;
Branch: 1 branch
BranchTrueFalseTotal
if $self->version 138 0 138
240 138 ▶ $json->{metadata} = $self->metadata if %{$self->metadata->TO_JSON};
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->metadata->TO_JSON;} 52 86 138
241 138 ▶ $json->{components} = $self->components if @{$self->components};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->components;} 76 62 138
242 138 ▶ $json->{services} = $self->services if @{$self->services};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->services;} 0 138 138
243 138 ▶ $json->{externalReferences} = $self->external_references if @{$self->external_references};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->external_references;} 0 138 138
244 138 ▶ $json->{dependencies} = $self->dependencies if @{$self->dependencies};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->dependencies;} 18 120 138
245 138 ▶ $json->{compositions} = $self->compositions if @{$self->compositions};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->compositions;} 0 138 138
246 138 ▶ $json->{vulnerabilities} = $self->vulnerabilities if @{$self->vulnerabilities};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->vulnerabilities;} 0 138 138
247 138 ▶ $json->{annotations} = $self->annotations if @{$self->annotations};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->annotations;} 0 138 138
248 138 ▶ $json->{formulation} = $self->formulation if @{$self->formulation};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->formulation;} 0 138 138
249 138 ▶ $json->{declarations} = $self->declarations if %{$self->declarations->TO_JSON};
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->declarations->TO_JSON;} 0 138 138
250 138 ▶ $json->{definitions} = $self->definitions if %{$self->definitions->TO_JSON};
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->definitions->TO_JSON;} 0 138 138
251 138 ▶ $json->{citations} = $self->citations if @{$self->citations};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->citations;} 0 138 138
252 138 ▶ $json->{properties} = $self->properties if @{$self->properties};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->properties;} 0 138 138
253 138 ▶ $json->{signature} = $self->signature if %{$self->signature};
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->signature;} 0 138 138
254  
255 138 return $json;
256  
257 }
258  
259 16 1;
260  
261 __END__
« CycloneDX/License.pm ↑ Summary CycloneDX/Version.pm »