U
    
W[W                     @   s   d Z zddlZW n ek
r(   dZY nX zddlZW n ek
rN   dZY nX zddlmZ W n ek
rx   dZY nX erererddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZmZ dd
lmZ G dd deZdS )z!
Tests for L{twisted.conch.tap}.
    N)unix)tap)OpenSSHFactory)StreamServerEndpointService)error)ISSHPrivateKey)IUsernamePasswordUsernamePassword)TestCasec                   @   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   sB   |   | _t| jd}|d| j W 5 Q R X t | _dS )z/
        Create a file with two users.
        zwb+   :N)	ZmktempfilenameopenwritejoinusernamePasswordr   Optionsoptions)selff r   =/usr/lib/python3/dist-packages/twisted/conch/test/test_tap.pysetUp4   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   makeServiceZassertIsInstancer   assertEqualZendpointZ_portfactoryr   )r   configservicer   r   r   
test_basic>   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_defaultAuthsK   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_authAddedZ   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    Nr'   r)   r   r   r   test_multipleAuthAddedc   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   Zfake)	r   r(   r   r	   r   ZassertFailurerequestAvatarIdr   ZUnauthorizedLogin)r   checkerZinvalidr   r   r   test_authFailuren   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!   r,   c                    s    |  j d S )N)r   username)r0   Zcorrectr   r   r   checkSuccess   s    z7MakeServiceTests.test_authSuccess.<locals>.checkSuccess)r   r(   r   r	   r   r-   ZaddCallback)r   r.   dr2   r   r1   r   test_authSuccess|   s    

z!MakeServiceTests.test_authSuccessc                 C   s<   t  }t |}|jj}| t|j tt	t
g 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   r5   r   r   r   test_checkers   s    

zMakeServiceTests.test_checkersN)__name__
__module____qualname____doc__cryptographyskippyasn1r   r   r   r   r#   r*   r+   r/   r4   r8   r   r   r   r   r   $   s    
	r   )r<   r=   ImportErrorr?   Ztwisted.conchr   r   Z$twisted.conch.openssh_compat.factoryr   Ztwisted.application.internetr   Ztwisted.credr   Ztwisted.cred.credentialsr   r   r	   Ztwisted.trial.unittestr
   r   r   r   r   r   <module>   s*   


