def test_client_read_timeout(self): client = SSHClient(self.host, port=self.port, pkey=self.user_key, num_retries=1) host_out = client.run_command('sleep 2; echo me', timeout=0.2) self.assertRaises(Timeout, list, host_out.stdout)
def test_client_wait_finished_timeout(self): client = SSHClient(self.host, port=self.port, pkey=self.user_key, num_retries=1, timeout=0.6) chan = client.execute('sleep 1') self.assertRaises(Timeout, client.wait_finished, chan)
def scope_killer(): for _ in range(5): client = SSHClient(self.host, port=self.port, pkey=self.user_key, num_retries=1, allow_agent=False) channel = client.execute(self.cmd) output = list(client.read_output(channel)) self.assertListEqual(output, [b'me'])
def scope_killer(): for _ in range(5): client = SSHClient(self.host, port=self.port, pkey=self.user_key, num_retries=1, allow_agent=False) host_out = client.run_command(self.cmd) output = list(host_out.stdout) self.assertListEqual(output, [self.resp])
def test_client_read_timeout(self): client = SSHClient(self.host, port=self.port, pkey=self.user_key, num_retries=1, timeout=0.2) channel, host, stdout, stderr, stdin = client.run_command( 'sleep 2; echo me') output_gen = client.read_output(channel) self.assertRaises(Timeout, list, output_gen)
def test_open_session_timeout(self): client = SSHClient(self.host, port=self.port, pkey=self.user_key, num_retries=1, timeout=1) def _session(timeout=2): sleep(2) client.open_session = _session self.assertRaises(GTimeout, client.run_command, self.cmd)
def test_open_session_exc(self): class Error(Exception): pass def _session(): raise Error client = SSHClient(self.host, port=self.port, pkey=self.user_key, num_retries=1) client._open_session = _session self.assertRaises(SessionError, client.open_session)
def test_client_exec_timeout(self): client = SSHClient(self.host, port=self.port, pkey=self.user_key, num_retries=1, timeout=0.00001) self.assertRaises(Timeout, client.execute, self.cmd)
def test_session_connect_exc(self): class Error(Exception): pass def _con(): raise Error client = SSHClient( self.host, port=self.port, pkey=self.user_key, num_retries=2, retry_delay=.2, ) client._session_connect = _con self.assertRaises(Error, client._init_session)
def test_client_disconnect_on_del(self): client = SSHClient(self.host, port=self.port, pkey=self.user_key, num_retries=1) client_sock = client.sock del client self.assertTrue(client_sock.closed)
def test_client_bad_sock(self): client = SSHClient(self.host, port=self.port, pkey=self.user_key, num_retries=1) client.disconnect() client.sock = None self.assertIsNone(client.poll())
def test_password_auth_failure(self): try: client = SSHClient( self.host, port=self.port, num_retries=1, allow_agent=False, identity_auth=False, password='******', ) except AuthenticationException as ex: self.assertIsInstance(ex.args[3], AuthenticationDenied) else: raise AssertionError
def setUpClass(cls): _mask = int('0600') if version_info <= (2, ) else 0o600 os.chmod(PKEY_FILENAME, _mask) cls.host = '127.0.0.1' cls.port = 2322 cls.server = OpenSSHServer(listen_ip=cls.host, port=cls.port) cls.server.start_server() cls.cmd = 'echo me' cls.resp = u'me' cls.user_key = PKEY_FILENAME cls.user_pub_key = PUB_FILE cls.user = pwd.getpwuid(os.geteuid()).pw_name cls.client = SSHClient(cls.host, port=cls.port, pkey=PKEY_FILENAME, num_retries=1)
def test_agent_auth_fake_success(self): def _agent_auth(): return client = SSHClient(self.host, port=self.port, pkey=self.user_key, num_retries=1, allow_agent=True, identity_auth=False) client.session.disconnect() client.pkey = None client._connect(self.host, self.port) client._agent_auth = _agent_auth self.assertIsNone(client.auth())
def test_disconnect_exc(self): class DiscError(Exception): pass def _disc(): raise DiscError client = SSHClient( self.host, port=self.port, pkey=self.user_key, num_retries=1, retry_delay=.1, ) client._disconnect_eagain = _disc client._connect_init_session_retry(0) client.disconnect()
def setUpClass(cls): _mask = int('0600') if version_info <= (2, ) else 0o600 for _file in [PKEY_FILENAME, USER_CERT_PRIV_KEY, CA_USER_KEY]: os.chmod(_file, _mask) sign_cert() cls.host = '127.0.0.1' cls.port = 2322 cls.server = OpenSSHServer(listen_ip=cls.host, port=cls.port) cls.server.start_server() cls.cmd = 'echo me' cls.resp = u'me' cls.user_key = PKEY_FILENAME cls.user_pub_key = PUB_FILE cls.cert_pkey = USER_CERT_PRIV_KEY cls.cert_file = USER_CERT_FILE cls.user = USER cls.client = SSHClient(cls.host, port=cls.port, pkey=PKEY_FILENAME, num_retries=1)
def test_agent_auth_failure(self): class UnknownError(Exception): pass def _agent_auth_unk(): raise UnknownError def _agent_auth_agent_err(): raise AuthenticationDenied client = SSHClient(self.host, port=self.port, pkey=self.user_key, num_retries=1, allow_agent=True, identity_auth=False) client.session.disconnect() client.pkey = None client._connect(self.host, self.port) client._agent_auth = _agent_auth_unk self.assertRaises(AuthenticationError, client.auth) client._agent_auth = _agent_auth_agent_err self.assertRaises(AuthenticationError, client.auth)
def test_context_manager(self): with SSHClient(self.host, port=self.port, pkey=self.user_key, num_retries=1) as client: self.assertIsInstance(client, SSHClient)
def test_identity_auth(self): class _SSHClient(SSHClient): IDENTITIES = (self.user_key, ) client = SSHClient(self.host, port=self.port, pkey=self.user_key, num_retries=1, timeout=1, allow_agent=False) client.disconnect() client.pkey = None del client.session del client.sock client._connect(self.host, self.port) client._init_session() client.IDENTITIES = (self.user_key, ) # Default identities auth only should succeed client._identity_auth() client.disconnect() del client.session del client.sock client._connect(self.host, self.port) client._init_session() # Auth should succeed self.assertIsNone(client.auth()) # Standard init with custom identities client = _SSHClient(self.host, port=self.port, num_retries=1, allow_agent=False) self.assertIsInstance(client, SSHClient)