HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //usr/lib/python3/dist-packages/twisted/cred/test/__pycache__/test_strcred.cpython-38.pyc
U


W[�f�@s>dZddlmZmZddlZddlmZddlmZddl	m
Z
mZmZm
Z
ddlmZmZmZddlmZdd	lmZdd
lmZddlmZddlmZzddlZWnek
r�dZYnXzddlZWnek
r�dZYnXzddlZWnek
�rdZYnXd
d�Z Gdd�dej!�Z"Gdd�dej!�Z#Gdd�dej!�Z$Gdd�dej!�Z%Gdd�dej!�Z&Gdd�dej!�Z'Gdd�dej!�Z(Gdd�dej!�Z)Gdd �d ej*e
j+�Z,Gd!d"�d"ej!�Z-Gd#d$�d$ej*e
j+�Z.Gd%d&�d&ej*e
j+�Z/Gd'd(�d(ej*e
j+�Z0Gd)d*�d*ej*e
j+�Z1Gd+d,�d,ej!�Z2dS)-z
L{twisted.cred.strcred}.
�)�absolute_import�divisionN)�plugin)�unittest)�credentials�checkers�error�strcred)�	cred_file�cred_anonymous�	cred_unix)�usage)�NativeStringIO)�FilePath)�UserDatabase)�
requireModulecCs(d}|dd�t��D�kr$|d7}q|S)zC
    Helper method to produce an auth type that doesn't exist.
    ZThisPluginDoesNotExistcSsg|]
}|j�qS�)�authType)�.0�factoryrr�@/usr/lib/python3/dist-packages/twisted/cred/test/test_strcred.py�
<listcomp>-sz&getInvalidAuthType.<locals>.<listcomp>�_)r	�findCheckerFactories)ZinvalidAuthTyperrr�getInvalidAuthType's�
rc@seZdZdd�Zdd�ZdS)�PublicAPITestscCs.t�}|�tjtj|�|�tjtj|�dS)z9
        The description string cannot be empty.
        N�r�assertRaisesr	ZInvalidAuthType�makeChecker�findCheckerFactory��selfZiatrrr�test_emptyDescription5s�z$PublicAPITests.test_emptyDescriptioncCs.t�}|�tjtj|�|�tjtj|�dS)z@
        An unrecognized auth type raises an exception.
        Nrr rrr�test_invalidAuthType?s�z#PublicAPITests.test_invalidAuthTypeN)�__name__�
__module__�__qualname__r"r#rrrrr3s
rc@seZdZdd�Zdd�ZdS)�StrcredFunctionsTestscCs.tt���}t�tj�D]}|�||�qdS)zP
        L{strcred.findCheckerFactories} returns all available plugins.
        N)�listr	rrZ
getPluginsZICheckerFactory�assertIn)r!ZavailablePluginsZplgrrr�test_findCheckerFactoriesLsz/StrcredFunctionsTests.test_findCheckerFactoriescCs|�t�d�tj�dS)z{
        L{strcred.findCheckerFactory} returns the first plugin
        available for a given authentication type.
        �fileN)�assertIdenticalr	rr
�theFileCheckerFactory�r!rrr�test_findCheckerFactoryUs�z-StrcredFunctionsTests.test_findCheckerFactoryN)r$r%r&r*r/rrrrr'Js	r'c@s<eZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�MemoryCheckerTestscCsHt�dd�|_t�dd�|_t�dd�|_t�dd�|_t�d�|_dS)	N�admin�asdf�alice�foo�foobar�x�yzzmemory:admin:asdf:alice:foo)	r�UsernamePasswordr1r3�badPass�badUserr	r�checkerr.rrr�setUpas
zMemoryCheckerTests.setUpcCs*|�tj�|j��|�tj|jj�dS�z�
        Verifies that strcred.makeChecker('memory') returns an object
        that implements the L{ICredentialsChecker} interface.
        N�	�
assertTruer�ICredentialsChecker�
providedByr;r)r�IUsernamePassword�credentialInterfacesr.rrr�test_isCheckeris�z!MemoryCheckerTests.test_isCheckercCs|�tjtjd�dS)z�
        An argument string which does not contain user:pass pairs
        (i.e., an odd number of ':' characters) raises an exception.
        zmemory:a:b:cN)rr	ZInvalidAuthArgumentStringrr.rrr�test_badFormatArgStringss�z*MemoryCheckerTests.test_badFormatArgStringcs �fdd�}�j��j��|�S)�;
        The checker works with valid credentials.
        cs��|�jj�dS�N��assertEqualr1�username�rJr.rr�
