o
    Òó·iÀÛ  ã                   @   sˆ  d Z ddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZmZ ddlZddlmZmZmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZmZmZmZ ddl m!Z!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z' ddl(m)Z)m*Z* ddl+m,Z,m-Z-m.Z. ddl/m0Z0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z= e;dƒ�rddl>m?Z? ddl@mAZAmBZB ddlCmDZDmEZE ddlFmGZGmHZHmIZImJZJmKZKmLZL ddlMmNZN ddlOmPZP dd lQmRZR dd!lSmTZT dd"lUmVZV dd#lWmXZX dd$lYmZZZ dd%l[m\Z\ dd&l]m^Z^m_Z_m`Z`maZa nd'ZbecZVecZ\ecZTecZXecZRecZPecZEecZBecZ?dd(ldmeZemfZf G d)d*„ d*eeƒZgG d+d,„ d,eRƒZhG d-d.„ d.eRƒZiG d/d0„ d0eRƒZjG d1d2„ d2ƒZkG d3d4„ d4e)ƒZlG d5d6„ d6ƒZmG d7d8„ d8e\ƒZnG d9d:„ d:eVƒZoe
e&ƒG d;d<„ d<ƒƒZpe
e'ƒG d=d>„ d>ƒƒZqG d?d@„ d@ƒZrG dAdB„ dBe=erƒZsG dCdD„ dDe=erƒZtG dEdF„ dFe=ƒZuG dGdH„ dHeƒZvG dIdJ„ dJƒZwG dKdL„ dLe=ƒZxdS )Mz'
Tests for L{twisted.conch.endpoints}.
é    N)ÚBytesIO)Úpack)ÚIO)Úimplementer)ÚverifyClassÚverifyObject)Ú
ConchErrorÚHostKeyChangedÚUserRejectedKey)Ú
IConchUser)Ú'InMemoryUsernamePasswordDatabaseDontUse)ÚPortal)ÚIPv4Address)ÚCancelledErrorÚDeferredÚfailÚsucceed)ÚConnectingCancelledErrorÚConnectionDoneÚConnectionRefusedErrorÚProcessTerminated)ÚIAddressÚIStreamClientEndpoint)ÚFactoryÚProtocol)ÚEventLoggingObserverÚMemoryReactorClockÚStringTransport)ÚLogLevelÚglobalLogPublisher)ÚnetworkString)ÚFailure)ÚFilePath)Úmsg)ÚrequireModule)ÚTestCaseZcryptography)Ú	ConchUser)ÚInMemorySSHKeyDBÚSSHPublicKeyChecker)Ú	ConsoleUIÚKnownHostsFile)ÚAuthenticationFailedÚSSHCommandAddressÚSSHCommandClientEndpointÚ_ExistingConnectionHelperÚ_ISSHConnectionCreatorÚ_NewConnectionHelper)Úcommon)ÚSSHAgentServer)Ú
SSHChannel)ÚSSHConnection)Ú
SSHFactory)ÚKey)ÚSSHClientTransport)ÚSSHUserAuthServer)ÚprivateDSA_opensshÚprivateRSA_opensshÚ privateRSA_openssh_encrypted_aesÚpublicRSA_opensshzcan't run w/o cryptography)ÚFakeTransportÚconnectc                   @   s   e Zd ZdZdZdd„ ZdS )ÚAbortableFakeTransportzC
    A L{FakeTransport} with added C{abortConnection} support.
    Fc                 C   ó
   d| _ dS )z}
        Abort the connection in a fake manner.

        This should really be implemented in the underlying module.
        TN©Úaborted©Úself© rE   úgC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/conch/test/test_endpoints.pyÚabortConnection]   ó   
z&AbortableFakeTransport.abortConnectionN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rB   rG   rE   rE   rE   rF   r?   V   s    r?   c                   @   ó   e Zd ZdZdd„ ZdS )ÚBrokenExecSessionzO
    L{BrokenExecSession} is a session on which exec requests always fail.
    c                 C   ó   dS )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   NrE   ©rD   ÚdatarE   rE   rF   Úrequest_execk   ó   
zBrokenExecSession.request_execN©rI   rJ   rK   rL   rR   rE   rE   rE   rF   rN   f   ó    rN   c                   @   rM   )ÚWorkingExecSessionzS
    L{WorkingExecSession} is a session on which exec requests always succeed.
    c                 C   rO   )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}
        é   NrE   rP   rE   rE   rF   rR   }   rS   zWorkingExecSession.request_execNrT   rE   rE   rE   rF   rV   x   rU   rV   c                   @   rM   )Ú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}
        N)r   rP   rE   rE   rF   rR   �   s   
z#UnsatisfiedExecSession.request_execNrT   rE   rE   rE   rF   rX   Š   s    rX   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚTrivialRealmc                 C   s
   i | _ d S ©N)ÚchannelLookuprC   rE   rE   rF   Ú__init__ž   ó   
