+
    Dfj2                        R t ^ RIt^ RIt^ RIt ^ RIt^ RIHt ^ RIH	t	H
t
 ^ RIHtHt ^ RIHtHt ^ RIHtHtHtHtHtHtHtHtHtHtHt ^ RIHt ^ RIH t  ^ R	I!H"t" ]! ]	PF                  PI                  ]
4      '       * R
4       ! R R]"4      4       t%]! ]	PF                  PI                  ]
4      '       * R
4       ! R R]"4      4       t&R#   ] d    Rt Li ; i)z
Whitebox tests for TCP APIs.
N)skipIf)
interfacesreactor)gatherResultsmaybeDeferred)ProtocolServerFactory)_ACCEPT_ERRORSEAGAINECONNABORTEDEINPROGRESSEMFILEENFILEENOBUFSENOMEMEPERMEWOULDBLOCKPort)log)platform)TestCasez?This test only applies to reactors that implement IReactorFDsetc                   |   a  ] tR t^)t o RtRtR tR tR t]	! ]
P                  ! 4       R8H  R4      R 4       tR	tV tR
# )PlatformAssumptionsTestsz,
Test assumptions about platform behaviors.
i    c                >   . V n         \        e   ^ RIHp \	        V! 4       4      ^,           p\        P
                  ! \        P                  4      V n        \        P                  ! \        P                  W P                  ^,          34       V^d,           V n	        R # R # )N)_listOpenFDs)
openSocketsresourcetwisted.internet.processr   len	getrlimitRLIMIT_NOFILEoriginalFileLimit	setrlimitsocketLimit)selfr   newLimits   &  A/usr/lib/python3/dist-packages/twisted/test/test_tcp_internals.pysetUpPlatformAssumptionsTests.setUp4   sy     ><>*Q.H%-%7%78N8N%OD"&&3I3I!3L(M  (#~D      c                n   V P                   '       d+   V P                   P                  4       P                  4        K<  \        eq   \        P                  ! \        P
                  4      ^,          p\        V P                  ^ ,          V4      p\        P                  ! \        P
                  W!34       R # R # N)	r   popcloser   r   r    minr!   r"   )r$   currentHardLimitnewSoftLimits   &  r&   tearDown!PlatformAssumptionsTests.tearDownF   s      "((*
  (11(2H2HI!Lt55a8:JKLx557WX  r)   c                f    \         P                   ! 4       pV P                  P                  V4       V# )zd
Create and return a new socket object, also tracking it so it can be
closed in the test tear down.
)socketr   append)r$   ss   & r&   r4   PlatformAssumptionsTests.socketR   s(    
 MMO"r)   win32zhWindows requires an unacceptably large amount of resources to provoke this behavior in the naive manner.c                   V P                  4       pVP                  R4       VP                  4       ^,          pVP                  ^4       V P                  4       pVP	                  R4       \        V P                  4       F  p V P                  4        K  	  V P                  R4       V P                  VP                  RV34      ^ \        34       V P                  \         P                   VP"                  4      pV P                  VP                  ^ ,          \        \        34       R#   \         d0   pTP                  ^ ,          \        \        39   d    Rp? K  h Rp?ii ; i)z
Test that the platform accept(2) call fails with either L{EMFILE} or
L{ENOBUFS} when there are too many file descriptors open.
	127.0.0.1FNz7Could provoke neither EMFILE nor ENOBUFS from platform.)r:       )r4   bindgetsocknamelistensetblockingranger#   OSErrorargsr   r   failassertIn
