o
    ¯blz  ã                   @   sø  d Z zddlZW n ey   dZY nw d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 dd
lmZmZ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mZ ddlmZ ddl m!Z! ddl"m#Z# ddl$m%Z% e!dƒre!dƒrdZ&ddl'm(Z( ddl)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/ ndZ&e0eddƒdu rªdZ1ndZ1G dd„ de%ƒZ2G dd„ de%ƒZ3G d d!„ d!e%ƒZ4G d"d#„ d#e%ƒZ5G d$d%„ d%e%ƒZ6G d&d'„ d'e%ƒZ7G d(d)„ d)e%ƒZ8ed*d+gƒZ9G d,d-„ d-e:ƒZ;G d.d/„ d/e%ƒZ<dS )0z&
Tests for L{twisted.conch.checkers}.
é    Nzcannot run without crypt moduleÚ )Úencodebytes)Ú
namedtuple)ÚBytesIO)ÚverifyObject)Ú'InMemoryUsernamePasswordDatabaseDontUse)ÚISSHPrivateKeyÚIUsernamePasswordÚSSHPrivateKeyÚUsernamePassword)ÚUnauthorizedLoginÚUnhandledCredentials)Úutil)ÚFailure)ÚShadowDatabaseÚUserDatabase)ÚFilePath)ÚrequireModule)ÚMockOS)ÚTestCaseÚcryptographyÚpyasn1)Úcheckers)ÚNotEnoughAuthenticationÚValidPublicKey)Úkeys)Úkeydataz)can't run without cryptography and PyASN1Úgeteuidz0Cannot run without effective UIDs (questionable)c                   @   sX   e Zd ZdZep	e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 )ÚHelperTestszl
    Tests for helper functions L{verifyCryptedPassword}, L{_pwdGetByName} and
    L{_shadowGetByName}.
    c                 C   s   t ƒ | _d S ©N)r   Úmockos©Úself© r#   úB/usr/lib/python3/dist-packages/twisted/conch/test/test_checkers.pyÚsetUp>   s   zHelperTests.setUpc                 C   ó4   d}d}t   ||¡}|  t ||¡d ||¡¡ dS )z–
        L{verifyCryptedPassword} returns C{True} if the plaintext password
        passed to it matches the encrypted password passed to it.
        úsecret stringÚsaltyz5{!r} supposed to be valid encrypted password for {!r}N©ÚcryptÚ
assertTruer   ÚverifyCryptedPasswordÚformat©r"   ÚpasswordÚsaltÚcryptedr#   r#   r$   Útest_verifyCryptedPasswordA   ó   
ÿþz&HelperTests.test_verifyCryptedPasswordc                 C   r&   )zŠ
        L{verifyCryptedPassword} returns True if the provided cleartext password
        matches the provided MD5 password hash.
        r/   z$1$saltz3{!r} supposed to be valid encrypted password for {}Nr)   r.   r#   r#   r$   Útest_verifyCryptedPasswordMD5P   r3   z)HelperTests.test_verifyCryptedPasswordMD5c                 C   s4   d}d}t   ||¡}|  t ||¡d ||¡¡ dS )zž
        L{verifyCryptedPassword} returns C{False} if the plaintext password
        passed to it does not match the encrypted password passed to it.
        zstring secretr'   z7{!r} not supposed to be valid encrypted password for {}N)r*   ÚassertFalser   r,   r-   )r"   r/   Úwrongr1   r#   r#   r$   Útest_refuteCryptedPassword_   r3   z&HelperTests.test_refuteCryptedPasswordc              	   C   sF   t ƒ }| ddddddd¡ |  td|¡ |  t d¡| d¡¡ d	S )
z‡
        L{_pwdGetByName} returns a tuple of items from the UNIX /etc/passwd
        database if the L{pwd} module is present.
        ÚaliceÚsecrité   é   z
first lastú/fooú/bin/shÚpwdN)r   ÚaddUserÚpatchr   ÚassertEqualÚ_pwdGetByNameÚgetpwnam©r"   Úuserdbr#   r#   r$   Útest_pwdGetByNamen   s   zHelperTests.test_pwdGetByNamec                 C   s"   |   tdd¡ |  t d¡¡ dS )zW
        If the C{pwd} module isn't present, L{_pwdGetByName} returns L{None}.
        r>   Nr8   )r@   r   ÚassertIsNonerB   r!   r#   r#   r$   Útest_pwdGetByNameWithoutPwdx   s   z'HelperTests.test_pwdGetByNameWithoutPwdc                 C   s’   t ƒ }| ddddddddd	¡	 |  td
|¡ d| j_d| j_|  td| j¡ |  t 	d¡| 
d¡¡ |  | jjddg¡ |  | jjddg¡ dS )z„
        L{_shadowGetByName} returns a tuple of items from the UNIX /etc/shadow
        database if the L{spwd} is present.
        ÚbobÚ
