
    EfS                         d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	 d dl
mZmZmZ  G d de      Z G d de      ZddZd	 Zd
 Z G d de      Zy)    N)ThreadAuthenticator)BaseZMQTestCaseSkipTest	skip_pypyc                   L     e Zd Z fdZd Zd Zd Z fdZd Zd Z	d Z
 xZS )	BaseAuthTestCasec                    t        j                         dk  rt        d      	 t        j                          t
        |           t        j                  d      j                  t        j                         | j                         | _        | j                         \  | _        | _        | _        | j#                          y # t         j                  $ r t        d      w xY w)N)   r   zsecurity is new in libzmq 4.0z.security requires libzmq to have curve supportzzmq.auth)zmqzmq_version_infor   curve_keypairZMQErrorsupersetUplogging	getLoggersetLevelDEBUG	make_authauthcreate_certsbase_dirpublic_keys_dirsecret_keys_dir
start_authself	__class__s    5/usr/lib/python3/dist-packages/zmq/tests/test_auth.pyr   zBaseAuthTestCase.setUp   s    !F*:;;	M 	*%..w}}=NN$	DHDUDUDWAt+T-A || 	MKLL	Ms   C C!c                     t               N)NotImplementedErrorr   s    r   r   zBaseAuthTestCase.make_auth    s    !##    c                 &     |j                   |i |S r!   )send)r   socketargskwargss       r   r&   zBaseAuthTestCase.send#   s    v{{D+F++r$   c                 8    | j                   j                          y r!   )r   startr#   s    r   r   zBaseAuthTestCase.start_auth'   s    		r$   c                     | j                   r!| j                   j                          d | _         | j                  | j                         t        |           y r!   )r   stopremove_certsr   r   tearDownr   s    r   r/   zBaseAuthTestCase.tearDown*   s<    99IINNDI$--(r$   c                    t        j                         }t        j                  j	                  |d      }t        j                  j	                  |d      }t        j                  j	                  |d      }t        j
                  |       t        j
                  |       t        j
                  |       t        j                  j                  |d      \  }}t        j                  j                  |d      \  }}t        j                  |      D ]f  }	|	j                  d      st        j                  t        j                  j	                  ||	      t        j                  j	                  |d             h t        j                  |      D ]f  }	|	j                  d      st        j                  t        j                  j	                  ||	      t        j                  j	                  |d             h |||fS )	z$Create CURVE certificates for a testcertificatespublic_keysprivate_keysserverclientz.key.z.key_secret)tempfilemkdtempospathjoinmkdirr   r   create_certificateslistdirendswithshutilmove)
r   r   keys_dirr   r   server_public_fileserver_secret_fileclient_public_fileclient_secret_filekey_files
             r   r   zBaseAuthTestCase.create_certs1   ss    ##%77<<.9'',,x?'',,x@

!
!141M1Mh2
.. 251M1Mh2
.. 

8, 	H  (GGLL84bggll?TW6X	 

8, 	H  /GGLL84bggll?TW6X	 /?;;r$   c                 .    t        j                  |       y)zRemove certificates for a testN)r@   rmtree)r   r   s     r   r.   zBaseAuthTestCase.remove_certsV   s    hr$   c                    t         j                  j                  |d      }t         j                  j                  |d      }t        j                  j                  |      \  }}t        j                  j                  |      \  }}||||fS )z)Return server and client certificate keyszserver.key_secretzclient.key_secret)r9   r:   r;   r   r   load_certificate)r   r   rD   rF   server_publicserver_secretclient_publicclient_secrets           r   
load_certszBaseAuthTestCase.load_certsZ   sq    WW\\/;NOWW\\/;NO'*xx'@'@AS'T$}'*xx'@'@AS'T$}m]MIIr$   )__name__
__module____qualname__r   r   r&   r   r/   r   r.   rP   __classcell__r   s   @r   r   r      s-    $,#<J Jr$   r   c                   P    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zed
        Zy)TestThreadAuthenticationz'Test authentication running in a threadc                 ,    t        | j                        S r!   )r   contextr#   s    r   r   z"TestThreadAuthentication.make_authh   s    "4<<00r$   c                 n   d}d}|j                  |      }|j                  d||fz         dg}|j                  dt        j                         |j                  dt        j                        r"	 |j                  |t        j                         ny|j                  d      r+	 |j                  t        j                        }||k(  sJ d}|S |S # t        j                  $ r t        j                  dt               Y yw xY w# t        j                  $ r t        j                  d	t               Y |S w xY w)
