U
    ˆj¡  ã                   @   s¼   d Z ddlmZmZmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZ ed
ƒZedƒZer¨er¨ddlmZ ddlmZ G dd„ deƒZdS )z!
Tests for L{twisted.conch.tap}.
é    )ÚAnyÚTupleÚUnion)ÚStreamServerEndpointService)Úerror)ÚFilePasswordDBÚICredentialsChecker)ÚISSHPrivateKeyÚIUsernamePasswordÚUsernamePassword)ÚDeferred)ÚrequireModule)ÚTestCaseÚcryptographyztwisted.conch.unix)Útap)ÚOpenSSHFactoryc                   @   s˜   e Zd ZdZesdZesdZdZddœdd„Zddœd	d
„Z	ddœdd„Z
ddœdd„Zddœdd„Zedœdd„Zed dœdd„Zddœdd„ZdS )ÚMakeServiceTestsz'
    Tests for L{tap.makeService}.
    zcan't run without cryptographyz can't run on non-posix computers)s   iamusers   thisispasswordN)Úreturnc              	   C   sB   |   ¡ | _t| jdƒ�}| d | j¡¡ W 5 Q R X t ¡ | _dS )z/
        Create a file with two users.
        zwb+ó   :N)	ÚmktempÚfilenameÚopenÚwriteÚjoinÚusernamePasswordr   ÚOptionsÚoptions)ÚselfÚf© r   ú?/tmp/pip-unpacked-wheel-j5h_c8_q/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   ÚassertEqualZendpointZ_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
        é   Z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)ZassertInr	   r   r
   r%   Úlen)r   Z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*   Nr1   r3   r   r   r    Útest_multipleAuthAdded\   s    üÿz'MakeServiceTests.test_multipleAuthAddedc                 C   sT   | j  dd| j g¡ | j d d }|  |t¡ 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   s   fake)r   r2   r   r$   r   r   r   ZassertFailureÚrequestAvatarIdr   ZUnauthorizedLogin)r   ÚcheckerÚinvalidr   r   r    Útest_authFailurel   s     ÿz!MakeServiceTests.test_authFailurec                    sd   ˆj  ddˆj g¡ ˆj d d }tˆjŽ ‰ | ˆ ¡}tttd f ddœ‡ ‡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+   r6   r   N)Úusernamer   c                    s   ˆ  | ˆ j¡ d S )N)r%   r;   )r;   ©Úcorrectr   r   r    ÚcheckSuccess†   s    z7MakeServiceTests.test_authSuccess.<locals>.checkSuccess)
r   r2   r   r   r   r7   r   Úbytesr   ZaddCallback)r   r8   Údr>   r   r<   r    Útest_authSuccess{   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%   ÚsetZcheckersÚkeysr	   r
   )r   r'   r(   rB   r   r   r    Útest_checkers‹   s    
 ÿzMakeServiceTests.test_checkers)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚskipÚunixr   r!   r)   r-   r4   r5   r   r:   r   rA   rE   r   r   r   r    r      s   	r   N)rI   Útypingr   r   r   Ztwisted.application.internetr   Ztwisted.credr   Ztwisted.cred.checkersr   r   Ztwisted.cred.credentialsr	   r
   r   Ztwisted.internet.deferr   Ztwisted.python.reflectr   Ztwisted.trial.unittestr   r   rK   Ztwisted.conchr   Z$twisted.conch.openssh_compat.factoryr   r   r   r   r   r    Ú<module>   s   