o
    Òó·i½d  ã                
   @  s  d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZmZ d dlmZmZmZ d dlmZ d dlmZ d dl m!Z! d dl"m#Z#m$Z$m%Z% d dl&m'Z' d dl(m)Z) d dl*m+Z+ d dl,m-Z-m.Z. d dl/m0Z0m1Z1 z
d dlm2Z2m3Z3 W n	 e4y¥   Y nw e+dƒZ5e5r½d dl6m7Z7 d dl8m9Z9m:Z:m;Z; nd dl<m9Z9 G dd„ dƒZ7zd dl=m:Z> W n e4yë Z? zdZ@eAe?ƒZB[?W Y dZ?[?ndZ?[?ww e>Z@G dd„ dƒZCG dd „ d e1ƒZDG d!d"„ d"ejEƒZFG d#d$„ d$ejGƒZHG d%d&„ d&ejIƒZJG d'd(„ d(ejIƒZKG d)d*„ d*ejEƒZLdHdId2d3„ZMG d4d5„ d5ƒZNG d6d7„ d7eNƒZOee9ƒG d8d9„ d9e7ƒƒZPG d:d;„ d;ƒZQG d<d=„ d=eNƒZRG d>d?„ d?ƒZSG d@dA„ dAeNeSe1ƒZTG dBdC„ dCeOeSe1ƒZUG dDdE„ dEeReSe1ƒZVG dFdG„ dGeOe1ƒZWdS )Jé    )ÚannotationsN)Úcount)ÚAny)Úimplementer)Ú
ConchError)ÚprivateRSA_opensshÚpublicRSA_openssh©ÚConchTestRealm)Úportal)ÚprotocolÚreactor)ÚDeferredÚgatherResultsÚmaybeDeferred)ÚProcessExitedAlready)ÚIReactorProcess)ÚLoopingCall)ÚfilepathÚlogÚruntime)ÚFilePath)Úwhich)ÚrequireModule)ÚHAS_IPV6ÚskipWithoutIPv6)ÚSkipTestÚTestCase)ÚConchTestServerFactoryÚconchTestPublicKeyCheckerÚcryptography)Ú	ConchUser)ÚISessionÚ
SSHSessionÚwrapProtocol)r"   c                   @  s   e Zd ZdS )r!   N)Ú__name__Ú
__module__Ú__qualname__© r(   r(   úcC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/conch/test/test_conch.pyr!   /   s    r!   )r#   c                   @  s   e Zd ZdZdZdd„ ZdS )Ú	FakeStdiozø
    A fake for testing L{twisted.conch.scripts.conch.SSHSession.eofReceived} and
    L{twisted.conch.scripts.cftp.SSHSession.eofReceived}.

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

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

    @ivar expectedExitCode: If the process exit code is not C{expectedExitCode}
    the set C{deferred} will by triggerd with a failure.
    NzDeferred[None] | NoneÚdeferredó    r   ÚintÚexpectedExitCodec                 C  ó   | j d }| _ |S ©N©rK   ©r-   Údr(   r(   r)   Ú_getDeferred…   ó   z$ConchTestOpenSSHProcess._getDeferredc                 C  ó   |  j |7  _ d S rP   )ÚbufrE   r(   r(   r)   ÚoutReceived‰   ó   z#ConchTestOpenSSHProcess.outReceivedc                 C  rV   rP   )ÚproblemsrE   r(   r(   r)   ÚerrReceivedŒ   rY   z#ConchTestOpenSSHProcess.errReceivedc              
   C  s\   |j j| jkr|  ¡  td | j|j j| j d¡¡ƒ¡ dS | j	 
dd¡}|  ¡  |¡ dS )z~
        Called when the process has ended.

        @param reason: a Failure giving the reason for the process' end.
        zexit code was not {}: {} ({})Úcharmaps   
rA   N)ÚvalueZexitCoderN   rT   Zerrbackr   ÚformatrZ   ÚdecoderW   ÚreplaceÚcallback)r-   r?   rW   r(   r(   r)   ÚprocessEnded�   s   
ýÿÿ
z$ConchTestOpenSSHProcess.processEnded)r%   r&   r'   r/   rK   Ú__annotations__rW   rZ   rN   rT   rX   r[   rb   r(   r(   r(   r)   rJ   t   s   
 rJ   c                   @  sT   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚConchTestForwardingProcessaæ  
    Manages a third-party process which launches a server.

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

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

        @type data: L{str}
        @param data: This is sent to the third-party server. Must end with '