zTrivialRealm.__init__c                 G   s   t ƒ }| j|_t|dd„ fS )Nc                   S   ó   d S rZ   rE   rE   rE   rE   rF   Ú<lambda>¤   ó    z,TrivialRealm.requestAvatar.<locals>.<lambda>)r&   r[   r   )rD   ZavatarIdZmindZ
interfacesZavatarrE   rE   rF   ÚrequestAvatar¡   s   zTrivialRealm.requestAvatarN)rI   rJ   rK   r\   ra   rE   rE   rE   rF   rY   �   s    rY   c                   @   s   e Zd ZdZdd„ ZdS )ÚAddressSpyFactoryNc                 C   s   || _ t | |¡S rZ   )Úaddressr   ÚbuildProtocol)rD   rc   rE   rE   rF   rd   ª   s   zAddressSpyFactory.buildProtocol)rI   rJ   rK   rc   rd   rE   rE   rE   rF   rb   §   ó    rb   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚFixedResponseUIc                 C   s
   || _ d S rZ   ©Úresult)rD   rh   rE   rE   rF   r\   °   r]   zFixedResponseUI.__init__c                 C   s
   t | jƒS rZ   )r   rh   ©rD   ÚtextrE   rE   rF   Úprompt³   r]   zFixedResponseUI.promptc                 C   r^   rZ   rE   ri   rE   rE   rF   Úwarn¶   s   zFixedResponseUI.warnN)rI   rJ   rK   r\   rk   rl   rE   rE   rE   rF   rf   ¯   s    rf   c                   @   s$   e Zd Zedd„ ƒZedd„ ƒZdS )ÚFakeClockSSHUserAuthServerc                 C   ó
   | j jjS )zy
        Use the C{attemptsBeforeDisconnect} value defined by the factory to make
        it easier to override.
        N)Ú	transportÚfactoryÚattemptsBeforeDisconnectrC   rE   rE   rF   rq   ¼   rH   z3FakeClockSSHUserAuthServer.attemptsBeforeDisconnectc                 C   rn   )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).
        N)ro   rp   ÚreactorrC   rE   rE   rF   ÚclockÄ   s   
z FakeClockSSHUserAuthServer.clockN)rI   rJ   rK   Úpropertyrq   rs   rE   rE   rE   rF   rm   º   s
    
rm   c                   @   s2   e Zd Zedd„ ƒZedd„ ƒZeedœZdZ	dS )ÚCommandFactoryc                 C   ó   dt jtd�iS ©Nó   ssh-rsa)rQ   )r6   Ú
fromStringr<   rC   rE   rE   rF   Ú
publicKeysÏ   ó   zCommandFactory.publicKeysc                 C   rv   rw   )r6   ry   r:   rC   rE   rE   rF   ÚprivateKeysÓ   r{   zCommandFactory.privateKeys)s   ssh-userauths   ssh-connectionr   N)
rI   rJ   rK   rt   rz   r|   rm   r4   Zservicesrq   rE   rE   rE   rF   ru   Î   s    

þ	ru   c                   @   s   e Zd ZdS )ÚMemoryAddressN)rI   rJ   rK   rE   rE   rE   rF   r}   ã   s    r}   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú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| _ || _dS )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   r\   ô   s   
z SingleUseMemoryEndpoint.__init__c                 C   sh   | j d ur	tdƒ‚z| tƒ ¡}W n ty   tƒ  Y S w t| jt| jdd�|t|dd�ƒ| _ t	|ƒS )Nz(SingleUseMemoryEndpoint was already usedT©ÚisServerF)
r   Ú	Exceptionrd   r}   ÚBaseExceptionr   r>   r€   r?   r   )rD   rp   ÚprotocolrE   rE   rF   r>   ý   s   

ÿ
üzSingleUseMemoryEndpoint.connectN)rI   rJ   rK   rL   r\   r>   rE   rE   rE   rF   r~   è   s    
	r~   c                   @   sª   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd*d#d$„Zd%d&„ Zd'd(„ Zd)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 ¡  |  | jj¡ tdddƒ| _tddd	ƒ| _d S )
Ns   ssh.example.comi&¤  s   users   passwordZTCPz10.0.0.1i90  z192.168.100.200i1Ô  )ÚhostnameÚportÚuserÚpasswordr   rr   rY   Úrealmr   Úportalr   ZpasswdDBZaddUserÚregisterCheckerru   rp   ZdoStartÚ
addCleanupZdoStopr   ÚclientAddressÚserverAddressrC   rE   rE   rF   ÚsetUp  s"   


z(SSHCommandClientEndpointTestsMixin.setUpc                 C   ó   t | jj›d�ƒ‚)z£
        Create and return a new L{SSHCommandClientEndpoint} to be tested.
        Override this to implement creation in an interesting way the endpoint.
        z did not implement createN©ÚNotImplementedErrorÚ	__class__rI   rC   rE   rE   rF   Úcreate1  ó   ÿz)SSHCommandClientEndpointTestsMixin.createc                 C   s   t d| jjf ƒ‚)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   ÚclientZimmediateCloserE   rE   rF   ÚassertClientTransportState:  s
   ÿÿz=SSHCommandClientEndpointTestsMixin.assertClientTransportStatec                 C   r“   )zˆ
        Do any remaining work necessary to complete an in-memory connection
        attempted initiated using C{self.reactor}.
        z# did not implement finishConnectionNr”   rC   rE   rE   rF   ÚfinishConnectionJ  r˜   z3SSHCommandClientEndpointTestsMixin.finishConnectionc                 C   sT   |  d¡}|  d¡}t|d| j| jd�}t|d| j| jd�}t||||ƒ}|||fS )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)rƒ   ZhostAddressZpeerAddressT)rd   r?   r�   r‘   r>   )rD   ZserverFactoryZclientFactoryZclientProtocolZserverProtocolZclientTransportZserverTransportr   rE   rE   rF   ÚconnectedServerAndClientS  s    


üü
z;SSHCommandClientEndpointTestsMixin.connectedServerAndClientc           	      C   sš   |   ¡ }tƒ }t|_| |¡}|  ¡ \}}}|  t¡}|  d|d j	j
|d j	j	f¡ |  dt|ƒ¡ |  |¡}| t¡ |  d|j	j	¡ |  |d¡ dS )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 channelr   rW   ó   unknown channelFN)r—   r   r   r†   r>   rœ   ÚflushLoggedErrorsr   ÚassertInÚvaluerQ   ÚassertEqualÚlenÚfailureResultOfÚtrapr›   )	rD   Úendpointrp   Ú	connectedr�   rš   r   ÚerrorsÚfrE   rE   rF   Útest_channelOpenFailurep  s   

 