_gotAvatar�szAMemoryCheckerTests.test_memoryCheckerSucceeds.<locals>._gotAvatar�r;�requestAvatarIdr1�addCallback�r!rLrr.r�test_memoryCheckerSucceeds|s��z-MemoryCheckerTests.test_memoryCheckerSucceedscCs|�|j�|j�tj�S�z=
        The checker fails with an invalid username.
        ��
assertFailurer;rNr:r�UnauthorizedLoginr.rrr�test_memoryCheckerFailsUsername�s�z2MemoryCheckerTests.test_memoryCheckerFailsUsernamecCs|�|j�|j�tj�S�z=
        The checker fails with an invalid password.
        �rTr;rNr9rrUr.rrr�test_memoryCheckerFailsPassword�s�z2MemoryCheckerTests.test_memoryCheckerFailsPasswordN)	r$r%r&r<rDrErQrVrYrrrrr0_s
	r0c@seZdZdd�Zdd�ZdS)�AnonymousCheckerTestscCs0t�d�}|�tj�|��|�tj|j	�dS)z�
        Verifies that strcred.makeChecker('anonymous') returns an object
        that implements the L{ICredentialsChecker} interface.
        �	anonymousN)
r	rr?rr@rAr)r�
IAnonymousrC)r!r;rrrrD�s
z$AnonymousCheckerTests.test_isCheckercs.t�d�}|�t���}�fdd�}|�|�S)z?
        We can log in anonymously using this checker.
        r[cs��tj|�dSrG)r,rZ	ANONYMOUS)Zavatarr.rrrL�szEAnonymousCheckerTests.testAnonymousAccessSucceeds.<locals>._gotAvatar)r	rrNrZ	AnonymousrO)r!r;ZrequestrLrr.r�testAnonymousAccessSucceeds�s
z1AnonymousCheckerTests.testAnonymousAccessSucceedsN)r$r%r&rDr]rrrrrZ�s
rZc@s�eZdZddd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�Zdd�Zde
eefkr�gZe
dfedfedffD]\ZZedkr�eeg7Zq�eee	e
eefD]Zdd�e�e_q�dS)�UnixCheckerTestsr2r4)r1r3cCs*t�|t�|j|d�dddddddf	�S)N�F/ri������)�spwdZstruct_spwd�crypt�users)r!rJrrr�_spwd_getspnam�s�zUnixCheckerTests._spwd_getspnamc
Cs�t�dd�|_t�dd�|_t�dd�|_t�dd�|_t�d�|_t�d	d
�|_	t�dd�|_
t�dd
�|_t�dd�|_t�d�|_
tr�t�}|j��D]*\}}|�|t�|d�dd|d|d�q�|�td|j�tr�|�td|j�dS)Nr1r2r3r4r5r6r7�unix�admin�asdf�alice�foo�foobar�x�yzr_i�z/home/z/bin/sh�getpwnamZgetspnam)rr8r1r3r9r:r	rr;�
adminBytesZ
aliceBytes�badPassBytes�badUserBytes�checkerBytes�pwdrrd�itemsZaddUserrcZpatchrnrbre)r!ZdatabaserJ�passwordrrrr<�s2
�zUnixCheckerTests.setUpcCsP|�tj�|j��|�tj|jj�|�tj�|j	��|�tj|j	j�dS)z�
        Verifies that strcred.makeChecker('unix') returns an object
        that implements the L{ICredentialsChecker} interface.
        N)
r?rr@rAr;r)rrBrCrrr.rrrrD�s�
��zUnixCheckerTests.test_isCheckercs �fdd�}�j��j��|�S)rFcs��|�jj�dSrGrHrKr.rrrL�sz=UnixCheckerTests.test_unixCheckerSucceeds.<locals>._gotAvatarrMrPrr.r�test_unixCheckerSucceeds�s��z)UnixCheckerTests.test_unixCheckerSucceedscs �fdd�}�j��j��|�S)zD
        The checker works with valid L{bytes} credentials.
        cs��|�jj�d��dS)N�utf-8)rIrorJ�decoderKr.rrrL�s�zBUnixCheckerTests.test_unixCheckerSucceedsBytes.<locals>._gotAvatar)rrrNrorOrPrr.r�test_unixCheckerSucceedsBytes�s��z.UnixCheckerTests.test_unixCheckerSucceedsBytescCs|�|j�|j�tj�SrRrSr.rrr�test_unixCheckerFailsUsername�s�z.UnixCheckerTests.test_unixCheckerFailsUsernamecCs|�|j�|j�tj�S)zF
        The checker fails with an invalid L{bytes} username.
        )rTrrrNrqrrUr.rrr�"test_unixCheckerFailsUsernameBytess

