
    Vh                        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=      Zxy)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y)AbortableFakeTransportzC
    A L{FakeTransport} with added C{abortConnection} support.
    Fc                     d| _         y)z}
        Abort the connection in a fake manner.

        This should really be implemented in the underlying module.
        TNabortedselfs    Q/home/dcms/DCMS/lib/python3.12/site-packages/twisted/conch/test/test_endpoints.pyabortConnectionz&AbortableFakeTransport.abortConnection]   s         N)__name__
__module____qualname____doc__rE   rI    rJ   rH   rB   rB   V   s     GrJ   rB   c                       e Zd ZdZd Zy)BrokenExecSessionzO
    L{BrokenExecSession} is a session on which exec requests always fail.
    c                      y)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        rJ   NrK   rL   rM   rN   rU   rO   rJ   rH   rQ   rQ   f       
rJ   rQ   c                       e Zd ZdZd Zy)WorkingExecSessionzS
    L{WorkingExecSession} is a session on which exec requests always succeed.
    c                      y)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y)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     zrJ   NrW   rO   rJ   rH   r^   r^      s    

rJ   r^   c                       e Zd Zd Zd Zy)TrivialRealmc                     i | _         y N)channelLookuprF   s    rH   __init__zTrivialRealm.__init__   s
    rJ   c                 L    t               }| j                  |_        t        |d fS )Nc                       y rc   rO   rO   rJ   rH   <lambda>z,TrivialRealm.requestAvatar.<locals>.<lambda>   s    rJ   )r(   rd   r   )rG   avatarIdmind
interfacesavatars        rH   requestAvatarzTrivialRealm.requestAvatar   s$    #11FL11rJ   N)rK   rL   rM   re   rm   rO   rJ   rH   ra   ra      s     2rJ   ra   c                       e Zd ZdZd Zy)AddressSpyFactoryNc                 <    || _         t        j                  | |      S rc   )addressr   buildProtocol)rG   rq   s     rH   rr   zAddressSpyFactory.buildProtocol   s    $$T733rJ   )rK   rL   rM   rq   rr   rO   rJ   rH   ro   ro      s    G4rJ   ro   c                       e Zd Zd Zd Zd Zy)FixedResponseUIc                     || _         y 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                      y rc   rO   rx   s     rH   warnzFixedResponseUI.warn   s    rJ   N)rK   rL   rM   re   rz   r|   rO   rJ   rH   rt   rt      s    $rJ   rt   c                   ,    e Zd Zed        Zed        Zy)FakeClockSSHUserAuthServerc                 B    | 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                 B    | 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~      s(    ? ? . .rJ   r~   c                   :    e Zd Zed        Zed        ZeedZdZ	y)CommandFactoryc                 :    dt        j                  t              iS N   ssh-rsa)rT   )r8   
fromStringr>   rF   s    rH   
publicKeyszCommandFactory.publicKeys   s    CNN0ABCCrJ   c                 :    dt        j                  t              iS r   )r8   r   r<   rF   s    rH   privateKeyszCommandFactory.privateKeys   s    CNN0BCDDrJ   )s   ssh-userauths   ssh-connectionr   N)
rK   rL   rM   r   r   r   r~   r6   servicesr   rO   rJ   rH   r   r      sC    D D E E 4(H  !rJ   r   c                       e Zd Zy)MemoryAddressN)rK   rL   rM   rO   rJ   rH   r   r      s    rJ   r   c                       e Zd ZdZd Zd Zy)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| _         || _        y)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                   t        d      	 |j                  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    99 FGG	%,,]_=H  &t||dC&x%@	DI 8$$  	6M	s   A: :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y)"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                  j                  | j                         t               | _        | j
                  | j                  _        | j                  | j                  _	        | j                  j!                          | j#                  | j                  j$                         t'        ddd      | _        t'        ddd	      | _        y )
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    *		#)+!^
TZZ(?Adii7##DMM2%'#||"kk++,(
EB(0A5IrJ   c                 F    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'    
 "~~&&))BC
 	
rJ   c                 F    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                 F    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'    
 "~~&&))LM
 	
rJ   c                     |j                  d      }|j                  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~t33rJ   c                 8   | j                         }t               }t        |_        |j	                  |      }| j                         \  }}}| j                  t              }| j                  d|d   j                  j                  |d   j                  j                  f       | j                  dt        |             | j                  |      }|j                  t               | j                  d|j                  j                         | j                  |d       y)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 ''
3'&)//*>*>q	@U@U)VWCK(   +	z+QWW]];''6rJ   c                    t         | j                  j                  d<   | j                         }t	               }t
        |_        |j                  |      }| j                         \  }}}| j                  |      }|j                  t               | j                  d|j                  j                         | j                  |d       y)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  +	z1177==A''6rJ   c                 b   t         | j                  j                  d<   | j                         }t	               }t
        |_        |j                  |      }| j                         \  }}}|j                          | j                  |      }|j                  t               | j                  |d       y)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  +	~''5rJ   c                 :   t         | j                  j                  d<   | j                         }t	               }t
        |_        |j                  |       | j                         \  }}}| j                  |j                  t               | j                  |j                  j                         |j                  j                         | j                  | j                   |j                  j"                         | j                  d|j                  j$                         y)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goo/@A))113W__5K5KLGOO$<$<=(?(?@rJ   c                 *   t         | j                  j                  d<   | j                         }t	               }t
        |_        |j                  |      }| j                         \  }}}| j                  |      }| j                  |j                         y)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''	2X//0rJ   c                    t         | j                  j                  d<   | j                         }t	               }t
        |_        |j                  |      }| j                         \  }}}| j                  |      }g }|j                  |_        |j                  j                  }	|j                  j                  |	   j!                  d       |j#                          | j%                  ddj'                  |             y)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''	2 , 3 3 &&))		*00A		#((<*@ArJ   c                    t         | j                  j                  d<   | j                         }t	               }t
        |_        |j                  |      }| j                         \  }}}| j                  |      }g }|j                  |_        |j                  j                  }	|j                  j                  |	   j!                          |j#                          |d   j%                  t&               | j)                  |d       y)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''	2"0"7"7 &&))		*99;		q~.''6rJ   c                    t         | j                  j                  d<   | j                         }t	               }t
        |_        |j                  |      }| j                         \  }}}| j                  |      }	g }