connect_exr   assertRaiseserroraccept)r$   portserverPortNumberclientieexcs   &      r&   test_acceptOutOfFiles.PlatformAssumptionsTests.test_acceptOutOfFiles[   s    {{}		"#++-a0A 5! t''(A ) IIOP 	{,<=>K@P	

 dkk:chhqkFG#45%  66!9 11 s   D++E%6"E E  E%)r   r!   r#   N)__name__
__module____qualname____firstlineno____doc__r#   r'   r1   r4   r   r   getTyperO   __static_attributes____classdictcell____classdict__s   @r&   r   r   )   sP     
 K.$
Y g%	5
%6
%6r)   r   c                   p  a  ] tR t^t o RtR tR tR tR tR t	R t
R t]! ]P                  ! 4       R	8H  R
4      R 4       t]! ]P                  ! 4       R	8H  R4      R 4       t]! ]P$                  P'                  R4      R8H  R4      R 4       t]! ]P                  ! 4       R	8H  R4      R 4       tR tRtV tR# )SelectReactorTestsz/
Tests for select-specific failure conditions.
c                v    . V n         . V n        \        P                  ! V P                  P                  4       R # r+   )portsmessagesr   addObserverr5   r$   s   &r&   r'   SelectReactorTests.setUp   s&    
,,-r)   c                    \         P                  ! V P                  P                  4       \	        V P
                   Uu. uF  p\        VP                  4      NK  	  up4      # u upi r+   )r   removeObserverr_   r5   r   r^   r   stopListening)r$   ps   & r&   r1   SelectReactorTests.tearDown   sD    4==//0djjQjmAOO<jQRRQs   A#c                v    \        WVR7      pVP                  4        V P                  P                  V4       V# )zg
Create, start, and return a new L{Port}, also tracking it so it can
be stopped in the test tear down.
	interface)r   startListeningr^   r5   )r$   
portNumberfactoryrj   rf   s   &&&& r&   rI   SelectReactorTests.port   s3    
 	:	

!r)   c                  a  ! V3R lR4      p\        4       pV P                  ^ VRR7      pV P                  VRV! 4       4       VP                  4        Rp\        P
                  S,          pV P                   Uu. uF3  pVP                  R4      V8H  ;'       d    VP                  R4      V8H  NK5  	  ppV P                  \        V4      ^ R	V P                  : 24       R
# u upi )aj  
Test behavior in the face of an exception from C{accept(2)}.

On any exception which indicates the platform is unable or unwilling
to allocate further resources to us, the existing port should remain
listening, a message should be logged, and the exception should not
propagate outward from doRead.

@param socketErrorNumber: The errno to simulate from accept.
c                   2   <a  ] tR t^t o RtV3R ltRtV tR# )9SelectReactorTests._acceptFailureTest.<locals>.FakeSocketz1
Pretend to be a socket in an overloaded system.
c                D   < \        S\        P                  ! S4      4      hr+   )rA   osstrerror)r$   socketErrorNumbers   &r&   rH   @SelectReactorTests._acceptFailureTest.<locals>.FakeSocket.accept   s    /=N1OPPr)    NrQ   rR   rS   rT   rU   rH   rW   rX   )rZ   ru   s   @r&   
FakeSocketrq      s     Q Qr)   ry   r:   ri   r4   z/Could not accept new connection ({acceptError})
log_formatacceptErrorz)Log event for failed accept not found in N)
r   rI   patchdoReaderrno	errorcoder_   getassertGreaterr   )	r$   ru   ry   rm   rI   expectedFormatexpectedErrorCodemsgmatchingMessagess	   &f       r&   _acceptFailureTest%SelectReactorTests._acceptFailureTest   s    	Q 	Q  /yyG{y;

4:<0J!OO,=> }}

 % %7 @ @GGM*.??@ % 	 
 	 !@DO	

s   <C#C#c                ,    V P                  \        4      # )z
C{accept(2)} can fail with C{EMFILE} when there are too many open file
descriptors in the process.  Test that this doesn't negatively impact
any other existing connections.

C{EMFILE} mainly occurs on Linux when the open file rlimit is
encountered.
)r   r   ra   s   &r&   test_tooManyFilesFromAccept.SelectReactorTests.test_tooManyFilesFromAccept        &&v..r)   c                ,    V P                  \        4      # )z
Similar to L{test_tooManyFilesFromAccept}, but test the case where
C{accept(2)} fails with C{ENOBUFS}.

