o
    9Bid                  
   @  s  d dl mZ d dlZd dlZd dl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mZ d d	lmZ d d
lmZm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#m$Z$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/m0Z0m1Z1 z
d dlm2Z2m3Z3 W n	 e4y   Y nw e+dZ5e5rd dl6m7Z7 d dl8m9Z9m:Z:m;Z; nd dl<m9Z9 G dd dZ7zd dl=m:Z> W n e4y Z? zdZ@eAe?ZB[?W Y dZ?[?ndZ?[?ww e>Z@G dd dZCG dd  d e1ZDG d!d" d"ejEZFG d#d$ d$ejGZHG d%d& d&ejIZJG d'd( d(ejIZKG d)d* d*ejEZLdHdId2d3ZMG d4d5 d5ZNG d6d7 d7eNZOee9G d8d9 d9e7ZPG d:d; d;ZQG d<d= d=eNZRG d>d? d?ZSG d@dA dAeNeSe1ZTG dBdC dCeOeSe1ZUG dDdE dEeReSe1ZVG dFdG dGeOe1ZWdS )J    )annotationsN)count)Any)implementer)
ConchError)privateRSA_opensshpublicRSA_opensshConchTestRealm)portal)protocolreactor)DeferredgatherResultsmaybeDeferred)ProcessExitedAlready)IReactorProcess)LoopingCall)filepathlogruntime)FilePath)which)requireModule)HAS_IPV6skipWithoutIPv6)SkipTestTestCase)ConchTestServerFactoryconchTestPublicKeyCheckercryptography)	ConchUser)ISession
SSHSessionwrapProtocol)r"   c                   @  s   e Zd ZdS )r!   N)__name__
__module____qualname__ r(   r(   ]/var/www/html/Trade-python/venv/lib/python3.10/site-packages/twisted/conch/test/test_conch.pyr!   /   s    r!   )r#   c                   @  s   e Zd ZdZdZdd ZdS )	FakeStdioz
    A fake for testing L{twisted.conch.scripts.conch.SSHSession.eofReceived} and
    L{twisted.conch.scripts.cftp.SSHSession.eofReceived}.

    @ivar writeConnLost: A flag which records whether L{loserWriteConnection}
        has been called.
    Fc                 C  s
   d| _ dS )z9
        Record the call to loseWriteConnection.
        TN)writeConnLostselfr(   r(   r)   loseWriteConnectionH   s   
zFakeStdio.loseWriteConnectionN)r%   r&   r'   __doc__r+   r.   r(   r(   r(   r)   r*   =   s    r*   c                   @  s$   e Zd ZdZedu reZdd ZdS )StdioInteractingSessionTestsz>
    Tests for L{twisted.conch.scripts.conch.SSHSession}.
    Nc                 C  s*   t  }t }||_|  | |j dS )z}
        L{twisted.conch.scripts.conch.SSHSession.eofReceived} loses the
        write half of its stdio connection.
        N)r*   StdioInteractingSessionstdioeofReceived
