HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/twisted/conch/test/__pycache__/test_endpoints.cpython-38.pyc
U

��W[!��@s�dZddlZddlmZddlmZddlZddlm	Z	m
Z
ddlmZddl
mZmZddlmZmZdd	lmZdd
lmZddlmZddlmZdd
lmZmZddlmZm 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&m0Z0m1Z1ddl2m3Z3ddl4m5Z5ddl6m7Z7ddl8m9Z9m:Z:m;Z;ed��r.ed��r.ddl<m=Z=ddl>m?Z?ddl@mAZAddlBmCZCddlDmEZEdd lFmGZGdd!lHmIZIdd"lJmKZKmLZLdd#lMmNZNmOZOdd$lPmQZQdd%lRmSZSmTZTmUZUmVZVdd&lWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^dd'l_m`Z`n(d(ZaebZ?ZAZCZEZGZIZKZNZQdd)l-mcZcdd*ldmeZemfZfGd+d,�d,ee�ZgGd-d.�d.eG�ZhGd/d0�d0eG�ZiGd1d2�d2eG�ZjGd3d4�d4eb�ZkGd5d6�d6e�ZlGd7d8�d8eb�ZmGd9d:�d:eA�ZnGd;d<�d<e?�Zoee�Gd=d>�d>eb��Zpee�Gd?d@�d@eb��ZqGdAdB�dBeb�ZrGdCdD�dDe,er�ZsGdEdF�dFe,er�ZtGdGdH�dHe,�ZuGdIdJ�dJeb�ZvGdKdL�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�HostKeyChangedZcryptographyzpyasn1.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@seZdZdZdZdd�ZdS)�AbortableFakeTransportzC
    A L{FakeTransport} with added C{abortConnection} support.
    FcCs
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�abortConnectionNsz&AbortableFakeTransport.abortConnectionN)�__name__�
__module__�__qualname__�__doc__rBrGrErErErFr@Gsr@c@seZdZdZdd�ZdS)�BrokenExecSessionzO
    L{BrokenExecSession} is a session on which exec requests always fail.
    cCsdS)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}
        rrE�rD�datarErErF�request_exec\s
zBrokenExecSession.request_execN�rHrIrJrKrOrErErErFrLXsrLc@seZdZdZdd�ZdS)�WorkingExecSessionzS
    L{WorkingExecSession} is a session on which exec requests always succeed.
    cCsdS)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}
        �rErMrErErFrOns
zWorkingExecSession.request_execNrPrErErErFrQjsrQc@seZdZdZdd�ZdS)�UnsatisfiedExecSessionz�
    L{UnsatisfiedExecSession} is a session on which exec requests are always
    delayed indefinitely, never succeeding or failing.
    cCst�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}
        )rrMrErErFrO�s
z#UnsatisfiedExecSession.request_execNrPrErErErFrS|srSc@seZdZdd�Zdd�ZdS)�TrivialRealmcCs
i|_dS�N)�
channelLookuprCrErErF�__init__�szTrivialRealm.__init__cGst�}|j|_t|dd�fS)NcSsdSrUrErErErErF�<lambda>��z,TrivialRealm.requestAvatar.<locals>.<lambda>)r0rVr!)rDZavatarIdZmindZ
interfacesZavatarrErErF�
requestAvatar�szTrivialRealm.requestAvatarN)rHrIrJrWrZrErErErFrT�srTc@seZdZdZdd�ZdS)�AddressSpyFactoryNcCs||_t�||�SrU)�addressr�
buildProtocol)rDr\rErErFr]�szAddressSpyFactory.buildProtocol)rHrIrJr\r]rErErErFr[�sr[c@s$eZdZdd�Zdd�Zdd�ZdS)�FixedResponseUIcCs
||_dSrU��result)rDr`rErErFrW�szFixedResponseUI.__init__cCs
t|j�SrU)rr`�rD�textrErErF�prompt�szFixedResponseUI.promptcCsdSrUrErarErErF�warn�szFixedResponseUI.warnN)rHrIrJrWrcrdrErErErFr^�sr^c@s$eZdZedd��Zedd��ZdS)�FakeClockSSHUserAuthServercCs
|jjjS)zy
        Use the C{attemptsBeforeDisconnect} value defined by the factory to make
        it easier to override.
        )�	transport�factory�attemptsBeforeDisconnectrCrErErFrh�sz3FakeClockSSHUserAuthServer.attemptsBeforeDisconnectcCs
|jjjS)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).
        )rfrg�reactorrCrErErF�clock�sz FakeClockSSHUserAuthServer.clockN)rHrIrJ�propertyrhrjrErErErFre�s
