js-libp2p/test/get-peer-info.spec.js
Jacob Heun b294301456
refactor: add core modules to libp2p (#400)
* refactor: add js-libp2p-connection-manager to repo

Co-authored-by: David Dias <daviddias.p@gmail.com>
Co-authored-by: Jacob Heun <jacobheun@gmail.com>
Co-authored-by: Pedro Teixeira <i@pgte.me>
Co-authored-by: Vasco Santos <vasco.santos@ua.pt>

* test(conn-mgr): only run in node

* refactor: add js-libp2p-identify to repo

Co-authored-by: David Dias <daviddias.p@gmail.com>
Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com>
Co-authored-by: Hugo Dias <hugomrdias@gmail.com>
Co-authored-by: Jacob Heun <jacobheun@gmail.com>
Co-authored-by: Maciej Krüger <mkg20001@gmail.com>
Co-authored-by: Richard Littauer <richard.littauer@gmail.com>
Co-authored-by: Vasco Santos <vasco.santos@moxy.studio>
Co-authored-by: Yusef Napora <yusef@protocol.ai>
Co-authored-by: ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>

* refactor: add libp2p-pnet to repo

Co-authored-by: Jacob Heun <jacobheun@gmail.com>
Co-authored-by: Vasco Santos <vasco.santos@moxy.studio>

* refactor: add libp2p-ping to repo

Co-authored-by: David Dias <daviddias.p@gmail.com>
Co-authored-by: Francisco Baio Dias <xicombd@gmail.com>
Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com>
Co-authored-by: Hugo Dias <mail@hugodias.me>
Co-authored-by: Jacob Heun <jacobheun@gmail.com>
Co-authored-by: João Antunes <j.goncalo.antunes@gmail.com>
Co-authored-by: Richard Littauer <richard.littauer@gmail.com>
Co-authored-by: Vasco Santos <vasco.santos@moxy.studio>
Co-authored-by: Vasco Santos <vasco.santos@ua.pt>
Co-authored-by: ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>

* refactor: add libp2p-circuit to repo

Co-authored-by: David Dias <daviddias.p@gmail.com>
Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com>
Co-authored-by: Hugo Dias <mail@hugodias.me>
Co-authored-by: Jacob Heun <jacobheun@gmail.com>
Co-authored-by: Maciej Krüger <mkg20001@gmail.com>
Co-authored-by: Oli Evans <oli@tableflip.io>
Co-authored-by: Pedro Teixeira <i@pgte.me>
Co-authored-by: Vasco Santos <vasco.santos@ua.pt>
Co-authored-by: Victor Bjelkholm <victorbjelkholm@gmail.com>
Co-authored-by: Yusef Napora <yusef@napora.org>
Co-authored-by: dirkmc <dirk@mccormick.cx>

* test(switch): avoid using instanceof

* chore(switch): update bignumber dep

* refactor(circuit): clean up tests

* refactor(switch): consolidate get peer utils

* test(identify): do deep checks of addresses

* test(identify): bump timeout for identify test

* test(switch): tidy up limit dialer test

* refactor(switch): remove redundant circuit tests

* chore: add coverage script

* refactor(circuit): consolidate get peer info

* docs: reference original repositories in each sub readme

* docs: fix comment

* refactor: clean up sub package.json files and readmes
2019-08-16 17:30:03 +02:00

128 lines
3.8 KiB
JavaScript

/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const PeerBook = require('peer-book')
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const MultiAddr = require('multiaddr')
const TestPeerInfos = require('./switch/test-data/ids.json').infos
const { getPeerInfo, getPeerInfoRemote } = require('../src/get-peer-info')
describe('Get Peer Info', () => {
describe('getPeerInfo', () => {
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)
})
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 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', () => {
let error
try {
getPeerInfo('/ip4/127.0.0.1/tcp/1234', peerBook)
} catch (err) {
error = err
}
expect(error.code).to.eql('ERR_INVALID_MULTIADDR')
})
})
describe('getPeerInfoRemote', () => {
it('should callback with error for invalid string multiaddr', async () => {
let error
try {
await getPeerInfoRemote('INVALID MULTIADDR')
} catch (err) {
error = err
}
expect(error.code).to.eql('ERR_INVALID_PEER_TYPE')
})
it('should callback with error for invalid non-peer multiaddr', async () => {
let error
try {
await getPeerInfoRemote('/ip4/8.8.8.8/tcp/1080')
} catch (err) {
error = err
}
expect(error.code).to.eql('ERR_INVALID_PEER_TYPE')
})
it('should callback with error for invalid non-peer multiaddr', async () => {
let error
try {
await getPeerInfoRemote(undefined)
} catch (err) {
error = err
}
expect(error.code).to.eql('ERR_INVALID_PEER_TYPE')
})
})
})