assertTruer+   )r-   r2   channelr(   r(   r)   test_eofReceivedW   s
   z-StdioInteractingSessionTests.test_eofReceived)r%   r&   r'   r/   r1   _reasonskipr6   r(   r(   r(   r)   r0   O   s
    r0   c                   @  s$   e Zd Zdd Zdd Zdd ZdS )Echoc                 C     t d d S )NzECHO CONNECTION MADEr   msgr,   r(   r(   r)   connectionMaded      zEcho.connectionMadec                 C  r:   )NzECHO CONNECTION DONEr;   r-   reasonr(   r(   r)   connectionLostg   r>   zEcho.connectionLostc                 C  s&   | j | d|v r| j   d S d S )N   
)	transportwriteloseConnectionr-   datar(   r(   r)   dataReceivedj   s   zEcho.dataReceivedN)r%   r&   r'   r=   rA   rH   r(   r(   r(   r)   r9   c   s    r9   c                   @  s   e Zd ZeZdS )EchoFactoryN)r%   r&   r'   r9   r   r(   r(   r(   r)   rI   p       rI   c                   @  sR   e Zd ZU dZdZded< dZdZdZded< d	d
 Z	dd Z
dd Zdd ZdS )ConchTestOpenSSHProcessa  
    Test protocol for launching an OpenSSH client process.

    @ivar deferred: Set by whatever uses this object. Accessed using
    L{_getDeferred}, which destroys the value so the Deferred is not
    fired twice. Fires when the process is terminated.

    @ivar expectedExitCode: If the process exit code is not C{expectedExitCode}
    the set C{deferred} will by triggerd with a failure.
    NzDeferred[None] | Nonedeferred    r   intexpectedExitCodec                 C     | j d }| _ |S NrL   r-   dr(   r(   r)   _getDeferred      z$ConchTestOpenSSHProcess._getDeferredc                 C     |  j |7  _ d S rQ   )bufrF   r(   r(   r)   outReceived      z#ConchTestOpenSSHProcess.outReceivedc                 C  rW   rQ   )problemsrF   r(   r(   r)   errReceived   rZ   z#ConchTestOpenSSHProcess.errReceivedc              
   C  s\   |j j| jkr|  td| j|j j| jd dS | j	
dd}|  | dS )z~
        Called when the process has ended.

        @param reason: a Failure giving the reason for the process' end.
        zexit code was not {}: {} ({})charmaps   
rB   N)valueexitCoderO   rU   errbackr   formatr[   decoderX   replacecallback)r-   r@   rX   r(   r(   r)   processEnded   s   

z$ConchTestOpenSSHProcess.processEnded)r%   r&   r'   r/   rL   __annotations__rX   r[   rO   rU   rY   r\   re   r(   r(   r(   r)   rK   t   s   
 rK   c                   @  sT   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd ZdS )ConchTestForwardingProcessa  
    Manages a third-party process which launches a server.

    Uses L{ConchTestForwardingPort} to connect to the third-party server.
    Once L{ConchTestForwardingPort} has disconnected, kill the process and fire
    a Deferred with the data received by the L{ConchTestForwardingPort}.

    @ivar deferred: Set by whatever uses this object. Accessed using
    L{_getDeferred}, which destroys the value so the Deferred is not
    fired twice. Fires when the process is terminated.
    Nc                 C  s   || _ d| _|| _dS )aF  
        @type port: L{int}
        @param port: The port on which the third-party server is listening.
        (it is assumed that the server is running on localhost).

        @type data: L{str}
        @param data: This is sent to the third-party server. Must end with '
'
        in order to trigger a disconnect.
        N)portbufferrG   )r-   rh   rG   r(   r(   r)   __init__   s   

z#ConchTestForwardingProcess.__init__c                 C  rP   rQ   rR   rS   r(   r(   r)   rU      rV   z'ConchTestForwardingProcess._getDeferredc                 C  s   |    d S rQ   )_connectr,   r(   r(   r)   r=      s   z)ConchTestForwardingProcess.connectionMadec                 C  s0   t tt| | j}|d| j}|| j |S )a  
        Connect to the server, which is often a third-party process.
        Tries to reconnect if it fails because we have no way of determining
        exactly when the port becomes available for listening -- we can only
        know when the process starts.
        	127.0.0.1)	r   ClientCreatorr   ConchTestForwardingPortrG   
connectTCPrh   
addErrback
_ebConnect)r-   ccrT   r(   r(   r)   rk      s   z#ConchTestForwardingProcess._connectc                 C  s   t d| j d S )Ng?)r   	callLaterrk   )r-   fr(   r(   r)   rq      rZ   z%ConchTestForwardingProcess._ebConnectc                 C  s.   || _ | jd | j  td| j dS )z
        The network connection has died; save the buffer of output
        from the network and attempt to quit the process gracefully,
        and then (after the reactor has spun) send it a KILL signal.
           r   N)ri   rC   rD   rE   r   rs   
