
    tf3                     J   d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	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mZ ddlmZ ddlmZ  ed          Z e rddl!m"Z"m#Z# ddl$m%Z%m&Z&m'Z'm(Z( n G d d          Z" G d d          Z) G d de"j*                  Z+ G d d          Z,ddlm-Z- e r e-j.        e,e+e(j/                    G d dej0                  Z1 G d d          Z2 G d d          Z3 G d  d!          Z4 G d" d#          Z5e dd$l!m6Z6 dd%l$m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<  G d& d'          Z= G d( d)e6j>                  Z? G d* d+e9j@                  ZA G d, d-          ZB G d. d/eBe;jC                  ZD G d0 d1eBe;jE                  ZF G d2 d3e<jG                  ZH G d4 d5e8jI                  ZJ G d6 d7e7jK                  ZLd8 ZM G d9 d:ejN                  ZO G d; d<ejN                  ZP G d= d>ejN                  ZQ G d? d@ejN                  ZRdS )Az!
Tests for L{twisted.conch.ssh}.
    N)chain)DictListTuple)privateDSA_opensshprivateRSA_opensshpublicDSA_opensshpublicRSA_openssh)LoopbackRelay)portal)UnauthorizedLogin)deferprotocolreactor)ProcessTerminated)failurelog)requireModule)unittestcryptography)avatarerror)_kexcommon
forwardingsessionc                   (    e Zd Z G d d          ZdS )r   c                       e Zd ZdS )avatar.ConchUserN)__name__
__module____qualname__     Z/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/conch/test/test_ssh.py	ConchUserr   $   s        Dr$   r&   N)r    r!   r"   r&   r#   r$   r%   r   r   #   s<        	 	 	 	 	 	 	 	 	 	r$   r   c                   "    e Zd ZdZdZd Zd ZdS )ConchTestRealma"  
    A realm which expects a particular avatarId to log in once and creates a
    L{ConchTestAvatar} for that request.

    @ivar expectedAvatarID: The only avatarID that this realm will produce an
        avatar for.

    @ivar avatar: A reference to the avatar after it is requested.
    Nc                     || _         d S N)expectedAvatarID)selfr+   s     r%   __init__zConchTestRealm.__init__5   s     0r$   c                     || j         k    rC| j        t          d          t                      | _        |d         | j        | j        j        fS t          d| j         d|          )z
        Return a new L{ConchTestAvatar} if the avatarID matches the expected one
        and this is the first avatar request.
        NzOnly one login allowedr   zOnly z may log in, not )r+   r   r   ConchTestAvatarlogout)r,   avatarIDmind
interfacess       r%   requestAvatarzConchTestRealm.requestAvatar8   sv    
 t,,,{&'(@AAA)++DKa=$+t{/AAAJD)JJhJJ
 
 	
r$   )r    r!   r"   __doc__r   r-   r4   r#   r$   r%   r(   r(   (   sC          F1 1 1
 
 
 
 
r$   r(   c                   B    e Zd ZdZesdZdZd Zd Zd Z	d Z
d Zd	 Zd
S )r/   z
    An avatar against which various SSH features can be tested.

    @ivar loggedOut: A flag indicating whether the avatar logout method has been
        called.
    cannot run without cryptographyFc                    t           j                            |            i | _        i | _        | j                            t          j        t          j
        d           | j                            dt          i           d S )N)   sessions   direct-tcpip   crazy)r   r&   r-   	listenersglobalRequestschannelLookupupdater   
