Skip to content

Commit

Permalink
Merge pull request #2 from jfromaniello/RETR-missing-150
Browse files Browse the repository at this point in the history
Thank you! The code is becoming much more mature with your changes.
  • Loading branch information
alanszlosek committed Apr 10, 2012
2 parents 5a694a3 + a619878 commit 3a939a3
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules/*
1 change: 1 addition & 0 deletions fixture/jose/data.txt
@@ -0,0 +1 @@
hola!
11 changes: 6 additions & 5 deletions ftpd.js
Expand Up @@ -294,7 +294,7 @@ function createServer(host, sandbox) {
for (var i = 0; i < files.length; i++) { for (var i = 0; i < files.length; i++) {
var file = files[ i ]; var file = files[ i ];
var s = fs.statSync(path + file); var s = fs.statSync(path + file);
var line = s.isDirectory() ? 'd' : 'h'; var line = s.isDirectory() ? 'd' : '-';
if (i > 0) pasvconn.write("\r\n"); if (i > 0) pasvconn.write("\r\n");
line += (0400 & s.mode) ? 'r' : '-'; line += (0400 & s.mode) ? 'r' : '-';
line += (0200 & s.mode) ? 'w' : '-'; line += (0200 & s.mode) ? 'w' : '-';
Expand Down Expand Up @@ -504,12 +504,13 @@ function createServer(host, sandbox) {
socket.totsize = 0; socket.totsize = 0;
socket.filename = filename; socket.filename = filename;
} }
fs.open(socket.sandbox + socket.filename, process.O_RDONLY, 0666, function (err, fd) { fs.open(socket.sandbox + socket.filename, "r", function (err, fd) {
dotrace("DATA file " + socket.filename + " opened"); console.trace("DATA file " + socket.filename + " opened");
socket.write("150 Opening " + socket.mode.toUpperCase() + " mode data connection\r\n");
function readChunk() { function readChunk() {
fs.read(fd, 4096, socket.totsize, socket.mode, function(err, chunk, bytes_read) { fs.read(fd, 4096, socket.totsize, socket.mode, function(err, chunk, bytes_read) {
if(err) { if(err) {
dotrace("Erro reading chunk"); console.trace("Erro reading chunk");
throw err; throw err;
return; return;
} }
Expand All @@ -519,7 +520,7 @@ function createServer(host, sandbox) {
readChunk(); readChunk();
} }
else { else {
dotrace("DATA file " + socket.filename + " closed"); console.trace("DATA file " + socket.filename + " closed");
pasvconn.end(); pasvconn.end();
socket.write("226 Closing data connection, sent " + socket.totsize + " bytes\r\n"); socket.write("226 Closing data connection, sent " + socket.totsize + " bytes\r\n");
fs.close(fd); fs.close(fd);
Expand Down
14 changes: 14 additions & 0 deletions 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"
}
}
48 changes: 48 additions & 0 deletions 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();
});
});
52 changes: 52 additions & 0 deletions 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.