_reallyDie)r-   ri   r(   r(   r)   forwardingPortDisconnected   s   
z5ConchTestForwardingProcess.forwardingPortDisconnectedc                 C  s(   z	| j d W d S  ty   Y d S w )NKILL)rC   signalProcessr   r,   r(   r(   r)   rv      s
   z%ConchTestForwardingProcess._reallyDiec                 C  s   |   | j dS )z
        Fire the Deferred at self.deferred with the data collected
        from the L{ConchTestForwardingPort} connection, if any.
        N)rU   rd   ri   r?   r(   r(   r)   re      s   z'ConchTestForwardingProcess.processEnded)r%   r&   r'   r/   rL   rj   rU   r=   rk   rq   rw   rv   re   r(   r(   r(   r)   rg      s    rg   c                   @  s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )rn   z
    Connects to server launched by a third-party process (managed by
    L{ConchTestForwardingProcess}) sends data, then reports whatever it
    received back to the L{ConchTestForwardingProcess} once the connection
    is ended.
    c                 C  s   || _ || _dS )z
        @type protocol: L{ConchTestForwardingProcess}
        @param protocol: The L{ProcessProtocol} which made this connection.

        @type data: str
        @param data: The data to be sent to the third-party server.
        N)r   rG   )r-   r   rG   r(   r(   r)   rj      s   
z ConchTestForwardingPort.__init__c                 C  s   d| _ | j| j d S )NrM   )ri   rC   rD   rG   r,   r(   r(   r)   r=     s   z&ConchTestForwardingPort.connectionMadec                 C  rW   rQ   )ri   rF   r(   r(   r)   rH     rZ   z$ConchTestForwardingPort.dataReceivedc                 C  s   | j | j d S rQ   )r   rw   ri   r?   r(   r(   r)   rA   
  rZ   z&ConchTestForwardingPort.connectionLostN)r%   r&   r'   r/   rj   r=   rH   rA   r(   r(   r(   r)   rn      s    rn   conchargs	list[str]modstrreturnlist[bytes]c                 C  s$   t jd| g}dd g || D S )Na$  -c
### Twisted Preamble
import sys, os
path = os.path.abspath(sys.argv[0])
while os.path.dirname(path) != path:
    if os.path.basename(path).startswith('Twisted'):
        sys.path.insert(0, path)
        break
    path = os.path.dirname(path)

from twisted.conch.scripts.%s import run
run()c                 S  s   g | ]}| d qS )utf-8)encode).0eachr(   r(   r)   
<listcomp>       z_makeArgs.<locals>.<listcomp>)sys
executable)r{   r}   startr(   r(   r)   	_makeArgs  s   r   c                   @  sJ   e Zd ZesdZedd Zdd Zdd Zdd	 Z	d
d Z
dddZdS )ConchServerSetupMixinzcan't run without cryptographyc                   C  s   t dS )Ns   testuserr	   r(   r(   r(   r)   realmFactory'  s   z"ConchServerSetupMixin.realmFactoryc                 C  s   dD ]}t j|rt | qtdd}|t W d    n1 s%w   Y  tdd}|t W d    n1 s?w   Y  t dd t	d
 }|jjsX|jjr\tdtdd}|dt  W d    d S 1 stw   Y  d S )	N)rsa_testrsa_test.pubkh_testr   wbr   i  zgprivate key readable by others despite chmod; possible windows permission issue? see https://tm.tl/9767r   s
   127.0.0.1 )ospathexistsremoveopenrD   r   r   chmodr   getPermissionsgroupreadotherr   )r-   rt   permissionsr(   r(   r)   _createFiles+  s&   
"z"ConchServerSetupMixin._createFilesc                 C  s*   t   }|d | d }|  |S )N) r      )socketbindgetsocknameclose)r-   srh   r(   r(   r)   _getFreePort>  s
   
z"ConchServerSetupMixin._getFreePortc                 C  s.   |   }t|}|t  t }||_|S )z
        Make a L{ConchTestServerFactory}, which allows us to start a
        L{ConchTestServer} -- i.e. an actually listening conch.
        )r   r   PortalregisterCheckerr   r   )r-   realmpfactoryr(   r(   r)   _makeConchFactoryE  s   
z'ConchServerSetupMixin._makeConchFactoryc                 C  sz   |    |  | _d| j_tjd| jdd| _tdt | _| j	 j
| _tr;tjdt dd| _| j	 j
| _d S d S )Nr   r   rl   )	interfacez::1)r   r   conchFactoryexpectedLoseConnectionr   	listenTCPconchServerrI   
echoServergetHostrh   echoPortr   echoServerV6
echoPortV6r,   r(   r(   r)   setUpQ  s   
zConchServerSetupMixin.setUpr   r   c                 C  sf   zd| j j_W n	 ty   Y nw | j jj  t| jjt| j	jg}t
r/|t| jj t|S )Nr   )r   protodoneAttributeErrorrC   rE   r   r   stopListeningr   r   appendr   r   )r-   	deferredsr(   r(   r)   tearDown^  s   

zConchServerSetupMixin.tearDownN)r   r   )r%   r&   r'   r    r8   staticmethodr   r   r   r   r   r   r(   r(   r(   r)   r   #  s    
r   c                   @  s(   e Zd ZdZdd Zdd Zdd ZdS )	ForwardingMixina  
    Template class for tests of the Conch server's ability to forward arbitrary
    protocols over SSH.

    These tests are integration tests, not unit tests. They launch a Conch
    server, a custom TCP server (just an L{EchoProtocol}) and then call
    L{execute}.

    L{execute} is implemented by subclasses of L{ForwardingMixin}. It should
    cause an SSH client to connect to the Conch server, asking it to forward
    data to the custom TCP server.
    c                 C  s   |  dt }|| jdS )z
        Test that we can use whatever client to send the command "echo goodbye"
        to the Conch server. Make sure we receive "goodbye" back from the
        server.
        echo goodbye   goodbye