SSHSessionr   openConnectForwardingClientsubsystemLookupCrazySubsystemr,   s    r%   r-   zConchTestAvatar.__init__T   s}    !!$''' !!#.!+!G 	
 	
 	
 	##X~$>?????r$   c                     || j         d<   dS )Nfoo   r<   r,   datas     r%   
global_foozConchTestAvatar.global_foo`   s    %)E"qr$   c                     || j         d<   dS )Nfoo_2)rF      datarG   rH   s     r%   global_foo_2zConchTestAvatar.global_foo_2d   s    '+G$zr$   c                    t          j        |          \  }}	 t          j        |t          j        | j        ||ft           j                  |          }|| j        ||f<   dS # t          $ r t          j
        d d           Y dS w xY w)N)	interfacerF   z2something went wrong with remote->local forwardingr   )r   unpackGlobal_tcpip_forwardr   	listenTCPSSHListenForwardingFactoryconn SSHListenServerForwardingChannelr;   BaseExceptionr   errr,   rI   hostportlisteners        r%   global_tcpip_forwardz$ConchTestAvatar.global_tcpip_forwardh   s    :4@@
d	(5Id|Z-X    H ,4DND$<(1  	 	 	GDNOOO11	s   <A# #BBc                     t          j        |          \  }}| j                            ||fd           }|sdS | j        ||f= |                                 dS )Nr   rF   )r   rQ   r;   getstopListeningrX   s        r%   global_cancel_tcpip_forwardz+ConchTestAvatar.global_cancel_tcpip_forwardy   sc    :4@@
d>%%tTlD99 	1ND$<(   qr$   c                     d| _         | j                                        D ]-}t          j        d|z             |                                 .d S )NTzstopListening %s)	loggedOutr;   valuesr   msgr_   )r,   r[   s     r%   r0   zConchTestAvatar.logout   s[    --// 	% 	%HG&1222""$$$$	% 	%r$   N)r    r!   r"   r5   r   skiprb   r-   rJ   rN   r\   r`   r0   r#   r$   r%   r/   r/   G   s           10I
@ 
@ 
@      "  % % % % %r$   r/   c                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	ConchSessionForTestAvatarz2
    An ISession adapter for ConchTestAvatar.
    c                     || _         | | j         _        d| _        d| _        d| _        d| _        t          j                    | _        dS )zq
        Initialize the session and create a reference to it on the avatar for
        later inspection.
        NFr   )	r   _testSessioncmdprotoptyReqeofr   DeferredonClose)r,   r   s     r%   r-   z"ConchSessionForTestAvatar.__init__   sD    
 #' 
~''r$   c                 X    t          j        d           || _        || _        d| _        d S )Nzpty reqT)r   rd   _terminalType_windowSizerl   )r,   term
windowSizeattrss       r%   getPtyz ConchSessionForTestAvatar.getPty   s-    	!%r$   c                 h    t          j        d           || _        t          |           d| _        d S )Nzopening shell   shell)r   rd   rk   EchoTransportrj   )r,   rk   s     r%   	openShellz#ConchSessionForTestAvatar.openShell   s2       
er$   c                    || _         || _        |                                d         }|dk    r*t          |          }t	          j        d|j                   n|dk    rAt          |          }|                    |dd                     |                                 n|dk    rAt          |          }|                    |dd                     |                                 n[|dk    rAt          |          }|                    |dd                     |                                 nt          j        d          d	| j        j        j        _        d S )
Nr      falses   echo   s   secho   s   eechobad execrF   )rj   rk   splitFalseTransportr   	callLaterloseConnectionry   writeSuperEchoTransportErrEchoTransportr   
ConchErrorr   rT   	transportexpectedLoseConnection)r,   rk   rj   fts        r%   execCommandz%ConchSessionForTestAvatar.execCommand   sH   
IIKKN==u%%A a!12222'\\e$$AGGCG(]]"5))AGGCG(]] ''AGGCG":...<="999r$   c                     d| _         d S NrF   )rm   rC   s    r%   eofReceivedz%ConchSessionForTestAvatar.eofReceived   s    r$   c                     t          j        d| j        z             | j        j        j        | _        | j                            d            d S )Nzclosed cmd "%s")	r   rd   rj   rk   r   remoteWindowLeftremoteWindowLeftAtClosero   callbackrC   s    r%   closedz ConchSessionForTestAvatar.closed   sF    !DH,---'+z'9'J$d#####r$   N)
r    r!   r"   r5   r-   rv   rz   r   r   r   r#   r$   r%   rg   rg      sx         ( ( (    > > >2  $ $ $ $ $r$   rg   )
componentsc                       e Zd Zd Zd ZdS )rB   c                     d S r*   r#   )r,   argskws      r%   r-   zCrazySubsystem.__init__   s    r$   c                     dS )z
        good ... good
        Nr#   rC   s    r%   connectionMadezCrazySubsystem.connectionMade   s      r$   N)r    r!   r"   r-   r   r#   r$   r%   rB   rB      s2              r$   rB   c                       e Zd ZdZd Zd ZdS )r   a  
    False transport should act like a /bin/false execution, i.e. just exit with
    nonzero status, writing nothing to the terminal.

    @ivar proto: The protocol associated with this transport.
    @ivar closed: A flag tracking whether C{loseConnection} has been called yet.
    c                 L    || _         |                    |            d| _        dS )zY
        @type p L{twisted.conch.ssh.session.SSHSessionProcessProtocol} instance
        r   Nrk   makeConnectionr   r,   ps     r%   r-   zFalseTransport.__init__   s*     
	r$   c           	      2   | j         rdS d| _         | j                                         | j                                         | j                                         | j                            t          j        t          ddd                               dS )zI
        Disconnect the protocol associated with this transport.
        NrF      	r   rk   inConnectionLostoutConnectionLosterrConnectionLostprocessEndedr   Failurer   rC   s    r%   r   zFalseTransport.loseConnection   s     ; 	F
##%%%
$$&&&
$$&&&
0A#tT0R0R S STTTTTr$   N)r    r!   r"   r5   r-   r   r#   r$   r%   r   r      sA           
U 
U 
U 
U 
Ur$   r   c                        e Zd Zd Zd Zd ZdS )ry   c                 L    || _         |                    |            d| _        d S Nr   r   r   s     r%   r-   zEchoTransport.__init__   (    
	r$   c                     t          j        t          |                     | j                            |           | j                            d           d|v r|                                  d S d S )N   
    )r   rd   reprrk   outReceivedr   rH   s     r%   r   zEchoTransport.write   sl    T


t$$$
w'''d??!!!!! ?r$   c           	      2   | j         rd S d| _         | j                                         | j                                         | j                                         | j                            t          j        t          dd d                                d S NrF   r   r   rC   s    r%   r   zEchoTransport.loseConnection      ; 	F
##%%%
$$&&&
$$&&&
0A!T40P0P Q QRRRRRr$   Nr    r!   r"   r-   r   r   r#   r$   r%   ry   ry      sF          
" " "S S S S Sr$   ry   c                        e Zd Zd Zd Zd ZdS )r   c                 L    || _         |                    |            d| _        d S r   r   r   s     r%   r-   zErrEchoTransport.__init__  r   r$   c                 n    | j                             |           | j                             d           d S Nr   )rk   errReceivedrH   s     r%   r   zErrEchoTransport.write  s4    
t$$$
w'''''r$   c           	      2   | j         rd S d| _         | j                                         | j                                         | j                                         | j                            t          j        t          dd d                                d S r   r   rC   s    r%   r   zErrEchoTransport.loseConnection  r   r$   Nr   r#   r$   r%   r   r     sF          
( ( (S S S S Sr$   r   c                        e Zd Zd Zd Zd ZdS )r   c                 L    || _         |                    |            d| _        d S r   r   r   s     r%   r-   zSuperEchoTransport.__init__%  r   r$   c                     | j                             |           | j                             d           | j                             |           | j                             d           d S r   )rk   r   r   rH   s     r%   r   zSuperEchoTransport.write*  s`    
t$$$
w'''
t$$$
w'''''r$   c           	      2   | j         rd S d| _         | j                                         | j                                         | j                                         | j                            t          j        t          dd d                                d S r   r   rC   s    r%   r   z!SuperEchoTransport.loseConnection0  r   r$   Nr   r#   r$   r%   r   r   $  sF          
( ( (S S S S Sr$   r   )checkers)channel
connectionfactorykeysr   userauthc                   $    e Zd Zej        fZd ZdS )ConchTestPasswordCheckerc                     |j         dk    r$|j        dk    rt          j        |j                   S t          j        t          d                    S )N   testuser   testpasszBad credentials)usernamepasswordr   succeedfail	Exception)r,   credentialss     r%   requestAvatarIdz(ConchTestPasswordChecker.requestAvatarIdH  sJ    $33(K77}[%9::::i(9::;;;r$   N)r    r!   r"   r   IUsernamePasswordcredentialInterfacesr   r#   r$   r%   r   r   E  s/         ( :<	< 	< 	< 	< 	<r$   r   c                       e Zd Zd ZdS )ConchTestSSHCheckerc                 R    |dk    st          | j        |                   dk     rdS dS )Nr      FT)lensuccessfulCredentials)r,   avatarIds     r%   areDonezConchTestSSHChecker.areDoneQ  s2    ;&&#d.H.R*S*SVW*W*Wu4r$   N)r    r!   r"   r   r#   r$   r%   r   r   P  s#        	 	 	 	 	r$   r   c                       e Zd ZdZej        ej        dZd Z	d Z
d Zdeeeeeef                  f         fdZd Zd	S )
ConchTestServerFactoryF)s   ssh-userauth   ssh-connectionc                    t                      }t          t          j        d | j                                        D                                 |_        | |_        t          | d          r| j	        |_	        || _
        |S )Nc              3   >   K   | ]}|                                 V  d S r*   )supportedSignatureAlgorithms).0keys     r%   	<genexpr>z7ConchTestServerFactory.buildProtocol.<locals>.<genexpr>a  sB       $ $ 4466$ $ $ $ $ $r$   r   )ConchTestServerlistr   from_iterableprivateKeysrc   supportedPublicKeysr   hasattrr   rk   )r,   addrrk   s      r%   buildProtocolz$ConchTestServerFactory.buildProtocol^  s    #%%E(,# $ $#/6688$ $ $  ) )E% !EMt566 K/3/J,DJLr$   c                     t           j                            t                    t           j                            t                    dS N)   ssh-rsa   ssh-dssr   Key
fromStringr
   r	   rC   s    r%   getPublicKeysz$ConchTestServerFactory.getPublicKeysn  s9     H//0ABB H//0ABB  r$   c                     t           j                            t                    t           j                            t                    dS r   r   r   r   r   r   rC   s    r%   getPrivateKeysz%ConchTestServerFactory.getPrivateKeyst  s9     H//0BCC H//0BCC  r$   returnc                 0    dt          j        d          giS )a%  
            Diffie-Hellman primes that can be used for the
            diffie-hellman-group-exchange-sha1 key exchange.

            @return: The primes and generators.
            @rtype: L{dict} mapping the key size to a C{list} of
                C{(generator, prime)} tupple.
            i   s   diffie-hellman-group14-sha1)r   getDHGeneratorAndPrimerC   s    r%   	getPrimesz ConchTestServerFactory.getPrimesz  s     467UVVWXXr$   c                 D    t           j                            | ||          S r*   )r   
SSHFactory
getService)r,   transnames      r%   r   z!ConchTestServerFactory.getService  s    %00udCCCr$   N)r    r!   r"   noisyr   SSHUserAuthServerr   SSHConnectionservicesr   r   r   r   intr   r   r   r   r#   r$   r%   r   r   V  s         &7)7
 

	 	 	 	 	 		 	 		YtCeCHo)>$>? 	Y 	Y 	Y 	Y	D 	D 	D 	D 	Dr$   r   c                   $    e Zd ZdZd Zd Zd ZdS )ConchTestBaser   c                 z    | j         rd S t          | d          st          j        d|  d|           d| _         d S )Nr   zunexpectedly lost connection 
rF   )doner   r   FailTestr,   reasons     r%   connectionLostzConchTestBase.connectionLost  sY    y 4!9:: 'DDDDFDD   DIIIr$   c           
          d| _         |t          j        k    r*t          j        t          d| d|d|                     |                                  d S )NrF   zgot disconnect for z	: reason z, desc: )r   r   DISCONNECT_BY_APPLICATIONr   rW   r   r   )r,   
reasonCodedescs      r%   receiveErrorzConchTestBase.receiveError  sm    *+D' Y@@@I44TT3    !!!!!r$   c                 0    t          j        d|           )Nzgot unimplemented: seqid )r   r  )r,   seqIDs     r%   receiveUnimplementedz"ConchTestBase.receiveUnimplemented  s    #$G$G$GHHHr$   N)r    r!   r"   r  r  r  r  r#   r$   r%   r  r    sK        	 	 		" 	" 	" 	I 	I 	I 	I 	Ir$   r  c                       e Zd Zd ZdS )r   c                 |    t                               | |           t          j                            | |           d S r*   )r  r  r   SSHServerTransportr  s     r%   r  zConchTestServer.connectionLost  7    ((v666(77fEEEEEr$   N)r    r!   r"   r  r#   r$   r%   r   r     s(        	F 	F 	F 	F 	Fr$   r   c                   *    e Zd ZdZd Zd Zd Zd ZdS )ConchTestClientz
        @ivar _channelFactory: A callable which accepts an SSH connection and
            returns a channel which will be attached to a new channel on that
            connection.
        c                     || _         d S r*   )_channelFactoryr,   channelFactorys     r%   r-   zConchTestClient.__init__  s    #1D   r$   c                 |    t                               | |           t          j                            | |           d S r*   )r  r  r   SSHClientTransportr  s     r%   r  zConchTestClient.connectionLost  r  r$   c                     |t           j                            t                                                    k    }|dk    }|r|rt          j        d          S t          j        t          d                    S )Ns/   85:25:04:32:58:55:96:9f:57:ee:fb:a8:1a:ea:69:darF   zKey or fingerprint mismatch)	r   r   r   r
   blobr   r   r   r   )r,   r   fpkeyMatchfingerprintMatchs        r%   verifyHostKeyzConchTestClient.verifyHostKey  sn    dh112CDDIIKKKH!%WW (, (}Q''':i(EFFGGGr$   c                 p    |                      t          dt          | j                                       d S )Nr   )requestServiceConchTestClientAuthConchTestClientConnectionr  rC   s    r%   connectionSecurez ConchTestClient.connectionSecure  sB    #!:4;O!P!P     r$   N)r    r!   r"   r5   r-   r  r)  r.  r#   r$   r%   r  r    s`        	 		2 	2 	2	F 	F 	F	H 	H 	H	 	 	 	 	r$   r  c                   2    e Zd ZdZdZdZd Zd Zd Zd Z	dS )r,  r   c                     | j         s| j        rt          j        d          t          j                            | |           d S )Nz2got USERAUTH_SUCCESS before password and publickey)canSucceedPasswordcanSucceedPublicKeyr   r  r   SSHUserAuthClientssh_USERAUTH_SUCCESS)r,   packets     r%   r4  z(ConchTestClientAuth.ssh_USERAUTH_SUCCESS  sP    * t/G 'H   &;;D&IIIIIr$   c                 8    d| _         t          j        d          S )NrF   r   )r1  r   r   rC   s    r%   getPasswordzConchTestClientAuth.getPassword  s    &'D#=---r$   c                 |    d| _         t          j        t          j                            t                              S r   )r2  r   r   r   r   r   r   rC   s    r%   getPrivateKeyz!ConchTestClientAuth.getPrivateKey  s+    '(D$=!4!45G!H!HIIIr$   c                 J    t           j                            t                    S r*   )r   r   r   r	   rC   s    r%   getPublicKeyz ConchTestClientAuth.getPublicKey  s    8&&'8999r$   N)
