
    Yf@                        d 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mZ ddlmZ ddlmZ 	 dd	lmZ d
Zn# e$ r dZdZY nw xY wdZdZdZdez  ZdZdez  Z G d dej                  Z G d d          Z G d deej                  Z G d deej                  Z G d deej                  Z G d dej                  Z  G d d ej                  Z!dS )!z4
Tests for L{twisted.words.protocols.jabber.client}
    )sha1)skipIf)defer)unittest)clienterrorjid	xmlstream)SASLInitiatingInitializer)utility)ssl)F N)TzSSL not availablez//iq[@type="get"]/query[@xmlns="jabber:iq:auth"]z//iq[@type="set"]/query[@xmlns="jabber:iq:auth"]z urn:ietf:params:xml:ns:xmpp-bindz"/iq[@type="set"]/bind[@xmlns="%s"]z#urn:ietf:params:xml:ns:xmpp-sessionz%/iq[@type="set"]/session[@xmlns="%s"]c                        e Zd Zd Zd Zd ZdS )CheckVersionInitializerTestsc                     t          j                    }t          j        |          }t          j        |          | _        d S N)r
   Authenticator	XmlStreamr   CheckVersionInitializerinit)selfaxss      V/var/www/html/env/lib/python3.11/site-packages/twisted/words/test/test_jabberclient.pysetUpz"CheckVersionInitializerTests.setUp#   s6    #%% ##2266			    c                 Z    d| j         j        _        | j                                          dS )z3
        Test supported version number 1.0
        )   r   N)r   r
   version
initializer   s    r   testSupportedz*CheckVersionInitializerTests.testSupported(   s+     '-	#	r   c                     d| j         j        _        |                     t          j        | j         j                  }|                     d|j                   dS )zK
        Test unsupported version number 0.0, and check exception.
        )r   r   zunsupported-versionN)	r   r
   r   assertRaisesr   StreamErrorr    assertEqual	condition)r   excs     r   testNotSupportedz-CheckVersionInitializerTests.testNotSupported/   sM     '-	# 1493GHH.>>>>>r   N)__name__
__module____qualname__r   r"   r)    r   r   r   r   "   sA        7 7 7
  ? ? ? ? ?r   r   c                       e Zd ZdZd Zd ZdS )InitiatingInitializerHarnessa  
    Testing harness for interacting with XML stream initializers.

    This sets up an L{utility.XmlPipe} to create a communication channel between
    the initializer and the stubbed receiving entity. It features a sink and
    source side that both act similarly to a real L{xmlstream.XmlStream}. The
    sink is augmented with an authenticator to which initializers can be added.

    The harness also provides some utility methods to work with event observers
    and deferreds.
    c                     g | _         t          j                    | _        | j        j        | _        t          j        d          | _        | j        | j        _        d S )Nzexample.org)outputr   XmlPipepipesinkr
   ConnectAuthenticatorauthenticatorr!   s    r   r   z"InitiatingInitializerHarness.setUpE   sI    O%%	&;MJJ'+'9$$$r   c                     t          j                    }|                    |           | j        j                            ||j                   |S )a  
        Observe an output event, returning a deferred.

        The returned deferred will be fired when the given event has been
        observed on the source end of the L{XmlPipe} tied to the protocol
        under test. The handler is added as the first callback.

        @param event: The event to be observed. See
            L{utility.EventDispatcher.addOnetimeObserver}.
        @param handler: The handler to be called with the observed event object.
        @rtype: L{defer.Deferred}.
        )r   DeferredaddCallbackr3   sourceaddOnetimeObservercallback)r   eventhandlerds       r   waitForz$InitiatingInitializerHarness.waitForL   sE     N	g	++E1:>>>r   N)r*   r+   r,   __doc__r   r@   r-   r   r   r/   r/   8   s<        
 
: : :    r   r/   c                   :     e Zd ZdZ fdZd Zd Zd Zd Z xZ	S )IQAuthInitializerTestsz0
    Tests for L{client.IQAuthInitializer}.
    c                     t                                                       t          j        | j                  | _        t          j        d          | j        _        d| j        _	        d S )Nuser@example.com/resourcesecret)
superr   r   IQAuthInitializerr
   r   r	   JIDr6   passwordr   	__class__s    r   r   zIQAuthInitializerTests.setUpd   sO    ,T^<<	!$)D!E!E&.###r   c                       fd} fd                      t          |          } j                                        }t	          j        ||g          S )z
        Test plain-text authentication.

        Act as a server supporting plain-text authentication and expect the
        C{password} field to be filled with the password. Then act as if
        authentication succeeds.
        c                 l   t          j        | d          }|                    d           |j                            d           |j                            d           |j                            d                               t
                    }j        j                            |           |S z
            Called when the initializer sent a query for authentication methods.

            The response informs the client that plain-text authentication
            is supported.
            resultzjabber:iq:authqueryusernamerJ   resource	r
   
