
    h@                        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 d dlmZ  G d dej8                        Z G d dej8                        Z G d dej8                        Z G d dej8                        Z  G d dej8                        Z!y# e$ r	 d dlmZ Y }w xY w)    )MockN)wildcards2patterns)WebSocketServerFactory)WebSocketServerProtocol)WebSocketClientProtocol)TransportDetailsConnectingRequest)Failure)ConnectionDoneConnectionAbortedConnectionLost)unittest)StringTransport)FakeTransportc                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	ExceptionHandlingTestsz^
    Tests that we format various exception variations properly during
    connectionLost
    c                     t               | _        t               | _        | j                  | j                  _        t	               | j                  _        y N)r   factoryr   protor   logselfs    b/var/www/Befach/backend/env/lib/python3.12/site-packages/autobahn/twisted/test/test_tx_protocol.pysetUpzExceptionHandlingTests.setUp7   s3    -/,.
!\\



    c                     | j                   j                  | j                   j                  | j                   j                  | j                   j                  fD ]  }||j                           y r   r   autoPingPendingCallautoPingTimeoutCallopenHandshakeTimeoutCallcloseHandshakeTimeoutCallcancelr   calls     r   tearDownzExceptionHandlingTests.tearDown=   S    

..

..

33

44	
D 
r   c           	      R   | j                   j                          | j                   j                  t        t	                            dj                  | j                   j                  j                  D cg c]  }t        |d          c}      }| j                  d|v        y c c}w )N    zclosed cleanly)
r   _connectionMadeconnectionLostr   r   joinr   
mock_callsstr
assertTruer   xmessagess      r   test_connection_donez+ExceptionHandlingTests.test_connection_doneG   ss    

""$

!!'.*:";<88

0I0IJ1S1YJK(H45 K   4B$c           	      R   | j                   j                          | j                   j                  t        t	                            dj                  | j                   j                  j                  D cg c]  }t        |d          c}      }| j                  d|v        y c c}w )Nr*   r+   z	 aborted )
r   r,   r-   r   r   r.   r   r/   r0   r1   r2   s      r   test_connection_abortedz.ExceptionHandlingTests.test_connection_abortedP   ss    

""$

!!'*;*=">?88

0I0IJ1S1YJKx/0 Kr6   c           	      R   | j                   j                          | j                   j                  t        t	                            dj                  | j                   j                  j                  D cg c]  }t        |d          c}      }| j                  d|v        y c c}w )Nr*   r+   
 was lost 
r   r,   r-   r   r   r.   r   r/   r0   r1   r2   s      r   test_connection_lostz+ExceptionHandlingTests.test_connection_lostY   sr    

""$

!!'.*:";<88

0I0IJ1S1YJK01 Kr6   c           
         | j                   j                          | j                   j                  t        t	        d                   dj                  | j                   j                  j                  D cg c]  }t        |d         t        |d         z   ! c}      }| j                  d|v        | j                  d|v        y c c}w )N	greetingsr*   r+      r:   r;   r2   s      r   test_connection_lost_argz/ExceptionHandlingTests.test_connection_lost_argb   s    

""$

!!'.*E"FG88DJJNN<U<UVqS1YQqT2VW01x/0 Ws   5$CN)
__name__
__module____qualname____doc__r   r'   r5   r8   r<   r@    r   r   r   r   1   s%    
 6121r   r   c                       e Zd ZdZd Zy)Hixie76RejectionTestsz@
    Hixie-76 should not be accepted by an Autobahn server.
    c                 ,   t               }t               }t               }||_        ||_        d}d|_        |j                          ||_        |j                          | j                  d|j                         | j                  d|j                         y)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openHandshakeTimeoutr,   dataprocessHandshakeassertIn_written)r   tfphttp_requests        r   test_handshake_failsz*Hixie76RejectionTests.test_handshake_failsq   s}     O"$#%	 U!"		oqzz27Dr   N)rA   rB   rC   rD   rS   rE   r   r   rG   rG   m   s    Er   rG   c                   X    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y)WebSocketOriginMatchingzD
    Test that we match Origin: headers properly, when asked to
    c                 J   t               | _        | j                  j                  ddg       t               | _        t               | j                  _        | j                  | j                  _        t               | j                  _        | j                  j                          y )Nz127.0.0.1:*z*.example.com:*allowedOrigins