executerK   addCallbackassertEqualrS   r(   r(   r)   	test_exec}  s   zForwardingMixin.test_execc                 C  >   |   }t|d}| jd|d|| jf d}|| jd |S )zy
        Test that we can use whatever client to forward a local port to a
        specified port on the server.
           test
r   z-N -L%i:127.0.0.1:%isshArgsr   rg   r   r   r   r   r-   	localPortprocessrT   r(   r(   r)   test_localToRemoteForwarding     
z,ForwardingMixin.test_localToRemoteForwardingc                 C  r   )zs
        Test that we can use whatever client to forward a port from the server
        to a port locally.
        r   r   z-N -R %i:127.0.0.1:%ir   r   r   r(   r(   r)   test_remoteToLocalForwarding  r   z,ForwardingMixin.test_remoteToLocalForwardingN)r%   r&   r'   r/   r   r   r   r(   r(   r(   r)   r   o  s
    	r   c                   @  sH   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dS )RekeyAvatara#  
    This avatar implements a shell which sends 60 numbered lines to whatever
    connects to it, then closes the session with a 0 exit status.

    60 lines is selected as being enough to send more than 2kB of traffic, the
    amount the client is configured to initiate a rekey after.
    c                 C  s   t |  t| jd< d S )Ns   session)r!   rj   r#   channelLookupr,   r(   r(   r)   rj     s   
zRekeyAvatar.__init__c                   sH   t  }| t|  fdd}t|t   d dS )zE
        Write 60 lines of data to the transport, then exit.
        c                   sZ   t | }|dkr   jjjdd   d S d|f }|d}| d S )N<   s   exit-statuss       zline #%02d
r   )nextstopsessionconnsendRequestrE   r   rD   )counterilinecallrC   r(   r)   rD     s   

