
    \d>                        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j        dd          dd         dk    rdZdZndZdZ G d dej                  ZdS )z0
Test cases for twisted.protocols.ident module.
    N)StringIO)defererror)ident)failure)StringTransport)unittestc                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	ClassParserTestsz*
    Test parsing of ident responses.
    c                 6    t          j                    | _        dS )z7
        Create an ident client used in tests.
        N)r   IdentClientclientselfs    7lib/python3.11/site-packages/twisted/test/test_ident.pysetUpzClassParserTests.setUp   s     '))    c                     t          j                    }| j        j                            |ddf           | j                            d           |                     |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   s`     N""AsC=111  !CDDD!!!U%5666r   c                     t          j                    }| j        j                            |ddf           | j                            d           |                     |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(   s_     N""AsC=111  !=>>>!!!U\222r   c                     t          j                    }| j        j                            |ddf           | j                            d           |                     |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   s`     N""AsC=111  !CDDD!!!U%6777r   c                     t          j                    }| j        j                            |ddf           | j                            d           |                     |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:   s`     N""AsC=111  !ABBB!!!U%5666r   c                 &   t          j                    }| j        j                            |ddf           | j                            t          j        t          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   sr    
 N""AsC=111""7?53G3I3I#J#JKKK!!!U%5666r   N)
__name__
__module____qualname____doc__r   r   r#   r'   r*   r/    r   r   r   r      sx         * * *7 7 73 3 38 8 87 7 77 7 7 7 7r   r   c                       e Zd Zd ZdS )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dS )TestErrorIdentServerc                 *    |                                  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dS )NewExceptionN)r0   r1   r2   r4   r   r   rD   rD   X   s        Dr   rD   c                       e Zd Zd Zd ZdS )ServerParserTestsc                 <   t                      }|                    t                                 g }|j        |_        t
          j        |_        |                    d           | 	                    |d         d           t
          j
        |_        |                    d           | 	                    |d         d           t
          j        |_        |                    d           | 	                    |d         d	           t
          j        |_        |                    d
           | 	                    |d         d           t          |_        |                    d           | 	                    |d         d           |                     t                    }| 	                    t          |          d           dD ]Y}|d d = |                    d|fz             |                    d|fz             | 	                    |d|fz  d|fz  g           Zd S )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    ""	**+++X
*	z"""1ABBB,	z"""1;<<<+	z"""1@AAA*	z"""1?@@@&	z"""1ABBB%%l33TA&&&' 
	 
	D!!!NN7dW,---NN7dW,---2dW<2dW<   	
	 
	r   c                     t                      }|                    t                                 g }|j        |_        d|_        |                    d           |                     |d         d           d S )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   so    	**+++X
'	z"""1?@@@@@r   N)r0   r1   r2   rV   rZ   r4   r   r   rF   rF   \   s8        & & &PA A A A Ar   rF   z=LrH      0100007F040302017F00000101020304c                   N    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dS )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                     t          j                    }|                     |                    t                    d           d S )N	127.0.0.1)r   ProcServerMixinrO   dottedQuadFromHexString_addr1r   rR   s     r   testDottedQuadFromHexStringz*ProcMixinTests.testDottedQuadFromHexString   s:    !##226::KHHHHHr   c                     t          j                    }|                     |                    t          dz             d           d S )Nz:0277)rc   iw  )r   rd   rO   unpackAddressrf   rg   s     r   testUnpackAddressz ProcMixinTests.testUnpackAddress   s>    !##')9::<NOOOOOr   c                     t          j                    }|                     |                    | j                  d           d S )N)rc      1.2.3.4  r   )r   rd   rO   	parseLinelinerg   s     r   testLineParserzProcMixinTests.testLineParser   sF    !##KK	""$L	
 	
 	
 	
 	
r   c                      g t          j                    } fd|_        fd|_                             |                    dd          |j        df                                dg           d S )Nc                  .    t           j        g          S r8   iterrs   r   s   r   <lambda>z4ProcMixinTests.testExistingAddress.<locals>.<lambda>       D$)-- r   c                 >                         |           dfd         S )NrootrH   )r   )uidusernames    r   ry   z4ProcMixinTests.testExistingAddress.<locals>.<lambda>   s    X__S%9%96$B1$E r   rm   ro   r|   r   )r   rd   entriesgetUsernamerO   r=   SYSTEM_NAME)r   rR   r~   s   ` @r   testExistingAddressz"ProcMixinTests.testExistingAddress   s    !##----	EEEEHH&(899AM6;R	
 	
 	
 	A3'''''r   c                 ,    t          j                    } fd|_                             t           j        |j        dd                                t           j        |j        dd                                t           j        |j        dd           d S )Nc                  .    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   s    !##----	%,2CEUVVV%,2CEUVVV%,2CEUVVVVVr   c                      g  fd}                      t          d|           t          j                    }                     t          j        |j        dd                                di fg           dS )zS
        L{ident.ProcServerMixin.lookup} uses the Linux TCP process table.
        c                  Z                         | |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---DO,,,r   openr   ro   )z/proc/net/tcpN)patchbuiltinsr   rd   r   r"   r=   rO   )r   r   rR   r   s   `  @r   testLookupProcNetTcpz#ProcMixinTests.testLookupProcNetTcp   s     
	- 	- 	- 	- 	- 	- 	

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