rec@s2eZdZedd��Zedd��Zeed�ZdZ	dS)�CommandFactorycCsdtjtd�iS�N�ssh-rsa)rN)r)�
fromStringr1rCrErErF�
publicKeys�s
�zCommandFactory.publicKeyscCsdtjtd�iSrm)r)ror2rCrErErF�privateKeys�s
�zCommandFactory.privateKeys)sssh-userauthsssh-connectionrN)
rHrIrJrkrprqrer(�servicesrhrErErErFrl�s

�	rlc@seZdZdS)�
MemoryAddressN)rHrIrJrErErErFrs�srsc@s eZdZdZdd�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}
    cCsd|_||_dS)z�
        @param server: An L{IProtocol} provider to which the client will be
            connected.
        @type server: L{IProtocol} provider
        N)�pump�_server)rD�serverrErErFrW�sz SingleUseMemoryEndpoint.__init__cCsf|jdk	rtd��z|�t��}Wnt�YSXt|jt|jdd�|t|dd��|_t|�SdS)Nz(SingleUseMemoryEndpoint was already usedT��isServerF)	ru�	Exceptionr]rsrr?rvr@r)rDrg�protocolrErErFr?�s 
�
�zSingleUseMemoryEndpoint.connectN)rHrIrJrKrWr?rErErErFrt�s	
rtc@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�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}.
    cCs�d|_d|_d|_d|_t�|_t�|_t|j�|_	t
�|_|j�|j|j�|j	�
|j�t�|_|j|j_|j	|j_	|j��|�|jj�tddd�|_tddd	�|_dS)
Nsssh.example.comi&�suserspasswordZTCPz10.0.0.1i90z192.168.100.200i1�)�hostname�port�user�passwordrrirT�realmr�portalr ZpasswdDBZaddUser�registerCheckerrlrgZdoStart�
addCleanupZdoStopr�
clientAddress�
serverAddressrCrErErF�setUps"


z(SSHCommandClientEndpointTestsMixin.setUpcCstd|jjf��dS)z�
        Create and return a new L{SSHCommandClientEndpoint} to be tested.
        Override this to implement creation in an interesting way the endpoint.
        z%r did not implement createN��NotImplementedError�	__class__rHrCrErErF�create2s�z)SSHCommandClientEndpointTestsMixin.createcCstd|jjf��dS)a�
        Make an assertion about the connectedness of the given protocol's
        transport.  Override this to implement either a check for the
        connection still being open or having been closed as appropriate.

        @param client: The client whose state is being checked.

        @param immediateClose: Boolean indicating whether the connection was
            closed immediately or not.
        z/%r did not implement assertClientTransportStateNr��rD�clientZimmediateCloserErErF�assertClientTransportState;s
��z=SSHCommandClientEndpointTestsMixin.assertClientTransportStatecCstd|jjf��dS)z�
        Do any remaining work necessary to complete an in-memory connection
        attempted initiated using C{self.reactor}.
        z%%r did not implement finishConnectionNr�rCrErErF�finishConnectionKs
��z3SSHCommandClientEndpointTestsMixin.finishConnectioncCsT|�d�}|�d�}t|d|j|jd�}t|d|j|jd�}t||||�}|||fS)aw
        Set up an in-memory connection between protocols created by
        C{serverFactory} and C{clientFactory}.

        @return: A three-tuple.  The first element is the protocol created by
            C{serverFactory}.  The second element is the protocol created by
            C{clientFactory}.  The third element is the L{IOPump} connecting
            them.
        NF)ryZhostAddressZpeerAddressT)r]r@r�r�r?)rDZ
serverFactoryZ
clientFactoryZclientProtocolZserverProtocolZclientTransportZserverTransportrurErErF�connectedServerAndClientUs*


���z;SSHCommandClientEndpointTestsMixin.connectedServerAndClientc	Cs�|��}t�}t|_|�|�}|��\}}}|�t�}|�d|dj	j
|dj	j	f�|�dt|��|�
|�}|�t�|�d|j	j	�|�|d�dS)z�
        If a channel cannot be opened on the authenticated SSH connection, the
        L{Deferred} returned by L{SSHCommandClientEndpoint.connect} fires with
        a L{Failure} wrapping the reason given by the server.
        �unknown channelrrR�unknown channelFN)r�rrr{r?r��flushLoggedErrorsr"�assertIn�valuerN�assertEqual�len�failureResultOf�trapr�)	rD�endpointrg�	connectedrwr�ru�errors�frErErF�test_channelOpenFailurens

�

z:SSHCommandClientEndpointTestsMixin.test_channelOpenFailurecCslt|jjd<|��}t�}t|_|�|�}|��\}}}|�	|�}|�
t�|�d|j
j
�|�|d�dS)z�
        If execution of the command fails, the L{Deferred} returned by
        L{SSHCommandClientEndpoint.connect} fires with a L{Failure} wrapping
        the reason given by the server.
        �sessionzchannel request failedFN)rLr�rVr�rrr{r?r�r�r�r"r�r�r��rDr�rgr�rwr�rur�rErErF�test_execFailure�s


z3SSHCommandClientEndpointTestsMixin.test_execFailurecCsdt|jjd<|��}t�}t|_|�|�}|��\}}}|�	�|�
|�}|�t�|�
|d�dS)z�
        If execution of the command is cancelled via the L{Deferred} returned
        by L{SSHCommandClientEndpoint.connect}, the connection is closed
        immediately.
        r�TN)rSr�rVr�rrr{r?r��cancelr�r�rr�r�rErErF�test_execCancelled�s


z5SSHCommandClientEndpointTestsMixin.test_execCancelledcCs�t|jjd<|��}t�}t|_|�|�|��\}}}|�	|j
t�|�|j
��|j
j�|�|j|j
j�|�d|j
j�dS)aD
        Once the necessary SSH actions have completed successfully,
        L{SSHCommandClientEndpoint.connect} uses the factory passed to it to
        construct a protocol instance by calling its C{buildProtocol} method
        with an address object representing the SSH connection and command
        executed.
        r��
/bin/ls -lN)rQr�rVr�r[rr{r?r��assertIsInstancer\r7r�rfZgetHostrwrZusernameZcommand)rDr�rgrwr�rurErErF�test_buildProtocol�s
z5SSHCommandClientEndpointTestsMixin.test_buildProtocolcCsRt|jjd<|��}t�}t|_|�|�}|��\}}}|�	|�}|�
|j�dS)a
        L{SSHCommandClientEndpoint} establishes an SSH connection, creates a
        channel in it, runs a command in that channel, and uses the protocol's
        C{makeConnection} to associate it with a protocol representing that
        command's stdin and stdout.
        r�N)rQr�rVr�rrr{r?r��successResultOf�assertIsNotNonerf�rDr�rgr�rwr�rur{rErErF�test_makeConnection�s

z6SSHCommandClientEndpointTestsMixin.test_makeConnectionc
Cs�t|jjd<|��}t�}t|_|�|�}|��\}}}|�	|�}g}|j
|_|jj
}	|jj|	�d�|��|�dd�|��dS)z�
        After establishing the connection, when the command on the SSH server
        produces output, it is delivered to the protocol's C{dataReceived}
        method.
        r��hello, worldrYN)rQr�rVr�rrr{r?r�r��append�dataReceivedrf�id�service�channels�writerur��join)
rDr�rgr�rwr�rur{r��	channelIdrErErF�test_dataReceived�s

z4SSHCommandClientEndpointTestsMixin.test_dataReceivedc
Cs�t|jjd<|��}t�}t|_|�|�}|��\}}}|�	|�}g}|j
|_|jj
}	|jj|	��|��|d�t�|�|d�dS)zq
        When the command closes the channel, the protocol's C{connectionLost}
        method is called.
        r�rFN)rQr�rVr�rrr{r?r�r�r��connectionLostrfr�r�r��loseConnectionrur�rr�)
rDr�rgr�rwr�rur{r�r�rErErF�test_connectionLost�s

z6SSHCommandClientEndpointTestsMixin.test_connectionLostc
Cs�t|jjd<|��}t�}t|_|�|�}|��\}}}|�	|�}	g}
|
j
|	_|	jj
}|jj|}|j�|||�|��|��|�|d�|
dS)zJ
        Test handling of non-zero exit statuses or exit signals.
        r�Fr)rQr�rVr�rrr{r?r�r�r�r�rfr�r�r�ZsendRequestr�rur�)
rDZrequestZ
requestArgr�rgr�rwr�rur{r�r�ZchannelrErErF�_exitStatusTests 

z2SSHCommandClientEndpointTestsMixin._exitStatusTestcCs$d}|�dtd|��}|�t�dS)��
        When the command exits with a non-zero status, the protocol's
        C{connectionLost} method is called with a L{Failure} wrapping an
        exception which encapsulates that status.
        r�exit-status�>LN)r�rr�r)rD�exitCode�excrErErF�test_zeroExitCode9sz4SSHCommandClientEndpointTestsMixin.test_zeroExitCodecCsHd}d}|�dtd|��}|�t�|�||jj�|�||jj�dS)r��{Nr�r�)r�rr�rr�r�r��signal)rDr�r�r�rErErF�test_nonZeroExitStatusDs
z9SSHCommandClientEndpointTestsMixin.test_nonZeroExitStatuscCs�t�}t�|�|�tj|�d}d}d�t�d�dt�d�t�d�g�}|�d|�}|�	t
�|�||jj
�|�||jj�d	}t�|t�t�t�tj�|dd
ddd����dS)
a
        When the command exits with a non-zero signal, the protocol's
        C{connectionLost} method is called with a L{Failure} wrapping an
        exception which encapsulates that status.

        Additional packet contents are logged at the C{info} level.
        N�rYsTERM�smessagesen-USsexit-signalz'twisted.conch.endpoints._CommandChannelT�message)Z	log_levelZ
log_namespaceZshortSignalNameZ
coreDumpedZerrorMessageZlanguageTag)rrZaddObserverr�ZremoveObserverr�r%ZNSr�r�rr�r�r�r��hamcrestZassert_thatZhas_itemZhas_entriesZequal_tor�info)rDZlogObserverr�r�Zpacketr�ZlogNamespacerErErF�test_nonZeroExitSignalRs<
�

����z9SSHCommandClientEndpointTestsMixin.test_nonZeroExitSignalFcs<|jj}g�|r�fdd�}n�j}t|jj|||��S)a
        Hook into and record events which happen to C{protocol}.

        @param server: The SSH server protocol over which C{protocol} is
            running.
        @type server: L{IProtocol} provider

        @param protocol:

        @param event:

        @param noArgs:
        cs