��z3UnixCheckerTests.test_unixCheckerFailsUsernameBytescCs|�|j�|j�tj�SrWrXr.rrr�test_unixCheckerFailsPasswords�z.UnixCheckerTests.test_unixCheckerFailsPasswordcCs|�|j�|j�tj�S)zF
        The checker fails with an invalid L{bytes} password.
        )rTrrrNrprrUr.rrr�"test_unixCheckerFailsPasswordBytess

��z3UnixCheckerTests.test_unixCheckerFailsPasswordBytesNrsrbrcz$Required module(s) are unavailable: z, )r$r%r&rdrer<rDrvryrzr{r|r}rsrbrcZavailability�module�name�method�join�skiprrrrr^�s6���r^c@s eZdZdZesdZdd�ZdS)�
CryptTestsz9
    L{crypt} has functions for encrypting password.
    z%Required module is unavailable: cryptc
	Csd}dD]V}zt�||�}Wntk
r4YqYnXd}|�t�||��|�t�||��qdD]�}tt|d�}|szqdd}t�||�}|d}t�||�}	|�|	�t�|�d�|�d��}	|�|	�t�||�}	|�|	�t�|�d�|�d��}	|�|	�qddS)	z4
        L{cred_unix.verifyCryptedPassword}
        zsample password ^%$)NZabz$1x1234)Z
METHOD_SHA512Z
METHOD_SHA256Z
METHOD_MD5ZMETHOD_CRYPTNzinteresting password xyzZblahfooincorrectrw)rc�	TypeErrorr?rZverifyCryptedPassword�assertFalse�getattr�encode)
r!ruZsaltZcryptedCorrectZcryptedIncorrectr�ZcryptMethodZcryptedZincorrectCrypted�resultrrr�test_verifyCryptedPassword1s@

�
�
�

�z%CryptTests.test_verifyCryptedPasswordN)r$r%r&�__doc__rcr�r�rrrrr�*sr�c@sPeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�ZdS)�FileDBCheckerTestsz*
    C{--auth=file:...} file checker.
    cCsht�dd�|_t�dd�|_t�dd�|_t�dd�|_|��|_t|j��	d�t
�d	|j�|_dS)
Nrgrhrirjrkrlrm�admin:asdf
alice:foo
�file:)
rr8r1r3r9r:�mktemp�filenamerZ
setContentr	rr;r.rrrr<ds
zFileDBCheckerTests.setUpcCsd}tj�|�r|d7}q|S)Nz
/DoesNotExistr)�os�path�exists)r!r�rrr�
_fakeFilenamens
z FileDBCheckerTests._fakeFilenamecCs*|�tj�|j��|�tj|jj�dSr=r>r.rrrrDus�z!FileDBCheckerTests.test_isCheckercs �fdd�}�j��j��|�S)rFcs��|�jj�dSrGrHrKr.rrrL�sz?FileDBCheckerTests.test_fileCheckerSucceeds.<locals>._gotAvatarrMrPrr.r�test_fileCheckerSucceedss��z+FileDBCheckerTests.test_fileCheckerSucceedscCs|�|j�|j�tj�SrRrSr.rrr�test_fileCheckerFailsUsername�s�z0FileDBCheckerTests.test_fileCheckerFailsUsernamecCs|�|j�|j�tj�SrWrXr.rrr�test_fileCheckerFailsPassword�s�z0FileDBCheckerTests.test_fileCheckerFailsPasswordcCs$|�ttjd�|�ttjd�dS)z4
        An empty filename raises an error.
        r+r�N)r�
ValueErrorr	rr.rrr�test_failsWithEmptyFilename�sz.FileDBCheckerTests.test_failsWithEmptyFilenamecCsFtjj}t�}|tj_t�d|���|tj_|�tj|�	��dS)zt
        When the file auth plugin is given a file that doesn't exist, it
        should produce a warning.
        r�N)
r
r-ZerrorOutputrr	rr�r)ZinvalidFileWarning�getvalue)r!Z	oldOutputZ	newOutputrrr�test_warnWithBadFilename�sz+FileDBCheckerTests.test_warnWithBadFilenameN)r$r%r&r�r<r�rDr�r�r�r�r�rrrrr�_s

