test-ssh.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. var SSH2Stream = require('../lib/ssh');
  2. var utils = require('../lib/utils');
  3. var parseKey = utils.parseKey;
  4. var basename = require('path').basename;
  5. var assert_ = require('assert');
  6. var inherits = require('util').inherits;
  7. var inspect = require('util').inspect;
  8. var TransformStream = require('stream').Transform;
  9. var fs = require('fs');
  10. var group = basename(__filename, '.js') + '/';
  11. var t = -1;
  12. var SERVER_KEY = fs.readFileSync(__dirname + '/fixtures/openssh_new_rsa');
  13. var HOST_KEYS = { 'ssh-rsa': parseKey(SERVER_KEY) };
  14. var HOST_ALGORITHMS = { serverHostKey: ['ssh-rsa'] };
  15. function SimpleStream() {
  16. TransformStream.call(this);
  17. this.buffer = '';
  18. }
  19. inherits(SimpleStream, TransformStream);
  20. SimpleStream.prototype._transform = function(chunk, encoding, cb) {
  21. this.buffer += chunk.toString('binary');
  22. cb(null, chunk);
  23. };
  24. var tests = [
  25. // client-side tests
  26. { run: function() {
  27. var algos = ['ssh-dss', 'ssh-rsa', 'ecdsa-sha2-nistp521'];
  28. var client = new SSH2Stream({
  29. algorithms: {
  30. serverHostKey: algos
  31. }
  32. });
  33. var clientBufStream = new SimpleStream();
  34. var clientReady = false;
  35. var server = new SSH2Stream({
  36. server: true,
  37. hostKeys: HOST_KEYS,
  38. algorithms: HOST_ALGORITHMS
  39. });
  40. var serverBufStream = new SimpleStream();
  41. var serverReady = false;
  42. function onNEWKEYS() {
  43. if (this === client) {
  44. assert(!clientReady, 'Already received client NEWKEYS event');
  45. clientReady = true;
  46. } else {
  47. assert(!serverReady, 'Already received server NEWKEYS event');
  48. serverReady = true;
  49. }
  50. if (clientReady && serverReady) {
  51. var traffic = clientBufStream.buffer;
  52. var algoList = algos.join(',');
  53. var re = new RegExp('\x00\x00\x00'
  54. + hexByte(algoList.length)
  55. + algoList);
  56. assert(re.test(traffic), 'Unexpected client algorithms');
  57. traffic = serverBufStream.buffer;
  58. assert(/\x00\x00\x00\x07ssh-rsa/.test(traffic),
  59. 'Unexpected server algorithms');
  60. next();
  61. }
  62. }
  63. client.on('NEWKEYS', onNEWKEYS);
  64. server.on('NEWKEYS', onNEWKEYS);
  65. client.pipe(clientBufStream)
  66. .pipe(server)
  67. .pipe(serverBufStream)
  68. .pipe(client);
  69. },
  70. what: 'Custom algorithms'
  71. },
  72. { run: function() {
  73. var serverIdent = 'testing \t';
  74. var expectedFullIdent = 'SSH-2.0-' + serverIdent;
  75. var client = new SSH2Stream({});
  76. client.on('header', function(header) {
  77. assert(header.identRaw === expectedFullIdent,
  78. '\nSaw: ' + inspect(header.identRaw) + '\n'
  79. + 'Expected: ' + inspect(expectedFullIdent));
  80. next();
  81. });
  82. var server = new SSH2Stream({
  83. server: true,
  84. hostKeys: HOST_KEYS,
  85. algorithms: HOST_ALGORITHMS,
  86. ident: serverIdent
  87. });
  88. client.pipe(server).pipe(client);
  89. },
  90. what: 'Remote ident is not trimmed'
  91. }
  92. ];
  93. function hexByte(n) {
  94. return String.fromCharCode(n);
  95. }
  96. function assert(expression, msg) {
  97. msg || (msg = 'failed assertion');
  98. assert_(expression, makeMsg(tests[t].what, msg));
  99. }
  100. function next() {
  101. if (Array.isArray(process._events.exit))
  102. process._events.exit = process._events.exit[1];
  103. if (++t === tests.length)
  104. return;
  105. var v = tests[t];
  106. v.run.call(v);
  107. }
  108. function makeMsg(what, msg) {
  109. return '[' + group + what + ']: ' + msg;
  110. }
  111. process.once('exit', function() {
  112. assert_(t === tests.length,
  113. makeMsg('_exit',
  114. 'Only finished ' + t + '/' + tests.length + ' tests'));
  115. });
  116. next();