-
Notifications
You must be signed in to change notification settings - Fork 9
feat(CSAF2.1): #366 add recommended test 6.2.46 - License #378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rainer-exxcellent
wants to merge
3
commits into
main
Choose a base branch
from
feat/366-csaf-2.1_recommended_test_6.2.46
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,246 @@ | ||
import Ajv from 'ajv/dist/jtd.js' | ||
import { parse, validate } from 'license-expressions' | ||
import license_information from '../../lib/license/license_information.js' | ||
import translations from '../../lib/language_specific_translation/translations.js' | ||
import bcp47 from 'bcp47' | ||
|
||
const ajv = new Ajv() | ||
|
||
/* | ||
This is the jtd schema that needs to match the input document so that the | ||
test is activated. If this schema doesn't match it normally means that the input | ||
document does not validate against the csaf json schema or optional fields that | ||
the test checks are not present. | ||
*/ | ||
const inputSchema = /** @type {const} */ ({ | ||
additionalProperties: true, | ||
properties: { | ||
document: { | ||
additionalProperties: true, | ||
properties: { | ||
license_expression: { | ||
type: 'string', | ||
}, | ||
}, | ||
optionalProperties: { | ||
lang: { | ||
type: 'string', | ||
}, | ||
notes: { | ||
elements: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
category: { | ||
type: 'string', | ||
}, | ||
title: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
const validateSchema = ajv.compile(inputSchema) | ||
|
||
const ABOUT_CODE_LICENSE_REF_PREFIX = 'LicenseRef-scancode-' | ||
|
||
const ABOUT_CODE_LICENSE_KEYS = new Set( | ||
license_information.licenses | ||
.filter((license) => license.source === 'aboutCode') | ||
.map((license) => license.license_key) | ||
) | ||
|
||
const SPDX_LICENSE_KEYS = new Set( | ||
license_information.licenses | ||
.filter((license) => license.source === 'spdx') | ||
.map((license) => license.license_key) | ||
) | ||
|
||
/** | ||
* Check whether license identifiers are not listed Aboutcode's "ScanCode LicenseDB" | ||
* @param {string} licenseRefToCheck | ||
* @return {boolean} | ||
*/ | ||
function isAboutCodeLicense(licenseRefToCheck) { | ||
if (!licenseRefToCheck.startsWith(ABOUT_CODE_LICENSE_REF_PREFIX)) { | ||
return false | ||
} else { | ||
const licenseKey = licenseRefToCheck.substring( | ||
ABOUT_CODE_LICENSE_REF_PREFIX.length | ||
) | ||
return ABOUT_CODE_LICENSE_KEYS.has(licenseKey) | ||
} | ||
} | ||
|
||
/** | ||
* Recursively checks if a parsed license expression contains not listed licenses. | ||
* | ||
* @param {import('license-expressions').ParsedSpdxExpression} parsedExpression - The parsed license expression | ||
* @returns {Array<string>} all not listed licenses | ||
*/ | ||
function notListedLicenses(parsedExpression) { | ||
/** @type {Array<string>} */ | ||
const deprecatedLicenses = [] | ||
// If it's a LicenseRef type directly | ||
if ('licenseRef' in parsedExpression) { | ||
if (!isAboutCodeLicense(parsedExpression.licenseRef)) { | ||
deprecatedLicenses.push(parsedExpression.licenseRef) | ||
} | ||
} | ||
|
||
if ( | ||
'license' in parsedExpression && | ||
!SPDX_LICENSE_KEYS.has(parsedExpression.license) | ||
) { | ||
deprecatedLicenses.push(parsedExpression.license) | ||
} | ||
|
||
if ( | ||
'exception' in parsedExpression && | ||
parsedExpression.exception && | ||
!SPDX_LICENSE_KEYS.has(parsedExpression.exception) | ||
) { | ||
deprecatedLicenses.push(parsedExpression.exception) | ||
} | ||
|
||
// If it's a conjunction, check both sides | ||
if ('conjunction' in parsedExpression) { | ||
deprecatedLicenses.push(...notListedLicenses(parsedExpression.left)) | ||
deprecatedLicenses.push(...notListedLicenses(parsedExpression.right)) | ||
} | ||
|
||
// If it's a LicenseInfo type, it doesn't contain not listed licenses | ||
return deprecatedLicenses | ||
} | ||
|
||
/** | ||
* Checks if a license expression string contains any not listed licenses. | ||
* | ||
* @param {string} licenseToCheck - The license expression to check | ||
* @returns {Array<string>} all not listed licenses | ||
*/ | ||
function allNotListedLicenses(licenseToCheck) { | ||
const parseResult = parse(licenseToCheck) | ||
return notListedLicenses(parseResult) | ||
} | ||
|
||
/** | ||
* check if the license_expression contains license identifiers or exceptions | ||
* that are not listed in the SPDX license list or Aboutcode's "ScanCode LicenseDB" | ||
* | ||
* @param {string} licenseToCheck - The license expression to check | ||
* @returns {Array<string>} all not listed licenses | ||
*/ | ||
export function getNotListedLicenses(licenseToCheck) { | ||
if (!licenseToCheck || !validate(licenseToCheck).valid) { | ||
return [] | ||
} else { | ||
return allNotListedLicenses(licenseToCheck) | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the document language is specified and not English | ||
* | ||
* @param {string | undefined} language - The language expression to check | ||
* @returns {boolean} True if the language is valid, false otherwise | ||
*/ | ||
export function isLangSpecifiedAndNotEnglish(language) { | ||
return ( | ||
!!language && !(bcp47.parse(language)?.langtag.language.language === 'en') | ||
) | ||
} | ||
|
||
/** | ||
* test whether exactly one item in document notes exists that has the given title. | ||
* The category of this item MUST be legal_disclaimer. | ||
* @param {({} & { category?: string | undefined; title?: string | undefined; } & Record<string, unknown>)[]} notes | ||
* @param {string} titleToFind | ||
* @returns {boolean} True if the language is valid, false otherwise | ||
*/ | ||
function containsOneLegalDisclaimerWithTitle(notes, titleToFind) { | ||
return ( | ||
notes.filter( | ||
(note) => | ||
note.category === 'legal_disclaimer' && note.title === titleToFind | ||
).length === 1 | ||
) | ||
} | ||
Comment on lines
+165
to
+172
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check the implementation in mandatory tests => wrong category should be separate error. |
||
|
||
/** | ||
* Get the language specific translation of the term License | ||
* @param {{ document: { lang?: string; }; }} doc | ||
* @return {string | undefined} | ||
*/ | ||
export function getLicenseInDocumentLang(doc) { | ||
if (!doc.document.lang) { | ||
return undefined | ||
} | ||
const language = bcp47.parse(doc.document.lang)?.langtag.language.language | ||
|
||
/** @type {Record<string, Record <string,string>>}*/ | ||
const translationByLang = translations.translation | ||
if (!language || !translationByLang[language]) { | ||
return undefined | ||
} else { | ||
return translationByLang[language]['license'] | ||
} | ||
} | ||
|
||
/** | ||
* If the document language is specified but not English, and the license_expression contains license | ||
* identifiers or exceptions that are not listed in the SPDX license list or Aboutcode's "ScanCode LicenseDB", | ||
* it MUST be tested that exactly one item in document notes exists that has the language specific translation | ||
* of the term License as title. The category of this item MUST be legal_disclaimer. | ||
* If no language-specific translation has been recorded, the test MUST be skipped | ||
* and output information to the user that no such translation is known. | ||
* | ||
* @param {unknown} doc | ||
*/ | ||
export function recommendedTest_6_2_46(doc) { | ||
/* | ||
The `ctx` variable holds the state that is accumulated during the test run and is | ||
finally returned by the function. | ||
*/ | ||
const ctx = { | ||
warnings: | ||
/** @type {Array<{ instancePath: string; message: string }>} */ ([]), | ||
} | ||
|
||
if (!validateSchema(doc)) { | ||
return ctx | ||
} | ||
|
||
const licenseInDocLang = getLicenseInDocumentLang(doc) | ||
if (!licenseInDocLang) { | ||
return ctx | ||
} | ||
|
||
const licenseToCheck = doc.document.license_expression | ||
if (isLangSpecifiedAndNotEnglish(doc.document.lang)) { | ||
const notListedLicenses = getNotListedLicenses(licenseToCheck) | ||
if (notListedLicenses.length > 0) { | ||
const notes = doc.document.notes | ||
if ( | ||
!notes || | ||
!containsOneLegalDisclaimerWithTitle(notes, licenseInDocLang) | ||
) { | ||
ctx.warnings.push({ | ||
instancePath: '/document/notes', | ||
message: | ||
`The license_expression contains contains the following license identifiers that ` + | ||
`are nor listed in Aboutcode or SPDX license list: ` + | ||
`"${notListedLicenses.join(', ')}". ` + | ||
`Therefore exactly one note with ` + | ||
`title "License" and category "legal_disclaimer" must exist`, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
return ctx | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* javascript version of JSON file: csaf_2.1/language_specific_translation/translations.json | ||
*/ | ||
export default { | ||
$schema: | ||
'https://raw.githubusercontent.com/oasis-tcs/csaf/master/csaf_2.1/test/language_specific_translation/translations_json_schema.json', | ||
translation_version: '2.1', | ||
translation: { | ||
de: { | ||
license: 'Lizenz', | ||
product_description: 'Produktbeschreibung', | ||
reasoning_for_supersession: 'Begründung für die Ersetzung', | ||
reasoning_for_withdrawal: 'Begründung für die Zurückziehung', | ||
superseding_document: 'Ersetzendes Dokument', | ||
}, | ||
}, | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.