
    Vh                         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e      Zy)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                   r    e Zd Z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efdZded   fdZddZy)MakeServiceTestsz'
    Tests for L{tap.makeService}.
    zcan't run without cryptographyz can't run on non-posix computers)s   iamusers   thisispasswordreturnNc                 
   | j                         | _        t        | j                  d      5 }|j                  dj	                  | j
                               ddd       t        j                         | _        y# 1 sw Y   #xY w)z/
        Create a file with two users.
        zwb+   :N)	mktempfilenameopenwritejoinusernamePasswordr   Optionsoptions)selffs     K/home/dcms/DCMS/lib/python3.12/site-packages/twisted/conch/test/test_tap.pysetUpzMakeServiceTests.setUp(   s^     $--' 	61GGDIId3345	6{{}	6 	6s   +A99Bc                    t        j                         }t        j                  |      }| j                  |t               | j                  |j                  j                  d       | j                  |j                  t               y)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services      r"   
test_basiczMakeServiceTests.test_basic1   s^     //&)g'BC))//4goo~>    c                     d}| j                  t        | j                  d   d       | j                  t        | j                  d   d       | j	                  |t        | j                  d         d|fz         y)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    numCheckerss     r"   test_defaultAuthsz"MakeServiceTests.test_defaultAuths=   sw    
 LL)*7	

 	LL)*8	

 	^,-4~E	
r/   c                     | j                   j                  dd| j                  z   g       | j                  t	        | j                   d         d       y)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:r3      Nr   parseOptionsr   r(   r5   r    s    r"   test_authAddedzMakeServiceTests.test_authAddedT   sB    
 	!!8Wt}}-D"EFT\\.9:A>r/   c                     | j                   j                  dd| j                  z   ddg       | j                  t	        | j                   d         d       y)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).
        r9   r:   zmemory:testuser:testpasswordr3   r1   Nr<   r>   s    r"   test_multipleAuthAddedz'MakeServiceTests.test_multipleAuthAdded\   sP     	!!$--'.		
 	T\\.9:A>r/   c                 6   | j                   j                  dd| j                  z   g       | j                   d   d   }| j                  |t               t        | j                  d   d      }| j                  |j                  |      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.
        r9   r:   r3   r   s   fake)r   r=   r   r'   r   r   r   assertFailurerequestAvatarIdr   UnauthorizedLogin)r    checkerinvalids      r"   test_authFailurez!MakeServiceTests.test_authFailurel   s     	!!8Wt}}-D"EF"&,,~">r"Bg~6"4#8#8#;WE!!##G,e.E.E
 	
r/   c                 $     j                   j                  dd j                  z   g        j                   d   d   }t         j                   |j                        }dt        t        t        d   f   ddf fd	}|j                  |      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.
        r9   r:   r3   rC   username r   Nc                 >    j                  | j                         y )N)r(   rK   )rK   correctr    s    r"   checkSuccessz7MakeServiceTests.test_authSuccess.<locals>.checkSuccess   s    Xw'7'78r/   )
r   r=   r   r   r   rE   r   bytesr   addCallback)r    rG   drO   rN   s   `   @r"   test_authSuccessz!MakeServiceTests.test_authSuccess{   s     	!!8Wt}}-D"EF'+||N'CB'G"D$9$9:##G,	95b	)9#: 	9t 	9 }}\**r/   c                     t        j                         }t        j                  |      }|j                  j                  }| j                  t        |j                  j                               t        t        h       y)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-   rU   s       r"   test_checkerszMakeServiceTests.test_checkers   sW     //&)''$$&'.:K)L	
r/   )r   N)__name__
__module____qualname____doc__r   skipunixr   r#   r.   r7   r?   rA   r   rI   r   rS   rY   rL   r/   r"   r   r      sY     /16%
?
.?? 
# 
+(4. + 
r/   r   N)r]   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   r_   twisted.conchr   $twisted.conch.openssh_compat.factoryr   r   rL   r/   r"   <module>rj      sZ    % $ D  E X X + 0 +^,)* D!C{
x {
r/   