An unreachable prekey server (packets dropped, not refused) held createShade for ~2 min per call, and a failed registration was never retried despite the log saying so. Every request now has a deadline, registration retries with backoff until it lands, and registration: 'background' lets an app open its identity offline. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
126 lines
5.0 KiB
TypeScript
126 lines
5.0 KiB
TypeScript
import { describe, test, expect } from 'bun:test';
|
|
import { SubtleCryptoProvider, MemoryStorage } from '@shade/crypto-web';
|
|
import { ShadeSessionManager } from '@shade/core';
|
|
import { createPrekeyServer, MemoryPrekeyStore } from '@shade/server';
|
|
import { ShadeFetchTransport } from '../src/fetch-transport.js';
|
|
|
|
const crypto = new SubtleCryptoProvider();
|
|
|
|
describe('ShadeFetchTransport', () => {
|
|
test('full flow: register → fetch bundle → establish session → talk', async () => {
|
|
const store = new MemoryPrekeyStore();
|
|
const server = createPrekeyServer({ crypto, store, disableRateLimit: true });
|
|
|
|
const port = 19000 + Math.floor(Math.random() * 1000);
|
|
const handle = Bun.serve({ port, fetch: server.fetch });
|
|
|
|
try {
|
|
const baseUrl = `http://localhost:${port}`;
|
|
|
|
// ─── Bob: set up session manager, register with transport ──
|
|
const bobStorage = new MemoryStorage();
|
|
const bobManager = new ShadeSessionManager(crypto, bobStorage);
|
|
await bobManager.initialize();
|
|
|
|
const bobIdentity = await bobStorage.getIdentityKeyPair();
|
|
const bobTransport = new ShadeFetchTransport({
|
|
baseUrl,
|
|
crypto,
|
|
signingPrivateKey: bobIdentity!.signingPrivateKey,
|
|
});
|
|
|
|
const bobOTPKs = await bobManager.generateOneTimePreKeys(5);
|
|
const bobBundle = await bobManager.createPreKeyBundle();
|
|
|
|
await bobTransport.register(
|
|
'bob',
|
|
bobManager.getPublicIdentity(),
|
|
bobBundle.signedPreKey,
|
|
bobOTPKs,
|
|
);
|
|
|
|
expect(await bobTransport.getKeyCount('bob')).toBe(5);
|
|
|
|
// ─── Alice: anonymous fetch + establish session ───────
|
|
const aliceStorage = new MemoryStorage();
|
|
const aliceManager = new ShadeSessionManager(crypto, aliceStorage);
|
|
await aliceManager.initialize();
|
|
|
|
// Alice doesn't need a signing key to fetch bundles
|
|
const aliceTransport = new ShadeFetchTransport({ baseUrl, crypto });
|
|
|
|
const fetchedBundle = await aliceTransport.fetchBundle('bob');
|
|
expect(fetchedBundle.identityDHKey).toEqual(bobManager.getPublicIdentity().dhKey);
|
|
|
|
expect(await aliceTransport.getKeyCount('bob')).toBe(4);
|
|
|
|
await aliceManager.initSessionFromBundle('bob', fetchedBundle);
|
|
|
|
// ─── Alice → Bob encrypted ───────────────────────────
|
|
const env1 = await aliceManager.encrypt('bob', 'Hello via transport!');
|
|
const plain1 = await bobManager.decrypt('alice', env1);
|
|
expect(plain1).toBe('Hello via transport!');
|
|
|
|
// ─── Bob → Alice reply ───────────────────────────────
|
|
const env2 = await bobManager.encrypt('alice', 'Got it!');
|
|
const plain2 = await aliceManager.decrypt('bob', env2);
|
|
expect(plain2).toBe('Got it!');
|
|
|
|
// ─── Replenish (signed) ──────────────────────────────
|
|
const remaining = await bobTransport.replenish('bob', [
|
|
{ keyId: 200, publicKey: crypto.randomBytes(32) },
|
|
{ keyId: 201, publicKey: crypto.randomBytes(32) },
|
|
]);
|
|
expect(remaining).toBe(6);
|
|
|
|
// ─── Unregister (signed) ─────────────────────────────
|
|
await bobTransport.unregister('bob');
|
|
await expect(aliceTransport.fetchBundle('bob')).rejects.toThrow();
|
|
|
|
} finally {
|
|
handle.stop();
|
|
}
|
|
});
|
|
});
|
|
|
|
/**
|
|
* A server that drops packets instead of refusing them used to hold every
|
|
* call until the kernel gave up on the TCP handshake — two minutes on
|
|
* Linux. Every request now has a deadline.
|
|
*/
|
|
describe('ShadeFetchTransport request deadline', () => {
|
|
/** A fetch that never answers, but honours abort like the real one. */
|
|
const blackHole = ((_url: unknown, init?: RequestInit) =>
|
|
new Promise<Response>((_resolve, reject) => {
|
|
init?.signal?.addEventListener('abort', () => reject(init.signal!.reason));
|
|
})) as unknown as typeof fetch;
|
|
|
|
test('a request nobody answers fails with a NetworkError at the deadline', async () => {
|
|
const transport = new ShadeFetchTransport({
|
|
baseUrl: 'https://prekey.invalid',
|
|
crypto,
|
|
fetch: blackHole,
|
|
requestTimeoutMs: 50,
|
|
});
|
|
const started = Date.now();
|
|
const err = await transport.getKeyCount('bob').catch((e) => e);
|
|
expect(err.name).toBe('NetworkError');
|
|
expect(err.message).toContain('No response from prekey.invalid within 50 ms');
|
|
expect(Date.now() - started).toBeLessThan(2_000);
|
|
});
|
|
|
|
test('every request goes through the supplied fetch', async () => {
|
|
const urls: string[] = [];
|
|
const transport = new ShadeFetchTransport({
|
|
baseUrl: 'https://prekey.invalid',
|
|
crypto,
|
|
fetch: (async (url: string) => {
|
|
urls.push(url);
|
|
return new Response(JSON.stringify({ count: 7 }), { status: 200 });
|
|
}) as unknown as typeof fetch,
|
|
});
|
|
expect(await transport.getKeyCount('bob')).toBe(7);
|
|
expect(urls).toEqual(['https://prekey.invalid/v1/keys/count/bob']);
|
|
});
|
|
});
|