z:SSHCommandClientEndpointTestsMixin.test_channelOpenFailurec                 C   sl   t | jjd< |  ¡ }tƒ }t|_| |¡}|  ¡ \}}}|  	|¡}| 
t¡ |  d|jj¡ |  |d¡ dS )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.
        ó   sessionzchannel request failedFN)rN   rŒ   r[   r—   r   r   r†   r>   rœ   r¥   r¦   r   r£   r¢   r›   ©rD   r§   rp   r¨   r�   rš   r   rª   rE   rE   rF   Útest_execFailureŠ  s   


z3SSHCommandClientEndpointTestsMixin.test_execFailurec                 C   sd   t | jjd< |  ¡ }tƒ }t|_| |¡}|  ¡ \}}}| 	¡  |  
|¡}| t¡ |  |d¡ dS )zµ
        If execution of the command is cancelled via the L{Deferred} returned
        by L{SSHCommandClientEndpoint.connect}, the connection is closed
        immediately.
        r¬   TN)rX   rŒ   r[   r—   r   r   r†   r>   rœ   Úcancelr¥   r¦   r   r›   r­   rE   rE   rF   Útest_execCancelledŸ  s   


z5SSHCommandClientEndpointTestsMixin.test_execCancelledc                 C   s‚   t | jjd< |  ¡ }tƒ }t|_| |¡ |  ¡ \}}}|  	|j
t¡ |  |j ¡ |j
j¡ |  | j|j
j¡ |  d|j
j¡ dS )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)rV   rŒ   r[   r—   rb   r   r†   r>   rœ   ÚassertIsInstancerc   r,   r£   ro   ZgetHostr�   rŠ   ÚusernameÚcommand)rD   r§   rp   r�   rš   r   rE   rE   rF   Útest_buildProtocol´  s   
z5SSHCommandClientEndpointTestsMixin.test_buildProtocolc                 C   sR   t | jjd< |  ¡ }tƒ }t|_| |¡}|  ¡ \}}}|  	|¡}|  
|j¡ dS )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)rV   rŒ   r[   r—   r   r   r†   r>   rœ   ÚsuccessResultOfÚassertIsNotNonero   ©rD   r§   rp   r¨   r�   rš   r   r†   rE   rE   rF   Útest_makeConnectionË  s   

z6SSHCommandClientEndpointTestsMixin.test_makeConnectionc           
      C   s†   t | jjd< |  ¡ }tƒ }t|_| |¡}|  ¡ \}}}|  	|¡}g }|j
|_|jj}	|jj|	  d¡ | ¡  |  dd |¡¡ dS )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, worldó    N)rV   rŒ   r[   r—   r   r   r†   r>   rœ   r¶   ÚappendÚdataReceivedro   ÚidÚserviceÚchannelsÚwriter   r£   Újoin)
rD   r§   rp   r¨   r�   rš   r   r†   r½   Ú	channelIdrE   rE   rF   Útest_dataReceivedÞ  s   

z4SSHCommandClientEndpointTestsMixin.test_dataReceivedc           
      C   sŒ   t | jjd< |  ¡ }tƒ }t|_| |¡}|  ¡ \}}}|  	|¡}g }|j
|_|jj}	|jj|	  ¡  | ¡  |d  t¡ |  |d¡ dS )zq
        When the command closes the channel, the protocol's C{connectionLost}
        method is called.
        r¬   r   FN)rV   rŒ   r[   r—   r   r   r†   r>   rœ   r¶   r¼   ÚconnectionLostro   r¾   r¿   rÀ   ÚloseConnectionr   r¦   r   r›   )
rD   r§   rp   r¨   r�   rš   r   r†   rÅ   rÃ   rE   rE   rF   Útest_connectionLostù  s   

z6SSHCommandClientEndpointTestsMixin.test_connectionLostc                 C   s–   t | jjd< |  ¡ }tƒ }t|_| |¡}|  ¡ \}}}|  	|¡}	g }
|
j
|	_|	jj}|jj| }|j |||¡ | ¡  | ¡  |  |d¡ |
d S )zJ
        Test handling of non-zero exit statuses or exit signals.
        r¬   Fr   N)rV   rŒ   r[   r—   r   r   r†   r>   rœ   r¶   r¼   rÅ   ro   r¾   r¿   rÀ   ZsendRequestrÆ   r   r›   )rD   ÚrequestZ
requestArgr§   rp   r¨   r�   rš   r   r†   rÅ   rÃ   ZchannelrE   rE   rF   Ú_exitStatusTest  s    

z2SSHCommandClientEndpointTestsMixin._exitStatusTestc                 C   s$   d}|   dtd|ƒ¡}| t¡ dS )úÊ
        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É   r   r¦   r   )rD   ÚexitCodeÚexcrE   rE   rF   Útest_zeroExitCode2  s   z4SSHCommandClientEndpointTestsMixin.test_zeroExitCodec                 C   sH   d}d}|   dtd|ƒ¡}| t¡ |  ||jj¡ |  ||jj¡ dS )rÊ   é{   NrË   rÌ   )rÉ   r   r¦   r   r£   r¢   rÍ   Úsignal)rD   rÍ   rÑ   rÎ   rE   rE   rF   Útest_nonZeroExitStatus<  s   
z9SSHCommandClientEndpointTestsMixin.test_nonZeroExitStatusc                 C   s¶   t ƒ }t |¡ |  tj|¡ d}d}d t d¡dt d¡t d¡g¡}|  d|¡}| 	t
¡ |  ||jj¡ |  ||jj¡ d	}t |t t t tj¡|dd
dddœ¡¡¡ dS )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é   r»   s   TERMó   s   messages   en-USs   exit-signalz'twisted.conch.endpoints._CommandChannelTÚmessage)Ú	log_levelZlog_namespaceZshortSignalNameZ
coreDumpedZerrorMessageZlanguageTag)r   r   ZaddObserverr�   ZremoveObserverrÂ   r1   ZNSrÉ   r¦   r   r£   r¢   rÍ   rÑ   ÚhamcrestZassert_thatZhas_itemZhas_entriesZequal_tor   Úinfo)rD   ZlogObserverrÍ   rÑ   ÚpacketrÎ   ZlogNamespacerE   rE   rF   Útest_nonZeroExitSignalI  s>   
ûÿ	

