
    Vhd                       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 	 d dlm2Z2m3Z3  e+d      Z5e5rd dl6m7Z7 d dl8m9Z9m:Z:m;Z; nd dl<m9Z9  G d d      Z7	 d dl=m:Z> e>Z? G d d      ZC G d d e1      ZD G d! d"ej                        ZF G d# d$ej                        ZH G d% d&ej                        ZJ G d' d(ej                        ZK G d) d*ej                        ZLd@dAd+ZM G d, d-      ZN G d. d/eN      ZO ee9       G d0 d1e7             ZP G d2 d3      ZQ G d4 d5eN      ZR G d6 d7      ZS G d8 d9eNeSe1      ZT G d: d;eOeSe1      ZU G d< d=eReSe1      ZV G d> d?eOe1      ZWy# e4$ r Y =w xY w# e4$ rZ@dZ? eAe@      ZB[@Y dZ@[@#dZ@[@ww xY w)B    )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                      e Zd Zy)r"   N)__name__
__module____qualname__     M/home/dcms/DCMS/lib/python3.12/site-packages/twisted/conch/test/test_conch.pyr"   r"   /   s    r+   r"   )r$   c                      e Zd ZdZdZd Zy)	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                    d| _         y)z9
        Record the call to loseWriteConnection.
        TN)writeConnLostselfs    r,   loseWriteConnectionzFakeStdio.loseWriteConnectionH   s     "r+   N)r'   r(   r)   __doc__r0   r3   r*   r+   r,   r.   r.   =   s     M"r+   r.   c                      e Zd ZdZeeZd Zy)StdioInteractingSessionTestsz>
    Tests for L{twisted.conch.scripts.conch.SSHSession}.
    Nc                    t               }t               }||_        |j                          | j	                  |j
                         y)z}
        L{twisted.conch.scripts.conch.SSHSession.eofReceived} loses the
        write half of its stdio connection.
        N)r.   StdioInteractingSessionstdioeofReceived
assertTruer0   )r2   r9   channels      r,   test_eofReceivedz-StdioInteractingSessionTests.test_eofReceivedW   s8    
 )+++,r+   )r'   r(   r)   r4   r8   _reasonskipr=   r*   r+   r,   r6   r6   O   s     &	-r+   r6   c                      e Zd Zd Zd Zd Zy)Echoc                .    t        j                  d       y )NzECHO CONNECTION MADEr   msgr1   s    r,   connectionMadezEcho.connectionMaded       &'r+   c                .    t        j                  d       y )NzECHO CONNECTION DONErC   r2   reasons     r,   connectionLostzEcho.connectionLostg   rF   r+   c                x    | j                   j                  |       d|v r| j                   j                          y y )N   
)	transportwriteloseConnectionr2   datas     r,   dataReceivedzEcho.dataReceivedj   s0    T"D=NN))+ r+   N)r'   r(   r)   rE   rJ   rR   r*   r+   r,   rA   rA   c   s    ((,r+   rA   c                      e Zd ZeZy)EchoFactoryN)r'   r(   r)   rA   r   r*   r+   r,   rT   rT   p   s    Hr+   rT   c                  N    e Zd ZU dZdZded<   dZdZdZded<   d	 Z	d
 Z
d Zd Zy)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deferredr+   r   intexpectedExitCodec                .    | j                   d c}| _         |S NrW   r2   ds     r,   _getDeferredz$ConchTestOpenSSHProcess._getDeferred       ==$4=r+   c                .    | xj                   |z  c_         y r[   )bufrP   s     r,   outReceivedz#ConchTestOpenSSHProcess.outReceived   s    Dr+   c                .    | xj                   |z  c_         y r[   )problemsrP   s     r,   errReceivedz#ConchTestOpenSSHProcess.errReceived   s    r+   c                   |j                   j                  | j                  k7  rq| j                         j	                  t        dj                  | j                  |j                   j                  | j                  j                  d                         y| j                  j                  dd      }| j                         j                  |       y)z~
        Called when the process has ended.

        @param reason: a Failure giving the reason for the process' end.
        zexit code was not {}: {} ({})charmaps   