passphraser:   r;   é   é   é   é   é   Úspwdé)	  éÒ  Úosr   N)r   r?   r@   r   r    ÚeuidÚegidr   rA   Ú_shadowGetByNameÚgetspnamÚseteuidCallsÚsetegidCallsrD   r#   r#   r$   Útest_shadowGetByName   s   z HelperTests.test_shadowGetByNamec                 C   sB   |   tdd¡ |  t d¡¡ |  | jjg ¡ |  | jjg ¡ dS )zP
        L{_shadowGetByName} returns L{None} if C{spwd} is not present.
        rP   NrI   )r@   r   rG   rV   rA   r    rX   rY   r!   r#   r#   r$   Útest_shadowGetByNameWithoutSpwd   s   z+HelperTests.test_shadowGetByNameWithoutSpwdN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	cryptSkipÚdependencySkipÚskipr%   r2   r4   r7   rF   rH   rZ   r[   r#   r#   r#   r$   r   6   s    
r   c                   @   sp   e Zd ZdZep	e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 )ÚSSHPublicKeyDatabaseTestsz,
    Tests for L{SSHPublicKeyDatabase}.
    c              	   C   s´   t  ¡ | _tdƒ| _tdƒ| _d| j d | j d | _tƒ | _t	|  
¡ ƒ| j_| jj ¡  |  td| j¡ | jj d¡| _| j ¡  tƒ }| dd	d
dd| jjjd¡ || j_d S )Nó   foobaró   eggspams   t1 s    foo
t2 s    egg
rS   ú.sshó   useró   passwordr:   r;   s
   first lastó
   /bin/shell)r   ÚSSHPublicKeyDatabaseÚcheckerr   Úkey1Úkey2Úcontentr   r    r   ÚmktempÚpathÚmakedirsr@   r   ÚchildÚsshDirr   r?   Ú_userdbrD   r#   r#   r$   r%   ¢   s*   



ù	zSSHPublicKeyDatabaseTests.setUpc                 C   sL   | j | jgd}|  |d d t¡ |  |d d d¡ |  t|ƒd¡ dS )zJ
        L{SSHPublicKeyDatabase} is deprecated as of version 15.0
        )ÚoffendingFunctionsr   ÚcategoryÚmessagezÜtwisted.conch.checkers.SSHPublicKeyDatabase was deprecated in Twisted 15.0.0: Please use twisted.conch.checkers.SSHPublicKeyChecker, initialized with an instance of twisted.conch.checkers.UNIXAuthorizedKeysFiles instead.r:   N)ÚflushWarningsr%   rA   ÚDeprecationWarningÚlen)r"   ÚwarningsShownr#   r#   r$   Útest_deprecated»   s   
þz)SSHPublicKeyDatabaseTests.test_deprecatedc                 C   sj   | j  |¡ | j¡ tddƒ}d|_|  | j |¡¡ d|_|  | j |¡¡ d|_|  	| j |¡¡ d S )Nrg   rh   rd   re   s
   notallowed)
rs   rr   Ú
setContentrn   r   Úblobr+   rk   ÚcheckKeyr5   )r"   ÚfilenameÚuserr#   r#   r$   Ú_testCheckKeyË   s   
z'SSHPublicKeyDatabaseTests._testCheckKeyc                 C   ó.   |   d¡ |  | jjg ¡ |  | jjg ¡ dS )z˜
        L{SSHPublicKeyDatabase.checkKey} should retrieve the content of the
        authorized_keys file and check the keys against that file.
        Úauthorized_keysN©r‚   rA   r    rX   rY   r!   r#   r#   r$   Útest_checkKeyÕ   ó   
z'SSHPublicKeyDatabaseTests.test_checkKeyc                 C   rƒ   )z™
        L{SSHPublicKeyDatabase.checkKey} should retrieve the content of the
        authorized_keys2 file and check the keys against that file.
        Úauthorized_keys2Nr…   r!   r#   r#   r$   Útest_checkKey2Þ   r‡   z(SSHPublicKeyDatabaseTests.test_checkKey2c                    sÄ   | j  d¡‰ ˆ  | j¡ ˆ  d¡ |  ˆ jd¡ | jj‰‡ ‡fdd„}d| j_d| j_	|  
| jd|¡ |  
td	| j¡ td
dƒ}d|_|  | j |¡¡ |  | jjg d¢¡ |  | jjddg¡ dS )z˜
        If the key file is readable, L{SSHPublicKeyDatabase.checkKey} should
        switch its uid/gid to the ones of the authenticated user.
        r„   r   éÿ  c                    s   ˆ   d¡ ˆ| ƒS )NrŠ   )Úchmod)rT   ©ÚkeyFileÚsavedSeteuidr#   r$   Úseteuidô   s   
z>SSHPublicKeyDatabaseTests.test_checkKeyAsRoot.<locals>.seteuidrQ   rR   r   rS   rg   rh   rd   )r   r:   r   rQ   r;   N)rs   rr   r}   rn   r‹   Ú
addCleanupr    r   rT   rU   r@   r   r   r~   r+   rk   r   rA   rX   rY   )r"   r   r   r#   rŒ   r$   Útest_checkKeyAsRootç   s   