'
        in order to trigger a disconnect.
        N)ÚportÚbufferrF   )r-   re   rF   r(   r(   r)   Ú__init__³   s   

z#ConchTestForwardingProcess.__init__c                 C  rO   rP   rQ   rR   r(   r(   r)   rT   Á   rU   z'ConchTestForwardingProcess._getDeferredc                 C  s   |   ¡  d S rP   )Ú_connectr,   r(   r(   r)   r<   Å   s   z)ConchTestForwardingProcess.connectionMadec                 C  s0   t  tt| | j¡}| d| j¡}| | j¡ |S )a  
        Connect to the server, which is often a third-party process.
        Tries to reconnect if it fails because we have no way of determining
        exactly when the port becomes available for listening -- we can only
        know when the process starts.
        ú	127.0.0.1N)	r   ZClientCreatorr   ÚConchTestForwardingPortrF   Z
connectTCPre   Z
addErrbackÚ
_ebConnect)r-   ÚccrS   r(   r(   r)   rh   È   s   z#ConchTestForwardingProcess._connectc                 C  s   t  d| j¡ d S )Ngš™™™™™¹?)r   Ú	callLaterrh   )r-   Úfr(   r(   r)   rk   Ô   rY   z%ConchTestForwardingProcess._ebConnectc                 C  s.   || _ | j d¡ | j ¡  t d| j¡ dS )zÖ
        The network connection has died; save the buffer of output
        from the network and attempt to quit the process gracefully,
        and then (after the reactor has spun) send it a KILL signal.
        ó   r   N)rf   rB   rC   rD   r   rm   Ú
_reallyDie)r-   rf   r(   r(   r)   ÚforwardingPortDisconnected×   s   
z5ConchTestForwardingProcess.forwardingPortDisconnectedc                 C  s(   z	| j  d¡ W d S  ty   Y d S w )NZKILL)rB   ZsignalProcessr   r,   r(   r(   r)   rp   â   s
   ÿz%ConchTestForwardingProcess._reallyDiec                 C  s   |   ¡  | j¡ dS )zŒ
        Fire the Deferred at self.deferred with the data collected
        from the L{ConchTestForwardingPort} connection, if any.
        N)rT   ra   rf   r>   r(   r(   r)   rb   è   s   z'ConchTestForwardingProcess.processEnded)r%   r&   r'   r/   rK   rg   rT   r<   rh   rk   rq   rp   rb   r(   r(   r(   r)   rd   ¤   s    rd   c                   @  s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )rj   zë
    Connects to server launched by a third-party process (managed by
    L{ConchTestForwardingProcess}) sends data, then reports whatever it
    received back to the L{ConchTestForwardingProcess} once the connection
    is ended.
    c                 C  s   || _ || _dS )zè
        @type protocol: L{ConchTestForwardingProcess}
        @param protocol: The L{ProcessProtocol} which made this connection.

        @type data: str
        @param data: The data to be sent to the third-party server.
        N)r   rF   )r-   r   rF   r(   r(   r)   rg   ø   s   
z ConchTestForwardingPort.__init__c                 C  s   d| _ | j | j¡ d S )NrL   )rf   rB   rC   rF   r,   r(   r(   r)   r<     s   z&ConchTestForwardingPort.connectionMadec                 C  rV   rP   )rf   rE   r(   r(   r)   rG     rY   z$ConchTestForwardingPort.dataReceivedc                 C  s   | j  | j¡ d S rP   )r   rq   rf   r>   r(   r(   r)   r@   
  rY   z&ConchTestForwardingPort.connectionLostN)r%   r&   r'   r/   rg   r<   rG   r@   r(   r(   r(   r)   rj   ð   s    rj   ÚconchÚargsú	list[str]ÚmodÚstrÚreturnúlist[bytes]c                 C  s$   t jd| g}dd„ g |¢| ¢D ƒS )Na$  -c
### Twisted Preamble
import sys, os
path = os.path.abspath(sys.argv[0])
while os.path.dirname(path) != path:
    if os.path.basename(path).startswith('Twisted'):
        sys.path.insert(0, path)
        break
    path = os.path.dirname(path)

