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   