toResponse
addElementrR   r@   IQ_AUTH_SETr3   r:   sendiqresponser?   	onAuthSetr   s      r   	onAuthGetz7IQAuthInitializerTests.testPlainText.<locals>.onAuthGets        !+B99H ;<<<N%%j111N%%j111N%%j111 [)44A I!!(+++Hr   c                 ~                        dt          | j        j                                                  dt          | j        j                                                  dt          | j        j                             t          j        | d          }j        j	        
                    |           dS )
            Called when the initializer sent the authentication request.

            The server checks the credentials and responds with an empty result
            signalling success.
            userrF   rT   rP   N)r&   strrR   rS   rJ   rT   r
   rV   r3   r:   rY   r[   r\   r   s     r   r]   z7IQAuthInitializerTests.testPlainText.<locals>.onAuthSet   s     VS):%;%;<<<Xs28+<'='=>>>ZRX->)?)?@@@ !+B99HI!!(+++++r   )r@   IQ_AUTH_GETr   r    r   gatherResultsr   r^   d1d2r]   s   `   @r   testPlainTextz$IQAuthInitializerTests.testPlainTextj   sz    	 	 	 	 	 	.	, 	, 	, 	, 	,  \\+y11 Y!!##"B8,,,r   c                       fd} fdd j         _                             t          |          } j                                        }t          j        ||g          S )a"  
        Test digest authentication.

        Act as a server supporting digest authentication and expect the
        C{digest} field to be filled with a sha1 digest of the concatenated
        stream session identifier and password. Then act as if authentication
        succeeds.
        c                 l   t          j        | d          }|                    d           |j                            d           |j                            d           |j                            d                               t
                    }j        j                            |           |S )z
            Called when the initializer sent a query for authentication methods.

            The response informs the client that digest authentication is
            supported.
            rP   rQ   rS   digestrT   rU   rZ   s      r   r^   z4IQAuthInitializerTests.testDigest.<locals>.onAuthGet   s     !+B99H ;<<<N%%j111N%%h///N%%j111 [)44A I!!(+++Hr   c                                         dt          | j        j                                                  t	          d                                          t          | j        j                                                  dt          | j        j                             t          j	        | d          }j
        j                            |           dS )ra   rb   s   12345secretrT   rP   N)r&   rc   rR   rS   r   	hexdigestrm   rT   r
   rV   r3   r:   rY   rd   s     r   r]   z4IQAuthInitializerTests.testDigest.<locals>.onAuthSet   s     VS):%;%;<<<T.11;;==s28??S?STTTZRX->)?)?@@@ !+B99HI!!(+++++r   12345)r
   sidr@   re   r   r    r   rf   rg   s   `   @r   
testDigestz!IQAuthInitializerTests.testDigest   s    	 	 	 	 	 	.	, 	, 	, 	, 	,  % \\+y11 Y!!##"B8,,,r   c                       fd}                      t          |          } j                                        }                     |t
          j                   t          j        ||g          S )zT
        Test initializer failure of request for fields for authentication.
        c                     t          j        d                              |           }j        j                            |           dS )z
            Called when the initializer sent a query for authentication methods.

            The server responds that the client is not authorized to authenticate.
            not-authorizedNr   StanzaErrorrV   r3   r:   rY   rd   s     r   r^   z?IQAuthInitializerTests.testFailRequestFields.<locals>.onAuthGet   sC     ()9::EEbIIHI!!(+++++r   	r@   re   r   r    assertFailurer   rw   r   rf   )r   r^   rh   ri   s   `   r   testFailRequestFieldsz,IQAuthInitializerTests.testFailRequestFields   sv    
	, 	, 	, 	, 	, \\+y11 Y!!## 	2u0111"B8,,,r   c                       fd} fd                      t          |          } j                                        }                     |t
          j                   t          j        ||g          S )z;
        Test initializer failure to authenticate.
        c                 l   t          j        | d          }|                    d           |j                            d           |j                            d           |j                            d                               t
                    }j        j                            |           |S rO   rU   rZ   s      r   r^   z6IQAuthInitializerTests.testFailAuth.<locals>.onAuthGet   r_   r   c                     t          j        d                              |           }j        j                            |           dS )z
            Called when the initializer sent the authentication request.

            The server checks the credentials and responds with a not-authorized
            stanza error.
            ru   Nrv   rd   s     r   r]   z6IQAuthInitializerTests.testFailAuth.<locals>.onAuthSet  sC     ()9::EEbIIHI!!(+++++r   rx   rg   s   `   @r   testFailAuthz#IQAuthInitializerTests.testFailAuth   s    
	 	 	 	 	 	.	, 	, 	, 	, 	, \\+y11 Y!!## 	2u0111"B8,,,r   )