This mainly occurs on Windows and FreeBSD, but may be possible on
Linux and other platforms as well.
)r   r   ra   s   &r&   test_noBufferSpaceFromAccept/SelectReactorTests.test_noBufferSpaceFromAccept   s     &&w//r)   c                ,    V P                  \        4      # )z
Similar to L{test_tooManyFilesFromAccept}, but test the case where
C{accept(2)} fails with C{ECONNABORTED}.

It is not clear whether this is actually possible for TCP
connections on modern versions of Linux.
)r   r   ra   s   &r&    test_connectionAbortedFromAccept3SelectReactorTests.test_connectionAbortedFromAccept   s     &&|44r)   r8   z(Windows accept(2) cannot generate ENFILEc                ,    V P                  \        4      # )z
Similar to L{test_tooManyFilesFromAccept}, but test the case where
C{accept(2)} fails with C{ENFILE}.

This can occur on Linux when the system has exhausted (!) its supply
of inodes.
)r   r   ra   s   &r&   test_noFilesFromAccept)SelectReactorTests.test_noFilesFromAccept   r   r)   z(Windows accept(2) cannot generate ENOMEMc                ,    V P                  \        4      # )as  
Similar to L{test_tooManyFilesFromAccept}, but test the case where
C{accept(2)} fails with C{ENOMEM}.

On Linux at least, this can sensibly occur, even in a Python program
(which eats memory like no ones business), when memory has become
fragmented or low memory has been filled (d_alloc calls
kmem_cache_alloc calls kmalloc - kmalloc only allocates out of low
memory).
)r   r   ra   s   &r&   test_noMemoryFromAccept*SelectReactorTests.test_noMemoryFromAccept   s     &&v..r)   INFRASTRUCTUREAZUREPIPELINESz(Hangs on Azure Pipelines due to firewallc                >  aa \        4       p\        Vn        V P                  ^ VRR7      oV P	                  SP
                  4       . oV3R lpV P	                  V4       V3R lpSP                  V! 4       4       ^Sn        SP                  4        V P                  SP                  ^4       SP                  V! 4       4       SP                  4        V P                  SP                  ^4       SP                  4        V P                  SP                  ^4       R# )z
L{tcp.Port.doRead} increases the number of consecutive
C{accept} calls it performs if all of the previous C{accept}
calls succeed; otherwise, it reduces the number to the amount
of successful calls.
r:   ri   c                  :   < S F  p V P                  4        K  	  R # r+   )r-   )rK   clientss    r&   closeAll7SelectReactorTests.test_acceptScaling.<locals>.closeAll  s    ! "r)   c                     < \         P                   ! \         P                  \         P                  4      p V P                  R SP	                  4       P
                  34       V # )r:   )r4   AF_INETSOCK_STREAMconnectgetHostrI   )rK   rI   s    r&   r   6SelectReactorTests.test_acceptScaling.<locals>.connect  s?    ]]6>>63E3EFFNNK)<)<=>Mr)   N)r   r   protocolrI   
addCleanupre   r5   numberAcceptsr}   r   assertEqual)r$   rm   r   r   r   rI   s   &   @@r&   test_acceptScaling%SelectReactorTests.test_acceptScaling  s      /#yyG{y;**+	 	!	
 	wy!4--q1wy! 	++Q/ 	++Q/r)   z'Windows accept(2) cannot generate EPERMc                  a aa ^{o^ .o ! VVV 3R lR4      p\        S4       F2  pS P                  \        P                  V! 4       P                  4       K4  	  S P                  S P
                  V! 4       P                  4       ^ .o\        4       pS P                  ^ VRR7      p^{Vn        S P                  VRV! 4       4       VP                  4        S P                  VP                  ^4       R# )z
C{accept(2)} returning C{EPERM} is treated as a transient
failure and the call retried no more than the maximum number
of consecutive C{accept(2)} calls.
c                   6   <a  ] tR tRt o RtVVV3R ltRtV tR# )LSelectReactorTests.test_permissionFailure.<locals>.FakeSocketWithAcceptLimiti<  {
Pretend to be a socket in an overloaded system whose
C{accept} method can only be called
C{maximumNumberOfAccepts} times.
c                   < S^ ;;,          ^,          uu&   S^ ,          S8  d   SP                  R4       \        \        \        P                  ! \        4      4      h)r;   z(Maximum number of accept calls exceeded.)rC   rA   r   rs   rt   )oselfacceptCallsmaximumNumberOfAcceptsr$   s   &r&   rH   SSelectReactorTests.test_permissionFailure.<locals>.FakeSocketWithAcceptLimit.acceptC  sA    A!#q>$::IIHIeR[[%788r)   rw   Nrx   )rZ   r   r   r$   s   @r&   FakeSocketWithAcceptLimitr   <  s     9 9r)   r   r:   ri   r4   N)r@   rF   r4   rG   rH   failureExceptionr   rI   r   r|   r}   assertEquals)r$   r   _rm   rI   r   r   s   f    @@r&   test_permissionFailure)SelectReactorTests.test_permissionFailure2  s     "%c	9 	9 -.Afll,E,G,N,NO / 	$//1J1L1S1STc/yyG{y; 

