o
    Óó·i}w  ã                	   @  s*  d Z ddlmZ ddlZddlmZmZmZmZm	Z	m
Z
mZmZmZmZmZ ddlmZ ddlmZmZmZmZmZmZ ddl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dl+m,Z,m-Z- G dd„ dƒZ.G dd„ de.ej/ƒZ0G dd„ de.ej1ƒZ2G dd„ de0ƒZ3G dd„ de4ƒZ5G dd„ dej/ƒZ6ee 7ed¡ dƒG dd„ de-ƒƒZ8ee 7ed¡ dƒG dd„ de-ƒƒZ9d7d*d+„Z:ee #ed¡ d,ƒee:eeed-d.ƒ d/ƒG d0d1„ d1e-ƒƒƒZ;ee #ed¡ d,ƒe*ee:ee	e
d2d3ƒ d4ƒG d5d6„ d6e;ƒƒƒƒZ<dS )8zG
Tests for implementations of L{IReactorUDP} and L{IReactorMulticast}.
é    )ÚannotationsN)ÚAF_INETÚAF_INET6ÚIP_ADD_MEMBERSHIPÚ
IPPROTO_IPÚIPPROTO_IPV6ÚIPV6_JOIN_GROUPÚ
SOCK_DGRAMÚAddressFamilyÚif_nameindexÚ	inet_ptonÚsocket)ÚskipIf)ÚdeferÚerrorÚ
interfacesÚprotocolÚreactorÚudp)ÚIPv4AddressÚIPv6Address)ÚDeferredÚgatherResultsÚmaybeDeferred)ÚMulticastJoinError)ÚIMulticastTransportÚIReactorMulticastÚIReactorTime)Ú
deferLater)Úruntime)ÚskipWithoutIPv6)ÚSkipTestÚTestCasec                   @  s0   e Zd ZdZdZdZdd„ Zdd„ Zdd„ ZdS )	ÚMixinr   Nc                 C  s
   g | _ d S ©N)Úpackets©Úself© r(   ú[C:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/test/test_udp.pyÚ__init__0   ó   
zMixin.__init__c                 C  s0   d| _ | jd ur| jd }| _| d ¡ d S d S ©Né   )ÚstartedÚstartedDeferredÚcallback©r'   Údr(   r(   r)   ÚstartProtocol3   s
   
þzMixin.startProtocolc                 C  s
   d| _ d S r,   )Ústoppedr&   r(   r(   r)   ÚstopProtocol9   r+   zMixin.stopProtocol)	Ú__name__Ú
__module__Ú__qualname__r.   r4   r/   r*   r3   r5   r(   r(   r(   r)   r#   *   s    r#   c                   @  s.   e Zd ZU dZded< dZded< dd„ ZdS )	ÚServerNzDeferred[None] | NoneÚpacketReceivedr   r   Ú	transportc                 C  s:   | j  ||f¡ | jd ur| jd }| _| d ¡ d S d S r$   ©r%   Úappendr:   r0   )r'   ÚdataÚaddrr2   r(   r(   r)   ÚdatagramReceivedB   s
   
þzServer.datagramReceived)r6   r7   r8   r:   Ú__annotations__Úrefusedr@   r(   r(   r(   r)   r9   =   s
   
 r9   c                   @  s,   e Zd ZdZdZdd„ Zdd„ Zdd„ ZdS )	ÚClientNr   c                 C  s6   | j  |¡ | jd ur| jd }| _| d ¡ d S d S r$   r<   )r'   r>   r2   r(   r(   r)   r@   M   s
   
þzClient.datagramReceivedc                 C  s,   | j d ur| j d }| _ | |¡ || _d S r$   )r/   ÚerrbackÚfailure)r'   rE   r2   r(   r(   r)   ÚconnectionFailedS   s   


