
    Vh3                     N   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"jT                        Z+ G d d      Z,ddlm-Z- e r e-j\                  e,e+e(j^                          G d dej`                        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;j                        ZD G d0 d1eBe;j                        ZF G d2 d3e<j                        ZH G d4 d5e8j                        ZJ G d6 d7e7j                        ZLd8 ZM G d9 d:ej                        ZO G d; d<ej                        ZP G d= d>ej                        ZQ G d? d@ej                        ZRy)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y)r   c                       e Zd Zy)avatar.ConchUserN)__name__
__module____qualname__     K/home/dcms/DCMS/lib/python3.12/site-packages/twisted/conch/test/test_ssh.py	ConchUserr   $   s    r$   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y)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                     || _         y N)expectedAvatarID)selfr+   s     r%   __init__zConchTestRealm.__init__5   s
     0r$   c                     || j                   k(  rL| 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.
        z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)+DKa=$++t{{/A/AAAD)),,=h\J
 	
r$   )r    r!   r"   __doc__r   r-   r4   r#   r$   r%   r(   r(   (   s     F1
r$   r(   c                   @    e Zd ZdZesdZdZd Zd Zd Z	d Z
d Zd	 Zy
)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                  j                  |        i | _        i | _        | j
                  j                  t        j                  t        j                  d       | j                  j                  dt        i       y )N)   sessions   direct-tcpip   crazy)r   r&   r-   	listenersglobalRequestschannelLookupupdater   
SSHSessionr   openConnectForwardingClientsubsystemLookupCrazySubsystemr,   s    r%   r-   zConchTestAvatar.__init__T   sk    !!$' !!#..!+!G!G	
 	##X~$>?r$   c                 "    || j                   d<   y)Nfoo   r<   r,   datas     r%   
global_foozConchTestAvatar.global_foo`   s    %)E"r$   c                 "    || j                   d<   y)Nfoo_2)rF      datarG   rH   s     r%   global_foo_2zConchTestAvatar.global_foo_2d   s    '+G$r$   c                 0   t        j                  |      \  }}	 t        j                  |t        j                  | j
                  ||ft         j                        |      }|| j                  ||f<   y# t        $ r t        j                  d d       Y y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5IId|Z-X-X H ,4DNND$<(  	GGDNO	s   AA3 3BBc                     t        j                  |      \  }}| j                  j                  ||fd       }|sy| j                  ||f= |j	                          y)Nr   rF   )r   rQ   r;   getstopListeningrX   s        r%   global_cancel_tcpip_forwardz+ConchTestAvatar.global_cancel_tcpip_forwardy   sU    ::4@
d>>%%tTlD9NND$<( r$   c                     d| _         | j                  j                         D ]*  }t        j                  d|z         |j                          , y )NTzstopListening %s)	loggedOutr;   valuesr   msgr_   )r,   r[   s     r%   r0   zConchTestAvatar.logout   sC    --/ 	%HGG&12""$	%r$   N)r    r!   r"   r5   r   skiprb   r-   rJ   rN   r\   r`   r0   r#   r$   r%   r/   r/   G   s5     0I
@"%r$   r/   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	ConchSessionForTestAvatarz2
    An ISession adapter for ConchTestAvatar.
    c                     || _         | | j                   _        d| _        d| _        d| _        d| _        t        j                         | _        y)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__   sA    
 #' 