from twisted.conch.scripts.%s import run
run()c                 S  s   g | ]}|  d ¡‘qS )úutf-8)Úencode)Ú.0Zeachr(   r(   r)   Ú
<listcomp>   ó    z_makeArgs.<locals>.<listcomp>)ÚsysÚ
executable)rs   ru   Ústartr(   r(   r)   Ú	_makeArgs  s   óþr�   c                   @  sJ   e Zd ZesdZedd„ ƒZdd„ Zdd„ Zdd	„ Z	d
d„ Z
ddd„ZdS )ÚConchServerSetupMixinzcan't run without cryptographyc                   C  s   t dƒS )Ns   testuserr	   r(   r(   r(   r)   ÚrealmFactory'  s   z"ConchServerSetupMixin.realmFactoryc                 C  sö   dD ]}t j |¡rt  |¡ qtddƒ�}| t¡ W d   ƒ n1 s%w   Y  tddƒ�}| t¡ W d   ƒ n1 s?w   Y  t  dd¡ t	dƒ 
¡ }|jjsX|jjr\tdƒ‚tddƒ�}| dt ¡ W d   ƒ d S 1 stw   Y  d S )	N)Úrsa_testúrsa_test.pubÚkh_testr„   Úwbr…   i€  zgprivate key readable by others despite chmod; possible windows permission issue? see https://tm.tl/9767r†   s
   127.0.0.1 )ÚosÚpathÚexistsÚremoveÚopenrC   r   r   Úchmodr   ZgetPermissionsÚgroupÚreadÚotherr   )r-   rn   Zpermissionsr(   r(   r)   Ú_createFiles+  s&   
€ÿÿÿ"ÿz"ConchServerSetupMixin._createFilesc                 C  s*   t   ¡ }| d¡ | ¡ d }| ¡  |S )N)Ú r   é   )ÚsocketÚbindÚgetsocknameÚclose)r-   Úsre   r(   r(   r)   Ú_getFreePort>  s
   
z"ConchServerSetupMixin._getFreePortc                 C  s.   |   ¡ }t |¡}| tƒ ¡ tƒ }||_|S )zŽ
        Make a L{ConchTestServerFactory}, which allows us to start a
        L{ConchTestServer} -- i.e. an actually listening conch.
        N)rƒ   r   ZPortalZregisterCheckerr   r   )r-   ÚrealmÚpÚfactoryr(   r(   r)   Ú_makeConchFactoryE  s   
z'ConchServerSetupMixin._makeConchFactoryc                 C  sz   |   ¡  |  ¡ | _d| j_tjd| jdd�| _t dtƒ ¡| _| j 	¡ j
| _tr;tjdtƒ dd�| _| j 	¡ j
| _d S d S )Nr“   r   ri   )Z	interfacez::1)r‘   r�   ÚconchFactoryZexpectedLoseConnectionr   Z	listenTCPÚconchServerrH   Ú
echoServerÚgetHostre   ÚechoPortr   ÚechoServerV6Ú
echoPortV6r,   r(   r(   r)   ÚsetUpQ  s   
ÿþzConchServerSetupMixin.setUprw   r   c                 C  sf   zd| j j_W n	 ty   Y nw | j jj ¡  t| jjƒt| j	jƒg}t
r/| t| jjƒ¡ t|ƒS )Nr“   )rž   ÚprotoÚdoneÚAttributeErrorrB   rD   r   rŸ   ÚstopListeningr    r   Úappendr£   r   )r-   Z	deferredsr(   r(   r)   ÚtearDown^  s   ÿ

þzConchServerSetupMixin.tearDownN)rw   r   )r%   r&   r'   r    r7   Ústaticmethodrƒ   r‘   r™   r�   r¥   r«   r(   r(   r(   r)   r‚   #  s    
r‚   c                   @  s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚForwardingMixinaÏ  
    Template class for tests of the Conch server's ability to forward arbitrary
    protocols over SSH.

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

    L{execute} is implemented by subclasses of L{ForwardingMixin}. It should
    cause an SSH client to connect to the Conch server, asking it to forward
    data to the custom TCP server.
    c                 C  s   |   dtƒ ¡}| | jd¡S )z³
        Test that we can use whatever client to send the command "echo goodbye"
        to the Conch server. Make sure we receive "goodbye" back from the
        server.
        úecho goodbyeó   goodbye
