3
(h^                 @   s   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 G dd dZ	G dd dej
ZG dd	 d	ej
ZG d
d deZdd ZG dd dej
ZdS )    N)base)	text_typec               @   s&   e Zd ZdddZdd Zdd ZdS )	TempUserNc             C   s   || _ || _|| _d| }|d k	r0|d| 7 }n(|d k	rX|d| 7 }|d k	rX|d| 7 }y|j| d| _W n tjjk
r   d| _Y nX y|jd||f  d| _W n tjjk
r   d| _Y nX d S )NzCREATE USER z IDENTIFIED BY '%s'z IDENTIFIED WITH %sz AS '%s'TFzGRANT SELECT ON %s.* TO %s)	_c_user_dbexecute_createdpymysqlerrInternalError_grant)selfcuserdbauthZauthdatapasswordcreate r   B/tmp/pip-install-q3hcpn_q/PyMySQL/pymysql/tests/test_connection.py__init__   s(    


zTempUser.__init__c             C   s   | S )Nr   )r   r   r   r   	__enter__"   s    zTempUser.__enter__c             C   s:   | j r| jjd| j| jf  | jr6| jjd| j  d S )NzREVOKE SELECT ON %s.* FROM %szDROP USER %s)r   r   r   r   r   r	   )r   exc_type	exc_value	tracebackr   r   r   __exit__%   s    zTempUser.__exit__)NNN)__name__
__module____qualname__r   r   r   r   r   r   r   r   
   s   
r   c               @   sF  e Zd ZdZdZdZdZdZdZdZ	ddl
Z
e
jjdZejjd j Zejddk	obejddMkZejf ej Zed	= ejd
 xeD ]Zed ed fdNkrqed dkred ZdZqed dkred dkrdZned dkrdZqed dkr"dZed jdd ZedkrFdZqed dkr6dZqed dkrdZ	qW dd Zejedejeddd Z ejedejed d!d" Z!d#d$ Z"G d%d& d&e#Z$G d'd( d(e#Z%G d)d* d*e#Z&ejedejed+d,d- Z'ejedejed.d/d0 Z(d1d2 Z)ejedejed3d4d5 Z*ejedejed6d7d8 Z+d9d: Z,ejedejed;eje
jjd<dkd=eje
jjd>dkd?d@dA Z-ejedejedBeje
jjd<dkd=eje
jjd>dkd?dCdD Z.dEdF Z/ejedejedGdHdI Z0ejedeje	dJdKdL Z1dS )OTestAuthenticationFr   NUSERunix_sockethost	localhost	127.0.0.1r   zSHOW PLUGINS      ACTIVEAUTHENTICATION   zauth_socket.soTzdialog_examples.sotwo_questionsthree_attemptspam.Zauth_pamZmysql_old_passwordsha256_passwordc             C   s   | j d| jd j d S )NZmysql_native_passwordr   )assertEqualconnectionsZ_auth_plugin_name)r   r   r   r   test_plugina   s    zTestAuthentication.test_pluginz"connection to unix_socket requiredzsocket plugin already installedc             C   s   | j d j }zy"|jd dt_d| _| j  W nd tjj	k
r   y"|jd dt_d| _| j  W n( tjj	k
r   dt_t
jdY nX Y nX W d tjr|jd	| j  X d S )
Nr   z2install plugin auth_socket soname 'auth_socket.so'TZauth_socketzinstall soname 'auth_socket'r"   Fz%we couldn't install the socket pluginzuninstall plugin %s)r1   cursorr   r    socket_foundsocket_plugin_namerealtestSocketAuthr
   r   r   	unittest2SkipTest)r   curr   r   r   testSocketAuthInstallPlugine   s"    

z.TestAuthentication.testSocketAuthInstallPluginzno socket pluginc             C   s   | j   d S )N)r6   )r   r   r   r   testSocketAuth|   s    z!TestAuthentication.testSocketAuthc             C   sR   t | jd j tjd | jd d | j}tjf dtji| j	}W d Q R X d S )Nr   z
@localhostr   r   )
r   r1   r3   r    osuser	databasesr5   r
   connectr   )r   ur   r   r   r   r6      s    z%TestAuthentication.realtestSocketAuthc               @   s    e Zd ZdZdd Zdd ZdS )zTestAuthentication.DialogFc             C   s   t jj| _d S )N)r    Dialogfail)r   conr   r   r   r      s    
