o
    >h@                     @   s*  d dl mZ d dlZe  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 zd dlmZ W n ey_   d dlmZ Y nw d dlmZ G dd dejZG dd dejZG dd dejZG dd dejZ G dd dejZ!dS )    )MockN)wildcards2patterns)WebSocketServerFactory)WebSocketServerProtocol)WebSocketClientProtocol)TransportDetailsConnectingRequest)Failure)ConnectionDoneConnectionAbortedConnectionLost)unittest)StringTransport)FakeTransportc                   @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )ExceptionHandlingTestsz^
    Tests that we format various exception variations properly during
    connectionLost
    c                 C   s(   t  | _t | _| j| j_t | j_d S N)r   factoryr   protor   logself r   /var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/autobahn/twisted/test/test_tx_protocol.pysetUp7   s   
zExceptionHandlingTests.setUpc                 C   6   | j j| j j| j j| j jfD ]
}|d ur|  qd S r   r   autoPingPendingCallautoPingTimeoutCallopenHandshakeTimeoutCallcloseHandshakeTimeoutCallcancelr   callr   r   r   tearDown=      zExceptionHandlingTests.tearDownc                 C   H   | j   | j tt  ddd | j jjD }| d|v  d S )N c                 S      g | ]}t |d  qS    str.0xr   r   r   
<listcomp>M       z?ExceptionHandlingTests.test_connection_done.<locals>.<listcomp>zclosed cleanly)	r   _connectionMadeconnectionLostr
   r   joinr   
mock_calls
assertTruer   messagesr   r   r   test_connection_doneG      
z+ExceptionHandlingTests.test_connection_donec                 C   r&   )Nr'   c                 S   r(   r)   r+   r-   r   r   r   r0   V   r1   zBExceptionHandlingTests.test_connection_aborted.<locals>.<listcomp>z	 aborted )	r   r2   r3   r
   r   r4   r   r5   r6   r7   r   r   r   test_connection_abortedP   r:   z.ExceptionHandlingTests.test_connection_abortedc                 C   r&   )Nr'   c                 S   r(   r)   r+   r-   r   r   r   r0   _   r1   z?ExceptionHandlingTests.test_connection_lost.<locals>.<listcomp>
 was lost 	r   r2   r3   r
   r   r4   r   r5   r6   r7   r   r   r   test_connection_lostY   r:   z+ExceptionHandlingTests.test_connection_lostc                 C   sX   | j   | j ttd ddd | j jjD }| d|v  | d|v  d S )N	greetingsr'   c                 S   s$   g | ]}t |d  t |d  qS )r*      r+   r-   r   r   r   r0   h   s   $ zCExceptionHandlingTests.test_connection_lost_arg.<locals>.<listcomp>r<   r=   r7   r   r   r   test_connection_lost_argb   s
   
z/ExceptionHandlingTests.test_connection_lost_argN)
__name__
__module____qualname____doc__r   r$   r9   r;   r>   rA   r   r   r   r   r   1   s    
			r   c                   @   s   e Zd ZdZdd ZdS )Hixie76RejectionTestsz@
    Hixie-76 should not be accepted by an Autobahn server.
    c                 C   s^   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openHandshakeTimeoutr2   dataprocessHandshakeassertIn_written)r   tfphttp_requestr   r   r   test_handshake_failsq   s   z*Hixie76RejectionTests.test_handshake_failsN)rB   rC   rD   rE   rQ   r   r   r   r   rF   m   s    rF   c                   @   sp   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )WebSocketOriginMatchingzD
    Test that we match Origin: headers properly, when asked to
    c                 C   sN   t  | _| jjddgd t | _t | j_| j| j_t | j_| j	  d S )Nz127.0.0.1:*z*.example.com:*allowedOrigins
r   r   setProtocolOptionsr   r   r   rG   r   failHandshaker2   r   r   r   r   r      s   


zWebSocketOriginMatching.setUpc                 C   r   r   r   r"   r   r   r   r$      r%   z WebSocketOriginMatching.tearDownc                 C   sV   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: websocketrX   Handshake should have failedr   r*   not allowed)r4   r   rI   consumeDatar6   rW   calledr5   r   argr   r   r   test_match_full_origin   s
   
z.WebSocketOriginMatching.test_match_full_originc                 C   s   | j jdgd | 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:*rS   FrX   	rY   rZ   r[   s   Origin: https://www.example.comr\   r]   r^   r_   rX   r`   r   r*   ra   )r   rV   allowedOriginsPatternsr   rT   isSecurer4   rI   rb   r6   rW   rc   r5   rd   r   r   r   test_match_wrong_scheme_origin   s   
z6WebSocketOriginMatching.test_match_wrong_scheme_originc                 C   sB   d| j _d| j _dg d| j_| j  | | jjj	d d S )NT  rX   rg   Handshake should have succeeded)
r   ri   portr4   r   rI   rb   assertFalserW   rc   r   r   r   r   test_match_origin_secure_scheme   s
   
z7WebSocketOriginMatching.test_match_origin_secure_schemec                 C   sR   | j jdgd 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:*rS   Trk   rX   )	rY   rZ   r[      Origin: http://www.example.comr\   r]   r^   r_   rX   rl   N)r   rV   ri   rm   r4   r   rI   rb   rn   rW   rc   r   r   r   r   'test_match_origin_documentation_example   s   
z?WebSocketOriginMatching.test_match_origin_documentation_examplec                 C   sB   ddl m}m} tdg}dD ]}| |||dd|| q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.protocolrs   rt   r   r6   r   rs   rt   policyurlr   r   r   test_match_origin_examples   s
   
