o
    ¯b[;  ã                   @   s
  d Z zddlZW n ey   dZY nw eZzddlZW n ey'   dZY nw eZddlZddlmZ ddl	m
Z
 ddlmZmZ ddlmZ dZdd	„ Zeƒ ZG d
d„ dƒZG dd„ deeƒZG dd„ deeƒZG dd„ dƒZG dd„ deeƒZG dd„ deeƒZdS )z&
Tests for L{twisted.python.fakepwd}.
é    N)Úgetitem)Ú_PYPY)ÚShadowDatabaseÚUserDatabase)ÚTestCaseiç  c                  C   s@   t } tdur	 zt | ¡ W n
 ty   Y | S w | d8 } q| S )aé  
    By convention, UIDs less than 1000 are reserved for the system.  A system
    which allocated every single one of those UIDs would likely have practical
    problems with allocating new ones, so let's assume that we'll be able to
    find one.  (If we don't, this will wrap around to negative values and
    I{eventually} find something.)

    @return: a user ID which does not exist on the local system.  Or, on
        systems without a L{pwd} module, return C{SYSTEM_UID_MAX}.
    NTé   )ÚSYSTEM_UID_MAXÚpwdÚgetpwuidÚKeyError)Úguess© r   úB/usr/lib/python3/dist-packages/twisted/python/test/test_fakepwd.pyÚfindInvalidUID    s   üúr   c                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚUserDatabaseTestsMixina^  
    L{UserDatabaseTestsMixin} defines tests which apply to any user database
    implementation.  Subclasses should mix it in, implement C{setUp} to create
    C{self.database} bound to a user database instance, and implement
    C{getExistingUserInfo} to return information about a user (such information
    should be unique per test method).
    c           
      C   s–   t dƒD ]D}|  ¡ \}}}}}}}| j |¡}	|  |	j|¡ |  |	j|¡ |  |	j|¡ |  |	j|¡ |  |	j	|¡ |  |	j
|¡ |  |	j|¡ qdS )zc
        I{getpwuid} accepts a uid and returns the user record associated with
        it.
        é   N)ÚrangeÚgetExistingUserInfoÚdatabaser
   ÚassertEqualÚpw_nameÚ	pw_passwdÚpw_uidÚpw_gidÚpw_gecosÚpw_dirÚpw_shell©
ÚselfÚiÚusernameÚpasswordÚuidÚgidÚgecosÚdirÚshellÚentryr   r   r   Útest_getpwuidC   ó   ôz$UserDatabaseTestsMixin.test_getpwuidc                 C   s   |   t| jjt¡ dS )zu
        I{getpwuid} raises L{KeyError} when passed a uid which does not exist
        in the user database.
        N)ÚassertRaisesr   r   r
   ÚINVALID_UID©r   r   r   r   Útest_noSuchUIDV   ó   z%UserDatabaseTestsMixin.test_noSuchUIDc           
      C   s–   t dƒD ]D}|  ¡ \}}}}}}}| j |¡}	|  |	j|¡ |  |	j|¡ |  |	j|¡ |  |	j|¡ |  |	j	|¡ |  |	j
|¡ |  |	j|¡ qdS )zh
        I{getpwnam} accepts a username and returns the user record associated
        with it.
        r   N)r   r   r   Úgetpwnamr   r   r   r   r   r   r   r   r   r   r   r   Útest_getpwnam]   r)   z$UserDatabaseTestsMixin.test_getpwnamc                 C   s"   t }trt}|  || jjd¡ dS )zN
        L{getpwnam} rejects a non-L{str} username with an exception.
        ó
   i-am-bytesN)Ú	TypeErrorr   Ú	Exceptionr*   r   r/   )r   Úexc_typer   r   r   Útest_getpwnamRejectsBytesp   s   z0UserDatabaseTestsMixin.test_getpwnamRejectsBytesc                 C   ó   |   t| jjd¡ dS )zz
        I{getpwnam} raises L{KeyError} when passed a username which does not
        exist in the user database.
        z.nosuchuserexiststhenameistoolongandhasinittooN)r*   r   r   r/   r,   r   r   r   Útest_noSuchName{   s
   ýz&UserDatabaseTestsMixin.test_noSuchNamec           
      C   sd   | j }|  ¡ \}}}}}}}| |¡| |¡| ¡ d fD ]}	|  t|	ƒt¡ |  t|	ƒd¡ qdS )zm
        The user record returned by I{getpwuid}, I{getpwnam}, and I{getpwall}
        has a length.
        r   é   N)	r   r   r
   r/   ÚgetpwallÚassertIsInstanceÚlenÚintr   ©
r   Údbr    r!   r"   r#   r$   r%   r&   r'   r   r   r   Útest_recordLength”   s   "þz(UserDatabaseTestsMixin.test_recordLengthc           
      C   sÜ   | j }|  ¡ \}}}}}}}| |¡| |¡| ¡ d fD ]N}	|  |	d |¡ |  |	d |¡ |  |	d |¡ |  |	d |¡ |  |	d |¡ |  |	d |¡ |  |	d |¡ |  t|	ƒtt|	ƒƒ¡ |  t	t
