
    tf                        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          rcd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*ee          Zg G d+ d,eR          Zh G d- d.eR          Zi G d/ d0eR          Zj G d1 d2          Zk G d3 d4e)          Zl G d5 d6          Zm G d7 d8e\          Zn G d9 d:eV          Zo e
e&           G d; d<                      Zp e
e'           G d= d>                      Zq G d? d@          Zr G dA dBe=er          Zs G dC dDe=er          Zt G dE dFe=          Zu G dG dHe          Zv G dI dJ          Zw G dK 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)TestCase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                       e Zd ZdZdZd ZdS )AbortableFakeTransportzC
    A L{FakeTransport} with added C{abortConnection} support.
    Fc                     d| _         dS )z}
        Abort the connection in a fake manner.

        This should really be implemented in the underlying module.
        TNabortedselfs    `/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/conch/test/test_endpoints.pyabortConnectionz&AbortableFakeTransport.abortConnection]   s         N)__name__
__module____qualname____doc__rE   rI    rJ   rH   rB   rB   V   s4          G    rJ   rB   c                       e Zd ZdZd ZdS )BrokenExecSessionzO
    L{BrokenExecSession} is a session on which exec requests always fail.
    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   rO   rG   datas     rH   request_execzBrokenExecSession.request_execk   	     qrJ   NrK   rL   rM   rN   rU   rO   rJ   rH   rQ   rQ   f   -         
 
 
 
 
rJ   rQ   c                       e Zd ZdZd ZdS )WorkingExecSessionzS
    L{WorkingExecSession} is a session on which exec requests always succeed.
    c                     dS )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}
           rO   rS   s     rH   rU   zWorkingExecSession.request_exec}   rV   rJ   NrW   rO   rJ   rH   rZ   rZ   x   rX   rJ   rZ   c                       e Zd ZdZd ZdS )UnsatisfiedExecSessionz
    L{UnsatisfiedExecSession} is a session on which exec requests are always
    delayed indefinitely, never succeeding or failing.
    c                     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   rS   s     rH   rU   z#UnsatisfiedExecSession.request_exec   s     zzrJ   NrW   rO   rJ   rH   r^   r^      s-         

 
 
 
 
rJ   r^   c                       e Zd Zd Zd ZdS )TrivialRealmc                     i | _         d S N)channelLookuprF   s    rH   __init__zTrivialRealm.__init__   s    rJ   c                 L    t                      }| j        |_        t          |d fS )Nc                      d S rc   rO   rO   rJ   rH   <lambda>z,TrivialRealm.requestAvatar.<locals>.<lambda>   s    D rJ   )r(   rd   r   )rG   avatarIdmind
interfacesavatars        rH   requestAvatarzTrivialRealm.requestAvatar   s%    #1FLL11rJ   N)rK   rL   rM   re   rm   rO   rJ   rH   ra   ra      s2             2 2 2 2 2rJ   ra   c                       e Zd ZdZd ZdS )AddressSpyFactoryNc                 :    || _         t          j        | |          S rc   )addressr   buildProtocol)rG   rq   s     rH   rr   zAddressSpyFactory.buildProtocol   s    $T7333rJ   )rK   rL   rM   rq   rr   rO   rJ   rH   ro   ro      s(        G4 4 4 4 4rJ   ro   c                        e Zd Zd Zd Zd ZdS )FixedResponseUIc                     || _         d S rc   )result)rG   rv   s     rH   re   zFixedResponseUI.__init__   s    rJ   c                 *    t          | j                  S rc   )r   rv   rG   texts     rH   promptzFixedResponseUI.prompt   s    t{###rJ   c                     d S rc   rO   rx   s     rH   warnzFixedResponseUI.warn   s    rJ   N)rK   rL   rM   re   rz   r|   rO   rJ   rH   rt   rt      sA          $ $ $    rJ   rt   c                   :    e Zd Zed             Zed             ZdS )FakeClockSSHUserAuthServerc                 $    | j         j        j        S )zy
        Use the C{attemptsBeforeDisconnect} value defined by the factory to make
        it easier to override.
        )	transportfactoryattemptsBeforeDisconnectrF   s    rH   r   z3FakeClockSSHUserAuthServer.attemptsBeforeDisconnect   s     ~%>>rJ   c                 $    | j         j        j        S )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).
        )r   r   reactorrF   s    rH   clockz FakeClockSSHUserAuthServer.clock   s     ~%--rJ   N)rK   rL   rM   propertyr   r   rO   rJ   rH   r~   r~      sH        ? ? X? . . X. . .rJ   r~   c                   H    e Zd Zed             Zed             ZeedZdZ	dS )CommandFactoryc                 :    dt          j        t                    iS N   ssh-rsa)rT   )r8   
fromStringr>   rF   s    rH   
publicKeyszCommandFactory.publicKeys   s    CN0ABBBCCrJ   c                 :    dt          j        t                    iS r   )r8   r   r<   rF   s    rH   privateKeyszCommandFactory.privateKeys   s    CN0BCCCDDrJ   )s   ssh-userauths   ssh-connectionr   N)
rK   rL   rM   r   r   r   r~   r6   servicesr   rO   rJ   rH   r   r      sf        D D XD E E XE 4( H  !rJ   r   c                       e Zd ZdS )MemoryAddressN)rK   rL   rM   rO   rJ   rH   r   r      s        DrJ   r   c                       e Zd ZdZd Z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                 "    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)rG   servers     rH   re   z SingleUseMemoryEndpoint.__init__   s     	rJ   c           	      J   | j         t          d          	 |                    t                                }t	          | j        t          | j        d          |t          |d                    | _         t          |          S # t          $ r t                      cY S w xY w)Nz(SingleUseMemoryEndpoint was already usedTisServerF)
r   	Exceptionrr   r   r@   r   rB   r   BaseExceptionr   )rG   r   protocols      rH   r@   zSingleUseMemoryEndpoint.connect   s    9 FGGG	%,,]__==H  &t|dCCC&x%@@@	 DI 8$$$  	 	 	66MMM	s   !B B"!B"N)rK   rL   rM   rN   re   r@   rO   rJ   rH   r   r      s<           % % % % %rJ   r   c                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd ZddZd Z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                    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   passwordTCPz10.0.0.1i90  z192.168.100.200i1  )hostnameportuserpasswordr   r   ra   realmr   portalr   passwdDBaddUserregisterCheckerr   r   doStart
addCleanupdoStopr   clientAddressserverAddressrF   s    rH   setUpz(SSHCommandClientEndpointTestsMixin.setUp  s   *		#)++!^^
TZ((?AAdi777##DM222%''#|"k+,,,(
EBB(0A5IIrJ   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 createNotImplementedError	__class__rK   rF   s    rH   createz)SSHCommandClientEndpointTestsMixin.create1  s'    
 "~&CCC
 
 	
rJ   c                 :    t          | j        j        d          )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- did not implement assertClientTransportStater   rG   clientimmediateCloses      rH   assertClientTransportStatez=SSHCommandClientEndpointTestsMixin.assertClientTransportState:  s)     "~&&&)
 
 	
rJ   c                 :    t          | j        j        d          )z
        Do any remaining work necessary to complete an in-memory connection
        attempted initiated using C{self.reactor}.
        z# did not implement finishConnectionr   rF   s    rH   finishConnectionz3SSHCommandClientEndpointTestsMixin.finishConnectionJ  s'    
 "~&MMM
 
 	
rJ   c                     |                     d          }|                     d          }t          |d| j        | j                  }t          |d| j        | j                  }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   hostAddresspeerAddressT)rr   rB   r   r   r@   )rG   serverFactoryclientFactoryclientProtocolserverProtocolclientTransportserverTransportr   s           rH   connectedServerAndClientz;SSHCommandClientEndpointTestsMixin.connectedServerAndClientS  s     '44T::&44T::0**	
 
 
 1**	
 
 
 ~XX~t33rJ   c                 p   |                                  }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   r\      unknown channelFN)r   r   r   r   r@   r   flushLoggedErrorsr	   assertInvaluerT   assertEquallenfailureResultOftrapr   )	rG   endpointr   	connectedr   r   r   errorsfs	            rH   test_channelOpenFailurez:SSHCommandClientEndpointTestsMixin.test_channelOpenFailurep  s    ;;==))#$$W--	#4466 ''
33'&)/*>q	@U)VWWWCKK(((   ++	z+QW];;;''66666rJ   c                    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)rQ   r   rd   r   r   r   r   r@   r   r   r   r	   r   r   r   rG   r   r   r   r   r   r   r   s           rH   test_execFailurez3SSHCommandClientEndpointTestsMixin.test_execFailure  s     0A
 ,;;==))#$$W--	#4466  ++	z117=AAA''66666rJ   c                    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)r^   r   rd   r   r   r   r   r@   r   cancelr   r   r   r   r   s           rH   test_execCancelledz5SSHCommandClientEndpointTestsMixin.test_execCancelled  s     0F
 ,;;==))#$$W--	#4466  ++	~''55555rJ   c                    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)rZ   r   rd   r   ro   r   r   r@   r   assertIsInstancerq   r.   r   r   getHostr   r   usernamecommand)rG   r   r   r   r   r   s         rH   test_buildProtocolz5SSHCommandClientEndpointTestsMixin.test_buildProtocol  s     0B
 ,;;==#%%#!!!#4466go/@AAA)1133W_5KLLLGO$<===(?@@@@@rJ   c                 B   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)rZ   r   rd   r   r   r   r   r@   r   successResultOfassertIsNotNoner   rG   r   r   r   r   r   r   r   s           rH   test_makeConnectionz6SSHCommandClientEndpointTestsMixin.test_makeConnection  s     0B
 ,;;==))#$$W--	#4466''	22X/00000rJ   c                    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, worldrJ   N)rZ   r   rd   r   r   r   r   r@   r   r   appenddataReceivedr   idservicechannelswriter   r   join)
rG   r   r   r   r   r   r   r   r   	channelIds
             rH   test_dataReceivedz4SSHCommandClientEndpointTestsMixin.test_dataReceived  s     0B
 ,;;==))#$$W--	#4466''	22 , 3 &)		*00AAA		#((<*@*@AAAAArJ   c                    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)rZ   r   rd   r   r   r   r   r@   r   r   r   connectionLostr   r   r   r   loseConnectionr   r   r   r   )
rG   r   r   r   r   r   r   r   r   r   s
             rH   test_connectionLostz6SSHCommandClientEndpointTestsMixin.test_connectionLost  s    
 0B
 ,;;==))#$$W--	#4466''	22"0"7 &)		*99;;;		q~...''66666rJ   c                 &   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   )rZ   r   rd   r   r   r   r   r@   r   r   r   r   r   r   r   r   sendRequestr   r   r   )rG   request
requestArgr   r   r   r   r   r   r   r   r   channels                rH   _exitStatusTestz2SSHCommandClientEndpointTestsMixin._exitStatusTest  s     0B
 ,;;==))#$$W--	#4466''	22"0"7
 &)	.))4""7GZ@@@   		''666a  rJ   c                     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   )rG   exitCodeexcs      rH   test_zeroExitCodez4SSHCommandClientEndpointTestsMixin.test_zeroExitCode2  s@     "">4h3G3GHH     rJ   c                 
   d}d}|                      dt          d|                    }|                    t                     |                     ||j        j                   |                     ||j        j                   dS )r   {   Nr  r  )r   r   r   r   r   r   r  signal)rG   r  r  r  s       rH   test_nonZeroExitStatusz9SSHCommandClientEndpointTestsMixin.test_nonZeroExitStatus<  s|     "">4h3G3GHH"###39#5666!122222rJ   c                    t                      }t          j        |           |                     t          j        |           d}d}d                    t          j        d          dt          j        d          t          j        d          g          }|                     d|          }|	                    t                     |                     ||j        j                   |                     ||j        j                   d	}t          j        |t          j        t          j        t          j        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   rJ   s   TERM   s   messages   en-USs   exit-signalz'twisted.conch.endpoints._CommandChannelTmessage)	log_levellog_namespaceshortSignalName
coreDumpederrorMessagelanguageTag)r   r    addObserverr   removeObserverr   r3   NSr   r   r   r   r   r  r  hamcrestassert_thathas_itemhas_entriesequal_tor   info)rG   logObserverr  r  packetr  logNamespaces          rH   test_nonZeroExitSignalz9SSHCommandClientEndpointTestsMixin.test_nonZeroExitSignalI  sT    +,,&{333*9;GGG	'""	*%%	(##
 
 "">6::"###39#5666!1222@$%-%6x}%E%E)5+2&*(1'/ 	 	 	
 	
 	
 	
 	
rJ   Fc                     |j         j        }g |rf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                  .                          d           S rc   )r   )recorders   rH   rh   z;SSHCommandClientEndpointTestsMixin.record.<locals>.<lambda>  s    -- rJ   )r   r   r   setattrr   r   )rG   r   r   eventnoArgsr   r   r#  s          @rH   recordz)SSHCommandClientEndpointTestsMixin.recordw  sU      &)	 	 ----AAA'	2E1===rJ   c                    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   rJ   N)rZ   r   rd   r   r   r   r   r@   r   r   r'  r   r   r   r   r   	rG   r   r   r   r   r   r   r   r   s	            rH   
test_writez-SSHCommandClientEndpointTestsMixin.test_write  s    
 0B
 ,;;==))#$$W--	#4466''	22{{68^DD  111		#((<*@*@AAAAArJ   c                    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   rJ   N)rZ   r   rd   r   r   r   r   r@   r   r   r'  r   writeSequencer   r   r   r)  s	            rH   test_writeSequencez5SSHCommandClientEndpointTestsMixin.test_writeSequence  s    
 0B
 ,;;==))#$$W--	#4466''	22{{68^DD((/):;;;		#((<*@*@AAAAArJ   N)F)rK   rL   rM   rN   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r	  r   r'  r*  r-  rO   rJ   rH   r   r     sR        J J J(
 
 

 
 
 
 
 
4 4 4:7 7 747 7 7*6 6 6*A A A.1 1 1&B B B67 7 78! ! !:! ! !3 3 3,
 ,
 ,
\   6B B B*B B B B BrJ   r   c                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdS )NewConnectionTestsz`
    Tests for L{SSHCommandClientEndpoint} when using the C{newConnection}
    constructor.
    c                    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.
        r   N)r   r   r#   mktemphostKeyPathr,   
