Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: test receiving invalid invites #566

Merged
merged 4 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/local-peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const MESSAGE_TYPES = {
}
const MESSAGES_MAX_ID = Math.max.apply(null, [...Object.values(MESSAGE_TYPES)])

export const kTestOnlySendRawInvite = Symbol('testOnlySendRawInvite')

/**
* @typedef {object} PeerInfoBase
* @property {string} deviceId
Expand Down Expand Up @@ -152,6 +154,14 @@ class Peer {
this.#connected.reject(new PeerFailedConnectionError())
this.#log('disconnected')
}
/**
* @param {Buffer} buf
*/
[kTestOnlySendRawInvite](buf) {
this.#assertConnected()
const messageType = MESSAGE_TYPES.Invite
this.#channel.messages[messageType].send(buf)
}
/** @param {Invite} invite */
sendInvite(invite) {
this.#assertConnected()
Expand Down Expand Up @@ -213,6 +223,7 @@ class Peer {
* @property {(peerId: string, inviteResponse: InviteResponse) => void} invite-response Emitted when an invite response is received
* @property {(peerId: string, details: ProjectJoinDetails) => void} got-project-details Emitted when project details are received
* @property {(discoveryKey: Buffer, protomux: Protomux<import('@hyperswarm/secret-stream')>) => void} discovery-key Emitted when a new hypercore is replicated (by a peer) to a peer protomux instance (passed as the second parameter)
* @property {(messageType: string, errorMessage?: string) => void} failed-to-handle-message Emitted when we received a message we couldn't handle for some reason. Primarily useful for testing
*/

/** @extends {TypedEmitter<LocalPeersEvents>} */
Expand Down Expand Up @@ -301,6 +312,16 @@ export class LocalPeers extends TypedEmitter {
peer.sendDeviceInfo(deviceInfo)
}

/**
* @param {string} deviceId
* @param {Buffer} buf
*/
async [kTestOnlySendRawInvite](deviceId, buf) {
await this.#waitForPendingConnections()
const peer = await this.#getPeerByDeviceId(deviceId)
peer[kTestOnlySendRawInvite](buf)
}

/**
* Connect to a peer over an existing NoiseSecretStream
*
Expand Down Expand Up @@ -391,7 +412,9 @@ export class LocalPeers extends TypedEmitter {
message
)
} catch (err) {
this.#l.log(`Error handling ${type} message: ${String(err)}`)
const errorMessage = String(err)
this.emit('failed-to-handle-message', type, errorMessage)
this.#l.log(`Error handling ${type} message: ${errorMessage}`)
}
},
}
Expand Down
28 changes: 27 additions & 1 deletion tests/local-peers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// @ts-check
import test from 'brittle'
import { keyToId, projectKeyToPublicId } from '../src/utils.js'
import { LocalPeers, UnknownPeerError } from '../src/local-peers.js'
import {
LocalPeers,
UnknownPeerError,
kTestOnlySendRawInvite,
} from '../src/local-peers.js'
import { once } from 'events'
import { Duplex } from 'streamx'
import { replicate } from './helpers/local-peers.js'
Expand Down Expand Up @@ -151,6 +155,28 @@ test('messages to unknown peers', async (t) => {
)
})

test('handles invalid invites', async (t) => {
t.plan(1)

const r1 = new LocalPeers()
const r2 = new LocalPeers()

r1.once('peers', async ([peer]) => {
await r1[kTestOnlySendRawInvite](peer.deviceId, Buffer.from([1, 2, 3]))
})

r2.on('invite', () => {
t.fail('should not receive invite')
})

r2.once('failed-to-handle-message', (messageType) => {
t.is(messageType, 'Invite')
})

const destroy = replicate(r1, r2)
t.teardown(destroy)
})

test('Disconnected peer shows in state', async (t) => {
t.plan(6)
const r1 = new LocalPeers()
Expand Down
Loading