N©ÚexecuterJ   ÚaddCallbackÚassertEqualrR   r(   r(   r)   Ú	test_exec}  s   zForwardingMixin.test_execc                 C  ó>   |   ¡ }t|dƒ}| jd|d|| jf d�}| | jd¡ |S )zy
        Test that we can use whatever client to forward a local port to a
        specified port on the server.
        ó   test
r’   z-N -L%i:127.0.0.1:%i©ÚsshArgsN©r™   rd   r±   r¢   r²   r³   ©r-   Z	localPortÚprocessrS   r(   r(   r)   Útest_localToRemoteForwarding†  ó   
ÿz,ForwardingMixin.test_localToRemoteForwardingc                 C  rµ   )zs
        Test that we can use whatever client to forward a port from the server
        to a port locally.
        r¶   r’   z-N -R %i:127.0.0.1:%ir·   Nr¹   rº   r(   r(   r)   Útest_remoteToLocalForwarding“  r½   z,ForwardingMixin.test_remoteToLocalForwardingN)r%   r&   r'   r/   r´   r¼   r¾   r(   r(   r(   r)   r­   o  s
    	r­   c                   @  sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚRekeyAvatara#  
    This avatar implements a shell which sends 60 numbered lines to whatever
    connects to it, then closes the session with a 0 exit status.

    60 lines is selected as being enough to send more than 2kB of traffic, the
    amount the client is configured to initiate a rekey after.
    c                 C  s   t  | ¡ t| jd< d S )Ns   session)r!   rg   r#   ZchannelLookupr,   r(   r(   r)   rg   ¯  s   
zRekeyAvatar.__init__c                   sH   t  ¡ }| ˆ¡ ˆ t|ƒ¡ ‡ ‡fdd„}t|tƒ ƒ‰ ˆ  d¡ dS )zE
        Write 60 lines of data to the transport, then exit.
        c                   sZ   t | ƒ}|dkrˆ  ¡  ˆjj ˆjdd¡ ˆ ¡  d S d|f }| d¡}ˆ |¡ d S )Né<   s   exit-statuss       zline #%02d
ry   )ÚnextÚstopÚsessionÚconnZsendRequestrD   rz   rC   )ÚcounterÚiÚline©ÚcallrB   r(   r)   rC   ½  s   ÿ

z$RekeyAvatar.openShell.<locals>.writeg{®Gáz„?N)r   ÚProtocolZmakeConnectionr$   r   r   r€   )r-   rB   r¦   rC   r(   rÈ   r)   Ú	openShell³  s   
zRekeyAvatar.openShellc                 C  s   dS )z2
        Ignore the close of the session.
        Nr(   r,   r(   r(   r)   ÚclosedÔ  ó    zRekeyAvatar.closedc                 C  ó   d S rP   r(   r,   r(   r(   r)   r3   Ù  ó   zRekeyAvatar.eofReceivedc                 C  rÎ   rP   r(   )r-   r¦   Úcommandr(   r(   r)   ÚexecCommandÝ  rÏ   zRekeyAvatar.execCommandc                 C  rÎ   rP   r(   )r-   ZtermZ
windowSizeÚmodesr(   r(   r)   ÚgetPtyá  rÏ   zRekeyAvatar.getPtyc                 C  rÎ   rP   r(   )r-   ZnewWindowSizer(   r(   r)   ÚwindowChangedå  rÏ   zRekeyAvatar.windowChangedN)r%   r&   r'   r/   rg   rË   rÌ   r3   rÑ   rÓ   rÔ   r(   r(   r(   r)   r¿   ¥  s    !r¿   c                   @  s   e Zd ZdZdd„ ZdS )Ú
RekeyRealmzS
    This realm gives out new L{RekeyAvatar} instances for any avatar request.
    c                 G  s   |d t ƒ dd„ fS )Nr   c                   S  rÎ   rP   r(   r(   r(   r(   r)   Ú<lambda>ð  rÍ   z*RekeyRealm.requestAvatar.<locals>.<lambda>)r¿   )r-   ZavatarIDZmindZ
interfacesr(   r(   r)   ÚrequestAvatarï  s   zRekeyRealm.requestAvatarN)r%   r&   r'   r/   r×   r(   r(   r(   r)   rÕ   ê  s    rÕ   c                   @  s   e Zd ZdZeZdd„ ZdS )ÚRekeyTestsMixinzp
    TestCase mixin which defines tests exercising L{SSHTransportBase}'s handling
    of rekeying messages.
    c                   s.   t ƒ }ˆ  d|d¡}‡ fdd„}| |¡ |S )z‡
        After a client-initiated rekey is completed, application data continues
        to be passed over the SSH connection.
        r’   z-o RekeyLimit=2Kc                   s6   d  dd„ tdƒD ƒ¡d }| d¡}ˆ  | |¡ d S )NÚ
