
    tf                         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Z G 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                     t           j                                        }t          |           |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                     g | ]}| d S )z.socket ).0ns     _/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/python/test/test_systemd.py
<listcomp>z$buildEnvironment.<locals>.<listcomp>    s    (M(M(M1A(M(M(M    LISTEN_FDNAMES
LISTEN_PID)osenvironcopystrjoinrange)r   r   results      r   buildEnvironmentr'      sf     Z__Fu::F<"xx(M(Me(M(M(MNNFs88F<Mr   c                      e Zd ZdZ e e e            dd                    dee         ddfd            Z	dd	Z
dd
ZddZddZddZddZddZ e e edd          d                    dee         ddfd            Z e e e             ed          d                    deeef         ddfd            Z e e edd          d                    dee         ddfd            ZdS )ListenFDsTestszV
    Apply tests to L{ListenFDs}, constructed based on an environment dictionary.
    r   
   )min_sizemax_sizenamesr   Nc                    t          |          }t          t          t          j        t          j        |z                       }t          j        t          t          j                              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fdss        r   test_fromEnvironmentEquivalencez.ListenFDsTests.test_fromEnvironmentEquivalence*   s     U5!193Cf3LMMNN'!")++..!&kk"%((5// 
 
 	C)Ku"F"FGGHHHHHr   c           	      (   |                      t          dt          dt          j                                         t	          j                    }|                     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'   r3   r   r2   assertEqualr0   r%   inheritedDescriptors)r5   sddaemons     r   test_defaultEnvironz"ListenFDsTests.test_defaultEnviron@   sq     	

2y"21bikk"B"BCCC,..eAuoo..0M0M0O0OPPPPPr   c                 r   t          dt          j                              }t          j        |          }t          j        |          }|                     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.
        r<   r!      N)	r'   r    r3   r   r2   r?   r0   r%   r@   )r5   envfirstseconds       r   test_secondEnvironmentz%ListenFDsTests.test_secondEnvironmentJ   s     q")++..)#666*3777eAqkk**E,F,F,H,HIIIV88::;;;;;r   c                     t          dt          j                    dz             }t          j        |          }|                     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.
        r<   r   rD   Nr'   r    r3   r   r2   r?   r@   r5   rF   rA   s      r   test_mismatchedPIDz!ListenFDsTests.test_mismatchedPIDW   sX     q")++/22,S999X::<<=====r   c                     t          dt          j                              }|d= t          j        |          }|                     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.
        r<   r   rD   NrK   rL   s      r   test_missingPIDVariablez&ListenFDsTests.test_missingPIDVariablec   s\     q")++..,S999X::<<=====r   c                     t          dd          }t          j        |          }|                     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.
        r<   hello, worldrD   N)r'   r   r2   r?   r@   rL   s      r   test_nonIntegerPIDVariablez)ListenFDsTests.test_nonIntegerPIDVariableo   sN    
 q.11,S999X::<<=====r   c                     t          dt          j                              }|d= |d= t          j        |          }|                     g |                                           dS )z
        If the I{LISTEN_FDS} and I{LISTEN_FDNAMES} environment variables
        are not present, no inherited descriptors are reported.
        r<   r   r   rD   NrK   rL   s      r   test_missingFDSVariablez&ListenFDsTests.test_missingFDSVariablex   se    
 q")++.. !,S999X::<<=====r   c                     t          dt          j                              }d|d<   t          j        |          }|                     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.
        r<   rQ   r   rD   NrK   rL   s      r   test_nonIntegerFDSVariablez)ListenFDsTests.test_nonIntegerFDSVariable   s^    
 q")++..*L,S999X::<<=====r   )	min_value	max_valueT)uniquer7   c                 J   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)	r4   mapr#   r   r@   r   r   appendr   )r5   r7   r-   r8   fdsCopys        r   test_inheritedDescriptorsz(ListenFDsTests.test_inheritedDescriptors   s     c#{++,,U++**,,K'!2!2333qKhw&7&7!8!899999r   )rW   )r,   expectedc                     t          |                                          }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                     g | ]\  }}|S r   r   )r   name_s      r   r   zAListenFDsTests.test_inheritedNamedDescriptors.<locals>.<listcomp>   s    +++'$+++r   c                     g | ]\  }}|S r   r   )r   rc   fds      r   r   zAListenFDsTests.test_inheritedNamedDescriptors.<locals>.<listcomp>   s    ---ear---r   N)r0   itemsr   r   inheritedNamedDescriptorsr   )r5   r_   rf   r-   r7   r8   s         r   test_inheritedNamedDescriptorsz-ListenFDsTests.test_inheritedNamedDescriptors   sz     X^^%%&&++U+++--u---U++C1133Xh5G5GHHHHHr   c                 R   t          t          t          |                    }t          ||          }|                     |                                |                                           |                     |                                |                                           dS )z
        Any subsequent calls to C{inheritedDescriptors} and
        C{inheritedNamedDescriptors} return the same list.
        N)r4   r[   r#   r   r?   r@   rg   )r5   r7   r-   rA   s       r   test_repeatedzListenFDsTests.test_repeated   s     c#{++,,[%00))++X-J-J-L-L	
 	
 	
 	..00(2T2T2V2V	
 	
 	
 	
 	
r   )r   N)__name__
__module____qualname____doc__r	   r   r   r   r#   r9   rB   rI   rM   rO   rR   rT   rV   r   intr^   r
   r   rh   rj   r   r   r   r)   r)   %   s7         U55''))ACCCDDIXc] It I I I EDI*Q Q Q Q< < < <
> 
> 
> 
>
> 
> 
> 
>> > > >	> 	> 	> 	>> > > > U55A444TBBBCC
:Xc] 
:t 
: 
: 
: DC
: U<<..00((Q2G2G2GRTUUUVV
IwsCx7H 
IT 
I 
I 
I WV
I U55A444TBBBCC
# 
4 
 
 
 DC
 
 
r   r)   )rn   r    typingr   r   r   hamcrestr   r   r   
hypothesisr	   hypothesis.strategiesr
   r   r   twisted.python.systemdr   twisted.trial.unittestr   
strategiesr   ro   objectr#   r'   r)   r   r   r   <module>rx      s,   
 
			 * * * * * * * * * * 0 0 0 0 0 0 0 0 0 0       ? ? ? ? ? ? ? ? ? ? , , , , , , 6 6 6 6 6 6 . . . . . .C f c3h     O
 O
 O
 O
 O
( O
 O
 O
 O
 O
r   