
    Vh<                        d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ  G d	 d
ej                         Z G d dej$                        Z G d dej$                        Z G d de      Z G d dej                         Z ej0                  dd      dd dk(  rdZdZndZdZ G d dej                         Zy)z0
Test cases for twisted.protocols.ident module.
    N)StringIO)defererror)StringTransport)ident)failure)unittestc                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	ClassParserTestsz*
    Test parsing of ident responses.
    c                 6    t        j                         | _        y)z7
        Create an ident client used in tests.
        N)r   IdentClientclientselfs    G/home/dcms/DCMS/lib/python3.12/site-packages/twisted/test/test_ident.pysetUpzClassParserTests.setUp   s     '')    c                     t        j                         }| j                  j                  j	                  |ddf       | j                  j                  d       | j                  |t        j                        S )zX
        'UNKNOWN-ERROR' error should map to the L{ident.IdentError} exception.
        {     z 123, 456 : ERROR : UNKNOWN-ERROR)	r   Deferredr   queriesappendlineReceivedassertFailurer   
IdentErrorr   ds     r   test_indentErrorz!ClassParserTests.test_indentError   sZ     NN""AsC=1  !CD!!!U%5%566r   c                     t        j                         }| j                  j                  j	                  |ddf       | j                  j                  d       | j                  |t        j                        S )zN
        'NO-USER' error should map to the L{ident.NoUser} exception.
           r   z234, 456 : ERROR : NO-USER)	r   r   r   r   r   r   r   r   NoUserr   s     r   test_noUSerErrorz!ClassParserTests.test_noUSerError(   sX     NN""AsC=1  !=>!!!U\\22r   c                     t        j                         }| j                  j                  j	                  |ddf       | j                  j                  d       | j                  |t        j                        S )zX
        'INVALID-PORT' error should map to the L{ident.InvalidPort} exception.
        iY  7  z 345, 567 :  ERROR : INVALID-PORT)	r   r   r   r   r   r   r   r   InvalidPortr   s     r   test_invalidPortErrorz&ClassParserTests.test_invalidPortError1   sZ     NN""AsC=1  !CD!!!U%6%677r   c                     t        j                         }| j                  j                  j	                  |ddf       | j                  j                  d       | j                  |t        j                        S )zV
        'HIDDEN-USER' error should map to the L{ident.HiddenUser} exception.
        r%   i  z567, 789 : ERROR : HIDDEN-USER)	r   r   r   r   r   r   r   r   
HiddenUserr   s     r   test_hiddenUserErrorz%ClassParserTests.test_hiddenUserError:   sZ     NN""AsC=1  !AB!!!U%5%566r   c                 :   t        j                         }| j                  j                  j	                  |ddf       | j                  j                  t        j                  t        j                                      | j                  |t        j                        S )zy
        A pending query which failed because of a ConnectionLost should
        receive an L{ident.IdentError}.
        i  i  )r   r   r   r   r   connectionLostr   Failurer   ConnectionLostr   r   r   r   s     r   test_lostConnectionz$ClassParserTests.test_lostConnectionC   sk    
 NN""AsC=1""7??53G3G3I#JK!!!U%5%566r   N)
__name__
__module____qualname____doc__r   r   r#   r'   r*   r/    r   r   r   r      s%    *73877r   r   c                       e Zd Zd Zy)TestIdentServerc                     | j                   S N)resultValuer   serverAddressclientAddresss      r   lookupzTestIdentServer.lookupO   s    r   Nr0   r1   r2   r=   r4   r   r   r6   r6   N   s     r   r6   c                       e Zd Zd Zy)TestErrorIdentServerc                 "    | j                         r8   )exceptionTyper:   s      r   r=   zTestErrorIdentServer.lookupT   s      ""r   Nr>   r4   r   r   r@   r@   S   s    #r   r@   c                       e Zd Zy)NewExceptionN)r0   r1   r2   r4   r   r   rD   rD   X   s    r   rD   c                       e Zd Zd Zd Zy)ServerParserTestsc                    t               }|j                  t                      g }|j                  |_        t
        j                  |_        |j                  d       | j                  |d   d       t
        j                  |_        |j                  d       | j                  |d   d       t
        j                  |_        |j                  d       | j                  |d   d	       t
        j                  |_        |j                  d
       | j                  |d   d       t        |_        |j                  d       | j                  |d   d       | j                  t              }| j                  t        |      d       dD ]M  }|d d = |j                  d|fz         |j                  d|fz         | j                  |d|fz  d|fz  g       O y )Nz123, 345r   z 123, 345 : ERROR : UNKNOWN-ERRORz432, 210   z432, 210 : ERROR : NO-USERz987, 654   z987, 654 : ERROR : INVALID-PORTz756, 827   z756, 827 : ERROR : HIDDEN-USERz987, 789   z 987, 789 : ERROR : UNKNOWN-ERROR)r   i   i  z%d, 5z5, %dz%d, 5 : ERROR : INVALID-PORTz5, %d : ERROR : INVALID-PORT)r@   makeConnectionr   r   sendLiner   r   rB   r   assertEqualr"   r&   r)   rD   flushLoggedErrorslen)r   pLerrsports        r   
