
    tfy                        U 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	 e	e
         ed<   	 ddlZdZn# e$ r dZY nw xY w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          rdZ*ddl+m,Z, ddl-m.Z.m/Z/ ddl0m1Z1 ddl2m3Z3 ndZ* e4edd          dZ5ndZ5 G d de)          Z6 G d de)          Z7 G d  d!e)          Z8 G d" d#e)          Z9 G d$ d%e)          Z: G d& d'e)          Z; G d( d)e)          Z< ed*d+g          Z= G d, d-e>          Z? G d. d/e)          Z@dS )0z&
Tests for L{twisted.conch.checkers}.
    N)encodebytes)
namedtuple)BytesIO)Optional	cryptSkipzcannot run without crypt module)verifyObject)'InMemoryUsernamePasswordDatabaseDontUse)ISSHPrivateKeyIUsernamePasswordSSHPrivateKeyUsernamePassword)UnauthorizedLoginUnhandledCredentials)Deferred)util)ShadowDatabaseUserDatabase)FilePath)requireModule)MockOS)TestCasecryptography)checkers)NotEnoughAuthenticationValidPublicKey)keys)keydatazcan't run without cryptographygeteuidz0Cannot run without effective UIDs (questionable)c                   J    e Zd ZdZepeZd Zd Zd Z	d Z
d Zd Zd Zd	 Zd
S )HelperTestszl
    Tests for helper functions L{verifyCryptedPassword}, L{_pwdGetByName} and
    L{_shadowGetByName}.
    c                 ,    t                      | _        d S N)r   mockosselfs    _/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/conch/test/test_checkers.pysetUpzHelperTests.setUp@   s    hh    c                     d}d}t          j         ||          }|                     t          j        ||          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crypteds       r&   test_verifyCryptedPasswordz&HelperTests.test_verifyCryptedPasswordC   sg    
 #+h--*7H==CJJ 	
 	
 	
 	
 	
r(   c                     d}d}t          j         ||          }|                     t          j        ||          d                    ||                     dS )z
        L{verifyCryptedPassword} returns True if the provided cleartext password
        matches the provided MD5 password hash.
        r2   z$1$saltz3{!r} supposed to be valid encrypted password for {}Nr,   r1   s       r&   test_verifyCryptedPasswordMD5z)HelperTests.test_verifyCryptedPasswordMD5R   sg    
 +h--*7H==AHH 	
 	
 	
 	
 	
r(   c                     d}d}t          j         ||          }|                     t          j        ||          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/   r0   )r%   r2   wrongr4   s       r&   test_refuteCryptedPasswordz&HelperTests.test_refuteCryptedPassworda   si    
 #+h11*7E::ELL 	
 	
 	
 	
 	
r(   c           	         t                      }|                    ddddddd           |                     t          d|           |                     t          j        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      
first last/foo/bin/shpwdN)r   addUserpatchr   assertEqual_pwdGetByNamegetpwnamr%   userdbs     r&   test_pwdGetByNamezHelperTests.test_pwdGetByNamep   st    
 w!QfiPPP

8UF+++/88&//':R:RSSSSSr(   c                     |                      t          dd           |                     t          j        d                     dS )zW
        If the C{pwd} module isn't present, L{_pwdGetByName} returns L{None}.
        rD   Nr=   )rF   r   assertIsNonerH   r$   s    r&   test_pwdGetByNameWithoutPwdz'HelperTests.test_pwdGetByNameWithoutPwdz   s?     	