~~'r$   c                 X    t        j                  d       || _        || _        d| _        y )Nzpty reqT)r   rd   _terminalType_windowSizerl   )r,   term
windowSizeattrss       r%   getPtyz ConchSessionForTestAvatar.getPty   s%    	!%r$   c                 `    t        j                  d       || _        t        |       d| _        y )Nzopening shell   shell)r   rd   rk   EchoTransportrj   )r,   rk   s     r%   	openShellz#ConchSessionForTestAvatar.openShell   s$     
er$   c                 Z   || _         || _        |j                         d   }|dk(  r,t        |      }t	        j
                  d|j                         n|dk(  r0t        |      }|j                  |dd         |j                          n|dk(  r0t        |      }|j                  |dd         |j                          nJ|dk(  r0t        |      }|j                  |dd         |j                          nt        j                  d      d	| j                  j                  j                  _        y )
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   s    
IIKN=u%A a!1!12'\e$AGGCG(]"5)AGGCG(] 'AGGCG"":..<=""9r$   c                     d| _         y NrF   )rm   rC   s    r%   eofReceivedz%ConchSessionForTestAvatar.eofReceived   s	    r$   c                     t        j                  d| j                  z         | j                  j                  j
                  | _        | j                  j                  d        y )Nzclosed cmd "%s")	r   rd   rj   rk   r   remoteWindowLeftremoteWindowLeftAtClosero   callbackrC   s    r%   closedz ConchSessionForTestAvatar.closed   sB    !DHH,-'+zz'9'9'J'J$d#r$   N)
r    r!   r"   r5   r-   rv   rz   r   r   r   r#   r$   r%   rg   rg      s%    (>2$r$   rg   )
componentsc                       e Zd Zd Zd Zy)rB   c                      y r*   r#   )r,   argskws      r%   r-   zCrazySubsystem.__init__   s    r$   c                      y)z
        good ... good
        Nr#   rC   s    r%   connectionMadezCrazySubsystem.connectionMade   s    r$   N)r    r!   r"   r-   r   r#   r$   r%   rB   rB      s    r$   rB   c                       e Zd ZdZd Zd Zy)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                 B    || _         |j                  |        d| _        y)zY
        @type p L{twisted.conch.ssh.session.SSHSessionProcessProtocol} instance
        r   Nrk   makeConnectionr   r,   ps     r%   r-   zFalseTransport.__init__   s      
	r$   c           	      :   | j                   ryd| _         | j                  j                          | j                  j                          | j                  j	                          | j                  j                  t        j                  t        ddd                   y)zI
        Disconnect the protocol associated with this transport.
        NrF      	r   rk   inConnectionLostoutConnectionLosterrConnectionLostprocessEndedr   Failurer   rC   s    r%   r   zFalseTransport.loseConnection   sj     ;;

##%

$$&

$$&

0A#tT0R STr$   N)r    r!   r"   r5   r-   r   r#   r$   r%   r   r      s    
Ur$   r   c                       e Zd Zd Zd Zd Zy)ry   c                 B    || _         |j                  |        d| _        y Nr   r   r   s     r%   r-   zEchoTransport.__init__       
	r$   c                     t        j                  t        |             | j                  j	                  |       | j                  j	                  d       d|v r| j                          y y )N   
    )r   rd   reprrk   outReceivedr   rH   s     r%   r   zEchoTransport.write   sN    T


t$

w'd?! r$   c           	      :   | j                   ry d| _         | j                  j                          | j                  j                          | j                  j	                          | j                  j                  t        j                  t        dd d                    y NrF   r   r   rC   s    r%   r   zEchoTransport.loseConnection  h    ;;

##%

$$&

$$&

0A!T40P QRr$   Nr    r!   r"   r-   r   r   r#   r$   r%   ry   ry      s    
"Sr$   ry   c                       e Zd Zd Zd Zd Zy)r   c                 B    || _         |j                  |        d| _        y r   r   r   s     r%   r-   zErrEchoTransport.__init__  r   r$   c                 p    | j                   j                  |       | j                   j                  d       y Nr   )rk   errReceivedrH   s     r%   r   zErrEchoTransport.write  s&    

t$

w'r$   c           	      :   | j                   ry d| _         | j                  j                          | j                  j                          | j                  j	                          | j                  j                  t        j                  t        dd d                    y r   r   rC   s    r%   r   zErrEchoTransport.loseConnection  r   r$   Nr   r#   r$   r%   r   r     s    
(Sr$   r   c                       e Zd Zd Zd Zd Zy)r   c                 B    || _         |j                  |        d| _        y r   r   r   s     r%   r-   zSuperEchoTransport.__init__%  r   r$   c                     | j                   j                  |       | j                   j                  d       | j                   j                  |       | j                   j                  d       y r   )rk   r   r   rH   s     r%   r   zSuperEchoTransport.write*  sJ    

t$

w'

t$

w'r$   c           	      :   | j                   ry d| _         | j                  j                          | j                  j                          | j                  j	                          | j                  j                  t        j                  t        dd d                    y r   r   rC   s    r%   r   z!SuperEchoTransport.loseConnection0  r   r$   Nr   r#   r$   r%   r   r   $  s    
(Sr$   r   )checkers)channel
connectionfactorykeysr   userauthc                   ,    e Zd Zej                  fZd Zy)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  sH    $$3((K7}}[%9%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y)ConchTestSSHCheckerc                 F    |dk7  st        | j                  |         dk  ryy)Nr      FT)lensuccessfulCredentials)r,   avatarIds     r%   areDonezConchTestSSHChecker.areDoneQ  s(    ;&#d.H.H.R*SVW*Wr$   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y	)
ConchTestServerFactoryF)s   ssh-userauth   ssh-connectionc                     t               }t        t        j                  d | j                  j                         D                    |_        | |_        t        | d      r| j                  |_	        || _
        |S )Nc              3   <   K   | ]  }|j                           y wr*   )supportedSignatureAlgorithms).0keys     r%   	<genexpr>z7ConchTestServerFactory.buildProtocol.<locals>.<genexpr>a  s!      $ 446$s   r   )ConchTestServerlistr   from_iterableprivateKeysrc   supportedPublicKeysr   hasattrr   rk   )r,   addrrk   s      r%   buildProtocolz$ConchTestServerFactory.buildProtocol^  so    #%E(,## $#//668$ )E% !EMt56/3/J/J,DJLr$   c                     t         j                  j                  t              t         j                  j                  t              dS N)   ssh-rsa   ssh-dssr   Key
fromStringr
   r	   rC   s    r%   getPublicKeysz$ConchTestServerFactory.getPublicKeysn  s2     HH//0AB HH//0AB r$   c                     t         j                  j                  t              t         j                  j                  t              dS r   r   r   r   r   r   rC   s    r%   getPrivateKeysz%ConchTestServerFactory.getPrivateKeyst  s2     HH//0BC HH//0BC r$   returnc                 2    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667UVWXXr$   c                 D    t         j                  j                  | ||      S r*   )r   
SSHFactory
getService)r,   transnames      r%   r   z!ConchTestServerFactory.getService  s    %%00ud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)77

	 			YtCeCHo)>$>? 	Y	Dr$   r   c                   "    e Zd ZdZd Zd Zd Zy)ConchTestBaser   c                 z    | j                   ry t        | d      st        j                  d|  d|       d| _         y )Nr   zunexpectedly lost connection 
rF   )doner   r   FailTestr,   reasons     r%   connectionLostzConchTestBase.connectionLost  sC    yy4!9:''3D6F8D  DIr$   c           
          d| _         |t        j                  k7  r't        j                  t        d| d|d|             | j                          y )NrF   zgot disconnect for z	: reason z, desc: )r   r   DISCONNECT_BY_APPLICATIONr   rW   r   r   )r,   
reasonCodedescs      r%   receiveErrorzConchTestBase.receiveError  sH    *+D' Y@@@T3 !r$   c                 2    t        j                  d|       )Nzgot unimplemented: seqid )r   r  )r,   seqIDs     r%   receiveUnimplementedz"ConchTestBase.receiveUnimplemented  s    ##&?w$GHHr$   N)r    r!   r"   r  r  r  r  r#   r$   r%   r  r    s    		" 	Ir$   r  c                       e Zd Zd Zy)r   c                 p    t         j                  | |       t        j                  j                  | |       y r*   )r  r  r   SSHServerTransportr  s     r%   r  zConchTestServer.connectionLost  (    ((v6((77fEr$   N)r    r!   r"   r  r#   r$   r%   r   r     s    	Fr$   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)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                     || _         y r*   )_channelFactoryr,   channelFactorys     r%   r-   zConchTestClient.__init__  s
    #1D r$   c                 p    t         j                  | |       t        j                  j                  | |       y r*   )r  r  r   SSHClientTransportr  s     r%   r  zConchTestClient.connectionLost  r  r$   c                     |t         j                  j                  t              j	                         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  s[    dhh112CDIIKKH!%WW,}}Q''::i(EFGGr$   c                 `    | j                  t        dt        | j                                     y )Nr   )requestServiceConchTestClientAuthConchTestClientConnectionr  rC   s    r%   connectionSecurez ConchTestClient.connectionSecure  s)    #!:4;O;O!Pr$   N)r    r!   r"   r5   r-   r  r)  r.  r#   r$   r%   r  r    s    		2	F	H	r$   r  c                   0    e Zd ZdZdZdZd Zd Zd Zd Z	y)r,  r   c                     | j                   s!| j                  rt        j                  d      t        j
                  j                  | |       y )Nz2got USERAUTH_SUCCESS before password and publickey)canSucceedPasswordcanSucceedPublicKeyr   r  r   SSHUserAuthClientssh_USERAUTH_SUCCESS)r,   packets     r%   r4  z(ConchTestClientAuth.ssh_USERAUTH_SUCCESS  sA    **t/G/G''H  &&;;D&Ir$   c                 :    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                  j                  t                    S r   )r2  r   r   r   r   r   r   rC   s    r%   getPrivateKeyz!ConchTestClientAuth.getPrivateKey  s)    '(D$==!4!45G!HIIr$   c                 H    t         j                  j                  t              S r*   r   r   r   r
   rC   s    r%   getPublicKeyz ConchTestClientAuth.getPublicKey  s    88&&'899r$   N)