knownHosts
addHostKeyr   r   r   r!   r   hostsaverF   s    rH   r   zNewConnectionTests.setUp  s    
 	+00666#DKKMM22()9::""4=$,2I*2UVVV""$,122DL4KJ4W	
 	
 	
 	rJ   c                     t          j        | j        d| j        | j        | j        | j        | j        t          d                    S )zu
        Create and return a new L{SSHCommandClientEndpoint} using the
        C{newConnection} constructor.
        r   Fr   r4  ui)	r/   newConnectionr   r   r   r   r   r4  rt   rF   s    rH   r   zNewConnectionTests.create  sJ    
 (5LIMI]u%%	
 	
 	
 		
rJ   c                 d    |                      | j        | j        j        d         d                   S )z}
        Establish the first attempted TCP connection using the SSH server which
        C{self.factory} can create.
        r      )r   r   r   
tcpClientsrF   s    rH   r   z#NewConnectionTests.finishConnection  s2    
 ,,L$,1!4Q7
 
 	
rJ   c                    |                      ||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'  r   r   r   r   reportDisconnect)rG   r   r   r   r   r@  s         rH   loseConnectionToServerz)NewConnectionTests.loseConnectionToServer  s    * VXxEE))+++		$(((
 			 	))+++++rJ   c                     |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)