9Check if client can connect to server using tcp transportFtcp://127.0.0.1%s:%i   Hello Worldd   i  z#server set POLLOUT, but cannot sendTz"client set POLLIN, but cannot recv)bind_to_random_portconnectpollr   POLLOUTsend_multipartNOBLOCKAgainwarningswarnRuntimeWarningrecv_multipart)r   r4   r5   resultifaceportmsgrcvd_msgs           r   can_connectz$TestThreadAuthentication.can_connectk   s   !))%0w%./ 	C%;;tS[[)%%c3;;7
 ;;t!00=  3&v 99 C^T 99 TBNS Ts$   0 C $D -D ?D -D43D4c                    | j                   j                          d| _         | j                         | _        | j	                  t
        j                        }| j	                  t
        j                        }| j                  ||      sJ | j	                  t
        j                        }d|_	        | j	                  t
        j                        }| j                  ||      sJ y)zthreaded auth - NULLN   global)
r   r-   ContextrY   r'   r   PUSHPULLrp   
zap_domainr   r4   r5   s      r   	test_nullz"TestThreadAuthentication.test_null   s     				||~SXX&SXX&///
 SXX&%SXX&///r$   c                     | j                   j                  d       | j                  t        j                        }d|_        | j                  t        j                        }| j                  ||      rJ y)zthreaded auth - Blacklist	127.0.0.1rr   N)r   denyr'   r   rt   rv   ru   rp   rw   s      r   test_blacklistz'TestThreadAuthentication.test_blacklist   s[     			{#SXX& &SXX&##FF3333r$   c                     | j                   j                  d       | j                  t        j                        }d|_        | j                  t        j                        }| j                  ||      sJ y)zthreaded auth - Whitelistrz   rr   N)r   allowr'   r   rt   rv   ru   rp   rw   s      r   test_whitelistz'TestThreadAuthentication.test_whitelist   sX     			$SXX& &SXX&///r$   c                    | j                  t        j                        }d|_        | j                  t        j                        }d|_        d|_        | j                  ||      rJ | j                  t        j                        }d|_        | j                  t        j                        }d|_        d|_        | j                  j                  dddi       | j                  ||      sJ | j                  t        j                        }d|_        | j                  t        j                        }d|_        d|_        | j                  ||      rJ | j                  j                          d	| _        | j                  t        j                        }| j                  t        j                        }| j                  ||      sJ |j                          |j                          y	)
zthreaded auth - PLAINT   admin   Password*adminPassworddomain	passwords   BogusN)r'   r   rt   plain_serverru   plain_usernameplain_passwordrp   r   configure_plainr-   closerw   s      r   
test_plainz#TestThreadAuthentication.test_plain   s}    SXX&"SXX& ( +##FF333 SXX&"SXX& ( +		!!*8M!N/// SXX&"SXX& ( (##FF333 				SXX&SXX&///r$   c                    | j                   j                  d       | j                  | j                        }|\  }}}}| j	                  t
        j                        }||_        ||_        d|_	        | j	                  t
        j                        }||_        ||_        ||_        | j                  ||      rJ | j                   j                  dt
        j                   j                         | j	                  t
        j                        }||_        ||_        d|_	        | j	                  t
        j                        }||_        ||_        ||_        | j                  ||      sJ | j                   j                  d| j                         | j	                  t
        j                        }||_        ||_        d|_	        | j	                  t
        j                        }||_        ||_        ||_        | j                  ||      sJ | j                   j!                          d| _         | j	                  t
        j                        }| j	                  t
        j                        }| j                  ||      sJ y)threaded auth - CURVErz   Tr   r   locationN)r   r~   rP   r   r'   r   rt   curve_publickeycurve_secretkeycurve_serverru   curve_serverkeyrp   configure_curveCURVE_ALLOW_ANYr   r-   )r   certsrL   rM   rN   rO   r4   r5   s           r   