úÿÿþz9SSHCommandClientEndpointTestsMixin.test_nonZeroExitSignalFc                    s<   |j j}g ‰ |r‡ fdd„}nˆ j}t|jj| ||ƒ ˆ 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:
        c                      s
   ˆ   d ¡S rZ   )r¼   rE   ©ZrecorderrE   rF   r_   ‹  s   
 z;SSHCommandClientEndpointTestsMixin.record.<locals>.<lambda>N)ro   r¾   r¼   Úsetattrr¿   rÀ   )rD   r�   r†   ÚeventÚnoArgsrÃ   rª   rE   rÛ   rF   Úrecordw  s   z)SSHCommandClientEndpointTestsMixin.recordc           	      C   sz   t | jjd< |  ¡ }tƒ }t|_| |¡}|  ¡ \}}}|  	|¡}|  
||d¡}|j d¡ | ¡  |  dd |¡¡ dS )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º   r»   N)rV   rŒ   r[   r—   r   r   r†   r>   rœ   r¶   rß   ro   rÁ   r   r£   rÂ   ©	rD   r§   rp   r¨   r�   rš   r   r†   r½   rE   rE   rF   Ú
test_write’  s   

z-SSHCommandClientEndpointTestsMixin.test_writec           	      C   s|   t | jjd< |  ¡ }tƒ }t|_| |¡}|  ¡ \}}}|  	|¡}|  
||d¡}|j dg¡ | ¡  |  dd |¡¡ dS )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º   r»   N)rV   rŒ   r[   r—   r   r   r†   r>   rœ   r¶   rß   ro   ZwriteSequencer   r£   rÂ   rà   rE   rE   rF   Útest_writeSequence§  s   

z5SSHCommandClientEndpointTestsMixin.test_writeSequenceN)F)rI   rJ   rK   rL   r’   r—   r›   rœ   r�   r«   r®   r°   rµ   r¹   rÄ   rÇ   rÉ   rÏ   rÒ   rÚ   rß   rá   râ   rE   rE   rE   rF   r‡     s*    		

