diff --git a/src/commands/registration/eprt.js b/src/commands/registration/eprt.js index d36d821..4ced2ef 100644 --- a/src/commands/registration/eprt.js +++ b/src/commands/registration/eprt.js @@ -9,11 +9,11 @@ const FAMILY = { module.exports = { directive: 'EPRT', handler: function ({command} = {}) { - this.connector = new ActiveConnector(this); - const [protocol, ip, port] = _.compact(command.arg.split('|')); + const [, protocol, ip, port] = _.chain(command).get('arg', '').split('|').value(); const family = FAMILY[protocol]; - if (!family) return this.reply(502, 'Unknown network protocol'); + if (!family) return this.reply(504, 'Unknown network protocol'); + this.connector = new ActiveConnector(this); return this.connector.setupConnection(ip, port, family) .then(() => this.reply(200)); }, diff --git a/src/commands/registry.js b/src/commands/registry.js index 8dbc0df..c0b5d5a 100644 --- a/src/commands/registry.js +++ b/src/commands/registry.js @@ -36,7 +36,9 @@ const commands = [ require('./registration/type'), require('./registration/user'), require('./registration/pbsz'), - require('./registration/prot') + require('./registration/prot'), + require('./registration/eprt'), + require('./registration/epsv') ]; const registry = commands.reduce((result, cmd) => { diff --git a/test/commands/registration/eprt.spec.js b/test/commands/registration/eprt.spec.js new file mode 100644 index 0000000..52f9d88 --- /dev/null +++ b/test/commands/registration/eprt.spec.js @@ -0,0 +1,60 @@ +const when = require('when'); +const {expect} = require('chai'); +const sinon = require('sinon'); + +const ActiveConnector = require('../../../src/connector/active'); + +const CMD = 'EPRT'; +describe(CMD, function () { + let sandbox; + const mockClient = { + reply: () => when.resolve() + }; + const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient); + + beforeEach(() => { + sandbox = sinon.sandbox.create(); + + sandbox.spy(mockClient, 'reply'); + sandbox.stub(ActiveConnector.prototype, 'setupConnection').resolves(); + }); + afterEach(() => { + sandbox.restore(); + }); + + it('// unsuccessful | no argument', () => { + return cmdFn() + .then(() => { + expect(mockClient.reply.args[0][0]).to.equal(504); + }); + }); + + it('// unsuccessful | invalid argument', () => { + return cmdFn({command: {arg: 'blah'}}) + .then(() => { + expect(mockClient.reply.args[0][0]).to.equal(504); + }); + }); + + it('// successful IPv4', () => { + return cmdFn({command: {arg: '|1|192.168.0.100|35286|'}}) + .then(() => { + const [ip, port, family] = ActiveConnector.prototype.setupConnection.args[0]; + expect(mockClient.reply.args[0][0]).to.equal(200); + expect(ip).to.equal('192.168.0.100'); + expect(port).to.equal('35286'); + expect(family).to.equal(4); + }); + }); + + it('// successful IPv6', () => { + return cmdFn({command: {arg: '|2|8536:933f:e7f3:3e91:6dc1:e8c6:8482:7b23|35286|'}}) + .then(() => { + const [ip, port, family] = ActiveConnector.prototype.setupConnection.args[0]; + expect(mockClient.reply.args[0][0]).to.equal(200); + expect(ip).to.equal('8536:933f:e7f3:3e91:6dc1:e8c6:8482:7b23'); + expect(port).to.equal('35286'); + expect(family).to.equal(6); + }); + }); +}); diff --git a/test/commands/registration/epsv.spec.js b/test/commands/registration/epsv.spec.js new file mode 100644 index 0000000..76387da --- /dev/null +++ b/test/commands/registration/epsv.spec.js @@ -0,0 +1,35 @@ +const when = require('when'); +const {expect} = require('chai'); +const sinon = require('sinon'); + +const PassiveConnector = require('../../../src/connector/passive'); + +const CMD = 'EPSV'; +describe(CMD, function () { + let sandbox; + const mockClient = { + reply: () => when.resolve() + }; + const cmdFn = require(`../../../src/commands/registration/${CMD.toLowerCase()}`).handler.bind(mockClient); + + beforeEach(() => { + sandbox = sinon.sandbox.create(); + + sandbox.stub(mockClient, 'reply').resolves(); + sandbox.stub(PassiveConnector.prototype, 'setupServer').resolves({ + address: () => ({port: 12345}) + }); + }); + afterEach(() => { + sandbox.restore(); + }); + + it('// successful IPv4', () => { + return cmdFn() + .then(() => { + const [code, message] = mockClient.reply.args[0]; + expect(code).to.equal(229); + expect(message).to.equal('EPSV OK (|||12345|)'); + }); + }); +});