o
    Òó·i¹ƒ  ã                   @   s  U d 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mZmZ ddlmZ dd	lmZmZ dd
lmZmZ ddlmZ ddlmZ ddlmZ ddlmZ dZ ee e!d< edƒr„ddl"m#Z# ddl$m Z m%Z%m&Z& ddl'm(Z( ddl)m*Z* nG dd„ dƒZ%G dd„ dƒZ&G dd„ de&j+ƒZ,G dd„ de&j+ƒZ-G dd„ de&j+ƒZ.G d d!„ d!e%j/ƒZ0eeƒG d"d#„ d#ƒƒZ1eeƒG d$d%„ d%ƒƒZ2eeƒG d&d'„ d'ƒƒZ3eeƒG d(d)„ d)ƒƒZ4G d*d+„ d+ej5ƒZ6G d,d-„ d-ej5ƒZ7G d.d/„ d/ej5ƒZ8G d0d1„ d1ej5ƒZ9dS )2zT
Tests for the implementation of the ssh-userauth service.

Maintainer: Paul Swartz
é    )Ú
ModuleType)ÚOptional)Úimplementer)Ú
ConchErrorÚValidPublicKey)ÚICredentialsChecker)Ú
IAnonymousÚISSHPrivateKeyÚIUsernamePassword)ÚUnauthorizedLogin)ÚIRealmÚPortal)ÚdeferÚtask)ÚDeferred)Úloopback)ÚrequireModule)ÚunittestNÚkeysZcryptography)ÚSSHProtocolChecker)r   Ú	transportÚuserauth)ÚNS)Úkeydatac                   @   ó   e Zd ZG dd„ dƒZdS )r   c                   @   ó   e Zd ZdZdS )ztransport.SSHTransportBaseúQ
            A stub class so that later class definitions won't die.
            N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r"   r"   úfC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/conch/test/test_userauth.pyÚSSHTransportBase#   ó    r$   N)r   r   r    r$   r"   r"   r"   r#   r   "   ó    r   c                   @   r   )r   c                   @   r   )zuserauth.SSHUserAuthClientr   Nr   r"   r"   r"   r#   ÚSSHUserAuthClient)   r%   r'   N)r   r   r    r'   r"   r"   r"   r#   r   (   r&   r   c                   @   s2   e Zd ZdZdd„ Zdd„ Zddd„Zd	d
„ ZdS )ÚClientUserAuthz"
    A mock user auth client.
    c                 C   s(   | j r
tj tj¡S t tj tj¡¡S )z˜
        If this is the first time we've been called, return a blob for
        the DSA key.  Otherwise, return a blob
        for the RSA key.
        N)	ZlastPublicKeyr   ÚKeyÚ
fromStringr   ÚpublicRSA_opensshr   ÚsucceedÚpublicDSA_openssh©Úselfr"   r"   r#   ÚgetPublicKey4   s   zClientUserAuth.getPublicKeyc                 C   s   t  tj tj¡¡S )z@
        Return the private key object for the RSA key.
        N)r   r,   r   r)   r*   r   ÚprivateRSA_opensshr.   r"   r"   r#   ÚgetPrivateKey?   ó   zClientUserAuth.getPrivateKeyNc                 C   ó
   t  d¡S )z/
        Return 'foo' as the password.
        ó   fooN©r   r,   )r/   Úpromptr"   r"   r#   ÚgetPasswordE   ó   
zClientUserAuth.getPasswordc                 C   r4   )z>
        Return 'foo' as the answer to two questions.
        )Úfoor:   Nr6   )r/   ÚnameÚinformationZanswersr"   r"   r#   ÚgetGenericAnswersK   r9   z ClientUserAuth.getGenericAnswers©N)r   r   r    r!   r0   r2   r8   r=   r"   r"   r"   r#   r(   /   s    
r(   c                   @   ó    e Zd ZdZdd„ Zdd„ ZdS )ÚOldClientAuthz~
    The old SSHUserAuthClient returned a cryptography key object from
    getPrivateKey() and a string from getPublicKey
    c                 C   s   t  tj tj¡j¡S r>   )r   r,   r   r)   r*   r   r1   Z	keyObjectr.   r"   r"   r#   r2   X   ó   zOldClientAuth.getPrivateKeyc                 C   s   t j tj¡ ¡ S r>   )r   r)   r*   r   r+   Úblobr.   r"   r"   r#   r0   [   s   zOldClientAuth.getPublicKeyN©r   r   r    r!   r2   r0   r"   r"   r"   r#   r@   R   s    r@   c                   @   r?   )ÚClientAuthWithoutPrivateKeyzP
    This client doesn't have a private key, but it does have a public key.
    c                 C   ó   d S r>   r"   r.   r"   r"   r#   r2   d   ó   z)ClientAuthWithoutPrivateKey.getPrivateKeyc                 C   s   t j tj¡S r>   )r   r)   r*   r   r+   r.   r"   r"   r#   r0   g   ó   z(ClientAuthWithoutPrivateKey.getPublicKeyNrC   r"   r"   r"   r#   rD   _   s    rD   c                   @   sL   e Zd ZdZG dd„ dƒZG dd„ dƒZdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚFakeTransporta_  
    L{userauth.SSHUserAuthServer} expects an SSH transport which has a factory
    attribute which has a portal attribute. Because the portal is important for
    testing authentication, we need to be able to provide an interesting portal
    object to the L{SSHUserAuthServer}.

    In addition, we want to be able to capture any packets sent over the
    transport.

    @ivar packets: a list of 2-tuples: (messageType, data).  Each 2-tuple is
        a sent packet.
    @type packets: C{list}
    @param lostConnecion: True if loseConnection has been called on us.
    @type lostConnection: L{bool}
    c                   @   s   e Zd ZdZdZdd„ ZdS )zFakeTransport.ServicezW
        A mock service, representing the other service offered by the server.
        ó   nancyc                 C   rE   r>   r"   r.   r"   r"   r#   ÚserviceStartedƒ   rF   z$FakeTransport.Service.serviceStartedN)r   r   r    r!   r;   rJ   r"   r"   r"   r#   ÚService|   s    rK   c                   @   ó   e Zd ZdZdd„ ZdS )zFakeTransport.Factoryzg
        A mock factory, representing the factory that spawned this user auth
        service.
        c                 C   s   |dkrt jS dS )z2
            Return our fake service.
            ó   noneN)rH   rK   )r/   r   Úservicer"   r"   r#   Ú
