-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathSpdxResolvedDocumentTest.kt
More file actions
663 lines (546 loc) · 29.6 KB
/
SpdxResolvedDocumentTest.kt
File metadata and controls
663 lines (546 loc) · 29.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/*
* Copyright (C) 2021 The ORT Project Copyright Holders <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
package org.ossreviewtoolkit.plugins.packagemanagers.spdxdocumentfile.utils
import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.client.WireMock.aResponse
import com.github.tomakehurst.wiremock.client.WireMock.anyUrl
import com.github.tomakehurst.wiremock.client.WireMock.exactly
import com.github.tomakehurst.wiremock.client.WireMock.get
import com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor
import com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo
import com.github.tomakehurst.wiremock.core.WireMockConfiguration
import io.kotest.core.spec.Spec
import io.kotest.core.spec.style.WordSpec
import io.kotest.core.test.TestCase
import io.kotest.engine.spec.tempdir
import io.kotest.matchers.collections.beEmpty
import io.kotest.matchers.collections.containExactly
import io.kotest.matchers.collections.containExactlyInAnyOrder
import io.kotest.matchers.nulls.beNull
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.contain
import io.kotest.matchers.types.beTheSameInstanceAs
import java.io.File
import java.net.URI
import org.ossreviewtoolkit.model.Issue
import org.ossreviewtoolkit.model.Severity
import org.ossreviewtoolkit.plugins.packagemanagers.spdxdocumentfile.SpdxDocumentFileFactory
import org.ossreviewtoolkit.utils.common.calculateHash
import org.ossreviewtoolkit.utils.common.div
import org.ossreviewtoolkit.utils.spdxdocument.SpdxModelMapper
import org.ossreviewtoolkit.utils.spdxdocument.model.SpdxChecksum
import org.ossreviewtoolkit.utils.spdxdocument.model.SpdxDocument
import org.ossreviewtoolkit.utils.spdxdocument.model.SpdxExternalDocumentReference
import org.ossreviewtoolkit.utils.spdxdocument.model.SpdxPackage
import org.ossreviewtoolkit.utils.spdxdocument.model.SpdxRelationship
class SpdxResolvedDocumentTest : WordSpec() {
private lateinit var tempDir: File
/** A mock server used for testing whether external references can reference documents via the Internet. */
private lateinit var server: WireMockServer
override suspend fun beforeSpec(spec: Spec) {
server = WireMockServer(
WireMockConfiguration.options()
.dynamicPort()
)
server.start()
}
override suspend fun beforeTest(testCase: TestCase) {
tempDir = tempdir()
server.resetAll()
}
override suspend fun afterSpec(spec: Spec) {
server.stop()
}
init {
"load" should {
"use the given loader to load a root document" {
val loader = SpdxDocumentCache()
val root = loader.load(BASE_DOCUMENT_FILE).getOrThrow()
val resolvedDoc = SpdxResolvedDocument.load(loader, BASE_DOCUMENT_FILE)
resolvedDoc.rootDocument.document should beTheSameInstanceAs(root)
resolvedDoc.rootDocument.url shouldBe BASE_DOCUMENT_FILE.toURI()
}
"load a root document without external references" {
val rootFile = createSpdxDocument(1, listOf(createPackage(1)))
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), rootFile)
resolvedDoc.referencedDocuments.keys should beEmpty()
}
"load the documents referenced from the root document" {
val extPackage10 = createPackage(10)
val extPackage11 = createPackage(11)
val extPackage20 = createPackage(12)
val referencedDoc1 = createSpdxDocument(2, listOf(extPackage10, extPackage11))
val referencedDoc2 = createSpdxDocument(3, listOf(extPackage20))
val ref1 = referencedDoc1.toExternalReference(1)
val ref2 = referencedDoc2.toExternalReference(2)
val rootFile =
createSpdxDocument(1, packages = listOf(createPackage(1)), references = listOf(ref1, ref2))
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), rootFile)
resolvedDoc.referencedDocuments.keys should containExactlyInAnyOrder(ref1, ref2)
resolvedDoc.referencedDocuments[ref1] shouldNotBeNull {
document.packages should containExactly(extPackage10, extPackage11)
}
}
"load referenced documents transitively" {
val extPackage = createPackage(30)
val referencedDocTrans = createSpdxDocument(4, listOf(extPackage))
val refTrans = referencedDocTrans.toExternalReference(3)
val referencedDoc1 = createSpdxDocument(2, listOf(createPackage(10)))
val referencedDoc2 = createSpdxDocument(3, listOf(createPackage(20)), references = listOf(refTrans))
val ref1 = referencedDoc1.toExternalReference(1)
val ref2 = referencedDoc2.toExternalReference(2)
val rootFile =
createSpdxDocument(1, packages = listOf(createPackage(1)), references = listOf(ref1, ref2))
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), rootFile)
resolvedDoc.referencedDocuments.keys should containExactlyInAnyOrder(ref1, ref2, refTrans)
resolvedDoc.referencedDocuments[refTrans] shouldNotBeNull {
document.packages should containExactly(extPackage)
}
}
"handle cyclic references" {
val referencedDoc3 = createSpdxDocument(3, listOf(createPackage(30)))
val ref3 = referencedDoc3.toExternalReference(3)
val referencedDoc2 = createSpdxDocument(2, listOf(createPackage(20)), references = listOf(ref3))
val ref2 = referencedDoc2.toExternalReference(2)
val referencedDoc1 = createSpdxDocument(1, listOf(createPackage(10)), references = listOf(ref2))
val ref1 = referencedDoc1.toExternalReference(1)
// Overwrite 3rd document with a cyclic reference
createSpdxDocument(3, listOf(createPackage(30)), references = listOf(ref1))
val rootFile = createSpdxDocument(0, listOf(createPackage(1)), references = listOf(ref1))
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), rootFile)
resolvedDoc.referencedDocuments.keys should containExactlyInAnyOrder(ref1, ref2, ref3)
}
}
"SpdxExternalDocumentReference.resolve" should {
"maintain a query string" {
val ref = SpdxExternalDocumentReference("DocumentRef-ort", "../lib/package.spdx.yml", DUMMY_CHECKSUM)
val baseUri = URI("http://localhost:${server.port()}/documents/spdx/app/package.spdx.yml?branch=main")
ref.resolve(SpdxDocumentCache(), baseUri)
server.verify(
exactly(1),
getRequestedFor(WireMock.urlEqualTo("/documents/spdx/lib/package.spdx.yml?branch=main"))
)
}
}
"getSpdxPackageForId" should {
"return a package from the root document" {
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), BASE_DOCUMENT_FILE)
val issues = mutableListOf<Issue>()
resolvedDoc.getSpdxPackageForId("SPDXRef-Package-xyz", issues) shouldNotBeNull {
name shouldBe "xyz"
versionInfo shouldBe "0.1.0"
}
issues should beEmpty()
}
"return a package declared in an external reference" {
val externalPackage = createPackage(1)
val refDocument = createSpdxDocument(2, listOf(externalPackage, createPackage(2)))
val otherRefDocument = createSpdxDocument(3, listOf(createPackage(3)))
val ref = refDocument.toExternalReference(1)
val otherRef = otherRefDocument.toExternalReference(2)
val rootFile = createSpdxDocument(1, listOf(createPackage(10)), references = listOf(otherRef, ref))
val pkgId = "${ref.externalDocumentId}:${externalPackage.spdxId}"
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), rootFile)
val issues = mutableListOf<Issue>()
resolvedDoc.getSpdxPackageForId(pkgId, issues) shouldBe externalPackage
issues should beEmpty()
}
"create an issue for a package, which cannot be resolved" {
val identifier = "unknownPackageId"
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), BASE_DOCUMENT_FILE)
val issues = mutableListOf<Issue>()
resolvedDoc.getSpdxPackageForId(identifier, issues) should beNull()
with(issues.single()) {
severity shouldBe Severity.ERROR
source shouldBe SpdxDocumentFileFactory.descriptor.displayName
message should contain(identifier)
}
}
"create an issue for an external reference with an invalid URI" {
val reference = SpdxExternalDocumentReference("DocumentRef-ort", "?Not a valid URI?!!", DUMMY_CHECKSUM)
val identifier = "${reference.externalDocumentId}:somePackage"
val doc = createSpdxDocument(1, listOf(createPackage(1)), references = listOf(reference))
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), doc)
val issues = mutableListOf<Issue>()
resolvedDoc.getSpdxPackageForId(identifier, issues) should beNull()
with(issues.single()) {
severity shouldBe Severity.ERROR
source shouldBe SpdxDocumentFileFactory.descriptor.displayName
message should contain(reference.externalDocumentId)
message should contain(reference.spdxDocument)
}
}
"create an issue for an external reference pointing to a non-existing file" {
val reference = SpdxExternalDocumentReference("DocumentRef-ort", "absent.spdx.yml", DUMMY_CHECKSUM)
val identifier = "${reference.externalDocumentId}:somePackage"
val doc = createSpdxDocument(1, listOf(createPackage(1)), references = listOf(reference))
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), doc)
val issues = mutableListOf<Issue>()
resolvedDoc.getSpdxPackageForId(identifier, issues) should beNull()
with(issues.single()) {
severity shouldBe Severity.ERROR
source shouldBe SpdxDocumentFileFactory.descriptor.displayName
message should contain(reference.externalDocumentId)
message should contain(reference.spdxDocument)
message should contain("does not exist")
}
}
"create an issue for an external reference with a wrong checksum" {
val externalPackage = createPackage(42)
val refDocument = createSpdxDocument(2, listOf(externalPackage))
val ref = refDocument.toExternalReference(1).copy(checksum = DUMMY_CHECKSUM)
val identifier = "${ref.externalDocumentId}:${externalPackage.spdxId}"
val rootDoc = createSpdxDocument(1, listOf(createPackage(1)), references = listOf(ref))
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), rootDoc)
val issues = mutableListOf<Issue>()
resolvedDoc.getSpdxPackageForId(identifier, issues) shouldBe externalPackage
with(issues.single()) {
severity shouldBe Severity.WARNING
source shouldBe SpdxDocumentFileFactory.descriptor.displayName
message should contain(ref.externalDocumentId)
message should contain(ref.spdxDocument)
message should contain(DUMMY_CHECKSUM.checksumValue)
}
}
"return a package from an external document downloaded from a server" {
val externalPackage = createPackage(111)
val refDocument = createSpdxDocument(2, listOf(externalPackage))
val documentPath = "/documents/spdx/external_package.spdx.yml"
val ref = refDocument.toExternalReference(1)
.copy(spdxDocument = "http://localhost:${server.port()}$documentPath")
val identifier = "${ref.externalDocumentId}:${externalPackage.spdxId}"
val rootDoc = createSpdxDocument(1, listOf(createPackage(1)), references = listOf(ref))
server.stubFor(
get(urlPathEqualTo(documentPath))
.willReturn(
aResponse().withStatus(200)
.withBody(refDocument.readText())
)
)
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), rootDoc)
val issues = mutableListOf<Issue>()
resolvedDoc.getSpdxPackageForId(identifier, issues) shouldBe externalPackage
issues should beEmpty()
}
"verify the checksum when downloading an external document" {
val externalPackage = createPackage(222)
val refDocument = createSpdxDocument(2, listOf(externalPackage))
val documentPath = "/documents/spdx/package_with_wrong_checksum.spdx.yml"
val ref = refDocument.toExternalReference(1)
.copy(spdxDocument = "http://localhost:${server.port()}$documentPath", checksum = DUMMY_CHECKSUM)
val identifier = "${ref.externalDocumentId}:${externalPackage.spdxId}"
val rootDoc = createSpdxDocument(1, listOf(createPackage(1)), references = listOf(ref))
server.stubFor(
get(urlPathEqualTo(documentPath))
.willReturn(
aResponse().withStatus(200)
.withBody(refDocument.readText())
)
)
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), rootDoc)
val issues = mutableListOf<Issue>()
resolvedDoc.getSpdxPackageForId(identifier, issues) shouldBe externalPackage
with(issues.single()) {
severity shouldBe Severity.WARNING
source shouldBe SpdxDocumentFileFactory.descriptor.displayName
message should contain(ref.externalDocumentId)
message should contain(ref.spdxDocument)
message should contain(DUMMY_CHECKSUM.checksumValue)
}
}
"handle a failure when downloading an external document" {
val errorRef = SpdxExternalDocumentReference(
"DocumentRef-ort",
"http://localhost:${server.port()}/doc.spdx.json",
DUMMY_CHECKSUM
)
val identifier = "${errorRef.externalDocumentId}:somePackage"
val rootDoc = createSpdxDocument(1, listOf(createPackage(1)), references = listOf(errorRef))
server.stubFor(
get(anyUrl())
.willReturn(aResponse().withStatus(400))
)
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), rootDoc)
val issues = mutableListOf<Issue>()
resolvedDoc.getSpdxPackageForId(identifier, issues) should beNull()
with(issues.single()) {
severity shouldBe Severity.ERROR
source shouldBe SpdxDocumentFileFactory.descriptor.displayName
message should contain(errorRef.externalDocumentId)
message should contain(errorRef.spdxDocument)
message should contain("download")
}
}
}
"relationships" should {
"contain the relations from the root document if there are no external references" {
val relations = listOf(
SpdxRelationship(packageId(1), SpdxRelationship.Type.DEPENDS_ON, packageId(2)),
SpdxRelationship(packageId(1), SpdxRelationship.Type.DEPENDS_ON, packageId(3)),
SpdxRelationship(packageId(4), SpdxRelationship.Type.DEPENDENCY_OF, packageId(3))
)
val packages = (1..5).map(::createPackage)
val rootDoc = createSpdxDocument(1, packages, relations = relations)
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), rootDoc)
resolvedDoc.relationships should containExactly(relations)
}
"contain the aggregated and qualified relations from all referenced documents" {
val pkg30 = createPackage(30)
val pkg31 = createPackage(31)
val relation31 = SpdxRelationship(pkg30.spdxId, SpdxRelationship.Type.DEPENDS_ON, pkg31.spdxId)
val doc3 = createSpdxDocument(3, listOf(pkg31, pkg30), relations = listOf(relation31))
val ref30 = doc3.toExternalReference(30)
val pkg20 = createPackage(20)
val pkg21 = createPackage(21)
val relation21 = SpdxRelationship(pkg21.spdxId, SpdxRelationship.Type.DEPENDENCY_OF, pkg20.spdxId)
val relation22 = SpdxRelationship(
pkg21.spdxId,
SpdxRelationship.Type.DEPENDS_ON,
packageId(31, referenceIndex = 30)
)
val doc2 = createSpdxDocument(
2,
listOf(pkg20, pkg21),
relations = listOf(relation21, relation22),
references = listOf(ref30)
)
val ref20 = doc2.toExternalReference(20)
val pkg1 = createPackage(1)
val pkg2 = createPackage(2)
val relation1 = SpdxRelationship(
pkg1.spdxId,
SpdxRelationship.Type.COPY_OF,
packageId(20, referenceIndex = 20)
)
val relation2 = SpdxRelationship(
pkg1.spdxId,
SpdxRelationship.Type.DEPENDS_ON,
packageId(21, referenceIndex = 20)
)
val relation3 = SpdxRelationship(pkg2.spdxId, SpdxRelationship.Type.DATA_FILE_OF, pkg1.spdxId)
val doc1 = createSpdxDocument(
1,
listOf(pkg1, pkg2),
relations = listOf(relation1, relation2, relation3),
references = listOf(ref20)
)
val expectedRelations = listOf(
relation1,
relation2,
relation3,
SpdxRelationship(
packageId(21, referenceIndex = 20),
relation21.relationshipType,
packageId(20, referenceIndex = 20)
),
relation22.copy(spdxElementId = packageId(21, referenceIndex = 20)),
SpdxRelationship(
packageId(30, referenceIndex = 30),
relation31.relationshipType,
packageId(31, referenceIndex = 30)
)
)
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), doc1)
resolvedDoc.relationships should containExactlyInAnyOrder(expectedRelations)
}
"contain only resolvable packages" {
val pkg30 = createPackage(30)
val pkg31 = createPackage(31)
val relation31 = SpdxRelationship(pkg30.spdxId, SpdxRelationship.Type.DEPENDS_ON, pkg31.spdxId)
val doc3 = createSpdxDocument(3, listOf(pkg31, pkg30), relations = listOf(relation31))
val ref30 = doc3.toExternalReference(30)
val pkg20 = createPackage(20)
val pkg21 = createPackage(21)
val relation21 = SpdxRelationship(pkg21.spdxId, SpdxRelationship.Type.DEPENDENCY_OF, pkg20.spdxId)
val relation22 = SpdxRelationship(
pkg21.spdxId,
SpdxRelationship.Type.DEPENDS_ON,
packageId(31, referenceIndex = 30)
)
val doc2 = createSpdxDocument(
2,
listOf(pkg20, pkg21),
relations = listOf(relation21, relation22),
references = listOf(ref30)
)
val ref20 = doc2.toExternalReference(20)
val pkg1 = createPackage(1)
val pkg2 = createPackage(2)
val relation1 = SpdxRelationship(
pkg1.spdxId,
SpdxRelationship.Type.COPY_OF,
packageId(20, referenceIndex = 20)
)
val relation2 = SpdxRelationship(
pkg1.spdxId,
SpdxRelationship.Type.DEPENDS_ON,
packageId(21, referenceIndex = 20)
)
val relation3 = SpdxRelationship(pkg2.spdxId, SpdxRelationship.Type.DATA_FILE_OF, pkg1.spdxId)
val doc1 = createSpdxDocument(
1,
listOf(pkg1, pkg2),
relations = listOf(relation1, relation2, relation3),
references = listOf(ref20)
)
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), doc1)
val issues = mutableListOf<Issue>()
resolvedDoc.relationships.forEach { relation ->
resolvedDoc.getSpdxPackageForId(relation.spdxElementId, issues) shouldNotBeNull {
spdxId shouldBe relation.spdxElementId.substringAfter(':')
}
resolvedDoc.getSpdxPackageForId(relation.relatedSpdxElement, issues) shouldNotBeNull {
spdxId shouldBe relation.relatedSpdxElement.substringAfter(':')
}
}
issues should beEmpty()
}
}
"getDefinitionFile" should {
"return the root document for a package declared there" {
val pkg = createPackage(1)
val doc = createSpdxDocument(1, listOf(pkg, createPackage(2)))
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), doc)
resolvedDoc.getDefinitionFile(pkg.spdxId) shouldBe doc
}
"return the definition file for a package declared in a referenced document" {
val pkg = createPackage(11)
val refDoc = createSpdxDocument(10, listOf(createPackage(10), pkg))
val ref = refDoc.toExternalReference(1)
val rootDoc = createSpdxDocument(1, listOf(createPackage(1)), references = listOf(ref))
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), rootDoc)
resolvedDoc.getDefinitionFile("${ref.externalDocumentId}:${pkg.spdxId}") shouldBe refDoc
}
"return null if the identifier refers to a non-existing reference" {
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), BASE_DOCUMENT_FILE)
resolvedDoc.getDefinitionFile("nonExistingReference:somePackage") should beNull()
}
"return null if the identifier refers to a non-existing package in the root document" {
val resolvedDoc = SpdxResolvedDocument.load(SpdxDocumentCache(), BASE_DOCUMENT_FILE)
resolvedDoc.getDefinitionFile("nonExistingPackage") should beNull()
}
}
"ResolvedSpdxDocument" should {
"return null for the definition file if it was resolved via an URI" {
val resolvedDocument = ResolvedSpdxDocument(
SpdxDocumentCache().load(BASE_DOCUMENT_FILE).getOrThrow(),
URI("https://www.example.org/spdx/package.spdx.yml")
)
resolvedDocument.definitionFile() should beNull()
}
"return the definition file if available" {
val resolvedDocument = ResolvedSpdxDocument(
SpdxDocumentCache().load(BASE_DOCUMENT_FILE).getOrThrow(),
BASE_DOCUMENT_FILE.toURI()
)
resolvedDocument.definitionFile() shouldBe BASE_DOCUMENT_FILE.absoluteFile
}
"return the definition file for a relative URI" {
val resolvedDocument = ResolvedSpdxDocument(
SpdxDocumentCache().load(BASE_DOCUMENT_FILE).getOrThrow(),
URI("src/funTest/assets/projects/synthetic/package-references/project-xyz.spdx.yml")
)
resolvedDocument.definitionFile() shouldBe BASE_DOCUMENT_FILE.absoluteFile
}
"return a normalized definition file" {
val resolvedDocument = ResolvedSpdxDocument(
SpdxDocumentCache().load(BASE_DOCUMENT_FILE).getOrThrow(),
URI(
"src/funTest/assets/projects/synthetic/package-references/project-xyz.spdx.yml"
)
)
resolvedDocument.definitionFile() shouldBe BASE_DOCUMENT_FILE.absoluteFile
}
"return null if the file does not exist" {
val nonExistingFile = File("non/existing/file.spdx.yml")
val resolvedDocument = ResolvedSpdxDocument(
SpdxDocumentCache().load(BASE_DOCUMENT_FILE).getOrThrow(),
nonExistingFile.toURI()
)
resolvedDocument.definitionFile() should beNull()
}
}
}
/**
* Create an [SpdxDocument] in [tempDir] with identifiers derived from [index] that contains the given [packages],
* [relations], and [references].
*/
private fun createSpdxDocument(
index: Int,
packages: List<SpdxPackage>,
relations: List<SpdxRelationship> = emptyList(),
references: List<SpdxExternalDocumentReference> = emptyList()
): File {
val document = spdxBaseDocument.copy(
name = "testDocument$index",
packages = packages,
relationships = relations,
externalDocumentRefs = references
)
val file = tempDir / "external$index.spdx.yml"
SpdxModelMapper.write(file, document)
return file
}
}
/** Prefix for SPDX identifiers generated by tests. */
private const val SPDX_REF = "SPDXRef-Package-Dummy"
/** References the SPDX file used to define the base document. */
private val BASE_DOCUMENT_FILE =
File("src/funTest/assets/projects/synthetic/package-references/project-xyz.spdx.yml")
/** Constant for a checksum to be used in case no correct hash value is needed or available. */
private val DUMMY_CHECKSUM = SpdxChecksum(SpdxChecksum.Algorithm.SHA1, "0123456789012345678901234567890123456789")
/** Holds an SPDX document that is used as basis when creating documents for tests. */
private val spdxBaseDocument: SpdxDocument = SpdxModelMapper.read(BASE_DOCUMENT_FILE)
/**
* Create a synthetic test package based in the given [index].
*/
private fun createPackage(index: Int): SpdxPackage =
SpdxPackage(
spdxId = packageId(index),
name = "testPackage$index",
copyrightText = "copyright$index",
downloadLocation = "/downloads/file$index",
licenseConcluded = "ASL-2",
licenseDeclared = "ASL-2",
packageFilename = "package$index"
)
/**
* Generate the ID of a test package based on the given [index]. If a [referenceIndex] is provided, generate an ID
* that is qualified with the ID of this reference.
*/
private fun packageId(index: Int, referenceIndex: Int? = null): String =
referenceIndex?.let { "${referenceId(it)}:" }.orEmpty() + "$SPDX_REF-$index"
/**
* Generate the ID of a test document reference based on the given [index].
*/
private fun referenceId(index: Int): String = "DocumentRef-ref$index"
/**
* Create an [SpdxExternalDocumentReference] that points to this file with an ID derived from [index].
*/
private fun File.toExternalReference(index: Int): SpdxExternalDocumentReference {
val hash = calculateHash(this)
return SpdxExternalDocumentReference(
externalDocumentId = referenceId(index),
spdxDocument = name,
checksum = SpdxChecksum(SpdxChecksum.Algorithm.SHA1, hash.toHexString())
)
}