rL   N)valueexitCoderY   r_   errbackr   formatre   decoderb   replacecallback)r2   rI   rb   s      r,   processEndedz$ConchTestOpenSSHProcess.processEnded   s     <<  D$9$99''3::----,,Y7 ((""7E2C((-r+   )r'   r(   r)   r4   rW   __annotations__rb   re   rY   r_   rc   rf   rp   r*   r+   r,   rV   rV   t   s=    	 '+H#*
CHc.r+   rV   c                  D    e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)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                .    || _         d| _        || _        y)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bufferrQ   )r2   ru   rQ   s      r,   __init__z#ConchTestForwardingProcess.__init__   s     		r+   c                .    | j                   d c}| _         |S r[   r\   r]   s     r,   r_   z'ConchTestForwardingProcess._getDeferred   r`   r+   c                $    | j                          y r[   )_connectr1   s    r,   rE   z)ConchTestForwardingProcess.connectionMade   s    r+   c                    t        j                  t        t        | | j                        }|j                  d| j                        }|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   ConchTestForwardingPortrQ   
connectTCPru   
addErrback
_ebConnect)r2   ccr^   s      r,   rz   z#ConchTestForwardingProcess._connect   sH     ##G-DdDIIVMM+tyy1	T__%r+   c                D    t        j                  d| j                         y )Ng?)r   	callLaterrz   )r2   fs     r,   r   z%ConchTestForwardingProcess._ebConnect   s    #t}}-r+   c                    || _         | j                  j                  d       | j                  j                          t	        j
                  d| j                         y)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)rv   rM   rN   rO   r   r   
_reallyDie)r2   rv   s     r,   forwardingPortDisconnectedz5ConchTestForwardingProcess.forwardingPortDisconnected   sA     W%%%'!T__-r+   c                Z    	 | j                   j                  d       y # t        $ r Y y w xY w)NKILL)rM   signalProcessr   r1   s    r,   r   z%ConchTestForwardingProcess._reallyDie   s+    	NN((0# 		s    	**c                V    | j                         j                  | j                         y)z
        Fire the Deferred at self.deferred with the data collected
        from the L{ConchTestForwardingPort} connection, if any.
        N)r_   ro   rv   rH   s     r,   rp   z'ConchTestForwardingProcess.processEnded   s    
 	$$T[[1r+   )r'   r(   r)   r4   rW   rw   r_   rE   rz   r   r   r   rp   r*   r+   r,   rs   rs      s6    
 H
.	.2r+   rs   c                  (    e Zd ZdZd Zd Zd Zd Zy)r~   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                     || _         || _        y)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   rQ   )r2   r   rQ   s      r,   rw   z ConchTestForwardingPort.__init__   s     !	r+   c                \    d| _         | j                  j                  | j                         y )Nr+   )rv   rM   rN   rQ   r1   s    r,   rE   z&ConchTestForwardingPort.connectionMade  s    TYY'r+   c                .    | xj                   |z  c_         y r[   )rv   rP   s     r,   rR   z$ConchTestForwardingPort.dataReceived  s    tr+   c                N    | j                   j                  | j                         y r[   )r   r   rv   rH   s     r,   rJ   z&ConchTestForwardingPort.connectionLost
  s    00=r+   N)r'   r(   r)   r4   rw   rE   rR   rJ   r*   r+   r,   r~   r~      s    	(>r+   r~   c                |    t         j                  d|z  g}g || D cg c]  }|j                  d       c}S c c}w )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()utf-8)sys
executableencode)argsmodstarteachs       r,   	_makeArgsr     sF    		 	E" .=u_t_=TDKK ===s   9c                  D    e Zd ZesdZed        Zd Zd Zd Z	d Z
d	dZy)
ConchServerSetupMixinzcan't run without cryptographyc                     t        d      S )Ns   testuserr
   r*   r+   r,   realmFactoryz"ConchServerSetupMixin.realmFactory'  s    k**r+   c                   dD ]7  }t         j                  j                  |      s#t        j                  |       9 t	        dd      5 }|j                  t               d d d        t	        dd      5 }|j                  t               d d d        t        j                  dd       t        d      j                         }|j                  j                  s|j                  j                  rt        d      t	        dd      5 }|j                  dt        z          d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)	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openrN   r   r	   chmodr   getPermissionsgroupreadotherr   )r2   r   permissionss      r,   _createFilesz"ConchServerSetupMixin._createFiles+  s   8 	Aww~~a 		!	 *d# 	(qGG&'	(.$' 	'1GG%&	'
