diff --git a/test/connection-wrap.spec.js b/test/connection-wrap.spec.js new file mode 100644 index 0000000..aceb2c6 --- /dev/null +++ b/test/connection-wrap.spec.js @@ -0,0 +1,121 @@ +/* eslint-env mocha */ +'use strict' + +const pull = require('pull-stream') +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const expect = chai.expect +chai.use(dirtyChai) +const TCP = require('../src') +const multiaddr = require('multiaddr') +const Connection = require('interface-connection').Connection + +describe('Connection Wrap', () => { + let tcp + let listener + const ma = multiaddr('/ip4/127.0.0.1/tcp/9090') + + beforeEach((done) => { + tcp = new TCP() + listener = tcp.createListener((conn) => { + pull(conn, conn) + }) + listener.on('listening', done) + listener.listen(ma) + }) + + afterEach((done) => { + listener.close(done) + }) + + it('simple wrap', (done) => { + const conn = tcp.dial(ma) + conn.setPeerInfo('peerInfo') + const connWrap = new Connection(conn) + pull( + pull.values(['hey']), + connWrap, + pull.collect((err, chunks) => { + expect(err).to.not.exist() + expect(chunks).to.be.eql([Buffer.from('hey')]) + + connWrap.getPeerInfo((err, peerInfo) => { + expect(err).to.not.exist() + expect(peerInfo).to.equal('peerInfo') + done() + }) + }) + ) + }) + + it('buffer wrap', (done) => { + const conn = tcp.dial(ma) + const connWrap = new Connection() + pull( + pull.values(['hey']), + connWrap, + pull.collect((err, chunks) => { + expect(err).to.not.exist() + expect(chunks).to.be.eql([Buffer.from('hey')]) + done() + }) + ) + + connWrap.setInnerConn(conn) + }) + + it('overload wrap', (done) => { + const conn = tcp.dial(ma) + const connWrap = new Connection(conn) + connWrap.getPeerInfo = (callback) => { + callback(null, 'none') + } + conn.getPeerInfo((err, peerInfo) => { + expect(err).to.exist() + }) + connWrap.getPeerInfo((err, peerInfo) => { + expect(err).to.not.exist() + expect(peerInfo).to.equal('none') + }) + pull( + pull.values(['hey']), + connWrap, + pull.collect((err, chunks) => { + expect(err).to.not.exist() + expect(chunks).to.be.eql([Buffer.from('hey')]) + done() + }) + ) + }) + + it('dial error', (done) => { + tcp.dial(multiaddr('/ip4/999.0.0.1/tcp/1234'), (err) => { + expect(err).to.exist() + done() + }) + }) + + it('matryoshka wrap', (done) => { + const conn = tcp.dial(ma) + const connWrap1 = new Connection(conn) + const connWrap2 = new Connection(connWrap1) + const connWrap3 = new Connection(connWrap2) + + conn.getPeerInfo = (callback) => { + callback(null, 'inner doll') + } + pull( + pull.values(['hey']), + connWrap3, + pull.collect((err, chunks) => { + expect(err).to.not.exist() + expect(chunks).to.eql([Buffer.from('hey')]) + connWrap3.getPeerInfo((err, peerInfo) => { + expect(err).to.not.exist() + expect(peerInfo).to.equal('inner doll') + done() + }) + }) + ) + }) +}) diff --git a/test/connection.spec.js b/test/connection.spec.js new file mode 100644 index 0000000..5100cb1 --- /dev/null +++ b/test/connection.spec.js @@ -0,0 +1,111 @@ +/* eslint-env mocha */ +'use strict' + +const pull = require('pull-stream') +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const expect = chai.expect +chai.use(dirtyChai) +const TCP = require('../src') +const multiaddr = require('multiaddr') + +describe('valid Connection', () => { + let tcp + + beforeEach(() => { + tcp = new TCP() + }) + + const ma = multiaddr('/ip4/127.0.0.1/tcp/9090') + + it('get observed addrs', (done) => { + let dialerObsAddrs + + const listener = tcp.createListener((conn) => { + expect(conn).to.exist() + conn.getObservedAddrs((err, addrs) => { + expect(err).to.not.exist() + dialerObsAddrs = addrs + pull(pull.empty(), conn) + }) + }) + + listener.listen(ma, () => { + const conn = tcp.dial(ma) + pull( + conn, + pull.onEnd(endHandler) + ) + + function endHandler () { + conn.getObservedAddrs((err, addrs) => { + expect(err).to.not.exist() + pull(pull.empty(), conn) + closeAndAssert(listener, addrs) + }) + } + + function closeAndAssert (listener, addrs) { + listener.close(() => { + expect(addrs[0]).to.deep.equal(ma) + expect(dialerObsAddrs.length).to.equal(1) + done() + }) + } + }) + }) + + it('get Peer Info', (done) => { + const listener = tcp.createListener((conn) => { + expect(conn).to.exist() + conn.getPeerInfo((err, peerInfo) => { + expect(err).to.exist() + expect(peerInfo).to.not.exist() + pull(pull.empty(), conn) + }) + }) + + listener.listen(ma, () => { + const conn = tcp.dial(ma) + + pull(conn, pull.onEnd(endHandler)) + + function endHandler () { + conn.getPeerInfo((err, peerInfo) => { + expect(err).to.exist() + expect(peerInfo).to.not.exist() + + listener.close(done) + }) + } + }) + }) + + it('set Peer Info', (done) => { + const listener = tcp.createListener((conn) => { + expect(conn).to.exist() + conn.setPeerInfo('batatas') + conn.getPeerInfo((err, peerInfo) => { + expect(err).to.not.exist() + expect(peerInfo).to.equal('batatas') + pull(pull.empty(), conn) + }) + }) + + listener.listen(ma, () => { + const conn = tcp.dial(ma) + + pull(conn, pull.onEnd(endHandler)) + + function endHandler () { + conn.setPeerInfo('arroz') + conn.getPeerInfo((err, peerInfo) => { + expect(err).to.not.exist() + expect(peerInfo).to.equal('arroz') + + listener.close(done) + }) + } + }) + }) +}) diff --git a/test/constructor.spec.js b/test/constructor.spec.js new file mode 100644 index 0000000..6183ad1 --- /dev/null +++ b/test/constructor.spec.js @@ -0,0 +1,15 @@ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const expect = chai.expect +chai.use(dirtyChai) +const TCP = require('../src') + +describe('Constructor', () => { + it('create an instance', () => { + const tcp = new TCP() + expect(tcp).to.exist() + }) +}) diff --git a/test/filter.spec.js b/test/filter.spec.js new file mode 100644 index 0000000..6047305 --- /dev/null +++ b/test/filter.spec.js @@ -0,0 +1,40 @@ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const expect = chai.expect +chai.use(dirtyChai) +const TCP = require('../src') +const multiaddr = require('multiaddr') + +describe('filter addrs', () => { + let tcp + + before(() => { + tcp = new TCP() + }) + + it('filter valid addrs for this transport', () => { + const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090') + const mh2 = multiaddr('/ip4/127.0.0.1/udp/9090') + const mh3 = multiaddr('/ip4/127.0.0.1/tcp/9090/http') + const mh4 = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw') + const mh5 = multiaddr('/ip4/127.0.0.1/tcp/9090/http/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw') + const mh6 = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw' + + '/p2p-circuit/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw') + + const valid = tcp.filter([mh1, mh2, mh3, mh4, mh5, mh6]) + expect(valid.length).to.equal(2) + expect(valid[0]).to.deep.equal(mh1) + expect(valid[1]).to.deep.equal(mh4) + }) + + it('filter a single addr for this transport', () => { + const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090') + + const valid = tcp.filter(mh1) + expect(valid.length).to.equal(1) + expect(valid[0]).to.deep.equal(mh1) + }) +}) diff --git a/test/index.spec.js b/test/index.spec.js index b1efc7a..6f15f32 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -9,7 +9,6 @@ chai.use(dirtyChai) const TCP = require('../src') const net = require('net') const multiaddr = require('multiaddr') -const Connection = require('interface-connection').Connection const isCI = process.env.CI describe('instantiate the transport', () => { @@ -267,249 +266,7 @@ describe('dial', () => { }) }) -describe('filter addrs', () => { - let tcp - - before(() => { - tcp = new TCP() - }) - - it('filter valid addrs for this transport', () => { - const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090') - const mh2 = multiaddr('/ip4/127.0.0.1/udp/9090') - const mh3 = multiaddr('/ip4/127.0.0.1/tcp/9090/http') - const mh4 = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw') - const mh5 = multiaddr('/ip4/127.0.0.1/tcp/9090/http/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw') - const mh6 = multiaddr('/ip4/127.0.0.1/tcp/9090/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw' + - '/p2p-circuit/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw') - - const valid = tcp.filter([mh1, mh2, mh3, mh4, mh5, mh6]) - expect(valid.length).to.equal(2) - expect(valid[0]).to.deep.equal(mh1) - expect(valid[1]).to.deep.equal(mh4) - }) - - it('filter a single addr for this transport', () => { - const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090') - - const valid = tcp.filter(mh1) - expect(valid.length).to.equal(1) - expect(valid[0]).to.deep.equal(mh1) - }) -}) - -describe('valid Connection', () => { - let tcp - - beforeEach(() => { - tcp = new TCP() - }) - - const ma = multiaddr('/ip4/127.0.0.1/tcp/9090') - - it('get observed addrs', (done) => { - let dialerObsAddrs - - const listener = tcp.createListener((conn) => { - expect(conn).to.exist() - conn.getObservedAddrs((err, addrs) => { - expect(err).to.not.exist() - dialerObsAddrs = addrs - pull(pull.empty(), conn) - }) - }) - - listener.listen(ma, () => { - const conn = tcp.dial(ma) - pull( - conn, - pull.onEnd(endHandler) - ) - - function endHandler () { - conn.getObservedAddrs((err, addrs) => { - expect(err).to.not.exist() - pull(pull.empty(), conn) - closeAndAssert(listener, addrs) - }) - } - - function closeAndAssert (listener, addrs) { - listener.close(() => { - expect(addrs[0]).to.deep.equal(ma) - expect(dialerObsAddrs.length).to.equal(1) - done() - }) - } - }) - }) - - it('get Peer Info', (done) => { - const listener = tcp.createListener((conn) => { - expect(conn).to.exist() - conn.getPeerInfo((err, peerInfo) => { - expect(err).to.exist() - expect(peerInfo).to.not.exist() - pull(pull.empty(), conn) - }) - }) - - listener.listen(ma, () => { - const conn = tcp.dial(ma) - - pull(conn, pull.onEnd(endHandler)) - - function endHandler () { - conn.getPeerInfo((err, peerInfo) => { - expect(err).to.exist() - expect(peerInfo).to.not.exist() - - listener.close(done) - }) - } - }) - }) - - it('set Peer Info', (done) => { - const listener = tcp.createListener((conn) => { - expect(conn).to.exist() - conn.setPeerInfo('batatas') - conn.getPeerInfo((err, peerInfo) => { - expect(err).to.not.exist() - expect(peerInfo).to.equal('batatas') - pull(pull.empty(), conn) - }) - }) - - listener.listen(ma, () => { - const conn = tcp.dial(ma) - - pull(conn, pull.onEnd(endHandler)) - - function endHandler () { - conn.setPeerInfo('arroz') - conn.getPeerInfo((err, peerInfo) => { - expect(err).to.not.exist() - expect(peerInfo).to.equal('arroz') - - listener.close(done) - }) - } - }) - }) -}) - describe.skip('turbolence', () => { it('dialer - emits error on the other end is terminated abruptly', (done) => {}) it('listener - emits error on the other end is terminated abruptly', (done) => {}) }) - -describe('Connection wrap', () => { - let tcp - let listener - const ma = multiaddr('/ip4/127.0.0.1/tcp/9090') - - beforeEach((done) => { - tcp = new TCP() - listener = tcp.createListener((conn) => { - pull(conn, conn) - }) - listener.on('listening', done) - listener.listen(ma) - }) - - afterEach((done) => { - listener.close(done) - }) - - it('simple wrap', (done) => { - const conn = tcp.dial(ma) - conn.setPeerInfo('peerInfo') - const connWrap = new Connection(conn) - pull( - pull.values(['hey']), - connWrap, - pull.collect((err, chunks) => { - expect(err).to.not.exist() - expect(chunks).to.be.eql([Buffer.from('hey')]) - - connWrap.getPeerInfo((err, peerInfo) => { - expect(err).to.not.exist() - expect(peerInfo).to.equal('peerInfo') - done() - }) - }) - ) - }) - - it('buffer wrap', (done) => { - const conn = tcp.dial(ma) - const connWrap = new Connection() - pull( - pull.values(['hey']), - connWrap, - pull.collect((err, chunks) => { - expect(err).to.not.exist() - expect(chunks).to.be.eql([Buffer.from('hey')]) - done() - }) - ) - - connWrap.setInnerConn(conn) - }) - - it('overload wrap', (done) => { - const conn = tcp.dial(ma) - const connWrap = new Connection(conn) - connWrap.getPeerInfo = (callback) => { - callback(null, 'none') - } - conn.getPeerInfo((err, peerInfo) => { - expect(err).to.exist() - }) - connWrap.getPeerInfo((err, peerInfo) => { - expect(err).to.not.exist() - expect(peerInfo).to.equal('none') - }) - pull( - pull.values(['hey']), - connWrap, - pull.collect((err, chunks) => { - expect(err).to.not.exist() - expect(chunks).to.be.eql([Buffer.from('hey')]) - done() - }) - ) - }) - - it('dial error', (done) => { - tcp.dial(multiaddr('/ip4/999.0.0.1/tcp/1234'), (err) => { - expect(err).to.exist() - done() - }) - }) - - it('matryoshka wrap', (done) => { - const conn = tcp.dial(ma) - const connWrap1 = new Connection(conn) - const connWrap2 = new Connection(connWrap1) - const connWrap3 = new Connection(connWrap2) - - conn.getPeerInfo = (callback) => { - callback(null, 'inner doll') - } - pull( - pull.values(['hey']), - connWrap3, - pull.collect((err, chunks) => { - expect(err).to.not.exist() - expect(chunks).to.be.eql([Buffer.from('hey')]) - connWrap3.getPeerInfo((err, peerInfo) => { - expect(err).to.not.exist() - expect(peerInfo).to.equal('inner doll') - done() - }) - }) - ) - }) -}) diff --git a/test/turbolence.spec.js b/test/turbolence.spec.js new file mode 100644 index 0000000..e54bae0 --- /dev/null +++ b/test/turbolence.spec.js @@ -0,0 +1,16 @@ +/* eslint-env mocha */ +'use strict' + +const chai = require('chai') +const dirtyChai = require('dirty-chai') +const expect = chai.expect +chai.use(dirtyChai) +// const TCP = require('../src') + +describe.skip('turbolence', () => { + it('dialer - emits error on the other end is terminated abruptly', (done) => { + expect('ok').to.equal('ok') + }) + + it('listener - emits error on the other end is terminated abruptly', (done) => {}) +})