|
j                  |	_        |	j                  j                  }|j                  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''	2"0"7"7
 &&))	..)))4""7GZ@ 		''6a  rJ   c                 j    d}| j                  dt        d|            }|j                  t               y)
        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h3GH rJ   c                    d}d}| j                  dt        d|            }|j                  t               | j	                  ||j
                  j                         | j	                  ||j
                  j                         y)r   {   Nr  r  )r   r   r   r   r   r   r  signal)rG   r  r  r  s       rH   test_nonZeroExitStatusz9SSHCommandClientEndpointTestsMixin.test_nonZeroExitStatus<  sh     "">4h3GH"#399#5#56!1!12rJ   c                    t               }t        j                  |       | j                  t        j                  |       d}d}dj                  t        j                  d      dt        j                  d      t        j                  d      g      }| j                  d|      }|j                  t               | j                  ||j                  j                         | j                  ||j                  j                         d	}t        j                   |t        j"                  t        j$                  t        j&                  t(        j*                        |dd
ddd                   y)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  s     +,&&{3*99;G		'"		*%		(#
 "">6:"#399#5#56!1!12@$$%-%6%6x}}%E)5+2&*(1'/		
rJ   c                     |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                  &     j                  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  sL      &&))	-AA''	2E1=rJ   c                    t         | j                  j                  d<   | j                         }t	               }t
        |_        |j                  |      }| j                         \  }}}| j                  |      }| j                  ||d      }|j                  j                  d       |j                          | j                  ddj                  |             y)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''	2{{68^D  1		#((<*@ArJ   c                    t         | j                  j                  d<   | j                         }t	               }t
        |_        |j                  |      }| j                         \  }}}| j                  |      }| j                  ||d      }|j                  j                  dg       |j                          | j                  ddj                  |             y)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''	2{{68^D((/):;		#((<*@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     sl    J(

 
4:747*6*A.1&B678!:!3,
\6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y)NewConnectionTestsz`
    Tests for L{SSHCommandClientEndpoint} when using the C{newConnection}
    constructor.
    c                    t         j                  |        t        | j                               | _        t        | j                        | _        | j                  j                  | j                  | j                  j                  d          | j                  j                  t        | j                  j                        | j                  j                  d          | j                  j                          y)
        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#DKKM2()9)9:""4==$,,2I2I*2UV""$,,112DLL4K4KJ4W	
 	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  sL    
 (55LLIIMMII]]u%	
 		
rJ   c                 n    | j                  | 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  s4    
 ,,LL$,,11!4Q7
 	
rJ   c                     | j                  ||dd      }|j                  j                          |j                          | j	                  dg|       |j                          |j                  j                          y)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  sf    * VXxE))+		$(
 			 	))+rJ   c                     |r&| j                  |j                  j                         y| j                  |j                  j                         y)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  s7     OOF,,445OOF,,::;rJ   c                     t        j                  | j                  dd| j                  | j                        }| j                  t        t        |             y)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  sB     ,99LL*M4==$))
 	%:HEFrJ   c                     t        j                  | j                  dd| j                        }| j	                  d|j
                  j                         y)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$  sD    
 ,99LL*M4==
 	X..334rJ   c                     t        j                  | j                  dd| j                  d      }| j	                  d|j
                  j                         y)zU
        L{SSHCommandClientEndpoint} uses the C{port} argument if specified.
        rG  rH  i  )r   NrM  rI  s     rH   test_specifiedPortz%NewConnectionTests.test_specifiedPort.  sF     ,99LL*M4==t
 	x00556rJ   c                 2   t        j                  | j                  d| j                  | j                  | j
                  | j                  | j                  t        d            }t               }t        |_        |j                  |       | j                  j                  d   \  }}}}}| j                  | j                  t        |             | j                  | j
                  |       | j                  dt!        | j                  j                               y)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9LLIIMMII]]u%	
 )#!48LL4K4KA4N1dGWkd(;<D)C 7 789rJ   c                    t        j                  | j                  dd| j                  | j                  | j
                  t        d            }t               }t        |_	        |j                  |      }| j                  j                  d   d   }|j                  dt        t                            | j                  |      j!                  t               y)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9LLMMIIu%
 )#W%,,))!,Q/&&tW5K5M-NOQ$$%;<rJ   c                    t        j                  | j                  dd| j                  | j                  t        | j                               t        d            }t               }t        |_
        |j                  |      }| j                  | j                  | j                  j                  d   d         \  }}}| j                  |      }|j!                  t"               y)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9LLMMII%dkkm4u%
 )#$$W-	#<<LL$,,11!4Q7 
   +	rJ   c           
         t        j                  t              j                         }t	        t        | j                                     }|j                  t        | j                  j                        |       t        j                  t        d      j                         }|j                  | j                  |       t        d      }t        j                  | j                   dd| j                  | j"                  d||      }t%               }t&        |_        |j+                  |      }| j-                  | j.                  | j                   j0                  d   d	         \  }}	}
| j3                  |      }|j5                  t6               y
)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  s8    >>"45<<>#HT[[]$;<
mD,>,>,C,CDhO~~,

&( 	 	dmm\:
 T"+99LLMMII&!	
 )#$$W-	#<<LL$,,11!4Q7 
   +	~rJ   c                    t        j                  | j                  dd| j                  | j                  | j
                  t        d            }t               }t        |_	        |j                  |      }t               }| j                  j                  d   d   }|j                  d      }|j                  |       |j                  t!        t#                            | j%                  |      j'                  t"               y)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9LLMMIIu%
 )#W%#%	,,))!,Q/&&t,i(gn&678Q$$^4rJ   c                 b   t        j                  | j                  dd| j                  | j                  | j
                  t        d            }t               }t        |_	        |j                  |      }t        dd      }| j                  j                  d   d   }|j                  d      }|j                  |       |j                          | j!                  |      j#                  t$               | j'                  |j(                         |j+                  t-        t/                            y)	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9LLMMIIu%
 )#W%*4%@	,,))!,Q/&&t,i(	
Q$$^4	))* 	gn&678rJ   c                    t        j                  | j                  dd| j                  | j                  | j
                  t        d            }t               }t        |_	        |j                  |      }|j                          | j                  |      j                  t               | j                  | j                  j                   d   j"                         y)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9LLMMIIu%
 )#W%	
Q$$%=>//2DDErJ   c                 N   t        j                  | j                  dd| j                  | j                  d| j
                  t        d            }t               }t        |_	        |j                  |      }| j                  | j                  | j                  j                  d   d         \  }}}| j                  j                  |j                  j                          |j#                          | j%                  |      }|j'                  t(               | j+                  |d       y)	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9LLMMII&u%	
 )#$$W-	#<<LL$,,11!4Q7 
 	V^^99: 	

  +	#$ 	''6rJ   c                     |j                         D ci c]*  \  }}|t        j                  |      j                         g, }}}t	        t        |            }|j                  |       yc c}}w )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}
        N)itemsr8   r   ra  r*   r)   r   )rG   r   userskvmappingcheckers          rH   setupKeyCheckerz"NewConnectionTests.setupKeyChecker-  sa     @E{{}Mtq!1s~~a(//122MM%&6w&?@w' Ns   /A,c                    t        j                  t              }| j                  | j                  | j
                  t        i       t        j                  | j                  d| j
                  | j                  | j                  |g| j                  t        d            }t               }t        |_        |j#                  |      }| j%                  | j&                  | j                  j(                  d   d         \  }}}| j+                  |      }|j-                  t.               | j1                  |j2                  j4                         y)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T[[4996H*IJ+99LLIIMMIIu%	
 )#$$W-	#<<LL$,,11!4Q7 
   +	#$ 	((667rJ   c                 :   t        j                  t              }| j                  | j                  | j
                  t        i       t        j                  | j                  d| j
                  | j                  | j                  |g| j                  | j                  t        d      	      }t               }t         |_        |j%                  |      }| j&                  xj(                  dz  c_        | j+                  | j&                  | j                  j,                  d   d         \  }}}|j/                          | j1                  t2              }| j5                  d|d   j6                  j8                  |d   j6                  j6                  f       | j;                  dt=        |             | j?                  |      }	|	jA                  t2               | j;                  d|	j6                  j6                         | jC                  |jD                  jF                         y	)
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T[[4996H*IJ+99LLIIMMII]]u%

 )#$$W-	 	--2-#<<LL$,,11!4Q7 
 			 ''
3'&)//*>*>q	@U@U)VWCK(   +	z+QWW]]; 	((667rJ   c                    t        j                  t              }| j                  | j                  | j
                  t        i       t        | j                  j                  d<   t        j                  | j                  d| j
                  | j                  | j                  |g| j                  t        d            }t!               }t"        |_        |j'                  |      }| j)                  | j*                  | j                  j,                  d   d         \  }}}| j/                  |      }| j1                  |j2                         y)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   
 nn/0T[[4996H*IJ/A

  ,+99LLIIMMIIu%	
 )#$$W-	#<<LL$,,11!4Q7 
 ''	2X//0rJ   c                 (   t        j                  | j                  d| j                  | j                  | j
                  | j                  t        d            }t               }t        |_
        |j                  |      }| j                  | j                  | j                  j                  d   d         \  }}}|j                          | j!                  |      }|j#                  t$               | j'                  |j(                  j*                         y)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9LLIIMMIIu%
 )#$$W-	#<<LL$,,11!4Q7 
 			   +	#$ 	((667rJ   c                 b   t        j                  t              }t               }t	               |_        |j                         |dfi|j
                  _        | j                  | j                  | j                  t        i       t        |      }t        j                  | j                  d| j                  | j                  | j                   | j"                  t%        d      |      }t&        | j(                  j*                  d<   t	               }t,        |_        |j1                  |      }| j3                  | j
                  | j                  j4                  d   d         \  }}}	t7        d      D ],  }
|j8                  j9                          |	j9                          . | j;                  |      }| j=                  |j>                         | jA                  ||||	       | jC                  |j>                  jD                         | jC                  |j8                  jF                  jD                         y	)
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    nn/0$&%i$'HHJc
#; T[[4996H*IJ/<+99LLIIMMIIu%'	
 0B

  ,)#$$W-	#<<LL$,,11!4Q7 
 r 	A##%IIK	 ''	2X//0 	##FFHdC((667**33AABrJ   c                 f   t         | j                  j                  d<   | j                         }t	               }t
        |_        |j                  |      }| j                         \  }}}| j                  |      }| j                  ||||       | j                  |j                  j                         y)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''	2##FFHdC 	((66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57:0=0 8*X5< 9DF,&7P("#8J.8`1>8B1Cf8rJ   r/  c                   (    e Zd ZdZd Zd Zd Zd Zy)ExistingConnectionTestsze
    Tests for L{SSHCommandClientEndpoint} when using the C{existingConnection}
    constructor.
    c                    t         j                  |        t        t        | j	                                     }|j                  | j                  | j                  j                  d          |j                  t        | j                  j                        | j                  j                  d          t        j                  | j                  d| j                  | j                  | j                   | j"                  |t%        d            | _        y)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#HT[[]$;<
dmmT\\-D-DZ-PQ$,,112DLL4K4KJ4W	
 1>>LLIIMMII]]!u%	
