/
jsftp_test.js
266 lines (216 loc) · 8.24 KB
/
jsftp_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* @package jsFTP
* @copyright Copyright(c) 2011 Ajax.org B.V. <info AT ajax DOT org>
* @author Sergi Mansilla <sergi DOT mansilla AT gmail DOT com>
* @license https://github.com/sergi/jsFTP/blob/master/LICENSE MIT License
*/
var assert = require("assert");
var Fs = require("fs");
var exec = require('child_process').spawn;
var Ftp = require("./jsftp");
// Write down your system credentials. This test suite will use OSX internal
// FTP server. If you want to test against a remote server, simply change the
// `host` and `port` properties as well.
var FTPCredentials = {
host: "localhost",
user: "",
port: 21,
pass: ""
};
var CWD = process.cwd();
console.log("Current working directory is " + CWD + "\n");
// Execution ORDER: test.setUpSuite, setUp, testFn, tearDown, test.tearDownSuite
module.exports = {
timeout: 10000,
setUp: function(next) {
try {
exec('/bin/launchctl', ['load', '-w', '/System/Library/LaunchDaemons/ftp.plist']);
} catch(e) {}
var self = this;
setTimeout(function() {
self.ftp = new Ftp(FTPCredentials);
next();
}, 200);
},
tearDown: function(next) {
try {
exec('/bin/launchctl', ['unload', '-w', '/System/Library/LaunchDaemons/ftp.plist']);
} catch (e) {}
var self = this;
next();
setTimeout(function() {
self.ftp = null;
next();
}, 200);
},
"test features command": function(next) {
var ftp = this.ftp;
ftp.auth(FTPCredentials.user, FTPCredentials.pass, function(err, res) {
assert.ok(!err);
assert.ok(Array.isArray(ftp.features));
if (ftp.features.length) {
var feat = ftp.features[0];
assert.ok(ftp.hasFeat(feat));
}
next();
});
},
"test print working directory": function(next) {
var ftp = this.ftp;
ftp.auth(FTPCredentials.user, FTPCredentials.pass, function(err, res) {
ftp.raw.pwd(function(err, res) {
if (err) throw err;
var code = parseInt(res.code, 10);
assert.ok(code === 257, "PWD command was not successful");
ftp.raw.quit(function(err, res) {
if (err) throw err;
next();
});
});
});
},
"test current working directory": function(next) {
var ftp = this.ftp;
ftp.auth(FTPCredentials.user, FTPCredentials.pass, function(err, res) {
if (err) throw err;
ftp.raw.cwd(CWD, function(err, res) {
if (err) throw err;
var code = parseInt(res.code, 10);
assert.ok(code === 200 || code === 250, "CWD command was not successful");
ftp.raw.pwd(function(err, res) {
if (err) throw err;
var code = parseInt(res.code, 10);
assert.ok(code === 257, "PWD command was not successful");
assert.ok(res.text.indexOf(CWD), "Unexpected CWD");
});
ftp.raw.cwd("/unexistentDir/", function(err, res) {
assert.ok(err);
code = parseInt(res.code, 10);
assert.ok(code === 550, "A (wrong) CWD command was successful. It should have failed");
next();
});
});
});
},
"test passive listing of current directory": function(next) {
var ftp = this.ftp;
ftp.auth(FTPCredentials.user, FTPCredentials.pass, function(res) {
ftp.list(CWD, function(err, res){
next();
});
});
},
"test ftp node stat": function(next) {
var ftp = this.ftp;
ftp.auth(FTPCredentials.user, FTPCredentials.pass, function(err, res) {
ftp.raw.cwd(CWD, function(err, res) {
ftp.raw.stat(CWD, function(err, res) {
assert.ok(!err);
assert.ok(res.code === 211);
next();
});
});
});
},
"test create and delete a directory": function(next) {
var self = this;
var newDir = CWD + "/ftp_test_dir";
var ftp = this.ftp;
ftp.auth(FTPCredentials.user, FTPCredentials.pass, function(err, res) {
ftp.raw.mkd(newDir, function(err, res) {
assert.ok(!err);
assert.ok(res.code === 257);
ftp.raw.rmd(newDir, function(err, res) {
assert.ok(!err);
next();
});
});
});
},
"test create and delete a file": function(next) {
var self = this;
var filePath = CWD + "/file_ftp_test.txt";
var ftp = this.ftp;
ftp.auth(FTPCredentials.user, FTPCredentials.pass, function(err, res) {
Fs.readFile(CWD + "/jsftp_test.js", "binary", function(err, data) {
var buffer = new Buffer(data, "binary");
ftp.put(filePath, buffer, function(err, res) {
assert.ok(!err, err);
ftp.raw.stat(filePath, function(err, res) {
assert.ok(!err);
assert.equal(buffer.length, Fs.statSync(filePath).size);
ftp.raw.dele(filePath, function(err, data) {
assert.ok(!err);
next();
});
});
});
});
});
},
"test rename a file": function(next) {
var self = this;
var from = CWD + "/file_ftp_test.txt";
var to = CWD + "/file_ftp_test_renamed.txt";
var ftp = this.ftp;
ftp.auth(FTPCredentials.user, FTPCredentials.pass, function(err, res) {
Fs.readFile(CWD + "/jsftp_test.js", "binary", function(err, data) {
var buffer = new Buffer(data, "binary");
ftp.put(from, buffer, function(err, res) {
assert.ok(!err, err);
ftp.rename(from, to, function(err, res) {
ftp.raw.stat(to, function(err, res) {
assert.ok(!err);
assert.equal(buffer.length, Fs.statSync(to).size);
ftp.raw.dele(to, function(err, data) {
assert.ok(!err);
next();
});
});
});
});
});
});
},
"test get a file": function(next) {
var filePath = CWD + "/jsftp_test.js";
var ftp = this.ftp;
ftp.auth(FTPCredentials.user, FTPCredentials.pass, function(err, res) {
Fs.readFile(filePath, "binary", function(err, data) {
var buffer = new Buffer(data, "binary");
ftp.get(filePath, function(err, data) {
assert.ok(!err, err);
assert.equal(buffer.length, data.length);
next()
});
});
});
},
"test get fileList array": function(next) {
var ftp = this.ftp;
var file1 = "jsftp.js";
ftp.auth(FTPCredentials.user, FTPCredentials.pass, function(err, res) {
ftp.raw.cwd(CWD, function(err, res) {
ftp.ls(function(err, res) {
assert.ok(!err);
assert.ok(Array.isArray(res));
var fileNames = res.map(function(file) {
return file ? file.name : null;
})
assert.ok(fileNames.indexOf(file1) > -1);
next();
});
});
ftp.ls(CWD, function(err, res) {
assert.ok(!err);
assert.ok(Array.isArray(res));
var fileNames = res.map(function(file) {
return file ? file.name : null;
})
assert.ok(fileNames.indexOf(file1) > -1);
next();
});
});
}
};
!module.parent && require("asyncjs").test.testcase(module.exports, "FTP").exec();