r    r!   r"   hasTriedNoner2  r1  r4  r7  r9  r;  r#   r$   r%   r,  r,    sg        	J 	J 	J	. 	. 	.	J 	J 	J	: 	: 	: 	: 	:r$   r,  c                   *    e Zd ZdZdZdZdZd Zd ZdS )r-  z
        @ivar _completed: A L{Deferred} which will be fired when the number of
            results collected reaches C{totalResults}.
        r   r      c                 R    t           j                            |            || _        d S r*   )r   r  r-   r  r   s     r%   r-   z"ConchTestClientConnection.__init__  s'    $--d333#1D   r$   c                 X    |                      |                     |                      d S )N)rT   )openChannelr  rC   s    r%   serviceStartedz(ConchTestClientConnection.serviceStarted  s-    T11t1<<=====r$   N)	r    r!   r"   r5   r  resultstotalResultsr-   rB  r#   r$   r%   r-  r-    sM        	 	
 !	2 	2 	2	> 	> 	> 	> 	>r$   r-  c                   >    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	S )
SSHTestChannelc                     || _         || _        g | _        g | _        t	          j                    | _        t          j        j	        | g|R i | d S r*   )
r  _openedreceivedreceivedExtr   rn   ro   r   
SSHChannelr-   )r,   r  openedr   kwargss        r%   r-   zSSHTestChannel.__init__  sX    DI!DLDM!D >++DL'>t>>>v>>>>>r$   c                 :    | j                             |           d S r*   )rH  errbackr  s     r%   
openFailedzSSHTestChannel.openFailed  s    L  (((((r$   c                 :    | j                             |            d S r*   )rH  r   )r,   ignores     r%   channelOpenzSSHTestChannel.channelOpen       L!!$'''''r$   c                 :    | j                             |           d S r*   )rI  appendrH   s     r%   dataReceivedzSSHTestChannel.dataReceived  s    M  &&&&&r$   c                     |t           j        k    r| j                            |           d S t	          j        d|           d S )NzUnrecognized extended data: )r   EXTENDED_DATA_STDERRrJ  rV  r   rd   )r,   dataTyperI   s      r%   extReceivedzSSHTestChannel.extReceived  sL    :::: ''-----CxCCDDDDDr$   c                 >    t          j        d|          \  | _        d S )Nz>L)structunpackstatus)r,   r_  s     r%   request_exit_statusz"SSHTestChannel.request_exit_status  s    "M$77MT[[[r$   c                     d| _         d S )NT)	eofCalledrC   s    r%   r   zSSHTestChannel.eofReceived  s    !DNNNr$   c                 :    | j                             d            d S r*   )ro   r   rC   s    r%   r   zSSHTestChannel.closed  rT  r$   N)r    r!   r"   r-   rP  rS  rW  r[  r`  r   r   r#   r$   r%   rF  rF    s        	? 	? 	?	) 	) 	)	( 	( 	(	' 	' 	'	E 	E 	E	8 	8 	8	" 	" 	"	( 	( 	( 	( 	(r$   rF  c                      t          j        dt          j                            t
                    gi          } t          j        |           S )z
        Produces a SSHPublicKeyChecker with an in-memory key mapping with
        a single use: 'testuser'

        @return: L{twisted.conch.checkers.SSHPublicKeyChecker}
        r   )r   InMemorySSHKeyDBr   r   r   r	   SSHPublicKeyChecker)conchTestPublicKeyDBs    r%   conchTestPublicKeyCheckerrh    sG      (848../@AABC 
  
 +,@AAAr$   c                   R    e Zd ZdZesdZddZd Zd Zd Z	d Z
d	 Zd
 Zd Zd ZdS )SSHProtocolTestsz^
    Tests for communication between L{SSHServerTransport} and
    L{SSHClientTransport}.
    can't run without cryptographyr9   c                    t          j                    t          d          | _        t	          j        | j                  }t                      }|                    t                                 |                    t                                 |                    |           t                      }||_        |                                 |                    d          | _        t          | j                  | _        t!          fd          | _        t          | j                  | _        | j                            | j                   | j                            | j                   S )z
        Create a connected SSH client and server protocol pair and return a
        L{Deferred} which fires with an L{SSHTestChannel} instance connected to
        a channel on that SSH connection.
        r   Nc                 $    t          fd| iS )NrT   )rF  )rT   rM  r  results    r%   <lambda>z:SSHProtocolTests._ourServerOurClientTest.<locals>.<lambda>>  s    fJJ4J6JJ r$   )r   rn   r(   realmr   Portalr   registerCheckerr   rh  r   startFactoryr   serverr   clientTransportr  clientserverTransportr   )r,   r  rM  r   sshpcfacrn  s    ``   @r%   _ourServerOurClientTestz(SSHProtocolTests._ourServerOurClientTest+  sB    !!#K00
M$*%%#%%688999799:::	%   $&&
''--,T[99%JJJJJJ
 
  -T[99""4#7888""4#7888r$   c                                                        } fd} |j        |            fd} |j        |            fd} |j        |            fd} |j        |           |S )z
        Run the Conch server against the Conch client.  Set up several different
        channels which exercise different behaviors and wait for them to
        complete.  Verify that the channels with errors log them.
        c           	          | _                             | j                            | dt	          j        d          d          t                    S )N	   subsystems	   not-crazyrF   r   assertFailurerT   sendRequestr   NSr   r   r,   s    r%   cbSubsystemzFSSHProtocolTests.test_subsystemsAndGlobalRequests.<locals>.cbSubsystemO  sO    "DL%%((\69\+B+BA  	  r$   c                 p    j         }|j                            |dt          j        d          d          S )Nr}  r:   rF   r   rT   r  r   r  ignoredr   r,   s     r%   cbNotCrazyFailedzKSSHProtocolTests.test_subsystemsAndGlobalRequests.<locals>.cbNotCrazyFailedZ  s7    lG<++vy':':A  r$   c                 P   j         }|j                            ddd          }|j                            ddd          }|                    j        d                               |j                            ddd          t                    }t          j        |||g          S )N   foo   barrF   s   foo-2   bar2rM   )	r   rT   sendGlobalRequestaddCallbackassertEqualr  r   r   gatherResults)r  r   d1d2d3r,   s        r%   cbGlobalRequestszKSSHProtocolTests.test_subsystemsAndGlobalRequests.<locals>.cbGlobalRequestsb  s    lG//BBB//'1EEBNN4+W555##..vvqAA9 B &B|444r$   c                                          j        j        j        ddd           j        }d|j        j        _        |j                                          |j	                     d S )Nr  r  )rE   rL   T)
r  rp  r   r<   r   rT   r   r   serviceStoppedr   r  s     r%   
disconnectzESSHProtocolTests.test_subsystemsAndGlobalRequests.<locals>.disconnectq  sq    
!0&72S2S   lG<@GL"9L'')))"G"$$$$$r$   rz  r  )r,   r   r  r  r  r  s   `     r%    test_subsystemsAndGlobalRequestsz1SSHProtocolTests.test_subsystemsAndGlobalRequestsG  s     ..00	 	 	 	 	 	K(((	 	 	 	 	 	,---	5 	5 	5 	5 	5 	,---	% 	% 	% 	% 	% 	J'''r$   c                                                       }t          j        ddd           fd} |j        |            fd} |j        |            fd} |j        |            fd} |j        |           |S )z
        L{SSHChannel.sendRequest} can open a shell with a I{pty-req} request,
        specifying a terminal type and window size.
           conch-test-term   P   r   r   r$   c                 L    | _         | j                            | dd          S )Ns   pty-reqrF   )r   rT   r  )r   rI   r,   s    r%   	cbChannelz.SSHProtocolTests.test_shell.<locals>.cbChannel  s&    "DL<++GZqIIIr$   c                 x   j         j        j        j        d         j        }                    |j        j         j                                       |j        d                               |j        d           	                    |j
                   j        }|j                            |ddd          S )Nr   r  r  rx   r$   rF   )rp  r   rT   channelsr   assertIsr  rq   rr   
