单元测试(新增,node:test 零依赖): - tests/unit/: ulid / hmac / ed25519 / acl / validate 共 26 项用例 - 覆盖最关键的安全函数:签名验签、ACL 授权、参数校验 - 新增 pnpm test:unit;CI 增加单元测试步骤 细节修复: 1. screen.info 的 scale 硬编码为 1 → 改读注册表 HKCU AppliedDPI, 真实反映 Windows DPI 缩放(100% -> 1.0, 150% -> 1.5) 2. verify.mjs 升级:支持多设备配置(resolveTarget)+ ed25519 模式 + Hub 中转 (此前直接用旧的扁平配置 + 仅 psk,配了多设备会取不到 host) 验证:构建 + 类型检查全绿;单元 26/26;E2E 21/21;verify.mjs 13/0/3
38 lines
1.5 KiB
JavaScript
38 lines
1.5 KiB
JavaScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { computeHmac, verifyHmac, generateNonce, generateSharedKey } from '../../packages/protocol/dist/index.js';
|
|
|
|
test('hmac:正确密钥/nonce 校验通过,错误一律拒绝', () => {
|
|
const key = 'test-key-0123456789abcdef';
|
|
const nonce = generateNonce();
|
|
const mac = computeHmac(key, nonce);
|
|
|
|
assert.equal(typeof mac, 'string');
|
|
assert.ok(mac.length > 0, 'HMAC 不应为空');
|
|
|
|
assert.equal(verifyHmac(key, nonce, mac), true, '正确密钥应通过');
|
|
assert.equal(verifyHmac('wrong-key', nonce, mac), false, '错误密钥应拒绝');
|
|
assert.equal(verifyHmac(key, 'wrong-nonce', mac), false, '错误 nonce 应拒绝');
|
|
assert.equal(verifyHmac(key, nonce, 'garbage'), false, '非法格式应拒绝');
|
|
assert.equal(verifyHmac(key, nonce, ''), false, '空值应拒绝');
|
|
assert.equal(verifyHmac(key, nonce, mac + 'x'), false, '长度不符应拒绝');
|
|
});
|
|
|
|
test('hmac:对同一输入结果确定(可重现)', () => {
|
|
const key = 'k';
|
|
const nonce = 'n';
|
|
assert.equal(computeHmac(key, nonce), computeHmac(key, nonce), '同一输入应得到同一 HMAC');
|
|
});
|
|
|
|
test('generateNonce / generateSharedKey:随机且格式正确', () => {
|
|
const n1 = generateNonce();
|
|
const n2 = generateNonce();
|
|
assert.notEqual(n1, n2, '两次 nonce 应不同');
|
|
|
|
const k1 = generateSharedKey();
|
|
const k2 = generateSharedKey();
|
|
assert.notEqual(k1, k2);
|
|
assert.equal(k1.length, 64, '32 字节 hex 应为 64 字符');
|
|
assert.match(k1, /^[0-9a-f]+$/, '应为小写 hex');
|
|
});
|