|	d¡ qd	S )
a:  
        The user record returned by I{getpwuid}, I{getpwnam}, and I{getpwall}
        is indexable, with successive indexes starting from 0 corresponding to
        the values of the C{pw_name}, C{pw_passwd}, C{pw_uid}, C{pw_gid},
        C{pw_gecos}, C{pw_dir}, and C{pw_shell} attributes, respectively.
        r   r   r   é   é   é   é   r8   N)r   r   r
   r/   r9   r   r;   Úlistr*   Ú
IndexErrorr   r=   r   r   r   Útest_recordIndexableŸ   s   "öz+UserDatabaseTestsMixin.test_recordIndexableN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r(   r-   r0   r5   r7   r?   rF   r   r   r   r   r   :   s    r   c                   @   ó(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚUserDatabaseTestsz$
    Tests for L{UserDatabase}.
    c                 C   s   t ƒ | _td | _dS )zC
        Create a L{UserDatabase} with no user data in it.
        r   N)r   r   r   Ú_counterr,   r   r   r   ÚsetUpº   s   zUserDatabaseTests.setUpc           	   	   C   s~   |  j d7  _ dt| j ƒ }d| }d| }| j }| j d }d| }d| }d| }| j |||||||¡ |||||||fS )	úP
        Add a new user to C{self.database} and return its information.
        r   Ú_r    r!   iè  r$   r%   r&   ©rM   Ústrr   ÚaddUser)	r   Úsuffixr    r!   r"   r#   r$   r%   r&   r   r   r   r   Á   s   
z%UserDatabaseTests.getExistingUserInfoc           
   	   C   sÄ   d}d}d}d}d}d}d}| j }| |||||||¡ | |¡g| |¡g| ¡ fD ]4\}	|  |	j|¡ |  |	j|¡ |  |	j|¡ |  |	j	|¡ |  |	j
|¡ |  |	j|¡ |  |	j|¡ q+dS )	á  
        L{UserDatabase.addUser} accepts seven arguments, one for each field of
        a L{pwd.struct_passwd}, and makes the new record available via
        L{UserDatabase.getpwuid}, L{UserDatabase.getpwnam}, and
        L{UserDatabase.getpwall}.
        ÚaliceÚsecr3té{   iÈ  zAlice,,,z/users/alicez/usr/bin/fooshN)r   rS   r
   r/   r9   r   r   r   r   r   r   r   r   )
r   r    r!   r"   r#   r$   Úhomer&   r>   r'   r   r   r   Útest_addUserÒ   s$   $ùzUserDatabaseTests.test_addUserN©rG   rH   rI   rJ   rN   r   rZ   r   r   r   r   rL   µ   s
    rL   c                   @   s2   e Zd ZdZedu rdZneZdd„ Zdd„ ZdS )ÚPwdModuleTestszÅ
    L{PwdModuleTests} runs the tests defined by L{UserDatabaseTestsMixin}
    against the built-in C{pwd} module.  This serves to verify that
    L{UserDatabase} is really a fake of that API.
    Nz2Cannot verify UserDatabase against pwd without pwdc                 C   s   t | j ¡ ƒ| _tƒ | _d S ©N)Úiterr   r9   Ú_usersÚsetÚ_uidsr,   r   r   r   rN   ú   s   zPwdModuleTests.setUpc                 C   s.   	 t | jƒ}|j}|| jvr| j |¡ |S q)zÔ
        Read and return the next record from C{self._users}, filtering out
        any records with previously seen uid values (as these cannot be
        found with C{getpwuid} and only cause trouble).
        )Únextr_   r   ra   Úadd)r   r'   r"   r   r   r   r   þ   s   

ûz"PwdModuleTests.getExistingUserInfo)	rG   rH   rI   rJ   r	   Úskipr   rN   r   r   r   r   r   r\   î   s    r\   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚShadowDatabaseTestsMixinan  
    L{ShadowDatabaseTestsMixin} defines tests which apply to any shadow user
    database implementation.  Subclasses should mix it in, implement C{setUp} to
    create C{self.database} bound to a shadow user database instance, and
    implement C{getExistingUserInfo} to return information about a user (such
    information should be unique per test method).
    c              
   C   s¶   t dƒD ]T}|  ¡ \	}}}}}}}}	}
| j |¡}|  |j|¡ |  |j|¡ |  |j|¡ |  |j|¡ |  |j	|¡ |  |j
|¡ |  |j|¡ |  |j|	¡ |  |j|
¡ qdS )zh
        L{getspnam} accepts a username and returns the user record associated
        with it.
        r   N)r   r   r   Úgetspnamr   Úsp_namÚsp_pwdÚ	sp_lstchgÚsp_minÚsp_maxÚsp_warnÚsp_inactÚ	sp_expireÚsp_flag)r   r   r    r!   Ú