r    r!   r"   hasTriedNoner2  r1  r4  r7  r9  r<  r#   r$   r%   r,  r,    s)    	J	.	J	:r$   r,  c                   (    e Zd ZdZdZdZdZd Zd Zy)r-  z
        @ivar _completed: A L{Deferred} which will be fired when the number of
            results collected reaches C{totalResults}.
        r   r      c                 P    t         j                  j                  |        || _        y r*   )r   r  r-   r  r   s     r%   r-   z"ConchTestClientConnection.__init__  s    $$--d3#1D r$   c                 F    | j                  | j                  |              y )N)rT   )openChannelr  rC   s    r%   serviceStartedz(ConchTestClientConnection.serviceStarted  s    T11t1<=r$   N)	r    r!   r"   r5   r  resultstotalResultsr-   rC  r#   r$   r%   r-  r-    s"    	
 !	2	>r$   r-  c                   <    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
y	)
SSHTestChannelc                     || _         || _        g | _        g | _        t	        j
                         | _        t        j                  j                  | g|i | y r*   )
r  _openedreceivedreceivedExtr   rn   ro   r   
SSHChannelr-   )r,   r  openedr   kwargss        r%   r-   zSSHTestChannel.__init__  sK    DI!DLDM!D >>+DL''>t>v>r$   c                 :    | j                   j                  |       y r*   )rI  errbackr  s     r%   
openFailedzSSHTestChannel.openFailed  s    LL  (r$   c                 :    | j                   j                  |        y r*   )rI  r   )r,   ignores     r%   channelOpenzSSHTestChannel.channelOpen       LL!!$'r$   c                 :    | j                   j                  |       y r*   )rJ  appendrH   s     r%   dataReceivedzSSHTestChannel.dataReceived  s    MM  &r$   c                     |t         j                  k(  r| j                  j                  |       y t	        j
                  d|       y )NzUnrecognized extended data: )r   EXTENDED_DATA_STDERRrK  rW  r   rd   )r,   dataTyperI   s      r%   extReceivedzSSHTestChannel.extReceived  s8    ::::  ''-6xlCDr$   c                 >    t        j                  d|      \  | _        y )Nz>L)structunpackstatus)r,   r`  s     r%   request_exit_statusz"SSHTestChannel.request_exit_status  s    "MM$7MT[r$   c                     d| _         y )NT)	eofCalledrC   s    r%   r   zSSHTestChannel.eofReceived  s	    !DNr$   c                 :    | j                   j                  d        y r*   )ro   r   rC   s    r%   r   zSSHTestChannel.closed  rU  r$   N)r    r!   r"   r-   rQ  rT  rX  r\  ra  r   r   r#   r$   r%   rG  rG    s+    	?	)	(	'	E	8	"	(r$   rG  c                      t        j                  dt        j                  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%   conchTestPublicKeyCheckerri    sD      (88488../@ABC 
 ++,@AAr$   c                   P    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y)SSHProtocolTestsz^
    Tests for communication between L{SSHServerTransport} and
    L{SSHClientTransport}.
    can't run without cryptographyc                    t        j                         t        d      | _        t	        j
                  | j                        }t               }|j                  t                      |j                  t                      |j                  |       t               }||_        |j                          |j                  d      | _        t        | j                        | _        t!        fd      | _        t        | j"                        | _        | j                  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   )rG  )rT   rN  r  results    r%   <lambda>z:SSHProtocolTests._ourServerOurClientTest.<locals>.<lambda>>  s    fJ4J6J r$   )r   rn   r(   realmr   Portalr   registerCheckerr   ri  r   startFactoryr   serverr   clientTransportr  clientserverTransportr   )r,   r  rN  r   sshpcfacro  s    ``   @r%   _ourServerOurClientTestz(SSHProtocolTests._ourServerOurClientTest+  s     !#K0
MM$**%#%68979:	% $&
''-,T[[9%J
  -T[[9""4#7#78""4#7#78r$   c                       j                         } 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                  | j                  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  sF    "DL%%((\699\+BA 	 r$   c                 ~    j                   }|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  s5    llG<<++vyy':A r$   c                 Z   j                   }|j                  j                  ddd      }|j                  j                  ddd      }|j                  j                  d       j                  |j                  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    llG//BB//'1EBNN4++W5##..vvqA9B &&B|44r$   c                    j                  j                  j                  j                  ddd       j                  }d|j
                  j                  _        |j
                  j                           |j                          y )Nr  r  )rE   rL   T)
r  rq  r   r<   r   rT   r   r   serviceStoppedr   r  s     r%   
disconnectzESSHProtocolTests.test_subsystemsAndGlobalRequests.<locals>.disconnectq  sf    

!!00&72S llG<@GLL""9LL'')"G""$r$   r{  r  )r,   r   r  r  r  r  s   `     r%    test_subsystemsAndGlobalRequestsz1SSHProtocolTests.test_subsystemsAndGlobalRequestsG  sv     ..0	 	K(	 	,-	5 	,-	% 	J'r$   c                      j                         }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                 N    | _         | j                  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Ir$   c                    j                   j                  j                  j                  d   j                  }j                  |j                  j                   j                         j                  |j                  d       j                  |j                  d       j                  |j                         j                  }|j                  j                  |ddd      S )Nr   r  r  rx   r$   rF   )rq  r   rT   channelsr   assertIsr  rq   rr   
assertTruerl   r   r  )r  r   r   r,   s      r%   cbPtyz*SSHProtocolTests.test_shell.<locals>.cbPty  s    jj'',,55a8@@GMM'..$***;*;<W224FGW00.AOOGNN+llG<<++GXsAFFr$   c                 >   j                   j                  d       j                   j                  j                  j                          t	        j
                  j                   j                  j                  j                  j                  j                  g      S )Ns   testing the shell! )
