docs(tests): clarify validation ownership and digest lifecycle checks

This commit is contained in:
2026-11-12 17:22:45 +08:00
parent eb287b8f23
commit 225170b024
6 changed files with 16 additions and 4 deletions
+2
View File
@@ -10,6 +10,7 @@ import {importSourceDigest} from '../../src/lib/canvas-runtime/import-integrity.
// 用 Node createHash 计算参照摘要;既不复用待测算法,也不以自身输出作为预期值。
const expected=bytes=>createHash('sha256').update(bytes).digest('hex');
// 空值、标准字符串、百万个 a 及长度 0–259 的随机输入,分别按六种块宽更新;hex 可重复读取,但最终化后禁止 update。
// 六种切分覆盖 64 字节块的前后边界及多块更新;随机内容每次不同,预期摘要始终由 Node crypto 独立计算。
test('incremental SHA-256 matches known vectors, every padding boundary and varied chunk splits',()=>{
for(const bytes of [Buffer.alloc(0),Buffer.from('abc'),Buffer.from('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'),Buffer.alloc(1000000,97),...Array.from({length:260},(_,n)=>randomBytes(n))]){
for(const chunk of [1,7,63,64,65,8192]){const hash=createSha256();for(let offset=0;offset<bytes.length;offset+=chunk)hash.update(bytes.subarray(offset,offset+chunk));assert.equal(hash.hex(),expected(bytes),`${bytes.length}/${chunk}`);assert.equal(hash.hex(),expected(bytes));assert.throws(()=>hash.update(new Uint8Array()),/digest_finalized/);}
@@ -20,6 +21,7 @@ test('source digest reads bounded slices, yields and never materializes the enti
const data=randomBytes(1024*1024+13),reads=[];let yields=0;
// 禁止对整个源 Blob 调 arrayBuffer,slice 记录请求块长并交给真实 Blob 读字节。
class SlicedBlob extends Blob{arrayBuffer(){throw Error('whole Blob read');}slice(start,end){reads.push(end-start);return super.slice(start,end);}}
// 最后一块只有 13 字节;slice 的请求长度不超过上限,并且只在块与块之间让出执行权。
assert.equal(await importSourceDigest(new SlicedBlob([data]),undefined,{chunkBytes:65536,yieldTask:async()=>{yields++;}}),expected(data));assert.equal(reads.length,17);assert.ok(reads.every(size=>size<=65536));assert.equal(yields,16);
});
// 分别在读块挂起时 abort 和 yield 回调内 abort;迟到块及调度返回后都不能完成摘要,统一拒绝 import_aborted。