-
Notifications
You must be signed in to change notification settings - Fork 9
feat(CSAF2.1): #196 add recommendedTest_6.2.31 #267
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
bendo-eXX
wants to merge
18
commits into
main
Choose a base branch
from
196-csaf-2.1_optional_test_6.2.31
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
18 commits
Select commit
Hold shift + click to select a range
aaa0ee3
feat(CSAF2.1): #196 add optionalTest_6_2_31.js
bendo-eXX 908ab0d
feat: rename Test to recommendedTest_6_2_30.js
bendo-eXX 1178c2c
feat: remove unknown doc typ
bendo-eXX a0d00a8
refactor(CSAF2.1): refactor typedef
bendo-eXX 8558d6b
feat(CSAF2.1): added tests to recommendedTest_6_2_31.js
bendo-eXX 1d38f95
feat(CSAF2.1): change massage
bendo-eXX a749d1a
feat(CSAF2.1): change massage
bendo-eXX d818203
feat(CSAF2.1): change branch schema
bendo-eXX 5133948
Merge branch 'main' into 196-csaf-2.1_optional_test_6.2.31
bendo-eXX d125194
Merge branch 'main' into 196-csaf-2.1_optional_test_6.2.31
bendo-eXX 2dd8ae2
feat: add check for relationships/full_product_name
bendo-eXX 93a9da9
Merge remote-tracking branch 'origin/main' into 196-csaf-2.1_optional…
bendo-eXX 6f2d56d
Merge branch 'refs/heads/main' into 196-csaf-2.1_optional_test_6.2.31
bendo-eXX f7ba999
feat(CSAF2.1): update test 6.2.31 to the new csaf 2.1 schema
bendo-eXX 439c6e1
feat(CSAF2.1): fix code style issue
bendo-eXX 312a601
Merge branch 'main' into 196-csaf-2.1_optional_test_6.2.31
bendo-eXX 06bdffc
feat(CSAF2.1): change test description
bendo-eXX 6b630b4
Merge branch 'main' into 196-csaf-2.1_optional_test_6.2.31
bendo-eXX 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
Some comments aren't visible on the classic Files Changed page.
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,236 @@ | ||
| import { Ajv } from 'ajv/dist/jtd.js' | ||
|
|
||
| const ajv = new Ajv() | ||
|
|
||
| const productIdentificationHelperSchema = /** @type {const} */ ({ | ||
| additionalProperties: true, | ||
| optionalProperties: { | ||
| serial_numbers: { | ||
| elements: { type: 'string' }, | ||
| }, | ||
| model_numbers: { | ||
| elements: { type: 'string' }, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| const productSchema = /** @type {const} */ ({ | ||
| additionalProperties: true, | ||
| optionalProperties: { | ||
| product_id: { type: 'string' }, | ||
| product_identification_helper: productIdentificationHelperSchema, | ||
| }, | ||
| }) | ||
|
|
||
| const subpathSchema = /** @type {const} */ ({ | ||
| additionalProperties: true, | ||
| optionalProperties: { | ||
| next_product_reference: { type: 'string' }, | ||
| }, | ||
| }) | ||
|
|
||
| const productPathSchema = /** @type {const} */ ({ | ||
| additionalProperties: true, | ||
| optionalProperties: { | ||
| beginning_product_reference: { type: 'string' }, | ||
| full_product_name: productSchema, | ||
| subpaths: { | ||
| elements: subpathSchema, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| const branchSchema = /** @type {const} */ ({ | ||
| additionalProperties: true, | ||
| optionalProperties: { | ||
| product: productSchema, | ||
| branches: { | ||
| elements: { | ||
| additionalProperties: true, | ||
| // AJV's JTD does not support recursive schemas. | ||
| // Nested branches are validated at runtime in checkBranches() by calling | ||
| // validateBranch() on each child branch individually during the recursive traversal. | ||
| properties: {}, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| const inputSchema = /** @type {const} */ ({ | ||
|
rainer-exxcellent marked this conversation as resolved.
|
||
| additionalProperties: true, | ||
| optionalProperties: { | ||
| product_tree: { | ||
| additionalProperties: true, | ||
| optionalProperties: { | ||
| branches: { | ||
| elements: branchSchema, | ||
| }, | ||
| full_product_names: { | ||
| elements: productSchema, | ||
| }, | ||
| product_paths: { | ||
| elements: productPathSchema, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| const validateInput = ajv.compile(inputSchema) | ||
| const validateBranch = ajv.compile(branchSchema) | ||
|
|
||
| /** | ||
| * @typedef {import('ajv/dist/core.js').JTDDataType<typeof branchSchema>} Branch | ||
| * @typedef {import('ajv/dist/core.js').JTDDataType<typeof productSchema>} FullProductName | ||
| * @typedef {import('ajv/dist/core.js').JTDDataType<typeof productPathSchema>} ProductPath | ||
| */ | ||
|
|
||
| /** | ||
| * This implements the recommended test 6.2.31 of the CSAF 2.1 standard. | ||
| * @param {unknown} doc | ||
| */ | ||
| export function recommendedTest_6_2_31(doc) { | ||
| const ctx = { | ||
| warnings: | ||
| /** @type {Array<{ instancePath: string; message: string }>} */ ([]), | ||
| } | ||
|
|
||
| if (!validateInput(doc)) { | ||
| return ctx | ||
| } | ||
|
|
||
| /** @type {ProductPath[]} */ | ||
| const productPaths = Array.isArray(doc.product_tree?.product_paths) | ||
| ? doc.product_tree.product_paths | ||
| : [] | ||
|
|
||
| // Start the recursive check from the root branches | ||
| checkBranches(doc.product_tree?.branches ?? [], productPaths, ctx) | ||
|
|
||
| checkFullProductNames( | ||
| doc.product_tree?.full_product_names ?? [], | ||
| productPaths, | ||
| ctx | ||
| ) | ||
|
|
||
|
bendo-eXX marked this conversation as resolved.
|
||
| productPaths.forEach((pp, index) => { | ||
| const fpn = pp?.full_product_name | ||
| if (!fpn?.product_identification_helper || !fpn.product_id) return | ||
| const { serial_numbers, model_numbers } = fpn.product_identification_helper | ||
| if ( | ||
| (serial_numbers?.length || model_numbers?.length) && | ||
| !checkProductPath(productPaths, fpn.product_id) | ||
| ) { | ||
| ctx.warnings.push({ | ||
| instancePath: `/product_tree/product_paths/${index}/full_product_name`, | ||
| message: | ||
| 'missing product path: product with serial number or model number should be referenced in a product path.', | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| return ctx | ||
| } | ||
|
|
||
| /** | ||
| * Check full_product_names for serial_numbers or model_numbers | ||
| * @param {FullProductName[]} full_product_names | ||
| * @param {ProductPath[]} productPaths | ||
| * @param {{ warnings: Array<{ instancePath: string; message: string }> }} ctx | ||
| */ | ||
| function checkFullProductNames(full_product_names, productPaths, ctx) { | ||
| full_product_names.forEach((fullProductName, index) => { | ||
| if ( | ||
| !fullProductName?.product_identification_helper || | ||
| !fullProductName.product_id | ||
| ) | ||
| return | ||
| const { serial_numbers, model_numbers } = | ||
| fullProductName.product_identification_helper | ||
|
|
||
| if ( | ||
| (serial_numbers?.length || model_numbers?.length) && | ||
| !checkProductPath(productPaths, fullProductName.product_id) | ||
| ) { | ||
| ctx.warnings.push({ | ||
| instancePath: `/product_tree/full_product_names/${index}`, | ||
| message: | ||
| 'missing product path: product with serial number or model number should be referenced in a product path.', | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Recursive function to check branches for products with serial_numbers or model_numbers | ||
| * but no corresponding product path. | ||
| * @param {Branch[]} branches - The current level of branches to process. | ||
| * @param {ProductPath[]} productPaths - The product paths array to check against. | ||
| * @param {{ warnings: Array<{ instancePath: string; message: string }> }} ctx - The context to store warnings. | ||
| * @param {string} [path='/product_tree/branches'] - The current JSON path. | ||
| */ | ||
| function checkBranches( | ||
| branches, | ||
| productPaths, | ||
| ctx, | ||
| path = '/product_tree/branches' | ||
| ) { | ||
| branches?.forEach((branch, branchIndex) => { | ||
| // Skip invalid branches | ||
| if (!validateBranch(branch)) return | ||
|
|
||
| const currentPath = `${path}/${branchIndex}` | ||
| const product = branch.product | ||
|
|
||
| if (product) { | ||
| if (product.product_id && product.product_identification_helper) { | ||
| const { serial_numbers, model_numbers } = | ||
| product.product_identification_helper | ||
|
|
||
| if ( | ||
| (serial_numbers?.length || model_numbers?.length) && | ||
| !checkProductPath(productPaths, product.product_id) | ||
| ) { | ||
| ctx.warnings.push({ | ||
| instancePath: `${currentPath}/product`, | ||
| message: | ||
| 'missing product path: product with serial number or model number should be referenced in a product path.', | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Recursively check nested branches | ||
| if (Array.isArray(branch.branches)) { | ||
| checkBranches( | ||
| branch.branches, | ||
| productPaths, | ||
| ctx, | ||
| `${currentPath}/branches` | ||
| ) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Check if there is a valid product path referencing the given productId. | ||
| * A product path is valid if it has at least one subpath and the productId | ||
| * matches either the beginning_product_reference or any subpaths[].next_product_reference. | ||
| * @param {ProductPath[]} productPaths | ||
| * @param {string} productId | ||
| * @returns {boolean} | ||
| */ | ||
| function checkProductPath(productPaths, productId) { | ||
| return productPaths.some((pp) => { | ||
| const subpaths = pp.subpaths | ||
| if (!Array.isArray(subpaths) || subpaths.length === 0) { | ||
| return false | ||
| } | ||
|
|
||
| if (pp.beginning_product_reference === productId) { | ||
| return true | ||
| } | ||
|
|
||
| return subpaths.some((sp) => sp.next_product_reference === productId) | ||
| }) | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -26,7 +26,6 @@ const excluded = [ | |
| '6.2.20', | ||
| '6.2.24', | ||
| '6.2.26', | ||
| '6.2.31', | ||
| '6.2.32', | ||
| '6.2.33', | ||
| '6.2.34', | ||
|
|
||
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.