U#z*99;!![%6%6%;%;* 
 )T" 	7aGGM$556	7 	7	( 	(	' 	'	7 	7s$   	D%3D1D=%D.1D:=Ec                    t        j                          }|j                  d       |j                         d   }|j                          |S )N) r      )socketbindgetsocknameclose)r2   sru   s      r,   _getFreePortz"ConchServerSetupMixin._getFreePort>  s6    MMO	w}}q!		r+   c                    | j                         }t        j                  |      }|j                  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   )r2   realmpfactorys       r,   _makeConchFactoryz'ConchServerSetupMixin._makeConchFactoryE  sF    
 !!#MM% 	356(*r+   c                    | j                          | j                         | _        d| j                  _        t	        j
                  d| j                  d      | _        t	        j
                  dt                     | _        | j                  j                         j                  | _        t        rOt	        j
                  dt               d      | _        | j                  j                         j                  | _        y y )Nr   r   r|   )	interfacez::1)r   r   conchFactoryexpectedLoseConnectionr   	listenTCPconchServerrT   
echoServergetHostru   echoPortr   echoServerV6
echoPortV6r1   s    r,   setUpzConchServerSetupMixin.setUpQ  s     224340",,t  K
 "++A{}=//166 ' 1 1![]e TD"//779>>DO r+   c                   	 d| j                   j                  _        | j                   j                  j                  j	                          t        | j                  j                        t        | j                  j                        g}t        r.|j                  t        | j                  j                               t        |      S # t
        $ r Y w xY w)Nr   )r   protodonerM   rO   AttributeErrorr   r   stopListeningr   r   appendr   r   )r2   	deferredss     r,   tearDownzConchServerSetupMixin.tearDown^  s    	?+,D##( ##--<<>$**889$//778
	 ]4+<+<+J+JKLY''  		s   C 	CCN)returnr   )r'   r(   r)   r!   r?   staticmethodr   r   r   r   r   r   r*   r+   r,   r   r   #  s5    /+ +7&
?(r+   r   c                  "    e Zd ZdZd Zd Zd Zy)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                n    | j                  dt                     }|j                  | 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
executerV   addCallbackassertEqualr]   s     r,   	test_execzForwardingMixin.test_exec}  s/     LL)@)BC}}T--|<<r+   c                    | j                         }t        |d      }| j                  d|d|| j                  fz        }|j	                  | 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   rs   r   r   r   r   r2   	localPortprocessr^   s       r,   test_localToRemoteForwardingz,ForwardingMixin.test_localToRemoteForwarding  sb    
 %%'	,Y	BLL!79dmm:T!T  
 	
d&&	2r+   c                    | j                         }t        |d      }| j                  d|d|| j                  fz        }|j	                  | j
                  d       |S )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   s       r,   test_remoteToLocalForwardingz,ForwardingMixin.test_remoteToLocalForwarding  sb    
 %%'	,Y	BLL!8It}};U!U  
 	
d&&	2r+   N)r'   r(   r)   r4   r   r   r   r*   r+   r,   r   r   o  s    =r+   r   c                  :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
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                T    t        j                  |        t        | j                  d<   y )Ns   session)r"   rw   r$   channelLookupr1   s    r,   rw   zRekeyAvatar.__init__  s     4 )3:&r+   c                    t        j                         }|j                         j                  t        |             fd}t	        |t                     j                  d       y)zE
        Write 60 lines of data to the transport, then exit.
        c                   t        |       }|dk(  rRj                          j                  j                  j	                  j                  dd       j                          y d|fz  }|j                  d      }j                  |       y )N<   s   exit-statuss       zline #%02d