r   r   rT   sendEOFr   r  ro   rq  r   ri   r  r,   s    r%   cbShellz,SSHProtocolTests.test_shell.<locals>.cbShell  sm    LL89LL%%dll3&&%%tzz'8'8'E'E'M'MN r$   c                    j                   j                  dk7  r-t        j                  dj                   j                  fz         j	                  dj                  j                   j                        d       j                  j                   j                         j                  j                  j                  j                  j                         y )Nr   zshell exit status was not 0: %ir$   s   testing the shell! 
)r   r`  r   rd   r  joinrJ  r  rc  rq  r   ri   rm   r  s    r%   cbExitedz-SSHProtocolTests.test_shell.<locals>.cbExited  s    ||""a'9T\\=P=P<RRS../1N OODLL223OODJJ--::>>?r$   )r{  r   packRequest_pty_reqr  )r,   r   r  r  r  r  rI   s   `     @r%   
test_shellzSSHProtocolTests.test_shell~  s    
 ..0**+=~sS	J 	I&	G 	E"	 	G$	@ 	H%r$   c                       j                         } 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                  | j                  j                  | dt	        j
                  d      d      t              S )N   execs	   jumboliahrF   r  r  s    r%   r  z3SSHProtocolTests.test_failedExec.<locals>.cbChannel  sB    "DL%%(('699\;RTUV r$   c                     j                  t        j                        }j                  |d   j                  j
                  d       y )Nr   )r   N)flushLoggedErrorsr   r   r  valuer   r  errorsr,   s     r%   cbFailedz2SSHProtocolTests.test_failedExec.<locals>.cbFailed  s:     ++E,<,<=FVAY__113EFr$   r  )r,   r   r  r  s   `   r%   test_failedExecz SSHProtocolTests.test_failedExec  sG    
 ..0	 	I&	G 	H%r$   c                       j                         } 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                 t    | _         | j                  j                  | dt        j                  d      d      S )Nr  r|   rF   r  r  s    r%   r  z5SSHProtocolTests.test_falseChannel.<locals>.cbChannel  s/    "DL<<++GWfii>QSTUUr$   c                 0    j                   j                  S r*   r   ro   r  s    r%   cbExecz2SSHProtocolTests.test_falseChannel.<locals>.cbExec      <<'''r$   c                     j                  j                  j                  g        j                  j                  j                  d       y r   )r  r   rJ  assertNotEqualr`  r  s    r%   cbClosedz4SSHProtocolTests.test_falseChannel.<locals>.cbClosed  s7    T\\22B7 3 3Q7r$   r  r,   r   r  r  r  s   `    r%   test_falseChannelz"SSHProtocolTests.test_falseChannel  s^    
 ..0	V 	I&	( 	F#	8
 	H%r$   c                       j                  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                 t    | _         | j                  j                  | dt        j                  d      d      S )Nr  s   eecho hellorF   r  r  s    r%   r  z5SSHProtocolTests.test_errorChannel.<locals>.cbChannel  3    "DL<<++&))N";Q r$   c                     t        j                  j                  j                  j                  j
                  j                  j                  g      S r*   r   r  r   ro   rq  r   ri   r  s    r%   r  z2SSHProtocolTests.test_errorChannel.<locals>.cbExec  >    &&%%tzz'8'8'E'E'M'MN r$   c                 :   j                  j                  j                  g        j                  dj                  j                  j                        d       j                  j                  j
                  d       j                  j                  j                         j                  j                  j                  d       j                  j                  j                  j                  j                  j                  j                         y )Nr$      hello
r   r  )r  r   rJ  r  rK  r`  r  rc  localWindowLeftrq  r   ri   r   r  s    r%   r  z4SSHProtocolTests.test_errorChannel.<locals>.cbClosed  s    T\\22B7SXXdll&>&>?NT\\00!4OODLL223T\\991=,,