rJ   c                 ,   t               }t        |_        | j                  j	                  |      }| j
                  j                  j                         }	 t        | j
                  j                  d<   | j                  | j                  | j                  j                  d   d         \  }}}| j
                  j                  j                          | j
                  j                  j                  |       || _        || _        || _        | j%                  |      }|j&                  j(                  }t+        j,                  |d      S # | j
                  j                  j                          | j
                  j                  j                  |       w xY w)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;  s9   
 )#MM))'2	 

00557		;3EDJJ$$Z0#'#@#@dll55a8;$ FFD
 JJ$$**,JJ$$++M:
''	2'',,
':::}UU JJ$$**,JJ$$++M:s   AE AFc                    | j                   j                          | j                   j                          | 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  sU     	







||T\\4::55rJ   c                     | j                  |j                  j                         | j                  |j                  j                         y)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  s8     	))778))112rJ   N)rK   rL   rM   rN   r   r   r   r   rO   rJ   rH   r  r    s    

0V<63rJ   r  c                   (    e Zd ZdZd Zd Zd Zd Zy)ExistingConnectionHelperTestsz1
    Tests for L{_ExistingConnectionHelper}.
    c                 J    | j                  t        t        t                     y)zT
        L{_ExistingConnectionHelper} implements L{_ISSHConnectionCreator}.
        N)rD  r   r1   r0   rF   s    rH   rJ  z,ExistingConnectionHelperTests.test_interfacew  s     	$:<UVWrJ   c                     t               }t        |      }| j                  || j                  |j	                                      y)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}  s7     *62fd2263J3J3LMNrJ   c                 ^    t        t                     }|j                  t               d       y)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  s$    
 +684 	  51rJ   c                 ^    t        t                     }|j                  t               d       y)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  s$    
 +684 	  40rJ   N)rK   rL   rM   rN   rJ  r  r  r  rO   rJ   rH   r  r  r  s    XO	2	1rJ   r  c                       e Zd ZdedefdZy)_WriteDiscarderrT   returnc                     y)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    
 rJ   N)rK   rL   rM   r  intr   rO   rJ   rH   r  r    s    &  rJ   r  c                   6    e Zd ZdZdeddfdZdedee   fdZy)_PTYPathzk
    A L{FilePath}-like object which can be opened to create a L{_ReadFile} with
    certain contents.
    contentsr  Nc                     || _         y)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    s1    