zClient.connectionFailedc                 C  ó2   | j d ur| j d }| _ | t d¡¡ d| _d S ©NZyupr-   ©r/   rD   r   ÚConnectionRefusedErrorrB   r1   r(   r(   r)   ÚconnectionRefusedY   ó   

zClient.connectionRefused)r6   r7   r8   r:   rB   r@   rF   rK   r(   r(   r(   r)   rC   I   s    rC   c                   @  ó   e Zd Zdd„ ZdS )Ú
GoodClientc                 C  rG   rH   rI   r1   r(   r(   r)   rK   a   rL   zGoodClient.connectionRefusedN)r6   r7   r8   rK   r(   r(   r(   r)   rN   `   ó    rN   c                   @  s   e Zd ZdZdS )ÚBadClientErrorzf
    Raised by BadClient at the end of every datagramReceived call to try and
    screw stuff up.
    N)r6   r7   r8   Ú__doc__r(   r(   r(   r)   rP   h   s    rP   c                   @  s$   e Zd ZdZdZdd„ Zdd„ ZdS )Ú	BadClientz�
    A DatagramProtocol which always raises an exception from datagramReceived.
    Used to test error handling behavior in the reactor for that method.
    Nc                 C  s
   || _ dS )ze
        Set the Deferred which will be called back when datagramReceived is
        called.
        N)r2   r1   r(   r(   r)   ÚsetDeferredw   s   
zBadClient.setDeferredc                 C  s*   | j d ur| j d }| _ | |¡ tdƒ‚)NzApplication code is very buggy!)r2   r0   rP   )r'   Úbytesr?   r2   r(   r(   r)   r@   ~   s   

zBadClient.datagramReceived)r6   r7   r8   rQ   r2   rS   r@   r(   r(   r(   r)   rR   o   s
    rR   ú!This reactor does not support UDPc                   @  sr   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zee	j
 d¡dkdƒdd„ ƒZdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚUDPTestsc                   s<   t ƒ }t ¡  }|_tjd|dd�‰ ‡ ‡fdd„}| |¡S )zu
        The C{type} of the host address of a listening L{DatagramProtocol}'s
        transport is C{"UDP"}.
        r   ú	127.0.0.1©Ú	interfacec                   s   ˆ   ¡ }ˆ |jd¡ ˆ  ¡ S )NZUDP)ÚgetHostÚassertEqualÚtypeÚstopListening)Úignoredr?   ©Úpr'   r(   r)   Ú	cbStarted�   s   z+UDPTests.test_oldAddress.<locals>.cbStartedN©r9   r   r   r/   r   Ú	listenUDPÚaddCallback)r'   Úserverr2   ra   r(   r_   r)   Útest_oldAddress‡   s
   
zUDPTests.test_oldAddressc                   sR   t ƒ ‰t ¡  }ˆ_tjdˆdd�‰ ‡ ‡‡fdd„}‡‡fdd„}| |¡ |¡S )	z±
        The L{DatagramProtocol}'s C{startProtocol} and C{stopProtocol}
        methods are called when its transports starts and stops listening,
        respectively.
        r   rW   rX   c                   s$   ˆ  ˆjd¡ ˆ  ˆjd¡ ˆ  ¡ S )Nr-   r   )r[   r.   r4   r]   ©r^   ©Úport1r'   re   r(   r)   ra   ¡   s   z*UDPTests.test_startStop.<locals>.cbStartedc                   s   ˆ   ˆjd¡ d S r,   )r[   r4   rg   )r'   re   r(   r)   Ú	cbStopped¦   s   z*UDPTests.test_startStop.<locals>.cbStoppedNrb   )r'   r2   ra   rj   r(   rh   r)   Útest_startStop—   s   zUDPTests.test_startStopc                   sF   t ƒ ‰t ¡  }ˆ_tjdˆdd�}dd„ ‰ ‡ ‡fdd„}| ˆ |¡S )	zr
        Re-listening with the same L{DatagramProtocol} re-invokes the
        C{startProtocol} callback.
        r   rW   rX   c                 S  s   |  ¡ S r$   ©r]   )r^   Úportr(   r(   r)   ra   ´   ó   z'UDPTests.test_rebind.<locals>.cbStartedc                   s*   t  ¡  }ˆ_tjdˆdd�}| ˆ |¡S ©Nr   rW   rX   )r   r   r/   r   rc   rd   )r^   r2   r`   ©ra   re   r(   r)   rj   ·   s   z'UDPTests.test_rebind.<locals>.cbStoppedNrb   )r'   r2   r`   rj   r(   rp   r)   Útest_rebind«   s   zUDPTests.test_rebindc                   sX   t ƒ ‰t ¡  }ˆ_tjdˆdd�‰ ‡ ‡‡fdd„}| |¡ ‡ fdd„}| |¡ |S )	z�
        A L{CannotListenError} exception is raised when attempting to bind a
        second protocol instance to an already bound port
        r   rW   rX   c                   s>   ˆ  ˆ  ¡ ˆj ¡ ¡ tƒ }ˆjtjtjˆ  ¡ j	|dd� d S )NrW   rX   )
r[   rZ   r;   r9   ÚassertRaisesr   ZCannotListenErrorr   rc   rm   )r^   Zserver2©rm   r'   re   r(   r)   ra   Ç   s   
ûz*UDPTests.test_bindError.<locals>.cbStartedc                   s   ˆ   ¡ S r$   rl   rg   ©rm   r(   r)   Ú
cbFinishedÔ   rn   z+UDPTests.test_bindError.<locals>.cbFinishedNrb   )r'   r2   ra   ru   r(   rs   r)   Útest_bindError¾   s   

zUDPTests.test_bindErrorc                   s    t ƒ ‰t ¡  }ˆ_tjdˆdd�‰tƒ ‰ t ¡  ‰ˆ _‡ ‡‡fdd„}| |¡}‡ ‡fdd„}| |¡ ‡ ‡‡fdd	„}| |¡ ‡‡fd
d„}| |¡ |S )zŽ
        Datagrams can be sent with the transport's C{write} method and
        received via the C{datagramReceived} callback method.
        r   rW   rX   c                   s   t jdˆ dd�ˆ_ˆS ro   )r   rc   Úport2rg   )ÚclientÚclientStartedr'   r(   r)   ÚcbServerStartedæ   s   z2UDPTests.test_sendPackets.<locals>.cbServerStartedc                   sŒ   ˆj  dˆj  ¡ j¡ ˆj  ¡ }ˆj  ¡ }t ¡  }ˆ_ˆj  d|j|jf¡ ddd|j|jffg‰‡ ‡‡‡fdd„‰ tj	ˆ d ƒ|gdd	�S )
NrW   ó   hello)ó   a)ó   bNó   cc                   s6   ˆrt  ¡  }ˆ_| ˆ ¡ ˆjjˆ d¡Ž  |S d S ©Nr   )r   r   r:   rd   r;   ÚwriteÚpop)r^   ZnextClientWrite)ÚcbClientSendrx   ÚclientWritesre   r(   r)   r‚   ö   s   
üzHUDPTests.test_sendPackets.<locals>.cbClientStarted.<locals>.cbClientSendT©ZfireOnOneErrback)
r;   ÚconnectrZ   rm   r   r   r:   r€   ÚhostÚDeferredList)r^   ÚcAddrÚsAddrZ
serverSend)rx   re   )r‚   rƒ   r)   ÚcbClientStartedì   s   


ÿz2UDPTests.test_sendPackets.<locals>.cbClientStartedc                   s`   ˆ j  ¡ }ˆj  ¡ }ˆ ˆ jd|j|jffg¡ |j|jf}ˆ ˆjd|fd|fd|fg¡ d S )Nr{   r|   r}   r~   )r;   rZ   r[   r%   r†   rm   )r^   rˆ   r‰   Z
clientAddr©rx   r'   re   r(   r)   ÚcbSendsFinished  s   

