o
    ¯b­  ã                   @   s˜   d Z ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZ e
dƒZe
dƒZe
d	ƒZerBerBerBdd
lmZ ddlmZ G dd„ deƒZdS )z!
Tests for L{twisted.conch.tap}.
é    )ÚStreamServerEndpointService)Úerror)ÚISSHPrivateKeyÚIUsernamePasswordÚUsernamePassword)ÚrequireModule)ÚTestCaseÚcryptographyÚpyasn1ztwisted.conch.unix)Útap)ÚOpenSSHFactoryc                   @   sl   e Zd ZdZes
dZesdZesdZdZdd„ Z	dd	„ Z
d
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚMakeServiceTestsz'
    Tests for L{tap.makeService}.
    zcan't run without cryptographyzCannot run without PyASN1z can't run on non-posix computers)s   iamusers   thisispasswordc                 C   sV   |   ¡ | _t| jdƒ�}| d | j¡¡ W d  ƒ n1 sw   Y  t ¡ | _dS )z/
        Create a file with two users.
        zwb+ó   :N)	ÚmktempÚfilenameÚopenÚwriteÚjoinÚusernamePasswordr   ÚOptionsÚoptions)ÚselfÚf© r   ú=/usr/lib/python3/dist-packages/twisted/conch/test/test_tap.pyÚsetUp(   s
   
ÿzMakeServiceTests.setUpc                 C   s@   t  ¡ }t  |¡}|  |t¡ |  |jjd¡ |  |jt	¡ dS )zÃ
        L{tap.makeService} returns a L{StreamServerEndpointService} instance
        running on TCP port 22, and the linked protocol factory is an instance
        of L{OpenSSHFactory}.
        é   N)
r   r   ÚmakeServiceÚassertIsInstancer   ÚassertEqualÚendpointÚ_portÚfactoryr   )r   ÚconfigÚservicer   r   r   Ú
test_basic1   s
   
zMakeServiceTests.test_basicc                 C   sN   d}|   t| jd d¡ |   t| jd d¡ |  |t| jd ƒd|f ¡ dS )z¡
        Make sure that if the C{--auth} command-line option is not passed,
        the default checkers are (for backwards compatibility): SSH and UNIX
        é   ÚcredInterfacesz)SSH should be one of the default checkersz*UNIX should be one of the default checkersÚcredCheckersz&There should be %d checkers by defaultN)ÚassertInr   r   r   r   Úlen)r   ÚnumCheckersr   r   r   Útest_defaultAuths=   s    ýýýz"MakeServiceTests.test_defaultAuthsc                 C   s0   | j  dd| j g¡ |  t| j d ƒd¡ dS )zŽ
        The C{--auth} command-line option will add a checker to the list of
        checkers, and it should be the only auth checker
        ú--authúfile:r(   é   N©r   ÚparseOptionsr   r   r*   ©r   r   r   r   Útest_authAddedT   s   zMakeServiceTests.test_authAddedc                 C   s4   | j  dd| j ddg¡ |  t| j d ƒd¡ dS )zÍ
        Multiple C{--auth} command-line options will add all checkers specified
        to the list ofcheckers, and there should only be the specified auth
        checkers (no default checkers).
        r-   r.   zmemory:testuser:testpasswordr(   r&   Nr0   r2   r   r   r   Útest_multipleAuthAdded\   s   üÿz'MakeServiceTests.test_multipleAuthAddedc                 C   sH   | j  dd| j g¡ | j d d }t| jd dƒ}|  | |¡tj¡S )z×
        The checker created by the C{--auth} command-line option returns a
        L{Deferred} that fails with L{UnauthorizedLogin} when
        presented with credentials that are unknown to that checker.
        r-   r.   r(   éÿÿÿÿr   Úfake)	r   r1   r   r   r   ÚassertFailureÚrequestAvatarIdr   ÚUnauthorizedLogin)r   ÚcheckerÚinvalidr   r   r   Útest_authFailurel   s   ÿz!MakeServiceTests.test_authFailurec                    sP   ˆj  ddˆj g¡ ˆj d d }tˆjŽ ‰ | ˆ ¡}‡ ‡fdd„}| |¡S )zË
        The checker created by the C{--auth} command-line option returns a
        L{Deferred} that returns the avatar id when presented with credentials
        that are known to that checker.
        r-   r.   r(   r5   c                    s   ˆ  | ˆ j¡ d S )N)r   Úusername)r=   ©Úcorrectr   r   r   ÚcheckSuccess…   s   z7MakeServiceTests.test_authSuccess.<locals>.checkSuccess)r   r1   r   r   r   r8   ÚaddCallback)r   r:   Údr@   r   r>   r   Útest_authSuccessz   s   


z!MakeServiceTests.test_authSuccessc                 C   s8   t  ¡ }t  |¡}|jj}|  t|j ¡ ƒt	t
h¡ dS )z³
        The L{OpenSSHFactory} built by L{tap.makeService} has a portal with
        L{ISSHPrivateKey} and L{IUsernamePassword} interfaces registered as
        checkers.
        N)r   r   r   r"   Úportalr   ÚsetÚcheckersÚkeysr   r   )r   r#   r$   rD   r   r   r   Útest_checkersŠ   s   
ÿzMakeServiceTests.test_checkersN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   Úskipr
   Úunixr   r   r%   r,   r3   r4   r<   rC   rH   r   r   r   r   r      s"    	r   N)rL   Útwisted.application.internetr   Útwisted.credr   Útwisted.cred.credentialsr   r   r   Útwisted.python.reflectr   Útwisted.trial.unittestr   r	   r
   rN   Útwisted.conchr   Ú$twisted.conch.openssh_compat.factoryr   r   r   r   r   r   Ú<module>   s   