��d�SrU)r�rE�ZrecorderrErFrX�rYz;SSHCommandClientEndpointTestsMixin.record.<locals>.<lambda>)rfr�r��setattrr�r�)rDrwr{Zevent�noArgsr�r�rEr�rF�record}sz)SSHCommandClientEndpointTestsMixin.recordc	Cszt|jjd<|��}t�}t|_|�|�}|��\}}}|�	|�}|�
||d�}|j�d�|�
�|�dd�|��dS)z�
        The transport connected to the protocol has a C{write} method which
        sends bytes to the input of the command executing on the SSH server.
        r�r�r�rYN)rQr�rVr�rrr{r?r�r�r�rfr�rur�r��	rDr�rgr�rwr�rur{r�rErErF�
test_write�s

z-SSHCommandClientEndpointTestsMixin.test_writec	Cs|t|jjd<|��}t�}t|_|�|�}|��\}}}|�	|�}|�
||d�}|j�dg�|�
�|�dd�|��dS)z�
        The transport connected to the protocol has a C{writeSequence} method which
        sends bytes to the input of the command executing on the SSH server.
        r�r�r�rYN)rQr�rVr�rrr{r?r�r�r�rfZ
writeSequencerur�r�r�rErErF�test_writeSequence�s

z5SSHCommandClientEndpointTestsMixin.test_writeSequenceN)F)rHrIrJrKr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rErErErFr|s(	
+
r|c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0S)1�NewConnectionTestsz`
    Tests for L{SSHCommandClientEndpoint} when using the C{newConnection}
    constructor.
    cCsht�|�t|���|_t|j�|_|j�|j|j	j
d�|j�t|jj
�|j	j
d�|j��dS)�
        Configure an SSH server with password authentication enabled for a
        well-known (to the tests) account.
        rnN)r|r�r�mktempZhostKeyPathr,�
knownHosts�
addHostKeyr}rgrpr
r��host�saverCrErErFr��s

�

�zNewConnectionTests.setUpc
Cs*tj|jd|j|j|j|j|jtd�d�S)zu
        Create and return a new L{SSHCommandClientEndpoint} using the
        C{newConnection} constructor.
        r�F�r�r��ui)	r8�
newConnectionrirr}r~r�r�r^rCrErErFr��s�zNewConnectionTests.createcCs|�|j|jjdd�S)z}
        Establish the first attempted TCP connection using the SSH server which
        C{self.factory} can create.
        r�)r�rgri�
tcpClientsrCrErErFr��s�z#NewConnectionTests.finishConnectioncCsH|j||ddd�}|j��|��|�dg|�|��|j��dS)a�
        Lose the connection to a server and pump the L{IOPump} sufficiently for
        the client to handle the lost connection. Asserts that the client
        disconnects its transport.

        @param server: The SSH server protocol over which C{protocol} is
            running.
        @type server: L{IProtocol} provider

        @param client: The SSH client protocol over which C{protocol} is
            running.
        @type client: L{IProtocol} provider

        @param protocol: The protocol created by calling connect on the ssh
            endpoint under test.
        @type protocol: L{IProtocol} provider

        @param pump: The L{IOPump} connecting client to server.
        @type pump: L{IOPump}
        �closedT)r�N)r�rfr�rur�ZreportDisconnect)rDrwr�r{rur�rErErF�loseConnectionToServer�s
z)NewConnectionTests.loseConnectionToServercCs&|r|�|jj�n|�|jj�dS)z�
        Assert that the transport for the given protocol has been disconnected.
        L{SSHCommandClientEndpoint.newConnection} creates a new dedicated SSH
        connection and cleans it up after the command exits.
        N)�
assertTruerfrB�
disconnectingr�rErErFr�sz-NewConnectionTests.assertClientTransportStatecCs,t�|jdd|j|j�}|�tt|��dS)zY
        L{SSHCommandClientEndpoint} instances provide L{IStreamClientEndpoint}.
        �
dummy command�
dummy userN)r8r�rir}r~r�rr�rDr�rErErF�test_interface"s�z!NewConnectionTests.test_interfacecCs(t�|jdd|j�}|�d|jj�dS)z�
        L{SSHCommandClientEndpoint} uses the default port number for SSH when
        the C{port} argument is not specified.
        r�r��N�r8r�rir}r�Z_creatorr~r�rErErF�test_defaultPort,s�z#NewConnectionTests.test_defaultPortcCs,tj|jdd|jdd�}|�d|jj�dS)zU
        L{SSHCommandClientEndpoint} uses the C{port} argument if specified.
        r�r�i�)r~Nr�r�rErErF�test_specifiedPort6s�z%NewConnectionTests.test_specifiedPortc
Cs�tj|jd|j|j|j|j|jtd�d�}t	�}t
|_|�|�|jj
d\}}}}}|�|jt|��|�|j|�|�dt|jj
��dS)z�
        L{SSHCommandClientEndpoint} uses the L{IReactorTCP} passed to it to
        attempt a connection to the host/port address also passed to it.
        r�Fr�rrRN)r8r�rirr}r~r�r�r^rrr{r?r�r�r
r�)rDr�rgr�r~ZtimeoutZbindAddressrErErF�test_destination@s"�
z#NewConnectionTests.test_destinationc	Csptj|jdd|j|j|jtd�d�}t�}t|_	|�
|�}|jjdd}|�dt
t���|�|��t�dS)z�
        If a connection cannot be established, the L{Deferred} returned by
        L{SSHCommandClientEndpoint.connect} fires with a L{Failure}
        representing the reason for the connection setup failure.
        r�r�F�r�r�rr�N)r8r�rir}r~r�r^rrr{r?r�ZclientConnectionFailedrrr�r��rDr�rg�drErErF�test_connectionFailedSs�
z(NewConnectionTests.test_connectionFailedc	Csxtj|jdd|j|jt|���td�d�}t�}t	|_
|�|�}|�|j
|jjdd�\}}}|�|�}|�t�dS)a"
        If the L{KnownHostsFile} instance used to construct
        L{SSHCommandClientEndpoint} rejects the SSH public key presented by the
        server, the L{Deferred} returned by L{SSHCommandClientEndpoint.connect}
        fires with a L{Failure} wrapping L{UserRejectedKey}.
        r�r�Fr�rr�N)r8r�rir}r~r,r�r^rrr{r?r�rgr�r�r�r#r�rErErF�test_userRejectedHostKeygs$
�
�

z+NewConnectionTests.test_userRejectedHostKeyc
Cs�t�t���}tt|����}|�t|j	j
�|�tjtdd���}|�|j|�t
d�}tj|jdd|j|jd||d�}t�}t|_|�|�}|�|j|jjdd	�\}}	}
|�|�}|�t�d
S)ac
        If the SSH public key presented by the SSH server does not match the
        previously remembered key, as reported by the L{KnownHostsFile}
        instance use to construct the endpoint, for that server, the
        L{Deferred} returned by L{SSHCommandClientEndpoint.connect} fires with
        a L{Failure} wrapping L{HostKeyChanged}.
        stestxp)Z
passphraseTr�r��dummy passwordr�rr�N)r)ror2�publicr,rr�r�r
r�r�r3r}r^r8r�rir~rrr{r?r�rgr�r�r�r$)rDZfirstKeyr�ZdifferentKeyr�r�rgr�rwr�rur�rErErF�test_mismatchedHostKey~s<
��
�
�

z)NewConnectionTests.test_mismatchedHostKeyc	Cs�tj|jdd|j|j|jtd�d�}t�}t|_	|�
|�}t�}|jjdd}|�
d�}|�|�|�tt���|�|��t�dS)aU
        If the connection closes at any point before the SSH transport layer
        has finished key exchange (ie, gotten to the point where we may attempt
        to authenticate), the L{Deferred} returned by
        L{SSHCommandClientEndpoint.connect} fires with a L{Failure} wrapping
        the reason for the lost connection.
        r�r�Fr�rr�N)r8r�rir}r~r�r^rrr{r?r=r�r]�makeConnectionr�rrr�r��rDr�rgr�rfr�rErErF�!test_connectionClosedBeforeSecure�s$�


z4NewConnectionTests.test_connectionClosedBeforeSecurec	Cs�tj|jdd|j|j|jtd�d�}t�}t|_	|�
|�}tddd�}|jjdd}|�
d�}|�|�|��|�|��t�|�|j�|�tt���dS)	a[
        If the connection is cancelled before the SSH transport layer has
        finished key exchange (ie, gotten to the point where we may attempt to
        authenticate), the L{Deferred} returned by
        L{SSHCommandClientEndpoint.connect} fires with a L{Failure} wrapping
        L{CancelledError} and the connection is aborted.
        r�r�Fr�Nrxrr�)r8r�rir}r~r�r^rrr{r?r@r�r]r�r�r�r�rr�rBr�rrr�rErErF�$test_connectionCancelledBeforeSecure�s(�


z7NewConnectionTests.test_connectionCancelledBeforeSecurec	Csjtj|jdd|j|j|jtd�d�}t�}t|_	|�
|�}|��|�|��
t�|�|jjdj�dS)zz
        If the connection is cancelled before it finishes connecting, the
        connection attempt is stopped.
        r�r�Fr�rN)r8r�rir}r~r�r^rrr{r?r�r�r�rr�Z
connectorsZstoppedConnectingr�rErErF�'test_connectionCancelledBeforeConnected�s�
z:NewConnectionTests.test_connectionCancelledBeforeConnectedc
Cs�tj|jdd|j|jd|jtd�d�}t�}t|_	|�
|�}|�|j|jj
dd�\}}}|j�|jj�|��|�|�}|�t�|�|d�dS)	z�
        If the SSH server rejects the password presented during authentication,
        the L{Deferred} returned by L{SSHCommandClientEndpoint.connect} fires
        with a L{Failure} wrapping L{AuthenticationFailed}.
        r�r�r�Fr�rr�N)r8r�rir}r~r�r^rrr{r?r�rgr�Zadvancer�Z
passwordDelay�flushr�r�r6r�r�rErErF�"test_passwordAuthenticationFailure�s,�
�


z5NewConnectionTests.test_passwordAuthenticationFailurecCs0tdd�t|�D��}tt|��}|�|�dS)a�
        Create an L{ISSHPrivateKey} checker which recognizes C{users} and add it
        to C{portal}.

        @param portal: A L{Portal} to which to add the checker.
        @type portal: L{Portal}

        @param users: The users and their keys the checker will recognize.  Keys
            are byte strings giving user names.  Values are byte strings giving
            OpenSSH-formatted private keys.
        @type users: L{dict}
        cSs$g|]\}}|t�|���gf�qSrE)r)ror�)�.0�k�vrErErF�
<listcomp>s�z6NewConnectionTests.setupKeyChecker.<locals>.<listcomp>N)�dictr	r.r/r�)rDr�Zusers�mappingZcheckerrErErF�setupKeyCheckers

�z"NewConnectionTests.setupKeyCheckerc	
Cs�t�t�}|�|j|jti�tj|j	d|j|j
|j|g|jt
d�d�}t�}t|_|�|�}|�|j|j	jdd�\}}}|�|�}|�t�|�|jj�dS)z�
        If the SSH server rejects the key pair presented during authentication,
        the L{Deferred} returned by L{SSHCommandClientEndpoint.connect} fires
        with a L{Failure} wrapping L{AuthenticationFailed}.
        r�F��keysr�r�rr�N)r)ror2r�r�rr4r8r�rir}r~r�r^rrr{r?r�rgr�r�r�r6r�rfr�)	rD�badKeyr�rgr�rwr�rur�rErErF�#test_publicKeyAuthenticationFailure#s,
�
�


z6NewConnectionTests.test_publicKeyAuthenticationFailurec
Cs
t�t�}|�|j|jti�tj|j	d|j|j
|j|g|j|j
td�d�	}t�}t|_|�|�}|jjd7_|�|j|j	jdd�\}}}|��|�t�}|�d|djj|djjf�|�dt|��|�|�}	|	� t�|�d|	jj�|�!|j"j#�d	S)
z{
        If the SSH server does not accept any of the specified SSH keys, the
        specified password is tried.
        r�F)r�r�r�r�rRrr�r�r�N)$r)ror2r�r�rr4r8r�rir}r~r�r�r^rrr{r?rgrhr�r�rur�r"r�r�rNr�r�r�r�r�rfr�)
rDrr�rgr�rwr�rur�r�rErErF�test_authenticationFallbackBs@
�
�

�

z.NewConnectionTests.test_authenticationFallbackc	
Cs�t�t�}|�|j|jti�t|jjd<t	j
|jd|j|j|j
|g|jtd�d�}t�}t|_|�|�}|�|j|jjdd�\}}}|�|�}|�|j�dS)z�
        If L{SSHCommandClientEndpoint} is initialized with any private keys, it
        will try to use them to authenticate with the SSH server.
        r�r�Fr�rr�N)r)ror4r�r�rrQr�rVr8r�rir}r~r�r^rrr{r?r�rgr�r�r�rf)	rD�keyr�rgr�rwr�rur{rErErF�test_publicKeyAuthenticationls,
�
�

z/NewConnectionTests.test_publicKeyAuthenticationc	Cs�tj|jd|j|j|j|jtd�d�}t�}t	|_
|�|�}|�|j
|jjdd�\}}}|��|�|�}|�t�|�|jj�dS)z�
        If the password is not specified, L{SSHCommandClientEndpoint} doesn't
        try it as an authentication mechanism.
        r�Fr�rr�N)r8r�rirr}r~r�r^rrr{r?r�rgr�rur�r�r6r�rfr�r�rErErF�test_skipPasswordAuthentication�s(�
�


z2NewConnectionTests.test_skipPasswordAuthenticationc
Cst�t�}t�}t�|_|��|dfi|j_|�|j	|j
ti�t|�}tj
|jd|j
|j|j|jtd�|d�}t|jjd<t�}t|_|�|�}|�|j|jjdd�\}}}	td�D]}
|j��|	��q�|�|�}|�|j�|� ||||	�|�!|jj"�|�!|jj#j"�d	S)
a

        If L{SSHCommandClientEndpoint} is initialized with an
        L{SSHAgentClient}, the agent is used to authenticate with the SSH
        server. Once the connection with the SSH server has concluded, the
        connection to the agent is disconnected.
        rYr�F)r�r��
agentEndpointr�rr��N)$r)ror2r+rrgZblobr�r�r�rrtr8r�rir}r~r�r^rQr�rVrr{r?r�r��rangerur�r�rfr�r�r�ZclientIO)rDrZagentServerrr�rgr�rwr�ru�ir{rErErF�test_agentAuthentication�s@
�
�



z+NewConnectionTests.test_agentAuthenticationcCsdt|jjd<|��}t�}t|_|�|�}|��\}}}|�	|�}|�
||||�|�|jj
�dS)z�
        The transport connected to the protocol has a C{loseConnection} method
        which causes the channel in which the command is running to close and
        the overall connection to be closed.
        r�N)rQr�rVr�rrr{r?r�r�r�r�rfr�r�rErErF�test_loseConnection�s

z&NewConnectionTests.test_loseConnectionN)rHrIrJrKr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrrr
rrErErErFr��s0	$


&"*-r�c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�ExistingConnectionTestsze
    Tests for L{SSHCommandClientEndpoint} when using the C{existingConnection}
    constructor.
    c
Cszt�|�tt|����}|�|j|jjd�|�t	|j
j�|jjd�tj
|jd|j|j|j|j|td�d�|_dS)r�rnr�Fr�N)r|r�r,rr�r�r}rgrpr
r�r�r8r�rirr~r�r^r�)rDr�rErErFr��s(

�

��zExistingConnectionTests.setUpc		Cs�t�}t|_|j�|�}|jj��}z0t
|jjd<|�|j|j
jdd�\}}}W5|jj��|jj�	|�X||_||_||_|�|�}|jj}t�|d�S)zz
        Create and return a new L{SSHCommandClientEndpoint} using the
        C{existingConnection} constructor.
        r�rr�r�)rrr{r�r?r�rV�copy�clear�updaterQr�rgrir�rv�_client�_pumpr�rfZconnr8ZexistingConnection)	rDrgr�rVrwr�rur{�
connectionrErErFr��s*�
�zExistingConnectionTests.createcCs8|j��|j��|j��|j��|j|j|jfS)z�
        Give back the connection established in L{create} over which the new
        command channel being tested will exchange data.
        )rrurvrrCrErErFr�s




z(ExistingConnectionTests.finishConnectioncCs |�|jj�|�|jj�dS)a
        Assert that the transport for the given protocol is still connected.
        L{SSHCommandClientEndpoint.existingConnection} re-uses an SSH connected
        created by some other code, so other code is responsible for cleaning
        it up.
        N)�assertFalserfr�rBr�rErErFr�+sz2ExistingConnectionTests.assertClientTransportStateN)rHrIrJrKr�r�r�r�rErErErFr�s
rc@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�ExistingConnectionHelperTestsz1
    Tests for L{_ExistingConnectionHelper}.
    cCs|�ttt��dS)zT
        L{_ExistingConnectionHelper} implements L{_ISSHConnectionCreator}.
        N)r�rr5r;rCrErErFr�;s�z,ExistingConnectionHelperTests.test_interfacecCs(t�}t|�}|�||�|����dS)z�
        L{_ExistingConnectionHelper.secureConnection} returns a L{Deferred}
        which fires with whatever object was fed to
        L{_ExistingConnectionHelper.__init__}.
        N)�objectr;�assertIsr�ZsecureConnection)rDr`�helperrErErF�test_secureConnectionCs�z3ExistingConnectionHelperTests.test_secureConnectioncCstt��}|�t�d�dS)z�
        L{_ExistingConnectionHelper.cleanupConnection} does nothing to the
        existing connection if called with C{immediate} set to C{False}.
        FN�r;r�cleanupConnection�rDrrErErF�$test_cleanupConnectionNotImmediatelyOs