r*   r+   r,   rA   r   rj   rr   rz   r~   __classcell__rL   s   @r   rC   rC   _   s         / / / / /4- 4- 4-l9- 9- 9-v- - -2/- /- /- /- /- /- /-r   rC   c                   .     e Zd ZdZ fdZd Zd Z xZS )BindInitializerTestsz.
    Tests for L{client.BindInitializer}.
    c                     t                                                       t          j        | j                  | _        t          j        d          | j        _        d S )NrE   )	rG   r   r   BindInitializerr
   r   r	   rI   r6   rK   s    r   r   zBindInitializerTests.setUp+  sD    *4>::	!$)D!E!Er   c                       fd} fd}                      t          |          } j                                        }|                    |           t          j        ||g          S )zK
        Set up a stream, and act as if resource binding succeeds.
        c                     t          j        | d          }|                    t          df           |j                            dd           j        j                            |           d S )NrP   bindr	   user@example.com/other resource)content)r
   rV   rW   NS_BINDr   r3   r:   rY   rd   s     r   onBindz.BindInitializerTests.testBasic.<locals>.onBind5  si     +B99H& 1222M$$U4U$VVVI!!(+++++r   c                 l                         t          j        d          j        j                   d S )Nr   )r&   r	   rI   r6   )rP   r   s    r   cbz*BindInitializerTests.testBasic.<locals>.cb;  s;    9::D<N<R    r   )r@   IQ_BIND_SETr   startr9   r   rf   )r   r   r   rh   ri   s   `    r   	testBasiczBindInitializerTests.testBasic0  s    
	, 	, 	, 	, 	,	 	 	 	 	
 \\+v..Y__
r"B8,,,r   c                       fd}                      t          |          } j                                        }                     |t
          j                   t          j        ||g          S )zH
        Set up a stream, and act as if resource binding fails.
        c                     t          j        d                              |           }j        j                            |           d S )Nconflictrv   rd   s     r   r   z0BindInitializerTests.testFailure.<locals>.onBindJ  s@    (44??CCHI!!(+++++r   )	r@   r   r   r   ry   r   rw   r   rf   )r   r   rh   ri   s   `   r   testFailurez BindInitializerTests.testFailureE  sp    
	, 	, 	, 	, 	, \\+v..Y__2u0111"B8,,,r   )r*   r+   r,   rA   r   r   r   r   r   s   @r   r   r   &  se         F F F F F
- - -*- - - - - - -r   r   c                   .     e Zd ZdZ fdZd Zd Z xZS )SessionInitializerTestsz1
    Tests for L{client.SessionInitializer}.
    c                     t                                                       t          j        | j                  | _        d S r   )rG   r   r   SessionInitializerr
   r   rK   s    r   r   zSessionInitializerTests.setUpY  s,    -dn==			r   c                       fd}                      t          |          } j                                        }t	          j        ||g          S )zP
        Set up a stream, and act as if session establishment succeeds.
        c                 p    t          j        | d          }j        j                            |           d S )NrP   )r
   rV   r3   r:   rY   rd   s     r   	onSessionz6SessionInitializerTests.testSuccess.<locals>.onSessionb  s4     +B99HI!!(+++++r   )r@   IQ_SESSION_SETr   r   r   rf   r   r   rh   ri   s   `   r   testSuccessz#SessionInitializerTests.testSuccess]  sW    
	, 	, 	, 	, 	, \\.)44Y__"B8,,,r   c                       fd}                      t          |          } j                                        }                     |t
          j                   t          j        ||g          S )zM
        Set up a stream, and act as if session establishment fails.
        c                     t          j        d                              |           }j        j                            |           d S )N	forbiddenrv   rd   s     r   r   z6SessionInitializerTests.testFailure.<locals>.onSessiono  s@    (55@@DDHI!!(+++++r   )	r@   r   r   r   ry   r   rw   r   rf   r   s   `   r   r   z#SessionInitializerTests.testFailurej  sp    
	, 	, 	, 	, 	, \\.)44Y__2u0111"B8,,,r   )r*   r+   r,   rA   r   r   r   r   r   s   @r   r   r   T  s`         > > > > >- - -- - - - - - -r   r   c                       e Zd ZdZd ZdS )BasicAuthenticatorTestszB
    Test for both BasicAuthenticator and basicClientFactory.
    c                 $   t          j        d          | _        t          j        | j        d                              d          }|                     d|j        j                   |                     | j        |j        j                    |                     d|j        j	                   |j
        \  }}|                     |t          j                   |                     |t          j                   |                     |j                   dS )a  
        Authenticator and stream are properly constructed by the factory.

        The L{xmlstream.XmlStream} protocol created by the factory has the new
        L{client.BasicAuthenticator} instance in its C{authenticator}
        attribute.  It is set up with C{jid} and C{password} as passed to the
        factory, C{otherHost} taken from the client JID. The stream futher has
        two initializers, for TLS and authentication, of which the first has
        its C{required} attribute set to C{True}.
        rE   rF   Nexample.com)r	   rI   
