o
    >h                     @   s   d Z ddlZddlmZmZmZ ddlmZmZm	Z	 ddl
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 dededeeef fddZG dd deZdS )z&
Tests for L{twisted.python.systemd}.
    N)DictMappingSequence)assert_thatequal_tonot_)given)dictionariesintegerslists)	ListenFDs)SynchronousTestCase   )systemdDescriptorNamescountpidreturnc                 C   sB   t j }t| |d< ddd t| D |d< t||d< |S )a+  
    @param count: The number of file descriptors to indicate as inherited.

    @param pid: The pid of the inheriting process to indicate.

    @return: A copy of the current process environment with the I{systemd}
        file descriptor inheritance-related environment variables added to it.
    
LISTEN_FDS:c                 S   s   g | ]}| d qS )z.socket ).0nr   r   /var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/python/test/test_systemd.py
<listcomp>    s    z$buildEnvironment.<locals>.<listcomp>LISTEN_FDNAMES
LISTEN_PID)osenvironcopystrjoinrange)r   r   resultr   r   r   buildEnvironment   s
   
	r#   c                   @   s  e Zd ZdZeee ddddee ddfdd	Z	d%d
dZ
d%ddZd%ddZd%ddZd%ddZd%ddZd%ddZeeeddddddee ddfddZeee eddddd eeef ddfd!d"Zeeeddddddee ddfd#d$ZdS )&ListenFDsTestszV
    Apply tests to L{ListenFDs}, constructed based on an environment dictionary.
    r   
   )min_sizemax_sizenamesr   Nc                 C   s^   t |}tttjtj| }ttt t|d	|d}t
|tt|t| dS )a  
        The L{ListenFDs} and L{ListenFDs.fromEnvironment} constructors are
        equivalent for their respective representations of the same
        information.

        @param names: The names of the file descriptors to represent as
            inherited in the test environment given to the parser.  The number
            of descriptors represented will equal the length of this list.
        r   )r   r   r   N)lenlistr!   r   _STARTfromEnvironmentr   r   getpidr    r   r   tuple)selfr(   numFDsdescriptorsfdsr   r   r   test_fromEnvironmentEquivalence*   s   
z.ListenFDsTests.test_fromEnvironmentEquivalencec                 C   s>   |  tdtdt  t }| ttdd|	  dS )z
        If the process environment is not explicitly passed to
        L{ListenFDs.fromEnvironment}, the real process environment dictionary
        is used.
        r            N)
patchr   r#   r-   r   r,   assertEqualr*   r!   inheritedDescriptors)r/   sddaemonr   r   r   test_defaultEnviron@   s   z"ListenFDsTests.test_defaultEnvironc                 C   sT   t dt }tj|d}tj|d}| ttdd|  | g |  dS )a  
        L{ListenFDs.fromEnvironment} removes information about the
        inherited file descriptors from the environment mapping so that the
        same inherited file descriptors cannot be handled repeatedly from
        multiple L{ListenFDs} instances.
        r5   r      N)	r#   r   r-   r   r,   r8   r*   r!   r9   )r/   envfirstsecondr   r   r   test_secondEnvironmentJ   s
   z%ListenFDsTests.test_secondEnvironmentc                 C   s2   t dt d }tj|d}| g |  dS )aV  
        If the current process PID does not match the PID in the
        environment then the systemd variables in the environment were set for
        a different process (perhaps our parent) and the inherited descriptors
        are not intended for this process so L{ListenFDs.inheritedDescriptors}
        returns an empty list.
        r5   r   r<   Nr#   r   r-   r   r,   r8   r9   r/   r>   r:   r   r   r   test_mismatchedPIDW   s   z!ListenFDsTests.test_mismatchedPIDc                 C   s4   t dt }|d= tj|d}| g |  dS )z
        If the I{LISTEN_PID} environment variable is not present then
        there is no clear indication that any file descriptors were inherited
        by this process so L{ListenFDs.inheritedDescriptors} returns an empty
        list.
        r5   r   r<   NrB   rC   r   r   r   test_missingPIDVariablec   s   z&ListenFDsTests.test_missingPIDVariablec                 C   s*   t dd}tj|d}| g |  dS )z
        If the I{LISTEN_PID} environment variable is set to a string that cannot
        be parsed as an integer, no inherited descriptors are reported.
        r5   hello, worldr<   N)r#   r   r,   r8   r9   rC   r   r   r   test_nonIntegerPIDVariableo   s   
z)ListenFDsTests.test_nonIntegerPIDVariablec                 C   s:   t dt }|d= |d= tj|d}| g |  dS )z
        If the I{LISTEN_FDS} and I{LISTEN_FDNAMES} environment variables
        are not present, no inherited descriptors are reported.
        r5   r   r   r<   NrB   rC   r   r   r   test_missingFDSVariablex   s
   z&ListenFDsTests.test_missingFDSVariablec                 C   s6   t dt }d|d< tj|d}| g |  dS )z
        If the I{LISTEN_FDS} environment variable is set to a string that cannot
        be parsed as an integer, no inherited descriptors are reported.
        r5   rF   r   r<   NrB   rC   r   r   r   test_nonIntegerFDSVariable   s   z)ListenFDsTests.test_nonIntegerFDSVariable)	min_value	max_valueT)uniquer1   c                 C   sN   t tt|}t||}| }t|t| |d t|tt| dS )zm
        L{ListenFDs.inheritedDescriptors} returns a copy of the inherited
        descriptors list.
        r   N)	r.   mapr   r   r9   r   r   appendr   )r/   r1   r(   r2   fdsCopyr   r   r   test_inheritedDescriptors   s   

z(ListenFDsTests.test_inheritedDescriptors)rJ   )r'   expectedc                 C   sH   t | }dd |D }dd |D }t||}t| t| dS )z
        L{ListenFDs.inheritedNamedDescriptors} returns a mapping from the
        descriptor names to their integer values, with items formed by
        pairwise combination of the input descriptors and names.
        c                 S   s   g | ]\}}|qS r   r   )r   name_r   r   r   r          zAListenFDsTests.test_inheritedNamedDescriptors.<locals>.<listcomp>c                 S   s   g | ]\}}|qS r   r   )r   rS   fdr   r   r   r      rT   N)r*   itemsr   r   inheritedNamedDescriptorsr   )r/   rQ   rV   r(   r1   r2   r   r   r   test_inheritedNamedDescriptors   s
   
z-ListenFDsTests.test_inheritedNamedDescriptorsc                 C   sD   t tt|}t||}| | |  | | |  dS )z
        Any subsequent calls to C{inheritedDescriptors} and
        C{inheritedNamedDescriptors} return the same list.
        N)r.   rM   r   r   r8   r9   rW   )r/   r1   r(   r:   r   r   r   test_repeated   s   
zListenFDsTests.test_repeated)r   N)__name__
__module____qualname____doc__r   r   r   r   r   r3   r;   rA   rD   rE   rG   rH   rI   r
   intrP   r	   r   rX   rY   r   r   r   r   r$   %   s"    






	

r$   )r]   r   typingr   r   r   hamcrestr   r   r   
hypothesisr   hypothesis.strategiesr	   r
   r   twisted.python.systemdr   twisted.trial.unittestr   
strategiesr   r^   objectr   r#   r$   r   r   r   r   <module>   s   