z$RekeyAvatar.openShell.<locals>.writeg{Gz?N)r   ProtocolmakeConnectionr$   r   r   r   )r-   rC   r   rD   r(   r   r)   	openShell  s   
zRekeyAvatar.openShellc                 C  s   dS )z2
        Ignore the close of the session.
        Nr(   r,   r(   r(   r)   closed      zRekeyAvatar.closedc                 C     d S rQ   r(   r,   r(   r(   r)   r3        zRekeyAvatar.eofReceivedc                 C  r   rQ   r(   )r-   r   commandr(   r(   r)   execCommand  r   zRekeyAvatar.execCommandc                 C  r   rQ   r(   )r-   term
windowSizemodesr(   r(   r)   getPty  r   zRekeyAvatar.getPtyc                 C  r   rQ   r(   )r-   newWindowSizer(   r(   r)   windowChanged  r   zRekeyAvatar.windowChangedN)r%   r&   r'   r/   rj   r   r   r3   r   r   r   r(   r(   r(   r)   r     s    !r   c                   @  s   e Zd ZdZdd ZdS )
RekeyRealmzS
    This realm gives out new L{RekeyAvatar} instances for any avatar request.
    c                 G  s   |d t  dd fS )Nr   c                   S  r   rQ   r(   r(   r(   r(   r)   <lambda>  r   z*RekeyRealm.requestAvatar.<locals>.<lambda>)r   )r-   avatarIDmind
interfacesr(   r(   r)   requestAvatar  s   zRekeyRealm.requestAvatarN)r%   r&   r'   r/   r   r(   r(   r(   r)   r     s    r   c                   @  s   e Zd ZdZeZdd ZdS )RekeyTestsMixinzp
    TestCase mixin which defines tests exercising L{SSHTransportBase}'s handling
    of rekeying messages.
    c                   s.   t  } d|d} fdd}|| |S )z
        After a client-initiated rekey is completed, application data continues
        to be passed over the SSH connection.
        r   z-o RekeyLimit=2Kc                   s6   d dd tdD d }|d} | | d S )N
c                 S  s   g | ]}d |f qS )z
line #%02dr(   )r   r   r(   r(   r)   r     r   zFRekeyTestsMixin.test_clientRekey.<locals>.finished.<locals>.<listcomp>r   r   )joinranger   r   )resultexpectedResultr,   r(   r)   finished  s   
z2RekeyTestsMixin.test_clientRekey.<locals>.finished)rK   r   r   )r-   r   rT   r   r(   r,   r)   test_clientRekey  s
   
z RekeyTestsMixin.test_clientRekeyN)r%   r&   r'   r/   r   r   r   r(   r(   r(   r)   r     s    r   c                   @  s&   e Zd Zeds
dZ	ddddZdS )OpenSSHClientMixinsshz$no ssh command-line client availabler   remoteCommandr~   r   rK   r   r   Deferred[None]c           
      C  sp   t   }|_d| d | }| j j}||  }g }|D ]
}	||	d qtt	
|tdd | |S )a  
        Connects to the SSH server started in L{ConchServerSetupMixin.setUp} by
        running the 'ssh' command line tool.

        @type remoteCommand: str
        @param remoteCommand: The command (with arguments) to run on the
        remote end.

        @type process: L{ConchTestOpenSSHProcess}

        @type sshArgs: str
        @param sshArgs: Arguments to pass to the 'ssh' process.

        @return: L{defer.Deferred}
        zssh -2 -l testuser -p %i -F /dev/null -oIdentitiesOnly=yes -oUserKnownHostsFile=kh_test -oPasswordAuthentication=no -oHostKeyAlgorithms=ssh-rsa -a -i rsa_test z 127.0.0.1 r   r   r   )r   rL   r   r   rh   splitr   r   r   r   spawnProcessr   )
r-   r   r   r   r   cmdlinerh   cmdsencodedCmdscmdr(   r(   r)   r     s    
zOpenSSHClientMixin.executeN)r   )r   r~   r   rK   r   r~   r   r   )r%   r&   r'   r   r8   r   r(   r(   r(   r)   r     s
    r   c                   @  sP   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )OpenSSHKeyExchangeTestsz\
    Tests L{SSHTransportBase}'s key exchange algorithm compatibility with
    OpenSSH.
    c                 C  s   g }zt jtdd ddgt jd}t|ts|d}| }W n	 ty*   Y nw ||vr6t	| d| 
dt d	| }|| jd
S )aI  
        Call execute() method of L{OpenSSHClientMixin} with an ssh option that
        forces the exclusive use of the key exchange algorithm specified by
        keyExchangeAlgo

        @type keyExchangeAlgo: L{str}
        @param keyExchangeAlgo: The key exchange algorithm to use

        @return: L{defer.Deferred}
        r   r   z-Qkex)stderrr   z not supported by ssh clientz