!!..FFr$   r  r  s   `    r%   test_errorChannelz"SSHProtocolTests.test_errorChannel  sd    
 ..1Q.O	 	I&	
 	F#		 	H%r$   c                 ~      j                   j                  d      t              } fd}|j                  |       |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                     j                  t        j                        }j                  |d   j                  j
                  d       j                  t        |      d       y )Nr   )   zunknown channelrF   )r  r   r   r  r  r   r   r  s     r%   r  z6SSHProtocolTests.test_unknownChannel.<locals>.cbFailed  sL    ++E,<,<=FVAY__113IJS[!,r$   )r  r{  r   r  )r,   dr  s   `  r%   test_unknownChannelz$SSHProtocolTests.test_unknownChannel  s>    
 (()ABI
	-
 	
hr$   c                       j                  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                 t    | _         | j                  j                  | dt        j                  d      d      S )Nr  s   secho hellorF   r  r  s    r%   r  z2SSHProtocolTests.test_maxPacket.<locals>.cbChannel  r  r$   c                 0    j                   j                  S r*   r  r  s    r%   r  z/SSHProtocolTests.test_maxPacket.<locals>.cbExec'  r  r$   c                    j                  j                  j                  d       j                  dj                  j                  j                        d       j                  dj                  j                  j
                        d       j                  j                  j                  d       j                  j                  j                         y )Nr   r$   r  r  )	r  r   r`  r  rJ  rK  r  r  rc  r  s    r%   r  z1SSHProtocolTests.test_maxPacket.<locals>.cbClosed,  s    T\\00!4SXXdll&;&;<lKSXXdll&>&>?NT\\992>OODLL223r$   r  r  s   `    r%   test_maxPacketzSSHProtocolTests.test_maxPacket  sd     ..2a.P	 	I&	( 	F#	4 	H%r$   c                       j                  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                 t    | _         | j                  j                  | dt        j                  d      d      S )Nr  s
   echo hellorF   r  r  s    r%   r  z-SSHProtocolTests.test_echo.<locals>.cbChannel=  s3    "DL<<++&))M":A r$   c                     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                  j                  d       j                  dj                  j                  j                        d       j                  j                  j
                  d       j                  j                  j                         j                  j                  j
                  j                  j                  j                  j                         y )Nr   r$   r  r  )r  r   r`  r  rJ  r  r  rc  rq  r   ri   r   r  s    r%   r  z,SSHProtocolTests.test_echo.<locals>.cbClosedL  s    T\\00!4SXXdll&;&;<lKT\\991=OODLL223,,

!!..FFr$   r  )r,   r   r  r  r  s   `    r%   	test_echozSSHProtocolTests.test_echo6  sd    
 ..1Q.O	 	I&	
 	F#	 	H%r$   N)r9   )r    r!   r"   r5   r   re   r{  r  r  r  r  r  r  r  r  r#   r$   r%   rk  rk  "  sB    
 /85n.`24"H"@!r$   rk  c                   @    e Zd ZesdZd
dZd Zd Zd Zd Z	d Z
d	 Zy)SSHFactoryTestsrl  Nc                     t        j                         }fd|_        d |_        d |_        |j                          |S )Nc                       S r*   r#   primess   r%   rp  z0SSHFactoryTests.makeSSHFactory.<locals>.<lambda>`  s    v r$   c                  L    dt         j                  j                  t              iS Nr   r;  r#   r$   r%   rp  z0SSHFactoryTests.makeSSHFactory.<locals>.<lambda>a  s    ++,=>,
 r$   c                  L    dt         j                  j                  t              iS r  )r   r   r   r   r#   r$   r%   rp  z0SSHFactoryTests.makeSSHFactory.<locals>.<lambda>d  s    ++,>?-
 r$   )r   r   r   r   r   rt  )r,   r  