r   r   setProtocolOptionsr   r   r   rI   r   failHandshaker,   r   s    r   r   zWebSocketOriginMatching.setUp   su    -/'')+<= 	( 	
 -.
.0

!\\

#'6

 

""$r   c                     | j                   j                  | j                   j                  | j                   j                  | j                   j                  fD ]  }||j                           y r   r   r%   s     r   r'   z WebSocketOriginMatching.tearDown   r(   r   c                 T   dj                  g d      | j                  _        | j                  j                          | j	                  | j                  j
                  j                  d       | j                  j
                  j                  d   d   d   }| j	                  d|v        y )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: websocketr^   Handshake should have failedr   r+   not allowed)r.   r   rK   consumeDatar1   r[   calledr/   r   args     r   test_match_full_originz.WebSocketOriginMatching.test_match_full_origin   s    !,, 
(
 


 	

 

00779WXjj&&11!4Q7:,-r   c                 D   | j                   j                  dg       | j                   j                  | j                  _        | j                   j                  | j                  _        d| j                   _        dj                  g d      | j                  _        | j                  j                          | j                  | j                  j                  j                  d       | j                  j                  j                  d   d   d   }| j                  d	|v        y )
Nzhttp://*.example.com:*rW   Fr^   	r_   r`   ra   s   Origin: https://www.example.comrb   rc   rd   re   r^   rf   r   r+   rg   )r   rZ   allowedOriginsPatternsr   rX   isSecurer.   rK   rh   r1   r[   ri   r/   rj   s     r   test_match_wrong_scheme_originz6WebSocketOriginMatching.test_match_wrong_scheme_origin   s     	''45 	( 	
 -1LL,O,O

)$(LL$?$?

! !&!,, 
(
 


 	

 

00779WXjj&&11!4Q7:,-r   c                     d| j                   _        d| j                   _        dj                  g d      | j                  _        | j                  j                          | j                  | j                  j                  j                  d       y )NT  r^   rn   Handshake should have succeeded)
r   rp   portr.   r   rK   rh   assertFalser[   ri   r   s    r   test_match_origin_secure_schemez7WebSocketOriginMatching.test_match_origin_secure_scheme   sf     $!,, 
(
 


 	

 1188:[\r   c                 Z   | j                   j                  dg       d| j                   _        d| j                   _        dj	                  g d      | j
                  _        | j
                  j                          | j                  | j
                  j                  j                  d       y)	z1
        Test the examples from the docs
        z*://*.example.com:*rW   Trs   r^   )	r_   r`   ra      Origin: http://www.example.comrb   rc   rd   re   r^   rt   N)r   rZ   rp   ru   r.   r   rK   rh   rv   r[   ri   r   s    r   'test_match_origin_documentation_examplez?WebSocketOriginMatching.test_match_origin_documentation_example   s     	''12 	( 	
 !%!,, 
(
 


 	

 1188:[\r   c           	      |    ddl m}m} t        dg      }dD ]#  }| j	                   | ||      dd|      |       % y)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   r1   r   r}   r~   policyurls        r   test_match_origin_examplesz2WebSocketOriginMatching.test_match_origin_examples   sC    
 	P#%5$67+C OOON3,?VTVYZ+r   c           	      z    ddl m}m} t        dg      }dD ]"  }| j	                   | ||      dd|             $ y)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   rv   r   s        r   "test_match_origin_counter_examplesz:WebSocketOriginMatching.test_match_origin_counter_examples	  s@    
 	P#]O4/C _^C-@&"fUV/r   c                     ddl m}m} t        dg      }| j	                   | |d      dd|             | j                   | |d      dd|             | j                   | |d	      dd|             y )
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   r1   rv   )r   r}   r~   r   s       r   test_match_origin_edgez.WebSocketOriginMatching.test_match_origin_edge  s~    O#%=$>?N+BCVRQWX	
 	N+BCVRQWX	
 	N+CDfbRXY	
r   c                 p    ddl m} | j                   |d      d       | j                   |d      d       y )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%  s=    > 	/0'	

 	/0'	
r   c                 @    ddl m} | j                  d |d             y )Nr   r   nullzfile:///etc/passwdr   r   s     r   test_origin_filez(WebSocketOriginMatching.test_origin_file3  s    >0D!EFr   c                 D   ddl m}m} | j                  d |d             | j	                   | |d      ddg              | j	                   | |d      ddg              | j	                   | |d      ddg              | j	                   | |d      d dg              y )Nr   r|   r   r   r   https )r   r}   r~   r   rv   )r   r}   r~   s      r   test_origin_nullz(WebSocketOriginMatching.test_origin_null7  s    O!78N62FBC	
 	N62GRD	
 	N62BB?	
 	N62D"bA	