getServiceŒ   s   ÿz FakeTransport.Factory.getServiceN)r   r   r    r!   rO   r"   r"   r"   r#   ÚFactory†   s    rP   c                 C   s(   |   ¡ | _|| j_d| _| | _g | _d S ©NF)rP   ÚfactoryÚportalÚlostConnectionr   Úpackets)r/   rS   r"   r"   r#   Ú__init__“   s
   

zFakeTransport.__init__c                 C   s   | j  ||f¡ dS )z8
        Record the packet sent by the service.
        N)rU   Úappend)r/   ZmessageTypeÚmessager"   r"   r#   Ú
sendPacketš   r3   zFakeTransport.sendPacketc                 C   ó   dS )z»
        Pretend that this transport encrypts traffic in both directions. The
        SSHUserAuthServer disables password authentication if the transport
        isn't encrypted.
        TNr"   )r/   Ú	directionr"   r"   r#   ÚisEncrypted    s   zFakeTransport.isEncryptedc                 C   s
   d| _ d S ©NT)rT   r.   r"   r"   r#   ÚloseConnection¨   s   
zFakeTransport.loseConnectionN)
r   r   r    r!   rK   rP   rV   rY   r\   r^   r"   r"   r"   r#   rH   k   s    
rH   c                   @   rL   )ÚRealmz¿
    A mock realm for testing L{userauth.SSHUserAuthServer}.

    This realm is not actually used in the course of testing, so it returns the
    simplest thing that could possibly work.
    c                 G   s   t  |d d dd„ f¡S )Nr   c                   S   rE   r>   r"   r"   r"   r"   r#   Ú<lambda>¶   ó    z%Realm.requestAvatar.<locals>.<lambda>r6   )r/   ZavatarIdZmindZ
interfacesr"   r"   r#   ÚrequestAvatarµ   s   zRealm.requestAvatarN)r   r   r    r!   rb   r"   r"   r"   r#   r_   ¬   s    r_   c                   @   ó   e Zd ZdZefZdd„ ZdS )ÚPasswordCheckerzŽ
    A very simple username/password checker which authenticates anyone whose
    password matches their username and rejects all others.
    c                 C   s&   |j |jkrt |j ¡S t tdƒ¡S )NzInvalid username/password pair)ÚusernameÚpasswordr   r,   Úfailr   )r/   Úcredsr"   r"   r#   ÚrequestAvatarIdÂ   s   zPasswordChecker.requestAvatarIdN)r   r   r    r!   r
   ÚcredentialInterfacesri   r"   r"   r"   r#   rd   ¹   ó    rd   c                   @   rc   )ÚPrivateKeyCheckerz•
    A very simple public key checker which authenticates anyone whose
    public/private keypair is the same keydata.public/privateRSA_openssh.
    c                 C   sX   |j tj tj¡  ¡ kr)|jd ur&tj |j ¡}| |j|j¡r#|j	S tƒ ‚t
ƒ ‚tƒ ‚r>   )rB   r   r)   r*   r   r+   Ú	signatureÚverifyÚsigDatare   r   r   )r/   rh   Úobjr"   r"   r#   ri   Ñ   s   
ÿz!PrivateKeyChecker.requestAvatarIdN)r   r   r    r!   r	   rj   ri   r"   r"   r"   r#   rl   È   rk   rl   c                   @   rc   )ÚAnonymousCheckerzI
    A simple checker which isn't supported by L{SSHUserAuthServer}.
    c                 C   rE   r>   r"   )r/   Úcredentialsr"   r"   r#   ri   ä   s   z AnonymousChecker.requestAvatarIdN)r   r   r    r!   r   rj   ri   r"   r"   r"   r#   rq   Ü   s    rq   c                   @   sÒ   e Zd ZdZedu rdZd.dd„Zdd„ Zd	eddf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d„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zded fd*d+„Zd,d-„ ZdS )/ÚSSHUserAuthServerTestsz&
    Tests for SSHUserAuthServer.
    Núcannot run without cryptographyÚreturnc                 C   sb   t ƒ | _t| jƒ| _| j tƒ ¡ | j tƒ ¡ t ¡ | _	t