c                 S  s   g | ]}d |f ‘qS )z
line #%02dr(   )r{   rÆ   r(   r(   r)   r|     r}   zFRekeyTestsMixin.test_clientRekey.<locals>.finished.<locals>.<listcomp>rÀ   ry   )ÚjoinÚrangerz   r³   )ÚresultZexpectedResultr,   r(   r)   Úfinished  s   
z2RekeyTestsMixin.test_clientRekey.<locals>.finishedN)rJ   r±   r²   )r-   r»   rS   rÝ   r(   r,   r)   Útest_clientRekeyû  s
   
z RekeyTestsMixin.test_clientRekeyN)r%   r&   r'   r/   rÕ   rƒ   rÞ   r(   r(   r(   r)   rØ   ó  s    rØ   c                   @  s&   e Zd Zedƒs
dZ	dddd„ZdS )ÚOpenSSHClientMixinÚsshz$no ssh command-line client availabler’   ÚremoteCommandrv   r»   rJ   r¸   rw   úDeferred[None]c           
      C  sp   t ƒ  }|_d| d | }| j ¡ j}||  ¡ }g }|D ]
}	| |	 d¡¡ qtt	ƒ 
|tdƒd |¡ |S )a¸  
        Connects to the SSH server started in L{ConchServerSetupMixin.setUp} by
        running the 'ssh' command line tool.

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

        @type process: L{ConchTestOpenSSHProcess}

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

        @return: L{defer.Deferred}
        zŸssh -2 -l testuser -p %i -F /dev/null -oIdentitiesOnly=yes -oUserKnownHostsFile=kh_test -oPasswordAuthentication=no -oHostKeyAlgorithms=ssh-rsa -a -i rsa_test z 127.0.0.1 ry   rà   r   N)r   rK   rŸ   r¡   re   Úsplitrª   rz   r   r   ÚspawnProcessr   )
r-   rá   r»   r¸   rÜ   Zcmdlinere   ZcmdsZencodedCmdsÚcmdr(   r(   r)   r±     s    
õôóÿzOpenSSHClientMixin.executeN)r’   )rá   rv   r»   rJ   r¸   rv   rw   râ   )r%   r&   r'   r   r7   r±   r(   r(   r(   r)   rß     s
    ÿrß   c                   @  sP   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚOpenSSHKeyExchangeTestsz\
    Tests L{SSHTransportBase}'s key exchange algorithm compatibility with
    OpenSSH.
    c                 C  sŽ   g }zt jtdƒd ddgt jd�}t|tƒs| d¡}| ¡ }W n	 ty*   Y nw ||vr6t	|› d�ƒ‚|  
dtƒ d	| ¡}| | jd
¡S )aI  
        Call execute() method of L{OpenSSHClientMixin} with an ssh option that
        forces the exclusive use of the key exchange algorithm specified by
        keyExchangeAlgo

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

        @return: L{defer.Deferred}
        rà   r   z-QZkex)Ústderrry   z not supported by ssh clientz