r   )nextstopsessionconnsendRequestrO   r   rN   )counterilinecallrM   s      r,   rN   z$RekeyAvatar.openShell.<locals>.write  sx    WABw		!!&&22%%~7J ((*%,{{7+%r+   g{Gz?N)r   ProtocolmakeConnectionr%   r   r   r   )r2   rM   r   rN   r   s    `  @r,   	openShellzRekeyAvatar.openShell  sU     !!#Y'  e!45	&( 5%'*

4r+   c                     y)z2
        Ignore the close of the session.
        Nr*   r1   s    r,   closedzRekeyAvatar.closed      r+   c                     y r[   r*   r1   s    r,   r:   zRekeyAvatar.eofReceived      r+   c                     y r[   r*   )r2   r   commands      r,   execCommandzRekeyAvatar.execCommand  r  r+   c                     y r[   r*   )r2   term
windowSizemodess       r,   getPtyzRekeyAvatar.getPty  r  r+   c                     y r[   r*   )r2   newWindowSizes     r,   windowChangedzRekeyAvatar.windowChanged  r  r+   N)r'   r(   r)   r4   rw   r   r  r:   r  r  r  r*   r+   r,   r   r     s+    4B
r+   r   c                      e Zd ZdZd Zy)
RekeyRealmzS
    This realm gives out new L{RekeyAvatar} instances for any avatar request.
    c                $    |d   t               d fS )Nr   c                      y r[   r*   r*   r+   r,   <lambda>z*RekeyRealm.requestAvatar.<locals>.<lambda>  r  r+   )r   )r2   avatarIDmind
interfacess       r,   requestAvatarzRekeyRealm.requestAvatar  s    !}km\99r+   N)r'   r(   r)   r4   r  r*   r+   r,   r  r    s    :r+   r  c                      e Zd ZdZeZd Zy)RekeyTestsMixinzp
    TestCase mixin which defines tests exercising L{SSHTransportBase}'s handling
    of rekeying messages.
    c                n     t               } j                  d|d      } fd}|j                  |       |S )z
        After a client-initiated rekey is completed, application data continues
        to be passed over the SSH connection.
        r   z-o RekeyLimit=2Kc                    dj                  t        d      D cg c]  }d|fz  
 c}      dz   }|j                  d      }j                  | |       y c c}w )N
r   z
line #%02dr   )joinranger   r   )resultr   expectedResultr2   s      r,   finishedz2RekeyTestsMixin.test_clientRekey.<locals>.finished  sS    !YYU2Y'Ot(;'OPSWWN+227;NV^4 (Ps   A)rV   r   r   )r2   r   r^   r"  s   `   r,   test_clientRekeyz RekeyTestsMixin.test_clientRekey  s8    
 *+LLW&89	5
 	
hr+   N)r'   r(   r)   r4   r  r   r#  r*   r+   r,   r  r    s    
 Lr+   r  c                  :    e Zd Z ed      sdZ	 d	 	 	 	 	 	 	 ddZy)OpenSSHClientMixinsshz$no ssh command-line client availablec                V   t               x}|_        d|z   dz   |z   }| j                  j                         j                  }||z  j                         }g }|D ]"  }	|j                  |	j                  d             $ t        t              j                  |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   rW   r   r   ru   splitr   r   r   r   spawnProcessr   )