assertTruer   rE   disconnectingr   s      rH   r   z-NewConnectionTests.assertClientTransportState  sH      	<OOF,455555OOF,:;;;;;rJ   c                     t          j        | j        dd| j        | j                  }|                     t          t          |                     dS )zY
        L{SSHCommandClientEndpoint} instances provide L{IStreamClientEndpoint}.
           dummy command
   dummy userN)r/   r;  r   r   r   rD  r   r   rG   r   s     rH   test_interfacez!NewConnectionTests.test_interface  sM     ,9L*M4=$)
 
 	%:HEEFFFFFrJ   c                     t          j        | 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.
        rG  rH     Nr/   r;  r   r   r   _creatorr   rI  s     rH   test_defaultPortz#NewConnectionTests.test_defaultPort$  sH    
 ,9L*M4=
 
 	X.344444rJ   c                     t          j        | j        dd| j        d          }|                     d|j        j                   dS )zU
        L{SSHCommandClientEndpoint} uses the C{port} argument if specified.
        rG  rH  i  )r   NrM  rI  s     rH   test_specifiedPortz%NewConnectionTests.test_specifiedPort.  sO     ,9L*M4=t
 
 
 	x0566666rJ   c                    t          j        | j        d| j        | j        | j        | j        | j        t          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   Fr9  r   r\   N)r/   r;  r   r   r   r   r   r4  rt   r   r   r   r@   r>  r   r!   r   )rG   r   r   r6  r   timeoutbindAddresss          rH   test_destinationz#NewConnectionTests.test_destination7  s    
 ,9LIMI]u%%	
 	
 	
 ))#!!!48L4KA4N1dGWkd(;(;<<<D)))C 78899999rJ   c                    t          j        | j        dd| j        | j        | j        t          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   rH  Fr4  r:  r   r=  N)r/   r;  r   r   r   r4  rt   r   r   r   r@   r>  clientConnectionFailedr"   r   r   r   rG   r   r   ds       rH   test_connectionFailedz(NewConnectionTests.test_connectionFailedO  s     ,9LMIu%%
 
 
 ))#W%%,)!,Q/&&tW5K5M5M-N-NOOOQ$$%;<<<<<rJ   c                    t          j        | j        dd| j        | j        t          |                                           t          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   rH  FrW  r   r=  N)r/   r;  r   r   r   r,   r2  rt   r   r   r   r@   r   r   r>  r   r   r   r   s           rH   test_userRejectedHostKeyz+NewConnectionTests.test_userRejectedHostKeyg  s     ,9LMI%dkkmm44u%%
 
 
 ))#$$W--	#<<L$,1!4Q7 
  
   ++	rJ   c           
      <   t          j        t                                                    }t	          t          |                                                     }|                    t          | j	        j
                  |           t          j        t          d                                          }|                    | j        |           t          d          }t          j        | j        dd| j        | j        d||          }t%                      }t&          |_        |                    |          }|                     | j        | j        j        d         d	                   \  }}	}
