js-libp2p-tcp/test/listen-dial.spec.js

243 lines
5.9 KiB
JavaScript
Raw Permalink Normal View History

/* eslint-env mocha */
'use strict'
2017-03-21 14:27:16 +00:00
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
2019-04-12 15:53:17 +08:00
const TCP = require('../src')
const net = require('net')
const multiaddr = require('multiaddr')
2019-04-12 15:53:17 +08:00
const pipe = require('it-pipe')
const { collect, map } = require('streaming-iterables')
const isCI = process.env.CI
const upgrader = require('./utils/upgrader')
describe('listen', () => {
let tcp
beforeEach(() => {
tcp = new TCP({ upgrader })
})
2019-04-12 15:53:17 +08:00
it('close listener with connections, through timeout', async () => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
const listener = tcp.createListener((conn) => {
2019-04-12 15:53:17 +08:00
pipe(conn, conn)
})
2016-08-05 14:22:18 +02:00
2019-04-12 15:53:17 +08:00
await listener.listen(mh)
const socket1 = net.connect(9090)
const socket2 = net.connect(9090)
2016-08-05 14:22:18 +02:00
2019-04-12 15:53:17 +08:00
socket1.write('Some data that is never handled')
socket1.end()
socket1.on('error', () => {})
socket2.on('error', () => {})
await new Promise((resolve) => {
socket1.on('connect', async () => {
2019-04-29 23:04:12 +08:00
await listener.close({ timeout: 100 })
2019-04-12 15:53:17 +08:00
resolve()
})
})
})
2019-04-12 15:53:17 +08:00
it('listen on port 0', async () => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/0')
const listener = tcp.createListener((conn) => {})
2019-04-12 15:53:17 +08:00
await listener.listen(mh)
await listener.close()
})
2019-04-12 15:53:17 +08:00
it('listen on IPv6 addr', async () => {
if (isCI) {
return
}
const mh = multiaddr('/ip6/::/tcp/9090')
const listener = tcp.createListener((conn) => {})
2019-04-12 15:53:17 +08:00
await listener.listen(mh)
await listener.close()
})
2019-04-12 15:53:17 +08:00
it('listen on any Interface', async () => {
const mh = multiaddr('/ip4/0.0.0.0/tcp/9090')
const listener = tcp.createListener((conn) => {})
2019-04-12 15:53:17 +08:00
await listener.listen(mh)
await listener.close()
})
2019-04-12 15:53:17 +08:00
it('getAddrs', async () => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
const listener = tcp.createListener((conn) => {})
2019-04-12 15:53:17 +08:00
await listener.listen(mh)
const multiaddrs = listener.getAddrs()
expect(multiaddrs.length).to.equal(1)
expect(multiaddrs[0]).to.deep.equal(mh)
await listener.close()
})
2019-04-12 15:53:17 +08:00
it('getAddrs on port 0 listen', async () => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/0')
const listener = tcp.createListener((conn) => {})
2019-04-12 15:53:17 +08:00
await listener.listen(mh)
const multiaddrs = listener.getAddrs()
expect(multiaddrs.length).to.equal(1)
await listener.close()
})
2019-04-12 15:53:17 +08:00
it('getAddrs from listening on 0.0.0.0', async () => {
const mh = multiaddr('/ip4/0.0.0.0/tcp/9090')
const listener = tcp.createListener((conn) => {})
2019-04-12 15:53:17 +08:00
await listener.listen(mh)
const multiaddrs = listener.getAddrs()
expect(multiaddrs.length > 0).to.equal(true)
expect(multiaddrs[0].toString().indexOf('0.0.0.0')).to.equal(-1)
await listener.close()
})
2019-04-12 15:53:17 +08:00
it('getAddrs from listening on 0.0.0.0 and port 0', async () => {
const mh = multiaddr('/ip4/0.0.0.0/tcp/0')
const listener = tcp.createListener((conn) => {})
2019-04-12 15:53:17 +08:00
await listener.listen(mh)
const multiaddrs = listener.getAddrs()
expect(multiaddrs.length > 0).to.equal(true)
expect(multiaddrs[0].toString().indexOf('0.0.0.0')).to.equal(-1)
await listener.close()
})
it('getAddrs preserves IPFS Id', async () => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
const listener = tcp.createListener((conn) => {})
2019-04-12 15:53:17 +08:00
await listener.listen(mh)
const multiaddrs = listener.getAddrs()
expect(multiaddrs.length).to.equal(1)
expect(multiaddrs[0]).to.deep.equal(mh)
2019-04-12 15:53:17 +08:00
await listener.close()
})
})
describe('dial', () => {
let tcp
let listener
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090')
2019-04-12 15:53:17 +08:00
beforeEach(async () => {
tcp = new TCP({ upgrader })
listener = tcp.createListener((conn) => {
2019-04-12 15:53:17 +08:00
pipe(
2016-08-05 14:22:18 +02:00
conn,
2019-04-12 15:53:17 +08:00
map((x) => Buffer.from(x.toString() + '!')),
2016-08-05 14:22:18 +02:00
conn
)
})
2019-04-12 15:53:17 +08:00
await listener.listen(ma)
})
2019-04-12 15:53:17 +08:00
afterEach(() => listener.close())
2019-04-12 15:53:17 +08:00
it('dial on IPv4', async () => {
const values = await pipe(
['hey'],
await tcp.dial(ma),
collect
2016-08-05 14:22:18 +02:00
)
2019-04-12 15:53:17 +08:00
expect(values).to.eql([Buffer.from('hey!')])
})
2019-04-12 15:53:17 +08:00
it('dial on IPv6', async () => {
if (isCI) {
return
}
const ma = multiaddr('/ip6/::/tcp/9066')
const listener = tcp.createListener((conn) => {
2019-04-12 15:53:17 +08:00
pipe(conn, conn)
})
2019-04-12 15:53:17 +08:00
await listener.listen(ma)
2019-04-12 15:53:17 +08:00
const values = await pipe(
['hey'],
await tcp.dial(ma),
collect
)
expect(values).to.be.eql([Buffer.from('hey')])
2016-08-05 14:22:18 +02:00
2019-04-12 15:53:17 +08:00
await listener.close()
})
2019-04-12 15:53:17 +08:00
it('dial and destroy on listener', async () => {
let handled
const handledPromise = new Promise((resolve) => {
handled = resolve
})
2019-04-29 23:04:12 +08:00
const ma = multiaddr('/ip6/::/tcp/0')
2019-04-12 15:53:17 +08:00
const listener = tcp.createListener(async (conn) => {
await pipe(
[],
conn
2016-08-05 14:22:18 +02:00
)
2019-04-12 15:53:17 +08:00
handled()
})
2019-04-12 15:53:17 +08:00
await listener.listen(ma)
2019-04-29 23:04:12 +08:00
const addrs = listener.getAddrs()
await pipe(await tcp.dial(addrs[0]))
2019-04-12 15:53:17 +08:00
await handledPromise
await listener.close()
})
2019-04-12 15:53:17 +08:00
it('dial and destroy on dialer', async () => {
if (isCI) {
return
}
2019-04-12 15:53:17 +08:00
let handled
const handledPromise = new Promise((resolve) => {
handled = resolve
})
2019-04-29 23:04:12 +08:00
const ma = multiaddr('/ip6/::/tcp/0')
2019-04-12 15:53:17 +08:00
const listener = tcp.createListener(async (conn) => {
// pull(conn, pull.onEnd(destroyed))
await pipe(conn)
handled()
})
2019-04-12 15:53:17 +08:00
await listener.listen(ma)
2019-04-29 23:04:12 +08:00
const addrs = listener.getAddrs()
await pipe(await tcp.dial(addrs[0]))
2019-04-12 15:53:17 +08:00
await handledPromise
await listener.close()
})
2019-04-12 15:53:17 +08:00
it('dial on IPv4 with IPFS Id', async () => {
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
2019-04-12 15:53:17 +08:00
const conn = await tcp.dial(ma)
2016-08-05 14:22:18 +02:00
2019-04-12 15:53:17 +08:00
const res = await pipe(
['hey'],
2016-08-05 14:22:18 +02:00
conn,
2019-04-12 15:53:17 +08:00
collect
2016-08-05 14:22:18 +02:00
)
2019-04-12 15:53:17 +08:00
expect(res).to.be.eql([Buffer.from('hey!')])
})
})