z-SSHPublicKeyDatabaseTests.test_checkKeyAsRootc                    s\   dd„ }ˆ   ˆ jd|¡ tddtjdtj tj¡ 	d¡ƒ}ˆ j 
|¡}‡ fdd„}| |¡S )	z
        L{SSHPublicKeyDatabase.requestAvatarId} should return the avatar id
        passed in if its C{_checkKey} method returns True.
        c                 S   ó   dS ©NTr#   ©Úignoredr#   r#   r$   Ú	_checkKey  ó   zASSHPublicKeyDatabaseTests.test_requestAvatarId.<locals>._checkKeyr   ó   testó   ssh-rsaó   fooc                    ó   ˆ   | d¡ d S ©Nr˜   ©rA   ©ÚavatarIdr!   r#   r$   Ú_verify  ó   z?SSHPublicKeyDatabaseTests.test_requestAvatarId.<locals>._verify)r@   rk   r
   r   ÚpublicRSA_opensshr   ÚKeyÚ
fromStringÚprivateRSA_opensshÚsignÚrequestAvatarIdÚaddCallback)r"   r–   ÚcredentialsÚdr    r#   r!   r$   Útest_requestAvatarId  s   û
z.SSHPublicKeyDatabaseTests.test_requestAvatarIdc                 C   sB   dd„ }|   | jd|¡ tddtjddƒ}| j |¡}|  |t¡S )a(  
        L{SSHPublicKeyDatabase.requestAvatarId} should raise L{ValidPublicKey}
        if the credentials represent a valid key without a signature.  This
        tells the user that the key is valid for login, but does not actually
        allow that user to do so without a signature.
        c                 S   r’   r“   r#   r”   r#   r#   r$   r–   "  r—   zQSSHPublicKeyDatabaseTests.test_requestAvatarIdWithoutSignature.<locals>._checkKeyr   r˜   r™   N)r@   rk   r
   r   r¢   r§   ÚassertFailurer   ©r"   r–   r©   rª   r#   r#   r$   Ú$test_requestAvatarIdWithoutSignature  s   ÿz>SSHPublicKeyDatabaseTests.test_requestAvatarIdWithoutSignaturec                 C   s0   dd„ }|   | jd|¡ | j d¡}|  |t¡S )z…
        If L{SSHPublicKeyDatabase.checkKey} returns False,
        C{_cbRequestAvatarId} should raise L{UnauthorizedLogin}.
        c                 S   r’   ©NFr#   r”   r#   r#   r$   r–   2  r—   zKSSHPublicKeyDatabaseTests.test_requestAvatarIdInvalidKey.<locals>._checkKeyr   N)r@   rk   r§   r¬   r   )r"   r–   rª   r#   r#   r$   Útest_requestAvatarIdInvalidKey,  s   z8SSHPublicKeyDatabaseTests.test_requestAvatarIdInvalidKeyc                 C   sR   dd„ }|   | jd|¡ tddtjdtj tj¡ 	d¡ƒ}| j 
|¡}|  |t¡S )z¡
        Valid keys with invalid signatures should cause
        L{SSHPublicKeyDatabase.requestAvatarId} to return a {UnauthorizedLogin}
        failure
        c                 S   r’   r“   r#   r”   r#   r#   r$   r–   @  r—   zQSSHPublicKeyDatabaseTests.test_requestAvatarIdInvalidSignature.<locals>._checkKeyr   r˜   r™   rš   )r@   rk   r
   r   r¢   r   r£   r¤   ÚprivateDSA_opensshr¦   r§   r¬   r   r­   r#   r#   r$   Ú$test_requestAvatarIdInvalidSignature9  s   ûz>SSHPublicKeyDatabaseTests.test_requestAvatarIdInvalidSignaturec                    sV   dd„ }ˆ   ˆ jd|¡ tdddddƒ}ˆ j |¡}‡ fd	d
„}| |¡ ˆ  |t¡S )z~
        Exceptions raised while verifying the key should be normalized into an
        C{UnauthorizedLogin} failure.
        c                 S   r’   r“   r#   r”   r#   r#   r$   r–   T  r—   zSSSHPublicKeyDatabaseTests.test_requestAvatarIdNormalizeException.<locals>._checkKeyr   r˜   Ns   blobs   sigDatas   sigc                    s    ˆ   tj¡}ˆ  t|ƒd¡ | S )Nr:   )ÚflushLoggedErrorsr   ÚBadKeyErrorrA   rz   )ÚfailureÚerrorsr!   r#   r$   Ú_verifyLoggedException[  ó   z`SSHPublicKeyDatabaseTests.test_requestAvatarIdNormalizeException.<locals>._verifyLoggedException)r@   rk   r
   r§   Ú
addErrbackr¬   r   )r"   r–   r©   rª   r·   r#   r!   r$   Ú&test_requestAvatarIdNormalizeExceptionN  s   
z@SSHPublicKeyDatabaseTests.test_requestAvatarIdNormalizeExceptionN)r\   r]   r^   r_   ÚeuidSkipra   rb   r%   r|   r‚   r†   r‰   r‘   r«   r®   r°   r²   rº   r#   r#   r#   r$   rc   ›   s    
		rc   c                   @   óD   e Zd ZdZeZdd„ Zdd„ Zdd„ Zdd	„ Z	d