þz2UDPTests.test_sendPackets.<locals>.cbSendsFinishedc                   s$   t jt  ˆ j¡t  ˆjj¡gdd�S ©NTr„   )r   r‡   r   r]   rw   rg   )ri   r'   r(   r)   ru     s   
þûz-UDPTests.test_sendPackets.<locals>.cbFinishedN)r9   r   r   r/   r   rc   rN   rd   )r'   ÚserverStartedrz   r2   rŠ   rŒ   ru   r(   )rx   ry   ri   r'   re   r)   Útest_sendPacketsÚ   s   




	zUDPTests.test_sendPacketsZINFRASTRUCTUREZAZUREPIPELINESz"Hangs on Pipelines due to firewallc                   s�   t ƒ ‰ t ¡  }ˆ _tjdˆ dd�‰tƒ ‰t ¡  }ˆ_tjdˆdd�‰tj||gdd�}‡ ‡‡fdd„}| |¡ ‡‡fdd	„}| |¡ |S )zÎ
        A L{ConnectionRefusedError} exception is raised when a connection
        attempt is actively refused by the other end.

        Note: This test assumes no one is listening on port 80 UDP.
        r   rW   rX   Tr„   c                   s^   t  ¡  }ˆ _ˆ j dd¡ tdƒD ]}ˆ j d|f ¡ ˆj d|f d¡ qˆ |tj	¡S )NrW   éP   é
   ó   %d©rW   r�   )