|                     |          }|                    t6                     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)
passphraseTr   rH     dummy passwordr9  r   r=  N)r8   r   r<   publicr,   r#   r2  r5  r!   r   r6  r=   r   rt   r/   r;  r   r   r   r   r   r@   r   r   r>  r   r   r
   )rG   firstKeyr4  differentKeyr:  r   r   r   r   r   r   r   s               rH   test_mismatchedHostKeyz)NewConnectionTests.test_mismatchedHostKey  sk    >"455<<>>#HT[[]]$;$;<<
mD,>,CDDhOOO~,
 
 

&(( 	 	dm\:::
 T""+9LMI&!	
 	
 	
 ))#$$W--	#<<L$,1!4Q7 
  
   ++	~rJ   c                 2   t          j        | j        dd| j        | j        | j        t          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   rH  FrW  r   r=  N)r/   r;  r   r   r   r4  rt   r   r   r   r@   r   r>  rr   makeConnectionr   r"   r   r   r   rG   r   r   rZ  r   r   s         rH   !test_connectionClosedBeforeSecurez4NewConnectionTests.test_connectionClosedBeforeSecure  s     ,9LMIu%%
 
 
 ))#W%%#%%	,)!,Q/&&t,,i(((gn&6&677888Q$$^44444rJ   c                    t          j        | j        dd| j        | j        | j        t          d                    }t                      }t          |_	        |
                    |          }t          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   rH  FrW  Nr   r   r=  )r/   r;  r   r   r   r4  rt   r   r   r   r@   rB   r>  rr   rf  r   r   r   r   rD  rE   r   r"   r   rg  s         rH   $test_connectionCancelledBeforeSecurez7NewConnectionTests.test_connectionCancelledBeforeSecure  s!    ,9LMIu%%
 
 
 ))#W%%*4%@@@	,)!,Q/&&t,,i(((	