zBExistingConnectionHelperTests.test_cleanupConnectionNotImmediatelycCstt��}|�t�d�dS)z�
        L{_ExistingConnectionHelper.cleanupConnection} does nothing to the
        existing connection if called with C{immediate} set to C{True}.
        TNrrrErErF�!test_cleanupConnectionImmediately[s
z?ExistingConnectionHelperTests.test_cleanupConnectionImmediatelyN)rHrIrJrKr�rrrrErErErFr7s
rc@s eZdZdZdd�Zdd�ZdS)�_PTYPathzk
    A L{FilePath}-like object which can be opened to create a L{_ReadFile} with
    certain contents.
    cCs
||_dS)zz
        @param contents: L{bytes} which will be the contents of the
            L{_ReadFile} this path can open.
        N)�contents)rDrrErErFrWmsz_PTYPath.__init__cCs |dkrt|j�Sttd��dS)z�
        If the mode is r+, return a L{_ReadFile} with the contents given to
        this path's initializer.

        @raise OSError: If the mode is unsupported.

        @return: A L{_ReadFile} instance
        zrb+zFunction not implementedN)r9r�OSErrorr)rD�moderErErF�openus	
z
_PTYPath.openN)rHrIrJrKrWr"rErErErFrhsrc@s`eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dS)�NewConnectionHelperTestsz,
    Tests for L{_NewConnectionHelper}.
    cCs|�ttt��dS)zO
        L{_NewConnectionHelper} implements L{_ISSHConnectionCreator}.
        N)r�rr5r:rCrErErFr��s�z'NewConnectionHelperTests.test_interfacecCs|�dtj�dS)zK
        The default I{known_hosts} path is I{~/.ssh/known_hosts}.
        z~/.ssh/known_hostsN)r�r:�_KNOWN_HOSTSrCrErErF�test_defaultPath�s�z)NewConnectionHelperTests.test_defaultPathcsHt��|�td�fdd��tdddddddddd�
}|��|j�dS)z�
        L{_NewConnectionHelper._knownHosts} is used to create a
        L{KnownHostsFile} if one is not passed to the initializer.
        �_knownHostscs�SrUrE)�clsr_rErFrX�rYzANewConnectionHelperTests.test_defaultKnownHosts.<locals>.<lambda>N)r�patchr:rr�rrEr_rF�test_defaultKnownHosts�s�z/NewConnectionHelperTests.test_defaultKnownHostscCs�t�jd}t|���}t|�}|�d|�|��td|jf�t	j�
