test-durability-ssh.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. var SSH2Stream = require('../lib/ssh');
  2. var fs = require('fs');
  3. var path = require('path');
  4. var inspect = require('util').inspect;
  5. var inherits = require('util').inherits;
  6. var TransformStream = require('stream').Transform;
  7. var assert = require('assert');
  8. var t = -1;
  9. var group = path.basename(__filename, '.js') + '/';
  10. var fixturesdir = path.join(__dirname, 'fixtures');
  11. var HOST_KEY_RSA = fs.readFileSync(path.join(fixturesdir, 'openssh_new_rsa'));
  12. var SERVER_CONFIG = {
  13. server: true,
  14. hostKeys: { 'ssh-rsa': HOST_KEY_RSA }
  15. };
  16. function SimpleStream() {
  17. TransformStream.call(this);
  18. this.buffer = '';
  19. }
  20. inherits(SimpleStream, TransformStream);
  21. SimpleStream.prototype._transform = function(chunk, encoding, cb) {
  22. this.buffer += chunk.toString('binary');
  23. cb();
  24. };
  25. var tests = [
  26. { run: function() {
  27. var what = this.what;
  28. var serverError = false;
  29. var server = new SSH2Stream(SERVER_CONFIG);
  30. var client = new SimpleStream();
  31. client.pipe(server).pipe(client);
  32. server.on('error', function(err) {
  33. serverError = err;
  34. assert(err.message === 'Protocol version not supported',
  35. makeMsg(what, 'Wrong error message'));
  36. }).on('end', function() {
  37. assert(client.buffer === server.config.ident + '\r\n',
  38. makeMsg(what, 'Wrong server ident: ' + inspect(client.buffer)));
  39. assert(serverError, makeMsg(what, 'Expected server error'));
  40. next();
  41. });
  42. client.push('SSH-1.0-aaa\r\n');
  43. },
  44. what: 'Incompatible client SSH protocol version'
  45. },
  46. { run: function() {
  47. var what = this.what;
  48. var serverError = false;
  49. var server = new SSH2Stream(SERVER_CONFIG);
  50. var client = new SimpleStream();
  51. client.pipe(server).pipe(client);
  52. server.on('error', function(err) {
  53. serverError = err;
  54. assert(err.message === 'Bad identification start',
  55. makeMsg(what, 'Wrong error message'));
  56. }).on('end', function() {
  57. assert(client.buffer === server.config.ident + '\r\n',
  58. makeMsg(what, 'Wrong server ident: ' + inspect(client.buffer)));
  59. assert(serverError, makeMsg(what, 'Expected server error'));
  60. next();
  61. });
  62. client.push('LOL-2.0-asdf\r\n');
  63. },
  64. what: 'Malformed client protocol identification'
  65. },
  66. { run: function() {
  67. var what = this.what;
  68. var serverError = false;
  69. var server = new SSH2Stream(SERVER_CONFIG);
  70. var client = new SimpleStream();
  71. client.pipe(server).pipe(client);
  72. server.on('error', function(err) {
  73. serverError = err;
  74. assert(err.message === 'Max identification string size exceeded',
  75. makeMsg(what, 'Wrong error message'));
  76. }).on('end', function() {
  77. assert(client.buffer === server.config.ident + '\r\n',
  78. makeMsg(what, 'Wrong server ident: ' + inspect(client.buffer)));
  79. assert(serverError, makeMsg(what, 'Expected server error'));
  80. next();
  81. });
  82. var ident = 'SSH-2.0-';
  83. for (var i = 0; i < 30; ++i)
  84. ident += 'foobarbaz';
  85. ident += '\r\n';
  86. client.push(ident);
  87. },
  88. what: 'SSH client protocol identification too long (> 255 characters)'
  89. },
  90. { run: function() {
  91. var what = this.what;
  92. var serverError = false;
  93. var server = new SSH2Stream(SERVER_CONFIG);
  94. var client = new SimpleStream();
  95. client.pipe(server).pipe(client);
  96. server.on('error', function(err) {
  97. serverError = err;
  98. assert(err.message === 'Bad packet length',
  99. makeMsg(what, 'Wrong error message'));
  100. }).on('end', function() {
  101. assert(client.buffer.length, makeMsg(what, 'Expected server data'));
  102. assert(serverError, makeMsg(what, 'Expected server error'));
  103. next();
  104. });
  105. client.push('SSH-2.0-asdf\r\n');
  106. // 500,000 byte packet_length
  107. client.push(Buffer.from([0x00, 0x07, 0xA1, 0x20, 0x00, 0x00, 0x00, 0x00]));
  108. },
  109. what: 'Bad packet length (max)'
  110. },
  111. { run: function() {
  112. var what = this.what;
  113. var serverError = false;
  114. var server = new SSH2Stream(SERVER_CONFIG);
  115. var client = new SimpleStream();
  116. client.pipe(server).pipe(client);
  117. server.on('error', function(err) {
  118. serverError = err;
  119. assert(err.message === 'Bad packet length',
  120. makeMsg(what, 'Wrong error message'));
  121. }).on('end', function() {
  122. assert(client.buffer.length, makeMsg(what, 'Expected server data'));
  123. assert(serverError, makeMsg(what, 'Expected server error'));
  124. next();
  125. });
  126. client.push('SSH-2.0-asdf\r\n');
  127. client.push(Buffer.from([0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]));
  128. },
  129. what: 'Bad packet length (min)'
  130. },
  131. ];
  132. function next() {
  133. if (Array.isArray(process._events.exit))
  134. process._events.exit = process._events.exit[1];
  135. if (++t === tests.length)
  136. return;
  137. var v = tests[t];
  138. v.run.call(v);
  139. }
  140. function makeMsg(what, msg) {
  141. return '[' + group + what + ']: ' + msg;
  142. }
  143. process.once('exit', function() {
  144. assert(t === tests.length,
  145. makeMsg('_exit',
  146. 'Only finished ' + t + '/' + tests.length + ' tests'));
  147. });
  148. next();