d„ Z
dd„ ZdS )ÚSSHProtocolCheckerTestsz*
    Tests for L{SSHProtocolChecker}.
    c                 C   sL   t  ¡ }|  |jg ¡ | t  ¡ ¡ |  |jtg¡ |  |j t t j¡ dS )z€
        L{SSHProcotolChecker.registerChecker} should add the given checker to
        the list of registered checkers.
        N)r   ÚSSHProtocolCheckerrA   ÚcredentialInterfacesÚregisterCheckerrj   r   ÚassertIsInstance©r"   rk   r#   r#   r$   Útest_registerCheckerk  s   ÿÿz,SSHProtocolCheckerTests.test_registerCheckerc                 C   sN   t  ¡ }|  |jg ¡ | t  ¡ t¡ |  |jtg¡ |  |j t t j¡ dS )zÙ
        If a specific interface is passed into
        L{SSHProtocolChecker.registerChecker}, that interface should be
        registered instead of what the checker specifies in
        credentialIntefaces.
        N)r   r¾   rA   r¿   rÀ   rj   r	   rÁ   rÂ   r#   r#   r$   Ú!test_registerCheckerWithInterfacez  s   ÿz9SSHProtocolCheckerTests.test_registerCheckerWithInterfacec                    sJ   t  ¡ }tƒ }| dd¡ | |¡ | tddƒ¡}‡ fdd„}| |¡S )z†
        L{SSHProtocolChecker.requestAvatarId} should defer to one if its
        registered checkers to authenticate a user.
        r˜   c                    r›   rœ   r   rž   r!   r#   r$   Ú	_callback”  r¡   z?SSHProtocolCheckerTests.test_requestAvatarId.<locals>._callback)r   r¾   r   r?   rÀ   r§   r   r¨   )r"   rk   ÚpasswordDatabaserª   rÅ   r#   r!   r$   r«   ‰  s   

z,SSHProtocolCheckerTests.test_requestAvatarIdc                 C   sV   t  ¡ }dd„ }|  |d|¡ tƒ }| dd¡ | |¡ | tddƒ¡}|  |t	¡S )zÂ
        If the client indicates that it is never satisfied, by always returning
        False from _areDone, then L{SSHProtocolChecker} should raise
        L{NotEnoughAuthentication}.
        c                 S   r’   r¯   r#   rž   r#   r#   r$   Ú_areDone¡  r—   zYSSHProtocolCheckerTests.test_requestAvatarIdWithNotEnoughAuthentication.<locals>._areDoneÚareDoner˜   )
r   r¾   r@   r   r?   rÀ   r§   r   r¬   r   )r"   rk   rÇ   rÆ   rª   r#   r#   r$   Ú/test_requestAvatarIdWithNotEnoughAuthentication™  s   
zGSSHProtocolCheckerTests.test_requestAvatarIdWithNotEnoughAuthenticationc                 C   s$   t  ¡ }| tddƒ¡}|  |t¡S )z™
        If the passed credentials aren't handled by any registered checker,
        L{SSHProtocolChecker} should raise L{UnhandledCredentials}.
        r˜   )r   r¾   r§   r   r¬   r   )r"   rk   rª   r#   r#   r$   Ú%test_requestAvatarIdInvalidCredential¬  s   z=SSHProtocolCheckerTests.test_requestAvatarIdInvalidCredentialc                 C   s   |   t ¡  d¡¡ dS )zV
        The default L{SSHProcotolChecker.areDone} should simply return True.
        N)r+   r   r¾   rÈ   r!   r#   r#   r$   Útest_areDoneµ  s   z$SSHProtocolCheckerTests.test_areDoneN)r\   r]   r^   r_   ra   rb   rÃ   rÄ   r«   rÉ   rÊ   rË   r#   r#   r#   r$   r½   d  s    	r½   c                   @   s`   e Zd ZdZep	e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 )ÚUNIXPasswordDatabaseTestsz,
    Tests for L{UNIXPasswordDatabase}.
    c                 C   sP   g }|  |j¡ |  t|ƒdd¡ t|d tƒr|d  ¡  |  |d |¡ dS )a±  
        Assert that the L{Deferred} passed in is called back with the value
        'username'.  This represents a valid login for this TestCase.

        NOTE: To work, this method's return value must be returned from the
        test method, or otherwise hooked up to the test machinery.

        @param d: a L{Deferred} from an L{IChecker.requestAvatarId} method.
        @type d: L{Deferred}
        @rtype: L{Deferred}
        r:   zlogin incompleter   N)ÚaddBothÚappendrA   rz   Ú
isinstancer   ÚraiseException)r"   rª   ÚusernameÚresultr#   r#   r$   ÚassertLoggedInÃ  s   z(UNIXPasswordDatabaseTests.assertLoggedInc                 C   s<  t  ¡ }dd„ }tƒ }| d|ddƒddddd	¡ | d
dddddd	¡ tƒ }| ddddddddd¡	 | d
|d
dƒddddddd¡	 |  t d|¡ |  t d|¡ tƒ }|  td|¡ d|_d|_	t
d d!ƒ}|  | |¡d ¡ |  |jg ¡ |  |jg ¡ d"|_|  | |¡d"¡ |  |jd#dg¡ |  |jd#dg¡ d$S )%z
        L{UNIXPasswordDatabase} with no arguments has checks the C{pwd} database
        and then the C{spwd} database.
        c                 S   s    t   || ¡}t   |d| ¡}|S )Nz$1$)r*   )rÑ   r/   r0   r1   r#   r#   r$   r1   Ý  r¸   z?UNIXPasswordDatabaseTests.test_defaultCheckers.<locals>.cryptedr8   r/   r:   r;   Úfoor<   r=   rI   ÚxÚbarú/barr6   rK   rL   rM   rN   rO   é   é	   é
   é   é   é   é   r>   rP   rS   rQ   rR   ó   alicerh   ó   bobr   N)r   ÚUNIXPasswordDatabaser   r?   r   r@   r   r   rT   rU   r   rÓ   r§   rA   rX   rY   rÑ   )r"   rk   r1   r>   rP   r    Úcredr#   r#   r$   Útest_defaultCheckersÖ  s0   ÿ 