Q$$^444	)*** 	gn&6&67788888rJ   c                    t          j        | j        dd| j        | j        | j        t          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   rH  FrW  r   N)r/   r;  r   r   r   r4  rt   r   r   r   r@   r   r   r   r   rD  
connectorsstoppedConnectingrY  s       rH   'test_connectionCancelledBeforeConnectedz:NewConnectionTests.test_connectionCancelledBeforeConnected  s    
 ,9LMIu%%
 
 
 ))#W%%	


Q$$%=>>>/2DEEEEErJ   c                 B   t          j        | j        dd| j        | j        d| j        t          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   rH  r`  Fr9  r   r=  N)r/   r;  r   r   r   r4  rt   r   r   r   r@   r   r   r>  advancer   passwordDelayflushr   r   r-   r   r   s           rH   "test_passwordAuthenticationFailurez5NewConnectionTests.test_passwordAuthenticationFailure  s    ,9LMI&u%%	
 	
 	
 ))#$$W--	#<<L$,1!4Q7 
  
 	V^9::: 	

  ++	#$$$ 	''66666rJ   c                     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                 d    i | ]-\  }}|t          j        |                                          g.S rO   )r8   r   ra  ).0kvs      rH   
<dictcomp>z6NewConnectionTests.setupKeyChecker.<locals>.<dictcomp>:  s7    MMMtq!1s~a((//112MMMrJ   N)itemsr*   r)   r   )rG   r   usersmappingcheckers        rH   setupKeyCheckerz"NewConnectionTests.setupKeyChecker-  sP     NMu{{}}MMM%&6w&?&?@@w'''''rJ   c                 p   t          j        t                    }|                     | j        | j        t          i           t          j        | j	        d| j        | j
        | j        |g| j        t          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keysr4  r:  r   r=  N)r8   r   r<   r~  r   r   r;   r/   r;  r   r   r   r4  rt   r   r   r   r@   r   r   r>  r   r   r-   rD  r   rE  )	rG   badKeyr   r   r   r   r   r   r   s	            rH   #test_publicKeyAuthenticationFailurez6NewConnectionTests.test_publicKeyAuthenticationFailure>  s     233T[496H*IJJJ+9LIMIu%%	
 	
 	
 ))#$$W--	#<<L$,1!4Q7 
  
   ++	#$$$ 	(677777rJ   c                    t          j        t                    }|                     | j        | j        t          i           t          j        | j	        d| j        | j
        | j        |g| j        | j        t          d          	  	        }t                      }t           |_        |                    |          }| j        xj        dz  c_        |                     | j        | j	        j        d         d                   \  }}}|                                 |                     t2                    }|                     d|d         j        j        |d         j        j        f           |                     dt=          |                     |                     |          }	|	                     t2                     |                     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   r4  r:  r\   r   r=  r   r   N)$r8   r   r<   r~  r   r   r;   r/   r;  r   r   r   r   r4  rt   r   r   r   r@   r   r   r   r>  r   r   r	   r   r   rT   r   r   r   r   rD  r   rE  )
rG   r  r   r   r   r   r   r   r   r   s
             rH   test_authenticationFallbackz.NewConnectionTests.test_authenticationFallbackc  s   
  233T[496H*IJJJ+9LIMI]u%%

 

 

 ))#$$W--	 	--2--#<<L$,1!4Q7 
  
 			 ''
33'&)/*>q	@U)VWWWCKK(((   ++	z+QW];;; 	(677777rJ   c                 Z   t          j        t                    }|                     | j        | j        t          i           t          | j        j        d<   t          j
        | j        d| j        | j        | j        |g| j        t          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)r8   r   r;   r~  r   r   rZ   r   rd   r/   r;  r   r   r   r4  rt   r   r   r   r@   r   r   r>  r   r   r   )	rG   keyr   r   r   r   r   r   r   s	            rH   test_publicKeyAuthenticationz/NewConnectionTests.test_publicKeyAuthentication  s   
 n/00T[496H*IJJJ/A
 ,+9LIMIu%%	
 	
 	
 ))#$$W--	#<<L$,1!4Q7 
  
 ''	22X/00000rJ   c                    t          j        | j        d| j        | j        | j        | j        t          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   FrW  r   r=  N)r/   r;  r   r   r   r   r4  rt   r   r   r   r@   r   r   r>  r   r   r   r-   rD  r   rE  r   s           rH   test_skipPasswordAuthenticationz2NewConnectionTests.test_skipPasswordAuthentication  s    
 ,9LIMIu%%
 
 
 ))#$$W--	#<<L$,1!4Q7 
  
 			   ++	#$$$ 	(677777rJ   c                 0   t          j        t                    }t                      }t	                      |_        |                                |dfi|j        _        |                     | j	        | j
        t          i           t          |          }t          j        | j        d| j
        | j        | j        | j        t%          d          |          }t&          | j        j        d<   t	                      }t,          |_        |                    |          }|                     | j        | j        j        d         d                   \  }}}	t7          d          D ]/}
|j                                         |	                                 0|                     |          }|                     |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.
        rJ   r   F)r4  r:  agentEndpointr   r   r=     N)$r8   r   r<   r4   r   r   blobr  r~  r   r   r   r/   r;  r   r   r   r4  rt   rZ   r   rd   r   r   r@   r   r>  ranger   r   r   r   rB  rD  rE  clientIO)rG   r  agentServerr  r   r   r   r   r   r   ir   s               rH   test_agentAuthenticationz+NewConnectionTests.test_agentAuthentication  s    n/00$&&%ii$'HHJJc
#; T[496H*IJJJ/<<+9LIMIu%%'	
 	
 	
 0B
 ,))#$$W--	#<<L$,1!4Q7 
  
 r 	 	A##%%%IIKKKK''	22X/000 	##FFHdCCC(6777*3ABBBBBrJ   c                 |   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)rZ   r   rd   r   r   r   r   r@   r   r   rB  rD  r   rE  r   s           rH   test_loseConnectionz&NewConnectionTests.test_loseConnection  s     0B
 ,;;==))#$$W--	#4466''	22##FFHdCCC 	(677777rJ   N)rK   rL   rM   rN   r   r   r   rB  r   rJ  rO  rQ  rU  r[  r]  rd  rh  rj  rn  rs  r~  r  r  r  r  r  r  rO   rJ   rH   r/  r/    s        
  
 
 
 
 
 
!, !, !,F< < <G G G5 5 57 7 7: : :0= = =0     8* * *X5 5 5< 9  9  9DF F F,&7 &7 &7P( ( ("#8 #8 #8J.8 .8 .8`1 1 1>8 8 8B1C 1C 1Cf8 8 8 8 8rJ   r/  c                   *    e Zd ZdZd Zd Zd Zd ZdS )ExistingConnectionTestsze
    Tests for L{SSHCommandClientEndpoint} when using the C{existingConnection}
    constructor.
    c                    t                               |            t          t          |                                                     }|                    | j        | j        j        d                    |                    t          | j
        j                  | j        j        d                    t          j        | j        d| j        | j        | j        | j        |t%          d                    | _        dS )r1  r   r   Fr9  N)r   r   r,   r#   r2  r5  r   r   r   r!   r   r6  r/   r;  r   r   r   r   rt   r   )rG   r4  s     rH   r   zExistingConnectionTests.setUp#  s    
 	+00666#HT[[]]$;$;<<