test_curvez#TestThreadAuthentication.test_curve   s	   		$ 4 45EJB}m] SXX&!.!."SXX&!.!.!.##FF333 			!!sxx7O7O!PSXX&!.!."SXX&!.!.!./// 			!!t7K7K!LSXX&!.!."SXX&!.!.!./// 				 SXX&SXX&///r$   c                    
 | j                   j                  d       | j                  | j                        }|\  }}
}| j	                  t
        j                        }||_        ||_        d|_	        | j	                  t
        j                        }
|_        ||_        ||_        | j                  ||      rJ  G 
fdd      } |       }| j                   j                  |       | j	                  t
        j                        }||_        ||_        d|_	        | j	                  t
        j                        }
|_        ||_        ||_        | j                  ||      sJ  G d d      }	 |	       }| j                   j                  |       | j	                  t
        j                        }||_        ||_        d|_	        | j	                  t
        j                        }
|_        ||_        ||_        | j                  ||      rJ y)	z2threaded auth - CURVE with callback authenticationrz   Tc                       e Zd Z fdZd Zy)ITestThreadAuthentication.test_curve_callback.<locals>.CredentialsProviderc                     | _         y r!   r5   )r   rN   s    r   __init__zRTestThreadAuthentication.test_curve_callback.<locals>.CredentialsProvider.__init__   s
    +r$   c                 $    || j                   k(  ryyNTFr   r   r   keys      r   callbackzRTestThreadAuthentication.test_curve_callback.<locals>.CredentialsProvider.callback#      $++% r$   NrQ   rR   rS   r   r   )rN   s   r   CredentialsProviderr     s    ,!r$   r   )credentials_providerc                       e Zd Zd Zd Zy)NTestThreadAuthentication.test_curve_callback.<locals>.WrongCredentialsProviderc                     d| _         y )NWrongCredentialsr   r#   s    r   r   zWTestThreadAuthentication.test_curve_callback.<locals>.WrongCredentialsProvider.__init__8  s	    0r$   c                 $    || j                   k(  ryyr   r   r   s      r   r   zWTestThreadAuthentication.test_curve_callback.<locals>.WrongCredentialsProvider.callback;  r   r$   Nr    r$   r   WrongCredentialsProviderr   7  s    1!r$   r   N)r   r~   rP   r   r'   r   rt   r   r   r   ru   r   rp   configure_curve_callback)r   r   rL   rM   rO   r4   r5   r   providerr   rN   s             @r   test_curve_callbackz,TestThreadAuthentication.test_curve_callback  s   		$ 4 45EJB}m] SXX&!.!."SXX&!.!.!.##FF333	! 	! '(		***ISXX&!.!."SXX&!.!.!.///	! 	! ,-		***ISXX&!.!."SXX&!.!.!.##FF3333r$   c                 6   | j                   j                  d       | j                  | j                        }|\  }}}}| j                   j	                  d| j
                         | j                  t        j                        }||_	        ||_
        d|_        | j                  t        j                        }||_	        ||_
        ||_        | j                  ||      sJ | j                  |d       | j!                  |d      }|j"                  dk(  sJ 	 |j%                  d      }	|	|j'                  d	      k(  sJ d
 | j                   _        | j                  t        j                        }
||
_	        ||
_
        ||
_        | j                  |
|      sJ | j                  |
d       | j!                  |d      }|j"                  dk(  sJ 	 |j%                  d      }	|	dk(  sJ y# t        j(                  $ r Y w xY w# t        j(                  $ r Y yw xY w)r   rz   r   r   Ts   testF)copyzUser-Idutf8c                      y)Ncustomr   )
client_keys    r   <lambda>z=TestThreadAuthentication.test_curve_user_id.<locals>.<lambda>k  s    r$   s   test2r   N)r   r~   rP   r   r   r   r'   r   ru   r   r   r   rt   r   rp   r&   recvbytesgetdecodeZMQVersionErrorcurve_user_id)r   r   rL   rM   rN   rO   r4   r5   rn   user_idclient2s              r   test_curve_user_idz+TestThreadAuthentication.test_curve_user_idM  s    			$ 4 45EJB}m]		!!t7K7K!LSXX&!.!."SXX&!.!.!./// 			&'"iiUi+yyG###	;ggi(G m226:::: #>		++chh'"/"/"/000		'8$iiUi+yyH$$$	'ggi(G h&&&- "" 		& "" 		s$   G) H )G?>G?HHN)rQ   rR   rS   __doc__r   rp   rx   r|   r   r   r   r   r   r   r   r$   r   rW   rW   e   sD    11<0*	4	0$L00d?4B -' -'r$   rW   c                       fd}|S )z*decorator for running tests with an IOLoopc                     |       }| j                   }r&| j                  j                  | j                         n%| j                  j                  | j                         |j                  d| j                         |j                  d| j                         r|j                  d| j                         n|j                  d| j                         |j                          | j                  r| j                  | j                         |S )N   g333333?   )io_loop
pullstreamon_recvon_message_succeedon_message_fail
call_laterattempt_connectionsend_msgon_test_timeout_failon_test_timeout_succeedr+   fail_msgfail)r   rloopexpect_successmethods      r   test_methodz with_ioloop.<locals>.test_method  s    4L||OO##D$;$;<OO##D$8$894223T]]+OOAt889OOAt;;<

