l^Oc@sdZddlZddlZddlmZddlmZddlmZm Z m Z m Z ddl m Z mZddlmZddlmZdd lmZyddlZWnek rdZnXyddlZWnek rdZnXyddlZWnek r#dZnXd Zd ejfd YZd ejfdYZdejfdYZdejfdYZdejfdYZ dejfdYZ!dejfdYZ"dej#e j$fdYZ%dejfdYZ&dej#e j$fdYZ'dej#e j$fd YZ(d!ej#e j$fd"YZ)d#ej#e j$fd$YZ*d%ejfd&YZ+dS('s$ Tests for L{twisted.cred.strcred}. iN(tplugin(tunittest(t credentialstcheckersterrortstrcred(t cred_filetcred_anonymous(tusage(tFilePath(t UserDatabasecCsCd}x6|gtjD]}|j^qkr>|d7}q W|S(sC Helper method to produce an auth type that doesn't exist. tThisPluginDoesNotExistt_(RtfindCheckerFactoriestauthType(tinvalidAuthTypetfactory((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pytgetInvalidAuthType$s %t TestPublicAPIcBseZdZdZRS(cCs?t}|jtjtj||jtjtj|dS(sC Test that the description string cannot be empty. N(Rt assertRaisesRtInvalidAuthTypet makeCheckertfindCheckerFactory(tselftiat((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_emptyDescription2s cCs?t}|jtjtj||jtjtj|dS(sJ Test that an unrecognized auth type raises an exception. N(RRRRRR(RR((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_invalidAuthType<s (t__name__t __module__RR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyR0s tTestStrcredFunctionscBseZdZdZRS(cCsCttj}x*tjtjD]}|j||q%WdS(sO Test that findCheckerFactories returns all available plugins. N(tlistRR Rt getPluginstICheckerFactorytassertIn(RtavailablePluginstplg((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_findCheckerFactoriesIscCs |jtjdtjdS(sz Test that findCheckerFactory returns the first plugin available for a given authentication type. tfileN(tassertIdenticalRRRttheFileCheckerFactory(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_findCheckerFactoryRs(RRR$R((((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyRGs tTestMemoryCheckercBs>eZdZdZdZdZdZdZRS(cCsjtjdd|_tjdd|_tjdd|_tjdd|_tjd|_dS( Ntadmintasdftalicetfootfoobartxtyzsmemory:admin:asdf:alice:foo( RtUsernamePasswordR*R,tbadPasstbadUserRRtchecker(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pytsetUp^s cCs9|jtjj|j|jtj|jjdS(s Verifies that strcred.makeChecker('memory') returns an object that implements the L{ICredentialsChecker} interface. N( t assertTrueRtICredentialsCheckert providedByR4R!RtIUsernamePasswordtcredentialInterfaces(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_isCheckerfs cCs|jtjtjddS(s Test that an argument string which does not contain user:pass pairs (i.e., an odd number of ':' characters) raises an exception. s memory:a:b:cN(RRtInvalidAuthArgumentStringR(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_badFormatArgStringps cs+fd}jjjj|S(sE Test that the checker works with valid credentials. csj|jjdS(N(t assertEqualR*tusername(R?(R(s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyt _gotAvatar}s(R4trequestAvatarIdR*t addCallback(RR@((Rs=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_memoryCheckerSucceedsys  cCs"|j|jj|jtjS(sG Test that the checker fails with an invalid username. (t assertFailureR4RAR3RtUnauthorizedLogin(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_memoryCheckerFailsUsernamescCs"|j|jj|jtjS(sG Test that the checker fails with an invalid password. (RDR4RAR2RRE(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_memoryCheckerFailsPasswords(RRR5R;R=RCRFRG(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyR)\s   tTestAnonymousCheckercBseZdZdZRS(cCsBtjd}|jtjj||jtj|j dS(s Verifies that strcred.makeChecker('anonymous') returns an object that implements the L{ICredentialsChecker} interface. t anonymousN( RRR6RR7R8R!Rt IAnonymousR:(RR4((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyR;scs@tjd}|jtj}fd}|j|S(sI Test that we can log in anonymously using this checker. RIcsjtj|dS(N(R&Rt ANONYMOUS(tavatar(R(s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyR@s(RRRARt AnonymousRB(RR4trequestR@((Rs=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttestAnonymousAccessSucceedss(RRR;RO(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyRHs tTestUnixCheckercBseZidd6dd6ZdZdZdZdZdZd Zd Z de e e fkrgZxKe d fe d fe d ffD](\ZZedkreeg7ZqqWx0eee fD]Zddjee_qWnRS(R+R*R-R,c Cs2|tj|j|ddddddddf S(NsF/iiii(tcrypttusers(RR?((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyt_spwdsc Cstjdd|_tjdd|_tjdd|_tjdd|_tjd|_t rt }xL|j j D];\}}|j |tj|d d d |d |d qW|jt d |jntrtj|_|jt_ndS(NR*R+R,R-R.R/R0tunixsF/is/home/s/bin/shtgetpwnam(RR1R*R,R2R3RRR4tpwdR RRtitemstaddUserRQtpatchRUtspwdtgetspnamt_spwd_getspnamRS(RtdatabaseR?tpassword((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyR5s  cCstr|jt_ndS(N(RZR\R[(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttearDownscCs9|jtjj|j|jtj|jjdS(s Verifies that strcred.makeChecker('unix') returns an object that implements the L{ICredentialsChecker} interface. N( R6RR7R8R4R!RR9R:(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyR;s cs+fd}jjjj|S(sE Test that the checker works with valid credentials. csj|jjdS(N(R>R*R?(R?(R(s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyR@s(R4RAR*RB(RR@((Rs=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_unixCheckerSucceedss  cCs"|j|jj|jtjS(sG Test that the checker fails with an invalid username. (RDR4RAR3RRE(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_unixCheckerFailsUsernamescCs"|j|jj|jtjS(sG Test that the checker fails with an invalid password. (RDR4RAR2RRE(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_unixCheckerFailsPasswordsRVtswpdRQs$Required module(s) are unavailable: s, N(RRRRRSR5R_R;R`RaRbtNoneRVRZRQt availabilitytmoduletnametmethodtjointskip(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyRPs(      .  tTestFileDBCheckercBsVeZdZdZdZdZdZdZdZdZ dZ RS( s4 Test for the --auth=file:... file checker. cCstjdd|_tjdd|_tjdd|_tjdd|_|j|_t|jj dt j d |j|_ dS( NR*R+R,R-R.R/R0sadmin:asdf alice:foo sfile:( RR1R*R,R2R3tmktemptfilenameR t setContentRRR4(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyR5 scCs-d}x tjj|r(|d7}q W|S(Ns /DoesNotExistR (tostpathtexists(RRm((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyt _fakeFilenamescCs9|jtjj|j|jtj|jjdS(s Verifies that strcred.makeChecker('memory') returns an object that implements the L{ICredentialsChecker} interface. N( R6RR7R8R4R!RR9R:(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyR;s cs+fd}jjjj|S(sE Test that the checker works with valid credentials. csj|jjdS(N(R>R*R?(R?(R(s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyR@)s(R4RAR*RB(RR@((Rs=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_fileCheckerSucceeds%s  cCs"|j|jj|jtjS(sG Test that the checker fails with an invalid username. (RDR4RAR3RRE(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_fileCheckerFailsUsername0scCs"|j|jj|jtjS(sG Test that the checker fails with an invalid password. (RDR4RAR2RRE(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_fileCheckerFailsPassword8scCs0|jttjd|jttjddS(s> Test that an empty filename raises an error. R%sfile:N(Rt ValueErrorRR(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_failsWithEmptyFilename@scCsftjj}tj}|tj_tjd|j}|tj_|jtj|j dS(st When the file auth plugin is given a file that doesn't exist, it should produce a warning. sfile:N( RR't errorOutputtStringIORRRrR!tinvalidFileWarningtgetvalue(Rt oldOutputt newOutputR4((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_warnWithBadFilenameHs     ( RRt__doc__R5RrR;RsRtRuRwR~(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyRks    tTestSSHCheckercBsMeZdZyddlZddlZWnek rAdZnXdZRS(s Tests for the --auth=sshkey:... checker. The majority of the tests for the ssh public key database checker are in L{twisted.conch.test.test_checkers.SSHPublicKeyDatabaseTestCase}. iNsPyCrypto is not availablecCsBtjd}|jtjj||jtj|j dS(s Verifies that strcred.makeChecker('sshkey') returns an object that implements the L{ICredentialsChecker} interface. tsshkeyN( RRR6RR7R8R!RtISSHPrivateKeyR:(Rt sshChecker((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyR;ds(RRRtCryptotpyasn1t ImportErrorRjR;(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyRVs   t DummyOptionscBseZdZRS(s@ Simple options for testing L{strcred.AuthOptionMixin}. (RRR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyRpstTestCheckerOptionscBsPeZdZdZdZdZdZdZdZdZ RS(cCsvt}|jddg|jt|ddt}|jddddg|jt|dddS(s| Test that the --auth command line creates a list in the Options instance and appends values to it. s--authtmemoryt credCheckersiiN(Rt parseOptionsR>tlen(Rtoptions((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_createsListys   cCsTt}t}|jtj|jd|g|jtj|jd|gdS(s Test that the --auth command line raises an exception when it gets a parameter it doesn't understand. s--auths--help-auth-typeN(RRRRt UsageErrorR(RRtinvalidParameter((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_invalidAuthErrors  cCst}|jddddg|d}|jt|tjd|jt|tjd|tjd}|tjd}|jtj j ||jtj j ||j tj|j |j tj|j dS(s Test that the --auth command line creates a dictionary mapping supported interfaces to the list of credentials checkers that support it. s--authRRItcredInterfacesiiN( RRR>RRRJR9R6RR7R8R!R:(RRtchdt chdAnonymoust chdUserPass((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_createsDictionarys     cCsEt}|jddddg|j|d|dtjdS(s Test that when two --auth arguments are passed along which support the same interface, a list with both is created. s--authRRTRRN(RRR>RR9(RR((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyt test_credInterfacesProvidesListss  cCsMg}t}x7|jD])}|j|j||j|jqWdS(sN Test that the list for --help-auth does not duplicate items. N(Rt_checkerFactoriesForOptHelpAutht assertNotInRtappend(Rt authTypesRtcf((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyt!test_listDoesNotDisplayDuplicatess  cCsktj}t}||_|jt|jdgx-tjD]}|j|j |j qDWdS(s Test that the --help-auth argument correctly displays all available authentication plugins, then exits. s --help-authN( RyRt authOutputRt SystemExitRRR R!RR{(Rt newStdoutRtcheckerFactory((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_displaysListCorrectlys    cCstj}t}||_|jt|jddgx?tjjD]1}|j rG|j |j |j qGqGWdS(s Test that the --help-auth-for argument will correctly display the help file for a particular authentication plugin. s--help-auth-typeR%N( RyRRRRRRR'tauthHelptstripR!R{(RRRtline((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_displaysHelpCorrectlys    cCsDt}|jtj|jddg}|jt|ddS(s When the checker specified by --auth raises an unexpected error, it should be caught and re-raised within a L{usage.UsageError}. s--authR%s,Unexpected error: 'file' requires a filenameN(RRRRRR>tstr(RRterr((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_unexpectedExceptions  ( RRRRRRRRRR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyRws   tOptionsForUsernamePasswordcBseZejfZRS((RRRR9tsupportedInterfaces(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyRst OptionsForUsernameHashedPasswordcBseZejfZRS((RRRtIUsernameHashedPasswordR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyRstOptionsSupportsAllInterfacescBseZdZRS(N(RRRdR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyRstOptionsSupportsNoInterfacescBseZgZRS((RRR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyRstTestLimitingInterfacescBsheZdZdZdZdZdZdZdZdZ dZ d Z d Z RS( s Tests functionality that allows an application to limit the credential interfaces it can support. For the purposes of this test, we use IUsernameHashedPassword, although this will never really be used by the command line. (I have, to date, not thought of a half-decent way for a user to specify a hash algorithm via the command-line. Nor do I think it's very useful.) I should note that, at first, this test is counter-intuitive, because we're using the checker with a pre-defined hash function as the 'bad' checker. See the documentation for L{twisted.cred.checkers.FilePasswordDB.hash} for more details. cCsn|j|_t|jdjdtj|j|_tj|jd|j|_tj |_ dS(Ntwsadmin:asdf alice:foo thash( RlRmR%twriteRtFilePasswordDBt goodCheckert_hasht badCheckertAllowAnonymousAccesst anonChecker(R((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyR5s cCs|S(s> A dumb hash that doesn't really do anything. ((RtnetworkUsernametnetworkPasswordtstoredPassword((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyRscCs[t}|j|jtj|j|jtj|jtj |j |j dS(sO Test that the supportsInterface method behaves appropriately. N( RR6tsupportsInterfaceRR9t assertFalseRJRRtUnsupportedInterfacest addCheckerR(RR((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_supportsInterface!s  cCs?t}|j|jtj|j|jtjdS(s Test that the supportsInterface method behaves appropriately when the supportedInterfaces attribute is None. N(RR6RRR9RJ(RR((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_supportsAllInterfaces/s  cCsKt}tj}tj}|j|j||j|j|dS(sT Test that the supportsCheckerFactory method behaves appropriately. N(RRR'RttheAnonymousCheckerFactoryR6tsupportsCheckerFactoryR(RRtfileCFtanonCF((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_supportsCheckerFactory;s    cCst}|j|j|jd}|j|d|d|j|j|dd|j|jt|d|d|jt|dddS(s Test that when addChecker is called with a checker that implements at least one of the interfaces our application supports, it is successful. iRRiN(RRRRR&R>R(RRtiface((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_canAddSupportedCheckerFs  cCs)t}|jtj|j|jdS(s Test that when addChecker is called with a checker that does not implement any supported interfaces, it fails. N(RRRRRR(RR((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyt#test_failOnAddingUnsupportedCheckerXs  cCs8t}tjj}|jtj|jd|gdS(sx Test that the --auth command line raises an exception when it gets a checker we don't support. s--authN(RRRRRRRR(RRR((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_unsupportedInterfaceErrorbs   cCskt}x[|jD]M}t}x)|jD]}|j|r,t}q,q,W|rtjqqWdS(s Test that --help-auth will only list checkers that purport to supply at least one of the credential interfaces our application can use. N(RRtTrueR:RtFalseRR(RRRtinvalidt interface((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_helpAuthLimitsOutputns  cCst}d}x-tjD]}|j|s|}PqqW|j|dtj}||_|jt |j ddg|j tj |j dS(s Test that --help-auth-type will display a warning if you get help for an authType that does not supply at least one of the credential interfaces our application can use. s--help-auth-typeRIN(RRdRR RtassertNotIdenticalRyRRRRR!tnotSupportedWarningR{(RRtinvalidFactoryRR((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_helpAuthTypeLimitsOutput~s    ( RRRR5RRRRRRRRR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyRs    (,RRoRyttwistedRt twisted.trialRt twisted.credRRRRttwisted.pluginsRRttwisted.pythonRttwisted.python.filepathR ttwisted.python.fakepwdR RQRRdRVRZRtTestCaseRRR)RHRPRkRtOptionstAuthOptionMixinRRRRRRR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pytsH  "       9XQu