mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 10:32:14 +00:00
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>
103 lines
3.1 KiB
JavaScript
103 lines
3.1 KiB
JavaScript
/* eslint-env mocha */
|
|
'use strict'
|
|
|
|
const chai = require('chai')
|
|
const dirtyChai = require('dirty-chai')
|
|
const expect = chai.expect
|
|
chai.use(dirtyChai)
|
|
|
|
const PeerBook = require('peer-book')
|
|
const PeerInfo = require('peer-info')
|
|
const PeerId = require('peer-id')
|
|
const MultiAddr = require('multiaddr')
|
|
const TestPeerInfos = require('./test-data/ids.json').infos
|
|
|
|
const getPeerInfo = require('libp2p-switch/get-peer-info')
|
|
|
|
describe('Get peer info', () => {
|
|
let peerBook
|
|
let peerInfoA
|
|
let multiaddrA
|
|
let peerIdA
|
|
|
|
before((done) => {
|
|
peerBook = new PeerBook()
|
|
PeerId.createFromJSON(TestPeerInfos[0].id, (err, id) => {
|
|
peerIdA = id
|
|
peerInfoA = new PeerInfo(peerIdA)
|
|
multiaddrA = MultiAddr('/ipfs/QmdWYwTywvXBeLKWthrVNjkq9SafEDn1PbAZdz4xZW7Jd9')
|
|
peerInfoA.multiaddrs.add(multiaddrA)
|
|
peerBook.put(peerInfoA)
|
|
done(err)
|
|
})
|
|
})
|
|
|
|
it('should be able get peer info from multiaddr', () => {
|
|
const _peerInfo = getPeerInfo(multiaddrA, peerBook)
|
|
expect(peerBook.has(_peerInfo)).to.equal(true)
|
|
expect(peerInfoA).to.deep.equal(_peerInfo)
|
|
})
|
|
|
|
it('should return a new PeerInfo with a multiAddr not in the PeerBook', () => {
|
|
const wrongMultiAddr = MultiAddr('/ipfs/QmckZzdVd72h9QUFuJJpQqhsZqGLwjhh81qSvZ9BhB2FQi')
|
|
const _peerInfo = getPeerInfo(wrongMultiAddr, peerBook)
|
|
expect(PeerInfo.isPeerInfo(_peerInfo)).to.equal(true)
|
|
expect(peerBook.has(_peerInfo)).to.equal(false)
|
|
})
|
|
|
|
it('should be able get peer info from peer id', () => {
|
|
const _peerInfo = getPeerInfo(multiaddrA, peerBook)
|
|
expect(peerBook.has(_peerInfo)).to.equal(true)
|
|
expect(peerInfoA).to.deep.equal(_peerInfo)
|
|
})
|
|
|
|
it('should not be able to get the peer info for a wrong peer id', (done) => {
|
|
PeerId.createFromJSON(TestPeerInfos[1].id, (err, id) => {
|
|
const func = () => {
|
|
getPeerInfo(id, peerBook)
|
|
}
|
|
|
|
expect(func).to.throw('Couldnt get PeerInfo')
|
|
|
|
done(err)
|
|
})
|
|
})
|
|
|
|
it('should add a peerInfo to the book', (done) => {
|
|
PeerId.createFromJSON(TestPeerInfos[1].id, (err, id) => {
|
|
const peerInfo = new PeerInfo(id)
|
|
expect(peerBook.has(peerInfo.id.toB58String())).to.eql(false)
|
|
|
|
expect(getPeerInfo(peerInfo, peerBook)).to.exist()
|
|
expect(peerBook.has(peerInfo.id.toB58String())).to.eql(true)
|
|
done(err)
|
|
})
|
|
})
|
|
|
|
it('should return the most up to date version of the peer', (done) => {
|
|
const ma1 = MultiAddr('/ip4/0.0.0.0/tcp/8080')
|
|
const ma2 = MultiAddr('/ip6/::/tcp/8080')
|
|
PeerId.createFromJSON(TestPeerInfos[1].id, (err, id) => {
|
|
const peerInfo = new PeerInfo(id)
|
|
peerInfo.multiaddrs.add(ma1)
|
|
expect(getPeerInfo(peerInfo, peerBook)).to.exist()
|
|
|
|
const peerInfo2 = new PeerInfo(id)
|
|
peerInfo2.multiaddrs.add(ma2)
|
|
const returnedPeerInfo = getPeerInfo(peerInfo2, peerBook)
|
|
expect(returnedPeerInfo.multiaddrs.toArray()).to.contain.members([
|
|
ma1, ma2
|
|
])
|
|
done(err)
|
|
})
|
|
})
|
|
|
|
it('an invalid peer type should throw an error', () => {
|
|
const func = () => {
|
|
getPeerInfo('/ip4/127.0.0.1/tcp/1234', peerBook)
|
|
}
|
|
|
|
expect(func).to.throw('peer type not recognized')
|
|
})
|
|
})
|