From 3407e0c96b6cd7c93e321e414164fbc276677f71 Mon Sep 17 00:00:00 2001 From: Gregor MacLennan Date: Tue, 14 Nov 2023 17:10:45 +0900 Subject: [PATCH] new tests for duplicate connections --- tests/local-peers.js | 92 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/tests/local-peers.js b/tests/local-peers.js index 04405f8dd..084b7e8ba 100644 --- a/tests/local-peers.js +++ b/tests/local-peers.js @@ -69,6 +69,98 @@ test('Send invite immediately', async (t) => { t.is(await responsePromise, LocalPeers.InviteResponse.ACCEPT) }) +test('Send invite, duplicate connections', async (t) => { + const r1 = new LocalPeers() + const r2 = new LocalPeers() + + const invite = { + projectKey: Buffer.allocUnsafe(32).fill(0), + encryptionKeys: { auth: randomBytes(32) }, + } + + const kp1 = NoiseSecretStream.keyPair() + const kp2 = NoiseSecretStream.keyPair() + + const destroy1 = replicate(r1, r2, { kp1, kp2 }) + const [peers1] = await once(r1, 'peers') + const destroy2 = replicate(r1, r2, { kp1, kp2 }) + const [peers2] = await once(r1, 'peers') + + t.is(peers1.length, 1) + t.is(peers2.length, 1) + t.is(peers1[0].connectedAt, peers2[0].connectedAt, 'first connected is used') + + { + const responsePromise = r1.invite(peers1[0].deviceId, invite) + const [peerId, receivedInvite] = await once(r2, 'invite') + t.alike(receivedInvite, invite) + + r2.inviteResponse(peerId, { + projectKey: receivedInvite.projectKey, + decision: LocalPeers.InviteResponse.ACCEPT, + }) + + t.is(await responsePromise, LocalPeers.InviteResponse.ACCEPT) + } + + destroy1() + const [peers3] = await once(r1, 'peers') + + t.is(peers3.length, 1) + t.ok( + peers3[0].connectedAt > peers1[0].connectedAt, + 'later connected peer is not used' + ) + + { + const responsePromise = r1.invite(peers1[0].deviceId, invite) + const [peerId, receivedInvite] = await once(r2, 'invite') + t.alike(receivedInvite, invite) + + r2.inviteResponse(peerId, { + projectKey: receivedInvite.projectKey, + decision: LocalPeers.InviteResponse.ACCEPT, + }) + + t.is(await responsePromise, LocalPeers.InviteResponse.ACCEPT) + } + + const now = Date.now() + destroy2() + const [peers4] = await once(r1, 'peers') + t.is(peers4.length, 1) + t.is(peers4[0].status, 'disconnected') + t.ok(peers4[0].disconnectedAt >= now, 'most recently disconnected exposed') +}) + +test('Duplicate connections with immediate disconnect', async (t) => { + const r1 = new LocalPeers() + const r2 = new LocalPeers() + + const invite = { + projectKey: Buffer.allocUnsafe(32).fill(0), + encryptionKeys: { auth: randomBytes(32) }, + } + + const kp1 = NoiseSecretStream.keyPair() + const kp2 = NoiseSecretStream.keyPair() + + replicate(r1, r2, { kp1, kp2 }) + const destroy2 = replicate(r1, r2, { kp1, kp2 }) + destroy2() + + const responsePromise = r1.invite(kp2.publicKey.toString('hex'), invite) + const [peerId, receivedInvite] = await once(r2, 'invite') + t.alike(receivedInvite, invite) + + r2.inviteResponse(peerId, { + projectKey: receivedInvite.projectKey, + decision: LocalPeers.InviteResponse.ACCEPT, + }) + + t.is(await responsePromise, LocalPeers.InviteResponse.ACCEPT) +}) + test('Send invite and reject', async (t) => { t.plan(3) const r1 = new LocalPeers()