echo helloz-oKexAlgorithms=s   hello
N)Ú
subprocessÚcheck_outputr   ÚSTDOUTÚ
isinstancerv   r_   rã   ÚBaseExceptionr   r±   rJ   r²   r³   )r-   ZkeyExchangeAlgoZkexAlgorithmsÚoutputrS   r(   r(   r)   ÚassertExecuteWithKexAlgorithmF  s&   ÿ

ÿýz5OpenSSHKeyExchangeTests.assertExecuteWithKexAlgorithmc                 C  ó
   |   d¡S )zb
        The ecdh-sha2-nistp256 key exchange algorithm is compatible with
        OpenSSH
        zecdh-sha2-nistp256N©rî   r,   r(   r(   r)   Útest_ECDHSHA256f  ó   
z'OpenSSHKeyExchangeTests.test_ECDHSHA256c                 C  rï   )zb
        The ecdh-sha2-nistp384 key exchange algorithm is compatible with
        OpenSSH
        zecdh-sha2-nistp384Nrð   r,   r(   r(   r)   Útest_ECDHSHA384m  rò   z'OpenSSHKeyExchangeTests.test_ECDHSHA384c                 C  rï   )zb
        The ecdh-sha2-nistp521 key exchange algorithm is compatible with
        OpenSSH
        zecdh-sha2-nistp521Nrð   r,   r(   r(   r)   Útest_ECDHSHA521t  rò   z'OpenSSHKeyExchangeTests.test_ECDHSHA521c                 C  rï   )zl
        The diffie-hellman-group14-sha1 key exchange algorithm is compatible
        with OpenSSH.
        zdiffie-hellman-group14-sha1Nrð   r,   r(   r(   r)   Útest_DH_GROUP14{  rò   z'OpenSSHKeyExchangeTests.test_DH_GROUP14c                 C  rï   )zs
        The diffie-hellman-group-exchange-sha1 key exchange algorithm is
        compatible with OpenSSH.
        z"diffie-hellman-group-exchange-sha1Nrð   r,   r(   r(   r)   Útest_DH_GROUP_EXCHANGE_SHA1‚  rò   z3OpenSSHKeyExchangeTests.test_DH_GROUP_EXCHANGE_SHA1c                 C  rï   )zu
        The diffie-hellman-group-exchange-sha256 key exchange algorithm is
        compatible with OpenSSH.
        z$diffie-hellman-group-exchange-sha256Nrð   r,   r(   r(   r)   Útest_DH_GROUP_EXCHANGE_SHA256‰  s   ÿz5OpenSSHKeyExchangeTests.test_DH_GROUP_EXCHANGE_SHA256c                 C  s   |   t| jd¡ dS )zy
        The list of key exchange algorithms supported
        by OpenSSH client is obtained with C{ssh -Q kex}.
        zunsupported-algorithmN)ÚassertRaisesr   rî   r,   r(   r(   r)   Útest_unsupported_algorithm’  s   ÿz2OpenSSHKeyExchangeTests.test_unsupported_algorithmN)r%   r&   r'   r/   rî   rñ   ró   rô   rõ   rö   r÷   rù   r(   r(   r(   r)   ræ   @  s     	ræ   c                   @  s   e Zd ZdZedd„ ƒZdS )ÚOpenSSHClientForwardingTestszR
    Connection forwarding tests run against the OpenSSL command line client.
    c                 C  rµ   )zG
        Forwarding of arbitrary IPv6 TCP connections via SSH.
        r¶   r’   z-N -L%i:[::1]:%ir·   N)r™   rd   r±   r¤   r²   r³   rº   r(   r(   r)   Útest_localToRemoteForwardingV6¡  r½   z;OpenSSHClientForwardingTests.test_localToRemoteForwardingV6N)r%   r&   r'   r/   r   rû   r(   r(   r(   r)   rú   œ  s    rú   c                   @  s   e Zd ZdZdS )ÚOpenSSHClientRekeyTestszE
    Rekeying tests run against the OpenSSL command line client.
    N)r%   r&   r'   r/   r(   r(   r(   r)   rü   ¯  rI   rü   c                   @  s^   e Zd ZdZejdkrdZ			dddd„Zdd„ Zdd„ Z	ddd„Z
ddd„Zddd„ZdS ) ÚCmdLineClientTestszP
    Connection forwarding tests run against the Conch command line client.
    Úwin32z!can't run cmdline client on win32r’   Nri   rá   rv   r»   rJ   r¸   Ú	conchArgsúlist[str] | NoneÚ
remoteHostrw   râ   c           
      C  sŒ   |du rg }t ƒ |_| j ¡ j}d |¡| d|› d� | }t| ¡ ƒ}tj	 