==IIdmm$r$   r   )r   r   r   s   `` r   with_ioloopr   ~  s    . r$   c                     t        | d      S )NTr   r   s    r   should_authr     s    vt$$r$   c                     t        | d      S )NFr   r   s    r   should_not_authr     s    vu%%r$   c                        e Zd ZdZ fdZd Z fdZd Zd Zd Z	d Z
d	 Zd
 Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Z xZS )TestIOLoopAuthenticationz%Test authentication running in ioloopc                     	 ddl m} ddlm d  _        j                  d       _	        t         -           j                  t        j                         _         j                  t        j                          _         fd} j                  j%                  |       y # t        $ r t        j                  d       Y w xY w)Nr   )ioloopzRequires tornado)	zmqstreamF)make_currentc                     K   j                   j                         _        j                   j                         _        y wr!   )	ZMQStreamr4   
pushstreamr5   r   )r   r   s   r   make_streamsz4TestIOLoopAuthentication.setUp.<locals>.make_streams  s3     '11$++>DO'11$++>DOs   AA)tornador   ImportErrorpytestskipzmq.eventloopr   r   IOLoopr   r   r   r'   r   rt   r4   ru   r5   run_sync)r   r   r   r   r   s   `  @r   r   zTestIOLoopAuthentication.setUp  s    	,& 	,}}%}8kk#((+kk#((+	? 	l+  	,KK*+	,s   B' 'CCc                 T     ddl m  fd} j                  j                  |      S )Nr   )IOLoopAuthenticatorc                  0   K     j                         S wr!   )rY   )r   r   s   r   r   z5TestIOLoopAuthentication.make_auth.<locals>.make_auth  s     &t||44s   )zmq.auth.ioloopr   r   r   )r   r   r   s   ` @r   r   z"TestIOLoopAuthentication.make_auth  s"    7	5 ||$$Y//r$   c                     | j                   r!| j                   j                          d | _         | j                  j                  d       t        |           y )NT)all_fds)r   r-   r   r   r   r/   r   s    r   r/   z!TestIOLoopAuthentication.tearDown  s?    99IINNDI4(r$   c                 ~    d}| j                   j                  |      }| j                  j                  d||fz         y)r[   r\   r]   N)r4   r`   r5   ra   )r   rl   rm   s      r   r   z+TestIOLoopAuthentication.attempt_connection  s6    !{{..u5Gudm34r$   c                 @    dg}| j                   j                  |       y)z&Send a message from server to a clientr^   N)r   rd   )r   rn   s     r   r   z!TestIOLoopAuthentication.send_msg  s    &&s+r$   c                 R    |dgk7  rd| _         | j                  j                          y)z$A message was received, as expected.r^   zUnexpected message receivedNr   r   r-   r   framess     r   r   z+TestIOLoopAuthentication.on_message_succeed  s$    n%%9DMr$   c                 F    d| _         | j                  j                          y)z%A message was received, unexpectedly.z/Received messaged unexpectedly, security failedNr   r   s     r   r   z(TestIOLoopAuthentication.on_message_fail  s    Ir$   c                 8    | j                   j                          y)z*Test timer expired, indicates test successN)r   r-   r#   s    r   r   z0TestIOLoopAuthentication.on_test_timeout_succeed  s    r$   c                 F    d| _         | j                  j                          y)z*Test timer expired, indicates test failurezTest timed outNr   r#   s    r   r   z-TestIOLoopAuthentication.on_test_timeout_fail  s    (r$   c                 F    | j                   j                          d| _         y)zioloop auth - NONEN)r   r-   r#   s    r   	test_nonez"TestIOLoopAuthentication.test_none  s     				r$   c                 &    d| j                   _        y)zioloop auth - NULLrr   N)r4   rv   r#   s    r   rx   z"TestIOLoopAuthentication.test_null  s     "+r$   c                 \    | j                   j                  d       d| j                  _        y)zioloop auth - Blacklistrz   rr   N)r   r{   r4   rv   r#   s    r   r|   z'TestIOLoopAuthentication.test_blacklist  s      			{#!*r$   c                     | j                   j                  d       | j                  j                  t        j
                  d       y)zioloop auth - Whitelistrz   rr   N)r   r~   r4   