dmT\-DZ-PQQQ$,122DL4KJ4W	
 	
 	
 1>LIMI]!u%%	
 	
 	
rJ   c                    t                      }t          |_        | j                            |          }| j        j                                        }	 t          | j        j        d<   | 	                    | j
        | j        j        d         d                   \  }}}| j        j                                         | j        j                            |           nB# | j        j                                         | j        j                            |           w xY w|| _        || _        || _        |                     |          }|j        j        }t+          j        |d          S )zz
        Create and return a new L{SSHCommandClientEndpoint} using the
        C{existingConnection} constructor.
        r   r   r=  r   )r   r   r   r   r@   r   rd   copyrZ   r   r   r   r>  clearupdater   _client_pumpr   r   connr/   existingConnection)	rG   r   r   rd   r   r   r   r   
connections	            rH   r   zExistingConnectionTests.create;  s<   
 ))#M))'22	 
05577		;3EDJ$Z0#'#@#@dl5a8;$ $ FFD
 J$**,,,J$++M:::: J$**,,,J$++M::::
''	22',
'::}UUUs   A	C ?Dc                     | 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.
        )r  r   r   r  rF   s    rH   r   z(ExistingConnectionTests.finishConnectionY  s_     	



|T\4:55rJ   c                     |                      |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)assertFalser   rE  rE   r   s      rH   r   z2ExistingConnectionTests.assertClientTransportStateg  s>     	)7888)122222rJ   N)rK   rL   rM   rN   r   r   r   r   rO   rJ   rH   r  r    s]         

 
 