z"TestAuthentication.Dialog.__init__c             C   s   | j rd| _ dS | jj|S )NFs   bad guess at a password)rA   mget)r   echopromptr   r   r   rF      s    z TestAuthentication.Dialog.promptN)r   r   r   rA   r   rF   r   r   r   r   r@      s   r@   c               @   s   e Zd Zdd Zdd ZdS )z TestAuthentication.DialogHandlerc             C   s
   || _ d S )N)rB   )r   rB   r   r   r   r      s    z)TestAuthentication.DialogHandler.__init__c             C   st   xn|j  }|d@ dk}|d@ dk}|j }|dkr@| jjd n| jjd | jj }|j  |j sj|rP qW |S )N   r'   r&   s   Password, please:s   stillnotverysecret s$   no idea what to do with this prompt )Z
read_uint8Zread_allrB   Zwrite_packetZ_read_packetZcheck_errorZis_ok_packet)r   ZpktflagrE   lastrF   r   r   r   authenticate   s    
z-TestAuthentication.DialogHandler.authenticateN)r   r   r   r   rJ   r   r   r   r   DialogHandler   s   rK   c               @   s   e Zd Zdd ZdS )z#TestAuthentication.DefectiveHandlerc             C   s
   || _ d S )N)rB   )r   rB   r   r   r   r      s    z,TestAuthentication.DefectiveHandler.__init__N)r   r   r   r   r   r   r   r   DefectiveHandler   s   rL   z&two_questions plugin already installedc             C   sj   | j d j }zDy|jd dt_| j  W n" tjjk
rN   t	j
dY nX W d tjrd|jd X d S )Nr   z8install plugin two_questions soname 'dialog_examples.so'Tz,we couldn't install the two_questions pluginzuninstall plugin two_questions)r1   r3   r   r    two_questions_foundrealTestDialogAuthTwoQuestionsr
   r   r   r7   r8   )r   r9   r   r   r   'testDialogAuthTwoQuestionsInstallPlugin   s    
z:TestAuthentication.testDialogAuthTwoQuestionsInstallPluginzno two questions auth pluginc             C   s   | j   d S )N)rN   )r   r   r   r   testDialogAuthTwoQuestions   s    z-TestAuthentication.testDialogAuthTwoQuestionsc             C   s   dt j_dddt j_t| jd j d| jd d dd	T}| jt	j
j t	jf d
di| j W d Q R X t	jf ddt jid| j W d Q R X d S )NFs   notverysecrets   yes, of course)s   Password, please:s   Are you sure ?r   zpymysql_2q@localhostr   r+   Znotverysecretr   Z
pymysql_2qs   dialog)r   auth_plugin_map)r    r@   rA   rC   r   r1   r3   r=   assertRaisesr
   r   OperationalErrorr>   r   )r   r?   r   r   r   rN      s     z1TestAuthentication.realTestDialogAuthTwoQuestionsz'three_attempts plugin already installedc             C   sj   | j d j }zDy|jd dt_| j  W n" tjjk
rN   t	j
dY nX W d tjrd|jd X d S )Nr   z9install plugin three_attempts soname 'dialog_examples.so'Tz-we couldn't install the three_attempts pluginzuninstall plugin three_attempts)r1   r3   r   r    three_attempts_foundrealTestDialogAuthThreeAttemptsr
   r   r   r7   r8   )r   r9   r   r   r   1testDialogAuthThreeAttemptsQuestionsInstallPlugin   s    
zDTestAuthentication.testDialogAuthThreeAttemptsQuestionsInstallPluginzno three attempts pluginc             C   s   | j   d S )N)rU   )r   r   r   r   testDialogAuthThreeAttempts   s    z.TestAuthentication.testDialogAuthThreeAttemptsc          /   C   s  ddit j_dt j_t| jd j d| jd d ddp}tj	f d	d
t jid| j
 tj	f d	d
t jid| j
 | jtjj" tj	f d	d
tid| j
 W d Q R X | jtjj$ tj	f d	d
t jid| j
 W d Q R X | jtjj$ tj	f d	dt jid| j
 W d Q R X ddit j_| jtjj$ tj	f d	d
t jid| j
 W d Q R X dd it j_| jtjj$ tj	f d	d