setsockoptr   
ZAP_DOMAINr#   s    r   r   z'TestIOLoopAuthentication.test_whitelist  s,     			$s~~y9r$   c                 j    d| j                   _        d| j                   _        d| j                  _        y)z(ioloop auth - PLAIN, unconfigured serverr   r   TN)r5   r   r   r4   r   r#   s    r   test_plain_unconfigured_serverz7TestIOLoopAuthentication.test_plain_unconfigured_server  s(     &."%0"#' r$   c                     d| j                   _        d| j                   _        d| j                  _        | j
                  j                  dddi       y)	z&ioloop auth - PLAIN, configured serverr   r   Tr   r   r   r   Nr5   r   r   r4   r   r   r   r#   s    r   test_plain_configured_serverz5TestIOLoopAuthentication.test_plain_configured_server  sD     &."%0"#' 		!!*8M!Nr$   c                     d| j                   _        d| j                   _        d| j                  _        | j
                  j                  dddi       y)	z&ioloop auth - PLAIN, bogus credentialsr   r   Tr   r   r   r   Nr  r#   s    r   test_plain_bogus_credentialsz5TestIOLoopAuthentication.test_plain_bogus_credentials  sD     &."%-"#' 		!!*8M!Nr$   c                 J   | j                  | j                        }|\  }}}}| j                  j                  d       || j                  _        || j                  _        d| j                  _        || j                  _        || j                  _        || j                  _	        y)z(ioloop auth - CURVE, unconfigured serverrz   TN)
rP   r   r   r~   r4   r   r   r   r5   r   r   r   rL   rM   rN   rO   s         r   test_curve_unconfigured_serverz7TestIOLoopAuthentication.test_curve_unconfigured_server!  s~      4 45EJB}m]		$&3#&3##' &3#&3#&3#r$   c                    | j                  | j                        }|\  }}}}| j                  j                  d       | j                  j	                  dt
        j                  j                         || j                  _        || j                  _	        d| j                  _
        || j                  _        || j                  _	        || j                  _        y)z$ioloop auth - CURVE, CURVE_ALLOW_ANYrz   r   r   TN)rP   r   r   r~   r   r   r   r4   r   r   r   r5   r   r  s         r   test_curve_allow_anyz-TestIOLoopAuthentication.test_curve_allow_any1  s      4 45EJB}m]		$		!!sxx7O7O!P&3#&3##' &3#&3#&3#r$   c                    | j                   j                  d       | j                  | j                        }|\  }}}}| j                   j	                  d| j
                         || j                  _        || j                  _        d| j                  _	        || j                  _        || j                  _        || j                  _        y)z&ioloop auth - CURVE, configured serverrz   r   r   TN)r   r~   rP   r   r   r   r4   r   r   r   r5   r   r  s         r   test_curve_configured_serverz5TestIOLoopAuthentication.test_curve_configured_serverB  s     			$ 4 45EJB}m]		!!t7K7K!L&3#&3##' &3#&3#&3#r$   )rQ   rR   rS   r   r   r   r/   r   r   r   r   r   r   r   r  rx   r   r|   r   r
  r  r  r  r  r  rT   rU   s   @r   r   r     s    /,&05,


   + + + + : : ( ( O O O O 4 4 4 4  4 4r$   r   )T)r   r9   r@   r7   rg   r   zmq.authr   zmq.auth.threadr   	zmq.testsr   r   r   r   rW   r   r   r   r   r   r$   r   <module>r     sd     	      / : :QJ QJhV'/ V'r:%&n4/ n4r$   