r   r   r/   r;   r…   Úranger€   ÚassertFailurer   rJ   )r^   rK   Úir‹   r(   r)   ra   3  s   z2UDPTests.test_connectionRefused.<locals>.cbStartedc                   ó"   t jt  ˆ j¡t  ˆj¡gdd�S r�   ©r   r‡   r   r]   rg   )rm   rw   r(   r)   ru   ?  ó   

þûz3UDPTests.test_connectionRefused.<locals>.cbFinishedN)	rN   r   r   r/   r   rc   r9   r‡   rd   )r'   ry   rŽ   r2   ra   ru   r(   )rx   rm   rw   r'   re   r)   Útest_connectionRefused  s   


	zUDPTests.test_connectionRefusedc           	        sÐ   t ƒ ‰ t ¡  }ˆ _tjdˆ dd�‰d‰tƒ ‰t ¡  }ˆ_t ¡  }ˆ_tjdˆdd�‰tj||gdd�}‡ ‡‡‡fdd„}| 	|¡ tj||gdd�}‡‡‡fd	d
„}| 	|¡ ‡‡fdd„}| 	|¡ |S )as  
        When a server fails to successfully read a packet the server should
        still be able to process future packets.
        The IOCP reactor had a historical problem where a failure to read caused
        the reactor to ignore any future reads. This test should prevent a regression.

        Note: This test assumes no one is listening on port 80 UDP.
        r   rW   rX   s   Sending test packet to serverTr„   c                   s&   ˆj  dd¡ ˆ j  ˆdˆjf¡ d S )Ns$   write to port no one is listening tor“   rW   )r;   r€   Z_realPortNumberrg   )rx   re   Ú
serverPortÚtest_data_to_sendr(   r)   ÚcbClientAndServerStartedb  s   ÿ
ÿzAUDPTests.test_serverReadFailure.<locals>.cbClientAndServerStartedc                   s   ˆ   ˆjd d ˆ¡ d S r   )r[   r%   rg   )r'   re   rœ   r(   r)   Úverify_server_got_datau  s   z?UDPTests.test_serverReadFailure.<locals>.verify_server_got_datac                   r—   r�   r˜   rg   )Ú
clientPortr›   r(   r)   Úcleanupz  r™   z0UDPTests.test_serverReadFailure.<locals>.cleanupN)
rN   r   r   r/   r   rc   r9   r:   r‡   rd   )	r'   ry   rŽ   ZserverGotDataZserver_client_started_dr�   Zall_data_sentrž   r    r(   )rx   rŸ   r'   re   r›   rœ   r)   Útest_serverReadFailureK  s*   	ÿ
ÿ

	zUDPTests.test_serverReadFailurec                 C  sV   t ƒ }tjd|dd�}|  tj|jjdd¡ |j dd¡ |  t|jjdd¡ | 	¡ S )a   
        A call to the transport's connect method fails with an
        L{InvalidAddressError} when a non-IP address is passed as the host
        value.

        A call to a transport's connect method fails with a L{RuntimeError}
        when the transport is already connected.
        r   rW   rX   Ú	localhostr�   N)
rN   r   rc   rr   r   ZInvalidAddressErrorr;   r…   ÚRuntimeErrorr]   )r'   rx   rm   r(   r(   r)   Útest_badConnect‡  s   	ÿzUDPTests.test_badConnectc                   s„   t  ¡ ‰‡fdd„}ˆ |¡ tƒ ‰tjdˆdd�‰‡fdd„}ˆ |¡ ˆ ¡ ‰ tt	dƒƒ‰g ‰‡ ‡‡‡‡‡‡fd	d
„‰ˆƒ  ˆS )zr
        When datagramReceived raises an exception it is logged but the port
        is not disconnected.
        c                   s*   ˆ   t¡}ˆ  t|ƒddt|ƒf ¡ dS )z…
            Flush the exceptions which the reactor should have logged and make
            sure they're actually there.
            é   z'Incorrectly found %d errors, expected 2N)ÚflushLoggedErrorsrP   r[   Úlen)ÚignZerrsr&   r(   r)   ÚcbCompleted   s   
