diff options
author | Philip Lorenz <philip@bithub.de> | 2014-09-21 12:31:40 +0200 |
---|---|---|
committer | Philip Lorenz <philip@bithub.de> | 2014-09-21 22:31:05 +0200 |
commit | 2a568863bb462fbae50fdd4795bf4d3e05e101bb (patch) | |
tree | bf4e9bbfca88d9c38ec4f3b0e6ad28fe64780586 /tests | |
parent | 84995f99a9528b84bd1666060c832ca673641a53 (diff) |
Support transmission of environment variables
The SSH protocol allows the client to transmit environment variables to
the server. This is particularly useful if the user wants to modify the
environment of an executed command without having to reexecute the
actual command from a shell.
This patch extends the Client and Channel interface to allow
the transmission of environment variables to the server side.
In order to use this feature the SSH server must accept environment
variables from the client (e.g. the AcceptEnv configuration directive of
OpenSSH).
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_client.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/test_client.py b/tests/test_client.py index 28d1cb46..0022a4d9 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -79,6 +79,16 @@ class NullServer (paramiko.ServerInterface): return False return True + def check_channel_env_request(self, channel, name, value): + if name == 'INVALID_ENV': + return False + + if not hasattr(channel, 'env'): + setattr(channel, 'env', {}) + + channel.env[name] = value + return True + class SSHClientTest (unittest.TestCase): @@ -344,3 +354,38 @@ class SSHClientTest (unittest.TestCase): password='pygmalion', banner_timeout=0.5 ) + + def test_update_environment(self): + """ + Verify that environment variables can be set by the client. + """ + threading.Thread(target=self._run).start() + + self.tc = paramiko.SSHClient() + self.tc.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + self.assertEqual(0, len(self.tc.get_host_keys())) + self.tc.connect(self.addr, self.port, username='slowdive', password='pygmalion') + + self.event.wait(1.0) + self.assertTrue(self.event.isSet()) + self.assertTrue(self.ts.is_active()) + + target_env = {b'A': b'B', b'C': b'd'} + + self.tc.exec_command('yes', environment=target_env) + schan = self.ts.accept(1.0) + self.assertEqual(target_env, getattr(schan, 'env', {})) + schan.close() + + # Cannot use assertRaises in context manager mode as it is not supported + # in Python 2.6. + try: + # Verify that a rejection by the server can be detected + self.tc.exec_command('yes', environment={b'INVALID_ENV': b''}) + except SSHException as e: + self.assertTrue('INVALID_ENV' in str(e), + 'Expected variable name in error message') + self.assertTrue(isinstance(e.args[1], SSHException), + 'Expected original SSHException in exception') + else: + self.assertFalse(False, 'SSHException was not thrown.') |