Files
nodeagent/tests/unit/ed25519.test.mjs
T
jkooe f4c605cc6d test+fix: 单元测试套件 + 两处细节修复
单元测试(新增,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
2026-09-27 17:04:21 +08:00

34 lines
1.6 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.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { generateKeyPair, signNonce, verifyNonce, keyId } from '../../packages/protocol/dist/index.js';
test('ed25519:生成的密钥对格式正确', () => {
const kp = generateKeyPair();
assert.ok(kp.publicKey.length > 0);
assert.ok(kp.privateKey.length > 0);
assert.notEqual(kp.publicKey, kp.privateKey, '公私钥应不同');
});
test('ed25519:签名验签成功;错误输入一律拒绝且不抛异常', () => {
const kp = generateKeyPair();
const nonce = 'challenge-nonce-0123456789';
const sig = signNonce(kp.privateKey, nonce);
assert.equal(verifyNonce(kp.publicKey, nonce, sig), true, '正确应通过');
assert.equal(verifyNonce(kp.publicKey, 'other-nonce', sig), false, '错误 nonce 应拒绝');
const other = generateKeyPair();
assert.equal(verifyNonce(other.publicKey, nonce, sig), false, '错误公钥应拒绝');
assert.equal(verifyNonce(kp.publicKey, nonce, 'garbage'), false, '非法签名应拒绝');
assert.equal(verifyNonce('garbage-key', nonce, sig), false, '非法公钥应拒绝(不抛异常)');
assert.equal(verifyNonce(kp.publicKey, nonce, ''), false, '空签名应拒绝');
});
test('ed25519:keyId 稳定且不同密钥不同', () => {
const a = generateKeyPair();
const b = generateKeyPair();
assert.equal(keyId(a.publicKey), keyId(a.publicKey), '同一公钥指纹应稳定');
assert.notEqual(keyId(a.publicKey), keyId(b.publicKey), '不同公钥指纹应不同');
assert.match(keyId(a.publicKey), /^[0-9a-f]{16}$/, '指纹应为 16 位小写 hex');
});