
    Yf+                         U d Z ddlZddlZddlZddlmZ ee         ed<   	 ddlZddl	m
Z
 dZn# e$ r dZY nw xY wddlmZ  G d d	          Z G d
 d          Z G d de          Z G d de          ZdS )z;
Tests for POSIX-based L{IReactorProcess} implementations.
    N)OptionalplatformSkip)processznon-POSIX platform)TestCasec                   *    e Zd ZdZd Zd Zd Zd ZdS )FakeFilez>
    A dummy file object which records when it is closed.
    c                 "    || _         || _        d S N)testcasefd)selfr   r   s      Y/var/www/html/env/lib/python3.11/site-packages/twisted/internet/test/test_posixprocess.py__init__zFakeFile.__init__    s         c                 N    | j         j                            | j                   d S r
   )r   _filesremover   r   s    r   closezFakeFile.close$   s#    ##DG,,,,,r   c                     | S r
    r   s    r   	__enter__zFakeFile.__enter__'   s    r   c                 .    |                                   d S r
   )r   )r   exc_type	exc_value	tracebacks       r   __exit__zFakeFile.__exit__*   s    

r   N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r      sZ           - - -      r   r   c                   "    e Zd ZdZdZd Zd ZdS )FakeResourceModulez
    Fake version of L{resource} which hard-codes a particular rlimit for maximum
    open files.

    @ivar _limit: The value to return for the hard limit of number of open files.
       c                     || _         d S r
   )_limitr   limits     r   r   zFakeResourceModule.__init__8   s    r   c                 2    || j         k    r	d| j        gS ddgS )zX
        A fake of L{resource.getrlimit} which returns a pre-determined result.
        r   {   i  )RLIMIT_NOFILEr&   )r   nos     r   	getrlimitzFakeResourceModule.getrlimit;   s)     ###t{##Szr   N)r   r   r    r!   r+   r   r-   r   r   r   r#   r#   .   sC          M      r   r#   c                   v    e Zd ZdZeZdZdZdZd Z	d Z
d Zd Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd ZdS )FDDetectorTestsa  
    Tests for _FDDetector class in twisted.internet.process, which detects
    which function to drop in place for the _listOpenFDs method.

    @ivar devfs: A flag indicating whether the filesystem fake will indicate
        that /dev/fd exists.

    @ivar accurateDevFDResults: A flag indicating whether the /dev/fd fake
        returns accurate open file information.

    @ivar procfs: A flag indicating whether the filesystem fake will indicate
        that /proc/<pid>/fd exists.
    Fc                     dS )z>
        Fake os.getpid, always return the same thing
        r*   r   r   s    r   getpidzFDDetectorTests.getpidZ   s	     sr   c                     t          t          | j                  }| j        r|d|                                 fz  k    r|S | j        r|dk    r| j        r|S g dS t                      )z
        Fake os.listdir, depending on what mode we're in to simulate behaviour.

        @param arg: the directory to list
        z/proc/%d/fdz/dev/fd)012)mapstrr   procfsr1   devfsaccurateDevFDResultsOSError)r   argaccurates      r   listdirzFDDetectorTests.listdir`   su     sDK((; 	3=DKKMM3C#CDDO: 	##**(  "??"iir   c           
          t          | t          t          t          d                    t          | j                  z
                      }| j                            |j                   |S )aO  
        This is a mock for L{open}.  It keeps track of opened files so extra
        descriptors can be returned from the mock for L{os.listdir} when used on
        one of the list-of-filedescriptors directories.

        A L{FakeFile} is returned which can be closed to remove the new
        descriptor from the open list.
           )r   minsetranger   appendr   )r   fnamemodefs       r   openfilezFDDetectorTests.openfileo   sU     T3s5;;//#dk2B2BBCCDD14   r   c                 $    dt           j        d<   dS )zp
        Make the L{resource} module unimportable for the remainder of the
        current test method.
        Nresource)sysmodulesr   s    r   hideResourceModulez"FDDetectorTests.hideResourceModule}   s    
 #'Jr   c                 >    t          |          t          j        d<   dS )a  
        Make a L{FakeResourceModule} instance importable at the L{resource}
        name.

        @param limit: The value which will be returned for the hard limit of
            number of open files by the fake resource module's C{getrlimit}
            function.
        rJ   N)r#   rK   rL   r'   s     r   revealResourceModulez$FDDetectorTests.revealResourceModule   s     #5U";";Jr   c                 j    |!	 t           j        d= dS # t          $ r Y dS w xY w|t           j        d<   dS )zI
        Restore the original resource module to L{sys.modules}.
        NrJ   )rK   rL   KeyError)r   values     r   replaceResourceModulez%FDDetectorTests.replaceResourceModule   sT     =K
+++    ',CK
###s    
!!c                    t          j                    | _        | j        | j        _        | j        | j        _        | j        | j        _        g d| _        |                     | j        t          j
                            d                     dS )z
        Set up the tests, giving ourselves a detector object to play with and
        setting up its testable knobs to refer to our mocked versions.
        r   r$      rJ   N)r   _FDDetectordetectorr>   r1   rH   r   
addCleanuprS   rK   rL   getr   s    r   setUpzFDDetectorTests.setUp   sn    
  +-- $#{!%ii2CKOOJ4O4OPPPPPr   c                      d }d } fd}|||g j         _                             | j                                                    dS )z
        L{FDDetector._getImplementation} returns the first method from its
        C{_implementations} list which returns results which reflect a newly
        opened file descriptor.
        c                       t          d          )NzThis does not work)
ValueErrorr   r   r   failWithExceptionzBFDDetectorTests.test_selectFirstWorking.<locals>.failWithException   s    1222r   c                  
    g dS NrU   r   r   r   r   failWithWrongResultszEFDDetectorTests.test_selectFirstWorking.<locals>.failWithWrongResults       99r   c                  "     j         d d          S r
   )r   r   s   r   correctz8FDDetectorTests.test_selectFirstWorking.<locals>.correct   s    ;qqq>!r   NrX   _implementationsassertIs_getImplementation)r   r_   rb   re   s   `   r   test_selectFirstWorkingz'FDDetectorTests.test_selectFirstWorking   s{    	3 	3 	3	 	 		" 	" 	" 	" 	"  *