ÿz8UDPTests.test_datagramReceivedError.<locals>.cbCompletedr   rW   rX   c                   s   t  ˆj¡ ‡ fdd„¡S )zƒ
            Disconnect the port we started and pass on whatever was given to us
            in case it was a Failure.
            c                   ó   ˆ S r$   r(   ©r¨   ©Úresultr(   r)   Ú<lambda>´  ó    zHUDPTests.test_datagramReceivedError.<locals>.cbCleanup.<locals>.<lambda>N)r   r   r]   ÚaddBothr¬   rt   r¬   r)   Ú	cbCleanup¯  s   z6UDPTests.test_datagramReceivedError.<locals>.cbCleanupé<   c                    sÆ   ˆszˆ  d¡ W n ty   ˆ ¡  Y nw ˆ ˆjdd¡ dˆ d¡f } t ¡ }ˆ |¡ ˆj 	| ˆj
ˆjf¡ ‡‡‡‡ fdd„}‡fdd	„}| ||¡ | ˆj¡ t d
|jt d¡¡‰ dS )a6  
            Send one packet to the listening BadClient.  Set up a 0.1 second
            timeout to do re-transmits in case the packet is dropped.  When two
            packets have been received by the BadClient, stop sending and let
            the finalDeferred's callbacks do some assertions.
            zNot enough packets receivedNzUDP Protocol lost its transportr’   r   c                   s<   ˆ  ¡  ˆ | ¡ tˆƒdkrt dˆ jd¡ dS ˆƒ  dS )z…
                A packet arrived.  Cancel the timeout for it, record it, and
                maybe finish the test.
                r¥   r   N)Úcancelr=   r§   r   Ú	callLaterr0   )Úpacket)ÚfinalDeferredÚmakeAttemptÚsucceededAttemptsÚtimeoutCallr(   r)   ÚcbPacketReceivedÛ  s
   

zRUDPTests.test_datagramReceivedError.<locals>.makeAttempt.<locals>.cbPacketReceivedc                   s
   ˆ ƒ  dS )aÄ  
                The packet wasn't received quickly enough.  Try sending another
                one.  It doesn't matter if the packet for which this was the
                timeout eventually arrives: makeAttempt throws away the
                Deferred on which this function is the errback, so when
                datagramReceived callbacks, so it won't be on this Deferred, so
                it won't raise an AlreadyCalledError.
                Nr(   )Úerr)r·   r(   r)   ÚebPacketTimeoutë  s   
	zQUDPTests.test_datagramReceivedError.<locals>.makeAttempt.<locals>.ebPacketTimeoutgš™™™™™¹?z&Timed out in testDatagramReceivedError)ZfailÚ	ExceptionrD   ZfailIfIdenticalr;   r�   r   r   rS   r€   r†   rm   ZaddCallbacksZ
addErrbackr   r´   r   ÚTimeoutError)rµ   ZpacketDeferredrº   r¼   )r?   Úattemptsrx   r¶   r·   r'   r¸   )r¹   r)   r·   Å  s,   ÿÿ
ýz8UDPTests.test_datagramReceivedError.<locals>.makeAttemptN)
r   r   rd   rR   r   rc   r°   rZ   Úlistr”   )r'   r©   r±   r(   )r?   r¿   rx   r¶   r·   rm   r'   r¸   r)   Útest_datagramReceivedError™  s   



:z#UDPTests.test_datagramReceivedErrorc                 C  sR   G dd„ dƒ}t  dtƒ ¡}|ƒ |_| dd¡ |  | jg¡}|  t|ƒd¡ dS )ai  
        C{'<broadcast>'} is an alternative way to say C{'255.255.255.255'}
        ({socket.gethostbyname("<broadcast>")} returns C{'255.255.255.255'}),
        so because it becomes a valid IP address, no deprecation warning about
        passing hostnames to L{twisted.internet.udp.Port.write} needs to be
        emitted by C{write()} in this case.
        c                   @  rM   )z6UDPTests.test_NoWarningOnBroadcast.<locals>.fakeSocketc                 S  s   d S r$   r(   )r'   ZfooÚbarr(   r(   r)   Úsendto  s   z=UDPTests.test_NoWarningOnBroadcast.<locals>.fakeSocket.sendtoN)r6   r7   r8   rÃ   r(   r(   r(   r)   Ú