0V V V<6 6 63 3 3 3 3rJ   r  c                   *    e Zd ZdZd Zd Zd Zd ZdS )ExistingConnectionHelperTestsz1
    Tests for L{_ExistingConnectionHelper}.
    c                 `    |                      t          t          t                               dS )zT
        L{_ExistingConnectionHelper} implements L{_ISSHConnectionCreator}.
        N)rD  r   r1   r0   rF   s    rH   rJ  z,ExistingConnectionHelperTests.test_interfacew  s(     	$:<UVVWWWWWrJ   c                     t                      }t          |          }|                     ||                     |                                                     dS )z
        L{_ExistingConnectionHelper.secureConnection} returns a L{Deferred}
        which fires with whatever object was fed to
        L{_ExistingConnectionHelper.__init__}.
        N)objectr0   assertIsr   secureConnection)rG   rv   helpers      rH   test_secureConnectionz3ExistingConnectionHelperTests.test_secureConnection}  sN     *622fd2263J3J3L3LMMNNNNNrJ   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r0   r  cleanupConnectionrG   r  s     rH   $test_cleanupConnectionNotImmediatelyzBExistingConnectionHelperTests.test_cleanupConnectionNotImmediately  s7    
 +68844 	  511111rJ   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{True}.
        TNr  r  s     rH   !test_cleanupConnectionImmediatelyz?ExistingConnectionHelperTests.test_cleanupConnectionImmediately  s7    
 +68844 	  400000rJ   N)rK   rL   rM   rN   rJ  r  r  r  rO   rJ   rH   r  r  r  s`         X X XO O O	2 	2 	2	1 	1 	1 	1 	1rJ   r  c                       e Zd ZdedefdZdS )_WriteDiscarderrT   returnc                    dS )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   rO   rS   s     rH   r   z_WriteDiscarder.write  s	    
 qrJ   N)rK   rL   rM   r  intr   rO   rJ   rH   r  r    s6        &       rJ   r  c                   >    e Zd ZdZdeddfdZdedee         fdZdS )_PTYPathzk
    A L{FilePath}-like object which can be opened to create a L{_ReadFile} with
    certain contents.
    contentsr  Nc                     || _         dS )zz
        @param contents: L{bytes} which will be the contents of the
            L{_ReadFile} this path can open.
        N)r  )rG   r  s     rH   re   z_PTYPath.__init__  s    
 !rJ   modec                 :    |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+)r  r  )rG   r  s     rH   openz_PTYPath.open  s!     t||||t}---rJ   )	rK   rL   rM   rN   bytesre   strr   r  rO   rJ   rH   r  r    sf         