! !4 !. .E .rJ   r  c                   N    e Zd ZdZd Zd ZddZd Zd Zd Z	d	 Z
d
 Zd Zd Zy)NewConnectionHelperTestsz,
    Tests for L{_NewConnectionHelper}.
    c                 J    | j                  t        t        t                     y)zO
        L{_NewConnectionHelper} implements L{_ISSHConnectionCreator}.
        N)rD  r   r1   r2   rF   s    rH   rJ  z'NewConnectionHelperTests.test_interface  s     	$:<PQRrJ   c                 D    | j                  dt        j                         y)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/PQrJ   Nc                     t               | j                  t        dfd       t        dddddddddd
      }| j                  |j                         y)z
        L{_NewConnectionHelper._knownHosts} is used to create a
        L{KnownHostsFile} if one is not passed to the initializer.
        _knownHostsc                     S rc   rO   )clsignoredrv   s     rH   rh   zANewConnectionHelperTests.test_defaultKnownHosts.<locals>.<lambda>  s    V rJ   N)r  patchr2   r  r4  )rG   r  rv   s     @rH   test_defaultKnownHostsz/NewConnectionHelperTests.test_defaultKnownHosts  s\    
 

'8ST%
 	ff//0rJ   c                    t               j                  d   }t        | j                               }t	        |      }|j                  d|       |j                          t        d|j                         t        j                  j                  d      }|j                  j                  |d      }| j                  t        d|       t        d|       t        j                         }| j                  |j!                  d|             y)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*499-89 ww!!$'))##D$/

