
    af@                        d dl mZ d dlZ ej                     d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dl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 n# e$ r	 d dlmZ Y nw xY wd dlmZ  G d dej                  Z G d dej                  Z G d dej                  Z G d dej                  Z  G d dej                  Z!dS )    )MockN)wildcards2patterns)WebSocketServerFactory)WebSocketServerProtocol)WebSocketClientProtocol)TransportDetailsConnectingRequest)Failure)ConnectionDoneConnectionAbortedConnectionLost)unittest)StringTransport)FakeTransportc                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	ExceptionHandlingTestsz^
    Tests that we format various exception variations properly during
    connectionLost
    c                     t                      | _        t                      | _        | j        | j        _        t	                      | j        _        d S N)r   factoryr   protor   logselfs    X/var/www/html/env/lib/python3.11/site-packages/autobahn/twisted/test/test_tx_protocol.pysetUpzExceptionHandlingTests.setUp7   s9    -//,..
!\

    c                     | j         j        | j         j        | j         j        | j         j        fD ]}||                                 d S r   r   autoPingPendingCallautoPingTimeoutCallopenHandshakeTimeoutCallcloseHandshakeTimeoutCallcancelr   calls     r   tearDownzExceptionHandlingTests.tearDown=   S    
.
.
3
4	
 	 	D 	 	r   c                 (   | j                                          | j                             t          t	                                           d                    d | j         j        j        D                       }|                     d|v            d S )N c                 8    g | ]}t          |d                    S    str.0xs     r   
<listcomp>z?ExceptionHandlingTests.test_connection_done.<locals>.<listcomp>M   "    JJJ1S1YYJJJr   zclosed cleanly)	r   _connectionMadeconnectionLostr   r   joinr   
mock_calls
assertTruer   messagess     r   test_connection_donez+ExceptionHandlingTests.test_connection_doneG   s    
""$$$
!!'.*:*:";";<<<88JJ
0IJJJKK(H455555r   c                 (   | j                                          | j                             t          t	                                           d                    d | j         j        j        D                       }|                     d|v            d S )Nr*   c                 8    g | ]}t          |d                    S r,   r.   r0   s     r   r3   zBExceptionHandlingTests.test_connection_aborted.<locals>.<listcomp>V   r4   r   z	 aborted )	r   r5   r6   r   r   r7   r   r8   r9   r:   s     r   test_connection_abortedz.ExceptionHandlingTests.test_connection_abortedP   s    
""$$$
!!'*;*=*=">">???88JJ
0IJJJKKx/00000r   c                 (   | j                                          | j                             t          t	                                           d                    d | j         j        j        D                       }|                     d|v            d S )Nr*   c                 8    g | ]}t          |d                    S r,   r.   r0   s     r   r3   z?ExceptionHandlingTests.test_connection_lost.<locals>.<listcomp>_   r4   r   
 was lost 	r   r5   r6   r   r   r7   r   r8   r9   r:   s     r   test_connection_lostz+ExceptionHandlingTests.test_connection_lostY   s~    
""$$$
!!'.*:*:";";<<<88JJ
0IJJJKK011111r   c                 X   | j                                          | j                             t          t	          d                               d                    d | j         j        j        D                       }|                     d|v            |                     d|v            d S )N	greetingsr*   c                 d    g | ]-}t          |d                    t          |d                   z   .S )r-      r.   r0   s     r   r3   zCExceptionHandlingTests.test_connection_lost_arg.<locals>.<listcomp>h   s1    VVVqS1YYQqT2VVVr   rB   rC   r:   s     r   test_connection_lost_argz/ExceptionHandlingTests.test_connection_lost_argb   s    
""$$$
!!'.*E*E"F"FGGG88VVDJN<UVVVWW0111x/00000r   N)
__name__
__module____qualname____doc__r   r'   r<   r?   rD   rI    r   r   r   r   1   sx         
       6 6 61 1 12 2 21 1 1 1 1r   r   c                       e Zd ZdZd ZdS )Hixie76RejectionTestsz@
    Hixie-76 should not be accepted by an Autobahn server.
    c                 R   t                      }t                      }t                      }||_        ||_        d}d|_        |                                 ||_        |                                 | 	                    d|j
                   | 	                    d|j
                   dS )zO
        A handshake from a client only supporting Hixie-76 will fail.
        s   GET /demo HTTP/1.1
