Files
cindy/scripts/__tests__/verify-sha256.test.mjs
Lizi bb3f71d084 chore: Cindy 首次开源提交 / initial open-source release
Cindy 是心动网络 (X.D. Network Inc.) 的跨端 AI Agent 客户端。
本次提交包含 Desktop(Electron)、Mobile(Expo)及全部共享 packages
的完整源码,以 Apache-2.0 协议开源。

This is the first public release of Cindy, X.D. Network's cross-platform
AI agent client: the Electron desktop app, the Expo mobile app, and all
shared workspace packages, licensed under Apache-2.0.

- 许可与合规:根 LICENSE(Apache-2.0)+ NOTICE;第三方组件归属声明与
  SPDX SBOM 收口在 docs/legal/notices/,由 pnpm licenses:generate 生成。
- 社区文件:README / CONTRIBUTING / CODE_OF_CONDUCT / SECURITY / SUPPORT
  中英双语;issue / PR 模板与 CI workflow 在 .github/。
- 协议层以公开 submodule 引入:github.com/makecindy/cindy-protocol。
- 提交前已通过仓库门禁:全量 pnpm test:unit 与涉及 package 的 typecheck。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-24 08:30:44 +08:00

96 lines
4.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// verify-sha256 单测:纯函数 + fail-closed 文件校验。
// node 内置 test runner,无 vitest 依赖:`node --test scripts/__tests__/verify-sha256.test.mjs`。
import { test } from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import {
sha256Hex,
sha256File,
normalizeExpectedSha256,
assertSha256,
verifyFileSha256OrRemove,
} from '../../tools/shared/verify-sha256.mjs';
// 已知向量(NIST):便于跨实现核对。
const EMPTY_SHA256 = 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855';
const ABC_SHA256 = 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad';
function tmpFile(name, data) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'verify-sha256-test-'));
const p = path.join(dir, name);
fs.writeFileSync(p, data);
return p;
}
test('sha256Hex: 已知向量(空串 / "abc")', () => {
assert.equal(sha256Hex(''), EMPTY_SHA256);
assert.equal(sha256Hex('abc'), ABC_SHA256);
assert.equal(sha256Hex(Buffer.from('abc')), ABC_SHA256);
});
test('sha256File: 与 sha256Hex 对同一内容结果一致', () => {
const p = tmpFile('bin', Buffer.from('abc'));
assert.equal(sha256File(p), ABC_SHA256);
// 大于 1MB 分块的内容也应正确(跨多个 read 块累积)。
const big = Buffer.alloc((1 << 20) + 123, 7);
const bp = tmpFile('big', big);
assert.equal(sha256File(bp), sha256Hex(big));
});
test('normalizeExpectedSha256: 裸 hex / sha256: 前缀 / 大小写', () => {
assert.equal(normalizeExpectedSha256(ABC_SHA256), ABC_SHA256);
assert.equal(normalizeExpectedSha256(`sha256:${ABC_SHA256}`), ABC_SHA256);
assert.equal(normalizeExpectedSha256(ABC_SHA256.toUpperCase()), ABC_SHA256);
assert.equal(normalizeExpectedSha256(` ${ABC_SHA256} `), ABC_SHA256);
});
test('normalizeExpectedSha256: 非法输入一律返回 null', () => {
assert.equal(normalizeExpectedSha256(null), null);
assert.equal(normalizeExpectedSha256(undefined), null);
assert.equal(normalizeExpectedSha256(''), null);
assert.equal(normalizeExpectedSha256('deadbeef'), null); // 太短
assert.equal(normalizeExpectedSha256(`${ABC_SHA256}00`), null); // 太长
assert.equal(normalizeExpectedSha256(`zz${ABC_SHA256.slice(2)}`), null); // 非 hex 字符
assert.equal(normalizeExpectedSha256(123), null); // 非字符串
});
test('assertSha256: 匹配(裸 + digest 形式)返回归一化 hex', () => {
assert.equal(assertSha256({ actualHex: ABC_SHA256, expected: ABC_SHA256, label: 'x' }), ABC_SHA256);
assert.equal(assertSha256({ actualHex: ABC_SHA256, expected: `sha256:${ABC_SHA256}`, label: 'x' }), ABC_SHA256);
// actual 大写也应通过。
assert.equal(assertSha256({ actualHex: ABC_SHA256.toUpperCase(), expected: ABC_SHA256, label: 'x' }), ABC_SHA256);
});
test('assertSha256: 不匹配抛错', () => {
assert.throws(
() => assertSha256({ actualHex: EMPTY_SHA256, expected: ABC_SHA256, label: 'claude darwin-arm64' }),
/SHA256 mismatch for claude darwin-arm64/,
);
});
test('assertSha256: 期望缺失 / 格式非法一律 fail-closed 抛错', () => {
assert.throws(() => assertSha256({ actualHex: ABC_SHA256, expected: null, label: 'x' }), /no trusted sha256/);
assert.throws(() => assertSha256({ actualHex: ABC_SHA256, expected: 'garbage', label: 'x' }), /no trusted sha256/);
});
test('verifyFileSha256OrRemove: 匹配时保留文件、返回 hex', () => {
const p = tmpFile('bin', Buffer.from('abc'));
assert.equal(verifyFileSha256OrRemove(p, `sha256:${ABC_SHA256}`, 'ok-bin'), ABC_SHA256);
assert.equal(fs.existsSync(p), true);
});
test('verifyFileSha256OrRemove: 不匹配时删除文件并抛错(fail-closed)', () => {
const p = tmpFile('bin', Buffer.from('tampered'));
assert.throws(() => verifyFileSha256OrRemove(p, ABC_SHA256, 'bad-bin'), /SHA256 mismatch/);
assert.equal(fs.existsSync(p), false); // 未验证的字节绝不留在磁盘上
});
test('verifyFileSha256OrRemove: 拿不到可信 hash 时也删除文件并抛错', () => {
const p = tmpFile('bin', Buffer.from('abc'));
assert.throws(() => verifyFileSha256OrRemove(p, null, 'no-hash-bin'), /no trusted sha256/);
assert.equal(fs.existsSync(p), false);
});