¡ }	tj tj¡|	d< ttƒj|tjt|| ƒ|	d� |jS )z{
        As for L{OpenSSHClientTestCase.execute}, except it runs the 'conch'
        command line tool, not 'ssh'.
        Nz[-p {} -l testuser --known-hosts kh_test --user-authentications publickey -a -i rsa_test -v ú Ú
PYTHONPATH)Úenv)r   rK   rŸ   r¡   re   r^   Úlistrã   rˆ   ÚenvironÚcopyÚpathseprÚ   r~   r‰   r   r   rä   r   r�   )
r-   rá   r»   r¸   rÿ   r  re   Zcmdtemplaterã   r  r(   r(   r)   r±   ½  s&   û
ûûÿ
ÿzCmdLineClientTests.executec                   sV   ‡ ‡fdd„}t  ˆ ¡ ¡‰ ˆjdtƒ ddˆ jddgd�}| ˆjd	¡ | |¡ |S )z4
        It can store logs to a local file.
        c                   s   ˆ   ¡ }ˆ d|¡ d S )Ns   Log opened.)Z
getContentZassertIn)rÜ   Z
logContent©ZlogPathr-   r(   r)   Úcb_check_logã  s   z<CmdLineClientTests.test_runWithLogFile.<locals>.cb_check_logr®   z--logz	--logfilez--host-key-algorithmszssh-rsa©rá   r»   rÿ   r¯   N)r   r   Úmktempr±   rJ   r‰   r²   r³   )r-   r
  rS   r(   r	  r)   Útest_runWithLogFileÞ  s   ûý
z&CmdLineClientTests.test_runWithLogFilec                 C  s"   | j dtƒ d�}| | jd¡ |S )zH
        Do not use --host-key-algorithms flag on command line.
        r®   )rá   r»   r¯   Nr°   rR   r(   r(   r)   Ú%test_runWithNoHostAlgorithmsSpecifiedù  s
   ÿz8CmdLineClientTests.test_runWithNoHostAlgorithmsSpecifiedc                 C  s   | j dtƒ dgd� | jd¡S )zS
        Simple smoke test for '--compress' flag to ensure we can connect.
        zecho compressedz
--compressr  s   compressed
Nr°   r,   r(   r(   r)   Ú test_runWithCompressionSpecified  s   ý
üz3CmdLineClientTests.test_runWithCompressionSpecifiedc                 C  s   t ƒ }d|_| jd|dd�S )z<
        Connecting to an invalid host should fail.
        r“   zecho nonfunctionalúnowhere.invalid©rá   r»   r  N)rJ   rN   r±   ©r-   ZexpectErrorr(   r(   r)   Útest_connectToInvalidHost  s   ýz,CmdLineClientTests.test_connectToInvalidHostc                 C  s   t ƒ }|  | jd|dd�t¡S )zm
        L{ConchTestOpenSSHProcess} fails with a L{ConchError} if an expectation
        is not met.
        zecho checkfailurer  r  N)rJ   ZassertFailurer±   r   r  r(   r(   r)   Útest_testFailure  s   ýúz#CmdLineClientTests.test_testFailure)r’   Nri   )rá   rv   r»   rJ   r¸   rv   rÿ   r   r  rv   rw   râ   )rw   râ   )r%   r&   r'   r/   r   ZplatformTyper7   r±   r  r  r  r  r  r(   r(   r(   r)   rý   µ  s    
ú!


rý   )rr   )rs   rt   ru   rv   rw   rx   )XÚ
__future__r   rˆ   r”   rè   r~   Ú	itertoolsr   Útypingr   Zzope.interfacer   Ztwisted.conch.errorr   Ztwisted.conch.test.keydatar   r   Ztwisted.conch.test.test_sshr
   Ztwisted.credr   Ztwisted.internetr   r   Ztwisted.internet.deferr   r   r   Ztwisted.internet.errorr   Ztwisted.internet.interfacesr   Ztwisted.internet.taskr   Ztwisted.pythonr   r   r   Ztwisted.python.filepathr   Ztwisted.python.procutilsr   Ztwisted.python.reflectr   Ztwisted.test.testutilsr   r   Ztwisted.trial.unittestr   r   r   r   ÚImportErrorr    Ztwisted.conch.avatarr!   Ztwisted.conch.ssh.sessionr"   r#   r$   Ztwisted.conch.interfacesZtwisted.conch.scripts.conchZ_StdioInteractingSessionÚer1   rv   r6   r*   r0   rÊ   r8   ÚFactoryrH   ZProcessProtocolrJ   rd   rj   r�   r‚   r­   r¿   rÕ   rØ   rß   ræ   rú   rü   rý   r(   r(   r(   r)   Ú<module>   s|   ÿ€ý0LL6D	4\