| jƒ| j	_| j	 ¡  | j	j ¡  d S r>   )r_   Úrealmr   rS   ÚregisterCheckerrd   rl   r   ÚSSHUserAuthServerÚ
authServerrH   r   rJ   ÚsupportedAuthenticationsÚsortr.   r"   r"   r#   ÚsetUpñ   s   

zSSHUserAuthServerTests.setUpc                 C   ó   | j  ¡  d | _ d S r>   )ry   ÚserviceStoppedr.   r"   r"   r#   ÚtearDownü   ó   

zSSHUserAuthServerTests.tearDownÚignoredc                 C   s>   t | jjtƒs	J ‚| jjjd }|  |tjtdƒd f¡ dS )z;
        Check that the authentication has failed.
        éÿÿÿÿs   password,publickeyó    N)	Ú
isinstancery   r   rH   rU   ÚassertEqualr   ZMSG_USERAUTH_FAILUREr   )r/   r�   Z
lastPacketr"   r"   r#   Ú_checkFailed   s   þz#SSHUserAuthServerTests._checkFailedc                 C   s,   | j  tdƒtdƒ tdƒ ¡}| | j¡S )zÃ
        A client may request a list of authentication 'method name' values
        that may continue by using the "none" authentication 'method name'.

        See RFC 4252 Section 5.2.
        r5   s   servicerM   N)ry   Ússh_USERAUTH_REQUESTr   ÚaddCallbackr†   )r/   Údr"   r"   r#   Útest_noneAuthentication  s   ÿz.SSHUserAuthServerTests.test_noneAuthenticationc                    sF   d  tdƒtdƒtdƒdtdƒg¡}ˆ j |¡}‡ fdd„}| |¡S )	zÝ
        When provided with correct password authentication information, the
        server should respond by sending a MSG_USERAUTH_SUCCESS message with
        no other data.

        See RFC 4252, Section 5.1.
        ó    r5   rM   ó   passwordrƒ   c                    ó   ˆ   ˆ jjjtjdfg¡ d S ©Nr‹   ©r…   ry   r   rU   r   ZMSG_USERAUTH_SUCCESS©r�   r.   r"   r#   Úcheck"  ó   
þzKSSHUserAuthServerTests.test_successfulPasswordAuthentication.<locals>.checkN)Újoinr   ry   r‡   rˆ   ©r/   Úpacketr‰   r‘   r"   r.   r#   Ú%test_successfulPasswordAuthentication  s   $
z<SSHUserAuthServerTests.test_successfulPasswordAuthenticationc                 C   sh   d  tdƒtdƒtdƒdtdƒg¡}t ¡ | j_| j |¡}|  | jjj	g ¡ | jj 
d¡ | | j¡S )	a;  
        When provided with invalid authentication details, the server should
        respond by sending a MSG_USERAUTH_FAILURE message which states whether
        the authentication was partially successful, and provides other, open
        options for authentication.

        See RFC 4252, Section 5.1.
        r‹   r5   rM   rŒ   rƒ   ó   baré   N)r“   r   r   ÚClockry   Úclockr‡   r…   r   rU   Úadvancerˆ   r†   ©r/   r•   r‰   r"   r"   r#   Ú!test_failedPasswordAuthentication*  s   $
z8SSHUserAuthServerTests.test_failedPasswordAuthenticationc                    s¦   t j tj¡ ¡ }t j tj¡}tdƒtdƒ tdƒ d t| ¡ ƒ t|ƒ }dˆ j	j
_| tdƒttjfƒ | ¡}|t|ƒ7 }ˆ j	 |¡}‡ fdd„}| |¡S )	zN
        Test that private key authentication completes successfully,
        r5   rM   ó	   publickeyó   ÿó   testc                    r�   rŽ   r�   r�   r.   r"   r#   r‘   P  r’   zMSSHUserAuthServerTests.test_successfulPrivateKeyAuthentication.<locals>.checkN)r   r)   r*   r   r+   rB   r1   r   ZsshTypery   r   Ú	sessionIDÚsignÚbytesr   ÚMSG_USERAUTH_REQUESTr‡   rˆ   )r/   rB   rp   r•   rm   r‰   r‘   r"   r.   r#   Ú'test_successfulPrivateKeyAuthentication;  s,   ÿþý
üûÿ
ÿ
z>SSHUserAuthServerTests.test_successfulPrivateKeyAuthenticationc                    sŒ   t  ¡ ‰ dd„ }dd„ }‡ fdd„}|  | jd|¡ |  | jd|¡ |  | jd	|¡ td
ƒtdƒ tdƒ tdƒ }| j |¡ |  ˆ t¡S )z‹
        ssh_USERAUTH_REQUEST should raise a ConchError if tryAuth returns
        None. Added to catch a bug noticed by pyflakes.
        c                 S   s   |   d¡ d S )Nz&request should have raised ConochError)rg   )r/   r�   r"   r"   r#   ÚmockCbFinishedAuth_  rG   zOSSHUserAuthServerTests.test_requestRaisesConchError.<locals>.mockCbFinishedAuthc                 S   rE   r>   r"   )ÚkindÚuserÚdatar"   r"   r#   ÚmockTryAuthb  rF   zHSSHUserAuthServerTests.test_requestRaisesConchError.<locals>.mockTryAuthc                    s   ˆ   | j¡ d S r>   )ZerrbackÚvalue)Úreason©r‰   r"   r#   ÚmockEbBadAuthe  s   zJSSHUserAuthServerTests.test_requestRaisesConchError.<locals>.mockEbBadAuthÚtryAuthZ_cbFinishedAuthZ
