|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const chai = require('chai') |
| 5 | +const dirtyChai = require('dirty-chai') |
| 6 | +const expect = chai.expect |
| 7 | +chai.use(dirtyChai) |
| 8 | +const Multiaddr = require('multiaddr') |
| 9 | +const PeerInfo = require('peer-info') |
| 10 | + |
| 11 | +const TransportManager = require('../src/transport') |
| 12 | + |
| 13 | +describe('Transport Manager', () => { |
| 14 | + describe('dialables', () => { |
| 15 | + let peerInfo |
| 16 | + const dialAllTransport = { filter: addrs => addrs } |
| 17 | + |
| 18 | + beforeEach(done => { |
| 19 | + PeerInfo.create((err, info) => { |
| 20 | + if (err) return done(err) |
| 21 | + peerInfo = info |
| 22 | + done() |
| 23 | + }) |
| 24 | + }) |
| 25 | + |
| 26 | + it('should return all transport addresses when peer info has 0 addrs', () => { |
| 27 | + const queryAddrs = [ |
| 28 | + '/ip4/127.0.0.1/tcp/4002', |
| 29 | + '/ip4/192.168.0.3/tcp/4002', |
| 30 | + '/ip6/::1/tcp/4001' |
| 31 | + ].map(a => Multiaddr(a)) |
| 32 | + |
| 33 | + const dialableAddrs = TransportManager.dialables(dialAllTransport, queryAddrs, peerInfo) |
| 34 | + |
| 35 | + expect(dialableAddrs).to.have.length(queryAddrs.length) |
| 36 | + |
| 37 | + queryAddrs.forEach(qa => { |
| 38 | + expect(dialableAddrs.some(da => da.equals(qa))).to.be.true() |
| 39 | + }) |
| 40 | + }) |
| 41 | + |
| 42 | + it('should return all transport addresses when we pass no peer info', () => { |
| 43 | + const queryAddrs = [ |
| 44 | + '/ip4/127.0.0.1/tcp/4002', |
| 45 | + '/ip4/192.168.0.3/tcp/4002', |
| 46 | + '/ip6/::1/tcp/4001' |
| 47 | + ].map(a => Multiaddr(a)) |
| 48 | + |
| 49 | + const dialableAddrs = TransportManager.dialables(dialAllTransport, queryAddrs) |
| 50 | + |
| 51 | + expect(dialableAddrs).to.have.length(queryAddrs.length) |
| 52 | + |
| 53 | + queryAddrs.forEach(qa => { |
| 54 | + expect(dialableAddrs.some(da => da.equals(qa))).to.be.true() |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should filter our addresses', () => { |
| 59 | + const queryAddrs = [ |
| 60 | + '/ip4/127.0.0.1/tcp/4002', |
| 61 | + '/ip4/192.168.0.3/tcp/4002', |
| 62 | + '/ip6/::1/tcp/4001' |
| 63 | + ].map(a => Multiaddr(a)) |
| 64 | + |
| 65 | + const ourAddrs = [ |
| 66 | + '/ip4/127.0.0.1/tcp/4002', |
| 67 | + '/ip4/192.168.0.3/tcp/4002' |
| 68 | + ] |
| 69 | + |
| 70 | + ourAddrs.forEach(a => peerInfo.multiaddrs.add(a)) |
| 71 | + |
| 72 | + const dialableAddrs = TransportManager.dialables(dialAllTransport, queryAddrs, peerInfo) |
| 73 | + |
| 74 | + expect(dialableAddrs).to.have.length(1) |
| 75 | + expect(dialableAddrs[0].toString()).to.equal('/ip6/::1/tcp/4001') |
| 76 | + }) |
| 77 | + |
| 78 | + it('should filter our addresses with peer ID suffix', () => { |
| 79 | + const queryAddrs = [ |
| 80 | + '/ip4/127.0.0.1/tcp/4002/ipfs/QmebzNV1kSzLfaYpSZdShuiABNUxoKT1vJmCdxM2iWsM2j', |
| 81 | + '/ip4/192.168.0.3/tcp/4002', |
| 82 | + '/ip6/::1/tcp/4001' |
| 83 | + ].map(a => Multiaddr(a)) |
| 84 | + |
| 85 | + const ourAddrs = [ |
| 86 | + `/ip4/127.0.0.1/tcp/4002`, |
| 87 | + `/ip4/192.168.0.3/tcp/4002/ipfs/${peerInfo.id.toB58String()}` |
| 88 | + ] |
| 89 | + |
| 90 | + ourAddrs.forEach(a => peerInfo.multiaddrs.add(a)) |
| 91 | + |
| 92 | + const dialableAddrs = TransportManager.dialables(dialAllTransport, queryAddrs, peerInfo) |
| 93 | + |
| 94 | + expect(dialableAddrs).to.have.length(1) |
| 95 | + expect(dialableAddrs[0].toString()).to.equal('/ip6/::1/tcp/4001') |
| 96 | + }) |
| 97 | + |
| 98 | + it('should filter our addresses over relay/rendezvous', () => { |
| 99 | + const peerId = peerInfo.id.toB58String() |
| 100 | + const queryAddrs = [ |
| 101 | + `/p2p-circuit/ipfs/${peerId}`, |
| 102 | + `/p2p-circuit/ip4/127.0.0.1/tcp/4002`, |
| 103 | + `/p2p-circuit/ip4/192.168.0.3/tcp/4002`, |
| 104 | + `/p2p-circuit/ip4/127.0.0.1/tcp/4002/ipfs/${peerId}`, |
| 105 | + `/p2p-circuit/ip4/192.168.0.3/tcp/4002/ipfs/${peerId}`, |
| 106 | + `/p2p-circuit/ip4/127.0.0.1/tcp/4002/ipfs/QmebzNV1kSzLfaYpSZdShuiABNUxoKT1vJmCdxM2iWsM2j`, |
| 107 | + `/p2p-circuit/ip4/192.168.0.3/tcp/4002/ipfs/QmebzNV1kSzLfaYpSZdShuiABNUxoKT1vJmCdxM2iWsM2j`, |
| 108 | + `/p2p-webrtc-star/ipfs/${peerId}`, |
| 109 | + `/p2p-websocket-star/ipfs/${peerId}`, |
| 110 | + `/p2p-stardust/ipfs/${peerId}`, |
| 111 | + '/ip6/::1/tcp/4001' |
| 112 | + ].map(a => Multiaddr(a)) |
| 113 | + |
| 114 | + const ourAddrs = [ |
| 115 | + `/ip4/127.0.0.1/tcp/4002`, |
| 116 | + `/ip4/192.168.0.3/tcp/4002/ipfs/${peerInfo.id.toB58String()}` |
| 117 | + ] |
| 118 | + |
| 119 | + ourAddrs.forEach(a => peerInfo.multiaddrs.add(a)) |
| 120 | + |
| 121 | + const dialableAddrs = TransportManager.dialables(dialAllTransport, queryAddrs, peerInfo) |
| 122 | + |
| 123 | + expect(dialableAddrs).to.have.length(1) |
| 124 | + expect(dialableAddrs[0].toString()).to.equal('/ip6/::1/tcp/4001') |
| 125 | + }) |
| 126 | + }) |
| 127 | +}) |
0 commit comments