'A(45%113)),<=rJ   c                 l    t        dddddddddd
      }| j                  |j                  t               y)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  s9    
 &$dD$dD$
 	fii3rJ   c                     t        d      }t        dddddddddd|      }| j                  |j                  j	                  d            }| j                  |       y)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  s]     v%$dD$dD$
 %%fii&6&67I&JKrJ   c                     t        | j                               }t        dddddddddd|      }| j                  |j                  j                  d            }| j                  |       y)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  se     t{{}%%$dD$dD$
 %%fii&6&67I&JK rJ   c                 v    t        dddddddddd
      }| j                  t        d      |j                         y)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"  s>    
 &$dD$dD$
 	+.

;rJ   c                     t        dddddddddd
      }t               }t               |_        |j	                  |d       | j                  |j                  j                         y)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,  s_    
 &$dD$dD$
 #_
.0
  U3
,,::;rJ   c                      G d d      }t        dddddddddd
      }t               }t               |_         |       |j                  _        |j	                  |d       | j                  |j                  j                  j                         y)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y)MNewConnectionHelperTests.test_cleanupConnectionImmediately.<locals>.AbortableFc                     d| _         y)z7
                Abort the connection.
                TNrD   rF   s    rH   rI   z]NewConnectionHelperTests.test_cleanupConnectionImmediately.<locals>.Abortable.abortConnectionB  s      $rJ   N)rK   rL   rM   rE   rI   rO   rJ   rH   	Abortabler  ?  s    G$rJ   r  NT)r2   r6   r9   r   r  rD  rE   )rG   r  r  r  s       rH   r  z:NewConnectionHelperTests.test_cleanupConnectionImmediately9  s    	$ 	$ &$dD$dD$
 #_
13
)2
&  T2
,,66>>?rJ   )r  N)rK   rL   rM   rN   rJ  r  r  r  r  r  r  r  r  r  rO   rJ   rH   r  r    s<    SR1.>.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     s       & ;  K K / I & 0 J J  H 7 
 8 / * , " 0 + .LI  )64:4*><  (DJM
CJNN I 5]  
 $ $Z &2 24 4 .!2 .(!Z !* X	 	 	 "##% #% $#%LkB kB\]	8#E ]	8@R3h(J R3j)1H )1Xg . .0P@x P@rJ   