Skip to content

Commit d490f82

Browse files
feat(CSAF2.1): #287 add mandatory test 6.1.42 - Part of the PURL that differs is appended to the message.
1 parent b464d73 commit d490f82

File tree

3 files changed

+59
-30
lines changed

3 files changed

+59
-30
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,6 @@ The following tests are not yet implemented and therefore missing:
314314
- Mandatory Test 6.1.26
315315
- Mandatory Test 6.1.27.13
316316
- Mandatory Test 6.1.27.18
317-
- Mandatory Test 6.1.27.19
318317
- Mandatory Test 6.1.44
319318
- Mandatory Test 6.1.46
320319
- Mandatory Test 6.1.47

csaf_2_1/mandatoryTests/mandatoryTest_6_1_42.js

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -81,36 +81,47 @@ const validate = ajv.compile(inputSchema)
8181
/**
8282
*
8383
* @param {PackageURL | null} firstPurl
84-
* @param {PackageURL} secondPurl
85-
* @return {boolean}
84+
* @param {PackageURL | null} secondPurl
85+
* @return {Array<string>} the parts of the PURLS that differ
8686
*/
87-
function onlyDifferInQualifiers(firstPurl, secondPurl) {
88-
return (
89-
!!firstPurl &&
90-
firstPurl.type === secondPurl.type &&
91-
firstPurl.namespace === secondPurl.namespace &&
92-
firstPurl.name === secondPurl.name &&
93-
firstPurl.version === secondPurl.version
94-
)
87+
function purlPartsThatDifferExceptQualifiers(firstPurl, secondPurl) {
88+
/** @type {Array<string>}*/
89+
const partsThatDiffer = []
90+
91+
if (firstPurl && secondPurl) {
92+
if (firstPurl.type !== secondPurl.type) {
93+
partsThatDiffer.push('type')
94+
}
95+
if (firstPurl.namespace !== secondPurl.namespace) {
96+
partsThatDiffer.push('namespace')
97+
}
98+
if (firstPurl.name !== secondPurl.name) {
99+
partsThatDiffer.push('name')
100+
}
101+
if (firstPurl.version !== secondPurl.version) {
102+
partsThatDiffer.push('version')
103+
}
104+
}
105+
return partsThatDiffer
95106
}
96107

97108
/**
98109
* Validates all given PURLs and check whether the PURLs
99110
* differ only in qualifiers to the first URL
100111
*
101112
* @param {Array<string> | undefined} purls PURLs to check
102-
* @return {Array<number>} indexes of the PURLs that differ
113+
* @return {Array<{index:number, purlParts: Array<string> }>} indexes and parts of the PURLs that differ
103114
*/
104115
export function checkPurls(purls) {
105-
/** @type {Array<number>}*/
116+
/** @type {Array<{index:number, purlParts: Array<string> }>} */
106117
const invalidPurls = []
107118
if (purls) {
108119
/** @type {Array<PackageURL | null>} */
109120
const packageUrls = purls.map((purl) => {
110121
try {
111122
return PackageURL.fromString(purl)
112123
} catch (e) {
113-
// ignore
124+
// ignore, tested in CSAF 2.1 test 6.1.13
114125
return null
115126
}
116127
})
@@ -121,9 +132,13 @@ export function checkPurls(purls) {
121132
if (packageUrls.length > 1) {
122133
const firstPurl = packageUrls[0]
123134
for (let i = 1; i < packageUrls.length; i++) {
124-
const packageUrl = packageUrls[i]
125-
if (!packageUrl || !onlyDifferInQualifiers(firstPurl, packageUrl)) {
126-
invalidPurls.push(i)
135+
/** @type {Array<string>}*/
136+
const purlParts = purlPartsThatDifferExceptQualifiers(
137+
firstPurl,
138+
packageUrls[i]
139+
)
140+
if (purlParts.length > 0) {
141+
invalidPurls.push({ index: i, purlParts: purlParts })
127142
}
128143
}
129144
}
@@ -183,15 +198,14 @@ export function mandatoryTest_6_1_42(doc) {
183198
* @param {FullProductName} fullProductName The "full product name" object.
184199
*/
185200
function checkFullProductName(prefix, fullProductName) {
186-
const invalidPurlsIndexes = checkPurls(
201+
const invalidPurls = checkPurls(
187202
fullProductName.product_identification_helper?.purls
188203
)
189-
invalidPurlsIndexes.forEach((invalidPurlIndex) => {
204+
invalidPurls.forEach((invalidPurl) => {
190205
ctx.isValid = false
191206
ctx.errors.push({
192-
instancePath: `${prefix}/product_identification_helper/purls/${invalidPurlIndex}`,
193-
message:
194-
'the PURL differs from the first PURL in other parts than just the qualifiers',
207+
instancePath: `${prefix}/product_identification_helper/purls/${invalidPurl.index}`,
208+
message: `the PURL differs from the first PURL in the following part(s): ${invalidPurl.purlParts.join()}`,
195209
})
196210
})
197211
}
@@ -205,15 +219,14 @@ export function mandatoryTest_6_1_42(doc) {
205219
* @param {Branch} branch The "branch" object.
206220
*/
207221
function checkBranch(prefix, branch) {
208-
const invalidPurlsIndexes = checkPurls(
222+
const invalidPurls = checkPurls(
209223
branch.product?.product_identification_helper?.purls
210224
)
211-
invalidPurlsIndexes.forEach((invalidPurlIndex) => {
225+
invalidPurls.forEach((invalidPurl) => {
212226
ctx.isValid = false
213227
ctx.errors.push({
214-
instancePath: `${prefix}/product/product_identification_helper/purls/${invalidPurlIndex}`,
215-
message:
216-
'the PURL differs from the first PURL in other parts than just the qualifiers',
228+
instancePath: `${prefix}/product/product_identification_helper/purls/${invalidPurl.index}`,
229+
message: `the PURL differs from the first PURL in the following parts: ${invalidPurl.purlParts.join()}`,
217230
})
218231
})
219232
branch.branches?.forEach((branch, index) => {

tests/csaf_2_1/mandatoryTest_6_1_42.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,32 @@ describe('mandatoryTest_6_1_42', function () {
3434
'pkg:golang/google.golang.com/genproto#googleapis/api/annotations',
3535
]),
3636
'change in namespace'
37-
).to.eql([1])
37+
).to.eql([{ index: 1, purlParts: ['namespace'] }])
38+
expect(
39+
checkPurls([
40+
'pkg:golang/google.golang.org/genproto#googleapis/api/annotations',
41+
'pkg:npm/google.golang.org/genproto#googleapis/api/annotations',
42+
]),
43+
'change in type'
44+
).to.eql([{ index: 1, purlParts: ['type'] }])
45+
expect(
46+
checkPurls([
47+
'pkg:golang/google.golang.org/genproto#googleapis/api/annotations',
48+
'pkg:golang/google.golang.org/genproto2#googleapis/api/annotations',
49+
]),
50+
'change in name'
51+
).to.eql([{ index: 1, purlParts: ['name'] }])
3852
expect(
3953
checkPurls([
4054
'pkg:npm/%40angular/[email protected]',
4155
'invalid',
4256
'pkg:npm/%40angular/[email protected]',
43-
'pkg:npm/%40angular/[email protected]',
57+
'pkg:golang/%40angular/[email protected]',
4458
]),
4559
'change in version and invalid PURL'
46-
).to.eql([1, 2, 3])
60+
).to.eql([
61+
{ index: 2, purlParts: ['version'] },
62+
{ index: 3, purlParts: ['type', 'version'] },
63+
])
4764
})
4865
})

0 commit comments

Comments
 (0)