Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion csaf_2_1/mandatoryTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export {
mandatoryTest_6_1_3,
mandatoryTest_6_1_5,
mandatoryTest_6_1_12,
mandatoryTest_6_1_14,
mandatoryTest_6_1_15,
mandatoryTest_6_1_16,
mandatoryTest_6_1_17,
Expand Down Expand Up @@ -38,6 +37,7 @@ export { mandatoryTest_6_1_9 } from './mandatoryTests/mandatoryTest_6_1_9.js'
export { mandatoryTest_6_1_10 } from './mandatoryTests/mandatoryTest_6_1_10.js'
export { mandatoryTest_6_1_11 } from './mandatoryTests/mandatoryTest_6_1_11.js'
export { mandatoryTest_6_1_13 } from './mandatoryTests/mandatoryTest_6_1_13.js'
export { mandatoryTest_6_1_14 } from './mandatoryTests/mandatoryTest_6_1_14.js'
export { mandatoryTest_6_1_27_3 } from './mandatoryTests/mandatoryTest_6_1_27_3.js'
export { mandatoryTest_6_1_27_4 } from './mandatoryTests/mandatoryTest_6_1_27_4.js'
export { mandatoryTest_6_1_27_5 } from './mandatoryTests/mandatoryTest_6_1_27_5.js'
Expand Down
110 changes: 110 additions & 0 deletions csaf_2_1/mandatoryTests/mandatoryTest_6_1_14.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import semver from 'semver'
import { Ajv } from 'ajv/dist/jtd.js'
import { compareZonedDateTimes } from '../dateHelper.js'

const { gt, valid } = semver

const ajv = new Ajv()

const revisionHistoryEntrySchema = /** @type {const} */ ({
additionalProperties: true,
properties: {
date: { type: 'string' },
number: { type: 'string' },
},
})

const inputSchema = /** @type {const} */ ({
additionalProperties: true,
optionalProperties: {
document: {
additionalProperties: true,
optionalProperties: {
tracking: {
additionalProperties: true,
optionalProperties: {
revision_history: {
elements: revisionHistoryEntrySchema,
},
},
},
},
},
},
})

const validate = ajv.compile(inputSchema)

/**
* @typedef {import('ajv/dist/core.js').JTDDataType<typeof revisionHistoryEntrySchema>} RevisionHistoryEntry
*/

/**
* Maps `number` to a value comparable with semver. Integer version numbers
* are mapped to semantic versioning by appending `.0.0`.
*
* @param {string} number
* @returns {string | null}
*/
const toComparableSemver = (number) => {
if (valid(number)) return number
return /^\d+$/.test(number) ? `${number}.0.0` : null
}

/**
* @param {RevisionHistoryEntry} a
* @param {RevisionHistoryEntry} b
* @returns {number}
*/
const compareEntries = (a, b) => {
const dateComparison = compareZonedDateTimes(a.date, b.date)
if (dateComparison !== 0) {
return dateComparison
}

const aVersion = toComparableSemver(a.number)
const bVersion = toComparableSemver(b.number)
if (aVersion === null || bVersion === null) {
return 0
}
return semver.compare(aVersion, bVersion)
}

/**
* @param {unknown} doc
*/
export function mandatoryTest_6_1_14(doc) {
const ctx = {
errors:
/** @type {Array<{ instancePath: string; message: string }>} */ ([]),
isValid: true,
}

if (
!validate(doc) ||
!Array.isArray(doc.document?.tracking?.revision_history)
) {
return ctx
}

const sortedNumbers = doc.document.tracking.revision_history
.slice()
.sort(compareEntries)
.map((entry) => toComparableSemver(entry.number))
.filter(/** @returns {n is string} */ (n) => n !== null)

const isAscending = sortedNumbers.every(
(number, index, all) => index === 0 || gt(number, all[index - 1])
)

if (!isAscending) {
ctx.isValid = false
ctx.errors.push({
instancePath: `/document/tracking/revision_history`,
message:
'items must be in ascending order when sorted by "date" and "number"',
})
}

return ctx
}
2 changes: 1 addition & 1 deletion lib/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface Result {
infos: Array<{ message: string; instancePath: string }>
}

interface TestResult {
export interface TestResult {
isValid?: boolean
warnings?: Array<{ message: string; instancePath: string }>
errors?: Array<{ message: string; instancePath: string }>
Expand Down
27 changes: 27 additions & 0 deletions tests/csaf_2_1/mandatoryTest_6_1_14.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { mandatoryTest_6_1_14 } from '../../csaf_2_1/mandatoryTests/mandatoryTest_6_1_14.js'

describe('mandatoryTest_6_1_14', function () {
it('only runs on relevant documents', function () {
expect(mandatoryTest_6_1_14({ product_tree: 'mydoc' }).isValid).to.equal(
true
)
})

it('skips documents with invalid revision_history entries', function () {
const doc = {
document: {
tracking: {
revision_history: [
{ date: '2020-01-01T00:00:00+00:00', number: '1.0.0' },
{ date: '2020-01-01T00:00:00+00:00', number: 'invalid' },
],
},
},
}

expect(() => mandatoryTest_6_1_14(doc)).toBeTruthy()
const result = mandatoryTest_6_1_14(doc)
expect(result.isValid).to.equal(true)
expect(result.errors).to.deep.equal([])
})
})
1 change: 0 additions & 1 deletion tests/csaf_2_1/oasis.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
const skippedTests = new Set([
'mandatory/oasis_csaf_tc-csaf_2_1-2024-6-1-03-01.json',
'mandatory/oasis_csaf_tc-csaf_2_1-2024-6-1-03-02.json',
'mandatory/oasis_csaf_tc-csaf_2_1-2024-6-1-14-32.json',
'mandatory/oasis_csaf_tc-csaf_2_1-2024-6-1-21-17.json',
'mandatory/oasis_csaf_tc-csaf_2_1-2024-6-1-27-08-02.json',
'recommended/oasis_csaf_tc-csaf_2_1-2024-6-2-38-13.json',
Expand Down Expand Up @@ -176,7 +175,7 @@
const result = await test(doc)

if (group === 'mandatory') {
expect(result.isValid).to.equal(testSpec.valid)

Check failure on line 178 in tests/csaf_2_1/oasis.js

View workflow job for this annotation

GitHub Actions / test-browser

[browser (chromium)] tests/csaf_2_1/oasis.js > mandatory > 6.1.14 > valid > mandatory/oasis_csaf_tc-csaf_2_1-2024-6-1-14-32.json

AssertionError: expected true to equal false - Expected + Received - false + true ❯ tests/csaf_2_1/oasis.js:178:44
expect(
Boolean(result.errors?.length),
type === 'failures'
Expand Down
Loading