Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/test/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyc


=Nc@sdZddlZddlZddlmZddlmZddlmZm	Z	m
Z
mZddlm
Z
mZddlmZddlmZdd	lmZyddlZWnek
rdZnXyddlZWnek
rdZnXyddlZWnek
r#dZnXd
ZdejfdYZd
ejfdYZdejfdYZdejfdYZdejfdYZ dejfdYZ!dej"ej#fdYZ$dejfdYZ%dej"ej#fdYZ&dej"ej#fdYZ'dej"ej#fd YZ(d!ej"ej#fd"YZ)d#ejfd$YZ*dS(%s$
Tests for L{twisted.cred.strcred}.
iN(tplugin(tunittest(tcredentialstcheckersterrortstrcred(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assertRaisesRtInvalidAuthTypetmakeCheckertfindCheckerFactory(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_findCheckerFactoriesGscCs |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_findCheckerFactoryPs(RRR$R((((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyREs		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_isCheckerds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_badFormatArgStringnscs+fd}jjjj|S(sE
        Test that the checker works with valid credentials.
        csj|jjdS(N(tassertEqualR*tusername(R?(R(s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyt
_gotAvatar{s(R4trequestAvatarIdR*taddCallback(RR@((Rs=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_memoryCheckerSucceedsws	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)Zs		
				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	deee
fkrgZxKedfe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|jj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
jd	|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.pyR5scCs-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_fileCheckerFailsUsername.scCs"|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_fileCheckerFailsPassword6scCs0|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'terrorOutputtStringIORRRrR!tinvalidFileWarningtgetvalue(Rt	oldOutputt	newOutputR4((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_warnWithBadFilenameFs(RRt__doc__R5RrR;RsRtRuRwR~(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyRks	
		
				tDummyOptionscBseZdZRS(s@
    Simple options for testing L{strcred.AuthOptionMixin}.
    (RRR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyRTstTestCheckerOptionscBsPeZdZdZdZdZdZdZdZdZ	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_createsList]s		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_invalidAuthErrorjs		cCst}|jddddg|d}|jt|tjd|jt|tjd|tjd}|tjd}|jtj	j
||jtj	j
||jtj|j|jtj|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chdAnonymoustchdUserPass((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_createsDictionary{s	

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_checkerFactoriesForOptHelpAuthtassertNotInRtappend(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	|jqGqGWdS(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.pyR[s	
					
	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	Zd
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%twriteRtFilePasswordDBtgoodCheckert_hasht
badCheckertAllowAnonymousAccesstanonChecker(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
|jdS(sO
        Test that the supportsInterface method behaves appropriately.
        N(RR6tsupportsInterfaceRR9tassertFalseRJRRtUnsupportedInterfacest
addCheckerR(RR((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyttest_supportsInterfaces	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_supportsAllInterfacess
	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_supportsCheckerFactorys
			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_canAddSupportedChecker(s	
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_failOnAddingUnsupportedChecker9s	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_unsupportedInterfaceErrorCs
	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_helpAuthLimitsOutputOs	
cCst}d}x-tjD]}|j|s|}PqqW|j|dtj}||_|jt	|j
ddg|jtj|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
RQtImportErrorRdRVRZRtTestCaseRRR)RHRPRktOptionstAuthOptionMixinRRRRRRR(((s=/usr/lib/python2.7/dist-packages/twisted/test/test_strcred.pyt<module>sF"





	9XQu