Host: example.com
Connection: Upgrade
Sec-WebSocket-Key2: 12998 5 Y3 1  .P00
Sec-WebSocket-Protocol: sample
Upgrade: WebSocket
Sec-WebSocket-Key1: 4 @1  46546xW%0l 1 5
Origin: http://example.com

^n:ds[4Ur   s   HTTP/1.1 400s   Hixie76 protocol not supportedN)r   r   r   r   	transportopenHandshakeTimeoutr5   dataprocessHandshakeassertIn_written)r   tfphttp_requests        r   test_handshake_failsz*Hixie76RejectionTests.test_handshake_failsq   s     OO"$$#%%	 U!"		oqz2227DDDDDr   N)rJ   rK   rL   rM   r\   rN   r   r   rP   rP   m   s2         E E E E Er   rP   c                   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S )WebSocketOriginMatchingzD
    Test that we match Origin: headers properly, when asked to
    c                 @   t                      | _        | j                            ddg           t                      | _        t                      | j        _        | j        | j        _        t                      | j        _        | j        	                                 d S )Nz127.0.0.1:*z*.example.com:*allowedOrigins
r   r   setProtocolOptionsr   r   r   rR   r   failHandshaker5   r   s    r   r   zWebSocketOriginMatching.setUp   s    -//'')+<= 	( 	
 	
 	
 -..
.00
!\
#'66
 
""$$$$$r   c                     | j         j        | j         j        | j         j        | j         j        fD ]}||                                 d S r   r   r%   s     r   r'   z WebSocketOriginMatching.tearDown   r(   r   c                 8   d                     g d          | j        _        | j                                         |                     | j        j        j        d           | j        j        j        d         d         d         }|                     d|v            d S )N   
)	   GET /ws HTTP/1.1   Host: www.example.com   Sec-WebSocket-Version: 13s,   Origin: http://www.example.com.malicious.com,   Sec-WebSocket-Extensions: permessage-deflate+   Sec-WebSocket-Key: tXAxWFUqnhi86Ajj7dRY5g==   Connection: keep-alive, Upgrade   Upgrade: websocketrg   Handshake should have failedr   r-   not allowed)r7   r   rT   consumeDatar9   rd   calledr8   r   args     r   test_match_full_originz.WebSocketOriginMatching.test_match_full_origin   s    !,, 
(
 
(
 
(
 
 

 	
   
079WXXXj&1!4Q7:,-----r   c                    | j                             dg           | j         j        | j        _        | j         j        | j        _        d| j         _        d                    g d          | j        _        | j                                         | 	                    | j        j
        j        d           | j        j
        j        d         d         d         }| 	                    d	|v            d S )
Nzhttp://*.example.com:*r`   Frg   	rh   ri   rj   s   Origin: https://www.example.comrk   rl   rm   rn   rg   ro   r   r-   rp   )r   rc   allowedOriginsPatternsr   ra   isSecurer7   rT   rq   r9   rd   rr   r8   rs   s     r   test_match_wrong_scheme_originz6WebSocketOriginMatching.test_match_wrong_scheme_origin   s     	''45 	( 	
 	
 	
 -1L,O
)$(L$?
! !&!,, 
(
 
(
 
(
 
 

 	
   
079WXXXj&1!4Q7:,-----r   c                     d| j         _        d| j         _        d                    g d          | j        _        | j                                         |                     | j        j        j	        d           d S )NT  rg   rw   Handshake should have succeeded)
r   ry   portr7   r   rT   rq   assertFalserd   rr   r   s    r   test_match_origin_secure_schemez7WebSocketOriginMatching.test_match_origin_secure_scheme   sw     $!,, 
(
 
(
 
(
 
 

 	
   18:[\\\\\r   c                 ,   | j                             dg           d| j         _        d| j         _        d                    g d          | j        _        | j                                         |                     | j        j	        j
        d           dS )	z1
        Test the examples from the docs
        z*://*.example.com:*r`   Tr|   rg   )	rh   ri   rj      Origin: http://www.example.comrk   rl   rm   rn   rg   r}   N)r   rc   ry   r~   r7   r   rT   rq   r   rd   rr   r   s    r   'test_match_origin_documentation_examplez?WebSocketOriginMatching.test_match_origin_documentation_example   s     	''12 	( 	
 	
 	
 !%!,, 
(
 
(
 
(
 
 

 	
   18:[\\\\\r   c           	          ddl m}m} t          dg          }dD ]-}|                      | ||          dd|          |           .dS )z>
        All the example origins from RFC6454 (3.2.1)
        r   _is_same_origin_url_to_originz*example.com:*)zhttp://example.com/zhttp://example.com:80/zhttp://example.com/path/filezhttp://example.com/;semi=truez//example.com/zhttp://@example.comhttpP   N)autobahn.websocket.protocolr   r   r   r9   r   r   r   policyurls        r   test_match_origin_examplesz2WebSocketOriginMatching.test_match_origin_examples   s    
 	POOOOOOO#%5$677+ 	[ 	[C OOOONN3,?,?VTTVYZZZZ	[ 	[r   c           	          ddl m}m} t          dg          }dD ],}|                      | ||          dd|                     -dS )zI
        All the example 'not-same' origins from RFC6454 (3.2.1)
        r   r   example.com)zhttp://ietf.org/zhttp://example.org/zhttps://example.com/zhttp://example.com:8080/zhttp://www.example.com/r   r   N)r   r   r   r   r   r   s        r   "test_match_origin_counter_examplesz:WebSocketOriginMatching.test_match_origin_counter_examples	  s~    
 	POOOOOOO#]O44/ 	W 	WC __^^C-@-@&"fUUVVVV	W 	Wr   c                 2   ddl m}m} t          dg          }|                      | |d          dd|                     |                      | |d          dd|                     |                      | |d	          dd|                     d S )
Nr   r   zhttp://*example.com:80zhttp://example.com:80r   r   zhttp://example.com:81Q   zhttps://example.com:80)r   r   r   r   r9   r   )r   r   r   r   s       r   test_match_origin_edgez.WebSocketOriginMatching.test_match_origin_edge  s    OOOOOOOO#%=$>??ONN+BCCVRQWXX	
 	
 	
 	ONN+BCCVRQWXX	
 	
 	
 	ONN+CDDfbRXYY	
 	
 	
 	
 	
r   c                     ddl m} |                      |d          d           |                      |d          d           d S )Nr   r   zhttp://example.com)r   r   r   zhTTp://example.comr   r   assertEqualr   r   s     r   test_origin_from_urlz,WebSocketOriginMatching.test_origin_from_url%  st    >>>>>> 	N/00'	
 	
 	

 	N/00'	
 	
 	
 	
 	