r2   remoteCommandr   r   r   cmdlineru   cmdsencodedCmdscmds
             r,   r   zOpenSSHClientMixin.execute  s    & %-J.!    	  $$,,.33$%%' 	4Cszz'23	4 --guU|ATr+   N)r   )r*  strr   rV   r   r/  r   Deferred[None])r'   r(   r)   r   r?   r   r*   r+   r,   r%  r%    s:    <5 TV- -+B-MP-	-r+   r%  c                  @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)OpenSSHKeyExchangeTestsz\
    Tests L{SSHTransportBase}'s key exchange algorithm compatibility with
    OpenSSH.
    c                   g }	 t        j                  t        d      d   ddgt         j                        }t	        |t
              s|j                  d      }|j                         }||vrt        | d      | j                  dt               d	|z         }|j                  | j                  d
      S # t        $ r Y Ww xY w)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/  rm   r(  BaseExceptionr   r   rV   r   r   )r2   keyExchangeAlgokexAlgorithmsoutputr^   s        r,   assertExecuteWithKexAlgorithmz5OpenSSHKeyExchangeTests.assertExecuteWithKexAlgorithmF  s     	,,ua$.z7H7HF fc*w/"LLNM -/o..JKLLLL#%0

 }}T--z::  		s   A%B5 5	C Cc                $    | j                  d      S )zb
        The ecdh-sha2-nistp256 key exchange algorithm is compatible with
        OpenSSH
        zecdh-sha2-nistp256r>  r1   s    r,   test_ECDHSHA256z'OpenSSHKeyExchangeTests.test_ECDHSHA256f      
 112FGGr+   c                $    | j                  d      S )zb
        The ecdh-sha2-nistp384 key exchange algorithm is compatible with
        OpenSSH
        zecdh-sha2-nistp384r@  r1   s    r,   test_ECDHSHA384z'OpenSSHKeyExchangeTests.test_ECDHSHA384m  rB  r+   c                $    | j                  d      S )zb
        The ecdh-sha2-nistp521 key exchange algorithm is compatible with
        OpenSSH
        zecdh-sha2-nistp521r@  r1   s    r,   test_ECDHSHA521z'OpenSSHKeyExchangeTests.test_ECDHSHA521t  rB  r+   c                $    | j                  d      S )zl
        The diffie-hellman-group14-sha1 key exchange algorithm is compatible
        with OpenSSH.
        zdiffie-hellman-group14-sha1r@  r1   s    r,   test_DH_GROUP14z'OpenSSHKeyExchangeTests.test_DH_GROUP14{  s    
 112OPPr+   c                $    | j                  d      S )zs
        The diffie-hellman-group-exchange-sha1 key exchange algorithm is
        compatible with OpenSSH.
        z"diffie-hellman-group-exchange-sha1r@  r1   s    r,   test_DH_GROUP_EXCHANGE_SHA1z3OpenSSHKeyExchangeTests.test_DH_GROUP_EXCHANGE_SHA1  s    
 112VWWr+   c                $    | j                  d      S )zu
        The diffie-hellman-group-exchange-sha256 key exchange algorithm is
        compatible with OpenSSH.
        z$diffie-hellman-group-exchange-sha256r@  r1   s    r,   test_DH_GROUP_EXCHANGE_SHA256z5OpenSSHKeyExchangeTests.test_DH_GROUP_EXCHANGE_SHA256  s    
 112
 	
r+   c                F    | j                  t        | j                  d       y)zy
        The list of key exchange algorithms supported
        by OpenSSH client is obtained with C{ssh -Q kex}.
        zunsupported-algorithmN)assertRaisesr   r>  r1   s    r,   test_unsupported_algorithmz2OpenSSHKeyExchangeTests.test_unsupported_algorithm  s     
 	d88:Q	
r+   N)r'   r(   r)   r4   r>  rA  rD  rF  rH  rJ  rL  rO  r*   r+   r,   r2  r2  @  s5    
;@HHHQX

r+   r2  c                       e Zd ZdZed        Zy)OpenSSHClientForwardingTestszR
    Connection forwarding tests run against the OpenSSL command line client.
    c                    | j                         }t        |d      }| j                  d|d|| j                  fz        }|j	                  | j
                  d       |S )zG
        Forwarding of arbitrary IPv6 TCP connections via SSH.
        r   r   z-N -L%i:[::1]:%ir   )r   rs   r   r   r   r   r   s       r,   test_localToRemoteForwardingV6z;OpenSSHClientForwardingTests.test_localToRemoteForwardingV6  sb    
 %%'	,Y	BLL!3y$//6R!R  
 	
d&&	2r+   N)r'   r(   r)   r4   r   rS  r*   r+   r,   rQ  rQ    s     
 
