ó
[³XMc           @   s¹   d  Z  y d d l Z Wn e k
 r/ d Z n Xd d l m Z d d l m Z d d l m	 Z	 d d l
 m Z d d d „  ƒ  YZ d	 e e f d
 „  ƒ  YZ d e e f d „  ƒ  YZ d S(   s&   
Tests for L{twisted.python.fakepwd}.
iÿÿÿÿN(   t   getitem(   t   TestCase(   t   UserDatabase(   t   sett   UserDatabaseTestsMixinc           B   sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s^  
    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Ó   xÌ t  d ƒ D]¾ } |  j ƒ  \ } } } } } } } |  j j | ƒ }	 |  j |	 j | ƒ |  j |	 j | ƒ |  j |	 j | ƒ |  j |	 j | ƒ |  j |	 j	 | ƒ |  j |	 j
 | ƒ |  j |	 j | ƒ q Wd S(   sc   
        I{getpwuid} accepts a uid and returns the user record associated with
        it.
        i   N(   t   ranget   getExistingUserInfot   databaset   getpwuidt   assertEqualst   pw_namet	   pw_passwdt   pw_uidt   pw_gidt   pw_gecost   pw_dirt   pw_shell(
   t   selft   it   usernamet   passwordt   uidt   gidt   gecost   dirt   shellt   entry(    (    sD   /usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyt   test_getpwuid   s    !c         C   s   |  j  t |  j j d ƒ d S(   su   
        I{getpwuid} raises L{KeyError} when passed a uid which does not exist
        in the user database.
        ióÿÿÿN(   t   assertRaisest   KeyErrorR   R   (   R   (    (    sD   /usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyt   test_noSuchUID1   s    c   
      C   sÓ   xÌ t  d ƒ D]¾ } |  j ƒ  \ } } } } } } } |  j j | ƒ }	 |  j |	 j | ƒ |  j |	 j | ƒ |  j |	 j | ƒ |  j |	 j | ƒ |  j |	 j	 | ƒ |  j |	 j
 | ƒ |  j |	 j | ƒ q Wd S(   sh   
        I{getpwnam} accepts a username and returns the user record associated
        with it.
        i   N(   R   R   R   t   getpwnamR	   R
   R   R   R   R   R   R   (
   R   R   R   R   R   R   R   R   R   R   (    (    sD   /usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyt   test_getpwnam9   s    !c         C   s   |  j  t |  j j d ƒ d S(   sz   
        I{getpwnam} raises L{KeyError} when passed a username which does not
        exist in the user database.
        s.   nosuchuserexiststhenameistoolongandhasinittooN(   R   R   R   R   (   R   (    (    sD   /usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyt   test_noSuchNameM   s    c   
      C   sz   |  j  } |  j ƒ  \ } } } } } } } xI | j | ƒ | j | ƒ | j ƒ  d g D] }	 |  j t |	 ƒ t ƒ qV Wd S(   sm   
        The user record returned by I{getpwuid}, I{getpwnam}, and I{getpwall}
        has a length.
        i    N(   R   R   R   R   t   getpwallt   assertIsInstancet   lent   int(
   R   t   dbR   R   R   R   R   R   R   R   (    (    sD   /usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyt   test_recordLengthX   s    	!2c   
      C   s(  |  j  } |  j ƒ  \ } } } } } } } x÷ | j | ƒ | j | ƒ | j ƒ  d g D]Ê }	 |  j |	 d | ƒ |  j |	 d | ƒ |  j |	 d | ƒ |  j |	 d | ƒ |  j |	 d | ƒ |  j |	 d | ƒ |  j |	 d | ƒ |  j t |	 ƒ t t |	 ƒ ƒ ƒ |  j t	 t
 |	 d ƒ qV Wd	 S(
   s:  
        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.
        i    i   i   i   i   i   i   i   N(   R   R   R   R   R"   R	   R$   t   listR   t
   IndexErrorR    (
   R   R&   R   R   R   R   R   R   R   R   (    (    sD   /usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyt   test_recordIndexablec   s    	!2"(	   t   __name__t
   __module__t   __doc__R   R   R    R!   R'   R*   (    (    (    sD   /usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyR      s   					t   UserDatabaseTestsc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s$   
    Tests for L{UserDatabase}.
    c         C   s   t  ƒ  |  _ d |  _ d S(   sC   
        Create a L{UserDatabase} with no user data in it.
        i    N(   R   R   t   _counter(   R   (    (    sD   /usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyt   setUp~   s    c   	      C   s¥   |  j  d 7_  d t |  j  ƒ } d | } d | } |  j  } |  j  d } d | } d | } d | } |  j j | | | | | | | ƒ | | | | | | | f S(	   sP   
        Add a new user to C{self.database} and return its information.
        i   t   _R   R   iè  R   R   R   (   R/   t   strR   t   addUser(	   R   t   suffixR   R   R   R   R   R   R   (    (    sD   /usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyR   †   s    

	


"c   
      C   s˜  d } d } d } d } d } d } d } |  j  } | j | | | | | | | ƒ x« | j | ƒ | j | ƒ g D]‹ }	 |  j |	 j | ƒ |  j |	 j | ƒ |  j |	 j | ƒ |  j |	 j | ƒ |  j |	 j	 | ƒ |  j |	 j
 | ƒ |  j |	 j | ƒ qq W| j ƒ  \ }	 |  j |	 j | ƒ |  j |	 j | ƒ |  j |	 j | ƒ |  j |	 j | ƒ |  j |	 j	 | ƒ |  j |	 j
 | ƒ |  j |	 j | ƒ d S(	   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}.
        t   alicet   secr3ti{   iÈ  s   Alice,,,s   /users/alices   /usr/bin/fooshN(   R   R3   R   R   R	   R
   R   R   R   R   R   R   R"   (
   R   R   R   R   R   R   t   homeR   R&   R   (    (    sD   /usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyt   test_addUser˜   s2    	%(   R+   R,   R-   R0   R   R8   (    (    (    sD   /usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyR.   z   s   		t   PwdModuleTestsc           B   s5   e  Z d  Z e d k r! d Z n  d „  Z d „  Z RS(   sÅ   
    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.
    s2   Cannot verify UserDatabase against pwd without pwdc         C   s1   t  |  _ t |  j j ƒ  ƒ |  _ t ƒ  |  _ d  S(   N(   t   pwdR   t   iterR"   t   _usersR   t   _uids(   R   (    (    sD   /usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyR0   È   s    	c         C   sI   xB t  rD |  j j ƒ  } | j |  j k r |  j j | j ƒ | Sq Wd S(   sÔ   
        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).
        N(   t   TrueR<   t   nextR   R=   t   add(   R   R   (    (    sD   /usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyR   Î   s
    	N(   R+   R,   R-   R:   t   Nonet   skipR0   R   (    (    (    sD   /usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyR9   ¾   s
   		(    (   R-   R:   t   ImportErrorRA   t   operatorR    t   twisted.trial.unittestR   t   twisted.python.fakepwdR   t   twisted.python.compatR   R   R.   R9   (    (    (    sD   /usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyt   <module>   s   
eD