testErrorszServerParserTests.testErrors]   s    "	*+XX
**	z"1AB,,	z"1;<++	z"1@A**	z"1?@&	z"1AB%%l3TA&' 
	D!NN7dW,-NN7dW,-2dW<2dW<	
	r   c                     t               }|j                  t                      g }|j                  |_        d|_        |j                  d       | j                  |d   d       y )N)SYSUSERz123, 456r   z123, 456 : USERID : SYS : USER)r6   rM   r   r   rN   r9   r   rO   )r   rR   rS   s      r   testSuccesszServerParserTests.testSuccess   sS    	*+XX
'	z"1?@r   N)r0   r1   r2   rV   rZ   r4   r   r   rF   rF   \   s    &PAr   rF   z=LrH      0100007F040302017F00000101020304c                   L    e Zd ZdededZdez   Zd Zd Zd Z	d Z
d	 Zd
 Zy)ProcMixinTestsz4: z:0019 z]:02FA 0A 00000000:00000000 00:00000000 00000000     0        0 10927 1 f72a5b80 3000 0 0 2 -1zd  sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
   c                 v    t        j                         }| j                  |j                  t              d       y )N	127.0.0.1)r   ProcServerMixinrO   dottedQuadFromHexString_addr1r   rR   s     r   testDottedQuadFromHexStringz*ProcMixinTests.testDottedQuadFromHexString   s+    !!#226:KHr   c                 |    t        j                         }| j                  |j                  t        dz         d       y )Nz:0277)rc   iw  )r   rd   rO   unpackAddressrf   rg   s     r   testUnpackAddressz ProcMixinTests.testUnpackAddress   s/    !!#')9:<NOr   c                     t        j                         }| j                  |j                  | j                        d       y )N)rc      1.2.3.4  r   )r   rd   rO   	parseLinelinerg   s     r   testLineParserzProcMixinTests.testLineParser   s1    !!#KK		"$L	
r   c                      g t        j                         } fd|_        fd|_         j	                  |j                  dd      |j                  df        j	                  dg       y )Nc                  0    t         j                  g      S r8   iterrs   r   s   r   <lambda>z4ProcMixinTests.testExistingAddress.<locals>.<lambda>       D$))- r   c                 0    j                  |       dfd   S )NrootrH   )r   )uidusernames    r   ry   z4ProcMixinTests.testExistingAddress.<locals>.<lambda>   s    X__S%96$B1$E r   rm   ro   r|   r   )r   rd   entriesgetUsernamerO   r=   SYSTEM_NAME)r   rR   r~   s   ` @r   testExistingAddressz"ProcMixinTests.testExistingAddress   sa    !!#-	EHH&(89AMM6;R	
 	A3'r   c                 J    t        j                         } fd|_         j                  t         j                  |j
                  dd        j                  t         j                  |j
                  dd        j                  t         j                  |j
                  dd       y )Nc                  0    t         j                  g      S r8   rw   r   s   r   ry   z7ProcMixinTests.testNonExistingAddress.<locals>.<lambda>   rz   r   rc      ro   rm   )z1.2.3.5rq   )rp   i  )r   rd   r   assertRaisesr"   r=   rg   s   ` r   testNonExistingAddressz%ProcMixinTests.testNonExistingAddress   sr    !!#-	%,,2CEUV%,,2CEUV%,,2CEUVr   c                      g  fd} j                  t        d|       t        j                         } j	                  t        j
                  |j                  dd        j                  di fg       y)zS
        L{ident.ProcServerMixin.lookup} uses the Linux TCP process table.
        c                  T    j                  | |f       t        j                        S )z[
            Mock for the open call to prevent actually opening /proc/net/tcp.
            )r   r   
sampleFile)argskwargs
open_callsr   s     r   mocked_openz8ProcMixinTests.testLookupProcNetTcp.<locals>.mocked_open   s&     tVn-DOO,,r   openr   ro   )z/proc/net/tcpN)patchbuiltinsr   rd   r   r"   r=   rO   )r   r   rR   r   s   `  @r   testLookupProcNetTcpz#ProcMixinTests.testLookupProcNetTcp   sf     
	- 	

8V[1!!#%,,2CEUV-r23Z@r   N)r0   r1   r2   rf   _addr2rs   r   rh   rk   rt   r   r   r   r4   r   r   ra   ra      sD    
 
	D	68<	= 
IP
(WAr   ra   )r3   r   structior   twisted.internetr   r   twisted.internet.testingr   twisted.protocolsr   twisted.pythonr   twisted.trialr	   TestCaser   IdentServerr6   r@   RuntimeErrorrD   rF   packrf   r   ra   r4   r   r   <module>r      s   
    ) 4 # " "77x(( 77t e''  
#5,, #
	< 	1A)) 1Ah 6;;tQ!'FFFF;AX&& ;Ar   