_ebBadAuths   userrM   s
   public-keys   dataN)r   r   Úpatchry   r   r‡   ÚassertFailurer   )r/   r¦   rª   r®   r•   r"   r­   r#   Útest_requestRaisesConchErrorX  s    z3SSHUserAuthServerTests.test_requestRaisesConchErrorc                    sb   t j tj¡ ¡ ‰ tdƒtdƒ tdƒ d tdƒ tˆ ƒ }ˆj |¡}‡ ‡fdd„}| 	|¡S )	z@
        Test that verifying a valid private key works.
        r5   rM   rž   rƒ   ó   ssh-rsac                    s*   ˆ  ˆjjjtjtdƒtˆ ƒ fg¡ d S )Nr³   )r…   ry   r   rU   r   ÚMSG_USERAUTH_PK_OKr   r�   ©rB   r/   r"   r#   r‘   �  s   þz@SSHUserAuthServerTests.test_verifyValidPrivateKey.<locals>.checkN)
r   r)   r*   r   r+   rB   r   ry   r‡   rˆ   r”   r"   rµ   r#   Útest_verifyValidPrivateKeyr  s    ÿþýüûÿ
z1SSHUserAuthServerTests.test_verifyValidPrivateKeyc                 C   sV   t j tj¡ ¡ }tdƒtdƒ tdƒ d tdƒ t|ƒ }| j |¡}| 	| j
¡S )úd
        Test that private key authentication fails when the public key
        is invalid.
        r5   rM   rž   rƒ   s   ssh-dsaN©r   r)   r*   r   r-   rB   r   ry   r‡   rˆ   r†   ©r/   rB   r•   r‰   r"   r"   r#   Ú3test_failedPrivateKeyAuthenticationWithoutSignature‰  s   ÿþýüûÿzJSSHUserAuthServerTests.test_failedPrivateKeyAuthenticationWithoutSignaturec                 C   s|   t j tj¡ ¡ }t j tj¡}tdƒtdƒ tdƒ d tdƒ t|ƒ t| |¡ƒ }d| j	j
_| j	 |¡}| | j¡S )r·   r5   rM   rž   rŸ   r³   r    N)r   r)   r*   r   r+   rB   r1   r   r¢   ry   r   r¡   r‡   rˆ   r†   )r/   rB   rp   r•   r‰   r"   r"   r#   Ú0test_failedPrivateKeyAuthenticationWithSignatureš  s&   ÿþýüûúÿ
	zGSSHUserAuthServerTests.test_failedPrivateKeyAuthenticationWithSignaturec                 C   sj   t j tj¡ ¡ }tdƒ|dd…  }tdƒtdƒ tdƒ d tdƒ t|ƒ }| j |¡}| 	| j
¡S )	z€
        Private key authentication fails when the public key type is
        unsupported or the public key is corrupt.
        s   ssh-bad-typeé   Nr5   rM   rž   rƒ   r³   r¸   r¹   r"   r"   r#   Útest_unsupported_publickey®  s    ÿþýüûÿz1SSHUserAuthServerTests.test_unsupported_publickeyc                 C   sR   t  ¡ }t| jƒ|_| j tƒ ¡ | ¡  | ¡  |j	 
¡  |  |j	ddg¡ dS )ah  
        L{SSHUserAuthServer} sets up
        C{SSHUserAuthServer.supportedAuthentications} by checking the portal's
        credentials interfaces and mapping them to SSH authentication method
        strings.  If the Portal advertises an interface that
        L{SSHUserAuthServer} can't map, it should be ignored.  This is a white
        box test.
        rŒ   rž   N)r   rx   rH   rS   r   rw   rq   rJ   r~   rz   r{   r…   ©r/   Úserverr"   r"   r#   Ú test_ignoreUnknownCredInterfacesÄ  s   	
z7SSHUserAuthServerTests.test_ignoreUnknownCredInterfacesc                 C   s�   |   d| jj¡ t ¡ }t| jƒ|_dd„ |j_| 	¡  | 
¡  |  d|j¡ t ¡ }t| jƒ|_dd„ |j_| 	¡  | 
¡  |   d|j¡ dS )z�
        Test that the userauth service does not advertise password
        authentication if the password would be send in cleartext.
        rŒ   c                 S   rZ   rQ   r"   ©Úxr"   r"   r#   r`   Þ  ra   zISSHUserAuthServerTests.test_removePasswordIfUnencrypted.<locals>.<lambda>c                 S   ó   | dkS ©NÚinr"   rÁ   r"   r"   r#   r`   å  ó    N)ZassertInry   rz   r   rx   rH   rS   r   r\   rJ   r~   ZassertNotIn)r/   ÚclearAuthServerÚhalfAuthServerr"   r"   r#   Ú test_removePasswordIfUnencryptedÕ  s   z7SSHUserAuthServerTests.test_removePasswordIfUnencryptedc                 C   s–   t | jƒ}| tƒ ¡ t ¡ }t|ƒ|_dd„ |j_| 	¡  | 