r   c                 P    ddl m} |                     d |d                     d S )Nr   r   nullzfile:///etc/passwdr   r   s     r   test_origin_filez(WebSocketOriginMatching.test_origin_file3  s;    >>>>>>0D!E!EFFFFFr   c                    ddl m}m} |                     d |d                     |                      | |d          ddg                      |                      | |d          ddg                      |                      | |d          ddg                      |                      | |d          d dg                      d S )Nr   r   r   r   r   https )r   r   r   r   r   )r   r   r   s      r   test_origin_nullz(WebSocketOriginMatching.test_origin_null7  s   OOOOOOOO!7!7888ONN622FBCC	
 	
 	
 	ONN622GRDD	
 	
 	
 	ONN622BB??	
 	
 	
 	ONN622D"bAA	
 	
 	
 	
 	
r   N)rJ   rK   rL   rM   r   r'   ru   rz   r   r   r   r   r   r   r   r   rN   r   r   r^   r^      s         	% 	% 	%  . . .$. . .8] ] ]$] ] ]0[ [ ["W W W
 
 

 
 
G G G
 
 
 
 
r   r^   c                   $    e Zd ZdZd Zd Zd ZdS )WebSocketXForwardedForzR
    Test that (only) a trusted X-Forwarded-For can replace the peer address.
    c                 <   t                      | _        | j                            d           t                      | _        t                      | j        _        | j        | j        _        t                      | j        _        | j        	                                 d S )NrH   )trustXForwardedForrb   r   s    r   r   zWebSocketXForwardedFor.setUpM  s    -//''  	( 	
 	
 	
 -..
.00
!\
#'66
 
