增加 CRC Hex 输入并修复依赖漏洞
This commit is contained in:
+34
-1
@@ -1,6 +1,11 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { calculateCrc, calculateCrcChunks, CRC_PRESETS, formatCrc } from '../src/lib/crc'
|
||||
import { calculateCrcReport } from '../src/lib/crcWorker'
|
||||
import {
|
||||
calculateCrcHexInput,
|
||||
calculateCrcReport,
|
||||
MAX_INLINE_CRC_DATA_BYTES,
|
||||
MAX_INLINE_CRC_INPUT_CHARS,
|
||||
} from '../src/lib/crcWorker'
|
||||
import { getFirmwareFormat, parseFirmwareContent } from '../src/lib/firmware'
|
||||
import { concatenateBlocks, parseIntelHex } from '../src/lib/intelHex'
|
||||
import { textToBytes } from '../src/lib/bytes'
|
||||
@@ -48,6 +53,34 @@ describe('CRC', () => {
|
||||
expect(calculateCrcChunks(chunks, preset)).toBe(calculateCrc(textToBytes('123456789'), preset))
|
||||
})
|
||||
|
||||
it('calculates inline Hex with the same separators used by other tools', () => {
|
||||
const preset = CRC_PRESETS[0]
|
||||
const inputs = [
|
||||
'A51F7ADD',
|
||||
'A5 1F 7A DD',
|
||||
'A5,1F,7A,DD',
|
||||
'A5,1F,7A,DD',
|
||||
'0xA5 0x1F 0x7A 0xDD',
|
||||
'0xa5, 1F\n0x7a,DD',
|
||||
]
|
||||
for (const input of inputs) {
|
||||
expect(calculateCrcHexInput(input, preset)).toEqual({
|
||||
combinedResult: '6139ED73',
|
||||
totalBytes: 4,
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
it('rejects empty, malformed, and oversized inline Hex', () => {
|
||||
const preset = CRC_PRESETS[0]
|
||||
expect(() => calculateCrcHexInput(' ', preset)).toThrow('请输入 Hex 数据')
|
||||
expect(() => calculateCrcHexInput('ABC', preset)).toThrow('偶数')
|
||||
expect(() => calculateCrcHexInput('A5,,1F', preset)).toThrow('片段缺少')
|
||||
expect(() => calculateCrcHexInput('GG', preset)).toThrow('无效')
|
||||
expect(() => calculateCrcHexInput('AA'.repeat(MAX_INLINE_CRC_DATA_BYTES + 1), preset)).toThrow('Hex 数据不能超过 1 MiB')
|
||||
expect(() => calculateCrcHexInput(' '.repeat(MAX_INLINE_CRC_INPUT_CHARS + 1), preset)).toThrow('输入不能超过')
|
||||
})
|
||||
|
||||
it('rejects malformed runtime parameters instead of coercing them', () => {
|
||||
const preset = CRC_PRESETS[0]
|
||||
expect(() => calculateCrc(new Uint8Array(), { ...preset, inputReflected: undefined as never })).toThrow('布尔值')
|
||||
|
||||
Reference in New Issue
Block a user