4#<#>? 	 	$,,a0r)   c                  a \        \        4      pVP                  \        \        \
        .4       \        R V 4       4      ^,           o ! V3R lR4      p\        4       pV P                  ^ VRR7      pV P                  VRV! 4       4       VP                  4        V P                  \        P                  4      pV P                  ^\        V4      4       V P                  V^ ,          P                   P"                  ^ ,          S4       R# )ze
A C{socket.error} raised by C{accept(2)} whose C{errno} is
unknown to the recovery logic is logged.
c              3   V   "   T F  p\        V\        4      '       g   K  Vx  K!  	  R # 5ir+   )
isinstanceint).0rG   s   & r&   	<genexpr>BSelectReactorTests.test_unknownSocketErrorRaise.<locals>.<genexpr>g  s     I;%*UC2H;s   )
)c                   2   <a  ] tR tRt o RtV3R ltRtV tR# )YSelectReactorTests.test_unknownSocketErrorRaise.<locals>.FakeSocketWithUnknownAcceptErrorij  r   c                   < \        SR 4      h)zunknown socket error message)rA   )r   unknownAcceptErrors   &r&   rH   `SelectReactorTests.test_unknownSocketErrorRaise.<locals>.FakeSocketWithUnknownAcceptError.acceptq  s    02PQQr)   rw   Nrx   )rZ   r   s   @r&    FakeSocketWithUnknownAcceptErrorr   j  s     R Rr)   r   r:   ri   r4   N)listr	   extendr
   r   r   maxr   rI   r|   r}   flushLoggedErrorsr4   rG   r   r   valuerB   )r$   knownErrorsr   rm   rI   failuresr   s   &     @r&   test_unknownSocketErrorRaise/SelectReactorTests.test_unknownSocketErrorRaise^  s    
 >*FE;78 I;IIAM 		R 	R  /yyG{y;

4#C#EF))&,,7CM*!**//24FGr)   )r_   r^   N)rQ   rR   rS   rT   rU   r'   r1   rI   r   r   r   r   r   r   rV   r   r   rs   environr   r   r   r   rW   rX   rY   s   @r&   r\   r\      s     
.
S'
R	/05 H')+UV/ W/ H')+UV/ W/ 


'(,<<2'0	'0R H')+TU)1 V)1VH Hr)   r\   )'rU   r~   rs   r4   r   ImportErrorunittestr   twisted.internetr   r   twisted.internet.deferr   r   twisted.internet.protocolr   r   twisted.internet.tcpr	   r
   r   r   r   r   r   r   r   r   r   twisted.pythonr   twisted.python.runtimer   twisted.trial.unittestr   IReactorFDSet
providedByr   r\   rw   r)   r&   <module>r      s   
  	   0 ? =     + +   ++G44EX6x X6	X6v   ++G44EpH pH	pH{  Hs   C
 
	CC