t jid| j
 W d Q R X W d Q R X d S )Ns   Password, please:s   stillnotverysecretTr   zpymysql_3a@localhostr   r,   ZstillnotverysecretZ
pymysql_3as   dialog)r   rQ   s   notdialogplugins   I do not know)r    r@   rC   rA   r   r1   r3   r=   r
   r>   r   rK   rR   r   rS   objectrL   )r   r?   r   r   r   rU      s$    &(((z2TestAuthentication.realTestDialogAuthThreeAttemptszpam plugin already installedPASSWORDzPASSWORD env var required
PAMSERVICEzPAMSERVICE env var requiredc             C   sj   | j d j }zDy|jd dt_| j  W n" tjjk
rN   t	j
dY nX W d tjrd|jd X d S )Nr   z'install plugin pam soname 'auth_pam.so'Tz'we couldn't install the auth_pam pluginzuninstall plugin pam)r1   r3   r   r    	pam_foundrealTestPamAuthr
   r   r   r7   r8   )r   r9   r   r   r   testPamAuthInstallPlugin   s    
z+TestAuthentication.testPamAuthInstallPluginzno pam pluginc             C   s   | j   d S )N)r\   )r   r   r   r   testPamAuth  s    zTestAuthentication.testPamAuthc          -   C   s  | j j }dd l}|jjd|d< | jd j }y8|jdtj	 d  |j
 d }|jdtj	 d  W n: tjk
r } z| jd|jd  d }W Y d d }~X nX t|tj	d | jd d d	|jjd
}y\tjf dtj	i|}d|d< | jtjj& tjf tj	dtjid| j  W d Q R X W nr tjk
r } zR| jd|jd  | jtjj& tjf tj	dtjid| j  W d Q R X W Y d d }~X nX W d Q R X |r|j| d S )Nr   rY   r   zshow grants for z
@localhostz
drop user i  r   r-   rZ   r   zvery bad guess at passwords   mysql_cleartext_password)r   rQ   )r   copyosenvironrD   r1   r3   r   r    r<   fetchoner
   rS   r0   argsr   r=   r>   rR   r   rL   )r   r   r`   r9   Zgrantser?   r   r   r   r   r\     s8    


0z"TestAuthentication.realTestPamAuthzno mysql_old_password pluginc              C   s  | j | jd drtjd| j | jd dr8tjd| jj }d|d< | jd T}tjdd	 dkr| j | jd dr| jt	j
j}|jd|d   W d Q R X n|jd|d   |j d }| j|d |jd |j d }|jd tjdd	 dkrB| j | jd drB| jt	j
j}|jd W d Q R X n
|jd t|d| jd d |d d(}t	jf ddi|j }|jd W d Q R X |jd|  W d Q R X d S )Nr         z%Old passwords aren't supported in 5.7rG   z'Old passwords don't authenticate in 5.6zcrummy p	asswordr   r'   r*   zSELECT OLD_PASSWORD('%s')Z2a01785203b08770zSELECT @@secure_authzset old_passwords=1zset global secure_auth=0zold_pass_user@localhostr   )r   r   Zold_pass_userzSELECT VERSION()zset global secure_auth=%r)re   rf   r   )re   rG   r   )r*   r'   )re   rG   r   )r*   r'   )re   rG   r   )mysql_server_isr1   r7   r8   r   r_   sysversion_infoassertWarnsr
   r   Warningr   rb   r0   r   r=   r>   r3   )r   r   r   cmvZsecure_auth_settingr?   r9   r   r   r   testMySQLOldPasswordAuth2  s2    


$

(
z+TestAuthentication.testMySQLOldPasswordAuthz.no sha256 password authentication plugin foundc             C   s   | j d j }t|d| jd d dx}| j| j d drF|jd n|jd |jd	 | jj }d
|d< | jt	j
j t	jf ddi| W d Q R X W d Q R X d S )Nr   zpymysql_sha256@localhostr   r/   re   rf   z;SET PASSWORD FOR 'pymysql_sha256'@'localhost' ='Sh@256Pa33'zSET old_passwords = 2zFSET PASSWORD FOR 'pymysql_sha256'@'localhost' = PASSWORD('Sh@256Pa33')z
Sh@256Pa33r   r   Zpymysql_256)re   rf   r   )r1   r3   r   r=   rg   r   r   r_   rR   r
   r   rS   r>   )r   r   r?   r   r   r   r   testAuthSHA256[  s    


z!TestAuthentication.testAuthSHA256)r$   r%   )r(   r)   )2r   r   r   Zsocket_authr4   rM   rT   r[   Zmysql_old_password_foundZsha256_password_foundr`   ra   rD   r<   r   PyMySQLTestCaser=   r_   r   r
   r>   r3   r9   r   rr5   splitZpam_plugin_namer2   r7   