8UD)))(099:::::r(   c                    t                      }|                    ddddddddd		  	         |                     t          d
|           d| j        _        d| j        _        |                     t          d| j                   |                     t          j	        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   rE   rF   r   r#   euidegidr   rG   _shadowGetByNamegetspnamseteuidCallssetegidCallsrJ   s     r&   test_shadowGetByNamez HelperTests.test_shadowGetByName   s    
  !!ulAq!Q1a@@@

8VV,,,

4t{+++25996??5;Q;QRRR1At9===1At9=====r(   c                    |                      t          dd           |                     t          j        d                     |                     | j        j        g            |                     | j        j        g            dS )zP
        L{_shadowGetByName} returns L{None} if C{spwd} is not present.
        rX   NrQ   )rF   r   rN   r^   rG   r#   r`   ra   r$   s    r&   test_shadowGetByNameWithoutSpwdz+HelperTests.test_shadowGetByNameWithoutSpwd   su     	

8VT***(3E::;;;126661266666r(   N)__name__
__module____qualname____doc__r   dependencySkipskipr'   r5   r7   r;   rL   rO   rb   rd    r(   r&   r    r    8   s         
 &D  
 
 

 
 

 
 
T T T; ; ;> > >"7 7 7 7 7r(   r    c                   ^    e Zd ZdZepeZddZd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd ZdS )SSHPublicKeyDatabaseTestsz,
    Tests for L{SSHPublicKeyDatabase}.
    returnNc           	         t          j                    | _        t          d          | _        t          d          | _        d| j        z   dz   | j        z   dz   | _        t                      | _        | 	                    t          d| j                   t          |                                           | _        t          | j        j        t                    sJ | j                            d          | _        | j                                         t'                      }|                    dd	d
dd| j        j        d           || j        _        d S )N   foobar   eggspams   t1 s    foo
t2 s    egg
r[   .sshuserr2   r?   r@   rA   
/bin/shell)r   SSHPublicKeyDatabasecheckerr   key1key2contentr   r#   rF   r   r   mktemppath
isinstancestrchildsshDirmakedirsr   rE   _userdbrJ   s     r&   r'   zSSHPublicKeyDatabaseTests.setUp   s   466	**	
++		)L849DyPhh

4t{+++T[[]]++	$).#.....ioof--IN	
 	
 	
  &r(   c                    |                      | j        g          }|                     |d         d         t                     |                     |d         d         d           |                     t	          |          d           dS )zJ
        L{SSHPublicKeyDatabase} is deprecated as of version 15.0
        )offendingFunctionsr   categorymessageztwisted.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'   rG   DeprecationWarninglen)r%   warningsShowns     r&   test_deprecatedz)SSHPublicKeyDatabaseTests.test_deprecated   s     **tzl*KKq)*57IJJJ!Y'F	
 	
 	
 	]++Q/////r(   c                    | j                             |                              | j                   t	          dd          }d|_        |                     | j                            |                     d|_        |                     | j                            |                     d|_        | 	                    | j                            |                     d S )N   user   passwordrp   rq   s
   notallowed)
r   r~   
setContentry   r   blobr.   rv   checkKeyr9   )r%   filenamers   s      r&   _testCheckKeyz'SSHPublicKeyDatabaseTests._testCheckKey   s    (##..t|<<<55	--d33444	--d33444!	..t4455555r(   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   rG   r#   r`   ra   r$   s    r&   test_checkKeyz'SSHPublicKeyDatabaseTests.test_checkKey   sU    
 	,---126661266666r(   c                     |                      d           |                     | j        j        g            |                     | j        j        g            dS )z
        L{SSHPublicKeyDatabase.checkKey} should retrieve the content of the
        authorized_keys2 file and check the keys against that file.
        authorized_keys2Nr   r$   s    r&   test_checkKey2z(SSHPublicKeyDatabaseTests.test_checkKey2   sU    
 	-...126661266666r(   c                    | j                             d                              | j                                       d           |                     j        d           | j        j        f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                 D                         d            |           S )Nr   )chmod)r\   keyFilesavedSeteuids    r&   seteuidz>SSHPublicKeyDatabaseTests.test_checkKeyAsRoot.<locals>.seteuid   s%    MM%   <%%%r(   rY   rZ   r   r[   r   r   rp   )r   r?   r   rY   r@   N)r   r~   r   ry   r   
addCleanupr#   r   r\   r]   rF   r   r   r   r.   rv   r   rG   r`   ra   )r%   r   rs   r   r   s      @@r&   test_checkKeyAsRootz-SSHPublicKeyDatabaseTests.test_checkKeyAsRoot   sD   
 +##$5664<(((eu---{*	& 	& 	& 	& 	& 	&  

4;	7333