.r‡   c                   @   sÈ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0S )1ÚNewConnectionTestsz`
    Tests for L{SSHCommandClientEndpoint} when using the C{newConnection}
    constructor.
    c                 C   sh   t  | ¡ t|  ¡ ƒ| _t| jƒ| _| j | j| j	j
d ¡ | j t| jjƒ| j	j
d ¡ | j ¡  dS )ú
        Configure an SSH server with password authentication enabled for a
        well-known (to the tests) account.
        rx   N)r‡   r’   r"   ÚmktempZhostKeyPathr*   Ú
knownHostsÚ
addHostKeyrˆ   rp   rz   r    r‘   ÚhostÚsaverC   rE   rE   rF   r’   Ã  s   
ÿzNewConnectionTests.setUpc              
   C   s*   t j| jd| j| j| j| j| jtdƒd�S )zu
        Create and return a new L{SSHCommandClientEndpoint} using the
        C{newConnection} constructor.
        r±   F©r‹   ræ   ÚuiN)	r-   ÚnewConnectionrr   rŠ   rˆ   r‰   r‹   ræ   rf   rC   rE   rE   rF   r—   Ò  s   øzNewConnectionTests.createc                 C   s   |   | j| jjd d ¡S )z}
        Establish the first attempted TCP connection using the SSH server which
        C{self.factory} can create.
        r   é   N)r�   rp   rr   Ú
tcpClientsrC   rE   rE   rF   rœ   â  s   ÿz#NewConnectionTests.finishConnectionc                 C   sH   | j ||ddd�}|j ¡  | ¡  |  dg|¡ | ¡  |j ¡  dS )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ß   ro   rÆ   r   r£   ZreportDisconnect)rD   r�   rš   r†   r   rï   rE   rE   rF   ÚloseConnectionToServerë  s   
z)NewConnectionTests.loseConnectionToServerc                 C   s(   |r|   |jj¡ dS |   |jj¡ dS )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)Ú
assertTruero   rB   Údisconnectingr™   rE   rE   rF   r›     s   z-NewConnectionTests.assertClientTransportStatec                 C   s,   t  | jdd| j| j¡}|  tt|ƒ¡ dS )zY
        L{SSHCommandClientEndpoint} instances provide L{IStreamClientEndpoint}.
        ó   dummy commandó
   dummy userN)r-   rì   rr   rˆ   r‰   rñ   r   r   ©rD   r§   rE   rE   rF   Útest_interface  s   ÿz!NewConnectionTests.test_interfacec                 C   s(   t  | jdd| j¡}|  d|jj¡ dS )z†
        L{SSHCommandClientEndpoint} uses the default port number for SSH when
        the C{port} argument is not specified.
        ró   rô   é   N©r-   rì   rr   rˆ   r£   Z_creatorr‰   rõ   rE   rE   rF   Útest_defaultPort$  s   ÿz#NewConnectionTests.test_defaultPortc                 C   s,   t j| jdd| jdd�}|  d|jj¡ dS )zU
        L{SSHCommandClientEndpoint} uses the C{port} argument if specified.
        ró   rô   i®  )r‰   Nrø   rõ   rE   rE   rF   Útest_specifiedPort.  s   ÿz%NewConnectionTests.test_specifiedPortc              
   C   sŽ   t j| jd| j| j| j| j| jtdƒd�}t	ƒ }t
|_| |¡ | jjd \}}}}}|  | jt|ƒ¡ |  | j|¡ |  dt| jjƒ¡ dS )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ê   r   rW   N)r-   rì   rr   rŠ   rˆ   r‰   r‹   ræ   rf   r   r   r†   r>   rî   r£   r    r¤   )rD   r§   rp   rè   r‰   ÚtimeoutZbindAddressrE   rE   rF   Útest_destination7  s"   ø

z#NewConnectionTests.test_destinationc              	   C   sp   t j| jdd| j| j| jtdƒd�}tƒ }t|_	| 
|¡}| jjd d }| dttƒ ƒ¡ |  |¡ t¡ dS )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ë   r   rí   N)r-   rì   rr   rˆ   r‰   ræ   rf   r   r   r†   r>   rî   ZclientConnectionFailedr!   r   r¥   r¦   ©rD   r§   rp   ÚdrE   rE   rF   Útest_connectionFailedO  s   ù	
z(NewConnectionTests.test_connectionFailedc              	   C   sx   t j| jdd| j| jt|  ¡ ƒtdƒd�}tƒ }t	|_
| |¡}|  | j| jjd d ¡\}}}|  |¡}| t¡ dS )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ý   r   rí   N)r-   rì   rr   rˆ   r‰   r*   rå   rf   r   r   r†   r>   r�   rp   rî   r¥   r¦   r
   r­   rE   rE   rF   Útest_userRejectedHostKeyg  s"   
ù


ÿ
z+NewConnectionTests.test_userRejectedHostKeyc              
   C   sÈ   t  t¡ ¡ }tt|  ¡ ƒƒ}| t| j	j
ƒ|¡ t jtdd� ¡ }| | j|¡ tdƒ}tj| jdd| j| jd||d�}tƒ }t|_| |¡}|  | j| jjd d	 ¡\}}	}
|  |¡}| 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}.
        s   testxp)Z
passphraseTr±   rô   ó   dummy passwordrê   r   rí   N)r6   ry   r:   Úpublicr*   r"   rå   rç   r    r‘   rè   r;   rˆ   rf   r-   rì   rr   r‰   r   r   r†   r>   r�   rp   rî   r¥   r¦   r	   )rD   ZfirstKeyræ   ZdifferentKeyrë   r§   rp   r¨   r�   rš   r   rª   rE   rE   rF   Útest_mismatchedHostKeyƒ  s8   ÿþø

ÿ
z)NewConnectionTests.test_mismatchedHostKeyc              	   C   sˆ   t j| jdd| j| j| jtdƒd�}tƒ }t|_	| 
|¡}tƒ }| jjd d }| d¡}| |¡ | ttƒ ƒ¡ |  |¡ t¡ dS )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ý   r   rí   N)r-   rì   rr   rˆ   r‰   ræ   rf   r   r   r†   r>   r   rî   rd   ÚmakeConnectionrÅ   r!   r   r¥   r¦   ©rD   r§   rp   rÿ   ro   rš   rE   rE   rF   Ú!test_connectionClosedBeforeSecure¯  s$   ù



z4NewConnectionTests.test_connectionClosedBeforeSecurec              	   C   s¢   t j| jdd| j| j| jtdƒd�}tƒ }t|_	| 
|¡}tddd�}| jjd d }| d¡}| |¡ | ¡  |  |¡ t¡ |  |j¡ | ttƒ ƒ¡ dS )	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ý   Nr‚   r   rí   )r-   rì   rr   rˆ   r‰   ræ   rf   r   r   r†   r>   r?   rî   rd   r  r¯   r¥   r¦   r   rñ   rB   rÅ   r!   r   r  rE   rE   rF   Ú$test_connectionCancelledBeforeSecureÍ  s(   ù



z7NewConnectionTests.test_connectionCancelledBeforeSecurec              	   C   sj   t j| jdd| j| j| jtdƒd�}tƒ }t|_	| 
|¡}| ¡  |  |¡ t¡ |  | jjd j¡ dS )zz
        If the connection is cancelled before it finishes connecting, the
        connection attempt is stopped.
        r±   rô   Frý   r   N)r-   rì   rr   rˆ   r‰   ræ   rf   r   r   r†   r>   r¯   r¥   r¦   r   rñ   Z
connectorsZstoppedConnectingrþ   rE   rE   rF   Ú'test_connectionCancelledBeforeConnectedï  s   ù

z:NewConnectionTests.test_connectionCancelledBeforeConnectedc              
   C   s˜   t j| jdd| j| jd| jtdƒd�}tƒ }t|_	| 
|¡}|  | j| jjd d ¡\}}}| j |jj¡ | ¡  |  |¡}| t¡ |  |d¡ dS )	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ê   r   rí   N)r-   rì   rr   rˆ   r‰   ræ   rf   r   r   r†   r>   r�   rp   rî   Zadvancer¿   ZpasswordDelayÚflushr¥   r¦   r+   r›   r­   rE   rE   rF   Ú"test_passwordAuthenticationFailure  s*   ø

ÿ

z5NewConnectionTests.test_passwordAuthenticationFailurec                 C   s,   dd„ |  ¡ D ƒ}tt|ƒƒ}| |¡ dS )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}
        c                 S   s"   i | ]\}}|t  |¡ ¡ g“qS rE   )r6   ry   r  )Ú.0ÚkÚvrE   rE   rF   Ú
<dictcomp>:  s   " z6NewConnectionTests.setupKeyChecker.<locals>.<dictcomp>N)Úitemsr(   r'   rŽ   )rD   r�   ZusersÚmappingÚcheckerrE   rE   rF   ÚsetupKeyChecker-  s   z"NewConnectionTests.setupKeyCheckerc           	   
   C   s¤   t  t¡}|  | j| jti¡ tj| j	d| j| j
| j|g| jtdƒd�}tƒ }t|_| |¡}|  | j| j	jd d ¡\}}}|  |¡}| t¡ |  |jj¡ dS )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ë   r   rí   N)r6   ry   r:   r  r�   rŠ   r9   r-   rì   rr   rˆ   r‰   ræ   rf   r   r   r†   r>   r�   rp   rî   r¥   r¦   r+   rñ   ro   rò   )	rD   ÚbadKeyr§   rp   r¨   r�   rš   r   rª   rE   rE   rF   Ú#test_publicKeyAuthenticationFailure>  s*   
ø

ÿ

z6NewConnectionTests.test_publicKeyAuthenticationFailurec           
      C   s
  t  t¡}|  | j| jti¡ tj| j	d| j| j
| j|g| j| jtdƒd�	}tƒ }t|_| |¡}| j jd7  _|  | j| j	jd d ¡\}}}| ¡  |  t¡}|  d|d jj|d jjf¡ |  dt|ƒ¡ |  |¡}	|	  t¡ |  d|	jj¡ |  !|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ë   rW   r   rí   rž   rŸ   N)$r6   ry   r:   r  r�   rŠ   r9   r-   rì   rr   rˆ   r‰   r‹   ræ   rf   r   r   r†   r>   rp   rq   r�   rî   r   r    r   r¡   r¢   rQ   r£   r¤   r¥   r¦   rñ   ro   rò   )
rD   r  r§   rp   r¨   r�   rš   r   r©   rª   rE   rE   rF   Útest_authenticationFallbackc  s8   
÷

ÿ
 

z.NewConnectionTests.test_authenticationFallbackc           	   
   C   s¤   t  t¡}|  | j| jti¡ t| jjd< t	j
| jd| j| j| j|g| jtdƒd�}tƒ }t|_| |¡}|  | j| jjd d ¡\}}}|  |¡}|  |j¡ dS )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  r   rí   N)r6   ry   r9   r  r�   rŠ   rV   rŒ   r[   r-   rì   rr   rˆ   r‰   ræ   rf   r   r   r†   r>   r�   rp   rî   r¶   r·   ro   )	rD   Úkeyr§   rp   r¨   r�   rš   r   r†   rE   rE   rF   Útest_publicKeyAuthentication“  s*   
ø

ÿ
z/NewConnectionTests.test_publicKeyAuthenticationc              	   C   sŠ   t j| jd| j| j| j| jtdƒd�}tƒ }t	|_
| |¡}|  | j| jjd d ¡\}}}| ¡  |  |¡}| t¡ |  |jj¡ dS )z†
        If the password is not specified, L{SSHCommandClientEndpoint} doesn't
        try it as an authentication mechanism.
        r±   Frý   r   rí   N)r-   rì   rr   rŠ   rˆ   r‰   ræ   rf   r   r   r†   r>   r�   rp   rî   r   r¥   r¦   r+   rñ   ro   rò   r­   rE   rE   rF   Útest_skipPasswordAuthentication²  s&   ù


ÿ

z2NewConnectionTests.test_skipPasswordAuthenticationc              
   C   s  t  t¡}tƒ }tƒ |_| ¡ |dfi|j_|  | j	| j
ti¡ t|ƒ}tj| jd| j
| j| j| jtdƒ|d�}t| jjd< tƒ }t|_| |¡}|  | j| jjd d ¡\}}}	tdƒD ]}
|j ¡  |	 ¡  q]|  |¡}|  |j¡ |   ||||	¡ |  !|jj"¡ |  !|jj#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.
        r»   r±   F)ræ   rë   ÚagentEndpointr¬   r   rí   é   N)$r6   ry   r:   r2   r   rp   Zblobr  r  r�   rŠ   r~   r-   rì   rr   rˆ   r‰   ræ   rf   rV   rŒ   r[   r   r†   r>   r�   rî   Úranger   r¶   r·   ro   rð   rñ   rò   ZclientIO)rD   r  ZagentServerr  r§   rp   r¨   r�   rš   r   Úir†   rE   rE   rF   Útest_agentAuthenticationÓ  s>   
ø

ÿ


z+NewConnectionTests.test_agentAuthenticationc                 C   sd   t | jjd< |  ¡ }tƒ }t|_| |¡}|  ¡ \}}}|  	|¡}|  
||||¡ |  |jj¡ dS )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)rV   rŒ   r[   r—   r   r   r†   r>   rœ   r¶   rð   rñ   ro   rò   r¸   rE   rE   rF   Útest_loseConnection  s   

z&NewConnectionTests.test_loseConnectionN)rI   rJ   rK   rL   r’   r—   rœ   rð   r›   rö   rù   rú   rü   r   r  r  r  r  r	  r  r  r  r  r  r  r   r!  rE   rE   rE   rF   rã   ½  s2    	#	
	,"(%0!3rã   c                   @   ó0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚExistingConnectionTestsze
    Tests for L{SSHCommandClientEndpoint} when using the C{existingConnection}
    constructor.
    c              
   C   sz   t  | ¡ tt|  ¡ ƒƒ}| | j| jjd ¡ | t	| j
jƒ| jjd ¡ tj| jd| j| j| j| j|tdƒd�| _dS )rä   rx   r±   Frê   N)r‡   r’   r*   r"   rå   rç   rˆ   rp   rz   r    r‘   rè   r-   rì   rr   rŠ   r‰   r‹   rf   r§   )rD   ræ   rE   rE   rF   r’   #  s    
ÿøzExistingConnectionTests.setUpc           	   	   C   s¼   t ƒ }t|_| j |¡}| jj ¡ }z%t| jjd< |  	| j
| jjd d ¡\}}}W | jj ¡  | jj |¡ n| jj ¡  | jj |¡ w || _|| _|| _|  |¡}|jj}t |d¡S )zz
        Create and return a new L{SSHCommandClientEndpoint} using the
        C{existingConnection} constructor.
        r¬   r   rí   r±   N)r   r   r†   r§   r>   rŒ   r[   ÚcopyrV   r�   rp   rr   rî   ÚclearÚupdater€   Ú_clientÚ_pumpr¶   ro   Úconnr-   ZexistingConnection)	rD   rp   r¨   r[   r�   rš   r   r†   Ú
connectionrE   rE   rF   r—   ;  s&   ÿÿ
zExistingConnectionTests.createc                 C   s8   | j  ¡  | j  ¡  | j  ¡  | j  ¡  | j| j| j fS )z�
        Give back the connection established in L{create} over which the new
        command channel being tested will exchange data.
        N)r(  r   r€   r'  rC   rE   rE   rF   rœ   Y  s
   



z(ExistingConnectionTests.finishConnectionc                 C   s    |   |jj¡ |   |jj¡ dS )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)ÚassertFalsero   rò   rB   r™   rE   rE   rF   r›   g  s   z2ExistingConnectionTests.assertClientTransportStateN)rI   rJ   rK   rL   r’   r—   rœ   r›   rE   rE   rE   rF   r#    s    r#  c                   @   r"  )ÚExistingConnectionHelperTestsz1
    Tests for L{_ExistingConnectionHelper}.
    c                 C   ó   |   tttƒ¡ dS )zT
        L{_ExistingConnectionHelper} implements L{_ISSHConnectionCreator}.
        N)rñ   r   r/   r.   rC   rE   rE   rF   rö   w  ó   z,ExistingConnectionHelperTests.test_interfacec                 C   s(   t ƒ }t|ƒ}|  ||  | ¡ ¡¡ dS )z¸
        L{_ExistingConnectionHelper.secureConnection} returns a L{Deferred}
        which fires with whatever object was fed to
        L{_ExistingConnectionHelper.__init__}.
        N)Úobjectr.   ÚassertIsr¶   ZsecureConnection)rD   rh   ÚhelperrE   rE   rF   Útest_secureConnection}  s   z3ExistingConnectionHelperTests.test_secureConnectionc                 C   ó   t tƒ ƒ}| tƒ d¡ dS )z�
        L{_ExistingConnectionHelper.cleanupConnection} does nothing to the
        existing connection if called with C{immediate} set to C{False}.
        FN©r.   r/  ÚcleanupConnection©rD   r1  rE   rE   rF   Ú$test_cleanupConnectionNotImmediately‡  ó   
zBExistingConnectionHelperTests.test_cleanupConnectionNotImmediatelyc                 C   r3  )zœ
        L{_ExistingConnectionHelper.cleanupConnection} does nothing to the
        existing connection if called with C{immediate} set to C{True}.
        TNr4  r6  rE   rE   rF   Ú!test_cleanupConnectionImmediately’  r8  z?ExistingConnectionHelperTests.test_cleanupConnectionImmediatelyN)rI   rJ   rK   rL   rö   r2  r7  r9  rE   rE   rE   rF   r,  r  s    
r,  c                   @   s   e Zd Zdedefdd„ZdS )Ú_WriteDiscarderrQ   Úreturnc                C   rO   )z§
        Discard writes because we are emulating a console object, where they'd
        go to the screen, not back into the buffer to be read like an a+ file.
        r   NrE   rP   rE   rE   rF   rÁ   Ÿ  s   z_WriteDiscarder.writeN)rI   rJ   rK   r/  ÚintrÁ   rE   rE   rE   rF   r:  ž  s    r:  c                   @   s8   e Zd ZdZdeddfdd„Zdedee fdd	„ZdS )
Ú_PTYPathzk
    A L{FilePath}-like object which can be opened to create a L{_ReadFile} with
    certain contents.
    Úcontentsr;  Nc                 C   s
   || _ dS )zz
        @param contents: L{bytes} which will be the contents of the
            L{_ReadFile} this path can open.
        N)r>  )rD   r>  rE   rE   rF   r\   ­  s   
z_PTYPath.__init__Úmodec                 C   s   |dksJ ‚t | jƒS )z¾
        If the mode is r+, return a file with the given contents as a line.

        @return: a buffer of the given contents, which will discard any writes
            given to it.
        zr+N)r:  r>  )rD   r?  rE   rE   rF   Úopen´  s   
z_PTYPath.open)	rI   rJ   rK   rL   Úbytesr\   Ústrr   r@  rE   rE   rE   rF   r=  §  s    r=  c                   @   sb   e Zd ZdZdd„ Zdd„ Zddd	„Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚNewConnectionHelperTestsz,
    Tests for L{_NewConnectionHelper}.
    c                 C   r-  )zO
        L{_NewConnectionHelper} implements L{_ISSHConnectionCreator}.
        N)rñ   r   r/   r0   rC   rE   rE   rF   rö   Ä  r.  z'NewConnectionHelperTests.test_interfacec                 C   s   |   dtj¡ dS )zK
        The default I{known_hosts} path is I{~/.ssh/known_hosts}.
        z~/.ssh/known_hostsN)r£   r0   Ú_KNOWN_HOSTSrC   rE   rE   rF   Útest_defaultPathÊ  s   z)NewConnectionHelperTests.test_defaultPathr;  Nc                    sH   t ƒ ‰ |  td‡ fdd„¡ tddddddddddƒ
}|  ˆ |j¡ dS )zŒ
        L{_NewConnectionHelper._knownHosts} is used to create a
        L{KnownHostsFile} if one is not passed to the initializer.
        Ú_knownHostsc                    s   ˆ S rZ   rE   )ÚclsZignoredrg   rE   rF   r_   Ö  r`   zANewConnectionHelperTests.test_defaultKnownHosts.<locals>.<lambda>N)r/  Úpatchr0   r0  ræ   r6  rE   rg   rF   Útest_defaultKnownHostsÐ  s   öz/NewConnectionHelperTests.test_defaultKnownHostsc                 C   s˜   t ƒ jd }t|  ¡ ƒ}t|ƒ}| d|¡ | ¡  td|j›�ƒ t	j 