d�}|j�|d�}|�t
d|�td|f�t
��}|�|�d|��dS)z�
        Existing entries in the I{known_hosts} file are reflected by the
        L{KnownHostsFile} created by L{_NewConnectionHelper} when none is
        supplied to it.
        rns	127.0.0.1zCreated known_hosts file at %rz~/r$zPatched _KNOWN_HOSTS with %rN)rlrprr�r,r�r�r
�path�os�
expanduser�replacer(r:r&r�Z
hasHostKey)rDrr*r��home�defaultZloadedrErErF�test_readExisting�sz*NewConnectionHelperTests.test_readExistingcCs,tdddddddddd�
}|�|jt�dS)zz
        If L{None} is passed for the C{ui} parameter to
        L{_NewConnectionHelper}, a L{ConsoleUI} is used.
        N)r:r�r�r-rrErErF�test_defaultConsoleUI�s�z.NewConnectionHelperTests.test_defaultConsoleUIcCsDtd�}tdddddddddd|�}|�|j�d��}|�|�dS)z�
        If L{None} is passed for the C{ui} parameter to L{_NewConnectionHelper}
        and /dev/tty is available, the L{ConsoleUI} used is associated with
        /dev/tty.
        syesNsdoes this work?)rr:r�r�rcr��rD�ttyrr`rErErF�test_ttyConsoleUI�s �z*NewConnectionHelperTests.test_ttyConsoleUIcCsHt|���}tdddddddddd|�}|�|j�d��}|�|�dS)z�
        If L{None} is passed for the C{ui} parameter to L{_NewConnectionHelper}
        and /dev/tty is not available, the L{ConsoleUI} used is associated with
        some file which always produces a C{b"no"} response.
        Nsdid this break?)rr�r:r�r�rcrr2rErErF�test_nottyUI�s �z%NewConnectionHelperTests.test_nottyUIcCs0tdddddddddd�
}|�td�|j�dS)zy
        If not passed the name of a tty in the filesystem,
        L{_NewConnectionHelper} uses C{b"/dev/tty"}.
        Ns/dev/tty)r:r�rr3rrErErF�test_defaultTTYFilename�s�z0NewConnectionHelperTests.test_defaultTTYFilenamecCsFtdddddddddd�
}t�}t�|_|�|d�|�|jj�dS)z�
        L{_NewConnectionHelper.cleanupConnection} closes the transport cleanly
        if called with C{immediate} set to C{False}.
        NF)r:r(r=rfrr�r�)rDrrrErErFr�s �z=NewConnectionHelperTests.test_cleanupConnectionNotImmediatelycCs`Gdd�d�}tdddddddddd�
}t�}t�|_|�|j_|�|d�|�|jjj�dS)z�
        L{_NewConnectionHelper.cleanupConnection} closes the transport with
        C{abortConnection} if called with C{immediate} set to C{True}.
        c@seZdZdZdd�ZdS)zMNewConnectionHelperTests.test_cleanupConnectionImmediately.<locals>.AbortableFcSs