z.UNIXPasswordDatabaseTests.test_defaultCheckersc                 C   s   |   tj| j|d¡ dS )aÅ  
        Asserts that the L{Deferred} passed in is erred back with an
        L{UnauthorizedLogin} L{Failure}.  This reprsents an invalid login for
        this TestCase.

        NOTE: To work, this method's return value must be returned from the
        test method, or otherwise hooked up to the test machinery.

        @param d: a L{Deferred} from an L{IChecker.requestAvatarId} method.
        @type d: L{Deferred}
        @rtype: L{None}
        zbogus valueN)ÚassertRaisesr   r   rÓ   ©r"   rª   r#   r#   r$   ÚassertUnauthorizedLoginþ  s   ÿz1UNIXPasswordDatabaseTests.assertUnauthorizedLoginc              	   C   sR   t   dd¡}tƒ }| d|ddddd¡ t |jg¡}|  | tdd	ƒ¡d¡ d
S )zo
        L{UNIXPasswordDatabase} takes a list of functions to check for UNIX
        user information.
        ÚsecretÚanybodyr:   r;   rÔ   r×   r=   s   anybodys   secretN)	r*   r   r?   r   rá   rC   rÓ   r§   r   )r"   r/   rE   rk   r#   r#   r$   Útest_passInCheckers  s   ÿz-UNIXPasswordDatabaseTests.test_passInCheckersc                 C   sJ   dd„ }dd„ }|   td|¡ t |g¡}tddƒ}|  | |¡d¡ dS )zÝ
        If the encrypted password provided by the getpwnam function is valid
        (verified by the L{verifyCryptedPassword} function), we callback the
        C{requestAvatarId} L{Deferred} with the username.
        c                 S   ó   | |kS r   r#   ©r1   Úpwr#   r#   r$   r,   #  ó   zLUNIXPasswordDatabaseTests.test_verifyPassword.<locals>.verifyCryptedPasswordc                 S   ó   | | gS r   r#   ©rÑ   r#   r#   r$   rC   &  rí   z?UNIXPasswordDatabaseTests.test_verifyPassword.<locals>.getpwnamr,   ó   usernameN©r@   r   rá   r   rÓ   r§   ©r"   r,   rC   rk   Ú
credentialr#   r#   r$   Útest_verifyPassword  s   
z-UNIXPasswordDatabaseTests.test_verifyPasswordc                 C   s2   dd„ }t  |g¡}tddƒ}|  | |¡¡ dS )z}
        If the getpwnam function raises a KeyError, the login fails with an
        L{UnauthorizedLogin} exception.
        c                 S   s   t | ƒ‚r   )ÚKeyErrorrï   r#   r#   r$   rC   4  rí   z?UNIXPasswordDatabaseTests.test_failOnKeyError.<locals>.getpwnamrð   N)r   rá   r   ræ   r§   )r"   rC   rk   ró   r#   r#   r$   Útest_failOnKeyError.  s   
z-UNIXPasswordDatabaseTests.test_failOnKeyErrorc                 C   sH   dd„ }dd„ }|   td|¡ t |g¡}tddƒ}|  | |¡¡ dS )z”
        If the verifyCryptedPassword function doesn't verify the password, the
        login fails with an L{UnauthorizedLogin} exception.
        c                 S   r’   r¯   r#   rë   r#   r#   r$   r,   A  r—   zOUNIXPasswordDatabaseTests.test_failOnBadPassword.<locals>.verifyCryptedPasswordc                 S   rî   r   r#   rï   r#   r#   r$   rC   D  rí   zBUNIXPasswordDatabaseTests.test_failOnBadPassword.<locals>.getpwnamr,   rð   N)r@   r   rá   r   ræ   r§   rò   r#   r#   r$   Útest_failOnBadPassword;  s   
z0UNIXPasswordDatabaseTests.test_failOnBadPasswordc                 C   sT   dd„ }dd„ }dd„ }|   td|¡ t ||g¡}tddƒ}|  | |¡d¡ d	S )
a  
        UNIXPasswordDatabase.requestAvatarId loops through each getpwnam
        function associated with it and returns a L{Deferred} which fires with
        the result of the first one which returns a value other than None.
        ones do not verify the password.
        c                 S   rê   r   r#   rë   r#   r#   r$   r,   T  rí   zRUNIXPasswordDatabaseTests.test_loopThroughFunctions.<locals>.verifyCryptedPasswordc                 S   s   | dgS )Nznot the passwordr#   rï   r#   r#   r$   Ú	getpwnam1W  rí   zFUNIXPasswordDatabaseTests.test_loopThroughFunctions.<locals>.getpwnam1c                 S   rî   r   r#   rï   r#   r#   r$   Ú	getpwnam2Z  rí   zFUNIXPasswordDatabaseTests.test_loopThroughFunctions.<locals>.getpwnam2r,   rð   Nrñ   )r"   r,   rø   rù   rk   ró   r#   r#   r$   Útest_loopThroughFunctionsL  s   