echo helloz-oKexAlgorithms=s   hello
)
subprocesscheck_outputr   STDOUT
isinstancer~   rb   r   BaseExceptionr   r   rK   r   r   )r-   keyExchangeAlgokexAlgorithmsoutputrT   r(   r(   r)   assertExecuteWithKexAlgorithmF  s&   

z5OpenSSHKeyExchangeTests.assertExecuteWithKexAlgorithmc                 C  
   |  dS )zb
        The ecdh-sha2-nistp256 key exchange algorithm is compatible with
        OpenSSH
        zecdh-sha2-nistp256r  r,   r(   r(   r)   test_ECDHSHA256f     
z'OpenSSHKeyExchangeTests.test_ECDHSHA256c                 C  r  )zb
        The ecdh-sha2-nistp384 key exchange algorithm is compatible with
        OpenSSH
        zecdh-sha2-nistp384r  r,   r(   r(   r)   test_ECDHSHA384m  r  z'OpenSSHKeyExchangeTests.test_ECDHSHA384c                 C  r  )zb
        The ecdh-sha2-nistp521 key exchange algorithm is compatible with
        OpenSSH
        zecdh-sha2-nistp521r  r,   r(   r(   r)   test_ECDHSHA521t  r  z'OpenSSHKeyExchangeTests.test_ECDHSHA521c                 C  r  )zl
        The diffie-hellman-group14-sha1 key exchange algorithm is compatible
        with OpenSSH.
        zdiffie-hellman-group14-sha1r  r,   r(   r(   r)   test_DH_GROUP14{  r  z'OpenSSHKeyExchangeTests.test_DH_GROUP14c                 C  r  )zs
        The diffie-hellman-group-exchange-sha1 key exchange algorithm is
        compatible with OpenSSH.
        z"diffie-hellman-group-exchange-sha1r  r,   r(   r(   r)   test_DH_GROUP_EXCHANGE_SHA1  r  z3OpenSSHKeyExchangeTests.test_DH_GROUP_EXCHANGE_SHA1c                 C  r  )zu
        The diffie-hellman-group-exchange-sha256 key exchange algorithm is
        compatible with OpenSSH.
        z$diffie-hellman-group-exchange-sha256r  r,   r(   r(   r)   test_DH_GROUP_EXCHANGE_SHA256  s   z5OpenSSHKeyExchangeTests.test_DH_GROUP_EXCHANGE_SHA256c                 C  s   |  t| jd dS )zy
        The list of key exchange algorithms supported
        by OpenSSH client is obtained with C{ssh -Q kex}.
        zunsupported-algorithmN)assertRaisesr   r  r,   r(   r(   r)   test_unsupported_algorithm  s   z2OpenSSHKeyExchangeTests.test_unsupported_algorithmN)r%   r&   r'   r/   r  r  r  r  r  r  r  r  r(   r(   r(   r)   r  @  s     	r  c                   @  s   e Zd ZdZedd ZdS )OpenSSHClientForwardingTestszR
    Connection forwarding tests run against the OpenSSL command line client.
    c                 C  r   )zG
        Forwarding of arbitrary IPv6 TCP connections via SSH.
        r   r   z-N -L%i:[::1]:%ir   )r   rg   r   r   r   r   r   r(   r(   r)   test_localToRemoteForwardingV6  r   z;OpenSSHClientForwardingTests.test_localToRemoteForwardingV6N)r%   r&   r'   r/   r   r  r(   r(   r(   r)   r    s    r  c                   @  s   e Zd ZdZdS )OpenSSHClientRekeyTestszE
    Rekeying tests run against the OpenSSL command line client.
    N)r%   r&   r'   r/   r(   r(   r(   r)   r    rJ   r  c                   @  s^   e Zd ZdZejdkrdZ			ddddZdd Zdd Z	dddZ
dddZdddZdS ) CmdLineClientTestszP
    Connection forwarding tests run against the Conch command line client.
    win32z!can't run cmdline client on win32r   Nrl   r   r~   r   rK   r   	conchArgslist[str] | None
