-
Notifications
You must be signed in to change notification settings - Fork 9
feat(CSAF2.1): #373 add mandatory test 6.1.44 #411
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
4
commits into
main
Choose a base branch
from
feat/373-csaf-2.1-mandatory-test-6.1.44
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
4 commits
Select commit
Hold shift + click to select a range
0564faf
feat(CSAF2.1): #373 add mandatory test 6.1.44
rainer-exxcellent 0401af8
feat(CSAF2.1): #373 add mandatory test 6.1.44 -improve comment
rainer-exxcellent e781acf
feat(CSAF2.1): #373 add mandatory test 6.1.44 - add comment to contai…
rainer-exxcellent 3572d35
feat(CSAF2.1): #373 add mandatory test 6.1.44 - changed string quotation
rainer-exxcellent 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,206 @@ | ||
import Ajv from 'ajv/dist/jtd.js' | ||
|
||
const ajv = new Ajv() | ||
|
||
const branchSchema = /** @type {const} */ ({ | ||
additionalProperties: true, | ||
optionalProperties: { | ||
branches: { | ||
elements: { | ||
additionalProperties: true, | ||
properties: {}, | ||
}, | ||
}, | ||
product: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
product_identification_helper: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
serial_numbers: { elements: { type: 'string' } }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
const validateBranch = ajv.compile(branchSchema) | ||
|
||
const fullProductNameSchema = /** @type {const} */ ({ | ||
additionalProperties: true, | ||
optionalProperties: { | ||
product_identification_helper: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
serial_numbers: { elements: { type: 'string' } }, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
/* | ||
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, | ||
optionalProperties: { | ||
product_tree: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
branches: { | ||
elements: branchSchema, | ||
}, | ||
full_product_names: { | ||
elements: fullProductNameSchema, | ||
}, | ||
relationships: { | ||
elements: { | ||
additionalProperties: true, | ||
optionalProperties: { | ||
full_product_name: fullProductNameSchema, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
const validate = ajv.compile(inputSchema) | ||
|
||
/** | ||
* @typedef {import('ajv/dist/core').JTDDataType<typeof branchSchema>} Branch | ||
* @typedef {import('ajv/dist/core').JTDDataType<typeof fullProductNameSchema>} FullProductName | ||
*/ | ||
|
||
/** | ||
* Checks if the `stringToCheck` includes more than one unescaped `*` character. A `*` character | ||
* can be escaped by prefixing it with a backslash (`\`). | ||
* | ||
* @param {string} stringToCheck | ||
* @return {boolean} | ||
*/ | ||
export function containMultipleUnescapedStars(stringToCheck) { | ||
const regex = /\*/g | ||
return ( | ||
(stringToCheck | ||
.replace(/\\\*/g, '') // remove escaped '*' | ||
.match(regex)?.length ?? 0) > 1 // check if there is more than 1 unescaped '*' | ||
) | ||
} | ||
|
||
/** | ||
* Validates all given serial numbers and | ||
* check whether they contain multiple unescaped stars | ||
* | ||
* @param {Array<string> | undefined} serialNumbers serial_numbers to check | ||
* @return {Array<string>} indexes of the serial_numbers that invalid | ||
*/ | ||
export function checkSerialNumbers(serialNumbers) { | ||
/** @type {Array<string>}*/ | ||
const invalidNumbers = [] | ||
if (serialNumbers) { | ||
for (let i = 0; i < serialNumbers.length; i++) { | ||
const modelNumber = serialNumbers[i] | ||
if (containMultipleUnescapedStars(modelNumber)) { | ||
invalidNumbers.push('' + i) | ||
} | ||
} | ||
} | ||
return invalidNumbers | ||
} | ||
|
||
/** | ||
* For each serial number, it MUST be tested | ||
* that it does not contain multiple unescaped stars. | ||
* | ||
* @param {unknown} doc | ||
*/ | ||
export function mandatoryTest_6_1_44(doc) { | ||
/* | ||
The `ctx` variable holds the state that is accumulated during the test run and is | ||
finally returned by the function. | ||
*/ | ||
const ctx = { | ||
errors: | ||
/** @type {Array<{ instancePath: string; message: string }>} */ ([]), | ||
isValid: true, | ||
} | ||
|
||
if (!validate(doc)) { | ||
return ctx | ||
} | ||
|
||
doc.product_tree?.branches?.forEach((branch, index) => { | ||
checkBranch(`/product_tree/branches/${index}`, branch) | ||
}) | ||
|
||
doc.product_tree?.full_product_names?.forEach((fullProduceName, index) => { | ||
checkFullProductName( | ||
`/product_tree/full_product_names/${index}`, | ||
fullProduceName | ||
) | ||
}) | ||
|
||
doc.product_tree?.relationships?.forEach((relationship, index) => { | ||
const fullProductName = relationship.full_product_name | ||
if (fullProductName) { | ||
checkFullProductName( | ||
`/product_tree/relationships/${index}/full_product_name`, | ||
fullProductName | ||
) | ||
} | ||
}) | ||
|
||
return ctx | ||
|
||
/** | ||
* Check whether the serial numbers contain multiple unescaped stars for a full product name object | ||
* | ||
* @param {string} prefix The instance path prefix of the "full product name". It is | ||
* used to generate error messages. | ||
* @param {FullProductName} fullProductName The "full product name" object. | ||
*/ | ||
function checkFullProductName(prefix, fullProductName) { | ||
const invalidNumberIndexes = checkSerialNumbers( | ||
fullProductName.product_identification_helper?.serial_numbers | ||
) | ||
invalidNumberIndexes.forEach((invalidNumberIndex) => { | ||
ctx.isValid = false | ||
ctx.errors.push({ | ||
instancePath: `${prefix}/product_identification_helper/serial_numbers/${invalidNumberIndex}`, | ||
message: 'Serial number contains multiple unescaped stars', | ||
}) | ||
}) | ||
} | ||
|
||
/** | ||
* Check whether the model numbers contain multiple unescaped stars for the given branch object | ||
* and its branch children. | ||
* | ||
* @param {string} prefix The instance path prefix of the "branch". It is | ||
* used to generate error messages. | ||
* @param {Branch} branch The "branch" object. | ||
*/ | ||
function checkBranch(prefix, branch) { | ||
const invalidNumberIndexes = checkSerialNumbers( | ||
branch.product?.product_identification_helper?.serial_numbers | ||
) | ||
invalidNumberIndexes.forEach((invalidNumberIndex) => { | ||
ctx.isValid = false | ||
ctx.errors.push({ | ||
instancePath: `${prefix}/product/product_identification_helper/serial_numbers/${invalidNumberIndex}`, | ||
message: 'Serial number contains multiple unescaped stars', | ||
}) | ||
}) | ||
branch.branches?.forEach((branch, index) => { | ||
if (validateBranch(branch)) { | ||
checkBranch(`${prefix}/branches/${index}`, branch) | ||
} | ||
}) | ||
} | ||
} |
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,9 @@ | ||
import assert from 'node:assert/strict' | ||
|
||
import { mandatoryTest_6_1_44 } from '../../csaf_2_1/mandatoryTests/mandatoryTest_6_1_44.js' | ||
|
||
describe('mandatoryTest_6_1_44', function () { | ||
it('only runs on relevant documents', function () { | ||
assert.equal(mandatoryTest_6_1_44({ product_tree: 'mydoc' }).isValid, true) | ||
}) | ||
}) |
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 |
---|---|---|
|
@@ -21,7 +21,6 @@ const excluded = [ | |
'6.1.27.18', | ||
'6.1.37', | ||
'6.1.42', | ||
'6.1.44', | ||
'6.1.46', | ||
'6.1.47', | ||
'6.1.48', | ||
|
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.