Skip to content
Browse files

Merge pull request #2 from jfromaniello/RETR-missing-150

Thank you! The code is becoming much more mature with your changes.
  • Loading branch information...
2 parents 5a694a3 + a619878 commit 3a939a378a04bcad93d6adf496f9d449f472938b @alanszlosek committed
Showing with 122 additions and 5 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 fixture/jose/data.txt
  3. +6 −5 ftpd.js
  4. +14 −0 package.json
  5. +48 −0 specs/list.specs.js
  6. +52 −0 specs/retr.specs.js
View
1 .gitignore
@@ -0,0 +1 @@
+node_modules/*
View
1 fixture/jose/data.txt
@@ -0,0 +1 @@
+hola!
View
11 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' : '-';
@@ -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);
View
14 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"
+ }
+}
View
48 specs/list.specs.js
@@ -0,0 +1,48 @@
+require('should');
+var ftpd = require('../ftpd'), Ftp = require("jsftp");
+
+
+describe('LIST ftpd command', 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 - 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){
+ return line.indexOf("data.txt") !== -1;
+ })[0];
+ fileLine[0].should.eql("-");
+ done();
+ });
+ });
+
+ afterEach(function(){
+ server.close();
+ });
+});
View
52 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();
+ });
+});

0 comments on commit 3a939a3

Please sign in to comment.
Something went wrong with that request. Please try again.