From 08ff59cf3cdc45bdf532f7ef3130c1750abb3cea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20F=2E=20Romaniello?= Date: Tue, 3 Apr 2012 10:31:17 -0300 Subject: [PATCH 1/3] fix LIST was not returning 'h' instead of dash for files --- .gitignore | 1 + fixture/jose/data.txt | 1 + ftpd.js | 2 +- package.json | 14 +++++++++++++ specs/list.specs.js | 48 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 fixture/jose/data.txt create mode 100644 package.json create mode 100644 specs/list.specs.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dbf0821 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/* \ No newline at end of file diff --git a/fixture/jose/data.txt b/fixture/jose/data.txt new file mode 100644 index 0000000..0d5acab --- /dev/null +++ b/fixture/jose/data.txt @@ -0,0 +1 @@ +hola! \ No newline at end of file diff --git a/ftpd.js b/ftpd.js index bec72f9..b6bfef7 100755 --- a/ftpd.js +++ b/ftpd.js @@ -294,7 +294,7 @@ function createServer(host, sandbox) { for (var i = 0; i < files.length; i++) { var file = files[ i ]; var s = fs.statSync(path + file); - var line = s.isDirectory() ? 'd' : 'h'; + var line = s.isDirectory() ? 'd' : '-'; if (i > 0) pasvconn.write("\r\n"); line += (0400 & s.mode) ? 'r' : '-'; line += (0200 & s.mode) ? 'w' : '-'; diff --git a/package.json b/package.json new file mode 100644 index 0000000..cca714e --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "ftpd", + "version": "0.0.1", + "dependencies": {}, + "devDependencies": { + "async": "~0.1.15", + "mocha": "~0.12.1", + "should": "~0.5.1", + "jsftp": "*" + }, + "scripts": { + "test" : "mocha specs/* --globals commandArg" + } +} diff --git a/specs/list.specs.js b/specs/list.specs.js new file mode 100644 index 0000000..b49086a --- /dev/null +++ b/specs/list.specs.js @@ -0,0 +1,48 @@ +require('should'); +var ftpd = require('../ftpd'), Ftp = require("jsftp"); + + +describe('application factory module', function(){ + var ftp, server; + + beforeEach(function(done){ + server = ftpd.createServer("127.0.0.1", __dirname + '/../fixture'); + server.on("client:connected", function(socket) { + var username; + socket.on("command:user", function(user, success, failure) { + if (user) { + username = user; + success(); + } else failure(); + }); + + socket.on("command:pass", function(pass, success, failure) { + if (pass) success(username); + else failure(); + }); + }); + server.listen(2021); + ftp = new Ftp({ + host: "127.0.0.1", + port: 2021 + }); + ftp.auth("jose", "esoj", function(err, res) { + done(); + }); + }); + + it("should return an application with port", function(done){ + ftp.list("/", function(err, d){ + var fileLine = d.substring(1).trim().split("\r\n") + .filter(function(line){ + return line.indexOf("data.txt") !== -1; + })[0]; + fileLine[0].should.eql("-"); + done(); + }); + }); + + afterEach(function(){ + server.close(); + }); +}); \ No newline at end of file From e9f7999b9a8a5bb4beaf7c49bf7958bb38299611 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20F=2E=20Romaniello?= Date: Tue, 3 Apr 2012 10:45:48 -0300 Subject: [PATCH 2/3] changed the description of the module and the specficiation --- specs/list.specs.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/specs/list.specs.js b/specs/list.specs.js index b49086a..61fbde3 100644 --- a/specs/list.specs.js +++ b/specs/list.specs.js @@ -2,7 +2,7 @@ require('should'); var ftpd = require('../ftpd'), Ftp = require("jsftp"); -describe('application factory module', function(){ +describe('LIST ftpd command', function(){ var ftp, server; beforeEach(function(done){ @@ -31,7 +31,7 @@ describe('application factory module', function(){ }); }); - it("should return an application with port", function(done){ + it("should return - as a first character for files", function(done){ ftp.list("/", function(err, d){ var fileLine = d.substring(1).trim().split("\r\n") .filter(function(line){ From a6198784f00835d3087b79b19f28097dc0bed039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20F=2E=20Romaniello?= Date: Tue, 3 Apr 2012 11:54:59 -0300 Subject: [PATCH 3/3] fix an issue not sending the 150 message when doing a RETR --- ftpd.js | 9 ++++---- specs/retr.specs.js | 52 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 specs/retr.specs.js diff --git a/ftpd.js b/ftpd.js index b6bfef7..1867791 100755 --- a/ftpd.js +++ b/ftpd.js @@ -504,12 +504,13 @@ function createServer(host, sandbox) { socket.totsize = 0; socket.filename = filename; } - fs.open(socket.sandbox + socket.filename, process.O_RDONLY, 0666, function (err, fd) { - dotrace("DATA file " + socket.filename + " opened"); + fs.open(socket.sandbox + socket.filename, "r", function (err, fd) { + console.trace("DATA file " + socket.filename + " opened"); + socket.write("150 Opening " + socket.mode.toUpperCase() + " mode data connection\r\n"); function readChunk() { fs.read(fd, 4096, socket.totsize, socket.mode, function(err, chunk, bytes_read) { if(err) { - dotrace("Erro reading chunk"); + console.trace("Erro reading chunk"); throw err; return; } @@ -519,7 +520,7 @@ function createServer(host, sandbox) { readChunk(); } else { - dotrace("DATA file " + socket.filename + " closed"); + console.trace("DATA file " + socket.filename + " closed"); pasvconn.end(); socket.write("226 Closing data connection, sent " + socket.totsize + " bytes\r\n"); fs.close(fd); diff --git a/specs/retr.specs.js b/specs/retr.specs.js new file mode 100644 index 0000000..8f10b56 --- /dev/null +++ b/specs/retr.specs.js @@ -0,0 +1,52 @@ +require('should'); +var ftpd = require('../ftpd'), Ftp = require("jsftp"), fs = require("fs"); + + +describe('RETR ftpd command', function(){ + var ftp, server; + + beforeEach(function(done){ + server = ftpd.createServer("127.0.0.1", fs.realpathSync(__dirname + '/../fixture')); + server.on("client:connected", function(socket) { + var username; + socket.on("command:user", function(user, success, failure) { + if (user) { + username = user; + success(); + } else failure(); + }); + + socket.on("command:pass", function(pass, success, failure) { + if (pass) success(username); + else failure(); + }); + }); + server.listen(2021); + ftp = new Ftp({ + host: "127.0.0.1", + port: 2021 + }); + ftp.auth("jose", "esoj", function(err, res) { + done(); + }); + }); + + it("should send a 150 changing mode before sending the content", function(done){ + var messages=[]; + ftp.socket.on("data", function(d){ + messages.push(d); + }); + ftp.setPassive({ + mode: "A", + cmd: "RETR " + "/data.txt", + pasvCallback: function(err, buffer){ + messages[3].should.eql("150 Opening ASCII mode data connection\r\n"); + done(); + } + }); + }); + + afterEach(function(){ + server.close(); + }); +}); \ No newline at end of file