File: //usr/lib/python3/dist-packages/twisted/conch/test/__pycache__/test_endpoints.cpython-38.pyc
U
��W[!� � @ s� d Z ddlZddlmZ ddlmZ ddlZddlm Z m
Z
ddlmZ ddl
mZmZ ddlmZmZ dd lmZ dd
lmZ ddlmZ ddlmZ dd
lmZmZ ddlmZm Z ddl!m"Z"m#Z#m$Z$m%Z% ddl&m'Z'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z.m/Z/ ddl&m0Z0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9m:Z:m;Z; ed��r.ed��r.ddl<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZC ddlDmEZE dd lFmGZG dd!lHmIZI dd"lJmKZKmLZL dd#lMmNZNmOZO dd$lPmQZQ dd%lRmSZSmTZTmUZUmVZV dd&lWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^ dd'l_m`Z` n(d(Zaeb Z? ZA ZC ZE ZG ZI ZK ZNZQdd)l-mcZc dd*ldmeZemfZf G d+d,� d,ee�ZgG d-d.� d.eG�ZhG d/d0� d0eG�ZiG d1d2� d2eG�ZjG d3d4� d4eb�ZkG d5d6� d6e�ZlG d7d8� d8eb�ZmG d9d:� d:eA�ZnG d;d<� d<e?�Zoee�G d=d>� d>eb��Zpee�G d?d@� d@eb��ZqG dAdB� dBeb�ZrG dCdD� dDe,er�ZsG dEdF� dFe,er�ZtG dGdH� dHe,�ZuG dIdJ� dJeb�ZvG dKdL� dLe,�ZwdS )Mz'
Tests for L{twisted.conch.endpoints}.
� N)�pack)�ENOSYS)�verifyObject�verifyClass)�implementer)�globalLogPublisher�LogLevel)� iteritems�
networkString)�Failure)�FilePath)�msg)�
requireModule)�IAddress�IStreamClientEndpoint)�Factory�Protocol)�CancelledError�Deferred�succeed�fail)�ConnectionDone�ConnectionRefusedError)�IPv4Address)�TestCase)�EventLoggingObserver�MemoryReactorClock)�ProcessTerminated�ConnectingCancelledError)�Portal)�'InMemoryUsernamePasswordDatabaseDontUse)�
IConchUser)�
ConchError�UserRejectedKey�HostKeyChangedZcryptographyzpyasn1.type)�common)�
SSHFactory)�SSHUserAuthServer)�
SSHConnection)�Key)�
SSHChannel)�SSHAgentServer)�KnownHostsFile� ConsoleUI)�SSHPublicKeyChecker�InMemorySSHKeyDB)� ConchUser)�publicRSA_openssh�privateRSA_openssh� privateRSA_openssh_encrypted_aes�privateDSA_openssh)�_ISSHConnectionCreator�AuthenticationFailed�SSHCommandAddress�SSHCommandClientEndpoint� _ReadFile�_NewConnectionHelper�_ExistingConnectionHelper)�SSHClientTransportz%can't run w/o cryptography and pyasn1)�StringTransport)�
FakeTransport�connectc @ s e Zd ZdZdZdd� ZdS )�AbortableFakeTransportzC
A L{FakeTransport} with added C{abortConnection} support.
Fc C s
d| _ dS )z}
Abort the connection in a fake manner.
This should really be implemented in the underlying module.
TN��aborted��self� rE �C/usr/lib/python3/dist-packages/twisted/conch/test/test_endpoints.py�abortConnectionN s z&AbortableFakeTransport.abortConnectionN)�__name__�
__module__�__qualname__�__doc__rB rG rE rE rE rF r@ G s r@ c @ s e Zd ZdZdd� ZdS )�BrokenExecSessionzO
L{BrokenExecSession} is a session on which exec requests always fail.
c C s dS )z�
Fail all exec requests.
@param data: Information about what is being executed.
@type data: L{bytes}
@return: C{0} to indicate failure
@rtype: L{int}
r rE �rD �datarE rE rF �request_exec\ s
zBrokenExecSession.request_execN�rH rI rJ rK rO rE rE rE rF rL X s rL c @ s e Zd ZdZdd� ZdS )�WorkingExecSessionzS
L{WorkingExecSession} is a session on which exec requests always succeed.
c C s dS )z�
Succeed all exec requests.
@param data: Information about what is being executed.
@type data: L{bytes}
@return: C{1} to indicate success
@rtype: L{int}
� rE rM rE rE rF rO n s
zWorkingExecSession.request_execNrP rE rE rE rF rQ j s rQ c @ s e Zd ZdZdd� ZdS )�UnsatisfiedExecSessionz�
L{UnsatisfiedExecSession} is a session on which exec requests are always
delayed indefinitely, never succeeding or failing.
c C s t � S )z�
Delay all exec requests indefinitely.
@param data: Information about what is being executed.
@type data: L{bytes}
@return: A L{Deferred} which will never fire.
@rtype: L{Deferred}
)r rM rE rE rF rO � s
z#UnsatisfiedExecSession.request_execNrP rE rE rE rF rS | s rS c @ s e Zd Zdd� Zdd� ZdS )�TrivialRealmc C s
i | _ d S �N)�
channelLookuprC rE rE rF �__init__� s zTrivialRealm.__init__c G s t � }| j|_t|dd� fS )Nc S s d S rU rE rE rE rE rF �<lambda>� � z,TrivialRealm.requestAvatar.<locals>.<lambda>)r0 rV r! )rD ZavatarIdZmindZ
interfacesZavatarrE rE rF �
requestAvatar� s zTrivialRealm.requestAvatarN)rH rI rJ rW rZ rE rE rE rF rT � s rT c @ s e Zd ZdZdd� ZdS )�AddressSpyFactoryNc C s || _ t�| |�S rU )�addressr �
buildProtocol)rD r\ rE rE rF r] � s zAddressSpyFactory.buildProtocol)rH rI rJ r\ r] rE rE rE rF r[ � s r[ c @ s$ e Zd Zdd� Zdd� Zdd� ZdS )�FixedResponseUIc C s
|| _ d S rU ��result)rD r` rE rE rF rW � s zFixedResponseUI.__init__c C s
t | j�S rU )r r` �rD �textrE rE rF �prompt� s zFixedResponseUI.promptc C s d S rU rE ra rE rE rF �warn� s zFixedResponseUI.warnN)rH rI rJ rW rc rd rE rE rE rF r^ � s r^ c @ s$ e Zd Zedd� �Zedd� �ZdS )�FakeClockSSHUserAuthServerc C s
| j jjS )zy
Use the C{attemptsBeforeDisconnect} value defined by the factory to make
it easier to override.
)� transport�factory�attemptsBeforeDisconnectrC rE rE rF rh � s z3FakeClockSSHUserAuthServer.attemptsBeforeDisconnectc C s
| j jjS )z�
Use the reactor defined by the factory, rather than the default global
reactor, to simplify testing (by allowing an alternate implementation
to be supplied by tests).
)rf rg �reactorrC rE rE rF �clock� s z FakeClockSSHUserAuthServer.clockN)rH rI rJ �propertyrh rj rE rE rE rF re � s
re c @ s2 e Zd Zedd� �Zedd� �Zeed�ZdZ dS )�CommandFactoryc C s dt jtd�iS �N� ssh-rsa)rN )r) �
fromStringr1 rC rE rE rF �
publicKeys� s
�zCommandFactory.publicKeysc C s dt jtd�iS rm )r) ro r2 rC rE rE rF �privateKeys� s
�zCommandFactory.privateKeys)s ssh-userauths ssh-connectionr N)
rH rI rJ rk rp rq re r( �servicesrh rE rE rE rF rl � s
� rl c @ s e Zd ZdS )�
MemoryAddressN)rH rI rJ rE rE rE rF rs � s rs c @ s e Zd ZdZdd� Zdd� ZdS )�SingleUseMemoryEndpointa]
L{SingleUseMemoryEndpoint} is a client endpoint which allows one connection
to be set up and then exposes an API for moving around bytes related to
that connection.
@ivar pump: L{None} until a connection is attempted, then a L{IOPump}
instance associated with the protocol which is connected.
@type pump: L{IOPump}
c C s d| _ || _dS )z�
@param server: An L{IProtocol} provider to which the client will be
connected.
@type server: L{IProtocol} provider
N)�pump�_server)rD �serverrE rE rF rW � s z SingleUseMemoryEndpoint.__init__c C sf | j d k rtd��z|�t� �}W n t� Y S X t| jt| jdd�|t|dd��| _ t|�S d S )Nz(SingleUseMemoryEndpoint was already usedT��isServerF) ru � Exceptionr] rs r r? rv r@ r )rD rg �protocolrE rE rF r? � s
�
�zSingleUseMemoryEndpoint.connectN)rH rI rJ rK rW r? rE rE rE rF rt � s
rt c @ s� e Zd ZdZdd� Zdd� Zdd� Zdd � Zd
d� Zdd
� Z dd� Z
dd� Zdd� Zdd� Z
dd� Zdd� Zdd� Zdd� Zdd� Zd d!� Zd*d#d$�Zd%d&� Zd'd(� Zd)S )+�"SSHCommandClientEndpointTestsMixina�
Tests for L{SSHCommandClientEndpoint}, an L{IStreamClientEndpoint}
implementations which connects a protocol with the stdin and stdout of a
command running in an SSH session.
These tests apply to L{SSHCommandClientEndpoint} whether it is constructed
using L{SSHCommandClientEndpoint.existingConnection} or
L{SSHCommandClientEndpoint.newConnection}.
Subclasses must override L{create}, L{assertClientTransportState}, and
L{finishConnection}.
c C s� d| _ d| _d| _d| _t� | _t� | _t| j�| _ t
� | _| j�| j| j� | j �
| j� t� | _| j| j_| j | j_ | j�� | �| jj� tddd�| _tddd �| _d S )
Ns ssh.example.comi&� s users passwordZTCPz10.0.0.1i90 z192.168.100.200i1� )�hostname�port�user�passwordr ri rT �realmr �portalr ZpasswdDBZaddUser�registerCheckerrl rg ZdoStart�
addCleanupZdoStopr �
clientAddress�
serverAddressrC rE rE rF �setUp s"