| 1 |
import signal |
| 2 |
import socket |
| 3 |
|
| 4 |
from unittest.mock import patch |
| 5 |
from pytest import raises |
| 6 |
|
| 7 |
from paramiko import ProxyCommand, ProxyCommandFailure |
| 8 |
|
| 9 |
|
| 10 |
class TestProxyCommand: |
| 11 |
@patch("paramiko.proxy.subprocess") |
| 12 |
def test_init_takes_command_string(self, subprocess): |
| 13 |
ProxyCommand(command_line="do a thing") |
| 14 |
subprocess.Popen.assert_called_once_with( |
| 15 |
["do", "a", "thing"], |
| 16 |
stdin=subprocess.PIPE, |
| 17 |
stdout=subprocess.PIPE, |
| 18 |
stderr=subprocess.PIPE, |
| 19 |
bufsize=0, |
| 20 |
) |
| 21 |
|
| 22 |
@patch("paramiko.proxy.subprocess.Popen") |
| 23 |
def test_send_writes_to_process_stdin_returning_length(self, Popen): |
| 24 |
proxy = ProxyCommand("hi") |
| 25 |
written = proxy.send(b"data") |
| 26 |
Popen.return_value.stdin.write.assert_called_once_with(b"data") |
| 27 |
assert written == len(b"data") |
| 28 |
|
| 29 |
@patch("paramiko.proxy.subprocess.Popen") |
| 30 |
def test_send_raises_ProxyCommandFailure_on_error(self, Popen): |
| 31 |
Popen.return_value.stdin.write.side_effect = IOError(0, "whoops") |
| 32 |
with raises(ProxyCommandFailure) as info: |
| 33 |
ProxyCommand("hi").send("data") |
| 34 |
assert info.value.command == "hi" |
| 35 |
assert info.value.error == "whoops" |
| 36 |
|
| 37 |
@patch("paramiko.proxy.subprocess.Popen") |
| 38 |
@patch("paramiko.proxy.os.read") |
| 39 |
@patch("paramiko.proxy.select") |
| 40 |
def test_recv_reads_from_process_stdout_returning_bytes( |
| 41 |
self, select, os_read, Popen |
| 42 |
): |
| 43 |
stdout = Popen.return_value.stdout |
| 44 |
select.return_value = [stdout], None, None |
| 45 |
fileno = stdout.fileno.return_value |
| 46 |
# Force os.read to return smaller-than-requested chunks |
| 47 |
os_read.side_effect = [b"was", b"t", b"e", b"of ti", b"me"] |
| 48 |
proxy = ProxyCommand("hi") |
| 49 |
# Ask for 5 bytes (ie b"waste") |
| 50 |
data = proxy.recv(5) |
| 51 |
# Ensure we got "waste" stitched together |
| 52 |
assert data == b"waste" |
| 53 |
# Ensure the calls happened in the sizes expected (starting with the |
| 54 |
# initial "I want all 5 bytes", followed by "I want whatever I believe |
| 55 |
# should be left after what I've already read", until done) |
| 56 |
assert [x[0] for x in os_read.call_args_list] == [ |
| 57 |
(fileno, 5), # initial |
| 58 |
(fileno, 2), # I got 3, want 2 more |
| 59 |
(fileno, 1), # I've now got 4, want 1 more |
| 60 |
] |
| 61 |
|
| 62 |
@patch("paramiko.proxy.subprocess.Popen") |
| 63 |
@patch("paramiko.proxy.os.read") |
| 64 |
@patch("paramiko.proxy.select") |
| 65 |
def test_recv_returns_buffer_on_timeout_if_any_read( |
| 66 |
self, select, os_read, Popen |
| 67 |
): |
| 68 |
stdout = Popen.return_value.stdout |
| 69 |
select.return_value = [stdout], None, None |
| 70 |
fileno = stdout.fileno.return_value |
| 71 |
os_read.side_effect = [b"was", socket.timeout] |
| 72 |
proxy = ProxyCommand("hi") |
| 73 |
data = proxy.recv(5) |
| 74 |
assert data == b"was" # not b"waste" |
| 75 |
assert os_read.call_args[0] == (fileno, 2) |
| 76 |
|
| 77 |
@patch("paramiko.proxy.subprocess.Popen") |
| 78 |
@patch("paramiko.proxy.os.read") |
| 79 |
@patch("paramiko.proxy.select") |
| 80 |
def test_recv_raises_timeout_if_nothing_read(self, select, os_read, Popen): |
| 81 |
stdout = Popen.return_value.stdout |
| 82 |
select.return_value = [stdout], None, None |
| 83 |
fileno = stdout.fileno.return_value |
| 84 |
os_read.side_effect = socket.timeout |
| 85 |
proxy = ProxyCommand("hi") |
| 86 |
with raises(socket.timeout): |
| 87 |
proxy.recv(5) |
| 88 |
assert os_read.call_args[0] == (fileno, 5) |
| 89 |
|
| 90 |
@patch("paramiko.proxy.subprocess.Popen") |
| 91 |
@patch("paramiko.proxy.os.read") |
| 92 |
@patch("paramiko.proxy.select") |
| 93 |
def test_recv_raises_ProxyCommandFailure_on_non_timeout_error( |
| 94 |
self, select, os_read, Popen |
| 95 |
): |
| 96 |
select.return_value = [Popen.return_value.stdout], None, None |
| 97 |
os_read.side_effect = IOError(0, "whoops") |
| 98 |
with raises(ProxyCommandFailure) as info: |
| 99 |
ProxyCommand("hi").recv(5) |
| 100 |
assert info.value.command == "hi" |
| 101 |
assert info.value.error == "whoops" |
| 102 |
|
| 103 |
@patch("paramiko.proxy.subprocess.Popen") |
| 104 |
@patch("paramiko.proxy.os.kill") |
| 105 |
def test_close_kills_subprocess(self, os_kill, Popen): |
| 106 |
proxy = ProxyCommand("hi") |
| 107 |
proxy.close() |
| 108 |
os_kill.assert_called_once_with(Popen.return_value.pid, signal.SIGTERM) |
| 109 |
|
| 110 |
@patch("paramiko.proxy.subprocess.Popen") |
| 111 |
def test_closed_exposes_whether_subprocess_has_exited(self, Popen): |
| 112 |
proxy = ProxyCommand("hi") |
| 113 |
Popen.return_value.returncode = None |
| 114 |
assert proxy.closed is False |
| 115 |
assert proxy._closed is False |
| 116 |
Popen.return_value.returncode = 0 |
| 117 |
assert proxy.closed is True |
| 118 |
assert proxy._closed is True |
| 119 |
|
| 120 |
@patch("paramiko.proxy.time.time") |
| 121 |
@patch("paramiko.proxy.subprocess.Popen") |
| 122 |
@patch("paramiko.proxy.os.read") |
| 123 |
@patch("paramiko.proxy.select") |
| 124 |
def test_timeout_affects_whether_timeout_is_raised( |
| 125 |
self, select, os_read, Popen, time |
| 126 |
): |
| 127 |
stdout = Popen.return_value.stdout |
| 128 |
select.return_value = [stdout], None, None |
| 129 |
# Base case: None timeout means no timing out |
| 130 |
os_read.return_value = b"meh" |
| 131 |
proxy = ProxyCommand("hello") |
| 132 |
assert proxy.timeout is None |
| 133 |
# Implicit 'no raise' check |
| 134 |
assert proxy.recv(3) == b"meh" |
| 135 |
# Use settimeout to set timeout, and it is honored |
| 136 |
time.side_effect = [0, 10] # elapsed > 7 |
| 137 |
proxy = ProxyCommand("ohnoz") |
| 138 |
proxy.settimeout(7) |
| 139 |
assert proxy.timeout == 7 |
| 140 |
with raises(socket.timeout): |
| 141 |
proxy.recv(3) |
| 142 |
|
| 143 |
@patch("paramiko.proxy.subprocess", new=None) |
| 144 |
@patch("paramiko.proxy.subprocess_import_error", new=ImportError("meh")) |
| 145 |
def test_raises_subprocess_ImportErrors_at_runtime(self): |
| 146 |
# Not an ideal test, but I don't know of a non-bad way to fake out |
| 147 |
# module-time ImportErrors. So we mock the symptoms. Meh! |
| 148 |
with raises(ImportError) as info: |
| 149 |
ProxyCommand("hi!!!") |
| 150 |
assert str(info.value) == "meh" |