UNPKG

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