d¡}|j |d¡}|  td|¡ td|›�ƒ t ¡ }|  | d|¡¡ dS )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.
        rx   s	   127.0.0.1zCreated known_hosts file at z~/rD  zPatched _KNOWN_HOSTS with N)ru   rz   r"   rå   r*   rç   ré   r#   ÚpathÚosÚ
expanduserÚreplacerH  r0   rF  rñ   Z
hasHostKey)rD   r  rJ  ræ   ÚhomeÚdefaultZloadedrE   rE   rF   Útest_readExistingç  s   z*NewConnectionHelperTests.test_readExistingc                 C   s,   t ddddddddddƒ
}|  |jt¡ dS )zz
        If L{None} is passed for the C{ui} parameter to
        L{_NewConnectionHelper}, a L{ConsoleUI} is used.
        N)r0   r²   rë   r)   r6  rE   rE   rF   Útest_defaultConsoleUIþ  s   ÿz.NewConnectionHelperTests.test_defaultConsoleUIc                 C   sD   t dƒ}tdddddddddd|ƒ}|  |j d¡¡}|  |¡ dS )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.
        s   yesNs   does this work?)r=  r0   r¶   rë   rk   rñ   ©rD   Úttyr1  rh   rE   rE   rF   Útest_ttyConsoleUI  s   ÿz*NewConnectionHelperTests.test_ttyConsoleUIc                 C   sH   t |  ¡ ƒ}tdddddddddd|ƒ}|  |j d¡¡}|  |¡ dS )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.
        Ns   did this break?)r"   rå   r0   r¶   rë   rk   r+  rR  rE   rE   rF   Útest_nottyUI  s   ÿz%NewConnectionHelperTests.test_nottyUIc                 C   s0   t ddddddddddƒ
}|  tdƒ|j¡ dS )zy
        If not passed the name of a tty in the filesystem,
        L{_NewConnectionHelper} uses C{b"/dev/tty"}.
        Ns   /dev/tty)r0   r£   r"   rS  r6  rE   rE   rF   Útest_defaultTTYFilename"  s   ÿz0NewConnectionHelperTests.test_defaultTTYFilenamec                 C   sF   t ddddddddddƒ
}tƒ }tƒ |_| |d¡ |  |jj¡ dS )z�
        L{_NewConnectionHelper.cleanupConnection} closes the transport cleanly
        if called with C{immediate} set to C{False}.
        NF)r0   r4   r   ro   r5  rñ   rò   )rD   r1  r*  rE   rE   rF   r7  ,  s   ÿz=NewConnectionHelperTests.test_cleanupConnectionNotImmediatelyc                 C   s`   G dd„ dƒ}t ddddddddddƒ
}tƒ }tƒ |_|ƒ |j_| |d¡ |  |jjj¡ dS )zœ
        L{_NewConnectionHelper.cleanupConnection} closes the transport with
        C{abortConnection} if called with C{immediate} set to C{True}.
        c                   @   rM   )zMNewConnectionHelperTests.test_cleanupConnectionImmediately.<locals>.AbortableFc                 S   r@   )z7
                Abort the connection.
                TNrA   rC   rE   rE   rF   rG   B  s   
z]NewConnectionHelperTests.test_cleanupConnectionImmediately.<locals>.Abortable.abortConnectionN)rI   rJ   rK   rB   rG   rE   rE   rE   rF   Ú	Abortable?  re   rW  NT)r0   r4   r7   ro   r5  rñ   rB   )rD   rW  r1  r*  rE   rE   rF   r9  9  s   	ÿ
z:NewConnectionHelperTests.test_cleanupConnectionImmediately)r;  N)rI   rJ   rK   rL   rö   rE  rI  rP  rQ  rT  rU  rV  r7  r9  rE   rE   rE   rF   rC  ¿  s    


rC  )yrL   Úos.pathrK  Úior   Ústructr   Útypingr   Zzope.interfacer   Zzope.interface.verifyr   r   r×   Ztwisted.conch.errorr   r	   r
   Ztwisted.conch.interfacesr   Ztwisted.cred.checkersr   Ztwisted.cred.portalr   Ztwisted.internet.addressr   Ztwisted.internet.deferr   r   r   r   Ztwisted.internet.errorr   r   r   r   Ztwisted.internet.interfacesr   r   Ztwisted.internet.protocolr   r   Ztwisted.internet.testingr   r   r   Ztwisted.loggerr   r   Ztwisted.python.compatr    Ztwisted.python.failurer!   Ztwisted.python.filepathr"   Ztwisted.python.logr#   Ztwisted.python.reflectr$   Ztwisted.trial.unittestr%   Ztwisted.conch.avatarr&   Ztwisted.conch.checkersr'   r(   Ztwisted.conch.client.knownhostsr)   r*   Ztwisted.conch.endpointsr+   r,   r-   r.   r/   r0   Ztwisted.conch.sshr1   Ztwisted.conch.ssh.agentr2   Ztwisted.conch.ssh.channelr3   Ztwisted.conch.ssh.connectionr4   Ztwisted.conch.ssh.factoryr5   Ztwisted.conch.ssh.keysr6   Ztwisted.conch.ssh.transportr7   Ztwisted.conch.ssh.userauthr8   Ztwisted.conch.test.keydatar9   r:   r;   r<   Úskipr/  Ztwisted.test.iosimr=   r>   r?   rN   rV   rX   rY   rb   rf   rm   ru   r}   r~   r‡   rã   r#  r,  r:  r=  rC  rE   rE   rE   rF   Ú<module>   sš   
 
&   1    dU,	