""$$$$$r   c                     | j         j        | j         j        | j         j        | j         j        fD ]}||                                 d S r   r   r%   s     r   r'   zWebSocketXForwardedFor.tearDownX  r(   r   c                     d                     g d          | j        _        | j                                         |                     | j        j        dd           d S )Nrg   )
rh   ri   r   rj   rk   rl   rm   rn   s0   X-Forwarded-For: 1.2.3.4, 2.3.4.5, 111.222.33.44rg   z2.3.4.5zQThe second address in X-Forwarded-For should have been picked as the peer address)r7   r   rT   rq   assertEqualspeerr   s    r   test_trusted_addressesz-WebSocketXForwardedFor.test_trusted_addressesb  sq    !,, (
 (
 (
  
 	
   JOY_	a 	a 	a 	a 	ar   N)rJ   rK   rL   rM   r   r'   r   rN   r   r   r   r   H  sP         	% 	% 	%  a a a a ar   r   c                   *    e Zd ZdZd Zd Zd Zd ZdS )OnConnectingTestsz
    Tests related to onConnecting callback

    These tests are testing generic behavior, but are somewhat tied to
    'a framework' so we're just testing using Twisted-specifics here.
    c                    d G fddt                     } |            }t                      |_        |                                }|                     |           |                     t          |j        j                  dk               d}t          t          |j        j                            D ]%}t          |j        j        |                   v rd}&|                     |d           d S )Nz	bad stuffc                   6    e Zd ZdZdZ e            Z fdZdS )DOnConnectingTests.test_on_connecting_client_fails.<locals>.TestProtoNTc                 "    t                    r   )RuntimeError)r   transport_details	MAGIC_STRs     r   onConnectingzQOnConnectingTests.test_on_connecting_client_fails.<locals>.TestProto.onConnecting  s    "9---r   )rJ   rK   rL   statewasCleanr   r   r   )r   s   r   	TestProtor     sC        EH$&&C. . . . . . .r   r   r   FTz!MAGIC_STR not found when expected)r   r   rR   startHandshakesuccessResultOfr9   lenr   r8   ranger/   )r   r   r   dmagic_foundir   s         @r   test_on_connecting_client_failsz1OnConnectingTests.test_on_connecting_client_fails~  s   		. 	. 	. 	. 	. 	. 	./ 	. 	. 	. 	'//  ""QEI011A5666s59/0011 	# 	#AC	 4Q 78888"%HIIIIIr   c                     G d dt                     } |            }t                      |_        t                      |_        |                                 |                                }|                     |          }|                     d|j	                   |                     d|j
                   |                     d|j                   d S )Nc                   <    e Zd ZdZdZg ZdZdZ e            Z	d Z
dS )FOnConnectingTests.test_on_connecting_client_success.<locals>.TestProtoNT      c                 &    t          ddd          S )Nr   r|   /wshostr~   resourcer	   )r   r   s     r   r   zSOnConnectingTests.test_on_connecting_client_success.<locals>.TestProto.onConnecting  s"    (&"   r   )rJ   rK   rL   r   r   perMessageCompressionOffersversionrS   r   r   r   rN   r   r   r   r     sI        EH*,'G#$ $&&C    r   r   r   r|   r   )r   r   rR   r   r   r5   r   r   r   r   r~   r   )r   r   r   r   reqs        r   !test_on_connecting_client_successz3OnConnectingTests.test_on_connecting_client_success  s    	 	 	 	 	/ 	 	 	 	'//  """"1%%111ch'''-----r   c                 ^    t          t           j        ddi           }t          |           d S )Nr   F)channel_typer   	is_secure
channel_id)r   CHANNEL_TYPE_FUNCTIONr/   )r   detailss     r   test_str_transportz$OnConnectingTests.test_str_transport  s6    ")?	
 
 
 	Gr   c                 H    t          ddd          }t          |           d S )Nr   1234r   r   )r
   r/   )r   r   s     r   test_str_connectingz%OnConnectingTests.test_str_connecting  s%    ]%PPPCr   N)rJ   rK   rL   rM   r   r   r   r   rN   r   r   r   r   v  s]         J J J0. . .8      r   r   )"unittest.mockr   txaiouse_twistedautobahn.utilr   autobahn.twisted.websocketr   r   r   autobahn.wamp.typesr   autobahn.websocket.typesr
   twisted.python.failurer   twisted.internet.errorr   r   r   twisted.trialr   twisted.internet.testingr   ImportErrortwisted.test.proto_helpersautobahn.testutilr   TestCaser   rP   r^   r   r   rN   r   r   <module>r      sd  6             , , , , , , = = = = = = > > > > > > > > > > > > 0 0 0 0 0 0 6 6 6 6 6 6 * * * * * *          " " " " " ";8888888 ; ; ;::::::::; + + + + + +91 91 91 91 91X. 91 91 91xE E E E EH- E E E2
 
 
 
 
h/ 
 
 
D+a +a +a +a +aX. +a +a +a\I I I I I) I I I I Is   A A*)A*