d|_dS)z7
                Abort the connection.
                TNrArCrErErFrGsz]NewConnectionHelperTests.test_cleanupConnectionImmediately.<locals>.Abortable.abortConnectionN)rHrIrJrBrGrErErErF�	Abortable�sr7NT)r:r(r<rfrr�rB)rDr7rrrErErFr�s$�
z:NewConnectionHelperTests.test_cleanupConnectionImmediatelyN)rHrIrJrKr�r%r)r0r1r4r5r6rrrErErErFr#�s




r#)xrK�os.pathr+Zstructr�errnorr�Zzope.interface.verifyrrZzope.interfacerZtwisted.loggerrrZtwisted.python.compatr	r
Ztwisted.python.failurerZtwisted.python.filepathrZtwisted.python.logr
Ztwisted.python.reflectrZtwisted.internet.interfacesrrZtwisted.internet.protocolrrZtwisted.internet.deferrrrrZtwisted.internet.errorrrZtwisted.internet.addressrZtwisted.trial.unittestrZtwisted.test.proto_helpersrrrrZtwisted.cred.portalrZtwisted.cred.checkersr Ztwisted.conch.interfacesr!Ztwisted.conch.errorr"r#r$Ztwisted.conch.sshr%Ztwisted.conch.ssh.factoryr&Ztwisted.conch.ssh.userauthr'Ztwisted.conch.ssh.connectionr(Ztwisted.conch.ssh.keysr)Ztwisted.conch.ssh.channelr*Ztwisted.conch.ssh.agentr+Ztwisted.conch.client.knownhostsr,r-Ztwisted.conch.checkersr.r/Ztwisted.conch.avatarr0Ztwisted.conch.test.keydatar1r2r3r4Ztwisted.conch.endpointsr5r6r7r8r9r:r;Ztwisted.conch.ssh.transportr<�skiprr=Ztwisted.test.iosimr>r?r@rLrQrSrTr[r^rerlrsrtr|r�rrrr#rErErErF�<module>s�$���	%9"S1