r   N)rA   rB   rC   rD   r   r'   rl   rq   rw   rz   r   r   r   r   r   r   rE   r   r   rU   rU      sH    	%.$.8]$]0["W

G
r   rU   c                   "    e Zd ZdZd Zd Zd Zy)WebSocketXForwardedForzR
    Test that (only) a trusted X-Forwarded-For can replace the peer address.
    c                 F   t               | _        | j                  j                  d       t               | _        t               | j                  _        | j                  | j                  _        t               | j                  _        | j                  j                          y )Nr?   )trustXForwardedForrY   r   s    r   r   zWebSocketXForwardedFor.setUpM  so    -/''  	( 	
 -.
.0

!\\

#'6

 

""$r   c                     | j                   j                  | j                   j                  | j                   j                  | j                   j                  fD ]  }||j                           y r   r   r%   s     r   r'   zWebSocketXForwardedFor.tearDownX  r(   r   c                     dj                  g d      | j                  _        | j                  j                          | j	                  | j                  j
                  dd       y )Nr^   )
r_   r`   ry   ra   rb   rc   rd   re   s0   X-Forwarded-For: 1.2.3.4, 2.3.4.5, 111.222.33.44r^   z2.3.4.5zQThe second address in X-Forwarded-For should have been picked as the peer address)r.   r   rK   rh   assertEqualspeerr   s    r   test_trusted_addressesz-WebSocketXForwardedFor.test_trusted_addressesb  sL    !,, (
 

 	

 JJOOY_	ar   N)rA   rB   rC   rD   r   r'   r   rE   r   r   r   r   H  s    	%ar   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)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               |_        |j                         }| j	                  |       | j                  t        |j                  j                        dkD         d}t        t        |j                  j                              D ])  }t        |j                  j                  |         v s(d}+ | j                  |d       y )Nz	bad stuffc                   .    e Zd ZdZdZ e       Z fdZy)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   )rA   rB   rC   statewasCleanr   r   r   )r   s   r   	TestProtor     s    EH&C.r   r   r   FTz!MAGIC_STR not found when expected)r   r   rI   startHandshakesuccessResultOfr1   lenr   r/   ranger0   )r   r   r   dmagic_foundir   s         @r   test_on_connecting_client_failsz1OnConnectingTests.test_on_connecting_client_fails~  s    		./ 	. '/  "QEII001A56s599//01AC		 4 4Q 788" 2 	%HIr   c                 v    G d dt               } |       }t               |_        t               |_        |j                          |j                         }| j                  |      }| j                  d|j                         | j                  d|j                         | j                  d|j                         y )Nc                   4    e Zd ZdZdZg ZdZdZ e       Z	d Z
y)FOnConnectingTests.test_on_connecting_client_success.<locals>.TestProtoNT      c                     t        ddd      S )Nr   rs   /wshostru   resourcer	   )r   r   s     r   r   zSOnConnectingTests.test_on_connecting_client_success.<locals>.TestProto.onConnecting  s    (&" r   )rA   rB   rC   r   r   perMessageCompressionOffersversionrJ   r   r   r   rE   r   r   r   r     s)    EH*,'G#$ &Cr   r   r   rs   r   )r   r   rI   r   r   r,   r   r   r   r   ru   r   )r   r   r   r   reqs        r   !test_on_connecting_client_successz3OnConnectingTests.test_on_connecting_client_success  s    	/ 	 '/  """1%1chh'-r   c                 T    t        t         j                  ddi       }t        |       y )Nr   F)channel_typer   	is_secure
channel_id)r   CHANNEL_TYPE_FUNCTIONr0   )r   detailss     r   test_str_transportz$OnConnectingTests.test_str_transport  s'    ")??	
 	Gr   c                 6    t        ddd      }t        |       y )Nr   1234r   r   )r
   r0   )r   r   s     r   test_str_connectingz%OnConnectingTests.test_str_connecting  s    ]%PCr   N)rA   rB   rC   rD   r   r   r   r   rE   r   r   r   r   v  s    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   rG   rU   r   r   rE   r   r   <module>r      s   6       , = > > 0 6 *  ";8 ,91X.. 91xEH-- E2
h// 
D+aX.. +a\I)) IU
  ;:;s   C CC