r�c@s<eZdZdZdZed�dkr dZed�dkr0dZdd�ZdS)	�SSHCheckerTestsz�
    Tests for the C{--auth=sshkey:...} checker.  The majority of the tests for the
    ssh public key database checker are in
    L{twisted.conch.test.test_checkers.SSHPublicKeyCheckerTestCase}.
    NZcryptographyzcryptography is not availableZpyasn1zpyasn1 is not availablecCs0t�d�}|�tj�|��|�tj|j	�dS)z�
        Verifies that strcred.makeChecker('sshkey') returns an object
        that implements the L{ICredentialsChecker} interface.
        ZsshkeyN)
r	rr?rr@rAr)rZISSHPrivateKeyrC)r!Z
sshCheckerrrrrD�s
�zSSHCheckerTests.test_isChecker)r$r%r&r�r�rrDrrrrr��sr�c@seZdZdZdS)�DummyOptionsz@
    Simple options for testing L{strcred.AuthOptionMixin}.
    N)r$r%r&r�rrrrr��sr�c@sLeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dd�Z
dS)�CheckerOptionsTestscCsXt�}|�ddg�|�t|d�d�t�}|�ddddg�|�t|d�d�dS)zu
        The C{--auth} command line creates a list in the
        Options instance and appends values to it.
        �--auth�memory�credCheckers��N)r��parseOptionsrI�len�r!�optionsrrr�test_createsList�sz$CheckerOptionsTests.test_createsListcCs<t�}t�}|�tj|jd|g�|�tj|jd|g�dS)zx
        The C{--auth} command line raises an exception when it
        gets a parameter it doesn't understand.
        r��--help-auth-typeN)r�rrr
�
UsageErrorr�)r!r�ZinvalidParameterrrr�test_invalidAuthError�s��z)CheckerOptionsTests.test_invalidAuthErrorcCs�t�}|�ddddg�|d}|�t|tj�d�|�t|tj�d�|tjd}|tjd}|�tj	�
|��|�tj	�
|��|�tj|j�|�tj|j�dS)z�
        The C{--auth} command line creates a dictionary mapping supported
        interfaces to the list of credentials checkers that support it.
        r�r�r[�credInterfacesr�rN)
r�r�rIr�rr\rBr?rr@rAr)rC)r!r�ZchdZchdAnonymousZchdUserPassrrr�test_createsDictionary�s��z*CheckerOptionsTests.test_createsDictionarycCs6t�}|�ddddg�|�|d|dtj�dS)z�
        When two C{--auth} arguments are passed along which support the same
        interface, a list with both is created.
        r�r�rfr�r�N)r�r�rIrrBr�rrr� test_credInterfacesProvidesListss�z4CheckerOptionsTests.test_credInterfacesProvidesListscCs6g}t�}|��D]}|�|j|�|�|j�qdS)zG
        The list for C{--help-auth} does not duplicate items.
        N)r��_checkerFactoriesForOptHelpAuthZassertNotInr�append)r!Z	authTypesr�Zcfrrr�!test_listDoesNotDisplayDuplicatess
z5CheckerOptionsTests.test_listDoesNotDisplayDuplicatescCsHt�}t�}||_|�t|jdg�t��D]}|�|j	|�
��q,dS)zz
        The C{--help-auth} argument correctly displays all
        available authentication plugins, then exits.
        z--help-authN)rr��
authOutputr�
SystemExitr�r	rr)rr�)r!�	newStdoutr�ZcheckerFactoryrrr�test_displaysListCorrectlysz.CheckerOptionsTests.test_displaysListCorrectlycCsTt�}t�}||_|�t|jddg�tjjD] }|�	�r.|�
|�	�|���q.dS)z�
        The C{--help-auth-for} argument will correctly display the help file for a
        particular authentication plugin.
        r�r+N)rr�r�rr�r�r
r-ZauthHelp�stripr)r�)r!r�r��linerrr�test_displaysHelpCorrectly+s�z.CheckerOptionsTests.test_displaysHelpCorrectlycCs0t�}|�tj|jddg�}|�t|�d�dS)z�
        When the checker specified by C{--auth} raises an unexpected error, it
        should be caught and re-raised within a L{usage.UsageError}.
        r�r+z,Unexpected error: 'file' requires a filenameN)r�rr
r�r�rI�str)r!r��errrrr�test_unexpectedException:s�
�z,CheckerOptionsTests.test_unexpectedExceptionN)r$r%r&r�r�r�r�r�r�r�r�rrrrr��s