z3UNIXPasswordDatabaseTests.test_loopThroughFunctionsc              	   C   s¶   t ƒ }| ddddddd¡ | ddddddd¡ | d	d
ddddd¡ |  td|¡ t tjg¡}tddƒ}|  | |¡¡ tddƒ}|  | |¡¡ tddƒ}|  | |¡¡ dS )z¨
        If the password returned by any function is C{""}, C{"x"}, or C{"*"} it
        is not compared against the supplied password.  Instead it is skipped.
        r8   r   r:   r;   rÔ   rÖ   rI   rÕ   ÚcarolÚ*r>   rß   ó    rà   ó   xs   caroló   *N)	r   r?   r@   r   rá   rB   r   ræ   r§   )r"   r>   rk   râ   r#   r#   r$   Útest_failOnSpecialb  s   


z,UNIXPasswordDatabaseTests.test_failOnSpecialN)r\   r]   r^   r_   r`   ra   rb   rÓ   rã   ræ   ré   rô   rö   r÷   rú   r   r#   r#   r#   r$   rÌ   ¼  s    (rÌ   c                   @   ó,   e Zd ZdZeZdd„ Zdd„ Zdd„ ZdS )	ÚAuthorizedKeyFileReaderTestsz5
    Tests for L{checkers.readAuthorizedKeyFile}
    c                 C   s0   t dƒ}t |dd„ ¡}|  ddgt|ƒ¡ dS )zg
        L{checkers.readAuthorizedKeyFile} does not attempt to turn comments
        into keys
        sE   # this comment is ignored
this is not
# this is again
and this is notc                 S   ó   | S r   r#   ©rÕ   r#   r#   r$   Ú<lambda>Š  ó    zCAuthorizedKeyFileReaderTests.test_ignoresComments.<locals>.<lambda>s   this is nots   and this is notN©r   r   ÚreadAuthorizedKeyFilerA   Úlist©r"   ÚfileobjrÒ   r#   r#   r$   Útest_ignoresComments  s
   ÿz1AuthorizedKeyFileReaderTests.test_ignoresCommentsc                 C   s0   t dƒ}tj|dd„ d}|  dgt|ƒ¡ dS )zw
        L{checkers.readAuthorizedKeyFile} ignores leading whitespace in
        lines, as well as empty lines
        sg   
                           # ignore
                           not ignored
                           c                 S   r  r   r#   r  r#   r#   r$   r  ˜  r  zYAuthorizedKeyFileReaderTests.test_ignoresLeadingWhitespaceAndEmptyLines.<locals>.<lambda>©ÚparseKeys   not ignoredNr  r
  r#   r#   r$   Ú*test_ignoresLeadingWhitespaceAndEmptyLines  s
   ÿzGAuthorizedKeyFileReaderTests.test_ignoresLeadingWhitespaceAndEmptyLinesc                 C   s4   dd„ }t dƒ}tj||d}|  dgt|ƒ¡ dS )zÇ
        L{checkers.readAuthorizedKeyFile} does not raise an exception
        when a key fails to parse (raises a
        L{twisted.conch.ssh.keys.BadKeyError}), but rather just keeps going
        c                 S   s   |   d¡r
t d¡‚| S )Nó   fzfailed to parse)Ú
startswithr   r´   )Úliner#   r#   r$   Ú
failOnSome¢  s   

zKAuthorizedKeyFileReaderTests.test_ignoresUnparsableKeys.<locals>.failOnSomes   failed key
good keyr  s   good keyNr  )r"   r  r  rÒ   r#   r#   r$   Útest_ignoresUnparsableKeys›  s   z7AuthorizedKeyFileReaderTests.test_ignoresUnparsableKeysN)	r\   r]   r^   r_   ra   rb   r  r  r  r#   r#   r#   r$   r  x  s    r  c                   @   r  )	ÚInMemorySSHKeyDBTestsz0
    Tests for L{checkers.InMemorySSHKeyDB}
    c                 C   s    t  ddgi¡}tt j|ƒ dS )z_
        L{checkers.InMemorySSHKeyDB} implements
        L{checkers.IAuthorizedKeysDB}
        rß   s   keyN)r   ÚInMemorySSHKeyDBr   ÚIAuthorizedKeysDB©r"   Úkeydbr#   r#   r$   Útest_implementsInterface³  s   z.InMemorySSHKeyDBTests.test_implementsInterfacec                 C   s*   t  ddgi¡}|  g t| d¡ƒ¡ dS )z½
        If the user is not in the mapping provided to
        L{checkers.InMemorySSHKeyDB}, an empty iterator is returned
        by L{checkers.InMemorySSHKeyDB.getAuthorizedKeys}
        rß   s   keysrà   N©r   r  rA   r	  ÚgetAuthorizedKeysr  r#   r#   r$   Útest_noKeysForUnauthorizedUser»  s   z4InMemorySSHKeyDBTests.test_noKeysForUnauthorizedUserc                 C   s0   t  dddgi¡}|  ddgt| d¡ƒ¡ dS )zÅ
        If the user is in the mapping provided to
        L{checkers.InMemorySSHKeyDB}, an iterator with all the keys
        is returned by L{checkers.InMemorySSHKeyDB.getAuthorizedKeys}
        rß   ó   aó   bNr  r  r#   r#   r$   Útest_allKeysForAuthorizedUserÄ  s   z3InMemorySSHKeyDBTests.test_allKeysForAuthorizedUserN)	r\   r]   r^   r_   ra   rb   r  r  r   r#   r#   r#   r$   r  ¬  s    	r  c                   @   r¼   )ÚUNIXAuthorizedKeysFilesTestsz8
    Tests for L{checkers.UNIXAuthorizedKeysFiles}.
    c              	   C   s~   t ƒ }t|  ¡ ƒ|_|j ¡  tƒ | _| j ddddd|jjd¡ |j d¡| _	| j	 ¡  | j	 d¡}| 
d	¡ d
dg| _d S )Nrß   rh   r:   r;   s   alice lastnameri   rf   r„   s   key 1
key 2s   key 1s   key 2)r   r   ro   rp   rq   r   rE   r?   rr   rs   r}   ÚexpectedKeys)r"   r    ÚauthorizedKeysr#   r#   r$   r%   Õ  s$   
ù


z"UNIXAuthorizedKeysFilesTests.setUpc                 C   s   t  | j¡}tt j|ƒ dS )zg
        L{checkers.UNIXAuthorizedKeysFiles} implements
        L{checkers.IAuthorizedKeysDB}.
        N)r   ÚUNIXAuthorizedKeysFilesrE   r   r  r  r#   r#   r$   r  ì  s   z5UNIXAuthorizedKeysFilesTests.test_implementsInterfacec                 C   s.   t j| jdd„ d}|  g t| d¡ƒ¡ dS )zÒ
        If the user is not in the user database provided to
        L{checkers.UNIXAuthorizedKeysFiles}, an empty iterator is returned
        by L{checkers.UNIXAuthorizedKeysFiles.getAuthorizedKeys}.
        c                 S   r  r   r#   r  r#   r#   r$   r  ú  r  zMUNIXAuthorizedKeysFilesTests.test_noKeysForUnauthorizedUser.<locals>.<lambda>r  rI   N)r   r$  rE   rA   r	  r  r  r#   r#   r$   r  ô  s   z;UNIXAuthorizedKeysFilesTests.test_noKeysForUnauthorizedUserc                 C   sH   | j  d¡ d¡ tj| jdd„ d}|  | jdg t| 	d¡ƒ¡ dS )a  
        If the user is in the user database provided to
        L{checkers.UNIXAuthorizedKeysFiles}, an iterator with all the keys in
        C{~/.ssh/authorized_keys} and C{~/.ssh/authorized_keys2} is returned
        by L{checkers.UNIXAuthorizedKeysFiles.getAuthorizedKeys}.
        rˆ   s   key 3c                 S   r  r   r#   r  r#   r#   r$   r    r  z`UNIXAuthorizedKeysFilesTests.test_allKeysInAllAuthorizedFilesForAuthorizedUser.<locals>.<lambda>r  rß   N)