4t{+++55	--d334441???CCC1At9=====r(   c           	      X    d }                       j        d|           t          ddt          j        dt
          j                            t          j                  	                    d                    } j        
                    |          } fd}|                    |          S )z
        L{SSHPublicKeyDatabase.requestAvatarId} should return the avatar id
        passed in if its C{_checkKey} method returns True.
        c                     dS NTrk   ignoreds    r&   	_checkKeyzASSHPublicKeyDatabaseTests.test_requestAvatarId.<locals>._checkKey      4r(   r      test   ssh-rsa   fooc                 4                         | d           d S Nr   rG   avatarIdr%   s    r&   _verifyz?SSHPublicKeyDatabaseTests.test_requestAvatarId.<locals>._verify      Xw/////r(   )rF   rv   r   r   publicRSA_opensshr   Key
fromStringprivateRSA_opensshsignrequestAvatarIdaddCallback)r%   r   credentialsdr   s   `    r&   test_requestAvatarIdz.SSHPublicKeyDatabaseTests.test_requestAvatarId  s    	 	 	 	

4<Y777#%H :;;@@HH
 
 L((55	0 	0 	0 	0 	0 }}W%%%r(   c                     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                     dS r   rk   r   s    r&   r   zQSSHPublicKeyDatabaseTests.test_requestAvatarIdWithoutSignature.<locals>._checkKey%  r   r(   r   r   r   N)rF   rv   r   r   r   r   assertFailurer   r%   r   r   r   s       r&   $test_requestAvatarIdWithoutSignaturez>SSHPublicKeyDatabaseTests.test_requestAvatarIdWithoutSignature  su    	 	 	 	

4<Y777#Z!:D$
 
 L((55!!!^444r(   c                     d }|                      | j        d|           | j                            d          }|                     |t                    S )z
        If L{SSHPublicKeyDatabase.checkKey} returns False,
        C{_cbRequestAvatarId} should raise L{UnauthorizedLogin}.
        c                     dS NFrk   r   s    r&   r   zKSSHPublicKeyDatabaseTests.test_requestAvatarIdInvalidKey.<locals>._checkKey5      5r(   r   N)rF   rv   r   r   r   )r%   r   r   s      r&   test_requestAvatarIdInvalidKeyz8SSHPublicKeyDatabaseTests.test_requestAvatarIdInvalidKey/  sW    	 	 	 	

4<Y777L((..!!!%6777r(   c           	      X   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                     dS r   rk   r   s    r&   r   zQSSHPublicKeyDatabaseTests.test_requestAvatarIdInvalidSignature.<locals>._checkKeyC  r   r(   r   r   r   r   )rF   rv   r   r   r   r   r   r   privateDSA_opensshr   r   r   r   r   s       r&   $test_requestAvatarIdInvalidSignaturez>SSHPublicKeyDatabaseTests.test_requestAvatarIdInvalidSignature<  s    	 	 	 	

4<Y777#%H :;;@@HH
 
 L((55!!!%6777r(   c                     d }                       j        d|           t          ddddd          } j                            |          } fd}|                    |                                |t                    S )	z~
        Exceptions raised while verifying the key should be normalized into an
        C{UnauthorizedLogin} failure.
        c                     dS r   rk   r   s    r&   r   zSSSHPublicKeyDatabaseTests.test_requestAvatarIdNormalizeException.<locals>._checkKeyW  r   r(   r   r   Ns   blobs   sigDatas   sigc                                          t          j                  }                    t	          |          d           | S )Nr?   )flushLoggedErrorsr   BadKeyErrorrG   r   )failureerrorsr%   s     r&   _verifyLoggedExceptionz`SSHPublicKeyDatabaseTests.test_requestAvatarIdNormalizeException.<locals>._verifyLoggedException^  s:    ++D,<==FS[[!,,,Nr(   )rF   rv   r   r   
addErrbackr   r   )r%   r   r   r   r   s   `    r&   &test_requestAvatarIdNormalizeExceptionz@SSHPublicKeyDatabaseTests.test_requestAvatarIdNormalizeExceptionQ  s    	 	 	 	

4<Y777#GT7JOOL((55	 	 	 	 	
 	
+,,,!!!%6777r(   rn   N)re   rf   rg   rh   euidSkipri   rj   r'   r   r   r   r   r   r   r   r   r   r   rk   r(   r&   rm   rm      s          %~D& & & &40 0 0 6 6 67 7 77 7 7> > >6& & &05 5 5$8 8 88 8 8*8 8 8 8 8r(   rm   c                   :    e Zd ZdZeZd Zd Zd Zd Z	d Z
d ZdS )	SSHProtocolCheckerTestsz*
    Tests for L{SSHProtocolChecker}.
    c                 P   t          j                    }|                     |j        g            |                    t          j                               |                     |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   SSHProtocolCheckerrG   credentialInterfacesregisterCheckerru   r
   assertIsInstancer%   rv   s     r&   test_registerCheckerz,SSHProtocolCheckerTests.test_registerCheckern  s    
 -//5r:::)++	
 	
 	
 	57GHHH^,h.K	
 	
 	
 	
 	
r(   c                 \   t          j                    }|                     |j        g            |                    t          j                    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   rG   r   r   ru   r   r   r   s     r&   !test_registerCheckerWithInterfacez9SSHProtocolCheckerTests.test_registerCheckerWithInterface}  s     -//5r::: = ? ?ARSSS58I7JKKK./1N	
 	
 	
 	
 	
r(   c                     t          j                    }t                      }|                    dd           |                    |           |                    t          dd                    } fd}|                    |          S )z
        L{SSHProtocolChecker.requestAvatarId} should defer to one if its
        registered checkers to authenticate a user.
        r   c                 4                         | d           d S r   r   r   s    r&   	_callbackz?SSHProtocolCheckerTests.test_requestAvatarId.<locals>._callback  r   r(   )r   r   r	   rE   r   r   r   r   )r%   rv   passwordDatabaser   r   s   `    r&   r   z,SSHProtocolCheckerTests.test_requestAvatarId  s    
 -//BDD  '222 0111##$4Wg$F$FGG	0 	0 	0 	0 	0 }}Y'''r(   c                 J   t          j                    }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                     dS r   rk   )r   s    r&   _areDonezYSSHProtocolCheckerTests.test_requestAvatarIdWithNotEnoughAuthentication.<locals>._areDone  r   r(   areDoner   )
r   r   rF   r	   rE   r   r   r   r   r   )r%   rv   r   r   r   s        r&   /test_requestAvatarIdWithNotEnoughAuthenticationzGSSHProtocolCheckerTests.test_requestAvatarIdWithNotEnoughAuthentication  s     -//	 	 	 	

