forked from mscdex/ssh2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-sftp.js
782 lines (737 loc) · 25.2 KB
/
test-sftp.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
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
'use strict';
const assert = require('assert');
const { constants } = require('fs');
const {
fixture,
mustCall,
mustCallAtLeast,
mustNotCall,
setup: setup_,
setupSimple
} = require('./common.js');
const { OPEN_MODE, Stats, STATUS_CODE } = require('../lib/protocol/SFTP.js');
const DEBUG = false;
setup('open', mustCall((client, server) => {
const path_ = '/tmp/foo.txt';
const handle_ = Buffer.from('node.js');
const pflags_ = (OPEN_MODE.TRUNC | OPEN_MODE.CREAT | OPEN_MODE.WRITE);
server.on('OPEN', mustCall((id, path, pflags, attrs) => {
assert(id === 0, `Wrong request id: ${id}`);
assert(path === path_, `Wrong path: ${path}`);
assert(pflags === pflags_, `Wrong flags: ${flagsToHuman(pflags)}`);
server.handle(id, handle_);
server.end();
}));
client.open(path_, 'w', mustCall((err, handle) => {
assert(!err, `Unexpected open() error: ${err}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
}));
}));
setup('close', mustCall((client, server) => {
const handle_ = Buffer.from('node.js');
server.on('CLOSE', mustCall((id, handle) => {
assert(id === 0, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
server.status(id, STATUS_CODE.OK);
server.end();
}));
client.close(handle_, mustCall((err) => {
assert(!err, `Unexpected close() error: ${err}`);
}));
}));
setup('read', mustCall((client, server) => {
const expected = Buffer.from('node.jsnode.jsnode.jsnode.jsnode.jsnode.js');
const handle_ = Buffer.from('node.js');
const buf = Buffer.alloc(expected.length);
server.on('READ', mustCall((id, handle, offset, len) => {
assert(id === 0, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
assert(offset === 5, `Wrong read offset: ${offset}`);
assert(len === buf.length, `Wrong read len: ${len}`);
server.data(id, expected);
server.end();
}));
client.read(handle_, buf, 0, buf.length, 5, mustCall((err, nb) => {
assert(!err, `Unexpected read() error: ${err}`);
assert.deepStrictEqual(buf, expected, 'read data mismatch');
}));
}));
setup('read (overflow)', mustCall((client, server) => {
const maxChunk = client._maxReadLen;
const expected = Buffer.alloc(3 * maxChunk, 'Q');
const handle_ = Buffer.from('node.js');
const buf = Buffer.alloc(expected.length, 0);
let reqs = 0;
server.on('READ', mustCall((id, handle, offset, len) => {
++reqs;
assert.strictEqual(id, reqs - 1, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
assert.strictEqual(offset,
(reqs - 1) * maxChunk,
`Wrong read offset: ${offset}`);
server.data(id, expected.slice(offset, offset + len));
if (reqs === 3)
server.end();
}, 3));
client.read(handle_, buf, 0, buf.length, 0, mustCall((err, nb) => {
assert(!err, `Unexpected read() error: ${err}`);
assert.deepStrictEqual(buf, expected);
assert.strictEqual(nb, buf.length, 'read nb mismatch');
}));
}));
setup('write', mustCall((client, server) => {
const handle_ = Buffer.from('node.js');
const buf = Buffer.from('node.jsnode.jsnode.jsnode.jsnode.jsnode.js');
server.on('WRITE', mustCall((id, handle, offset, data) => {
assert(id === 0, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
assert(offset === 5, `Wrong write offset: ${offset}`);
assert.deepStrictEqual(data, buf, 'write data mismatch');
server.status(id, STATUS_CODE.OK);
server.end();
}));
client.write(handle_, buf, 0, buf.length, 5, mustCall((err, nb) => {
assert(!err, `Unexpected write() error: ${err}`);
assert.strictEqual(nb, buf.length, 'wrong bytes written');
}));
}));
setup('write (overflow)', mustCall((client, server) => {
const maxChunk = client._maxWriteLen;
const handle_ = Buffer.from('node.js');
const buf = Buffer.allocUnsafe(3 * maxChunk);
let reqs = 0;
server.on('WRITE', mustCall((id, handle, offset, data) => {
++reqs;
assert.strictEqual(id, reqs - 1, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
assert.strictEqual(offset,
(reqs - 1) * maxChunk,
`Wrong write offset: ${offset}`);
assert((offset + data.length) <= buf.length, 'bad offset');
assert.deepStrictEqual(data,
buf.slice(offset, offset + data.length),
'write data mismatch');
server.status(id, STATUS_CODE.OK);
if (reqs === 3)
server.end();
}, 3));
client.write(handle_, buf, 0, buf.length, 0, mustCall((err, nb) => {
assert(!err, `Unexpected write() error: ${err}`);
assert.strictEqual(nb, buf.length, 'wrote bytes written');
}));
}));
setup('lstat', mustCall((client, server) => {
const path_ = '/foo/bar/baz';
const attrs_ = new Stats({
size: 10 * 1024,
uid: 9001,
gid: 9001,
atime: (Date.now() / 1000) | 0,
mtime: (Date.now() / 1000) | 0
});
server.on('LSTAT', mustCall((id, path) => {
assert(id === 0, `Wrong request id: ${id}`);
assert(path === path_, `Wrong path: ${path}`);
server.attrs(id, attrs_);
server.end();
}));
client.lstat(path_, mustCall((err, attrs) => {
assert(!err, `Unexpected lstat() error: ${err}`);
assert.deepStrictEqual(attrs, attrs_, 'attrs mismatch');
}));
}));
setup('fstat', mustCall((client, server) => {
const handle_ = Buffer.from('node.js');
const attrs_ = new Stats({
size: 10 * 1024,
uid: 9001,
gid: 9001,
atime: (Date.now() / 1000) | 0,
mtime: (Date.now() / 1000) | 0
});
server.on('FSTAT', mustCall((id, handle) => {
assert(id === 0, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
server.attrs(id, attrs_);
server.end();
}));
client.fstat(handle_, mustCall((err, attrs) => {
assert(!err, `Unexpected fstat() error: ${err}`);
assert.deepStrictEqual(attrs, attrs_, 'attrs mismatch');
}));
}));
setup('setstat', mustCall((client, server) => {
const path_ = '/foo/bar/baz';
const attrs_ = new Stats({
uid: 9001,
gid: 9001,
atime: (Date.now() / 1000) | 0,
mtime: (Date.now() / 1000) | 0
});
server.on('SETSTAT', mustCall((id, path, attrs) => {
assert(id === 0, `Wrong request id: ${id}`);
assert(path === path_, `Wrong path: ${path}`);
assert.deepStrictEqual(attrs, attrs_, 'attrs mismatch');
server.status(id, STATUS_CODE.OK);
server.end();
}));
client.setstat(path_, attrs_, mustCall((err) => {
assert(!err, `Unexpected setstat() error: ${err}`);
}));
}));
setup('fsetstat', mustCall((client, server) => {
const handle_ = Buffer.from('node.js');
const attrs_ = new Stats({
uid: 9001,
gid: 9001,
atime: (Date.now() / 1000) | 0,
mtime: (Date.now() / 1000) | 0
});
server.on('FSETSTAT', mustCall((id, handle, attrs) => {
assert(id === 0, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
assert.deepStrictEqual(attrs, attrs_, 'attrs mismatch');
server.status(id, STATUS_CODE.OK);
server.end();
}));
client.fsetstat(handle_, attrs_, mustCall((err) => {
assert(!err, `Unexpected fsetstat() error: ${err}`);
}));
}));
setup('opendir', mustCall((client, server) => {
const handle_ = Buffer.from('node.js');
const path_ = '/tmp';
server.on('OPENDIR', mustCall((id, path) => {
assert(id === 0, `Wrong request id: ${id}`);
assert(path === path_, `Wrong path: ${path}`);
server.handle(id, handle_);
server.end();
}));
client.opendir(path_, mustCall((err, handle) => {
assert(!err, `Unexpected opendir() error: ${err}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
}));
}));
setup('readdir', mustCall((client, server) => {
const handle_ = Buffer.from('node.js');
const list_ = [
{ filename: '.',
longname: 'drwxr-xr-x 56 nodejs nodejs 4096 Nov 10 01:05 .',
attrs: new Stats({
mode: 0o755 | constants.S_IFDIR,
size: 4096,
uid: 9001,
gid: 8001,
atime: 1415599549,
mtime: 1415599590
})
},
{ filename: '..',
longname: 'drwxr-xr-x 4 root root 4096 May 16 2013 ..',
attrs: new Stats({
mode: 0o755 | constants.S_IFDIR,
size: 4096,
uid: 0,
gid: 0,
atime: 1368729954,
mtime: 1368729999
})
},
{ filename: 'foo',
longname: 'drwxrwxrwx 2 nodejs nodejs 4096 Mar 8 2009 foo',
attrs: new Stats({
mode: 0o777 | constants.S_IFDIR,
size: 4096,
uid: 9001,
gid: 8001,
atime: 1368729954,
mtime: 1368729999
})
},
{ filename: 'bar',
longname: '-rw-r--r-- 1 nodejs nodejs 513901992 Dec 4 2009 bar',
attrs: new Stats({
mode: 0o644 | constants.S_IFREG,
size: 513901992,
uid: 9001,
gid: 8001,
atime: 1259972199,
mtime: 1259972199
})
}
];
server.on('READDIR', mustCall((id, handle) => {
assert(id === 0, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
server.name(id, list_);
server.end();
}));
client.readdir(handle_, mustCall((err, list) => {
assert(!err, `Unexpected readdir() error: ${err}`);
assert.deepStrictEqual(list,
list_.slice(2),
'dir list mismatch');
}));
}));
setup('readdir (full)', mustCall((client, server) => {
const handle_ = Buffer.from('node.js');
const list_ = [
{ filename: '.',
longname: 'drwxr-xr-x 56 nodejs nodejs 4096 Nov 10 01:05 .',
attrs: new Stats({
mode: 0o755 | constants.S_IFDIR,
size: 4096,
uid: 9001,
gid: 8001,
atime: 1415599549,
mtime: 1415599590
})
},
{ filename: '..',
longname: 'drwxr-xr-x 4 root root 4096 May 16 2013 ..',
attrs: new Stats({
mode: 0o755 | constants.S_IFDIR,
size: 4096,
uid: 0,
gid: 0,
atime: 1368729954,
mtime: 1368729999
})
},
{ filename: 'foo',
longname: 'drwxrwxrwx 2 nodejs nodejs 4096 Mar 8 2009 foo',
attrs: new Stats({
mode: 0o777 | constants.S_IFDIR,
size: 4096,
uid: 9001,
gid: 8001,
atime: 1368729954,
mtime: 1368729999
})
},
{ filename: 'bar',
longname: '-rw-r--r-- 1 nodejs nodejs 513901992 Dec 4 2009 bar',
attrs: new Stats({
mode: 0o644 | constants.S_IFREG,
size: 513901992,
uid: 9001,
gid: 8001,
atime: 1259972199,
mtime: 1259972199
})
}
];
server.on('READDIR', mustCall((id, handle) => {
assert(id === 0, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
server.name(id, list_);
server.end();
}));
client.readdir(handle_, { full: true }, mustCall((err, list) => {
assert(!err, `Unexpected readdir() error: ${err}`);
assert.deepStrictEqual(list, list_, 'dir list mismatch');
}));
}));
setup('readdir (EOF)', mustCall((client, server) => {
const handle_ = Buffer.from('node.js');
server.on('READDIR', mustCall((id, handle) => {
assert(id === 0, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
server.status(id, STATUS_CODE.EOF);
server.end();
}));
client.readdir(handle_, mustCall((err, list) => {
assert(err && err.code === STATUS_CODE.EOF,
`Expected EOF, got: ${err}`);
}));
}));
setup('unlink', mustCall((client, server) => {
const path_ = '/foo/bar/baz';
server.on('REMOVE', mustCall((id, path) => {
assert(id === 0, `Wrong request id: ${id}`);
assert(path === path_, `Wrong path: ${path}`);
server.status(id, STATUS_CODE.OK);
server.end();
}));
client.unlink(path_, mustCall((err) => {
assert(!err, `Unexpected unlink() error: ${err}`);
}));
}));
setup('mkdir', mustCall((client, server) => {
const path_ = '/foo/bar/baz';
server.on('MKDIR', mustCall((id, path) => {
assert(id === 0, `Wrong request id: ${id}`);
assert(path === path_, `Wrong path: ${path}`);
server.status(id, STATUS_CODE.OK);
server.end();
}));
client.mkdir(path_, mustCall((err) => {
assert(!err, `Unexpected mkdir() error: ${err}`);
}));
}));
setup('rmdir', mustCall((client, server) => {
const path_ = '/foo/bar/baz';
server.on('RMDIR', mustCall((id, path) => {
assert(id === 0, `Wrong request id: ${id}`);
assert(path === path_, `Wrong path: ${path}`);
server.status(id, STATUS_CODE.OK);
server.end();
}));
client.rmdir(path_, mustCall((err) => {
assert(!err, `Unexpected rmdir() error: ${err}`);
}));
}));
setup('realpath', mustCall((client, server) => {
const path_ = '/foo/bar/baz';
const name_ = { filename: '/tmp/foo' };
server.on('REALPATH', mustCall((id, path) => {
assert(id === 0, `Wrong request id: ${id}`);
assert(path === path_, `Wrong path: ${path}`);
server.name(id, name_);
server.end();
}));
client.realpath(path_, mustCall((err, name) => {
assert(!err, `Unexpected realpath() error: ${err}`);
assert.deepStrictEqual(name, name_.filename, 'name mismatch');
}));
}));
setup('stat', mustCall((client, server) => {
const path_ = '/foo/bar/baz';
const attrs_ = new Stats({
mode: 0o644 | constants.S_IFREG,
size: 10 * 1024,
uid: 9001,
gid: 9001,
atime: (Date.now() / 1000) | 0,
mtime: (Date.now() / 1000) | 0
});
server.on('STAT', mustCall((id, path) => {
assert(id === 0, `Wrong request id: ${id}`);
assert(path === path_, `Wrong path: ${path}`);
server.attrs(id, attrs_);
server.end();
}));
client.stat(path_, mustCall((err, attrs) => {
assert(!err, `Unexpected stat() error: ${err}`);
assert.deepStrictEqual(attrs, attrs_, 'attrs mismatch');
const expectedTypes = {
isDirectory: false,
isFile: true,
isBlockDevice: false,
isCharacterDevice: false,
isSymbolicLink: false,
isFIFO: false,
isSocket: false
};
for (const [fn, expect] of Object.entries(expectedTypes))
assert(attrs[fn]() === expect, `attrs.${fn}() failed`);
}));
}));
setup('rename', mustCall((client, server) => {
const oldPath_ = '/foo/bar/baz';
const newPath_ = '/tmp/foo';
server.on('RENAME', mustCall((id, oldPath, newPath) => {
assert(id === 0, `Wrong request id: ${id}`);
assert(oldPath === oldPath_, `Wrong old path: ${oldPath}`);
assert(newPath === newPath_, `Wrong new path: ${newPath}`);
server.status(id, STATUS_CODE.OK);
server.end();
}));
client.rename(oldPath_, newPath_, mustCall((err) => {
assert(!err, `Unexpected rename() error: ${err}`);
}));
}));
setup('readlink', mustCall((client, server) => {
const linkPath_ = '/foo/bar/baz';
const name = { filename: '/tmp/foo' };
server.on('READLINK', mustCall((id, linkPath) => {
assert(id === 0, `Wrong request id: ${id}`);
assert(linkPath === linkPath_, `Wrong link path: ${linkPath}`);
server.name(id, name);
server.end();
}));
client.readlink(linkPath_, mustCall((err, targetPath) => {
assert(!err, `Unexpected readlink() error: ${err}`);
assert(targetPath === name.filename,
`Wrong target path: ${targetPath}`);
}));
}));
setup('symlink', mustCall((client, server) => {
const linkPath_ = '/foo/bar/baz';
const targetPath_ = '/tmp/foo';
server.on('SYMLINK', mustCall((id, linkPath, targetPath) => {
assert(id === 0, `Wrong request id: ${id}`);
assert(linkPath === linkPath_, `Wrong link path: ${linkPath}`);
assert(targetPath === targetPath_, `Wrong target path: ${targetPath}`);
server.status(id, STATUS_CODE.OK);
server.end();
}));
client.symlink(targetPath_, linkPath_, mustCall((err) => {
assert(!err, `Unexpected symlink() error: ${err}`);
}));
}));
setup('readFile', mustCall((client, server) => {
const path_ = '/foo/bar/baz';
const handle_ = Buffer.from('hi mom!');
const data_ = Buffer.from('hello world');
server.on('OPEN', mustCall((id, path, pflags, attrs) => {
assert(id === 0, `Wrong request id: ${id}`);
assert(path === path_, `Wrong path: ${path}`);
assert(pflags === OPEN_MODE.READ, `Wrong flags: ${flagsToHuman(pflags)}`);
server.handle(id, handle_);
})).on('FSTAT', mustCall((id, handle) => {
assert(id === 1, `Wrong request id: ${id}`);
const attrs = new Stats({
size: data_.length,
uid: 9001,
gid: 9001,
atime: (Date.now() / 1000) | 0,
mtime: (Date.now() / 1000) | 0
});
server.attrs(id, attrs);
})).on('READ', mustCall((id, handle, offset, len) => {
assert(id === 2, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
assert(offset === 0, `Wrong read offset: ${offset}`);
server.data(id, data_);
})).on('CLOSE', mustCall((id, handle) => {
assert(id === 3, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
server.status(id, STATUS_CODE.OK);
server.end();
}));
client.readFile(path_, mustCall((err, buf) => {
assert(!err, `Unexpected error: ${err}`);
assert.deepStrictEqual(buf, data_, 'data mismatch');
}));
}));
setup('readFile (no size from fstat)', mustCall((client, server) => {
const path_ = '/foo/bar/baz';
const handle_ = Buffer.from('hi mom!');
const data_ = Buffer.from('hello world');
let reads = 0;
server.on('OPEN', mustCall((id, path, pflags, attrs) => {
assert(id === 0, `Wrong request id: ${id}`);
assert(path === path_, `Wrong path: ${path}`);
assert(pflags === OPEN_MODE.READ, `Wrong flags: ${flagsToHuman(pflags)}`);
server.handle(id, handle_);
})).on('FSTAT', mustCall((id, handle) => {
assert(id === 1, `Wrong request id: ${id}`);
const attrs = new Stats({
uid: 9001,
gid: 9001,
atime: (Date.now() / 1000) | 0,
mtime: (Date.now() / 1000) | 0
});
server.attrs(id, attrs);
})).on('READ', mustCall((id, handle, offset, len) => {
assert(++reads + 1 === id, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
switch (id) {
case 2:
assert(offset === 0, `Wrong read offset for first read: ${offset}`);
server.data(id, data_);
break;
case 3:
assert(offset === data_.length,
`Wrong read offset for second read: ${offset}`);
server.status(id, STATUS_CODE.EOF);
break;
}
}, 2)).on('CLOSE', mustCall((id, handle) => {
assert(id === 4, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
server.status(id, STATUS_CODE.OK);
server.end();
}));
client.readFile(path_, mustCall((err, buf) => {
assert(!err, `Unexpected error: ${err}`);
assert.deepStrictEqual(buf, data_, 'data mismatch');
}));
}));
setup('ReadStream', mustCall((client, server) => {
let reads = 0;
const path_ = '/foo/bar/baz';
const handle_ = Buffer.from('hi mom!');
const data_ = Buffer.from('hello world');
server.on('OPEN', mustCall((id, path, pflags, attrs) => {
assert(id === 0, `Wrong request id: ${id}`);
assert(path === path_, `Wrong path: ${path}`);
assert(pflags === OPEN_MODE.READ, `Wrong flags: ${flagsToHuman(pflags)}`);
server.handle(id, handle_);
})).on('READ', mustCall((id, handle, offset, len) => {
assert(id === ++reads, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
if (reads === 1) {
assert(offset === 0, `Wrong read offset: ${offset}`);
server.data(id, data_);
} else {
server.status(id, STATUS_CODE.EOF);
}
}, 2)).on('CLOSE', mustCall((id, handle) => {
assert(id === 3, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
server.status(id, STATUS_CODE.OK);
server.end();
}));
let buf = [];
client.createReadStream(path_).on('readable', mustCallAtLeast(function() {
let chunk;
while ((chunk = this.read()) !== null)
buf.push(chunk);
})).on('end', mustCall(() => {
buf = Buffer.concat(buf);
assert.deepStrictEqual(buf, data_, 'data mismatch');
}));
}));
setup('ReadStream (fewer bytes than requested)', mustCall((client, server) => {
const path_ = '/foo/bar/baz';
const handle_ = Buffer.from('hi mom!');
const data_ = Buffer.from('hello world');
server.on('OPEN', mustCall((id, path, pflags, attrs) => {
server.handle(id, handle_);
})).on('READ', mustCallAtLeast((id, handle, offset, len) => {
if (offset > data_.length) {
server.status(id, STATUS_CODE.EOF);
} else {
// Only read 4 bytes at a time
server.data(id, data_.slice(offset, offset + 4));
}
})).on('CLOSE', mustCall((id, handle) => {
server.status(id, STATUS_CODE.OK);
server.end();
}));
let buf = [];
client.createReadStream(path_).on('readable', mustCallAtLeast(function() {
let chunk;
while ((chunk = this.read()) !== null)
buf.push(chunk);
})).on('end', mustCall(() => {
buf = Buffer.concat(buf);
assert.deepStrictEqual(buf, data_, 'data mismatch');
}));
}));
setup('ReadStream (error)', mustCall((client, server) => {
const path_ = '/foo/bar/baz';
server.on('OPEN', mustCall((id, path, pflags, attrs) => {
assert(id === 0, `Wrong request id: ${id}`);
assert(path === path_, `Wrong path: ${path}`);
assert(pflags === OPEN_MODE.READ, `Wrong flags: ${flagsToHuman(pflags)}`);
server.status(id, STATUS_CODE.NO_SUCH_FILE);
server.end();
}));
client.createReadStream(path_).on('error', mustCall((err) => {
assert(err.code === STATUS_CODE.NO_SUCH_FILE);
}));
}));
setup('WriteStream', mustCall((client, server) => {
let writes = 0;
const path_ = '/foo/bar/baz';
const handle_ = Buffer.from('hi mom!');
const data_ = Buffer.from('hello world');
const pflags_ = OPEN_MODE.TRUNC | OPEN_MODE.CREAT | OPEN_MODE.WRITE;
server.on('OPEN', mustCall((id, path, pflags, attrs) => {
assert(id === 0, `Wrong request id: ${id}`);
assert(path === path_, `Wrong path: ${path}`);
assert(pflags === pflags_, `Wrong flags: ${flagsToHuman(pflags)}`);
server.handle(id, handle_);
})).on('FSETSTAT', mustCall((id, handle, attrs) => {
assert(id === 1, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
assert.strictEqual(attrs.mode, 0o666, 'Wrong file mode');
server.status(id, STATUS_CODE.OK);
})).on('WRITE', mustCall((id, handle, offset, data) => {
assert(id === ++writes + 1, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
assert(offset === ((writes - 1) * data_.length),
`Wrong write offset: ${offset}`);
assert.deepStrictEqual(data, data_, 'Wrong data');
server.status(id, STATUS_CODE.OK);
}, 3)).on('CLOSE', mustCall((id, handle) => {
assert(id === 5, `Wrong request id: ${id}`);
assert.deepStrictEqual(handle, handle_, 'handle mismatch');
server.status(id, STATUS_CODE.OK);
server.end();
}));
const writer = client.createWriteStream(path_);
writer.cork && writer.cork();
writer.write(data_);
writer.write(data_);
writer.write(data_);
writer.uncork && writer.uncork();
writer.end();
}));
{
const { client, server } = setup_(
'SFTP server aborts with exit-status',
{
client: { username: 'foo', password: 'bar' },
server: { hostKeys: [ fixture('ssh_host_rsa_key') ] },
},
);
server.on('connection', mustCall((conn) => {
conn.on('authentication', mustCall((ctx) => {
ctx.accept();
})).on('ready', mustCall(() => {
conn.on('session', mustCall((accept, reject) => {
accept().on('sftp', mustCall((accept, reject) => {
const sftp = accept();
// XXX: hack
sftp._protocol.exitStatus(sftp.outgoing.id, 127);
sftp._protocol.channelClose(sftp.outgoing.id);
}));
}));
}));
}));
client.on('ready', mustCall(() => {
const timeout = setTimeout(mustNotCall(), 1000);
client.sftp(mustCall((err, sftp) => {
clearTimeout(timeout);
assert(err, 'Expected error');
assert(err.code === 127, `Expected exit code 127, saw: ${err.code}`);
client.end();
}));
}));
}
// =============================================================================
function setup(title, cb) {
const { client, server } = setupSimple(DEBUG, title);
let clientSFTP;
let serverSFTP;
const onSFTP = mustCall(() => {
if (clientSFTP && serverSFTP)
cb(clientSFTP, serverSFTP);
}, 2);
client.on('ready', mustCall(() => {
client.sftp(mustCall((err, sftp) => {
assert(!err, `[${title}] Unexpected client sftp start error: ${err}`);
sftp.on('close', mustCall(() => {
client.end();
}));
clientSFTP = sftp;
onSFTP();
}));
}));
server.on('connection', mustCall((conn) => {
conn.on('ready', mustCall(() => {
conn.on('session', mustCall((accept, reject) => {
accept().on('sftp', mustCall((accept, reject) => {
const sftp = accept();
sftp.on('close', mustCall(() => {
conn.end();
}));
serverSFTP = sftp;
onSFTP();
}));
}));
}));
}));
}
function flagsToHuman(flags) {
const ret = [];
for (const [name, value] of Object.entries(OPEN_MODE)) {
if (flags & value)
ret.push(name);
}
return ret.join(' | ');
}