mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-20 12:36:31 +00:00
refactor: add js-libp2p-switch to the libp2p codebase (#388)
Co-authored-by: Alan Shaw <alan.shaw@protocol.ai> Co-authored-by: Alan Shaw <alan@tableflip.io> Co-authored-by: Arnaud <arnaud.valensi@gmail.com> Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: David Dias <mail@daviddias.me> Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com> Co-authored-by: Francisco Baio Dias <xicombd@gmail.com> Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com> Co-authored-by: Haad <haadcode@users.noreply.github.com> Co-authored-by: Hugo Dias <mail@hugodias.me> Co-authored-by: Hugo Dias <hugomrdias@gmail.com> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Kevin Kwok <antimatter15@gmail.com> Co-authored-by: Kobi Gurkan <kobigurk@gmail.com> Co-authored-by: Maciej Krüger <mkg20001@gmail.com> Co-authored-by: Matteo Collina <matteo.collina@gmail.com> Co-authored-by: Michael Fakhry <fakhrimichael@live.com> Co-authored-by: Oli Evans <oli@tableflip.io> Co-authored-by: Pau Ramon Revilla <masylum@gmail.com> Co-authored-by: Pedro Teixeira <i@pgte.me> Co-authored-by: Pius Nyakoojo <piusnyakoojo@gmail.com> Co-authored-by: Richard Littauer <richard.littauer@gmail.com> Co-authored-by: Sid Harder <sideharder@gmail.com> Co-authored-by: Vasco Santos <vasco.santos@ua.pt> Co-authored-by: harrshasri <35241544+harrshasri@users.noreply.github.com> Co-authored-by: kumavis <kumavis@users.noreply.github.com> Co-authored-by: ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>
This commit is contained in:
88
test/switch/dialSelf.spec.js
Normal file
88
test/switch/dialSelf.spec.js
Normal file
@ -0,0 +1,88 @@
|
||||
'use strict'
|
||||
|
||||
/* eslint-env mocha */
|
||||
|
||||
const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
chai.use(dirtyChai)
|
||||
|
||||
const { EventEmitter } = require('events')
|
||||
const PeerBook = require('peer-book')
|
||||
const Duplex = require('pull-pair/duplex')
|
||||
|
||||
const utils = require('./utils')
|
||||
const createInfos = utils.createInfos
|
||||
const Swarm = require('libp2p-switch')
|
||||
|
||||
class MockTransport extends EventEmitter {
|
||||
constructor () {
|
||||
super()
|
||||
this.conn = Duplex()
|
||||
}
|
||||
|
||||
dial (addr, cb) {
|
||||
const c = this.conn[0]
|
||||
this.emit('connection', this.conn[1])
|
||||
setImmediate(() => cb(null, c))
|
||||
return c
|
||||
}
|
||||
|
||||
listen (addr, cb) {
|
||||
return cb()
|
||||
}
|
||||
|
||||
filter (mas) {
|
||||
return Array.isArray(mas) ? mas : [mas]
|
||||
}
|
||||
}
|
||||
|
||||
describe(`dial self`, () => {
|
||||
let swarmA
|
||||
let peerInfos
|
||||
|
||||
before((done) => createInfos(2, (err, infos) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
const peerA = infos.shift()
|
||||
peerInfos = infos
|
||||
|
||||
peerA.multiaddrs.add('/ip4/127.0.0.1/tcp/9001')
|
||||
peerA.multiaddrs.add(`/ip4/127.0.0.1/tcp/9001/ipfs/${peerA.id.toB58String()}`)
|
||||
peerA.multiaddrs.add(`/ip4/127.0.0.1/tcp/9001/p2p-circuit/ipfs/${peerA.id.toB58String()}`)
|
||||
peerA.multiaddrs.add('/ip4/0.0.0.0/tcp/9001')
|
||||
peerA.multiaddrs.add(`/ip4/0.0.0.0/tcp/9001/ipfs/${peerA.id.toB58String()}`)
|
||||
peerA.multiaddrs.add(`/ip4/0.0.0.0/tcp/9001/p2p-circuit/ipfs/${peerA.id.toB58String()}`)
|
||||
|
||||
swarmA = new Swarm(peerA, new PeerBook())
|
||||
|
||||
swarmA.transport.add('tcp', new MockTransport())
|
||||
|
||||
done()
|
||||
}))
|
||||
|
||||
after((done) => swarmA.stop(done))
|
||||
|
||||
it('node should not be able to dial itself', (done) => {
|
||||
swarmA.dial(swarmA._peerInfo, (err, conn) => {
|
||||
expect(err).to.exist()
|
||||
expect(() => { throw err }).to.throw(/A node cannot dial itself/)
|
||||
expect(conn).to.not.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('node should not be able to dial another peers address that matches its own', (done) => {
|
||||
const peerB = peerInfos.shift()
|
||||
peerB.multiaddrs.add('/ip4/127.0.0.1/tcp/9001')
|
||||
peerB.multiaddrs.add('/ip4/0.0.0.0/tcp/9001')
|
||||
peerB.multiaddrs.add(`/ip4/0.0.0.0/tcp/9001/ipfs/${peerB.id.toB58String()}`)
|
||||
|
||||
swarmA.dial(peerB, (err, conn) => {
|
||||
expect(err).to.exist()
|
||||
expect(err.code).to.eql('CONNECTION_FAILED')
|
||||
expect(conn).to.not.exist()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user