o
    ¯b>  ã                   @   sø   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
„ d
ejƒZG dd„ de	jƒZG dd„ de	jƒZG dd„ deƒZG dd„ dejƒZe dd¡dd… dkrmdZdZndZdZG dd„ dejƒZdS )z0
Test cases for twisted.protocols.ident module.
é    N)ÚStringIO)ÚdeferÚerror)Úident)Úfailure)ÚStringTransport)Úunittestc                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚClassParserTestsz*
    Test parsing of ident responses.
    c                 C   s   t  ¡ | _dS )z7
        Create an ident client used in tests.
        N)r   ÚIdentClientÚclient©Úself© r   ú9/usr/lib/python3/dist-packages/twisted/test/test_ident.pyÚsetUp   s   zClassParserTests.setUpc                 C   ó6   t  ¡ }| 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   Údr   r   r   Útest_indentError   ó   z!ClassParserTests.test_indentErrorc                 C   r   )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   r   r   r   Útest_noUSerError(   r   z!ClassParserTests.test_noUSerErrorc                 C   r   )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   r   r   r   Útest_invalidPortError1   r   z&ClassParserTests.test_invalidPortErrorc                 C   r   )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   r   r   r   Útest_hiddenUserError:   r   z%ClassParserTests.test_hiddenUserErrorc                 C   s@   t  ¡ }| jj |ddf¡ | j t t 	¡ ¡¡ |  
|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   r   r   r   Útest_lostConnectionC   s   z$ClassParserTests.test_lostConnectionN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r    r#   r%   r)   r   r   r   r   r	      s    				r	   c                   @   ó   e Zd Zdd„ ZdS )ÚTestIdentServerc                 C   s   | j S ©N)ÚresultValue©r   ÚserverAddressÚclientAddressr   r   r   ÚlookupO   s   zTestIdentServer.lookupN©r*   r+   r,   r5   r   r   r   r   r/   N   ó    r/   c                   @   r.   )ÚTestErrorIdentServerc                 C   s   |   ¡ ‚r0   )ÚexceptionTyper2   r   r   r   r5   T   s   zTestErrorIdentServer.lookupNr6   r   r   r   r   r8   S   r7   r8   c                   @   s   e Zd ZdS )ÚNewExceptionN)r*   r+   r,   r   r   r   r   r:   X   s    r:   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚServerParserTestsc                 C   s4  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 ]%}|d d …= | d|f ¡ | d|f ¡ |  	|d|f d|f g¡ qrd 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)r8   ÚmakeConnectionr   r   ÚsendLiner   r   r9   r   ÚassertEqualr   r"   r$   r:   ÚflushLoggedErrorsÚlen)r   ÚpÚLÚerrsÚportr   r   r   Ú
testErrors]   s@   






þþüzServerParserTests.testErrorsc                 C   sB   t ƒ }| tƒ ¡ g }|j|_d|_| d¡ |  |d d¡ d S )N)ÚSYSÚUSERz123, 456r   z123, 456 : USERID : SYS : USER)r/   rA   r   r   rB   r1   r   rC   )r   rF   rG   r   r   r   ÚtestSuccess…   s   
zServerParserTests.testSuccessN)r*   r+   r,   rJ   rM   r   r   r   r   r;   \   s    (r;   z=Lr<   ó   Ú0100007FÚ04030201Ú7F000001Ú01020304c                   @   sP   e Zd Zdeef Zde Zdd„ Zdd„ Zdd„ Z	d	d
„ Z
dd„ Zdd„ ZdS )ÚProcMixinTestszj4: %s:0019 %s: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                 C   s   t  ¡ }|  | t¡d¡ d S )Nú	127.0.0.1)r   ÚProcServerMixinrC   ÚdottedQuadFromHexStringÚ_addr1©r   rF   r   r   r   ÚtestDottedQuadFromHexString£   s   z*ProcMixinTests.testDottedQuadFromHexStringc                 C   s"   t  ¡ }|  | td ¡d¡ d S )Nz:0277)rT   iw  )r   rU   rC   ÚunpackAddressrW   rX   r   r   r   ÚtestUnpackAddress§   s   z ProcMixinTests.testUnpackAddressc                 C   s    t  ¡ }|  | | j¡d¡ d S )N)©rT   é   ©ú1.2.3.4éú  r   )r   rU   rC   Ú	parseLineÚlinerX   r   r   r   ÚtestLineParser«   s   ÿzProcMixinTests.testLineParserc                    sT   g ‰t  ¡ }‡ fdd„|_‡fdd„|_ˆ  | dd¡|jdf¡ ˆ  ˆdg¡ d S )Nc                      ó   t ˆ jgƒS r0   ©Úiterrb   r   r   r   r   Ú<lambda>´   ó    z4ProcMixinTests.testExistingAddress.<locals>.<lambda>c                    s   ˆ   | ¡dfd S )NÚrootr<   )r   )Úuid)Úusernamer   r   rg   µ   s    r\   r^   ri   r   )r   rU   ÚentriesÚgetUsernamerC   r5   ÚSYSTEM_NAMErX   r   )r   rk   r   ÚtestExistingAddress±   s   ÿz"ProcMixinTests.testExistingAddressc                    sV   t  ¡ }‡ fdd„|_ˆ  t j|jdd¡ ˆ  t j|jdd¡ ˆ  t j|jdd¡ d S )Nc                      rd   r0   re   r   r   r   r   rg   ½   rh   z7ProcMixinTests.testNonExistingAddress.<locals>.<lambda>©rT   é   r^   r\   )z1.2.3.5r`   )r_   iû  )r   rU   rl   ÚassertRaisesr   r5   rX   r   r   r   ÚtestNonExistingAddress»   s
   z%ProcMixinTests.testNonExistingAddressc                    sR   g ‰ ‡ ‡fdd„}ˆ  td|¡ t ¡ }ˆ tj|jdd¡ ˆ di fgˆ ¡ dS )zS
        L{ident.ProcServerMixin.lookup} uses the Linux TCP process table.
        c                     s   ˆ   | |f¡ tˆjƒS )z[
            Mock for the open call to prevent actually opening /proc/net/tcp.
            )r   r   Ú
sampleFile)ÚargsÚkwargs©Ú
open_callsr   r   r   Úmocked_openÈ   s   
z8ProcMixinTests.testLookupProcNetTcp.<locals>.mocked_openÚopenrp   r^   )z/proc/net/tcpN)ÚpatchÚbuiltinsr   rU   rr   r   r5   rC   )r   ry   rF   r   rw   r   ÚtestLookupProcNetTcpÂ   s   z#ProcMixinTests.testLookupProcNetTcpN)r*   r+   r,   rW   Ú_addr2rb   rt   rY   r[   rc   ro   rs   r}   r   r   r   r   rS   ˜   s    üÿÿ
rS   )r-   r|   ÚstructÚior   Útwisted.internetr   r   Útwisted.protocolsr   Útwisted.pythonr   Útwisted.test.proto_helpersr   Útwisted.trialr   ÚTestCaser	   ÚIdentServerr/   r8   ÚRuntimeErrorr:   r;   ÚpackrW   r~   rS   r   r   r   r   Ú<module>   s(   :4