remoteHostr   r   c           
      C  s   |du rg }t  |_| j j}d|| d| d | }t| }tj	
 }	tjtj|	d< ttj|tjt|| |	d |jS )z{
        As for L{OpenSSHClientTestCase.execute}, except it runs the 'conch'
        command line tool, not 'ssh'.
        Nz[-p {} -l testuser --known-hosts kh_test --user-authentications publickey -a -i rsa_test -v  
PYTHONPATH)env)r   rL   r   r   rh   ra   listr   r   environcopypathsepr   r   r   r   r   r   r   r   )
r-   r   r   r   r!  r#  rh   cmdtemplater   r&  r(   r(   r)   r     s&   

zCmdLineClientTests.executec                   sV    fdd}t   jdt dd jddgd}|jd	 || |S )
z4
        It can store logs to a local file.
        c                   s      }d| d S )Ns   Log opened.)
getContentassertIn)r   
logContentlogPathr-   r(   r)   cb_check_log  s   z<CmdLineClientTests.test_runWithLogFile.<locals>.cb_check_logr   z--logz	--logfilez--host-key-algorithmszssh-rsar   r   r!  r   )r   r   mktempr   rK   r   r   r   )r-   r1  rT   r(   r/  r)   test_runWithLogFile  s   
z&CmdLineClientTests.test_runWithLogFilec                 C  s"   | j dt d}|| jd |S )zH
        Do not use --host-key-algorithms flag on command line.
        r   )r   r   r   r   rS   r(   r(   r)   %test_runWithNoHostAlgorithmsSpecified  s
   z8CmdLineClientTests.test_runWithNoHostAlgorithmsSpecifiedc                 C  s   | j dt dgd| jdS )zS
        Simple smoke test for '--compress' flag to ensure we can connect.
        zecho compressedz
--compressr2  s   compressed
r   r,   r(   r(   r)    test_runWithCompressionSpecified  s   
z3CmdLineClientTests.test_runWithCompressionSpecifiedc                 C  s   t  }d|_| jd|ddS )z<
        Connecting to an invalid host should fail.
        r   zecho nonfunctionalnowhere.invalidr   r   r#  )rK   rO   r   r-   expectErrorr(   r(   r)   test_connectToInvalidHost  s   z,CmdLineClientTests.test_connectToInvalidHostc                 C  s   t  }| | jd|ddtS )zm
        L{ConchTestOpenSSHProcess} fails with a L{ConchError} if an expectation
        is not met.
        zecho checkfailurer7  r8  )rK   assertFailurer   r   r9  r(   r(   r)   test_testFailure  s   z#CmdLineClientTests.test_testFailure)r   Nrl   )r   r~   r   rK   r   r~   r!  r"  r#  r~   r   r   )r   r   )r%   r&   r'   r/   r   platformTyper8   r   r4  r5  r6  r;  r=  r(   r(   r(   r)   r    s    
!


r  )rz   )r{   r|   r}   r~   r   r   )X
__future__r   r   r   r  r   	itertoolsr   typingr   zope.interfacer   twisted.conch.errorr   twisted.conch.test.keydatar   r   twisted.conch.test.test_sshr
   twisted.credr   twisted.internetr   r   twisted.internet.deferr   r   r   twisted.internet.errorr   twisted.internet.interfacesr   twisted.internet.taskr   twisted.pythonr   r   r   twisted.python.filepathr   twisted.python.procutilsr   twisted.python.reflectr   twisted.test.testutilsr   r   twisted.trial.unittestr   r   r   r   ImportErrorr    twisted.conch.avatarr!   twisted.conch.ssh.sessionr"   r#   r$   twisted.conch.interfacestwisted.conch.scripts.conch_StdioInteractingSessioner1   r~   r7   r*   r0   r   r9   FactoryrI   ProcessProtocolrK   rg   rn   r   r   r   r   r   r   r   r  r  r  r  r(   r(   r(   r)   <module>   s|   0LL6D	4\