& 	gt}??AABBBBBr   c                     d }d }||g| j         _        |                     || j                                                    dS )z
        L{FDDetector._getImplementation} returns the last method from its
        C{_implementations} list if none of the implementations manage to return
        results which reflect a newly opened file descriptor.
        c                  
    g dS )N)      	   r   r   r   r   rb   z=FDDetectorTests.test_selectLast.<locals>.failWithWrongResults   rc   r   c                  
    g dS ra   r   r   r   r   failWithOtherWrongResultszBFDDetectorTests.test_selectLast.<locals>.failWithOtherWrongResults   rc   r   Nrf   )r   rb   rq   s      r   test_selectLastzFDDetectorTests.test_selectLast   s_    	 	 		 	 	 !%*
&
 	/1Q1Q1S1STTTTTr   c                    t          j                    }|j        j        }|                                 |j        j        }|                                 |j        j        }|                     ||           |                     ||           dS )z
        Check that the identity of _listOpenFDs changes after running
        _listOpenFDs the first time, but not after the second time it's run.

        In other words, check that the monkey patching actually works.
        N)r   rW   _listOpenFDsr   assertNotEqualassertEqual)r   rX   firstsecondthirds        r   !test_identityOfListOpenFDsChangesz1FDDetectorTests.test_identityOfListOpenFDsChanges   s     &((%.&/%.E6***'''''r   c                     d| _         |                     t          | j        j                   d| _         d| _        |                     g d| j                                                   dS )z
        L{_FDDetector._devFDImplementation} raises L{OSError} if there is no
        I{/dev/fd} directory, otherwise it returns the basenames of its children
        interpreted as integers.
        FTrU   N)r9   assertRaisesr;   rX   _devFDImplementationr:   rv   r   s    r   test_devFDImplementationz(FDDetectorTests.test_devFDImplementation   sc     
'4=#EFFF
$)!DM$F$F$H$HIIIIIr   c                     d| _         |                     t          | j        j                   d| _         |                     g d| j                                                   dS )z
        L{_FDDetector._procFDImplementation} raises L{OSError} if there is no
        I{/proc/<pid>/fd} directory, otherwise it returns the basenames of its
        children interpreted as integers.
        FTrU   N)r8   r|   r;   rX   _procFDImplementationrv   r   s    r   test_procFDImplementationz)FDDetectorTests.test_procFDImplementation   s[     '4=#FGGGDM$G$G$I$IJJJJJr   c                    |                      d           |                     t          t          d                    t          | j                                                             |                      d           |                     t          t          d                    t          | j                                                             dS )z
        L{_FDDetector._fallbackFDImplementation} uses the L{resource} module if
        it is available, returning a range of integers from 0 to the
        minimum of C{1024} and the hard I{NOFILE} limit.
        i   i   r@   N)rO   rv   listrC   rX   _fallbackFDImplementationr   s    r   test_resourceFDImplementationz-FDDetectorTests.test_resourceFDImplementation   s     	!!#&&&sd4=#J#J#L#LMM	
 	
 	

 	!!$'''ttDM$K$K$M$MNN	
 	
 	
 	
 	
r   c                     |                                   |                     t          t          d                    t          | j                                                             dS )z
        L{_FDDetector._fallbackFDImplementation}, the implementation of last
        resort, succeeds with a fixed range of integers from 0 to 1024 when the
        L{resource} module is not importable.
        r@   N)rM   rv   r   rC   rX   r   r   s    r   test_fallbackFDImplementationz-FDDetectorTests.test_fallbackFDImplementation  sa     	!!!ttDM$K$K$M$MNN	
 	
 	
 	
 	