7Ix000BDD  '222 0111##$4Wg$F$FGG!!!%<===r(   c                     t          j                    }|                    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%   rv   r   s      r&   %test_requestAvatarIdInvalidCredentialz=SSHProtocolCheckerTests.test_requestAvatarIdInvalidCredential  sG    
 -//##$4Wg$F$FGG!!!%9:::r(   c                 x    |                      t          j                                        d                     dS )zV
        The default L{SSHProcotolChecker.areDone} should simply return True.
        N)r.   r   r   r   r$   s    r&   test_areDonez$SSHProtocolCheckerTests.test_areDone  s3     	355==dCCDDDDDr(   N)re   rf   rg   rh   ri   rj   r   r   r   r   r   r   rk   r(   r&   r   r   g  s          D
 
 

 
 
( ( ( > > >&; ; ;E E E E Er(   r   c                   j    e Zd ZdZepeZdee         deddfdZ	d Z
d Zd	 Zd
 Zd Zd Zd Zd ZdS )UNIXPasswordDatabaseTestsz,
    Tests for L{UNIXPasswordDatabase}.
    r   usernamern   Nc                 X    |                      |                     |          |           dS )z
        Assert that the L{Deferred} passed in is called back with the value
        'username'.  This represents a valid login for this TestCase.

        @param d: a L{Deferred} from an L{IChecker.requestAvatarId} method.
        N)rG   successResultOf)r%   r   r   s      r&   assertLoggedInz(UNIXPasswordDatabaseTests.assertLoggedIn  s.     	--a00(;;;;;r(   c                    t          j                    }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                 `    t          j         ||           }t          j         |d|z             }|S )Nz$1$)r-   )r   r2   r3   r4   s       r&   r4   z?UNIXPasswordDatabaseTests.test_defaultCheckers.<locals>.crypted  s-    ;x22Dk(EDL99GNr(   r=   r2   r?   r@   foorB   rC   rQ   xbar/barr:   rS   rT   rU   rV   rW      	   
               rD   rX   r[   rY   rZ      alicer      bobr   N)r   UNIXPasswordDatabaser   rE   r   rF   r   r   r\   r]   r   r   r   rG   r`   ra   r   )r%   rv   r4   rD   rX   r#   creds          r&   test_defaultCheckersz.UNIXPasswordDatabaseTests.test_defaultCheckers  s   
 /11	 	 	
 nnWWWj111a		
 	
 	
 	E31eVY???Wgq!Q1a;;;UGGE:661b"b"bQQQ

8UC(((

8VT***

4v&&&+66G33D998DDD,b111,b111G33D996BBB,q$i888,q$i88888r(   c                 F    |                      |t          j                   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}
        N)failureResultOfr   r   r%   r   s     r&   assertUnauthorizedLoginz1UNIXPasswordDatabaseTests.assertUnauthorizedLogin  s#     	Q :;;;;;r(   c           	      $   t          j         dd          }t                      }|                    d|ddddd           t          j        |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   rC   s   anybodys   secretN)	r-   r   rE   r   r  rI   r   r   r   )r%   r2   rK   rv   s       r&   test_passInCheckersz-UNIXPasswordDatabaseTests.test_passInCheckers  s    
 ;x22y(Aq%KKK/0ABB##$4Z$K$KLLj	
 	
 	
 	
 	
r(   c                     d }d }|                      t          d|           t          j        |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                     | |k    S r"   rk   r4   pws     r&   r/   zLUNIXPasswordDatabaseTests.test_verifyPassword.<locals>.verifyCryptedPassword      b= r(   c                 
    | | gS r"   rk   r   s    r&   rI   z?UNIXPasswordDatabaseTests.test_verifyPassword.<locals>.getpwnam  s    h''r(   r/      usernameNrF   r   r  r   r   r   r%   r/   rI   rv   
credentials        r&   test_verifyPasswordz-UNIXPasswordDatabaseTests.test_verifyPassword  s    	! 	! 	!	( 	( 	( 	

846KLLL/
;;%k;??
G33J??MMMMMr(   c                     d }t          j        |g          }t          dd          }|                     |                    |                     dS )z}
        If the getpwnam function raises a KeyError, the login fails with an
        L{UnauthorizedLogin} exception.
        c                      t          |           r"   )KeyErrorr  s    r&   rI   z?UNIXPasswordDatabaseTests.test_failOnKeyError.<locals>.getpwnam+  s    8$$$r(   r  r   N)r   r  r   r  r   )r%   rI   rv   r  s       r&   test_failOnKeyErrorz-UNIXPasswordDatabaseTests.test_failOnKeyError%  s\    	% 	% 	% /
;;%k;??
$$W%<%<Z%H%HIIIIIr(   c                     d }d }|                      t          d|           t          j        |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                     dS r   rk   r  s     r&   r/   zOUNIXPasswordDatabaseTests.test_failOnBadPassword.<locals>.verifyCryptedPassword8  r   r(   c                 
    | dgS )Nr   rk   r  s    r&   rI   zBUNIXPasswordDatabaseTests.test_failOnBadPassword.<locals>.getpwnam;  s    k**r(   r/   r  r   N)rF   r   r  r   r  r   r  s        r&   test_failOnBadPasswordz0UNIXPasswordDatabaseTests.test_failOnBadPassword2  s    	 	 		+ 	+ 	+ 	

846KLLL/
;;%k;??
$$W%<%<Z%H%HIIIIIr(   c                     d }d }d }|                      t          d|           t          j        ||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                     | |k    S r"   rk   r  s     r&   r/   zRUNIXPasswordDatabaseTests.test_loopThroughFunctions.<locals>.verifyCryptedPasswordK  r  r(   c                 
    | dgS )Nznot the passwordrk   r  s    r&   	getpwnam1zFUNIXPasswordDatabaseTests.test_loopThroughFunctions.<locals>.getpwnam1N  s    011r(   c                 
    | dgS )Nr2   rk   r  s    r&   	getpwnam2zFUNIXPasswordDatabaseTests.test_loopThroughFunctions.<locals>.getpwnam2Q  s    j))r(   r/   r  r   Nr  )r%   r/   r'  r)  rv   r  s         r&   test_loopThroughFunctionsz3UNIXPasswordDatabaseTests.test_loopThroughFunctionsC  s    	! 	! 	!	2 	2 	2	* 	* 	* 	