! !4 ! ! ! !. .E . . . . . .rJ   r  c                   N    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd ZdS )NewConnectionHelperTestsz,
    Tests for L{_NewConnectionHelper}.
    c                 `    |                      t          t          t                               dS )zO
        L{_NewConnectionHelper} implements L{_ISSHConnectionCreator}.
        N)rD  r   r1   r2   rF   s    rH   rJ  z'NewConnectionHelperTests.test_interface  s(     	$:<PQQRRRRRrJ   c                 F    |                      dt          j                   dS )zK
        The default I{known_hosts} path is I{~/.ssh/known_hosts}.
        z~/.ssh/known_hostsN)r   r2   _KNOWN_HOSTSrF   s    rH   test_defaultPathz)NewConnectionHelperTests.test_defaultPath  s%     	-/C/PQQQQQrJ   c                     t                      |                     t          df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 rc   rO   )clsrv   s    rH   rh   zANewConnectionHelperTests.test_defaultKnownHosts.<locals>.<lambda>  s    F rJ   N)r  patchr2   r  r4  )rG   r  rv   s     @rH   test_defaultKnownHostsz/NewConnectionHelperTests.test_defaultKnownHosts  sr    
 

'8J8J8J8JKKK%$dD$dD$
 
 	ff/00000rJ   c                 b   t                      j        d         }t          |                                           }t	          |          }|                    d|           |                                 t          d|j                   t          j        
                    d          }|j                            |d          }|                     t          d|           t          d|           t          j                    }|                     |                    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.
        r   s	   127.0.0.1zCreated known_hosts file at z~/r  zPatched _KNOWN_HOSTS with N)r   r   r#   r2  r,   r5  r7  r$   pathos
expanduserreplacer  r2   r  rD  
hasHostKey)rG   r  r  r4  homedefaultloadeds          rH   test_readExistingz*NewConnectionHelperTests.test_readExisting  s
    )*5&&#D))
lC00084988999 w!!$'')##D$//

'AAA444555%133)),<<=====rJ   c                 v    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)r2   r   r:  r+   r  s     rH   test_defaultConsoleUIz.NewConnectionHelperTests.test_defaultConsoleUI  sH    
 &$dD$dD$
 
 	fi33333rJ   c                     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  r2   r   r:  rz   rD  rG   ttyr  rv   s       rH   test_ttyConsoleUIz*NewConnectionHelperTests.test_ttyConsoleUI  su     v%$dD$dD$
 
 %%fi&6&67I&J&JKKrJ   c                     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#   r2  r2   r   r:  rz   r  r  s       rH   test_nottyUIz%NewConnectionHelperTests.test_nottyUI  s     t{{}}%%%$dD$dD$
 
 %%fi&6&67I&J&JKK     rJ   c                     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)r2   r   r#   r  r  s     rH   test_defaultTTYFilenamez0NewConnectionHelperTests.test_defaultTTYFilename  sP    
 &$dD$dD$
 
 	+..
;;;;;rJ   c                     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)r2   r6   r   r   r  rD  rE  )rG   r  r  s      rH   r  z=NewConnectionHelperTests.test_cleanupConnectionNotImmediately#  su    
 &$dD$dD$
 
 #__
.00
  U333
,:;;;;;rJ   c                 .    G 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                       e Zd ZdZd ZdS )MNewConnectionHelperTests.test_cleanupConnectionImmediately.<locals>.AbortableFc                     d| _         dS )z7
                Abort the connection.
                TNrD   rF   s    rH   rI   z]NewConnectionHelperTests.test_cleanupConnectionImmediately.<locals>.Abortable.abortConnection9  s      $rJ   N)rK   rL   rM   rE   rI   rO   rJ   rH   	Abortabler  6  s(        G$ $ $ $ $rJ   r  NT)r2   r6   r9   r   r  rD  rE   )rG   r  r  r  s       rH   r  z:NewConnectionHelperTests.test_cleanupConnectionImmediately0  s    	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ &$dD$dD$
 
 #__
133
)2
&  T222
,6>?????rJ   N)rK   rL   rM   rN   rJ  r  r  r  r  r  r  r  r  r  rO   rJ   rH   r  r    s         S S SR R R1 1 1> > >.4 4 4     ! ! !< < << < <@ @ @ @ @rJ   r  )yrN   os.pathr  ior   structr   typingr   zope.interfacer   zope.interface.verifyr   r   r  twisted.conch.errorr	   r
   r   twisted.conch.interfacesr   twisted.cred.checkersr   twisted.cred.portalr   twisted.internet.addressr   twisted.internet.deferr   r   r   r   twisted.internet.errorr   r   r   r   twisted.internet.interfacesr   r   twisted.internet.protocolr   r   twisted.internet.testingr   r   r   twisted.loggerr   r    twisted.python.compatr!   twisted.python.failurer"   twisted.python.filepathr#   twisted.python.logr$   twisted.python.reflectr%   twisted.trial.unittestr&   twisted.conch.avatarr(   twisted.conch.checkersr)   r*   twisted.conch.client.knownhostsr+   r,   twisted.conch.endpointsr-   r.   r/   r0   r1   r2   twisted.conch.sshr3   twisted.conch.ssh.agentr4   twisted.conch.ssh.channelr5   twisted.conch.ssh.connectionr6   twisted.conch.ssh.factoryr7   twisted.conch.ssh.keysr8   twisted.conch.ssh.transportr9   twisted.conch.ssh.userauthr:   twisted.conch.test.keydatar;   r<   r=   r>   skipr  twisted.test.iosimr?   r@   rB   rQ   rZ   r^   ra   ro   rt   r~   r   r   r   r   r/  r  r  r  r  r  rO   rJ   rH   <module>r     sZ                       & & & & & & ; ; ; ; ; ; ; ;  K K K K K K K K K K / / / / / / I I I I I I & & & & & & 0 0 0 0 0 0 J J J J J J J J J J J J            H G G G G G G G 7 7 7 7 7 7 7 7         
 8 7 7 7 7 7 7 7 / / / / / / * * * * * * , , , , , , " " " " " " 0 0 0 0 0 0 + + + + + +=   $......LLLLLLLLIIIIIIII                )(((((666666444444::::::444444******>>>>>><<<<<<             (DJM
CJNN I 5 5 5 5 5 5 5 5    ]        
   $       $    Z   &2 2 2 2 2 2 2 24 4 4 4 4 4 4 4       . . . . .!2 . . .(! ! ! ! !Z ! ! !* X	 	 	 	 	 	 	 	 "###% #% #% #% #% #% #% $##%LkB kB kB kB kB kB kB kB\]	8 ]	8 ]	8 ]	8 ]	8#E ]	8 ]	8 ]	8@R3 R3 R3 R3 R3h(J R3 R3 R3j)1 )1 )1 )1 )1H )1 )1 )1X    g   . . . . . . . .0G@ G@ G@ G@ G@x G@ G@ G@ G@ G@rJ   