import test from 'node:test'; import assert from 'node:assert/strict'; import { createIdentity, hello, signIdentity, verifyIdentity, exportBackup, importBackup, parseHello, validateContacts, type IdentityContext } from '../src/lib/identity.ts'; const fingerprint = (byte: string) => Array(32).fill(byte).join(' '); async function setup() { const first = await createIdentity(), second = await createIdentity(); const context: IdentityContext = { offer: hello(first), answer: hello(second), offerFingerprint: fingerprint('AA'), answerFingerprint: fingerprint('BB') }; return { first, second, context }; } test('device proof binds both keys, fresh nonces, roles, and both DTLS fingerprints', async () => { const { first, second, context } = await setup(); const proof = await signIdentity(first, context, true); await verifyIdentity(proof, context, true); await verifyIdentity(await signIdentity(second, context, false), context, false); await assert.rejects(verifyIdentity(proof, context, false)); for (const changed of [ { ...context, offer: hello(first) }, { ...context, answer: hello(second) }, { ...context, offer: hello(second) }, { ...context, answer: hello(first) }, { ...context, offerFingerprint: fingerprint('CC') }, { ...context, answerFingerprint: fingerprint('CC') }, ]) await assert.rejects(verifyIdentity(proof, changed, true)); const imposter = await createIdentity(); await assert.rejects(signIdentity(imposter, context, true)); await assert.rejects(verifyIdentity('00'.repeat(64), context, true)); }); test('identity input is bounded and contacts cannot supply duplicate identities', async () => { const identity = await createIdentity(); assert.throws(() => parseHello({ key: identity.publicKey, nonce: 'bad' })); assert.throws(() => parseHello({ key: 'ff'.repeat(65), nonce: '00'.repeat(32) })); assert.throws(() => validateContacts([{ key: identity.publicKey, name: ' ' }])); assert.throws(() => validateContacts(Array(101).fill({ key: identity.publicKey, name: 'friend' }))); assert.throws(() => validateContacts(Array(2).fill({ key: identity.publicKey, name: 'friend' }))); }); test('encrypted backup restores signing identity and contacts; wrong password and tampering fail', async () => { const { first, second, context } = await setup(); const state = { identity: first, contacts: [{ key: second.publicKey, name: 'Other device' }] }; const password = 'a long private backup passphrase'; const backup = await exportBackup(state, password); assert.doesNotMatch(backup, /Other device|privateKey/); const restored = await importBackup(backup, password); assert.deepEqual(restored.contacts, state.contacts); assert.equal(restored.identity!.publicKey, first.publicKey); await verifyIdentity(await signIdentity(restored.identity!, context, true), context, true); await assert.rejects(importBackup(backup, 'the incorrect password')); const damaged = JSON.parse(backup); damaged.data = (damaged.data.startsWith('00') ? 'ff' : '00') + damaged.data.slice(2); await assert.rejects(importBackup(JSON.stringify(damaged), password)); damaged.iterations = 1; await assert.rejects(importBackup(JSON.stringify(damaged), password)); await assert.rejects(importBackup('x'.repeat(100001), password)); await assert.rejects(exportBackup(state, 'short')); });