846KLLL/I0FGG%k;??
G33J??MMMMMr(   c           	         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          j        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.
        r=    r?   r@   r   r   rQ   r   carol*rD   r  r(   r     xs   carol   *N)	r   rE   rF   r   r  rH   r   r  r   )r%   rD   rv   r  s       r&   test_failOnSpecialz,UNIXPasswordDatabaseTests.test_failOnSpecialY  s,   
 nnGRAr5%888E31b%777GS!QE5999

8UC(((/1G0HII#..$$W%<%<T%B%BCCC--$$W%<%<T%B%BCCC$//$$W%<%<T%B%BCCCCCr(   )re   rf   rg   rh   r   ri   rj   r   bytesr   r  r  r  r  r  r#  r*  r1  rk   r(   r&   r   r     s          &D< <5 <T < < < <&9 &9 &9P< < <
 
 
N N N$J J JJ J J"N N N,D D D D Dr(   r   c                   (    e Zd ZdZeZd Zd Zd ZdS )AuthorizedKeyFileReaderTestsz5
    Tests for L{checkers.readAuthorizedKeyFile}
    c                     t          d          }t          j        |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 r"   rk   r   s    r&   <lambda>zCAuthorizedKeyFileReaderTests.test_ignoresComments.<locals>.<lambda>  s    1 r(   s   this is nots   and this is notNr   r   readAuthorizedKeyFilerG   listr%   fileobjresults      r&   test_ignoresCommentsz1AuthorizedKeyFileReaderTests.test_ignoresCommentsv  sV    
 
 
 /EE.*<=tF||LLLLLr(   c                     t          d          }t          j        |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"   rk   r7  s    r&   r8  zYAuthorizedKeyFileReaderTests.test_ignoresLeadingWhitespaceAndEmptyLines.<locals>.<lambda>  s    A r(   parseKeys   not ignoredNr9  r<  s      r&   *test_ignoresLeadingWhitespaceAndEmptyLineszGAuthorizedKeyFileReaderTests.test_ignoresLeadingWhitespaceAndEmptyLines  sV    
 
 
 /++NNN.)4<<88888r(   c                     d }t          d          }t          j        ||          }|                     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                 X    |                      d          rt          j        d          | S )N   fzfailed to parse)