¡  |  |jdg¡ t ¡ }t|ƒ|_dd„ |j_| 	¡  | 
¡  |  |jdg¡ dS )zÁ
        If the L{SSHUserAuthServer} is not advertising passwords, then an
        unencrypted connection should not cause any warnings or exceptions.
        This is a white box test.
        c                 S   rZ   rQ   r"   rÁ   r"   r"   r#   r`   ÷  ra   zSSSHUserAuthServerTests.test_unencryptedConnectionWithoutPasswords.<locals>.<lambda>rž   c                 S   rÃ   rÄ   r"   rÁ   r"   r"   r#   r`   ÿ  rÆ   N)r   rv   rw   rl   r   rx   rH   r   r\   rJ   r~   r…   rz   )r/   rS   rÇ   rÈ   r"   r"   r#   Ú*test_unencryptedConnectionWithoutPasswordsê  s   


zASSHUserAuthServerTests.test_unencryptedConnectionWithoutPasswordsc                 C   s€   t  ¡ }t ¡ |_t| jƒ|_| ¡  |j 	d¡ | 
¡  |  |jjtjdttjfƒ tdƒ tdƒ fg¡ |  |jj¡ dS )z0
        Test that the login times out.
        é°š  ó      s   you took too longr‹   N)r   rx   r   r™   rš   rH   rS   r   rJ   r›   r~   r…   rU   ÚMSG_DISCONNECTr£   Ú)DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLEr   Ú
assertTruerT   ©r/   ZtimeoutAuthServerr"   r"   r#   Útest_loginTimeout  s(   

ÿþýþÿþz(SSHUserAuthServerTests.test_loginTimeoutc                 C   s\   t  ¡ }t ¡ |_t| jƒ|_| ¡  | 	¡  |j 
d¡ |  |jjg ¡ |  |jj¡ dS )zN
        Test that stopping the service also stops the login timeout.
        rË   N)r   rx   r   r™   rš   rH   rS   r   rJ   r~   r›   r…   rU   ÚassertFalserT   rÐ   r"   r"   r#   Útest_cancelLoginTimeout  s   
z.SSHUserAuthServerTests.test_cancelLoginTimeoutc                    sn   d  tdƒtdƒtdƒdtdƒg¡}t ¡ ˆ j_tdƒD ]}ˆ j |¡}ˆ jj d¡ q‡ fd	d
„}| 	|¡S )zm
        Test that the server disconnects if the client fails authentication
        too many times.
        r‹   r5   rM   rŒ   rƒ   r—   é   r˜   c                    s<   ˆ   ˆ jjjd tjdttjfƒ tdƒ tdƒ f¡ d S )Nr‚   rÌ   s   too many bad authsr‹   )r…   ry   r   rU   rÍ   r£   rÎ   r   r�   r.   r"   r#   r‘   4  s   
ÿþýþþz:SSHUserAuthServerTests.test_tooManyAttempts.<locals>.checkN)
r“   r   r   r™   ry   rš   Úranger‡   r›   rˆ   )r/   r•   Úir‰   r‘   r"   r.   r#   Útest_tooManyAttempts)  s   $
z+SSHUserAuthServerTests.test_tooManyAttemptsc                 C   sT   t dƒt dƒ t dƒ d t dƒ }t ¡ | j_| j |¡}|dus$J ‚| | j¡S )zo
        If the user requests a service that we don't support, the
        authentication should fail.
        r5   r‹   rŒ   rƒ   N)r   r   r™   ry   rš   r‡   rˆ   r†   rœ   r"   r"   r#   Útest_failIfUnknownServiceB  s
   $z0SSHUserAuthServerTests.test_failIfUnknownServicec                    sV   dd„ }ˆ   ˆ jd|¡ ˆ   ˆ jdd¡ ‡ fdd„}ˆ j ddd¡}ˆ  |t¡ |¡S )	aZ  
        tryAuth() has two edge cases that are difficult to reach.

        1) an authentication method auth_* returns None instead of a Deferred.
        2) an authentication type that is defined does not have a matching
           auth_* method.

        Both these cases should return a Deferred which fails with a
        ConchError.
        c                 S   rE   r>   r"   )r•   r"   r"   r#   ÚmockAuthY  rF   z>SSHUserAuthServerTests.test_tryAuthEdgeCases.<locals>.mockAuthZauth_publickeyZauth_passwordNc                    s   ˆ j  dd d ¡}ˆ  |t¡S )NrŒ   )ry   r¯   r±   r   )r�   Zd2r.   r"   r#   Ú