fakeSocket  rO   rÄ   r   s   test)z<broadcast>iÒ  N)	r   ZPortr9   r   r€   ZflushWarningsÚtest_NoWarningOnBroadcastr[   r§   )r'   rÄ   r`   Úwarningsr(   r(   r)   rÅ     s   	z"UDPTests.test_NoWarningOnBroadcastN)r6   r7   r8   rf   rk   rq   rv   r�   r   ÚosÚenvironÚgetrš   r¡   r¤   rÁ   rÅ   r(   r(   r(   r)   rV   …   s    Dþ
)<irV   c                   @  s8   e Zd ZdZe ed¡sdZdd„ Zdd„ Z	dd	„ Z
dS )
ÚReactorShutdownInteractionTestsz!Test reactor shutdown interactionNrU   c                 C  s    t ƒ | _tjd| jdd�| _dS )zStart a UDP portr   rW   rX   N)r9   re   r   rc   rm   r&   r(   r(   r)   ÚsetUp  s   z%ReactorShutdownInteractionTests.setUpc                 C  s
   | j  ¡ S )zStop the UDP portN)rm   r]   r&   r(   r(   r)   ÚtearDown#  s   
z(ReactorShutdownInteractionTests.tearDownc                   sh   t  ¡ ‰ t  ¡  }ˆj_‡ ‡fdd„}| |¡ ‡fdd„}ˆ  |¡ ˆjj ddˆjj ¡ jf¡ ˆ S )z0Test reactor shutdown while in a recvfrom() loopc                   s    ˆj j ¡  t dˆ jd ¡ d S r   )re   r;   ZconnectionLostr   r´   r0   rg   ©Úfinishedr'   r(   r)   ÚpktRece6  s   zQReactorShutdownInteractionTests.testShutdownFromDatagramReceived.<locals>.pktRecec                   s   ˆ   ¡  d S r$   )r¦   rg   r&   r(   r)   ÚflushErrors@  s   zUReactorShutdownInteractionTests.testShutdownFromDatagramReceived.<locals>.flushErrorss@                                                                   rW   N)	r   r   re   r:   rd   r;   r€   rZ   rm   )r'   ÚprrÏ   rÐ   r(   rÍ   r)   Ú testShutdownFromDatagramReceived'  s   

ÿz@ReactorShutdownInteractionTests.testShutdownFromDatagramReceived)r6   r7   r8   rQ   r   ÚIReactorUDPr   ÚskiprË   rÌ   rÒ   r(   r(   r(   r)   rÊ     s    rÊ   Úafr
   ÚipprotoÚintÚjoinÚgroupÚstrÚbindtoÚreturnÚboolc                 C  s|   t | tƒ}| |df¡ t| |ƒ}t| |ƒ}z"z| |||| ¡ W n ty1   Y W | ¡  dS w W | ¡  dS | ¡  w )a	  
    Bind a socket to the given network interface and attempt to join a
    multicast group with the given address family.  This is used to determine
    whether the local networking stack configuration allows for multicast
    within the given address family.
    r   FTN)r   r	   Úbindr   Ú
setsockoptÚOSErrorÚclose)rÕ   rÖ   rØ   rÙ   rÛ   ÚsZ	packgroupZpackifr(   r(   r)   ÚcheckMulticastAvailabilityN  s   
	

ûrã   z'This reactor does not support multicastú225.0.0.250ú0.0.0.0z:The local networking stack does not enable IPv4 multicast.c                   @  sú   e Zd ZU dZdZded< dZded< dZded< d	Zded
< dZ	ded< dZ
ded< dZded< dZded< dd„ Z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j ¡ ocej ¡  d!ƒd"d#„ ƒZd$d%„ Zeej ¡ d&ƒd*d'd(„ƒZd)S )+ÚMulticastTestszs
    Tests for sending and receiving multicast datagrams.  (This suite supports
    IPv4, see below for IPv6.)
    rå   rÚ   rY   ú	str | intÚexpectedInterfacerW   ÚclientAddressrä   ÚmulticastGroupÚalternateInterfacer¢   ÚinterfaceSynonymÚinvalidGroupú::1ÚwrongAddressFamilyc                 C  sX   t ƒ | _tƒ | _t d| j| j¡| _t d| j| j¡| _| jj	 
| j| jj	 ¡ j¡ d S r   )r9   re   rC   rx   r   ÚlistenMulticastrY   ri   rw   r;   r…   ré   rZ   rm   r&   r(   r(   r)   rË   €  s   þzMulticastTests.setUpc                 C  s   t t| jjƒt| jjƒgƒS r$   )r   r   ri   r]   rw   r&   r(   r(   r)   rÌ   ‹  s
   

þÿzMulticastTests.tearDownrÜ   ÚNonec                   s<   ˆ j jjtkrtdƒ‚d‡ fdd„}|ˆ jƒ |ˆ j ƒ d S )	Nz+only IPv4 multicast has TTLs, IPv6 has hopsÚoúServer | ClientrÜ   rñ   c                   sD   t  | j¡sJ ‚ˆ  | j ¡ d¡ | j d¡ ˆ  | j ¡ d¡ d S )Nr-   r¥   )r   Z
providedByr;   r[   ZgetTTLZsetTTL)rò   r&   r(   r)   Úcheckttl—  s   z(MulticastTests.testTTL.<locals>.checkttl)rò   ró   rÜ   rñ   )re   r;   ZaddressFamilyr   r!   rx   )r'   rô   r(   r&   r)   ÚtestTTL“  s
   