skipUnlessZskipIfr:   r;   r6   rX   r@   rK   rL   rO   rP   rN   rV   rW   rU   r]   r^   r\   rn   ro   r   r   r   r   r    ,   s   











&

&$
(
r    c               @   s~   e 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ejejdd dkddd ZdS )TestConnectionc             C   s&   | j d j }d|d< tjf |}dS )zThis test requires MySQL >= 5.5r   Zutf8mb4charsetN)r=   r_   r
   r>   )r   argconnr   r   r   test_utf8mb4n  s    zTestConnection.test_utf8mb4c             C   sf   | j d j }|jd |j d dk r4td dS dd }|jd	| d
  |j d |ksbtdS )z!Large query and response (>=16MB)r   zSELECT @@max_allowed_packet   i   
   z*Set max_allowed_packet to bigger than 17MBNazSELECT ''i @  i   i
  i @  i   )r1   r3   r   rb   printAssertionError)r   r9   tr   r   r   test_largedatat  s    
zTestConnection.test_largedatac             C   sr   | j d }| j|j  |j }|jd | j|j  |jd | j|j  |jd | j|j d d d S )Nr   zSET AUTOCOMMIT=1FzSELECT @@AUTOCOMMIT)	r1   assertFalseZget_autocommitr3   r   
assertTrueZ
autocommitr0   rb   )r   rB   r9   r   r   r   test_autocommit  s    



zTestConnection.test_autocommitc             C   sx   | j d }| jd d }| jd d }|j }|jd | j|j d | |j| |jd | j|j d | d S )Nr   r   r&   zSELECT database())r1   r=   r3   r   r0   rb   Z	select_db)r   rB   
current_dbZother_dbr9   r   r   r   test_select_db  s    