r   N)r   r   r    r!   r   skipr9   r:   r8   r1   r>   rH   rM   rO   rS   r[   rj   rr   rz   r~   r   r   r   r   r   r   r/   r/   D   s         DE F      ' ' '	< 	< 	<
, 
, 
,
Q 
Q 
QC C C0U U U(( ( (&
J 
J 
J	K 	K 	K
 
 
$	
 	
 	
 	
 	
r   r/   c                   "    e Zd ZdZeZd Zd ZdS )FileDescriptorTestsz<
    Tests for L{twisted.internet.process._listOpenFDs}
    c           	          t          j                    D ]i}	 t          j        |t          j                   ## t          $ r:}|                     t          j        |j        d||j        fz             Y d}~bd}~ww xY wdS )z
        File descriptors returned by L{_listOpenFDs} are mostly open.

        This test assumes that zero-legth writes fail with EBADF on closed
        file descriptors.
        z2fcntl(%d, F_GETFL) failed with unexpected errno %dN)r   rt   fcntlF_GETFLr;   rv   errnoEBADF)r   r   errs      r   test_openFDsz FileDescriptorTests.test_openFDs"  s     &(( 		 		BB....     KIH39o&       		 		s   7
A;0A66A;c                 t   t          t          j                  }t          j                    }|                     |                                |           t          j        |                                          }|                     ||                                k    d           	 |	                                 |                     |t          j                               t          j	        |           n# t          j	        |           w xY w| 
                    |t          j                               dS )zB
        L{_listOpenFDs} lists expected file descriptors.
        z>Expected duplicate file descriptor to be greater than originalN)openosdevnullr   rt   assertInfilenodup
assertTruer   assertNotIn)r   rG   openfdsr   s       r   test_expectedFDsz$FileDescriptorTests.test_expectedFDs4  s     &((ahhjj'*** VAHHJJ 	OL	
 	
 	

	 GGIIIMM"g244555 HRLLLLBHRLLLLW13344444s   (;C8 8DN)r   r   r    r!   r   r   r   r   r   r   r   r   r     sC          D  $)5 )5 )5 )5 )5r   r   )r!   r   r   rK   typingr   r7   __annotations__r   twisted.internetr   r   ImportErrortwisted.trial.unittestr   r   r#   r/   r   r   r   r   <module>r      sw    
  				 



      sm   LLL )(((((LL  ( ( ('LLL( , + + + + +       &       ,T
 T
 T
 T
 T
h T
 T
 T
nB5 B5 B5 B5 B5( B5 B5 B5 B5 B5s   / 99