parser.test.js
20.2 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/*
* @package jsftp
* @copyright Copyright(c) 2011 Ajax.org B.V. <info AT ajax DOT org>
* @author Sergi Mansilla <sergi.mansilla@gmail.com>
* @license https://github.com/sergi/jsFTP/blob/master/LICENSE MIT License
*/
"use strict";
var assert = require("assert");
var Parser = require("../src/parser");
describe("jsftp file listing parser", function() {
it("test ftp unix STAT responses", function() {
var str = "drwxr-xr-x 5 1001 1001 4096 Jan 09 11:52 .\r\n\
drwxr-xr-x 4 0 0 4096 Sep 19 13:50 ..\r\n\
-rw------- 1 1001 1001 1118 Jan 09 12:09 .bash_history\r\n\
-rw------- 1 1001 1001 943 Jan 09 11:52 .viminfo\r\n\
drwxrwxr-x 5 1001 1001 4096 Jan 09 11:52 inaccessible\r\n\
drwxrwxrwx 2 1001 1001 4096 Sep 21 11:20 project1\r\n\
drwx------ 2 1001 1001 4096 Oct 19 16:17 project2\r\n";
var unixEntries = [
{
type: 0,
size: 1118,
name: ".bash_history",
time: +new Date("Jan 9 12:09 " + new Date().getFullYear()),
owner: "1001",
group: "1001",
userReadPerm: true,
userWritePerm: true,
userExecPerm: false,
groupReadPerm: false,
groupWritePerm: false,
groupExecPerm: false,
otherReadPerm: false,
otherWritePerm: false,
otherExecPerm: false
},
{
type: 0,
size: 943,
name: ".viminfo",
time: +new Date("Jan 9 11:52 " + new Date().getFullYear()),
owner: "1001",
group: "1001",
userReadPerm: true,
userWritePerm: true,
userExecPerm: false,
groupReadPerm: false,
groupWritePerm: false,
groupExecPerm: false,
otherReadPerm: false,
otherWritePerm: false,
otherExecPerm: false
},
{
type: 1,
size: 4096,
name: "inaccessible",
time: +new Date("Jan 9 11:52 " + new Date().getFullYear()),
owner: "1001",
group: "1001",
userReadPerm: true,
userWritePerm: true,
userExecPerm: true,
groupReadPerm: true,
groupWritePerm: true,
groupExecPerm: true,
otherReadPerm: true,
otherWritePerm: false,
otherExecPerm: true
},
{
type: 1,
size: 4096,
name: "project1",
time: +new Date("Sep 21 11:20 " + new Date().getFullYear()),
owner: "1001",
group: "1001",
userReadPerm: true,
userWritePerm: true,
userExecPerm: true,
groupReadPerm: true,
groupWritePerm: true,
groupExecPerm: true,
otherReadPerm: true,
otherWritePerm: true,
otherExecPerm: true
},
{
type: 1,
size: 4096,
name: "project2",
time: +new Date("Oct 19 16:17 " + new Date().getFullYear()),
owner: "1001",
group: "1001",
userReadPerm: true,
userWritePerm: true,
userExecPerm: true,
groupReadPerm: false,
groupWritePerm: false,
groupExecPerm: false,
otherReadPerm: false,
otherWritePerm: false,
otherExecPerm: false
}
];
Parser.parseEntries(str, function(err, entryArray) {
entryArray.forEach(function(entry, i) {
assert.equal(unixEntries[i].type, entry.type);
assert.equal(unixEntries[i].size, entry.size);
assert.equal(unixEntries[i].name, entry.name);
//assert.equal(unixEntries[i].time, entry.time);
assert.equal(unixEntries[i].owner, entry.owner);
assert.equal(unixEntries[i].group, entry.group);
assert.equal(unixEntries[i].userReadPerm, entry.userPermissions.read);
assert.equal(unixEntries[i].userWritePerm, entry.userPermissions.write);
assert.equal(unixEntries[i].userExecPerm, entry.userPermissions.exec);
assert.equal(unixEntries[i].groupReadPerm, entry.groupPermissions.read);
assert.equal(unixEntries[i].groupWritePerm, entry.groupPermissions.write);
assert.equal(unixEntries[i].groupExecPerm, entry.groupPermissions.exec);
assert.equal(unixEntries[i].otherReadPerm, entry.otherPermissions.read);
assert.equal(unixEntries[i].otherWritePerm, entry.otherPermissions.write);
assert.equal(unixEntries[i].otherExecPerm, entry.otherPermissions.exec);
});
})
});
it("test ftp unix LIST responses", function() {
var str = "drwx--x--- 10 mrclash adm 4096 Aug 9 14:48 .\r\n\
drwx--x--- 10 mrclash adm 4096 Aug 9 14:48 ..\r\n\
-rw-r--r-- 1 mrclash pg223090 260 Mar 25 2008 .alias\r\n\
-rw------- 1 mrclash pg223090 2219 Sep 5 2010 .bash_history\r\n\
-rw-r--r-- 1 mrclash pg223090 55 Mar 25 2008 .bashrc\r\n\
drwx------ 2 mrclash pg223090 4096 Aug 9 14:39 .ssh\r\n\
-rw-r--r-- 1 mrclash pg223090 18 Aug 8 13:06 Cloud9 FTP connection test.\r\n\
-rwxr-xr-x 1 mrclash pg223090 68491314 Jan 22 2009 Documents.zip\r\n\
-rwxr-xr-x 1 mrclash pg223090 141 Nov 1 2008 EcPxMptYISIdOSjS.XFV.Q--.html\r\n\
dr-xr-x--- 7 mrclash dhapache 4096 May 29 07:47 logs\r\n\
drwxr-xr-x 7 mrclash pg223090 4096 Aug 9 14:48 re-alpine.git\r\n\
-rwxr-xr-x 1 mrclash pg223090 312115 Jan 22 2009 restaurants.csv\r\n\
drwxr-xr-x 12 mrclash pg223090 4096 Jul 24 02:42 sergimansilla.com\r\n\
drwxr-xr-x 10 mrclash pg223090 4096 Aug 3 2009 svn\r\n\
-rwxr-xr-x 1 mrclash pg223090 76 Aug 9 14:47 sync-alpine.sh\r\n\
drwxr-xr-x 2 mrclash pg223090 4096 Aug 4 10:00 test_c9\r\n\
-rw-r--r-- 1 mrclash pg223090 4 Aug 4 09:11 testfile.txt\r\n\
lrwxr-xr-x 1 sergi staff 11 Jul 7 2011 .vimrc -> .vim/.vimrc\r\n";
var unixEntries = [
{
//line: "-rw-r--r-- 1 mrclash pg223090 260 Mar 25 2008 .alias",
type: 0,
size: 260,
name: ".alias",
time: +new Date("Mar 25 2008"),
owner: "mrclash",
group: "pg223090",
userReadPerm: true,
userWritePerm: true,
userExecPerm: false,
groupReadPerm: true,
groupWritePerm: false,
groupExecPerm: false,
otherReadPerm: true,
otherWritePerm: false,
otherExecPerm: false
},
{
//line: "-rw------- 1 mrclash pg223090 2219 Sep 5 2010 .bash_history",
type: 0,
size: 2219,
name: ".bash_history",
time: +new Date("Sep 5 2010"),
owner: "mrclash",
group: "pg223090",
userReadPerm: true,
userWritePerm: true,
userExecPerm: false,
groupReadPerm: false,
groupWritePerm: false,
groupExecPerm: false,
otherReadPerm: false,
otherWritePerm: false,
otherExecPerm: false
},
{
type: 0,
size: 55,
name: ".bashrc",
time: +new Date("Mar 25 2008"),
owner: "mrclash",
group: "pg223090",
userReadPerm: true,
userWritePerm: true,
userExecPerm: false,
groupReadPerm: true,
groupWritePerm: false,
groupExecPerm: false,
otherReadPerm: true,
otherWritePerm: false,
otherExecPerm: false
},
{
type: 1,
size: 4096,
name: ".ssh",
time: +new Date("Aug 9 14:39 " + new Date().getFullYear()),
owner: "mrclash",
group: "pg223090",
userReadPerm: true,
userWritePerm: true,
userExecPerm: true,
groupReadPerm: false,
groupWritePerm: false,
groupExecPerm: false,
otherReadPerm: false,
otherWritePerm: false,
otherExecPerm: false
},
{
type: 0,
size: 18,
name: "Cloud9 FTP connection test.",
time: +new Date("Aug 8 13:06 " + new Date().getFullYear()),
owner: "mrclash",
group: "pg223090",
userReadPerm: true,
userWritePerm: true,
userExecPerm: false,
groupReadPerm: true,
groupWritePerm: false,
groupExecPerm: false,
otherReadPerm: true,
otherWritePerm: false,
otherExecPerm: false
},
{
type: 0,
size: 68491314,
name: "Documents.zip",
time: +new Date("Jan 22 2009"),
owner: "mrclash",
group: "pg223090",
userReadPerm: true,
userWritePerm: true,
userExecPerm: true,
groupReadPerm: true,
groupWritePerm: false,
groupExecPerm: true,
otherReadPerm: true,
otherWritePerm: false,
otherExecPerm: true
},
{
type: 0,
size: 141,
name: "EcPxMptYISIdOSjS.XFV.Q--.html",
time: +new Date("Nov 1 2008"),
owner: "mrclash",
group: "pg223090",
userReadPerm: true,
userWritePerm: true,
userExecPerm: true,
groupReadPerm: true,
groupWritePerm: false,
groupExecPerm: true,
otherReadPerm: true,
otherWritePerm: false,
otherExecPerm: true
},
{
type: 1,
size: 4096,
name: "logs",
time: +new Date("May 29 07:47 " + new Date().getFullYear()),
owner: "mrclash",
group: "dhapache",
userReadPerm: true,
userWritePerm: false,
userExecPerm: true,
groupReadPerm: true,
groupWritePerm: false,
groupExecPerm: true,
otherReadPerm: false,
otherWritePerm: false,
otherExecPerm: false
},
{
type: 1,
size: 4096,
name: "re-alpine.git",
time: +new Date("Aug 9 14:48 " + new Date().getFullYear()),
owner: "mrclash",
group: "pg223090",
userReadPerm: true,
userWritePerm: true,
userExecPerm: true,
groupReadPerm: true,
groupWritePerm: false,
groupExecPerm: true,
otherReadPerm: true,
otherWritePerm: false,
otherExecPerm: true
},
{
type: 0,
size: 312115,
time: +new Date("Jan 22 2009"),
name: "restaurants.csv",
owner: "mrclash",
group: "pg223090",
userReadPerm: true,
userWritePerm: true,
userExecPerm: true,
groupReadPerm: true,
groupWritePerm: false,
groupExecPerm: true,
otherReadPerm: true,
otherWritePerm: false,
otherExecPerm: true
},
{
type: 1,
size: 4096,
time: +new Date("Jul 24 02:42 " + new Date().getFullYear()),
name: "sergimansilla.com",
owner: "mrclash",
group: "pg223090",
userReadPerm: true,
userWritePerm: true,
userExecPerm: true,
groupReadPerm: true,
groupWritePerm: false,
groupExecPerm: true,
otherReadPerm: true,
otherWritePerm: false,
otherExecPerm: true
},
{
type: 1,
size: 4096,
time: +new Date("Aug 3 2009"),
name: "svn",
owner: "mrclash",
group: "pg223090",
userReadPerm: true,
userWritePerm: true,
userExecPerm: true,
groupReadPerm: true,
groupWritePerm: false,
groupExecPerm: true,
otherReadPerm: true,
otherWritePerm: false,
otherExecPerm: true
},
{
type: 0,
size: 76,
time: +new Date("Aug 9 14:47 " + new Date().getFullYear()),
name: "sync-alpine.sh",
owner: "mrclash",
group: "pg223090",
userReadPerm: true,
userWritePerm: true,
userExecPerm: true,
groupReadPerm: true,
groupWritePerm: false,
groupExecPerm: true,
otherReadPerm: true,
otherWritePerm: false,
otherExecPerm: true
},
{
type: 1,
size: 4096,
time: +new Date("Aug 4 10:00 " + new Date().getFullYear()),
name: "test_c9",
owner: "mrclash",
group: "pg223090",
userReadPerm: true,
userWritePerm: true,
userExecPerm: true,
groupReadPerm: true,
groupWritePerm: false,
groupExecPerm: true,
otherReadPerm: true,
otherWritePerm: false,
otherExecPerm: true
},
{
type: 0,
size: 4,
time: +new Date("Aug 4 09:11 " + new Date().getFullYear()),
name: "testfile.txt",
owner: "mrclash",
group: "pg223090",
userReadPerm: true,
userWritePerm: true,
userExecPerm: false,
groupReadPerm: true,
groupWritePerm: false,
groupExecPerm: false,
otherReadPerm: true,
otherWritePerm: false,
otherExecPerm: false
},
{
type: 2,
size: 11,
time: +new Date("Jul 7 2011"),
name: ".vimrc",
target: ".vim/.vimrc",
owner: "sergi",
group: "staff",
userReadPerm: true,
userWritePerm: true,
userExecPerm: true,
groupReadPerm: true,
groupWritePerm: false,
groupExecPerm: true,
otherReadPerm: true,
otherWritePerm: false,
otherExecPerm: true
}
];
var str2 = "\
-drwxr-x--- 2 userName alternc 4096 Aug 22 03:45 .\r\n\
-drwxr-x--- 5 userName alternc 4096 Aug 22 03:45 ..\r\n\
--rw-r----- 1 userName alternc 460 Aug 22 03:45 test1\r\n\
--rw-r----- 1 userName alternc 560 Aug 22 03:47 test2\r\n";
var unixEntries2 = [
{
//line: "-rw-r--r-- 1 mrclash pg223090 260 Mar 25 2008 .alias",
type: 0,
size: 460,
name: "test1",
time: +new Date("Aug 22 03:45 " + new Date().getFullYear()),
owner: "userName",
group: "alternc",
userReadPerm: true,
userWritePerm: true,
userExecPerm: false,
groupReadPerm: true,
groupWritePerm: false,
groupExecPerm: false,
otherReadPerm: false,
otherWritePerm: false,
otherExecPerm: false
},
{
//line: "-rw-r--r-- 1 mrclash pg223090 260 Mar 25 2008 .alias",
type: 0,
size: 560,
name: "test2",
time: +new Date("Aug 22 03:47 " + new Date().getFullYear()),
owner: "userName",
group: "alternc",
userReadPerm: true,
userWritePerm: true,
userExecPerm: false,
groupReadPerm: true,
groupWritePerm: false,
groupExecPerm: false,
otherReadPerm: false,
otherWritePerm: false,
otherExecPerm: false
}
];
Parser.parseEntries(str, function(err, entryArray) {
entryArray.forEach(function(entry, i) {
assert.equal(unixEntries[i].type, entry.type);
assert.equal(unixEntries[i].size, entry.size);
assert.equal(unixEntries[i].name, entry.name);
//assert.equal(unixEntries[i].time, entry.time);
assert.equal(unixEntries[i].owner, entry.owner);
assert.equal(unixEntries[i].group, entry.group);
assert.equal(unixEntries[i].userReadPerm, entry.userPermissions.read);
assert.equal(unixEntries[i].userWritePerm, entry.userPermissions.write);
assert.equal(unixEntries[i].userExecPerm, entry.userPermissions.exec);
assert.equal(unixEntries[i].groupReadPerm, entry.groupPermissions.read);
assert.equal(unixEntries[i].groupWritePerm, entry.groupPermissions.write);
assert.equal(unixEntries[i].groupExecPerm, entry.groupPermissions.exec);
assert.equal(unixEntries[i].otherReadPerm, entry.otherPermissions.read);
assert.equal(unixEntries[i].otherWritePerm, entry.otherPermissions.write);
assert.equal(unixEntries[i].otherExecPerm, entry.otherPermissions.exec);
});
});
Parser.parseEntries(str2, function(err, entryArray) {
entryArray.forEach(function(entry, i) {
assert.equal(unixEntries2[i].type, entry.type);
assert.equal(unixEntries2[i].size, entry.size);
assert.equal(unixEntries2[i].name, entry.name);
//assert.equal(unixEntries2[i].time, entry.time);
assert.equal(unixEntries2[i].owner, entry.owner);
assert.equal(unixEntries2[i].group, entry.group);
assert.equal(unixEntries2[i].userReadPerm, entry.userPermissions.read);
assert.equal(unixEntries2[i].userWritePerm, entry.userPermissions.write);
assert.equal(unixEntries2[i].userExecPerm, entry.userPermissions.exec);
assert.equal(unixEntries2[i].groupReadPerm, entry.groupPermissions.read);
assert.equal(unixEntries2[i].groupWritePerm, entry.groupPermissions.write);
assert.equal(unixEntries2[i].groupExecPerm, entry.groupPermissions.exec);
assert.equal(unixEntries2[i].otherReadPerm, entry.otherPermissions.read);
assert.equal(unixEntries2[i].otherWritePerm, entry.otherPermissions.write);
assert.equal(unixEntries2[i].otherExecPerm, entry.otherPermissions.exec);
});
})
});
it("test ftp windows/DOS LIST responses", function() {
var dosEntries = [
{
line: '04-27-00 09:09PM <DIR> licensed',
type: 1,
size: 0,
time: +(new Date("04-27-00 09:09 PM")),
name: 'licensed',
},
{
line: '11-18-03 10:16AM <DIR> pub',
type: 1,
size: 0,
time: +(new Date("11-18-03 10:16 AM")),
name: 'pub',
},
{
line: '11-18-03 10:16AM <DIR> 2015',
type: 1,
size: 0,
time: +(new Date("11-18-03 10:16 AM")),
name: '2015',
},
{
line: '04-14-99 03:47PM 589 readme.htm',
type: 0,
size: 589,
time: +(new Date("04-14-99 03:47 PM")),
name: 'readme.htm'
}
];
dosEntries.forEach(function(entry) {
var result = Parser.parseEntry(entry.line);
assert.equal(result.type, entry.type);
assert.equal(result.size, entry.size);
assert.equal(result.name, entry.name);
assert.equal(result.time, entry.time);
});
});
it("test FTP listing with bad line breaks", function(next) {
var badStr = "\
213-Status follows:\r\n\
-rw-r--r-- 1 0 0 105981956 Dec 20 18:07 GAT\r\n\
SBY.MPG\r\n\
-rw-r--r-- 1 0 0 74450948 Jan 17 18:16 GIJO.MPG\r\n\
drwxr-xr-x 3 0 0 4096 Apr 16 2011 bourd\n\
arie\r\n\
drwxr-xr-x 2 0 0 4096 Apr 16 2011 denton\r\n\
213 End of status";
Parser.parseFtpEntries(badStr, function(err, entries) {
assert.equal("GATSBY.MPG", entries[0].name);
assert.equal("GIJO.MPG", entries[1].name);
assert.equal("bourdarie", entries[2].name);
assert.equal("denton", entries[3].name);
next();
});
});
/*
* We are not supporting MLSx commands yet
*
* http://rfc-ref.org/RFC-TEXTS/3659/chapter7.html
* http://www.rhinosoft.com/newsletter/NewsL2005-07-06.asp?prod=rs
*
"test parse MLSD command lines" : function(next) {
var lines = [
{
line: "Type=file;Size=17709913;Modify=20050502182143; Choices.mp3",
Type: "file",
Size: "17709913",
Modify: "20050502182143",
name: "Choices.mp3"
},
{
line: "Type=cdir;Perm=el;Unique=keVO1+ZF4; test",
type: "file",
perm: "el",
},
{
line: "Type=pdir;Perm=e;Unique=keVO1+d?3; .."
}
];
//"Type=cdir;Perm=el;Unique=keVO1+ZF4; test",
//"Type=pdir;Perm=e;Unique=keVO1+d?3; ..",
//"Type=OS.unix=slink:/foobar;Perm=;Unique=keVO1+4G4; foobar",
//"Type=OS.unix=chr-13/29;Perm=;Unique=keVO1+5G4; device",
//"Type=OS.unix=blk-11/108;Perm=;Unique=keVO1+6G4; block",
//"Type=file;Perm=awr;Unique=keVO1+8G4; writable",
//"Type=dir;Perm=cpmel;Unique=keVO1+7G4; promiscuous",
//"Type=dir;Perm=;Unique=keVO1+1t2; no-exec",
//"Type=file;Perm=r;Unique=keVO1+EG4; two words",
//"Type=file;Perm=r;Unique=keVO1+IH4; leading space",
//"Type=file;Perm=r;Unique=keVO1+1G4; file1",
//"Type=dir;Perm=cpmel;Unique=keVO1+7G4; incoming",
//"Type=file;Perm=r;Unique=keVO1+1G4; file2",
//"Type=file;Perm=r;Unique=keVO1+1G4; file3",
//"Type=file;Perm=r;Unique=keVO1+1G4; file4",
var parsed = Parser.parseMList(line);
assert.equal("file", parsed.Type);
assert.equal("17709913", parsed.Size);
assert.equal("20050502182143", parsed.Modify);
assert.equal("Choices.mp3", parsed.name);
next();
}
*/
});