secondTest_  s   z@SSHUserAuthServerTests.test_tryAuthEdgeCases.<locals>.secondTestrž   )r°   ry   r¯   r±   r   rˆ   )r/   rÙ   rÚ   Zd1r"   r.   r#   Útest_tryAuthEdgeCasesM  s   z,SSHUserAuthServerTests.test_tryAuthEdgeCases)ru   N)r   r   r    r!   r   Úskipr|   r   Úobjectr†   rŠ   r–   r�   r¥   r²   r¶   rº   r»   r½   rÀ   rÉ   rÊ   rÑ   rÓ   r×   r   rØ   rÛ   r"   r"   r"   r#   rs   é   s0    
rs   c                   @   s”   e Zd ZdZedu r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d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ ZdS )"ÚSSHUserAuthClientTestsz&
    Tests for SSHUserAuthClient.
    Nrt   c                 C   s4   t dt ¡ ƒ| _td ƒ| j_d| jj_| j ¡  d S )Nr5   r    )r(   rH   rK   Ú
authClientr   r¡   rJ   r.   r"   r"   r#   r|   o  s   
zSSHUserAuthClientTests.setUpc                 C   r}   r>   )rß   r~   r.   r"   r"   r#   r   u  r€   zSSHUserAuthClientTests.tearDownc                 C   sT   |   | jjd¡ |   | jjjd¡ |   | jjjtjt	dƒt	dƒ t	dƒ fg¡ dS )z;
        Test that client is initialized properly.
        r5   rI   rM   N)
r…   rß   r¨   Úinstancer;   r   rU   r   r¤   r   r.   r"   r"   r#   Ú	test_inity  s   þz SSHUserAuthClientTests.test_initc                    s@   dg‰ ‡ fdd„}|| j j_| j  d¡ |  ˆ d | j j¡ dS )z9
        Test that the client succeeds properly.
        Nc                    s   | ˆ d< d S )Nr   r"   )rN   ©rà   r"   r#   ÚstubSetServiceŠ  s   zDSSHUserAuthClientTests.test_USERAUTH_SUCCESS.<locals>.stubSetServicer‹   r   )rß   r   Z
setServiceZssh_USERAUTH_SUCCESSr…   rà   )r/   rã   r"   râ   r#   Útest_USERAUTH_SUCCESS„  s
   
z,SSHUserAuthClientTests.test_USERAUTH_SUCCESSc              	   C   s˜  | j  tdƒd ¡ |  | j jjd tjtdƒtdƒ tdƒ d tdƒ ttj	 
tj¡ ¡ ƒ f¡ | j  tdƒd ¡ ttj	 
tj¡ ¡ ƒ}|  | j jjd tjtdƒtdƒ tdƒ d tdƒ | f¡ | j  tdƒttj	 
tj¡ ¡ ƒ ¡ t| j jjƒttjfƒ tdƒ tdƒ tdƒ d tdƒ | }tj	 
tj¡}|  | j jjd tjtdƒtdƒ tdƒ d tdƒ | t| |¡ƒ f¡ d	S )
zJ
        Test that the client can authenticate with a public key.
        rž   rƒ   r‚   r5   rI   s   ssh-dssr³   ó   N)rß   Ússh_USERAUTH_FAILUREr   r…   r   rU   r   r¤   r   r)   r*   r   r-   rB   r+   Ússh_USERAUTH_PK_OKr¡   r£   r1   r¢   )r/   rB   ro   rp   r"   r"   r#   Útest_publickey‘  s’   ÿþýüûþþÿþýüûýþÿ
ÿþýüûúùÿ
ÿþýüûúþþz%SSHUserAuthClientTests.test_publickeyc                 C   sz   t dt ¡ ƒ}tdƒ|_d|j_| ¡  | d¡ g |j_|  | 	d¡¡ |  
|jjtjtdƒtdƒ tdƒ fg¡ dS )z¼
        If the SSHUserAuthClient doesn't return anything from signData,
        the client should start the authentication over again by requesting
        'none' authentication.
        r5   Nr    rž   r‹   rI   rM   )rD   rH   rK   r   r¡   rJ   r¯   rU   ÚassertIsNonerç   r…   r   r¤   r   )r/   rß   r"   r"   r#   Ú!test_publickey_without_privatekeyÏ  s   