rs   rr   r}   r   r$  rE   rA   r"  r	  r  r  r#   r#   r$   Ú1test_allKeysInAllAuthorizedFilesForAuthorizedUserý  s
   ÿzNUNIXAuthorizedKeysFilesTests.test_allKeysInAllAuthorizedFilesForAuthorizedUserc                 C   s0   t j| jdd„ d}|  | jt| d¡ƒ¡ dS )z¸
        L{checkers.UNIXAuthorizedKeysFiles.getAuthorizedKeys} returns only
        the keys in C{~/.ssh/authorized_keys} and C{~/.ssh/authorized_keys2}
        if they exist.
        c                 S   r  r   r#   r  r#   r#   r$   r    r  zJUNIXAuthorizedKeysFilesTests.test_ignoresNonexistantFile.<locals>.<lambda>r  rß   N)r   r$  rE   rA   r"  r	  r  r  r#   r#   r$   Útest_ignoresNonexistantFile
  s   z8UNIXAuthorizedKeysFilesTests.test_ignoresNonexistantFilec                 C   s@   | j  d¡ ¡  tj| jdd„ d}|  | jt| 	d¡ƒ¡ dS )z¿
        L{checkers.UNIXAuthorizedKeysFiles.getAuthorizedKeys} returns only
        the keys in C{~/.ssh/authorized_keys} and C{~/.ssh/authorized_keys2}
        if they are readable.
        rˆ   c                 S   r  r   r#   r  r#   r#   r$   r    r  zIUNIXAuthorizedKeysFilesTests.test_ignoresUnreadableFile.<locals>.<lambda>r  rß   N)
rs   rr   rq   r   r$  rE   rA   r"  r	  r  r  r#   r#   r$   Útest_ignoresUnreadableFile  s   z7UNIXAuthorizedKeysFilesTests.test_ignoresUnreadableFileN)r\   r]   r^   r_   ra   rb   r%   r  r  r%  r&  r'  r#   r#   r#   r$   r!  Î  s    		r!  Ú_KeyDBr  c                   @   s   e Zd ZdZdS )Ú_DummyExceptionz0
    Fake exception to be used for testing.
    N)r\   r]   r^   r_   r#   r#   r#   r$   r)  !  s    r)  c                   @   sL   e Zd ZdZeZdd„ Zdd„ Zdd„ Zdd	„ Z	d
