12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313 |
- var SFTPStream = require('../lib/sftp');
- var Stats = SFTPStream.Stats;
- var STATUS_CODE = SFTPStream.STATUS_CODE;
- var OPEN_MODE = SFTPStream.OPEN_MODE;
- var constants = require('constants');
- var basename = require('path').basename;
- var assert = require('assert');
- var group = basename(__filename, '.js') + '/';
- var t = -1;
- var tests = [
- // successful client requests
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var path_ = '/tmp/foo.txt';
- var handle_ = Buffer.from('node.js');
- server.on('OPEN', function(id, path, pflags, attrs) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert(path === path_, makeMsg(what, 'Wrong path: ' + path));
- assert(pflags === (OPEN_MODE.TRUNC
- | OPEN_MODE.CREAT
- | OPEN_MODE.WRITE),
- makeMsg(what, 'Wrong flags: ' + flagsToHuman(pflags)));
- server.handle(id, handle_);
- server.end();
- });
- client.open(path_, 'w', function(err, handle) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected open() error: ' + err));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- });
- };
- },
- what: 'open'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var handle_ = Buffer.from('node.js');
- server.on('CLOSE', function(id, handle) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- server.status(id, STATUS_CODE.OK);
- server.end();
- });
- client.close(handle_, function(err) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected close() error: ' + err));
- });
- };
- },
- what: 'close'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var handle_ = Buffer.from('node.js');
- var expected =
- Buffer.from('node.jsnode.jsnode.jsnode.jsnode.jsnode.js');
- var buffer = Buffer.alloc(expected.length);
- server.on('READ', function(id, handle, offset, len) {
- assert(++self.state.requests <= 2,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- assert(offset === 5, makeMsg(what, 'Wrong read offset: ' + offset));
- assert(len === buffer.length,
- makeMsg(what, 'Wrong read len: ' + len));
- server.data(id, expected);
- server.end();
- });
- client.readData(handle_, buffer, 0, buffer.length, 5, clientReadCb);
- function clientReadCb(err, nb) {
- assert(++self.state.responses <= 2,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected readData() error: ' + err));
- assert.deepEqual(buffer,
- expected,
- makeMsg(what, 'read data mismatch'));
- }
- };
- },
- what: 'readData'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var handle_ = Buffer.from('node.js');
- var buf = Buffer.from('node.jsnode.jsnode.jsnode.jsnode.jsnode.js');
- server.on('WRITE', function(id, handle, offset, data) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- assert(offset === 5, makeMsg(what, 'Wrong write offset: ' + offset));
- assert.deepEqual(data, buf, makeMsg(what, 'write data mismatch'));
- server.status(id, STATUS_CODE.OK);
- server.end();
- });
- client.writeData(handle_, buf, 0, buf.length, 5, function(err, nb) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected writeData() error: ' + err));
- assert.equal(nb, buf.length);
- });
- };
- },
- what: 'write'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var handle_ = Buffer.from('node.js');
- var buf = Buffer.allocUnsafe(3 * 32 * 1024);
- server.on('WRITE', function(id, handle, offset, data) {
- ++self.state.requests;
- assert.equal(id,
- self.state.requests - 1,
- makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- assert.equal(offset,
- (self.state.requests - 1) * 32 * 1024,
- makeMsg(what, 'Wrong write offset: ' + offset));
- assert((offset + data.length) <= buf.length);
- assert.deepEqual(data,
- buf.slice(offset, offset + data.length),
- makeMsg(what, 'write data mismatch'));
- server.status(id, STATUS_CODE.OK);
- if (self.state.requests === 3)
- server.end();
- });
- client.writeData(handle_, buf, 0, buf.length, 0, function(err, nb) {
- ++self.state.responses;
- assert(!err, makeMsg(what, 'Unexpected writeData() error: ' + err));
- assert.equal(nb, buf.length);
- });
- };
- },
- expected: {
- requests: 3,
- responses: 1
- },
- what: 'write (overflow)'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var path_ = '/foo/bar/baz';
- var attrs_ = new Stats({
- size: 10 * 1024,
- uid: 9001,
- gid: 9001,
- atime: (Date.now() / 1000) | 0,
- mtime: (Date.now() / 1000) | 0
- });
- server.on('LSTAT', function(id, path) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert(path === path_, makeMsg(what, 'Wrong path: ' + path));
- server.attrs(id, attrs_);
- server.end();
- });
- client.lstat(path_, function(err, attrs) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected lstat() error: ' + err));
- assert.deepEqual(attrs, attrs_, makeMsg(what, 'attrs mismatch'));
- });
- };
- },
- what: 'lstat'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var handle_ = Buffer.from('node.js');
- var attrs_ = new Stats({
- size: 10 * 1024,
- uid: 9001,
- gid: 9001,
- atime: (Date.now() / 1000) | 0,
- mtime: (Date.now() / 1000) | 0
- });
- server.on('FSTAT', function(id, handle) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- server.attrs(id, attrs_);
- server.end();
- });
- client.fstat(handle_, function(err, attrs) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected fstat() error: ' + err));
- assert.deepEqual(attrs, attrs_, makeMsg(what, 'attrs mismatch'));
- });
- };
- },
- what: 'fstat'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var path_ = '/foo/bar/baz';
- var attrs_ = new Stats({
- uid: 9001,
- gid: 9001,
- atime: (Date.now() / 1000) | 0,
- mtime: (Date.now() / 1000) | 0
- });
- server.on('SETSTAT', function(id, path, attrs) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert(path === path_, makeMsg(what, 'Wrong path: ' + path));
- assert.deepEqual(attrs, attrs_, makeMsg(what, 'attrs mismatch'));
- server.status(id, STATUS_CODE.OK);
- server.end();
- });
- client.setstat(path_, attrs_, function(err) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected setstat() error: ' + err));
- });
- };
- },
- what: 'setstat'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var handle_ = Buffer.from('node.js');
- var attrs_ = new Stats({
- uid: 9001,
- gid: 9001,
- atime: (Date.now() / 1000) | 0,
- mtime: (Date.now() / 1000) | 0
- });
- server.on('FSETSTAT', function(id, handle, attrs) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- assert.deepEqual(attrs, attrs_, makeMsg(what, 'attrs mismatch'));
- server.status(id, STATUS_CODE.OK);
- server.end();
- });
- client.fsetstat(handle_, attrs_, function(err) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected fsetstat() error: ' + err));
- });
- };
- },
- what: 'fsetstat'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var handle_ = Buffer.from('node.js');
- var path_ = '/tmp';
- server.on('OPENDIR', function(id, path) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert(path === path_, makeMsg(what, 'Wrong path: ' + path));
- server.handle(id, handle_);
- server.end();
- });
- client.opendir(path_, function(err, handle) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected opendir() error: ' + err));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- });
- };
- },
- what: 'opendir'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var handle_ = Buffer.from('node.js');
- var list_ = [
- { filename: '.',
- longname: 'drwxr-xr-x 56 nodejs nodejs 4096 Nov 10 01:05 .',
- attrs: new Stats({
- mode: 0755 | 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: 0755 | 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: 0777 | 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: 0644 | constants.S_IFREG,
- size: 513901992,
- uid: 9001,
- gid: 8001,
- atime: 1259972199,
- mtime: 1259972199
- })
- }
- ];
- server.on('READDIR', function(id, handle) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- server.name(id, list_);
- server.end();
- });
- client.readdir(handle_, function(err, list) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected readdir() error: ' + err));
- assert.deepEqual(list,
- list_.slice(2),
- makeMsg(what, 'dir list mismatch'));
- });
- };
- },
- what: 'readdir'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var handle_ = Buffer.from('node.js');
- var list_ = [
- { filename: '.',
- longname: 'drwxr-xr-x 56 nodejs nodejs 4096 Nov 10 01:05 .',
- attrs: new Stats({
- mode: 0755 | 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: 0755 | 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: 0777 | 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: 0644 | constants.S_IFREG,
- size: 513901992,
- uid: 9001,
- gid: 8001,
- atime: 1259972199,
- mtime: 1259972199
- })
- }
- ];
- server.on('READDIR', function(id, handle) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- server.name(id, list_);
- server.end();
- });
- client.readdir(handle_, { full: true }, function(err, list) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected readdir() error: ' + err));
- assert.deepEqual(list, list_, makeMsg(what, 'dir list mismatch'));
- });
- };
- },
- what: 'readdir (full)'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var path_ = '/foo/bar/baz';
- server.on('REMOVE', function(id, path) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert(path === path_, makeMsg(what, 'Wrong path: ' + path));
- server.status(id, STATUS_CODE.OK);
- server.end();
- });
- client.unlink(path_, function(err) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected unlink() error: ' + err));
- });
- };
- },
- what: 'remove'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var path_ = '/foo/bar/baz';
- server.on('MKDIR', function(id, path) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert(path === path_, makeMsg(what, 'Wrong path: ' + path));
- server.status(id, STATUS_CODE.OK);
- server.end();
- });
- client.mkdir(path_, function(err) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected mkdir() error: ' + err));
- });
- };
- },
- what: 'mkdir'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var path_ = '/foo/bar/baz';
- server.on('RMDIR', function(id, path) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert(path === path_, makeMsg(what, 'Wrong path: ' + path));
- server.status(id, STATUS_CODE.OK);
- server.end();
- });
- client.rmdir(path_, function(err) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected rmdir() error: ' + err));
- });
- };
- },
- what: 'rmdir'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var path_ = '/foo/bar/baz';
- var name_ = { filename: '/tmp/foo' };
- server.on('REALPATH', function(id, path) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert(path === path_, makeMsg(what, 'Wrong path: ' + path));
- server.name(id, name_);
- server.end();
- });
- client.realpath(path_, function(err, name) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected realpath() error: ' + err));
- assert.deepEqual(name,
- name_.filename,
- makeMsg(what, 'name mismatch'));
- });
- };
- },
- what: 'realpath'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var path_ = '/foo/bar/baz';
- var attrs_ = new Stats({
- size: 10 * 1024,
- uid: 9001,
- gid: 9001,
- atime: (Date.now() / 1000) | 0,
- mtime: (Date.now() / 1000) | 0
- });
- server.on('STAT', function(id, path) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert(path === path_, makeMsg(what, 'Wrong path: ' + path));
- server.attrs(id, attrs_);
- server.end();
- });
- client.stat(path_, function(err, attrs) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected stat() error: ' + err));
- assert.deepEqual(attrs, attrs_, makeMsg(what, 'attrs mismatch'));
- });
- };
- },
- what: 'stat'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var oldPath_ = '/foo/bar/baz';
- var newPath_ = '/tmp/foo';
- server.on('RENAME', function(id, oldPath, newPath) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert(oldPath === oldPath_,
- makeMsg(what, 'Wrong old path: ' + oldPath));
- assert(newPath === newPath_,
- makeMsg(what, 'Wrong new path: ' + newPath));
- server.status(id, STATUS_CODE.OK);
- server.end();
- });
- client.rename(oldPath_, newPath_, function(err) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected rename() error: ' + err));
- });
- };
- },
- what: 'rename'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var linkPath_ = '/foo/bar/baz';
- var name = { filename: '/tmp/foo' };
- server.on('READLINK', function(id, linkPath) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert(linkPath === linkPath_,
- makeMsg(what, 'Wrong link path: ' + linkPath));
- server.name(id, name);
- server.end();
- });
- client.readlink(linkPath_, function(err, targetPath) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected readlink() error: ' + err));
- assert(targetPath === name.filename,
- makeMsg(what, 'Wrong target path: ' + targetPath));
- });
- };
- },
- what: 'readlink'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var linkPath_ = '/foo/bar/baz';
- var targetPath_ = '/tmp/foo';
- server.on('SYMLINK', function(id, linkPath, targetPath) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert(linkPath === linkPath_,
- makeMsg(what, 'Wrong link path: ' + linkPath));
- assert(targetPath === targetPath_,
- makeMsg(what, 'Wrong target path: ' + targetPath));
- server.status(id, STATUS_CODE.OK);
- server.end();
- });
- client.symlink(targetPath_, linkPath_, function(err) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(!err, makeMsg(what, 'Unexpected symlink() error: ' + err));
- });
- };
- },
- what: 'symlink'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var path_ = '/foo/bar/baz';
- var handle_ = Buffer.from('hi mom!');
- var data_ = Buffer.from('hello world');
- server.once('OPEN', function(id, path, pflags, attrs) {
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert(path === path_, makeMsg(what, 'Wrong path: ' + path));
- assert(pflags === OPEN_MODE.READ,
- makeMsg(what, 'Wrong flags: ' + flagsToHuman(pflags)));
- server.handle(id, handle_);
- }).once('FSTAT', function(id, handle) {
- assert(id === 1, makeMsg(what, 'Wrong request id: ' + id));
- var attrs = new Stats({
- size: data_.length,
- uid: 9001,
- gid: 9001,
- atime: (Date.now() / 1000) | 0,
- mtime: (Date.now() / 1000) | 0
- });
- server.attrs(id, attrs);
- }).once('READ', function(id, handle, offset, len) {
- assert(id === 2, makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- assert(offset === 0, makeMsg(what, 'Wrong read offset: ' + offset));
- server.data(id, data_);
- }).once('CLOSE', function(id, handle) {
- ++self.state.requests;
- assert(id === 3, makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- server.status(id, STATUS_CODE.OK);
- server.end();
- });
- client.readFile(path_, function(err, buf) {
- ++self.state.responses;
- assert(!err, makeMsg(what, 'Unexpected error: ' + err));
- assert.deepEqual(buf, data_, makeMsg(what, 'data mismatch'));
- });
- };
- },
- what: 'readFile'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var path_ = '/foo/bar/baz';
- var handle_ = Buffer.from('hi mom!');
- var data_ = Buffer.from('hello world');
- var reads = 0;
- server.once('OPEN', function(id, path, pflags, attrs) {
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert(path === path_, makeMsg(what, 'Wrong path: ' + path));
- assert(pflags === OPEN_MODE.READ,
- makeMsg(what, 'Wrong flags: ' + flagsToHuman(pflags)));
- server.handle(id, handle_);
- }).once('FSTAT', function(id, handle) {
- assert(id === 1, makeMsg(what, 'Wrong request id: ' + id));
- var attrs = new Stats({
- uid: 9001,
- gid: 9001,
- atime: (Date.now() / 1000) | 0,
- mtime: (Date.now() / 1000) | 0
- });
- server.attrs(id, attrs);
- }).on('READ', function(id, handle, offset, len) {
- assert(++reads <= 2, makeMsg(what, 'Saw too many READs'));
- assert(id === 2 || id === 3,
- makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- switch(id) {
- case 2:
- assert(offset === 0,
- makeMsg(what,
- 'Wrong read offset for first read: ' + offset));
- server.data(id, data_);
- break;
- case 3:
- assert(offset === data_.length,
- makeMsg(what,
- 'Wrong read offset for second read: ' + offset));
- server.status(id, STATUS_CODE.EOF);
- break;
- }
- }).once('CLOSE', function(id, handle) {
- ++self.state.requests;
- assert(id === 4, makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- server.status(id, STATUS_CODE.OK);
- server.end();
- });
- client.readFile(path_, function(err, buf) {
- ++self.state.responses;
- assert(!err, makeMsg(what, 'Unexpected error: ' + err));
- assert.deepEqual(buf, data_, makeMsg(what, 'data mismatch'));
- });
- };
- },
- what: 'readFile (no size from fstat)'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var opens = 0;
- var reads = 0;
- var closes = 0;
- var path_ = '/foo/bar/baz';
- var handle_ = Buffer.from('hi mom!');
- var data_ = Buffer.from('hello world');
- server.on('OPEN', function(id, path, pflags, attrs) {
- assert(++opens === 1, makeMsg(what, 'Saw too many OPENs'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert(path === path_, makeMsg(what, 'Wrong path: ' + path));
- assert(pflags === OPEN_MODE.READ,
- makeMsg(what, 'Wrong flags: ' + flagsToHuman(pflags)));
- server.handle(id, handle_);
- }).on('READ', function(id, handle, offset, len) {
- assert(++reads <= 2, makeMsg(what, 'Saw too many READs'));
- assert(id === reads, makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- if (reads === 1) {
- assert(offset === 0, makeMsg(what, 'Wrong read offset: ' + offset));
- server.data(id, data_);
- } else
- server.status(id, STATUS_CODE.EOF);
- }).on('CLOSE', function(id, handle) {
- ++self.state.requests;
- assert(++closes === 1, makeMsg(what, 'Saw too many CLOSEs'));
- assert(id === 3, makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- server.status(id, STATUS_CODE.OK);
- server.end();
- });
- var buf = [];
- client.createReadStream(path_).on('readable', function() {
- var chunk;
- while ((chunk = this.read()) !== null) {
- buf.push(chunk);
- }
- }).on('end', function() {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- buf = Buffer.concat(buf);
- assert.deepEqual(buf, data_, makeMsg(what, 'data mismatch'));
- });
- };
- },
- what: 'ReadStream'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var opens = 0;
- var path_ = '/foo/bar/baz';
- var error;
- server.on('OPEN', function(id, path, pflags, attrs) {
- ++opens;
- ++self.state.requests;
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert(path === path_, makeMsg(what, 'Wrong path: ' + path));
- assert(pflags === OPEN_MODE.READ,
- makeMsg(what, 'Wrong flags: ' + flagsToHuman(pflags)));
- server.status(id, STATUS_CODE.NO_SUCH_FILE);
- server.end();
- });
- client.createReadStream(path_).on('error', function(err) {
- error = err;
- }).on('close', function() {
- assert(opens === 1, makeMsg(what, 'Saw ' + opens + ' OPENs'));
- assert(error, makeMsg(what, 'Expected error'));
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- });
- };
- },
- what: 'ReadStream (error)'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var opens = 0;
- var writes = 0;
- var closes = 0;
- var fsetstat = false;
- var path_ = '/foo/bar/baz';
- var handle_ = Buffer.from('hi mom!');
- var data_ = Buffer.from('hello world');
- var expFlags = OPEN_MODE.TRUNC | OPEN_MODE.CREAT | OPEN_MODE.WRITE;
- server.on('OPEN', function(id, path, pflags, attrs) {
- assert(++opens === 1, makeMsg(what, 'Saw too many OPENs'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert(path === path_, makeMsg(what, 'Wrong path: ' + path));
- assert(pflags === expFlags,
- makeMsg(what, 'Wrong flags: ' + flagsToHuman(pflags)));
- server.handle(id, handle_);
- }).once('FSETSTAT', function(id, handle, attrs) {
- fsetstat = true;
- assert(id === 1, makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- assert.strictEqual(attrs.mode,
- parseInt('0666', 8),
- makeMsg(what, 'Wrong file mode'));
- server.status(id, STATUS_CODE.OK);
- }).on('WRITE', function(id, handle, offset, data) {
- assert(++writes <= 3, makeMsg(what, 'Saw too many WRITEs'));
- assert(id === writes + 1, makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- assert(offset === ((writes - 1) * data_.length),
- makeMsg(what, 'Wrong write offset: ' + offset));
- assert.deepEqual(data, data_, makeMsg(what, 'Wrong data'));
- server.status(id, STATUS_CODE.OK);
- }).on('CLOSE', function(id, handle) {
- ++self.state.requests;
- assert(++closes === 1, makeMsg(what, 'Saw too many CLOSEs'));
- assert(id === 5, makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- server.status(id, STATUS_CODE.OK);
- server.end();
- }).on('end', function() {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(opens === 1, makeMsg(what, 'Wrong OPEN count'));
- assert(writes === 3, makeMsg(what, 'Wrong WRITE count'));
- assert(closes === 1, makeMsg(what, 'Wrong CLOSE count'));
- assert(fsetstat, makeMsg(what, 'Expected FSETSTAT'));
- });
- var writer = client.createWriteStream(path_);
- if (writer.cork)
- writer.cork();
- writer.write(data_);
- writer.write(data_);
- writer.write(data_);
- if (writer.uncork)
- writer.uncork();
- writer.end();
- };
- },
- what: 'WriteStream'
- },
- // other client request scenarios
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var handle_ = Buffer.from('node.js');
- server.on('READDIR', function(id, handle) {
- assert(++self.state.requests === 1,
- makeMsg(what, 'Saw too many requests'));
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- server.status(id, STATUS_CODE.EOF);
- server.end();
- });
- client.readdir(handle_, function(err, list) {
- assert(++self.state.responses === 1,
- makeMsg(what, 'Saw too many responses'));
- assert(err && err.code === STATUS_CODE.EOF,
- makeMsg(what, 'Expected EOF, got: ' + err));
- });
- };
- },
- what: 'readdir (EOF)'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var path_ = '/tmp/foo.txt';
- var reqs = 0;
- var continues = 0;
- client.unpipe(server);
- function clientCb(err, handle) {
- assert(++self.state.responses <= reqs,
- makeMsg(what, 'Saw too many responses'));
- if (self.state.responses === reqs) {
- assert(continues === 1, makeMsg(what, 'no continue event seen'));
- server.end();
- }
- }
- client.on('continue', function() {
- assert(++continues === 1, makeMsg(what, 'saw > 1 continue event'));
- });
- while (true) {
- ++reqs;
- if (!client.open(path_, 'w', clientCb))
- break;
- }
- client.pipe(server);
- };
- },
- expected: {
- requests: -1,
- responses: -1
- },
- what: '"continue" event after push() === false'
- },
- { run: function() {
- var self = this;
- var client = new SFTPStream();
- client.once('ready', function() {
- client.open('/foo/bar', 'w', function(err, handle) {
- assert(err, 'Expected error');
- assert.strictEqual(err.code, 4);
- assert.strictEqual(err.message, 'Uh oh');
- assert.strictEqual(err.lang, '');
- next();
- });
- client.write(Buffer.from([
- 0, 0, 0, 18,
- 101,
- 0, 0, 0, 0,
- 0, 0, 0, SFTPStream.STATUS_CODE.FAILURE,
- 0, 0, 0, 5, 85, 104, 32, 111, 104
- ]));
- });
- client.write(Buffer.from([
- 0, 0, 0, 5,
- 2,
- 0, 0, 0, 3
- ]));
- },
- what: 'Can parse status response without language'
- },
- { run: function() {
- var self = this;
- var client = new SFTPStream();
- client.once('ready', function() {
- client.open('/foo/bar', 'w', function(err, handle) {
- assert(err, 'Expected error');
- assert.strictEqual(err.code, 4);
- assert.strictEqual(err.message, 'Failure');
- assert.strictEqual(err.lang, '');
- next();
- });
- client.write(Buffer.from([
- 0, 0, 0, 9,
- 101,
- 0, 0, 0, 0,
- 0, 0, 0, SFTPStream.STATUS_CODE.FAILURE
- ]));
- });
- client.write(Buffer.from([
- 0, 0, 0, 5,
- 2,
- 0, 0, 0, 3
- ]));
- },
- what: 'Can parse status response without message'
- },
- { run: function() {
- var self = this;
- var err;
- var client = new SFTPStream();
- client.once('ready', function() {
- assert(false, 'Handshake should not succeed');
- }).once('error', function(err_) {
- err = err_;
- }).once('end', function() {
- assert.strictEqual(err && err.message,
- 'Unexpected packet before version');
- next();
- });
- client.write(Buffer.from([
- 1, 2, 3, 4,
- 5,
- 6, 7, 8, 9
- ]));
- },
- what: 'End SFTP stream on bad handshake (client)'
- },
- { run: function() {
- var self = this;
- var err;
- var client = new SFTPStream({ server: true });
- client.once('ready', function() {
- assert(false, 'Handshake should not succeed');
- }).once('error', function(err_) {
- err = err_;
- }).once('end', function() {
- assert.strictEqual(err && err.message,
- 'Unexpected packet before init');
- next();
- });
- client.write(Buffer.from([
- 1, 2, 3, 4,
- 5,
- 6, 7, 8, 9
- ]));
- },
- what: 'End SFTP stream on bad handshake (server)'
- },
- { run: function() {
- setup(this);
- var self = this;
- var what = this.what;
- var client = this.client;
- var server = this.server;
- this.onReady = function() {
- var handle_ = Buffer.from([0,0,0,1]);
- var buffer = Buffer.allocUnsafe(4);
- server.once('READ', function(id, handle, offset, len) {
- ++self.state.requests;
- assert(id === 0, makeMsg(what, 'Wrong request id: ' + id));
- assert.deepEqual(handle, handle_, makeMsg(what, 'handle mismatch'));
- assert(offset === 0, makeMsg(what, 'Wrong read offset: ' + offset));
- assert(len === buffer.length,
- makeMsg(what, 'Wrong read length: ' + len));
- server.data(id, Buffer.alloc(len + 1));
- });
- client.readData(handle_, buffer, 0, buffer.length, 0, clientReadCb);
- function clientReadCb(err, buf) {
- ++self.state.responses;
- assert(err && err.message.indexOf('more data than requested') !== -1,
- makeMsg(what, 'Expected error'));
- server.end();
- }
- };
- },
- what: 'Abort on data chunk larger than requested'
- },
- ];
- function setup(self) {
- var expectedRequests = (self.expected && self.expected.requests) || 1;
- var expectedResponses = (self.expected && self.expected.responses) || 1;
- var clientEnded = false;
- var serverEnded = false;
- self.state = {
- clientReady: false,
- serverReady: false,
- requests: 0,
- responses: 0
- };
- self.client = new SFTPStream();
- self.server = new SFTPStream({ server: true });
- self.server.on('error', onError)
- .on('ready', onReady)
- .on('end', onEnd);
- self.client.on('error', onError)
- .on('ready', onReady)
- .on('end', onEnd);
- function onError(err) {
- var which = (this === self.server ? 'server' : 'client');
- assert(false, makeMsg(self.what, 'Unexpected ' + which + ' error: ' + err));
- }
- function onReady() {
- if (this === self.client) {
- assert(!self.state.clientReady,
- makeMsg(self.what, 'Received multiple ready events for client'));
- self.state.clientReady = true;
- } else {
- assert(!self.state.serverReady,
- makeMsg(self.what, 'Received multiple ready events for server'));
- self.state.serverReady = true;
- }
- if (self.state.clientReady && self.state.serverReady)
- self.onReady && self.onReady();
- }
- function onEnd() {
- if (this === self.client) {
- assert(!clientEnded,
- makeMsg(self.what, 'Received multiple close events for client'));
- clientEnded = true;
- } else {
- assert(!serverEnded,
- makeMsg(self.what, 'Received multiple close events for server'));
- serverEnded = true;
- }
- if (clientEnded && serverEnded) {
- var msg;
- if (expectedRequests > 0) {
- msg = 'Expected ' + expectedRequests + ' request(s) but received '
- + self.state.requests;
- assert(self.state.requests === expectedRequests,
- makeMsg(self.what, msg));
- }
- if (expectedResponses > 0) {
- msg = 'Expected ' + expectedResponses + ' response(s) but received '
- + self.state.responses;
- assert(self.state.responses === expectedResponses,
- makeMsg(self.what, msg));
- }
- next();
- }
- }
- process.nextTick(function() {
- self.client.pipe(self.server).pipe(self.client);
- });
- }
- function flagsToHuman(flags) {
- var ret = [];
- for (var i = 0, keys = Object.keys(OPEN_MODE); i < keys.length; ++i)
- if (flags & OPEN_MODE[keys[i]])
- ret.push(keys[i]);
- return ret.join(' | ');
- }
- function next() {
- if (++t === tests.length)
- return;
- var v = tests[t];
- v.run.call(v);
- }
- function makeMsg(what, msg) {
- return '[' + group + what + ']: ' + msg;
- }
- process.once('exit', function() {
- assert(t === tests.length,
- makeMsg('_exit',
- 'Only finished ' + t + '/' + tests.length + ' tests'));
- });
- next();
|