Ϫf[;4dZ ddlZeZ ddlZeZddlZddlmZddl m Z ddl m Z m Z ddlmZdZdZeZGd d ZGd d eeZGd deeZGddZGddeeZGddeeZy#e$rdZYwxYw#e$rdZYwxYw)z& Tests for L{twisted.python.fakepwd}. N)getitem)_PYPY)ShadowDatabase UserDatabase)TestCaseicxt}t tj||dz}|S#t$rY|SwxYw)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}. )SYSTEM_UID_MAXpwdgetpwuidKeyError)guesss B/usr/lib/python3/dist-packages/twisted/python/test/test_fakepwd.pyfindInvalidUIDr sT E   U#   L   L  s , 99c:eZdZdZdZdZdZdZdZdZ dZ y ) 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). ctdD]}|j\}}}}}}}|jj|} |j | j ||j | j ||j | j||j | j||j | j||j | j||j | j|y)zc I{getpwuid} accepts a uid and returns the user record associated with it. N) rangegetExistingUserInfodatabaser assertEqualpw_name pw_passwdpw_uidpw_gidpw_gecospw_dirpw_shell selfiusernamepassworduidgidgecosdirshellentrys r test_getpwuidz$UserDatabaseTestsMixin.test_getpwuidCs q 4A>B>V>V>X ;HhS%eMM**3/E   U]]H 5   U__h 7   U\\3 /   U\\3 /   U^^U 3   U\\3 /   U^^U 3 4cb|jt|jjty)zu I{getpwuid} raises L{KeyError} when passed a uid which does not exist in the user database. N) assertRaisesr rr INVALID_UIDr!s rtest_noSuchUIDz%UserDatabaseTestsMixin.test_noSuchUIDVs (DMM$:$:KHr,ctdD]}|j\}}}}}}}|jj|} |j | j ||j | j ||j | j||j | j||j | j||j | j||j | j|y)zh I{getpwnam} accepts a username and returns the user record associated with it. rN) rrrgetpwnamrrrrrrrrr s r test_getpwnamz$UserDatabaseTestsMixin.test_getpwnam]s q 4A>B>V>V>X ;HhS%eMM**84E   U]]H 5   U__h 7   U\\3 /   U\\3 /   U^^U 3   U\\3 /   U^^U 3 4r,cvt}trt}|j||jj dy)zN L{getpwnam} rejects a non-L{str} username with an exception. i-am-bytesN) TypeErrorr Exceptionr.rr3)r!exc_types rtest_getpwnamRejectsBytesz0UserDatabaseTestsMixin.test_getpwnamRejectsBytesps/ !H (DMM$:$:MJr,cZ|jt|jjdy)zz I{getpwnam} raises L{KeyError} when passed a username which does not exist in the user database. z.nosuchuserexiststhenameistoolongandhasinittooN)r.r rr3r0s rtest_noSuchNamez&UserDatabaseTestsMixin.test_noSuchName{s'   MM " "  r,c2|j}|j\}}}}}}}|j||j||j dfD]<} |j t | t|jt | d>y)zm The user record returned by I{getpwuid}, I{getpwnam}, and I{getpwall} has a length. rN) rrr r3getpwallassertIsInstancelenintr r!dbr#r$r%r&r'r(r)r*s rtest_recordLengthz(UserDatabaseTestsMixin.test_recordLengths ]]:>:R:R:T7(CeS%kk#& H(=r{{}Q?OP ,E  ! !#e*c 2   SZ + ,r,c v|j}|j\}}}}}}}|j||j||j dfD]} |j | d||j | d||j | d||j | d||j | d||j | d||j | d||j t | t t| |jtt| dy ) 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. rr rr>N) rrr r3r?rrAlistr. IndexErrorrrCs rtest_recordIndexablez+UserDatabaseTestsMixin.test_recordIndexables]]:>:R:R:T7(CeS%kk#& H(=r{{}Q?OP =E   U1Xx 0   U1Xx 0   U1Xs +   U1Xs +   U1Xu -   U1Xs +   U1Xu -   SZT%[)9 :   j'5! < =r,N) __name__ __module__ __qualname____doc__r+r1r4r:r<rErMr,rrr:s,4&I4& K 2 ,=r,rc"eZdZdZdZdZdZy)UserDatabaseTestsz$ Tests for L{UserDatabase}. c>t|_tdz|_y)zC Create a L{UserDatabase} with no user data in it. r N)rrr _counterr0s rsetUpzUserDatabaseTests.setUps% &* r,c |xjdz c_dt|jz}d|z}d|z}|j}|jdz}d|z}d|z}d|z}|jj||||||||||||||fS) P Add a new user to C{self.database} and return its information. r _r#r$ir'r(r)rVstrraddUser) r!suffixr#r$r%r&r'r(r)s rrz%UserDatabaseTests.getExistingUserInfos  s4==))&&mmmmd"& fn&  h#sE3N(CeS%@@r,c bd}d}d}d}d}d}d}|j}|j||||||||j|g|j|g|j fD]\} |j | j ||j | j||j | j||j | j||j | j||j | j||j | j|y)  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}. alicesecr3t{izAlice,,,z /users/alicez/usr/bin/fooshN) rr]r r3r?rrrrrrrr) r!r#r$r%r&r'homer)rDr*s r test_addUserzUserDatabaseTests.test_addUsers   ]] 8XsCeDS)*R[[-B,CR[[]S 4GU   U]]H 5   U__h 7   U\\3 /   U\\3 /   U^^U 3   U\\4 0   U^^U 3 4r,NrNrOrPrQrWrrerRr,rrTrTs+A"4r,rTc*eZdZdZedZneZdZdZy)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 pwdcrt|jj|_t |_yN)iterrr?_usersset_uidsr0s rrWzPwdModuleTests.setUps$4==1134 U r,c t|j}|j}||jvr|jj ||SM)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). )nextrlrrnadd)r!r*r%s rrz"PwdModuleTests.getExistingUserInfosD %E,,C$**$ s# r,) rNrOrPrQr skiprrWrrRr,rrhrhs"  {C r,rhc.eZdZdZdZdZdZdZdZy)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 tdD]4}|j\ }}}}}}}} } |jj|} |j | j ||j | j ||j | j||j | j||j | j||j | j||j | j||j | j| |j | j| 7y)zh L{getspnam} accepts a username and returns the user record associated with it. rN)rrrgetspnamrsp_namsp_pwd sp_lstchgsp_minsp_maxsp_warnsp_inact sp_expiresp_flag) r!r"r#r$ lastChangeminmaxwarninactexpireflagr*s r test_getspnamz&ShadowDatabaseTestsMixin.test_getspnams q 2A((* MM**84E   U\\8 4   U\\8 4   U__j 9   U\\3 /   U\\3 /   U]]D 1   U^^U 3   U__f 5   U]]D 1/ 2r,cZ|jt|jjdy)zz I{getspnam} raises L{KeyError} when passed a username which does not exist in the user database. raN)r.r rrvr0s rr<z(ShadowDatabaseTestsMixin.test_noSuchName3s (DMM$:$:GDr,cZ|jt|jjdy)zm I{getspnam} raises L{TypeError} when passed a L{bytes}, just like L{spwd.getspnam}. r6N)r.r7rrvr0s rtest_getspnamBytesz+ShadowDatabaseTestsMixin.test_getspnamBytes:s )T]]%;%;]Kr,c|j}|jd}|j||jdfD]<}|j t |t |jt |d>y)zf The shadow user record returned by I{getspnam} and I{getspall} has a length. r N)rrrvgetspallr@rArBr)r!rDr#r*s rrEz*ShadowDatabaseTestsMixin.test_recordLengthAsp ]]++-a0kk(+R[[]1-=> ,E  ! !#e*c 2   SZ + ,r,c |j}|j\ }}}}}}}} } |j||jdfD] } |j | d||j | d||j | d||j | d||j | d||j | d||j | d||j | d| |j | d | |j t | t t | |jtt| d  y ) 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. rr rrGrHrIrJr>rN) rrrvrrrArKr.rLr) r!rDr#r$rrrrrrrr*s rrMz-ShadowDatabaseTestsMixin.test_recordIndexableLsH]]  $ $ &         kk(+R[[]1-=> =E   U1Xx 0   U1Xx 0   U1Xz 2   U1Xs +   U1Xs +   U1Xt ,   U1Xu -   U1Xv .   U1Xt ,   SZT%[)9 :   j'5! < =r,N) rNrOrPrQrr<rrErMrRr,rrtrt s"2<EL , =r,rtc"eZdZdZdZdZdZy)ShadowDatabaseTestsz& Tests for L{ShadowDatabase}. c0t|_d|_y)zE Create a L{ShadowDatabase} with no user data in it. rN)rrrVr0s rrWzShadowDatabaseTests.setUpts'(  r,c |xjdz c_dt|jz}d|z}d|z}|jdz}|jdz}|jdz}|jdz}|jdz}|jd z} |jd z} |jj|||||||| | |||||||| | f S) rYr rZr#r$rrGrHrIrJr>r[) r!r^r#r$rrrrrrrs rrz'ShadowDatabaseTests.getExistingUserInfo{s  s4==))&&]]Q& mmamma}}q  !"}}q   h CdE64 (JS$vtTTr,c d}d}d}d}d}d}d}d}d} |j} | j||||||||| | j|g| jfD]\} |j | j ||j | j ||j | j||j | j||j | j||j | j||j | j||j | j||j | j| y ) r`rarb*i rGiN)rr]rvrrrwrxryrzr{r|r}r~r) r!r#r$rrrrrrrrDr*s rrez ShadowDatabaseTests.test_addUsers3  ]] 8Xz3T5&RVWX./? 2GU   U\\8 4   U\\8 4   U__j 9   U\\3 /   U\\3 /   U]]D 1   U^^U 3   U__f 5   U]]D 1 2r,NrfrRr,rrrosU*2r,rcXeZdZdZedZnejdk7rdZneZdZ dZ y)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 spwdrz1Cannot access shadow user database except as rootcTt|jj|_yrj)rkrrrlr0s rrWzSPwdModuleTests.setUps4==1134 r,c,t|jS)zF Read and return the next record from C{self._users}. )rprlr0s rrz#SPwdModuleTests.getExistingUserInfosDKK  r,) rNrOrPrQspwdrrosgetuidrrWrrRr,rrrs7  |G  B5!r,r)rQr _pwd ImportErrorr_spwdroperatorrtwisted.python.compatrtwisted.python.fakepwdrrtwisted.trial.unittestrr rr/rrTrhrtrrrRr,rrs C D '?+. x=x=v64"864rX5<`=`=F>2($<>2B!h 8!M  C Ds"BB B  B  BB