z2WebSocketOriginMatching.test_match_origin_examplesc                 C   s@   ddl m}m} tdg}dD ]}| |||dd| qdS )zI
        All the example 'not-same' origins from RFC6454 (3.2.1)
        r   rr   example.com)zhttp://ietf.org/zhttp://example.org/zhttps://example.com/zhttp://example.com:8080/zhttp://www.example.com/ru   rv   N)rw   rs   rt   r   rn   rx   r   r   r   "test_match_origin_counter_examples	  s
   
z:WebSocketOriginMatching.test_match_origin_counter_examplesc                 C   sf   ddl m}m} tdg}| ||ddd| | ||ddd| | ||d	dd| d S )
Nr   rr   zhttp://*example.com:80zhttp://example.com:80ru   rv   zhttp://example.com:81Q   zhttps://example.com:80)rw   rs   rt   r   r6   rn   )r   rs   rt   ry   r   r   r   test_match_origin_edge  s   
z.WebSocketOriginMatching.test_match_origin_edgec                 C   s0   ddl m} | |dd | |dd d S )Nr   rt   zhttp://example.com)ru   r|   rv   zhTTp://example.comrw   rt   assertEqualr   rt   r   r   r   test_origin_from_url%  s   z,WebSocketOriginMatching.test_origin_from_urlc                 C   s    ddl m} | d|d d S )Nr   r   nullzfile:///etc/passwdr   r   r   r   r   test_origin_file3  s   z(WebSocketOriginMatching.test_origin_filec                 C   s   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   rr   r   ru   rv   https )rw   rs   rt   r   rn   )r   rs   rt   r   r   r   test_origin_null7  s   z(WebSocketOriginMatching.test_origin_nullN)rB   rC   rD   rE   r   r$   rf   rj   ro   rq   r{   r}   r   r   r   r   r   r   r   r   rR      s    
rR   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	WebSocketXForwardedForzR
    Test that (only) a trusted X-Forwarded-For can replace the peer address.
    c                 C   sJ   t  | _| jjdd t | _t | j_| j| j_t | j_| j	  d S )Nr@   )trustXForwardedForrU   r   r   r   r   r   M  s   


zWebSocketXForwardedFor.setUpc                 C   r   r   r   r"   r   r   r   r$   X  r%   zWebSocketXForwardedFor.tearDownc                 C   s2   d g d| j_| j  | | jjdd d S )NrX   )
rY   rZ   rp   r[   r\   r]   r^   r_   s0   X-Forwarded-For: 1.2.3.4, 2.3.4.5, 111.222.33.44rX   z2.3.4.5zQThe second address in X-Forwarded-For should have been picked as the peer address)r4   r   rI   rb   assertEqualspeerr   r   r   r   test_trusted_addressesb  s   
z-WebSocketXForwardedFor.test_trusted_addressesN)rB   rC   rD   rE   r   r$   r   r   r   r   r   r   H  s
    
r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zd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                    s   d G  fdddt }| }t |_| }| | | t|jjdk d}t	t|jjD ]} t
|jj| v r?d}q1| |d d S )Nz	bad stuffc                       s&   e Zd ZdZdZe Z fddZdS )zDOnConnectingTests.test_on_connecting_client_fails.<locals>.TestProtoNTc                    s   t  r   )RuntimeErrorr   transport_details	MAGIC_STRr   r   onConnecting  s   zQOnConnectingTests.test_on_connecting_client_fails.<locals>.TestProto.onConnecting)rB   rC   rD   statewasCleanr   r   r   r   r   r   r   	TestProto  s
    r   r   FTz!MAGIC_STR not found when expected)r   r   rG   startHandshakesuccessResultOfr6   lenr   r5   ranger,   )r   r   r   dmagic_foundir   r   r   test_on_connecting_client_fails~  s   
z1OnConnectingTests.test_on_connecting_client_failsc                 C   sn   G dd dt }| }t |_t |_|  | }| |}| d|j	 | d|j
 | d|j d S )Nc                   @   s.   e Zd ZdZdZg ZdZdZe Z	dd Z
dS )zFOnConnectingTests.test_on_connecting_client_success.<locals>.TestProtoNT      c                 S   s   t ddddS )Nr|   rk   /wshostrm   resourcer   r   r   r   r   r     s
   zSOnConnectingTests.test_on_connecting_client_success.<locals>.TestProto.onConnecting)rB   rC   rD   r   r   perMessageCompressionOffersversionrH   r   r   r   r   r   r   r   r     s    r   r|   rk   r   )r   r   rG   r   r   r2   r   r   r   r   rm   r   )r   r   r   r   reqr   r   r   !test_on_connecting_client_success  s   
z3OnConnectingTests.test_on_connecting_client_successc                 C   s   t t jddi d}t| d S )Nr|   F)channel_typer   	is_secure
channel_id)r   CHANNEL_TYPE_FUNCTIONr,   )r   detailsr   r   r   test_str_transport  s   z$OnConnectingTests.test_str_transportc                 C   s   t dddd}t| d S )Nr|   1234r   r   )r	   r,   )r   r   r   r   r   test_str_connecting  s   z%OnConnectingTests.test_str_connectingN)rB   rC   rD   rE   r   r   r   r   r   r   r   r   r   v  s    
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   rF   rR   r   r   r   r   r   r   <module>   s0   < C.