test-shell.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. 'use strict';
  2. const assert = require('assert');
  3. const { inspect } = require('util');
  4. const {
  5. mustCall,
  6. mustCallAtLeast,
  7. setupSimple,
  8. } = require('./common.js');
  9. const DEBUG = false;
  10. const setup = setupSimple.bind(undefined, DEBUG);
  11. {
  12. const { client, server } = setup('Simple shell()');
  13. const OUTPUT = 'shell output!\n';
  14. server.on('connection', mustCall((conn) => {
  15. conn.on('ready', mustCall(() => {
  16. conn.on('session', mustCall((accept, reject) => {
  17. const session = accept();
  18. session.on('pty', mustCall((accept, reject, info) => {
  19. accept();
  20. session.on('shell', mustCall((accept, reject) => {
  21. let input = '';
  22. const stream = accept();
  23. stream.write(OUTPUT);
  24. stream.on('data', mustCallAtLeast((data) => {
  25. input += data;
  26. if (input === 'exit\n') {
  27. stream.end();
  28. conn.end();
  29. }
  30. }));
  31. }));
  32. }));
  33. }));
  34. }));
  35. }));
  36. client.on('ready', mustCall(() => {
  37. let output = '';
  38. client.on('close', mustCall(() => {
  39. assert(output === OUTPUT, `Wrong shell output: ${inspect(output)}`);
  40. })).shell(mustCall((err, stream) => {
  41. assert(!err, `Unexpected shell error: ${err}`);
  42. stream.write('exit\n');
  43. stream.on('data', mustCallAtLeast((d) => {
  44. output += d;
  45. })).on('close', mustCall(() => {}));
  46. }));
  47. }));
  48. }
  49. {
  50. const { client, server } = setup('Shell with environment set');
  51. const OUTPUT = 'shell output!\n';
  52. const clientEnv = { SSH2NODETEST: 'foo' };
  53. server.on('connection', mustCall((conn) => {
  54. conn.on('ready', mustCall(() => {
  55. conn.on('session', mustCall((accept, reject) => {
  56. let pty = false;
  57. let env = false;
  58. accept().on('pty', mustCall((accept, reject, info) => {
  59. accept();
  60. pty = true;
  61. })).on('env', mustCall((accept, reject, info) => {
  62. accept && accept();
  63. env = true;
  64. assert(info.key === Object.keys(clientEnv)[0],
  65. `Wrong env key: ${inspect(info.key)}`);
  66. assert(info.val === Object.values(clientEnv)[0],
  67. `Wrong env value: ${inspect(info.val)}`);
  68. })).on('shell', mustCall((accept, reject) => {
  69. assert(pty, 'Expected pty before shell');
  70. assert(env, 'Expected env before shell');
  71. let input = '';
  72. const stream = accept();
  73. stream.write(OUTPUT);
  74. stream.on('data', mustCallAtLeast((data) => {
  75. input += data;
  76. if (input === 'exit\n') {
  77. stream.end();
  78. conn.end();
  79. }
  80. }));
  81. }));
  82. }));
  83. }));
  84. }));
  85. client.on('ready', mustCall(() => {
  86. let output = '';
  87. client.on('close', mustCall(() => {
  88. assert(output === OUTPUT, `Wrong shell output: ${inspect(output)}`);
  89. })).shell({ env: clientEnv }, mustCall((err, stream) => {
  90. assert(!err, `Unexpected shell error: ${err}`);
  91. stream.write('exit\n');
  92. stream.on('data', mustCallAtLeast((d) => {
  93. output += d;
  94. })).on('close', mustCall(() => {}));
  95. }));
  96. }));
  97. }