zMulticastTests.testTTLc                 Ã  sâ   �| j }|  | jj ¡ d¡ | jj ¡ }| jj |¡I dH  tƒ | j_| jj 	d||j
f¡ | jjI dH  |  t| jjƒd¡ | jj d¡ |  | jj ¡ d¡ | jj 	d||j
f¡ tttƒdƒI dH  |  t| jjƒd¡ dS )zv
        Test that after loopback mode has been set, multicast packets are
        delivered to their sender.
        r-   Nr{   r   )rê   r[   re   r;   ZgetLoopbackModerZ   Ú	joinGroupr   r:   r€   rm   r§   r%   ZsetLoopbackModer   r   r   )r'   rÙ   r?   r(   r(   r)   Útest_loopback   s   €
zMulticastTests.test_loopbackc                 Ã  sN   �|   t¡� | jjj| j| jd�I dH  W d  ƒ dS 1 s w   Y  dS )z”
        A sensible error will be reported when using the wrong address family
        literal for the 'interface' argument to 'joinGroup'.
        rX   N)rr   r   re   r;   rö   rê   rï   r&   r(   r(   r)   Útest_wrongFamilyInterfaceJoin¸  s   €ÿ"ÿz,MulticastTests.test_wrongFamilyInterfaceJoinc                 Ã  sŠ   �|   | jj ¡ | j¡ |   | jj ¡ | j¡ | jj | j¡I dH  | jj | j¡I dH  |   | jj ¡ | j¡ |   | jj ¡ | j¡ dS )zK
        Test C{getOutgoingInterface} and C{setOutgoingInterface}.
        N)	r[   rx   r;   ZgetOutgoingInterfacerè   re   ZsetOutgoingInterfacerë   rì   r&   r(   r(   r)   Útest_interfaceÂ  s"   €ÿÿ
þ
þzMulticastTests.test_interfacec                 Ã  s^   �| j j | j¡I dH  | j j | j¡I dH  | jj | j¡I dH  | jj | j¡I dH  dS )zE
        Test that multicast a group can be joined and left.
        N)rx   r;   rö   rê   Z
leaveGroupre   r&   r(   r(   r)   Útest_joinLeaveÙ  s
   €zMulticastTests.test_joinLeavez2Windows' UDP multicast is not yet fully supported.c                 C  s   |   | jj | j¡tj¡S )zŠ
        Test that an attempt to join an address which is not a multicast
        address fails with L{error.MulticastJoinError}.
        N)r•   rx   r;   rö   rí   r   r   r&   r(   r(   r)   Útest_joinFailureã  s   
ÿzMulticastTests.test_joinFailurec                   s|   t ƒ ‰t dˆˆj¡‰ˆjj ¡ ‰ ˆjj ˆj¡}‡ ‡‡fdd„}| 	|¡ ‡fdd„}| 	|¡ ‡fdd„}| 	|¡ |S )	zn
        Test that a multicast group can be joined and messages sent to and
        received from it.
        r   c                   s(   t ƒ  }ˆj_ˆj dˆjˆ jf¡ |S )Nó   hello world)r   re   r:   r;   r€   rê   rm   )r^   r2   )r?   Úcr'   r(   r)   ÚcbJoinedü  s   z/MulticastTests.test_multicast.<locals>.cbJoinedc                   s   ˆ   ˆ jjd d d¡ d S )Nr   rü   )r[   re   r%   rg   r&   r(   r)   ÚcbPacket  s   z/MulticastTests.test_multicast.<locals>.cbPacketc                   s    t ˆjƒ}| ‡ fdd„¡ |S )Nc                   rª   r$   r(   r«   ©Úpassthroughr(   r)   r®   
  r¯   z@MulticastTests.test_multicast.<locals>.cleanup.<locals>.<lambda>)r   r]   rd   )r  r­   )r`   r   r)   r      s   
z.MulticastTests.test_multicast.<locals>.cleanupN)
r9   r   rð   rY   re   r;   rZ   rö   rê   rd   )r'   Zjoinedrþ   rÿ   r    r(   )r?   rý   r`   r'   r)   Útest_multicastñ  s   


