o
    >h                     @   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rTerTd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Zdd	d
Z	dddZ
dddZdddZdefddZded fddZ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   thisispasswordreturnNc                 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   {/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-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%   r0   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).
        r3   r4   zmemory:testuser:testpasswordr.   r,   Nr6   r8   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.
        r3   r4   r.   r   s   fake)r   r7   r   r$   r   r   r   assertFailurerequestAvatarIdr   UnauthorizedLogin)r   checkerinvalidr   r   r    test_authFailurel   s   z!MakeServiceTests.test_authFailurec                    sf   j ddj g j d d }tj  | }dtttd f ddf 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.
        r3   r4   r.   r;   usernamer   r   Nc                    s    |  j d S )N)r%   rB   )rB   correctr   r   r    checkSuccess   s   z7MakeServiceTests.test_authSuccess.<locals>.checkSuccess)
r   r7   r   r   r   r=   r   bytesr   addCallback)r   r?   drE   r   rC   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%   setcheckerskeysr	   r
   )r   r)   r*   rJ   r   r   r    test_checkers   s   
zMakeServiceTests.test_checkers)r   N)__name__
__module____qualname____doc__r   skipunixr   r!   r+   r2   r9   r:   r   rA   r   rI   rN   r   r   r   r    r      s    

	


r   N)rR   typingr   r   r   twisted.application.internetr   twisted.credr   twisted.cred.checkersr   r   twisted.cred.credentialsr	   r
   r   twisted.internet.deferr   twisted.python.reflectr   twisted.trial.unittestr   r   rT   twisted.conchr   $twisted.conch.openssh_compat.factoryr   r   r   r   r   r    <module>   s   