sshFactorys    ` r%   makeSSHFactoryzSSHFactoryTests.makeSSHFactory^  sA    '')
-
$

 %

! 	!r$   c                     | j                         } |j                  d      }| j                  |t        j                         y)zc
        By default, buildProtocol() constructs an instance of
        SSHServerTransport.
        N)r  r   assertIsInstancer   r  )r,   r   r   s      r%   test_buildProtocolz"SSHFactoryTests.test_buildProtocolj  s:    
 %%'(7((.h	(D(DEr$   c                     g fd}| j                         }||_         |j                  d       | j                  dg       y)zc
        buildProtocol() calls 'self.protocol()' to construct a protocol
        instance.
        c                  N    j                  |        t        j                         S r*   )rW  r   r  )r   callss    r%   makeProtocolzHSSHFactoryTests.test_buildProtocolRespectsProtocol.<locals>.makeProtocolz  s    LL//11r$   Nr#   )r  r   r   r  )r,   r  r   r  s      @r%   "test_buildProtocolRespectsProtocolz2SSHFactoryTests.test_buildProtocolRespectsProtocols  sI    
 	2 %%''d#"u%r$   c                     t        j                         }d |_        d |_        |j	                          |j                  d      }| j                  g d|j                         y)zq
        buildProtocol() sets supportedPublicKeys to the list of supported
        signature algorithms.
        c                      t         j                  j                  t              t         j                  j                  t              dS r   r   r#   r$   r%   rp  zGSSHFactoryTests.test_buildProtocolSignatureAlgorithms.<locals>.<lambda>  s/    ++,=>++,=>#
 r$   c                      t         j                  j                  t              t         j                  j                  t              dS r   r   r#   r$   r%   rp  zGSSHFactoryTests.test_buildProtocolSignatureAlgorithms.<locals>.<lambda>  s/    ++,>?++,>?$
 r$   N)s   rsa-sha2-512s   rsa-sha2-256r   r   )r   r   r   r   rt  r   r  r   )r,   r   r   s      r%   %test_buildProtocolSignatureAlgorithmsz5SSHFactoryTests.test_buildProtocolSignatureAlgorithms  sY    
  

 	
OOD!F!!	
r$   c                     | j                  d      }|j                  d      }| j                  d|j                         | j                  d|j                         y)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  s[    
    -d#123K3K	
 	3R5M5M	
r$   c                     | j                  ddi      }|j                  d      }| j                  d|j                         | j                  d|j                         y)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  sU       F 4d#;R=U=UV=r?W?WXr$   c                     | j                         }|j                  d      }| j                  ddj                  |j                               y)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  sE       "d# 	GIIb../	
r$   r*   )r    r!   r"   r   re   r  r  r  r  r  r  r  r#   r$   r%   r  r  Z  s/    /
F& 
*
 	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y)	MPTestszs
    Tests for L{common.getMP}.

    @cvar getMP: a method providing a MP parser.
    @type getMP: C{callable}
    rl  c                 F    | j                  | j                  d      d       y)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(Sr$   c                 F    | j                  | j                  d      d       y)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  s!    
 	JJ:;_	
r$   c                 H    | j                  | j                  dd      d       y)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  s)    
 	JJW 	
r$   c                 F    | j                  | j                  d      d       y)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  s!    
 	JJ=>	
r$   c                 Z    | j                  t        j                  | j                  d       y)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=     /V\\*T


Ar$   r  c                       e Zd ZdZesdZd Zy)GMPYInstallDeprecationTestszI
    Tests for the deprecation of former GMPY accidental public API.
    r7   c                     t        j                          | j                  | j                  g      }| j	                  t        |      d       | j	                  |d   d   d       y)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     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-  rL  rG  ri  TestCaserk  r  r  r  r#   r$   r%   <module>r6     s  
   $ $  6  0 5 5 4 ' 0 "^,+CC 

 
>?%f&& ?%D=$ =$@ &J!?G4D4D
X&& U U<S S.S S(S S, & 	< 	<h99 4D!3!3 4DlI I>F-)E)E F
-)E)E 8:h88 :.>J$<$< >"(++ (B
Bux(( up	f
h'' f
R9Ah 9Ax
("3"3 
r$   