assertTruerl   r   r  )r  r   r   r,   s      r%   cbPtyz*SSHProtocolTests.test_shell.<locals>.cbPty  s    j',5a8@GMM'.$**;<<<W24FGGGW0.AAAOOGN+++lG<++GXsAFFFr$   c                     j                             d           j         j                            j                    t	          j        j         j        j        j        j	        j        g          S )Ns   testing the shell! )
r   r   rT   sendEOFr   r  ro   rp  r   ri   r  r,   s    r%   cbShellz,SSHProtocolTests.test_shell.<locals>.cbShell  sb    L8999L%%dl333&%tz'8'E'MN  r$   c                 b   j         j        dk    r"t          j        dj         j        fz                                 d                    j         j                  d                               j         j                                       j	        j
        j        j                   d S )Nr   zshell exit status was not 0: %ir$   s   testing the shell! 
)r   r_  r   rd   r  joinrI  r  rb  rp  r   ri   rm   r  s    r%   cbExitedz-SSHProtocolTests.test_shell.<locals>.cbExited  s    |"a''9T\=P<RRSSS.//1N   OODL2333OODJ-:>?????r$   )rz  r   packRequest_pty_reqr  )r,   r   r  r  r  r  rI   s   `     @r%   
test_shellzSSHProtocolTests.test_shell~  s    
 ..00*+=~sSS	J 	J 	J 	J 	J 	J 	I&&&	G 	G 	G 	G 	G 	E"""	 	 	 	 	 	G$$$	@ 	@ 	@ 	@ 	@ 	H%%%r$   c                                                        } fd} |j        |            fd} |j        |           |S )z
        If L{SSHChannel.sendRequest} issues an exec which the server responds to
        with an error, the L{Deferred} it returns fires its errback.
        c           	          | _                             | j                            | dt	          j        d          d          t                    S )N   execs	   jumboliahrF   r~  r  s    r%   r  z3SSHProtocolTests.test_failedExec.<locals>.cbChannel  sI    "DL%%(('69\;R;RTUVV  r$   c                                          t          j                  }                    |d         j        j        d           d S )Nr   )r   N)flushLoggedErrorsr   r   r  valuer   r  errorsr,   s     r%   cbFailedz2SSHProtocolTests.test_failedExec.<locals>.cbFailed  sA     ++E,<==FVAY_13EFFFFFr$   r  )r,   r   r  r  s   `   r%   test_failedExecz SSHProtocolTests.test_failedExec  s|    
 ..00	 	 	 	 	 	I&&&	G 	G 	G 	G 	G 	H%%%r$   c                                                        } fd} |j        |            fd} |j        |            fd} |j        |           |S )z
        When the process started by a L{SSHChannel.sendRequest} exec request
        exits, the exit status is reported to the channel.
        c                 p    | _         | j                            | dt          j        d          d          S )Nr  r|   rF   r  r  s    r%   r  z5SSHProtocolTests.test_falseChannel.<locals>.cbChannel  s1    "DL<++GWfi>Q>QSTUUUr$   c                     j         j        S r*   r   ro   r  s    r%   cbExecz2SSHProtocolTests.test_falseChannel.<locals>.cbExec      <''r$   c                                          j        j        g                                j        j        d           d S r   )r  r   rI  assertNotEqualr_  r  s    r%   cbClosedz4SSHProtocolTests.test_falseChannel.<locals>.cbClosed  s?    T\2B777 3Q77777r$   r  r,   r   r  r  r  s   `    r%   test_falseChannelz"SSHProtocolTests.test_falseChannel  s    
 ..00	V 	V 	V 	V 	V 	I&&&	( 	( 	( 	( 	( 	F###	8 	8 	8 	8 	8
 	H%%%r$   c                                            dd          } fd} |j        |            fd} |j        |            fd} |j        |           |S )z
        Bytes sent over the extended channel for stderr data are delivered to
        the channel's C{extReceived} method.
           r}   localWindowlocalMaxPacketc                 p    | _         | j                            | dt          j        d          d          S )Nr  s   eecho hellorF   r  r  s    r%   r  z5SSHProtocolTests.test_errorChannel.<locals>.cbChannel  7    "DL<++&)N";";Q  r$   c                 l    t          j        j        j        j        j        j        j        g          S r*   r   r  r   ro   rp  r   ri   r  s    r%   r  z2SSHProtocolTests.test_errorChannel.<locals>.cbExec  0    &%tz'8'E'MN  r$   c                                         j        j        g                                 d                    j        j                  d                                j        j        d                               j        j                                        j        j        d                                j        j        j	        j
        j        j                   d S )Nr$      hello
r   r  )r  r   rI  r  rJ  r_  r  rb  localWindowLeftrp  r   ri   r   r  s    r%   r  z4SSHProtocolTests.test_errorChannel.<locals>.cbClosed  s    T\2B777SXXdl&>??NNNT\0!444OODL2333T\91===,
!.F    r$   r  r  s   `    r%   test_errorChannelz"SSHProtocolTests.test_errorChannel  s    
 ..1Q.OO	 	 	 	 	 	I&&&	 	 	 	 	
 	F###		 		 		 		 		 	H%%%r$   c                                                                 d          t                    } fd}|                    |           |S )z
        When an attempt is made to open an unknown channel type, the L{Deferred}
        returned by L{SSHChannel.sendRequest} fires its errback.
        s   crazy-unknown-channelc                                          t          j                  }                    |d         j        j        d                               t          |          d           d S )Nr   )   zunknown channelrF   )r  r   r   r  r  r   r   r  s     r%   r  z6SSHProtocolTests.test_unknownChannel.<locals>.cbFailed  s[    ++E,<==FVAY_13IJJJS[[!,,,,,r$   )r  rz  r   r  )r,   dr  s   `  r%   test_unknownChannelz$SSHProtocolTests.test_unknownChannel  s`    
 (()ABBI
 
	- 	- 	- 	- 	-
 	
hr$   c                                            dd          } fd} |j        |            fd} |j        |            fd} |j        |           |S )zc
        An L{SSHChannel} can be configured with a maximum packet size to
        receive.
           rF   r  c                 p    | _         | j                            | dt          j        d          d          S )Nr  s   secho hellorF   r  r  s    r%   r  z2SSHProtocolTests.test_maxPacket.<locals>.cbChannel  r  r$   c                     j         j        S r*   r  r  s    r%   r  z/SSHProtocolTests.test_maxPacket.<locals>.cbExec'  r  r$   c                                         j        j        d                                d                    j        j                  d                                d                    j        j                  d                                j        j        d                               j        j                   d S )Nr   r$   r  r  )	r  r   r_  r  rI  rJ  r  r  rb  r  s    r%   r  z1SSHProtocolTests.test_maxPacket.<locals>.cbClosed,  s    T\0!444SXXdl&;<<lKKKSXXdl&>??NNNT\92>>>OODL233333r$   r  r  s   `    r%   test_maxPacketzSSHProtocolTests.test_maxPacket  s     ..2a.PP	 	 	 	 	 	I&&&	( 	( 	( 	( 	( 	F###	4 	4 	4 	4 	4 	H%%%r$   c                                            dd          } fd} |j        |            fd} |j        |            fd} |j        |           |S )ze
        Normal standard out bytes are sent to the channel's C{dataReceived}
        method.
        r  r}   r  c                 p    | _         | j                            | dt          j        d          d          S )Nr  s
   echo hellorF   r  r  s    r%   r  z-SSHProtocolTests.test_echo.<locals>.cbChannel=  s7    "DL<++&)M":":A  r$   c                 l    t          j        j        j        j        j        j        j        g          S r*   r  r  s    r%   cbEchoz*SSHProtocolTests.test_echo.<locals>.cbEchoE  r  r$   c                                         j        j        d                                d                    j        j                  d                                j        j        d                               j        j                                        j        j        j        j	        j
        j                   d S )Nr   r$   r  r  )r  r   r_  r  rI  r  r  rb  rp  r   ri   r   r  s    r%   r  z,SSHProtocolTests.test_echo.<locals>.cbClosedL  s    T\0!444SXXdl&;<<lKKKT\91===OODL2333,
!.F    r$   r  )r,   r   r  r  r  s   `    r%   	test_echozSSHProtocolTests.test_echo6  s    
 ..1Q.OO	 	 	 	 	 	I&&&	 	 	 	 	
 	F###	 	 	 	 	 	H%%%r$   N)r9   )r    r!   r"   r5   r   re   rz  r  r  r  r  r  r  r  r  r#   r$   r%   rj  rj  "  s         
  0/   85 5 5n. . .`  2  4" " "H  "  @! ! ! ! !r$   rj  c                   B    e Zd ZesdZd
dZd Zd Zd Zd Z	d Z
d	 ZdS )SSHFactoryTestsrk  Nc                     t          j                    }fd|_        d |_        d |_        |                                 |S )Nc                       S r*   r#   primess   r%   ro  z0SSHFactoryTests.makeSSHFactory.<locals>.<lambda>`  s    v r$   c                  N    dt           j                            t                    iS Nr   )r   r   r   r
   r#   r$   r%   ro  z0SSHFactoryTests.makeSSHFactory.<locals>.<lambda>a  s    ++,=>>,
 r$   c                  N    dt           j                            t                    iS r  )r   r   r   r   r#   r$   r%   ro  z0SSHFactoryTests.makeSSHFactory.<locals>.<lambda>d  s    ++,>??-
 r$   )r   r   r   r   r   rs  )r,   r  
sshFactorys    ` r%   makeSSHFactoryzSSHFactoryTests.makeSSHFactory^  sW    '))
-~~~
$
 $

 %
 %

! 	!!!r$   c                     |                                  } |j        d          }|                     |t          j                   dS )zc
        By default, buildProtocol() constructs an instance of
        SSHServerTransport.
        N)r  r   assertIsInstancer   r  )r,   r   r   s      r%   test_buildProtocolz"SSHFactoryTests.test_buildProtocolj  sF    
 %%''(7(..h	(DEEEEEr$   c                     g fd}|                                  }||_         |j        d           |                     dg           dS )zc
        buildProtocol() calls 'self.protocol()' to construct a protocol
        instance.
        c                  T                         |            t          j                    S r*   )rV  r   r  )r   callss    r%   makeProtocolzHSSHFactoryTests.test_buildProtocolRespectsProtocol.<locals>.makeProtocolz  s$    LL/111r$   Nr#   )r  r   r   r  )r,   r  r   r  s      @r%   "test_buildProtocolRespectsProtocolz2SSHFactoryTests.test_buildProtocolRespectsProtocols  so    
 	2 	2 	2 	2 	2 %%'''d###"u%%%%%r$   c                     t          j                    }d |_        d |_        |                                 |                    d          }|                     g d|j                   dS )zq
        buildProtocol() sets supportedPublicKeys to the list of supported
        signature algorithms.
        c                      t           j                            t                    t           j                            t                    dS r   r   r#   r$   r%   ro  zGSSHFactoryTests.test_buildProtocolSignatureAlgorithms.<locals>.<lambda>  s6    ++,=>>++,=>>#
 #
 r$   c                      t           j                            t                    t           j                            t                    dS r   r   r#   r$   r%   ro  zGSSHFactoryTests.test_buildProtocolSignatureAlgorithms.<locals>.<lambda>  s6    ++,>??++,>??$
 $
 r$   N)s   rsa-sha2-512s   rsa-sha2-256r   r   )r   r   r   r   rs  r   r  r   )r,   r   r   s      r%   %test_buildProtocolSignatureAlgorithmsz5SSHFactoryTests.test_buildProtocolSignatureAlgorithms  s    
   
 

 
 	
OOD!!FFF!	
 	
 	
 	
 	
r$   c                     |                      d          }|                    d          }|                     d|j                   |                     d|j                   dS )zg
        Group key exchanges are not supported when we don't have the primes
        database.
        Nr  "   diffie-hellman-group-exchange-sha1$   diffie-hellman-group-exchange-sha256)r  r   assertNotInsupportedKeyExchanges)r,   f1p1s      r%   test_buildProtocolNoPrimesz*SSHFactoryTests.test_buildProtocolNoPrimes  sy    
    --d##123K	
 	
 	
 	3R5M	
 	
 	
 	
 	
r$   c                     |                      ddi          }|                    d          }|                     d|j                   |                     d|j                   dS )zU
        Group key exchanges are supported when we have the primes database.
        rF   )r   r  r  Nr  r  )r  r   assertInr  r,   f2p2s      r%   test_buildProtocolWithPrimesz,SSHFactoryTests.test_buildProtocolWithPrimes  sg       F 44d##;R=UVVV=r?WXXXXXr$   c                     |                                  }|                    d          }|                     dd                    |j                             dS )z_
        ECDSA key exchanges are listed with 256 having a higher priority among ECDSA.
        Ns8   ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521   ,)r  r   r  r  r  r  s      r%   test_buildProtocolKexECDSAz*SSHFactoryTests.test_buildProtocolKexECDSA  s_       ""d## 	GIIb.//	
 	
 	
 	
 	
r$   r*   )r    r!   r"   r   re   r  r  r  r  r  r  r
  r#   r$   r%   r  r  Z  s         0/
 
 
 
F F F& & & 
 
 
*
 
 
 	Y 	Y 	Y
 
 
 
 
r$   r  c                   \    e Zd ZdZesdZer eej                  Zd Z	d Z
d Zd Zd ZdS )	MPTestszs
    Tests for L{common.getMP}.

    @cvar getMP: a method providing a MP parser.
    @type getMP: C{callable}
    rk  c                 X    |                      |                     d          d           dS )z
        L{common.getMP} should parse the a multiple precision integer from a
        string: a 4-byte length followed by length bytes of the integer.
        s         )rF   r$   Nr  getMPrC   s    r%   
test_getMPzMPTests.test_getMP  s-    
 	$GHH(SSSSSr$   c                 X    |                      |                     d          d           dS )zv
        L{common.getMP} should be able to parse a big enough integer
        (that doesn't fit on one byte).
        s      )ir$   Nr  rC   s    r%   test_getMPBigIntegerzMPTests.test_getMPBigInteger  s8    
 	JJ:;;_	
 	
 	
 	
 	
r$   c                 Z    |                      |                     dd          d           dS )zg
        L{common.getMP} has the ability to parse multiple integer in the same
        string.
        s               r   )rF   r   r$   Nr  rC   s    r%   test_multipleGetMPzMPTests.test_multipleGetMP  sB    
 	JJW  	
 	
 	
 	
 	
r$   c                 X    |                      |                     d          d           dS )zu
        When more data than needed is sent to L{common.getMP}, it should return
        the remaining data.
        s         foo)rF   r  Nr  rC   s    r%   test_getMPRemainingDatazMPTests.test_getMPRemainingData  s8    
 	JJ=>>	
 	
 	
 	
 	
r$   c                 R    |                      t          j        | j        d           dS )z
        When the string passed to L{common.getMP} doesn't even make 5 bytes,
        it should raise a L{struct.error}.
        s    N)assertRaisesr]  r   r  rC   s    r%   test_notEnoughDatazMPTests.test_notEnoughData  s&    
 	&,
K@@@@@r$   N)r    r!   r"   r5   r   re   staticmethodr   r  r  r  r  r  r  r#   r$   r%   r  r    s           0/ +V\**T T T
 
 

 
 

 
 
A A A A Ar$   r  c                        e Zd ZdZesdZd ZdS )GMPYInstallDeprecationTestszI
    Tests for the deprecation of former GMPY accidental public API.
    r7   c                     t          j                     |                     | j        g          }|                     t          |          d           |                     |d         d         d           dS )zD
        L{twisted.conch.ssh.common.install} is deprecated.
        rF   r   messagezAtwisted.conch.ssh.common.install was deprecated in Twisted 16.5.0N)r   installflushWarningstest_deprecatedr  r   )r,   warningss     r%   r!  z+GMPYInstallDeprecationTests.test_deprecated  sx     	%%t';&<==X***QK	"O	
 	
 	
 	
 	
r$   N)r    r!   r"   r5   r   re   r!  r#   r$   r%   r  r    s<           10

 

 

 

 

r$   r  )Sr5   r]  	itertoolsr   typingr   r   r   twisted.conch.test.keydatar   r   r	   r
   twisted.conch.test.loopbackr   twisted.credr   twisted.cred.errorr   twisted.internetr   r   r   twisted.internet.errorr   twisted.pythonr   r   twisted.python.reflectr   twisted.trialr   r   twisted.conchr   r   twisted.conch.sshr   r   r   r   r(   r&   r/   rg   r   registerAdapterISessionProtocolrB   r   ry   r   r   r   r   r   r   r   r   r   r   SSHProtocolCheckerr   r   r   r  r  r   r#  r  r3  r,  r  r-  rK  rF  rh  TestCaserj  r  r  r  r#   r$   r%   <module>r5     sO   
        $ $ $ $ $ $ $ $ $ $            6 5 5 5 5 5       0 0 0 0 0 0 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 ' ' ' ' ' ' ' ' 0 0 0 0 0 0 " " " " " "}^,, ++++++++CCCCCCCCCCCCC       

 
 
 
 
 
 
 
>?% ?% ?% ?% ?%f& ?% ?% ?%D=$ =$ =$ =$ =$ =$ =$ =$@ & % % % % % J!?G4D  
    X&   U U U U U U U U<S S S S S S S S.S S S S S S S S(S S S S S S S S, &&&&&&               	< 	< 	< 	< 	< 	< 	< 	<    h9   4D 4D 4D 4D 4D!3 4D 4D 4DlI I I I I I I I>F F F F F-)E F F F
    -)E   8: : : : :h8 : : :.> > > > >J$< > > >"( ( ( ( (+ ( ( (B
B 
B 
Bu u u u ux( u u up	f
 f
 f
 f
 f
h' f
 f
 f
R9A 9A 9A 9A 9Ah 9A 9A 9Ax
 
 
 
 
("3 
 
 
 
 
r$   