startswithr   r   )lines    r&   
failOnSomezKAuthorizedKeyFileReaderTests.test_ignoresUnparsableKeys.<locals>.failOnSome  s-    t$$ :&'8999Kr(   s   failed key
good keyrB  s   good keyNr9  )r%   rJ  r=  r>  s       r&   test_ignoresUnparsableKeysz7AuthorizedKeyFileReaderTests.test_ignoresUnparsableKeys  sZ    	 	 	
 122/*MMM+V55555r(   N)	re   rf   rg   rh   ri   rj   r?  rD  rK  rk   r(   r&   r4  r4  o  sU          DM M M9 9 96 6 6 6 6r(   r4  c                   (    e Zd ZdZeZd Zd Zd ZdS )InMemorySSHKeyDBTestsz0
    Tests for L{checkers.InMemorySSHKeyDB}
    c                 h    t          j        ddgi          }t          t           j        |           dS )z_
        L{checkers.InMemorySSHKeyDB} implements
        L{checkers.IAuthorizedKeysDB}
        r  s   keyN)r   InMemorySSHKeyDBr   IAuthorizedKeysDBr%   keydbs     r&   test_implementsInterfacez.InMemorySSHKeyDBTests.test_implementsInterface  s4    
 )8fX*>??X/77777r(   c                     t          j        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   rO  rG   r;  getAuthorizedKeysrQ  s     r&   test_noKeysForUnauthorizedUserz4InMemorySSHKeyDBTests.test_noKeysForUnauthorizedUser  sM     )8gY*?@@T%"9"9&"A"ABBCCCCCr(   c                     t          j        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   bNrU  rQ  s     r&   test_allKeysForAuthorizedUserz3InMemorySSHKeyDBTests.test_allKeysForAuthorizedUser  sS     )8dD\*BCC$tE,C,CH,M,M'N'NOOOOOr(   N)	re   rf   rg   rh   ri   rj   rS  rW  r[  rk   r(   r&   rM  rM    sZ          D8 8 8D D DP P P P Pr(   rM  c                   <    e Zd ZdZeZd
dZd Zd Zd Z	d Z
d	 ZdS )UNIXAuthorizedKeysFilesTestsz8
    Tests for L{checkers.UNIXAuthorizedKeysFiles}.
    rn   Nc           	          t          |                                           | _        t          | j        j        t                    sJ | j                                         t                      | _        | j                            ddddd| j        j        d           | j        	                    d          | _
        | j
                                         | j
        	                    d          }|                    d	           d
dg| _        d S )Nr=   r2   r?   r@   zalice lastnamert   rr   r   s   key 1
key 2s   key 1s   key 2)r   rz   r{   r|   r}   r   r   rK   rE   r~   r   r   expectedKeys)r%   authorizedKeyss     r&   r'   z"UNIXAuthorizedKeysFilesTests.setUp  s    T[[]]++	$).#.....	"nnIN	
 	
 	
 ioof--**+<==!!/222%x0r(   c                 l    t          j        | j                  }t          t           j        |           dS )zg
        L{checkers.UNIXAuthorizedKeysFiles} implements
        L{checkers.IAuthorizedKeysDB}.
        N)r   UNIXAuthorizedKeysFilesrK   r   rP  rQ  s     r&   rS  z5UNIXAuthorizedKeysFilesTests.test_implementsInterface  s/    
 0==X/77777r(   c                     t          j        | j        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"   rk   r7  s    r&   r8  zMUNIXAuthorizedKeysFilesTests.test_noKeysForUnauthorizedUser.<locals>.<lambda>      QR r(   rB  r  N)r   rb  rK   rG   r;  rV  rQ  s     r&   rW  z;UNIXAuthorizedKeysFilesTests.test_noKeysForUnauthorizedUser  sO     0{{SSST%"9"9&"A"ABBCCCCCr(   c                    | j                             d                              d           t          j        | j        d           }|                     | j        dgz   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"   rk   r7  s    r&   r8  z`UNIXAuthorizedKeysFilesTests.test_allKeysInAllAuthorizedFilesForAuthorizedUser.<locals>.<lambda>  re  r(   rB  r  N)
r   r~   r   r   rb  rK   rG   r_  r;  rV  rQ  s     r&   1test_allKeysInAllAuthorizedFilesForAuthorizedUserzNUNIXAuthorizedKeysFilesTests.test_allKeysInAllAuthorizedFilesForAuthorizedUser  s     	,--88BBB0{{SSS
*D1H1H1R1R,S,S	
 	
 	
 	
 	
r(   c                     t          j        | j        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"   rk   r7  s    r&   r8  zJUNIXAuthorizedKeysFilesTests.test_ignoresNonexistantFile.<locals>.<lambda>  re  r(   rB  r  N)r   rb  rK   rG   r_  r;  rV  rQ  s     r&   test_ignoresNonexistantFilez8UNIXAuthorizedKeysFilesTests.test_ignoresNonexistantFile  sR     0{{SSS*D1H1H1R1R,S,STTTTTr(   c                    | j                             d                                           t          j        | j        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"   rk   r7  s    r&   r8  zIUNIXAuthorizedKeysFilesTests.test_ignoresUnreadableFile.<locals>.<lambda>  re  r(   rB  r  N)
r   r~   r   r   rb  rK   rG   r_  r;  rV  rQ  s     r&   test_ignoresUnreadableFilez7UNIXAuthorizedKeysFilesTests.test_ignoresUnreadableFile
  su     	,--668880{{SSS*D1H1H1R1R,S,STTTTTr(   r   )re   rf   rg   rh   ri   rj   r'   rS  rW  rh  rk  rn  rk   r(   r&   r]  r]    s          D1 1 1 1.8 8 8D D D
 
 
U U UU U U U Ur(   r]  _KeyDBrV  c                       e Zd ZdZdS )_DummyExceptionz0
    Fake exception to be used for testing.
    N)re   rf   rg   rh   rk   r(   r&   rq  rq    s          	Dr(   rq  c                   @    e Zd ZdZeZd Zd Zd Zd Z	d Z
d Zd Zd	S )
SSHPublicKeyCheckerTestsz4
    Tests for L{checkers.SSHPublicKeyChecker}.
    c           	      $   t          ddt          j        dt          j                            t          j                                      d                    | _        t          d           | _
        t          j        | j
                  | _        d S )Nr  r   r   c                 V    t           j                            t          j                  gS r"   )r   r   r   r   r   )_s    r&   r8  z0SSHPublicKeyCheckerTests.setUp.<locals>.<lambda>/  s    tx':':7;T'U'U&V r(   )r   r   r   r   r   r   r   r   r   ro  rR  r   SSHPublicKeyCheckerrv   r$   s    r&   r'   zSSHPublicKeyCheckerTests.setUp'  st    (%H :;;@@HH
 
 VVWW
3DJ??r(   c                     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   	signaturer	  rv   r   r   r$   s    r&    test_credentialsWithoutSignaturez9SSHPublicKeyCheckerTests.test_credentialsWithoutSignature2  sI    
 &*"L(()9::N	
 	
 	
 	
 	
r(   c                     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	  rv   r   r   r   r$   s    r&   test_credentialsWithBadKeyz3SSHPublicKeyCheckerTests.test_credentialsWithBadKey<  sL    
 !$L(()9::D<L	
 	
 	
 	
 	
r(   c                     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	  rv   r   r   r$   s    r&   test_credentialsNoMatchingKeyz6SSHPublicKeyCheckerTests.test_credentialsNoMatchingKeyF  sM     !( 9L(()9::<M	
 	
 	
 	
 	
r(   c                    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   ry  r	  rv   r   r   r$   s    r&    test_credentialsInvalidSignaturez9SSHPublicKeyCheckerTests.test_credentialsInvalidSignatureR  sr     &*X%8%8&&
 &

$v,, 	" 	L(()9::<M	
 	
 	
 	
 	
r(   c                     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                      t                      r"   )rq  )argskwargss     r&   failz?SSHPublicKeyCheckerTests.test_failureVerifyingKey.<locals>.failf  s    !###r(   verifyN)
rF   r   r   r	  rv   r   r   r   r   rq  )r%   r  s     r&   test_failureVerifyingKeyz1SSHPublicKeyCheckerTests.test_failureVerifyingKey_  sx    	$ 	$ 	$ 	

48Xt,,,L(()9::<M	
 	
 	
 	/////r(   c                     | j                             | j                  }|                     d|                     |                     dS )zu
        L{checker.SSHPublicKeyChecker.requestAvatarId}, if successful,
        callbacks with the username.
        r  N)rv   r   r   rG   r   r
  s     r&   test_usernameReturnedOnSuccessz7SSHPublicKeyCheckerTests.test_usernameReturnedOnSuccessp  sF    
 L(()9::4#7#7#:#:;;;;;r(   N)re   rf   rg   rh   ri   rj   r'   rz  r|  r  r  r  r  rk   r(   r&   rs  rs     s          D	@ 	@ 	@
 
 

 
 


 

 


 
 
0 0 0"< < < < <r(   rs  )Arh   r[   base64r   collectionsr   ior   typingr   r}   __annotations__r-   r   ImportErrorzope.interface.verifyr   twisted.cred.checkersr	   twisted.cred.credentialsr
   r   r   r   twisted.cred.errorr   r   twisted.internet.deferr   twisted.pythonr   twisted.python.fakepwdr   r   twisted.python.filepathr   twisted.python.reflectr   twisted.test.test_processr   twisted.trial.unittestr   ri   twisted.conchr   twisted.conch.errorr   r   twisted.conch.sshr   twisted.conch.testr   getattrr   r    rm   r   r   r4  rM  r]  ro  	Exceptionrq  rs  rk   r(   r&   <module>r     s6    
 
			       " " " " " "            C=   LLL II  2 2 21III2
 / . . . . . I I I I I I            G F F F F F F F + + + + + +       ? ? ? ? ? ? ? ? , , , , , , 0 0 0 0 0 0 , , , , , , + + + + + +=   6N&&&&&&KKKKKKKK&&&&&&*******5N
72y$+HHAHb7 b7 b7 b7 b7( b7 b7 b7JG8 G8 G8 G8 G8 G8 G8 G8TUE UE UE UE UEh UE UE UEpmD mD mD mD mD mD mD mD`16 16 16 16 168 16 16 16hP P P P PH P P PDMU MU MU MU MU8 MU MU MU` 
H23	4	4	 	 	 	 	i 	 	 	V< V< V< V< V<x V< V< V< V< V<s   3 ==