zMulticastTests.test_multicastzjon non-linux platforms it appears multiple processes can listen, but not multiple sockets in same process?c           
      Ã  s  �t ƒ }ttƒ}|jd|d| jd�}| ¡ }t|ttfƒsJ ‚|j	}t ƒ }|j||d| jd�}t
| jj | j¡| | j¡| | j¡gƒI dH  tƒ  }|_tƒ  }	|_|j d| j|f¡ t
||	gƒI dH  |  |jd d d¡ |  |jd d d¡ t
t|jƒt|jƒgƒI dH  dS )z¢
        Test that multiple sockets can listen on the same multicast port and
        that they both receive multicast messages directed to that address.
        r   T)ZlistenMultiplerY   Nrü   )r9   r   r   rð   rY   rZ   Ú
isinstancer   r   rm   r   re   r;   rö   rê   r   r:   r€   r[   r%   r   r]   )
r'   ZfirstClientZmreactorZ	firstPortZfpAddrZportnoZsecondClientZ
secondPortZd1Zd2r(   r(   r)   Útest_multiListen  sB   €
ÿü

ý
ÿ	þÿzMulticastTests.test_multiListenN)rÜ   rñ   )r6   r7   r8   rQ   rY   rA   rè   ré   rê   rë   rì   rí   rï   rË   rÌ   rõ   r÷   rø   rù   rú   r   r   ÚplatformZ	isWindowsZisVistarû   r  r  r(   r(   r(   r)   ræ   e  s8   
 






þ

 þræ   úff03::1ú::z/The local stack does not enable IPv6 multicast.c                   @  s‚   e Zd ZU dZdZded< dZded< dZded< ed	d
„ e	ƒ D ƒƒZ
ded< e
Zded< dZded< dZded< dZded< dS )ÚMulticastTestsIPv6zC
    Tests for sending and receiving IPv6 multicast datagrams.
    r  rÚ   rY   rî   ré   r  rê   c                 c  s&   � | ]}|d    d¡r|d V  qdS )r-   Úlor   N)Ú
startswith)Ú.0Zidxnmr(   r(   r)   Ú	<genexpr>^  s   €$ zMulticastTestsIPv6.<genexpr>rç   rë   rì   rí   r   rè   rW   rï   N)r6   r7   r8   rQ   rY   rA   ré   rê   Únextr   rë   rì   rí   rè   rï   r(   r(   r(   r)   r  H  s   
 ÿr  )rÕ   r
   rÖ   r×   rØ   r×   rÙ   rÚ   rÛ   rÚ   rÜ   rÝ   )=rQ   Ú
__future__r   rÇ   r   r   r   r   r   r   r   r	   r
   r   r   Zunittestr   Ztwisted.internetr   r   r   r   r   r   Ztwisted.internet.addressr   r   Ztwisted.internet.deferr   r   r   Ztwisted.internet.errorr   Ztwisted.internet.interfacesr   r   r   Ztwisted.internet.taskr   Ztwisted.pythonr   Ztwisted.test.testutilsr    Ztwisted.trial.unittestr!   r"   r#   ÚDatagramProtocolr9   ZConnectedDatagramProtocolrC   rN   r½   rP   rR   rÓ   rV   rÊ   rã   ræ   r  r(   r(   r(   r)   Ú<module>   sh   4    
6þ
ÿü Zþ
ÿü