1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
# Copyright (c) 2019 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
# distribution and is available at:
#
# http://www.eclipse.org/legal/epl-2.0/
#
# This program may also be made available under the following secondary
# licenses when the conditions for such availability set forth in the
# Eclipse Public License v2.0 are satisfied:
#
# GNU General Public License, Version 2.0, or any later versions of
# that license
#
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
#
# Contributors:
# Ron Frederick - initial implementation, API, and documentation
"""Unit tests for AsyncSSH subprocess API"""
import asyncio
from signal import SIGINT
import asyncssh
from .server import Server, ServerTestCase
from .util import asynctest, echo
class _SubprocessProtocol(asyncssh.SSHSubprocessProtocol):
"""Unit test SSH subprocess protocol"""
def __init__(self):
self._chan = None
self.recv_buf = {1: [], 2: []}
self.exc = {1: None, 2: None}
def pipe_connection_lost(self, fd, exc):
"""Handle remote process close"""
self.exc[fd] = exc
def pipe_data_received(self, fd, data):
"""Handle data from the remote process"""
self.recv_buf[fd].append(data)
async def _create_subprocess(conn, command=None, **kwargs):
"""Create a client subprocess"""
return await conn.create_subprocess(_SubprocessProtocol, command, **kwargs)
class _SubprocessServer(Server):
"""Server for testing the AsyncSSH subprocess API"""
def begin_auth(self, username):
"""Handle client authentication request"""
return False
def session_requested(self):
"""Handle a request to create a new session"""
return self._begin_session
async def _begin_session(self, stdin, stdout, stderr):
"""Begin processing a new session"""
# pylint: disable=no-self-use
action = stdin.channel.get_command()
if not action:
action = 'echo'
if action == 'exit_status':
stdout.channel.exit(1)
elif action == 'signal':
try:
await stdin.readline()
except asyncssh.SignalReceived as exc:
stdout.channel.exit_with_signal(exc.signal)
else:
await echo(stdin, stdout, stderr)
class _TestSubprocess(ServerTestCase):
"""Unit tests for AsyncSSH subprocess API"""
@classmethod
async def start_server(cls):
"""Start an SSH server for the tests to use"""
return (await cls.create_server(
_SubprocessServer, authorized_client_keys='authorized_keys'))
async def _check_subprocess(self, conn, command=None, *,
encoding=None, **kwargs):
"""Start a subprocess and test if an input line is echoed back"""
transport, protocol = await _create_subprocess(conn, command,
encoding=encoding,
*kwargs)
data = str(id(self))
if encoding is None:
data = data.encode('ascii')
stdin = transport.get_pipe_transport(0)
self.assertTrue(stdin.can_write_eof())
stdin.writelines([data])
self.assertFalse(transport.is_closing())
stdin.write_eof()
self.assertTrue(transport.is_closing())
await transport.wait_closed()
sep = '' if encoding else b''
for buf in protocol.recv_buf.values():
self.assertEqual(sep.join([data]), sep.join(buf))
transport.close()
@asynctest
async def test_shell(self):
"""Test starting a shell"""
async with self.connect() as conn:
await self._check_subprocess(conn)
@asynctest
async def test_exec(self):
"""Test execution of a remote command"""
async with self.connect() as conn:
await self._check_subprocess(conn, 'echo')
@asynctest
async def test_encoding(self):
"""Test setting encoding"""
async with self.connect() as conn:
await self._check_subprocess(conn, 'echo', encoding='ascii')
@asynctest
async def test_input(self):
"""Test providing input when creating a subprocess"""
data = str(id(self)).encode('ascii')
async with self.connect() as conn:
transport, protocol = await _create_subprocess(conn, input=data)
await transport.wait_closed()
for buf in protocol.recv_buf.values():
self.assertEqual(b''.join(buf), data)
@asynctest
async def test_redirect_stderr(self):
"""Test redirecting stderr to file"""
data = str(id(self)).encode('ascii')
async with self.connect() as conn:
transport, protocol = await _create_subprocess(conn,
stderr='stderr')
stdin = transport.get_pipe_transport(0)
stdin.write(data)
stdin.write_eof()
await transport.wait_closed()
with open('stderr', 'rb') as f:
stderr_data = f.read()
self.assertEqual(b''.join(protocol.recv_buf[1]), data)
self.assertEqual(b''.join(protocol.recv_buf[2]), b'')
self.assertEqual(stderr_data, data)
@asynctest
async def test_close(self):
"""Test closing transport"""
async with self.connect() as conn:
transport, protocol = await _create_subprocess(conn)
transport.close()
for buf in protocol.recv_buf.values():
self.assertEqual(b''.join(buf), b'')
@asynctest
async def test_exit_status(self):
"""Test reading exit status"""
async with self.connect() as conn:
transport, protocol = await _create_subprocess(conn, 'exit_status')
await transport.wait_closed()
for buf in protocol.recv_buf.values():
self.assertEqual(b''.join(buf), b'')
self.assertEqual(transport.get_returncode(), 1)
@asynctest
async def test_stdin_abort(self):
"""Test abort on stdin"""
async with self.connect() as conn:
transport, protocol = await _create_subprocess(conn)
stdin = transport.get_pipe_transport(0)
stdin.abort()
for buf in protocol.recv_buf.values():
self.assertEqual(b''.join(buf), b'')
@asynctest
async def test_stdin_close(self):
"""Test closing stdin"""
async with self.connect() as conn:
transport, protocol = await _create_subprocess(conn)
stdin = transport.get_pipe_transport(0)
stdin.close()
for buf in protocol.recv_buf.values():
self.assertEqual(b''.join(buf), b'')
@asynctest
async def test_read_pause(self):
"""Test read pause"""
async with self.connect() as conn:
transport, protocol = await _create_subprocess(conn)
stdin = transport.get_pipe_transport(0)
stdout = transport.get_pipe_transport(1)
stdout.pause_reading()
stdin.write(b'\n')
await asyncio.sleep(0.1)
for buf in protocol.recv_buf.values():
self.assertEqual(b''.join(buf), b'')
stdout.resume_reading()
for buf in protocol.recv_buf.values():
self.assertEqual(b''.join(buf), b'\n')
stdin.close()
@asynctest
async def test_signal(self):
"""Test sending a signal"""
async with self.connect() as conn:
transport, _ = await _create_subprocess(conn, 'signal')
transport.send_signal(SIGINT)
await transport.wait_closed()
self.assertEqual(transport.get_returncode(), -SIGINT)
@asynctest
async def test_misc(self):
"""Test other transport and pipe methods"""
async with self.connect() as conn:
transport, _ = await _create_subprocess(conn)
self.assertEqual(transport.get_pid(), None)
stdin = transport.get_pipe_transport(0)
self.assertEqual(transport.get_extra_info('socket'),
stdin.get_extra_info('socket'))
stdin.set_write_buffer_limits()
self.assertEqual(stdin.get_write_buffer_size(), 0)
stdin.close()
|