2016-06-15 03:22:09 -04:00
|
|
|
/* 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)
|
2016-06-15 03:22:09 -04:00
|
|
|
const TCP = require('../src')
|
|
|
|
const net = require('net')
|
2019-12-06 09:42:43 +01:00
|
|
|
const os = require('os')
|
|
|
|
const path = require('path')
|
2016-06-15 03:22:09 -04:00
|
|
|
const multiaddr = require('multiaddr')
|
2019-09-16 16:19:47 +01:00
|
|
|
const pipe = require('it-pipe')
|
|
|
|
const { collect, map } = require('streaming-iterables')
|
2018-01-12 12:27:39 +00:00
|
|
|
const isCI = process.env.CI
|
2019-12-06 09:42:43 +01:00
|
|
|
const isWindows = os.platform() === 'win32'
|
|
|
|
|
|
|
|
const skipOnWindows = isWindows ? it.skip : it
|
2016-06-15 03:22:09 -04:00
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
describe('construction', () => {
|
|
|
|
it('requires an upgrader', () => {
|
|
|
|
expect(() => new TCP()).to.throw()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-06-15 03:22:09 -04:00
|
|
|
describe('listen', () => {
|
|
|
|
let tcp
|
2019-12-06 09:42:43 +01:00
|
|
|
let listener
|
2016-06-15 03:22:09 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2019-09-16 16:19:47 +01:00
|
|
|
tcp = new TCP({
|
|
|
|
upgrader: {
|
|
|
|
upgradeOutbound: maConn => maConn,
|
|
|
|
upgradeInbound: maConn => maConn
|
|
|
|
}
|
|
|
|
})
|
2016-06-15 03:22:09 -04:00
|
|
|
})
|
2019-12-06 09:42:43 +01:00
|
|
|
afterEach(async () => {
|
|
|
|
listener && await listener.close()
|
|
|
|
})
|
2016-06-15 03:22:09 -04:00
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
it('close listener with connections, through timeout', async () => {
|
2016-08-31 06:41:34 -04:00
|
|
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
2019-12-06 09:42:43 +01:00
|
|
|
listener = tcp.createListener((conn) => {
|
2019-09-16 16:19:47 +01:00
|
|
|
pipe(conn, conn)
|
2016-06-15 03:22:09 -04:00
|
|
|
})
|
2016-08-05 14:22:18 +02:00
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
await listener.listen(mh)
|
2016-08-05 14:22:18 +02:00
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
const socket1 = net.connect(9090)
|
|
|
|
const socket2 = net.connect(9090)
|
|
|
|
|
|
|
|
socket1.write('Some data that is never handled')
|
|
|
|
socket1.end()
|
|
|
|
socket1.on('error', () => {})
|
|
|
|
socket2.on('error', () => {})
|
|
|
|
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
socket1.on('connect', async () => {
|
|
|
|
await listener.close({ timeout: 100 })
|
|
|
|
resolve()
|
2016-06-15 03:22:09 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-12-06 09:42:43 +01:00
|
|
|
// Windows doesn't support unix paths
|
|
|
|
skipOnWindows('listen on path', async () => {
|
|
|
|
const mh = multiaddr(`/unix${path.resolve(os.tmpdir(), '/tmp/p2pd.sock')}`)
|
|
|
|
|
|
|
|
listener = tcp.createListener((conn) => {})
|
|
|
|
await listener.listen(mh)
|
|
|
|
})
|
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
it('listen on port 0', async () => {
|
2016-06-15 03:22:09 -04:00
|
|
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/0')
|
2019-12-06 09:42:43 +01:00
|
|
|
listener = tcp.createListener((conn) => {})
|
2019-09-16 16:19:47 +01:00
|
|
|
await listener.listen(mh)
|
2016-06-15 03:22:09 -04:00
|
|
|
})
|
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
it('listen on IPv6 addr', async () => {
|
|
|
|
if (isCI) {
|
|
|
|
return
|
|
|
|
}
|
2016-06-15 03:22:09 -04:00
|
|
|
const mh = multiaddr('/ip6/::/tcp/9090')
|
2019-12-06 09:42:43 +01:00
|
|
|
listener = tcp.createListener((conn) => {})
|
2019-09-16 16:19:47 +01:00
|
|
|
await listener.listen(mh)
|
2016-06-15 03:22:09 -04:00
|
|
|
})
|
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
it('listen on any Interface', async () => {
|
2016-06-15 03:22:09 -04:00
|
|
|
const mh = multiaddr('/ip4/0.0.0.0/tcp/9090')
|
2019-12-06 09:42:43 +01:00
|
|
|
listener = tcp.createListener((conn) => {})
|
2019-09-16 16:19:47 +01:00
|
|
|
await listener.listen(mh)
|
2016-06-15 03:22:09 -04:00
|
|
|
})
|
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
it('getAddrs', async () => {
|
2016-06-15 03:22:09 -04:00
|
|
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
2019-12-06 09:42:43 +01:00
|
|
|
listener = tcp.createListener((conn) => {})
|
2019-09-16 16:19:47 +01:00
|
|
|
await listener.listen(mh)
|
|
|
|
|
|
|
|
const multiaddrs = listener.getAddrs()
|
|
|
|
expect(multiaddrs.length).to.equal(1)
|
|
|
|
expect(multiaddrs[0]).to.deep.equal(mh)
|
2016-06-15 03:22:09 -04:00
|
|
|
})
|
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
it('getAddrs on port 0 listen', async () => {
|
2016-06-15 03:22:09 -04:00
|
|
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/0')
|
2019-12-06 09:42:43 +01:00
|
|
|
listener = tcp.createListener((conn) => {})
|
2019-09-16 16:19:47 +01:00
|
|
|
await listener.listen(mh)
|
|
|
|
|
|
|
|
const multiaddrs = listener.getAddrs()
|
|
|
|
expect(multiaddrs.length).to.equal(1)
|
2016-06-15 03:22:09 -04:00
|
|
|
})
|
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
it('getAddrs from listening on 0.0.0.0', async () => {
|
2016-06-15 03:22:09 -04:00
|
|
|
const mh = multiaddr('/ip4/0.0.0.0/tcp/9090')
|
2019-12-06 09:42:43 +01:00
|
|
|
listener = tcp.createListener((conn) => {})
|
2019-09-16 16:19:47 +01: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)
|
2016-06-15 03:22:09 -04:00
|
|
|
})
|
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
it('getAddrs from listening on 0.0.0.0 and port 0', async () => {
|
2016-06-15 03:22:09 -04:00
|
|
|
const mh = multiaddr('/ip4/0.0.0.0/tcp/0')
|
2019-12-06 09:42:43 +01:00
|
|
|
listener = tcp.createListener((conn) => {})
|
2019-09-16 16:19:47 +01: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)
|
2016-06-15 03:22:09 -04:00
|
|
|
})
|
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
it('getAddrs preserves IPFS Id', async () => {
|
2016-06-15 03:22:09 -04:00
|
|
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
2019-12-06 09:42:43 +01:00
|
|
|
listener = tcp.createListener((conn) => {})
|
2019-09-16 16:19:47 +01:00
|
|
|
await listener.listen(mh)
|
|
|
|
|
|
|
|
const multiaddrs = listener.getAddrs()
|
|
|
|
expect(multiaddrs.length).to.equal(1)
|
|
|
|
expect(multiaddrs[0]).to.deep.equal(mh)
|
2016-06-15 03:22:09 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('dial', () => {
|
|
|
|
let tcp
|
|
|
|
let listener
|
|
|
|
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090')
|
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
beforeEach(async () => {
|
|
|
|
tcp = new TCP({
|
|
|
|
upgrader: {
|
|
|
|
upgradeOutbound: maConn => maConn,
|
|
|
|
upgradeInbound: maConn => maConn
|
|
|
|
}
|
|
|
|
})
|
2016-06-15 03:22:09 -04:00
|
|
|
listener = tcp.createListener((conn) => {
|
2019-09-16 16:19:47 +01:00
|
|
|
pipe(
|
2016-08-05 14:22:18 +02:00
|
|
|
conn,
|
2019-09-16 16:19:47 +01:00
|
|
|
map((x) => Buffer.from(x.toString() + '!')),
|
2016-08-05 14:22:18 +02:00
|
|
|
conn
|
|
|
|
)
|
2016-06-15 03:22:09 -04:00
|
|
|
})
|
2019-09-16 16:19:47 +01:00
|
|
|
await listener.listen(ma)
|
2016-06-15 03:22:09 -04:00
|
|
|
})
|
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
afterEach(() => listener.close())
|
2016-06-15 03:22:09 -04:00
|
|
|
|
2019-09-16 16:19:47 +01: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-09-16 16:19:47 +01:00
|
|
|
expect(values).to.eql([Buffer.from('hey!')])
|
2016-06-23 08:35:49 +01:00
|
|
|
})
|
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
it('dial on IPv6', async () => {
|
|
|
|
if (isCI) {
|
|
|
|
return
|
|
|
|
}
|
2018-01-12 12:27:39 +00:00
|
|
|
|
2016-06-15 03:22:09 -04:00
|
|
|
const ma = multiaddr('/ip6/::/tcp/9066')
|
|
|
|
const listener = tcp.createListener((conn) => {
|
2019-09-16 16:19:47 +01:00
|
|
|
pipe(conn, conn)
|
2016-06-15 03:22:09 -04:00
|
|
|
})
|
2019-09-16 16:19:47 +01:00
|
|
|
await listener.listen(ma)
|
2016-06-15 03:22:09 -04:00
|
|
|
|
2019-09-16 16:19:47 +01: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-09-16 16:19:47 +01:00
|
|
|
await listener.close()
|
2016-06-15 03:22:09 -04:00
|
|
|
})
|
|
|
|
|
2019-12-06 09:42:43 +01:00
|
|
|
// Windows doesn't support unix paths
|
|
|
|
skipOnWindows('dial on path', async () => {
|
|
|
|
const ma = multiaddr(`/unix${path.resolve(os.tmpdir(), '/tmp/p2pd.sock')}`)
|
|
|
|
|
|
|
|
const listener = tcp.createListener((conn) => {
|
|
|
|
pipe(conn, conn)
|
|
|
|
})
|
|
|
|
await listener.listen(ma)
|
|
|
|
|
|
|
|
const connection = await tcp.dial(ma)
|
|
|
|
|
|
|
|
const values = await pipe(
|
|
|
|
['hey'],
|
|
|
|
connection,
|
|
|
|
collect
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(values).to.be.eql([Buffer.from('hey')])
|
|
|
|
await listener.close()
|
|
|
|
})
|
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
it('dial and destroy on listener', async () => {
|
|
|
|
let handled
|
|
|
|
const handledPromise = new Promise((resolve) => {
|
|
|
|
handled = resolve
|
|
|
|
})
|
2016-06-26 21:13:39 +01:00
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
const ma = multiaddr('/ip6/::/tcp/0')
|
2016-06-26 21:13:39 +01:00
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
const listener = tcp.createListener(async (conn) => {
|
|
|
|
await pipe(
|
|
|
|
[],
|
|
|
|
conn
|
2016-08-05 14:22:18 +02:00
|
|
|
)
|
2019-09-16 16:19:47 +01:00
|
|
|
handled()
|
2016-06-26 21:13:39 +01:00
|
|
|
})
|
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
await listener.listen(ma)
|
|
|
|
const addrs = listener.getAddrs()
|
|
|
|
await pipe(await tcp.dial(addrs[0]))
|
2016-06-26 21:13:39 +01:00
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
await handledPromise
|
|
|
|
await listener.close()
|
2016-06-26 21:13:39 +01:00
|
|
|
})
|
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
it('dial and destroy on dialer', async () => {
|
|
|
|
if (isCI) {
|
|
|
|
return
|
|
|
|
}
|
2018-01-12 12:27:39 +00:00
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
let handled
|
|
|
|
const handledPromise = new Promise((resolve) => {
|
|
|
|
handled = resolve
|
|
|
|
})
|
2016-06-26 21:13:39 +01:00
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
const ma = multiaddr('/ip6/::/tcp/0')
|
2016-06-26 21:13:39 +01:00
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
const listener = tcp.createListener(async (conn) => {
|
|
|
|
// pull(conn, pull.onEnd(destroyed))
|
|
|
|
await pipe(conn)
|
|
|
|
handled()
|
2016-06-26 21:13:39 +01:00
|
|
|
})
|
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
await listener.listen(ma)
|
|
|
|
const addrs = listener.getAddrs()
|
|
|
|
await pipe(await tcp.dial(addrs[0]))
|
2016-06-26 21:13:39 +01:00
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
await handledPromise
|
|
|
|
await listener.close()
|
2016-06-26 21:13:39 +01:00
|
|
|
})
|
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
it('dial on IPv4 with IPFS Id', async () => {
|
2016-06-15 03:22:09 -04:00
|
|
|
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
2019-09-16 16:19:47 +01:00
|
|
|
const conn = await tcp.dial(ma)
|
2016-08-05 14:22:18 +02:00
|
|
|
|
2019-09-16 16:19:47 +01:00
|
|
|
const res = await pipe(
|
|
|
|
['hey'],
|
2016-08-05 14:22:18 +02:00
|
|
|
conn,
|
2019-09-16 16:19:47 +01:00
|
|
|
collect
|
2016-08-05 14:22:18 +02:00
|
|
|
)
|
2019-09-16 16:19:47 +01:00
|
|
|
expect(res).to.be.eql([Buffer.from('hey!')])
|
2016-06-15 03:22:09 -04:00
|
|
|
})
|
|
|
|
})
|