þz8SSHUserAuthClientTests.test_publickey_without_privatekeyc                    s.   dd„ ˆ j _ˆ j  d¡}‡ fdd„}| |¡S )z{
        If there's no public key, auth_publickey should return a Deferred
        called back with a False value.
        c                 S   rE   r>   r"   rÁ   r"   r"   r#   r`   ç  ra   z:SSHUserAuthClientTests.test_no_publickey.<locals>.<lambda>rž   c                    s   ˆ   | ¡ d S r>   )rÒ   ©Úresultr.   r"   r#   r‘   ê  rG   z7SSHUserAuthClientTests.test_no_publickey.<locals>.checkN)rß   r0   r¯   rˆ   )r/   r‰   r‘   r"   r.   r#   Útest_no_publickeyâ  s   
z(SSHUserAuthClientTests.test_no_publickeyc                 C   s¬   | j  tdƒd ¡ |  | j jjd tjtdƒtdƒ tdƒ d tdƒ f¡ | j  tdƒtdƒ ¡ |  | j jjd tjtdƒtdƒ tdƒ d tdƒd  f¡ d	S )
zx
        Test that the client can authentication with a password.  This
        includes changing the password.
        rŒ   rƒ   r‚   r5   rI   r‹   rŸ   r˜   N)	rß   ræ   r   r…   r   rU   r   r¤   rç   r.   r"   r"   r#   Útest_passwordï  s   "þþ&þþz$SSHUserAuthClientTests.test_passwordc                 C   s"   dd„ | j _|  | j  d¡¡ dS )zK
        If getPassword returns None, tryAuth should return False.
        c                   S   rE   r>   r"   r"   r"   r"   r#   r`   	  ra   z9SSHUserAuthClientTests.test_no_password.<locals>.<lambda>rŒ   N)rß   r8   rÒ   r¯   r.   r"   r"   r#   Útest_no_password  s   z'SSHUserAuthClientTests.test_no_passwordc                 C   s`   | j  tdƒtdƒ tdƒ d tdƒ d ¡ |  | j jjd tjdtdƒ tdƒ f¡ dS )	zj
        Make sure that the client can authenticate with the keyboard
        interactive method.
        r‹   s      s
   Password: rƒ   r‚   s      r5   N)rß   Z'ssh_USERAUTH_PK_OK_keyboard_interactiver   r…   r   rU   r   ZMSG_USERAUTH_INFO_RESPONSEr.   r"   r"   r#   Útest_keyboardInteractive  s&   ÿþýüûÿþþz/SSHUserAuthClientTests.test_keyboardInteractivec                 C   sP   d| j _g | j j_| j  d¡ |  | j jjtjtdƒtdƒ tdƒ fg¡ dS )z¾
        If C{SSHUserAuthClient} gets a MSG_USERAUTH_PK_OK packet when it's not
        expecting it, it should fail the current authentication and move on to
        the next type.
        s   unknownr‹   r5   rI   rM   N)	rß   ZlastAuthr   rU   rç   r…   r   r¤   r   r.   r"   r"   r#   Ú"test_USERAUTH_PK_OK_unknown_method!  s   
þz9SSHUserAuthClientTests.test_USERAUTH_PK_OK_unknown_methodc                    s®   ‡ fdd„}‡ fdd„}|ˆ j _|ˆ j _ˆ j  tdƒd ¡ ˆ  ˆ j jjd tj	tdƒtd	ƒ td
ƒ d tdƒ f¡ ˆ j  tdƒd ¡ ˆ  ˆ j jjdd… ddg¡ dS )z×
        ssh_USERAUTH_FAILURE should sort the methods by their position
        in SSHUserAuthClient.preferredOrder.  Methods that are not in
        preferredOrder should be sorted at the end of that list.
        c                      s   ˆ j j dd¡ d S )Néÿ   ó   here is data©rß   r   rY   r"   r.   r"   r#   Úauth_firstmethod6  s   zNSSHUserAuthClientTests.test_USERAUTH_FAILURE_sorting.<locals>.auth_firstmethodc                      s   ˆ j j dd¡ dS )Néþ   ó
   other dataTrô   r"   r.   r"   r#   Úauth_anothermethod9  s   zPSSHUserAuthClientTests.test_USERAUTH_FAILURE_sorting.<locals>.auth_anothermethods   anothermethod,passwordrƒ   r‚   r5   rI   rŒ   s"   firstmethod,anothermethod,passwordrŸ   éþÿÿÿN)rò   ró   )rö   r÷   )
rß   rõ   rø   ræ   r   r…   r   rU   r   r¤   )r/   rõ   rø   r"   r.   r#   Útest_USERAUTH_FAILURE_sorting/  s$   "þþ
ÿþz4SSHUserAuthClientTests.test_USERAUTH_FAILURE_sortingc                 C   sT   | j  tdƒd ¡ | j  tdƒd ¡ |  | j jjd tjdtdƒ d f¡ dS )	z»
        If there are no more available user authentication messages,
        the SSHUserAuthClient should disconnect with code
        DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE.
        rŒ   rƒ   rŸ   r‚   s      s(   no more authentication methods availables       N)rß   ræ   r   r…   r   rU   rÍ   r.   r"   r"   r#   Ú%test_disconnectIfNoMoreAuthenticationS  s   ÿþþþz<SSHUserAuthClientTests.test_disconnectIfNoMoreAuthenticationc                 C   sH   g | j j_| j  d¡ |  | j jjtjtdƒtdƒ tdƒ fg¡ dS )zˆ
        _ebAuth (the generic authentication error handler) should send
        a request for the 'none' authentication method.
        Nr5   rI   rM   )rß   r   rU   Z_ebAuthr…   r   r¤   r   r.   r"   r"   r#   Útest_ebAuthe  s   
þz"SSHUserAuthClientTests.test_ebAuthc                    s`   t  dt ¡ ¡‰ ˆ ˆ  ¡ ¡ ‡ ‡‡fdd„}‡ ‡‡fdd„‰dd„ ‰ˆ  ¡ }| ˆj¡ 	|¡S )	zÛ
        getPublicKey() should return None.  getPrivateKey() should return a
        failed Deferred.  getPassword() should return a failed Deferred.
        getGenericAnswers() should return a failed Deferred.
        r5   c                    s$   |   t¡ ˆ  ¡ }| ˆj¡ ˆ¡S r>   )ÚtrapÚNotImplementedErrorr8   rˆ   rg   Ú