client_jidr   basicClientFactorybuildProtocolr&   r6   	otherHostrJ   initializersassertIsInstancer
   TLSInitiatingInitializerrH   assertFalserequired)r   r   tlsauths       r   
test_basicz"BasicAuthenticatorTests.test_basic~  s     '"=>>
 &tAAOOPTUU 	(8(BCCC"*:*>???2#3#<=== O	Tc9#EFFFdF$<===&&&&&r   N)r*   r+   r,   rA   r   r-   r   r   r   r   y  s-         ' ' ' ' 'r   r   c                   4    e Zd ZdZd Z ee d             ZdS )XMPPAuthenticatorTestsz@
    Test for both XMPPAuthenticator and XMPPClientFactory.
    c                 <   t          j        d          | _        t          j        | j        d                              d          }|                     d|j        j                   |                     | j        |j        j                    |                     d|j        j	                   |j
        \  }}}}}|                     |t          j                   |                     |t                     |                     |t          j                   |                     |t          j                   |                     |j                   |                     |j                   |                     |j                   |                     |j                   dS )z
        Test basic operations.

        Setup an XMPPClientFactory, which sets up an XMPPAuthenticator, and let
        it produce a protocol instance. Then inspect the instance variables of
        the authenticator and XML stream objects.
        rE   rF   Nr   )r	   rI   r   r   XMPPClientFactoryr   r&   r6   r   rJ   r   r   r
   r   r   r   r   
assertTruer   r   )r   r   r   r   saslr   sessions          r   r   z!XMPPAuthenticatorTests.test_basic  sa    '"=>>
 %dox@@NNtTT 	(8(BCCC"*:*>???2#3#<=== -/O)dD'c9#EFFFd$=>>>dF$:;;;gv'@AAA%%%&&&&&&)*****r   c                   
 g 
d	
fd	}t          j        d          | _        t          j                    }t          j        | j        d|          }|                     t          j	        d|           |
                    d          }|j        \  }}}}}	|                     |t          j	                   |                     |
d                    dS )
zG
        A TLS configuration is passed to the TLS initializer.
        TNc                 2                         |           d S r   )append)r   r   r   configurationForTLSconfigss       r   r   z:XMPPAuthenticatorTests.test_tlsConfiguration.<locals>.init  s    NN./////r   rE   rF   )r   __init__r   )TN)r	   rI   r   r   CertificateOptionsr   r   patchr
   r   r   r   r   assertIs)r   r   r   factoryr   r   r   r   r   r   r   s             @r   test_tlsConfigurationz,XMPPAuthenticatorTests.test_tlsConfiguration  s    
 	0 	0 	0 	0 	0 	0 '"=>>
 "466*OX;N
 
 
 	

95z4HHH""4(( -/O)dD'c9#EFFF)71:66666r   N)r*   r+   r,   rA   r   r   skipWhenNoSSLr   r-   r   r   r   r     sQ         + + +B V]7 7 7 7 7r   r   )"rA   hashlibr   r   r   twisted.internetr   twisted.trialtwisted.words.protocols.jabberr   r   r	   r
   #twisted.words.protocols.jabber.saslr   twisted.words.xishr   r   r   ImportErrorre   rX   r   r   
NS_SESSIONr   TestCaser   r/   rC   r   r   r   r   r-   r   r   <module>r      sw   
             " " " " " " " " " " " " H H H H H H H H H H H H I I I I I I & & & & & & $$$$$$
  MM	  0 0 0
C/MMM0 @?
,2W<2
8:E? ? ? ? ?8#4 ? ? ?,$ $ $ $ $ $ $ $ND- D- D- D- D-98;L D- D- D-N+- +- +- +- +-79J +- +- +-\"- "- "- "- "-:H<M "- "- "-J"' "' "' "' "'h/ "' "' "'J@7 @7 @7 @7 @7X. @7 @7 @7 @7 @7s   = 	A	A	