lastChangeÚminÚmaxÚwarnÚinactÚexpireÚflagr'   r   r   r   Útest_getspnam  s.   öéz&ShadowDatabaseTestsMixin.test_getspnamc                 C   r6   )zz
        I{getspnam} raises L{KeyError} when passed a username which does not
        exist in the user database.
        rV   N)r*   r   r   rf   r,   r   r   r   r7   3  r.   z(ShadowDatabaseTestsMixin.test_noSuchNamec                 C   r6   )zm
        I{getspnam} raises L{TypeError} when passed a L{bytes}, just like
        L{spwd.getspnam}.
        r1   N)r*   r2   r   rf   r,   r   r   r   Útest_getspnamBytes:  r.   z+ShadowDatabaseTestsMixin.test_getspnamBytesc                 C   sR   | j }|  ¡ d }| |¡| ¡ d fD ]}|  t|ƒt¡ |  t|ƒd¡ qdS )zf
        The shadow user record returned by I{getspnam} and I{getspall} has a
        length.
        r   é	   N)r   r   rf   Úgetspallr:   r;   r<   r   )r   r>   r    r'   r   r   r   r?   A  s   þz*ShadowDatabaseTestsMixin.test_recordLengthc              	   C   sø   | j }|  ¡ \	}}}}}}}}	}
| |¡| ¡ d fD ]^}|  |d |¡ |  |d |¡ |  |d |¡ |  |d |¡ |  |d |¡ |  |d |¡ |  |d |¡ |  |d |	¡ |  |d	 |
¡ |  t|ƒtt|ƒƒ¡ |  tt	|d
¡ qdS )aS  
        The shadow user record returned by I{getpwnam} and I{getspall} is
        indexable, with successive indexes starting from 0 corresponding to the
        values of the C{sp_nam}, C{sp_pwd}, C{sp_lstchg}, C{sp_min}, C{sp_max},
        C{sp_warn}, C{sp_inact}, C{sp_expire}, and C{sp_flag} attributes,
        respectively.
        r   r   r   r@   rA   rB   rC   r8   é   ry   N)
r   r   rf   rz   r   r;   rD   r*   rE   r   )r   r>   r    r!   rp   rq   rr   rs   rt   ru   rv   r'   r   r   r   rF   L  s2   öôz-ShadowDatabaseTestsMixin.test_recordIndexableN)	rG   rH   rI   rJ   rw   r7   rx   r?   rF   r   r   r   r   re     s    re   c                   @   rK   )	ÚShadowDatabaseTestsz&
    Tests for L{ShadowDatabase}.
    c                 C   s   t ƒ | _d| _dS )zE
        Create a L{ShadowDatabase} with no user data in it.
        r   N)r   r   rM   r,   r   r   r   rN   t  s   
zShadowDatabaseTests.setUpc                 C   s¤   |  j d7  _ dt| j ƒ }d| }d| }| j d }| j d }| j d }| j d }| j d }| j d	 }	| j d
 }
| j ||||||||	|
¡	 ||||||||	|
f	S )rO   r   rP   r    r!   r   r@   rA   rB   rC   r8   rQ   )r   rT   r    r!   rp   rq   rr   rs   rt   ru   rv   r   r   r   r   {  s   






ÿz'ShadowDatabaseTests.getExistingUserInfoc                 C   sâ   d}d}d}d}d}d}d}d}d}	| j }
|
 |||||||||	¡	 |
 |¡g|
 ¡ fD ]B\}|  |j|¡ |  |j|¡ |  |j|¡ |  |j|¡ |  |j	|¡ |  |j
|¡ |  |j|¡ |  |j|¡ |  |j|	¡ q,d	S )
rU   rV   rW   é   é*   éi   é   r@   i  N)r   rS   rf   rz   r   rg   rh   ri   rj   rk   rl   rm   rn   ro   )r   r    r!   rp   rq   rr   rs   rt   ru   rv   r>   r'   r   r   r   rZ     s,   ÷z ShadowDatabaseTests.test_addUserNr[   r   r   r   r   r|   o  s
    r|   c                   @   sD   e Zd ZdZedu rdZne ¡ dkrdZneZdd„ Z	dd	„ Z
dS )
ÚSPwdModuleTestszË
    L{SPwdModuleTests} runs the tests defined by L{ShadowDatabaseTestsMixin}
    against the built-in C{spwd} module.  This serves to verify that
    L{ShadowDatabase} is really a fake of that API.
    Nz6Cannot verify ShadowDatabase against spwd without spwdr   z1Cannot access shadow user database except as rootc                 C   s   t | j ¡ ƒ| _d S r]   )r^   r   rz   r_   r,   r   r   r   rN   ¾  s   zSPwdModuleTests.setUpc                 C   s
   t | jƒS )zF
        Read and return the next record from C{self._users}.
        )rb   r_   r,   r   r   r   r   Á  s   
z#SPwdModuleTests.getExistingUserInfo)rG   rH   rI   rJ   Úspwdrd   ÚosÚgetuidr   rN   r   r   r   r   r   r   °  s    r   )rJ   r	   Ú_pwdÚImportErrorr‚   Ú_spwdrƒ   Úoperatorr   Útwisted.python.compatr   Útwisted.python.fakepwdr   r   Útwisted.trial.unittestr   r   r   r+   r   rL   r\   re   r|   r   r   r   r   r   Ú<module>   s6   ÿÿ{9cA