r�c@seZdZejfZdS)�OptionsForUsernamePasswordN)r$r%r&rrB�supportedInterfacesrrrrr�Gsr�c@seZdZejfZdS)� OptionsForUsernameHashedPasswordN)r$r%r&rZIUsernameHashedPasswordr�rrrrr�Lsr�c@seZdZdZdS)�OptionsSupportsAllInterfacesN�r$r%r&r�rrrrr�Qsr�c@seZdZgZdS)�OptionsSupportsNoInterfacesNr�rrrrr�Vsr�c@s`eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dS)�LimitingInterfacesTestsa�
    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.
    c	Cs\|��|_t|jd��}|�d�W5QRXt�|j�|_tj|j|jd�|_t�	�|_
dS)N�wbr�)�hash)r�r��open�writerZFilePasswordDB�goodChecker�_hash�
badCheckerZAllowAnonymousAccess�anonChecker)r!�frrrr<ls
�zLimitingInterfacesTests.setUpcCs|S)z>
        A dumb hash that doesn't really do anything.
        r)r!ZnetworkUsernameZnetworkPasswordZstoredPasswordrrrr�vszLimitingInterfacesTests._hashcCsBt�}|�|�tj��|�|�tj��|�tj	|j
|j�dS)zE
        The supportsInterface method behaves appropriately.
        N)r�r?�supportsInterfacerrBr�r\rr	�UnsupportedInterfaces�
addCheckerr�r�rrr�test_supportsInterface}s
�
��z.LimitingInterfacesTests.test_supportsInterfacecCs.t�}|�|�tj��|�|�tj��dS)z|
        The supportsInterface method behaves appropriately
        when the supportedInterfaces attribute is None.
        N)r�r?r�rrBr\r�rrr�test_supportsAllInterfaces�s
�
�z2LimitingInterfacesTests.test_supportsAllInterfacescCs6t�}tj}tj}|�|�|��|�|�|��dS)zJ
        The supportsCheckerFactory method behaves appropriately.
        N)r�r
r-r�theAnonymousCheckerFactoryr?�supportsCheckerFactoryr�)r!r�ZfileCFZanonCFrrr�test_supportsCheckerFactory�s
z3LimitingInterfacesTests.test_supportsCheckerFactorycCs|t�}|�|j�|jd}|�|d|d|j�|�|dd|j�|�t|d|�d�|�t|d�d�dS)z�
        When addChecker is called with a checker that implements at least one
        of the interfaces our application supports, it is successful.
        rr�r�r�N)r�r�r�r�r,rIr�)r!r�Zifacerrr�test_canAddSupportedChecker�s
�z3LimitingInterfacesTests.test_canAddSupportedCheckercCst�}|�tj|j|j�dS)z~
        When addChecker is called with a checker that does not implement any
        supported interfaces, it fails.
        N)r�rr	r�r�r�r�rrr�#test_failOnAddingUnsupportedChecker�s
�z;LimitingInterfacesTests.test_failOnAddingUnsupportedCheckercCs(t�}tjj}|�tj|jd|g�dS)zq
        The C{--auth} command line raises an exception when it
        gets a checker we don't support.
        r�N)r�rr�rrr
r�r�)r!r�rrrr�test_unsupportedInterfaceError�s�z6LimitingInterfacesTests.test_unsupportedInterfaceErrorcCsBt�}|��D].}d}|jD]}|�|�rd}q|rt���qdS)z�
        C{--help-auth} will only list checkers that purport to
        supply at least one of the credential interfaces our
        application can use.
        TFN)r�r�rCr�r	r�)r!r�rZinvalidZ	interfacerrr�test_helpAuthLimitsOutput�s

z1LimitingInterfacesTests.test_helpAuthLimitsOutputcCslt�}d}t��D]}|�|�s|}q*q|�|d�t�}||_|�t|j	ddg�|�
tj|���dS)z�
        C{--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.
        Nr�r[)
r�r	rr�ZassertNotIdenticalrr�rr�r�r)ZnotSupportedWarningr�)r!r�ZinvalidFactoryrr�rrr�test_helpAuthTypeLimitsOutput�s

�z5LimitingInterfacesTests.test_helpAuthTypeLimitsOutputN)r$r%r&r�r<r�r�r�r�r�r�r�r�r�rrrrr�[s

r�)3r�Z
__future__rrr�ZtwistedrZ
twisted.trialrZtwisted.credrrrr	Ztwisted.pluginsr
rrZtwisted.pythonr
Ztwisted.python.compatrZtwisted.python.filepathrZtwisted.python.fakepwdrZtwisted.python.reflectrrc�ImportErrorrsrbrZTestCaserr'r0rZr^r�r�r�ZOptionsZAuthOptionMixinr�r�r�r�r�r�r�rrrr�<module>sN


9z5Qt