zTestConnection.test_select_dbc             C   s`   | j d }|j }|jd tjd | jtj}|jd W dQ R X | j|j	j
d d dS )	z
        http://dev.mysql.com/doc/refman/5.0/en/gone-away.html
        http://dev.mysql.com/doc/refman/5.0/en/error-messages-client.html#error_cr_server_gone_error
        r   zSET wait_timeout=1r'   z
SELECT 1+1N    )r   r   )r1   r3   r   timesleeprR   r
   rS   ZassertIn	exceptionrc   )r   rB   r9   rl   r   r   r   test_connection_gone_away  s    


z(TestConnection.test_connection_gone_awayc          
   C   sn   t jf ddi| jd }|j }|jd | jd|j  |j  | jt j	j
 |jdd W d Q R X d S )	NZinit_commandzSELECT "bar"; SELECT "baz"r   zselect "foobar";foobarF)Z	reconnect)r   )r
   r>   r=   r3   r   r0   rb   closerR   r   ErrorZping)r   rw   r   r   r   r   test_init_command  s    
z TestConnection.test_init_commandc             C   s*   t jf ddi| jd }| j|j d S )NZread_default_groupclientr   )r
   r>   r=   r   open)r   rw   r   r   r   test_read_default_group  s    z&TestConnection.test_read_default_groupc              C   s   | j tT tjf | jd }|2}|jd |j  |jd td|j  W d Q R X W d Q R X tjf | jd }|.}|jd | jd|j	 d  |jd W d Q R X |.}|jd | jd|j	 d  |jd W d Q R X d S )Nr   zcreate table test ( a int )zinsert into test values ((1))zpseudo abortzselect count(*) from testr&   zdrop table test)
rR   
ValueErrorr
   r>   r=   r   begincommitr0   rb   )r   r   r9   r   r   r   test_context  s"    



zTestConnection.test_contextc             C   s    t jf | jd }|jd d S )Nr   utf8)r
   r>   r=   set_charset)r   r   r   r   r   test_set_charset  s    zTestConnection.test_set_charsetc             C   s   dd l }x| jD ]}|j }y"|j |j|j}|j|d  W n2 tk
rp   |j|jdd|jddf}Y nX x,d	D ]$}y
||= W qx tk
r   Y qxX qxW t	jf ddi|}| j
|j |j| |j  |j  qW d S )
Nr   r"   r#   r$   porti  Zdefer_connectT)r"   r#   r   )socketr=   r_   AF_UNIXSOCK_STREAMr>   KeyErrorcreate_connectionrD   r
   r   r   r   )r   r   r   dsockkr   r   r   r   test_defer_connect  s&     



z!TestConnection.test_defer_connectr   r'   r*   zrequired py-3.2c          
   C   s<   | j d j }d|d< | jt}tjf |}W d Q R X d S )Nr   TZno_delay)r=   r_   rj   DeprecationWarningr
   r>   )r   r   rl   rw   r   r   r   test_no_delay_warning  s    z$TestConnection.test_no_delay_warningN)r*   r'   )r   r   r   rx   r   r   r   r   r   r   r   r   r   r7   rs   rh   ri   r   r   r   r   r   rt   l  s   rt   c               @   s   e Zd ZdZdS )FoobarN)r   r   r   valuer   r   r   r   r     s   r   c             C   s   | j S )N)r   )xr   r   r   r   
escape_foo  s    r   c               @   sT   e 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S )
TestEscapec             C   sD   | j d }|j }| j|jdd |jd | j|jdd d S )Nr   zfoo'barz
'foo\'bar'z7SET sql_mode='NO_BACKSLASH_ESCAPES,NO_AUTO_CREATE_USER'z
'foo''bar')r1   r3   r0   escaper   )r   rB   r9   r   r   r   test_escape_string  s
    

zTestEscape.test_escape_stringc             C   s>   | j d }|j }tjddddd}| j|j||jd d S )Nr   i  r*      re   rG   z'2012-03-04 05:06:00')r1   r3   datetimer0   r   encoders)r   rB   r9   valr   r   r   test_escape_builtin_encoders  s    
z'TestEscape.test_escape_builtin_encodersc             C   s4   | j d }|j }tti}| j|jt |d d S )Nr   r   )r1   r3   r   r   r0   r   )r   rB   r9   mappingr   r   r   test_escape_custom_object  s    
z$TestEscape.test_escape_custom_objectc             C   sH   | j d }|j }G dd dt}ttji}| j|j|d|d d S )Nr   c               @   s   e Zd ZdS )z7TestEscape.test_escape_fallback_encoder.<locals>.CustomN)r   r   r   r   r   r   r   Custom  s   r   r   z'foobar')r1   r3   strr   r
   Zescape_stringr0   r   )r   rB   r9   r   r   r   r   r   test_escape_fallback_encoder  s
    

z'TestEscape.test_escape_fallback_encoderc             C   s(   | j d }|j }| jt|jdi  d S )Nr   *   )r1   r3   rR   	TypeErrorr   )r   rB   r9   r   r   r   test_escape_no_default  s    
z!TestEscape.test_escape_no_defaultc             C   sF   | j d }|j }|jj }t|t< | j|jdt i|ddi d S )Nr   Zfoor   )r1   r3   r   r_   r   r   r0   r   )r   rB   r9   r   r   r   r   test_escape_dict_value"  s
    

z!TestEscape.test_escape_dict_valuec             C   s@   | j d }|j }|jj }t|t< | j|jt g|d d S )Nr   z(bar))r1   r3   r   r_   r   r   r0   r   )r   rB   r9   r   r   r   r   test_escape_list_item*  s
    

z TestEscape.test_escape_list_itemc             C   sF   | j d }|j }|jd |j }|jd | j|j d d d S )Nr   zSELECT 1; SELECT 2zSELECT 3r*   )r1   r3   r   r0   rb   )r   rB   Zcur1Zcur2r   r   r   test_previous_cursor_not_closed2  s    


z*TestEscape.test_previous_cursor_not_closedc             C   sF   | j d }|j }|jd |j  |jd | j|j d d d S )Nr   zSELECT 1; SELECT 2zSELECT 3r*   )r1   r3   r   r   r0   rb   )r   rB   r9   r   r   r   test_commit_during_multi_result:  s    


z*TestEscape.test_commit_during_multi_resultN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s   	
r   )r   rh   r   r7   r
   Zpymysql.testsr   Zpymysql._compatr   r   rp   r    rt   rX   r   r   r   r   r   r   r   <module>   s   "  B 