Skip to content

Commit 21f077a

Browse files
feat(CSAF2.1): add informative test 6.3.17
1 parent a7e7792 commit 21f077a

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

csaf_2_1/informativeTests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ export {
1111
} from '../informativeTests.js'
1212
export { informativeTest_6_3_1 } from './informativeTests/informativeTest_6_3_1.js'
1313
export { informativeTest_6_3_4 } from './informativeTests/informativeTest_6_3_4.js'
14+
export { informativeTest_6_3_17 } from './informativeTests/informativeTest_6_3_17.js'
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import Ajv from 'ajv/dist/jtd.js'
2+
3+
const ABOUTCODE_LICENSE_DB =
4+
'https://scancode-licensedb.aboutcode.org/index.json'
5+
const SPDX_LICENSE_DB =
6+
'https://raw.githubusercontent.com/composer/spdx-licenses/refs/heads/main/res/spdx-licenses.json'
7+
8+
const ajv = new Ajv()
9+
10+
const inputSchema = /** @type {const} */ ({
11+
additionalProperties: true,
12+
properties: {
13+
document: {
14+
additionalProperties: true,
15+
properties: {
16+
license_expression: {
17+
type: 'string',
18+
},
19+
},
20+
},
21+
},
22+
})
23+
24+
const validateInput = ajv.compile(inputSchema)
25+
26+
/**
27+
* Read JSON from given URL
28+
* @param {string | URL | Request} dataUrl
29+
* @returns
30+
*/
31+
async function readJson(dataUrl) {
32+
/** @type {any} */
33+
const headers = { Accept: 'application/vnd.github.v3+json' }
34+
const response = await fetch(dataUrl, { headers })
35+
if (!response.ok) {
36+
throw new Error(`Response status: ${response.status}`)
37+
}
38+
39+
return await response.json()
40+
}
41+
42+
/**
43+
* Read considered licenses from SPDX and About Code
44+
* @returns {Promise<Set<string>>}
45+
*/
46+
async function readConsideredLicenses() {
47+
/** @type {Array<{ license_key: string; spdx_license_key: string }>} */
48+
const aboutcodeLicenses = await readJson(ABOUTCODE_LICENSE_DB)
49+
/** @type {Record<string, []>} */
50+
const spdxLicenses = await readJson(SPDX_LICENSE_DB)
51+
52+
const consideredLicenses = new Set(
53+
aboutcodeLicenses.map((aboutCode) => aboutCode.license_key)
54+
)
55+
Object.keys(spdxLicenses).forEach((item) => consideredLicenses.add(item))
56+
return consideredLicenses
57+
}
58+
59+
/**
60+
* It MUST be tested that the all license identifiers and exceptions are listed either
61+
* in the official SPDX license identifier list or AboutCode's "ScanCode LicenseDB".
62+
* @param {unknown} doc
63+
* @returns
64+
*/
65+
export async function informativeTest_6_3_17(doc) {
66+
const ctx = {
67+
infos: /** @type {Array<{ message: string; instancePath: string }>} */ ([]),
68+
}
69+
70+
if (!validateInput(doc)) {
71+
return ctx
72+
}
73+
74+
const consideredLicenses = await readConsideredLicenses()
75+
76+
const licenseToCheck = doc.document.license_expression
77+
if (!consideredLicenses.has(licenseToCheck)) {
78+
ctx.infos.push({
79+
instancePath: '/document/license_expression',
80+
message: `Invalid license: '${licenseToCheck}'`,
81+
})
82+
}
83+
84+
return ctx
85+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import assert from 'node:assert'
2+
import { informativeTest_6_3_17 } from '../../csaf_2_1/informativeTests.js'
3+
4+
describe('informativeTest_6_3_17', function () {
5+
it('only runs on relevant documents', function () {
6+
informativeTest_6_3_17({ document: 'mydoc' }).then((result) =>
7+
assert.equal(result.infos.length, 0)
8+
)
9+
})
10+
})

tests/csaf_2_1/oasis.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ const excluded = [
8181
'6.3.12',
8282
'6.3.13',
8383
'6.3.16',
84-
'6.3.17',
8584
]
8685

8786
/** @typedef {import('../../lib/shared/types.js').DocumentTest} DocumentTest */

0 commit comments

Comments
 (0)