r+   rQ  c                      e Zd ZdZy)OpenSSHClientRekeyTestszE
    Rekeying tests run against the OpenSSL command line client.
    N)r'   r(   r)   r4   r*   r+   r,   rU  rU    s    r+   rU  c                  |    e Zd ZdZej
                  dk(  rdZ	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZd Zd Z	ddZ
dd	Zdd
Zy)CmdLineClientTestszP
    Connection forwarding tests run against the Conch command line client.
    win32z!can't run cmdline client on win32Nc                
   |g }t               |_        | j                  j                         j                  }dj                  |      |z   d| dz   |z   }t        |j                               }t        j                  j                         }	t        j                  j                  t        j                        |	d<   t        t               j#                  |t        j$                  t'        ||z         |	       |j                  S )z{
        As for L{OpenSSHClientTestCase.execute}, except it runs the 'conch'
        command line tool, not 'ssh'.
        z[-p {} -l testuser --known-hosts kh_test --user-authentications publickey -a -i rsa_test -v  
PYTHONPATH)env)r   rW   r   r   ru   rl   listr(  r   environcopypathsepr  r   r   r   r   r)  r   r   )
r2   r*  r   r   	conchArgs
remoteHostru   cmdtemplater(  r\  s
             r,   r   zCmdLineClientTests.execute  s     I#:'')..
 &,)
 ./zl!+<=
 @MM 	 [&&()jjooJJOOCHH5L --S^^Yy5/@%As 	. 	
 r+   c           	          fd}t        j                   j                                j                  dt	               ddj
                  ddg      }|j                   j                  d       |j                  |       |S )	z4
        It can store logs to a local file.
        c                J    j                         }j                  d|       y )Ns   Log opened.)
getContentassertIn)r   
logContentlogPathr2   s     r,   cb_check_logz<CmdLineClientTests.test_runWithLogFile.<locals>.cb_check_log  s     ++-JMM.*5r+   r   z--logz	--logfilez--host-key-algorithmszssh-rsar*  r   ra  r   )r   r   mktempr   rV   r   r   r   )r2   rj  r^   ri  s   `  @r,   test_runWithLogFilez&CmdLineClientTests.test_runWithLogFile  s{    
	6 ##DKKM2LL(+-'  

 	
d&&5	l#r+   c                t    | j                  dt                     }|j                  | j                  d       |S )zH
        Do not use --host-key-algorithms flag on command line.
        r   )r*  r   r   r   r]   s     r,   %test_runWithNoHostAlgorithmsSpecifiedz8CmdLineClientTests.test_runWithNoHostAlgorithmsSpecified  s;     LL(2I2K  
 	
d&&5r+   c                p    | j                  dt               dg      j                  | j                  d      S )zS
        Simple smoke test for '--compress' flag to ensure we can connect.
        zecho compressedz
--compressrk  s   compressed
r   r1   s    r,    test_runWithCompressionSpecifiedz3CmdLineClientTests.test_runWithCompressionSpecified  s<     ||++-#n  
 +d&&
8		9r+   c                L    t               }d|_        | j                  d|d      S )z<
        Connecting to an invalid host should fail.
        r   zecho nonfunctionalnowhere.invalidr*  r   rb  )rV   rY   r   r2   expectErrors     r,   test_connectToInvalidHostz,CmdLineClientTests.test_connectToInvalidHost  s3     ./'($||.(  
 	
r+   c                f    t               }| j                  | j                  d|d      t              S )zm
        L{ConchTestOpenSSHProcess} fails with a L{ConchError} if an expectation
        is not met.
        zecho checkfailurers  rt  )rV   assertFailurer   r   ru  s     r,   test_testFailurez#CmdLineClientTests.test_testFailure  s>    
 ./!!LL1#,  
 
 	
r+   )r   Nr|   )r*  r/  r   rV   r   r/  ra  zlist[str] | Nonerb  r/  r   r0  )r   r0  )r'   r(   r)   r4   r   platformTyper?   r   rm  ro  rq  rw  rz  r*   r+   r,   rW  rW    s     w&2 &*%   )  	 
 $    
 B6	9


r+   rW  )conch)r   z	list[str]r   r/  r   zlist[bytes])X
__future__r   r   r   r6  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_StdioInteractingSessionr8   er/  r>   r.   r6   r   rA   FactoryrT   ProcessProtocolrV   rs   r~   r   r   r   r   r  r  r%  r2  rQ  rU  rW  r*   r+   r,   <module>r     s   # 	   
   & * L 6  . I I 7 7 - 1 1 , * 0 < 5	 ^,.LL1 7R 7" "$-8 -(
,8 
,("" -.h66 -.`I2!9!9 I2X>h// ><>*I( I(X/+ /l XA) A AH: :+ 21 1hY
35G Y
x?4F &o/A8 r
( r
c  		"  
"!fG	
s*   G G GGG8"G33G8