addErrback©rì   r‰   )rß   Úcheck2r/   r"   r#   r‘   z  s   
z3SSHUserAuthClientTests.test_defaults.<locals>.checkc                    s*   |   t¡ ˆ  d d d ¡}| ˆj¡ ˆ¡S r>   )rý   rþ   r=   rˆ   rg   rÿ   r   )rß   Úcheck3r/   r"   r#   r    s   
z4SSHUserAuthClientTests.test_defaults.<locals>.check2c                 S   s   |   t¡ d S r>   )rý   rþ   rë   r"   r"   r#   r  „  rG   z4SSHUserAuthClientTests.test_defaults.<locals>.check3N)
r   r'   rH   rK   ré   r0   r2   rˆ   rg   rÿ   )r/   r‘   r‰   r"   )rß   r  r  r/   r#   Útest_defaultsq  s   z$SSHUserAuthClientTests.test_defaults)r   r   r    r!   r   rÜ   r|   r   rá   rä   rè   rê   rí   rî   rï   rð   rñ   rú   rû   rü   r  r"   r"   r"   r#   rÞ   g  s&    >$rÞ   c                   @   s.   e Zd Zedu r
dZG dd„ dƒZdd„ ZdS )ÚLoopbackTestsNrt   c                   @   s"   e Zd ZG dd„ dƒZdd„ ZdS )zLoopbackTests.Factoryc                   @   r?   )zLoopbackTests.Factory.Serviceó   TestServicec                 C   s   | j  ¡  d S r>   )r   r^   r.   r"   r"   r#   rJ   “  rG   z,LoopbackTests.Factory.Service.serviceStartedc                 C   rE   r>   r"   r.   r"   r"   r#   r~   –  rF   z,LoopbackTests.Factory.Service.serviceStoppedN)r   r   r    r;   rJ   r~   r"   r"   r"   r#   rK   �  s    rK   c                 C   s   | j S r>   )rK   )r/   Zavatarr;   r"   r"   r#   rO   ™  s   z LoopbackTests.Factory.getServiceN)r   r   r    rK   rO   r"   r"   r"   r#   rP   �  s    	rP   c                    s   t  ¡ ‰tdˆj ¡ ƒ}t ¡ ˆ_ˆˆj_dd„ ˆj_t ¡ |_||j_d ˆj_	|j_	dd„  ˆj_
|j_
ˆ ¡ ˆj_dˆ_tƒ }t|ƒ}tƒ ‰ ˆ  tƒ ¡ ˆ  tƒ ¡ ‡ fdd„ˆ _| ˆ ¡ |ˆjj_t ˆj|j¡}dd„ ˆjj_d	d„ |jj_ˆ ¡  | ¡  ‡‡fd
d„}| |¡S )zW
        Test that the userauth server and client play nicely with each other.
        r5   c                 S   rZ   r]   r"   rÁ   r"   r"   r#   r`   ¦  ra   z-LoopbackTests.test_loopback.<locals>.<lambda>r‹   c                   S   rE   r>   r"   r"   r"   r"   r#   r`   «  ra   r   c                    s   t ˆ j|  ƒdkS )Nr˜   )ÚlenZsuccessfulCredentials)ZaId)Úcheckerr"   r#   r`   µ  s    c                   S   rZ   )NZ_ServerLoopbackr"   r"   r"   r"   r#   r`   º  ra   c                   S   rZ   )NZ_ClientLoopbackr"   r"   r"   r"   r#   r`   »  ra   c                    s   ˆ   ˆjjjd¡ d S )Nr  )r…   r   rN   r;   r�   r¾   r"   r#   r‘   À  rA   z*LoopbackTests.test_loopback.<locals>.checkN)r   rx   r(   rP   rK   r   r$   rN   r\   r¡   ZsendKexInitrR   ZpasswordDelayr_   r   r   rw   rd   rl   ZareDonerS   r   ZloopbackAsyncZ	logPrefixrJ   rˆ   )r/   Úclientrv   rS   r‰   r‘   r"   )r  r/   r¿   r#   Útest_loopbackœ  s4   




zLoopbackTests.test_loopback)r   r   r    r   rÜ   rP   r	  r"   r"   r"   r#   r  ‹  s
    r  c                   @   s    e Zd Zedu r
dZdd„ ZdS )ÚModuleInitializationTestsNrt   c                 C   s,   |   tjjd d¡ |   tjjd d¡ d S )Né<   r´   )r…   r   rx   ZprotocolMessagesr'   r.   r"   r"   r#   Útest_messagesÊ  s   ÿÿz'ModuleInitializationTests.test_messages)r   r   r    r   rÜ   r  r"   r"   r"   r#   r
  Æ  s    r
  ):r!   Útypesr   Útypingr   Zzope.interfacer   Ztwisted.conch.errorr   r   Ztwisted.cred.checkersr   Ztwisted.cred.credentialsr   r	   r
   Ztwisted.cred.errorr   Ztwisted.cred.portalr   r   Ztwisted.internetr   r   Ztwisted.internet.deferr   Ztwisted.protocolsr   Ztwisted.python.reflectr   Ztwisted.trialr   r   Ú__annotations__Ztwisted.conch.checkersr   Ztwisted.conch.sshr   r   Ztwisted.conch.ssh.commonr   Ztwisted.conch.testr   r'   r(   r@   rD   r$   rH   r_   rd   rl   rq   ZTestCasers   rÞ   r  r
  r"   r"   r"   r#   Ú<module>   sV   #A     &;