d„ Z
dd„ Zdd„ ZdS )ÚSSHPublicKeyCheckerTestsz4
    Tests for L{checkers.SSHPublicKeyChecker}.
    c                 C   sD   t ddtjdtj tj¡ d¡ƒ| _t	dd„ ƒ| _
t | j
¡| _d S )Nrß   r™   rš   c                 S   s   t j tj¡gS r   )r   r£   r¤   r   r¢   )Ú_r#   r#   r$   r  8  s    z0SSHPublicKeyCheckerTests.setUp.<locals>.<lambda>)r
   r   r¢   r   r£   r¤   r¥   r¦   r©   r(  r  r   ÚSSHPublicKeyCheckerrk   r!   r#   r#   r$   r%   0  s   ûzSSHPublicKeyCheckerTests.setUpc                 C   s"   d| j _|  | j | j ¡t¡ dS )z
        Calling L{checkers.SSHPublicKeyChecker.requestAvatarId} with
        credentials that do not have a signature fails with L{ValidPublicKey}.
        N)r©   Ú	signatureÚfailureResultOfrk   r§   r   r!   r#   r#   r$   Ú test_credentialsWithoutSignature;  s   ÿz9SSHPublicKeyCheckerTests.test_credentialsWithoutSignaturec                 C   s$   d| j _|  | j | j ¡tj¡ dS )z–
        Calling L{checkers.SSHPublicKeyChecker.requestAvatarId} with
        credentials that have a bad key fails with L{keys.BadKeyError}.
        rý   N)r©   r~   r.  rk   r§   r   r´   r!   r#   r#   r$   Útest_credentialsWithBadKeyE  s   ÿz3SSHPublicKeyCheckerTests.test_credentialsWithBadKeyc                 C   s$   t j| j_|  | j | j¡t¡ dS )zÙ
        If L{checkers.IAuthorizedKeysDB.getAuthorizedKeys} returns no keys
        that match the credentials,
        L{checkers.SSHPublicKeyChecker.requestAvatarId} fails with
        L{UnauthorizedLogin}.
        N)r   ÚpublicDSA_opensshr©   r~   r.  rk   r§   r   r!   r#   r#   r$   Útest_credentialsNoMatchingKeyO  s   
ÿz6SSHPublicKeyCheckerTests.test_credentialsNoMatchingKeyc                 C   s2   t j tj¡ d¡| j_|  | j	 
| j¡t¡ dS )z§
        Calling L{checkers.SSHPublicKeyChecker.requestAvatarId} with
        credentials that are incorrectly signed fails with
        L{UnauthorizedLogin}.
        rš   N)r   r£   r¤   r   r±   r¦   r©   r-  r.  rk   r§   r   r!   r#   r#   r$   Ú test_credentialsInvalidSignature[  s   ÿþÿz9SSHPublicKeyCheckerTests.test_credentialsInvalidSignaturec                 C   s<   dd„ }|   tjd|¡ |  | j | j¡t¡ |  t	¡ dS )z
        If L{keys.Key.verify} raises an exception,
        L{checkers.SSHPublicKeyChecker.requestAvatarId} fails with
        L{UnauthorizedLogin}.
        c                  _   s   t ƒ ‚r   )r)  )ÚargsÚkwargsr#   r#   r$   Úfailo  s   z?SSHPublicKeyCheckerTests.test_failureVerifyingKey.<locals>.failÚverifyN)
r@   r   r£   r.  rk   r§   r©   r   r³   r)  )r"   r6  r#   r#   r$   Útest_failureVerifyingKeyh  s   ÿz1SSHPublicKeyCheckerTests.test_failureVerifyingKeyc                 C   s$   | j  | j¡}|  d|  |¡¡ dS )zu
        L{checker.SSHPublicKeyChecker.requestAvatarId}, if successful,
        callbacks with the username.
        rß   N)rk   r§   r©   rA   ÚsuccessResultOfrå   r#   r#   r$   Útest_usernameReturnedOnSuccessy  s   z7SSHPublicKeyCheckerTests.test_usernameReturnedOnSuccessN)r\   r]   r^   r_   ra   rb   r%   r/  r0  r2  r3  r8  r:  r#   r#   r#   r$   r*  )  s    

r*  )=r_   r*   ÚImportErrorr`   rS   Úbase64r   Úcollectionsr   Úior   Úzope.interface.verifyr   Útwisted.cred.checkersr   Útwisted.cred.credentialsr   r	   r
   r   Útwisted.cred.errorr   r   Útwisted.pythonr   Útwisted.python.failurer   Útwisted.python.fakepwdr   r   Útwisted.python.filepathr   Útwisted.python.reflectr   Útwisted.test.test_processr   Útwisted.trial.unittestr   ra   Útwisted.conchr   Útwisted.conch.errorr   r   Útwisted.conch.sshr   Útwisted.conch.testr   Úgetattrr»   r   rc   r½   rÌ   r  r  r!  r(  Ú	Exceptionr)  r*  r#   r#   r#   r$   Ú<module>   sX   ÿe JX =4"P