a
    W×akP  ã                   @   sX   d Z ddlm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ej
jƒZdS )z«Tests for the Credentials Python bindings.

Note that this just tests the bindings work. It does not intend to test
the functionality, that's already done in other tests.
é    )ÚcredentialsN)ÚPY3)Úmiscc                       sD  e Zd Z‡ f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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/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Zd;d<„ Z d=d>„ Z!d?d@„ Z"dAdB„ Z#dCdD„ Z$dEdF„ Z%dGdH„ Z&dIdJ„ Z'dKdL„ Z(‡  Z)S )MÚCredentialsTestsc                    s.   t t| ƒ ¡  t ¡ | _tr$d| _nd| _d S )NÚxZwx)Úsuperr   ÚsetUpr   ÚCredentialsÚcredsr   Ú	open_mode©Úself©Ú	__class__© ú9/usr/lib/python3/dist-packages/samba/tests/credentials.pyr   "   s
    
zCredentialsTests.setUpc                 C   s"   | j  d¡ |  d| j  ¡ ¡ d S )NÚsomebody)r
   Úset_usernameÚassertEqualÚget_usernamer   r   r   r   Útest_set_username-   s    z"CredentialsTests.test_set_usernamec                 C   s"   | j  d¡ |  d| j  ¡ ¡ d S )NZS3CreT)r
   Úset_passwordr   Úget_passwordr   r   r   r   Útest_set_password1   s    z"CredentialsTests.test_set_passwordc                 C   s6   d}|  d¡}|  | j |¡¡ |  || j ¡ ¡ d S )NZS3cRetú	utf-16-le)ÚencodeÚ
assertTruer
   Zset_utf16_passwordr   r   ©r   ÚpasswordZ	passbytesr   r   r   Útest_set_utf16_password5   s    
z(CredentialsTests.test_set_utf16_passwordc                 C   s:   |   d | j ¡ ¡ |  | j d¡¡ |   d| j ¡ ¡ d S )NZS3c0ndS3CreT)r   r
   Úget_old_passwordr   Zset_old_passwordr   r   r   r   Útest_set_old_password;   s    z&CredentialsTests.test_set_old_passwordc                 C   s6   d}|  d¡}|  | j |¡¡ |  || j ¡ ¡ d S )NZ	0ldS3cRetr   )r   r   r
   Zset_old_utf16_passwordr   r    r   r   r   r   Útest_set_old_utf16_password@   s    
z,CredentialsTests.test_set_old_utf16_passwordc                 C   s4   | j  d¡ |  d| j  ¡ ¡ |  | j  ¡ d ¡ d S )NZABMAS)r
   Ú
set_domainr   Ú
get_domainÚget_principalr   r   r   r   Útest_set_domainF   s    z CredentialsTests.test_set_domainc                 C   s4   | j  d¡ |  d| j  ¡ ¡ |  | j  ¡ d ¡ d S )NZmyrealmZMYREALM)r
   Ú	set_realmr   Ú	get_realmr%   r   r   r   r   Útest_set_realmK   s    zCredentialsTests.test_set_realmc                 C   s4   | j  d¡ |  d| j  ¡ ¡ |  d | j  ¡ ¡ d S )Nú%Ú ©r
   Úparse_stringr   r   r   r   r   r   r   Útest_parse_string_anonP   s    z'CredentialsTests.test_parse_string_anonc                 C   s4   | j  d¡ |  d| j  ¡ ¡ |  d| j  ¡ ¡ d S )Nzsomeone%Úsomeoner+   r,   r   r   r   r   Útest_parse_string_empty_pwU   s    z+CredentialsTests.test_parse_string_empty_pwc                 C   s4   | j  d¡ |  d| j  ¡ ¡ |  d | j  ¡ ¡ d S )Nr/   r,   r   r   r   r   Útest_parse_string_none_pwZ   s    z*CredentialsTests.test_parse_string_none_pwc                 C   sF   | j  d¡ |  d| j  ¡ ¡ |  d| j  ¡ ¡ |  d| j  ¡ ¡ d S )Nzdom\someone%secrr/   ZsecrZDOM)r
   r-   r   r   r   r$   r   r   r   r   Ú test_parse_string_user_pw_domain_   s    z1CredentialsTests.test_parse_string_user_pw_domainc                 C   s4   |   d | j ¡ ¡ | j d¡ |   d| j ¡ ¡ d S )Nzdc=foo,cn=bar)r   r
   Zget_bind_dnZset_bind_dnr   r   r   r   Útest_bind_dne   s    zCredentialsTests.test_bind_dnc                 C   sV   | j  d¡ |  | j  ¡ ¡ | j  d¡ |  | j  ¡ ¡ | j  ¡  |  | j  ¡ ¡ d S ©Nr+   r   )r
   r   r   Úis_anonymousÚassertFalseÚset_anonymousr   r   r   r   Útest_is_anonj   s    
zCredentialsTests.test_is_anonc                 C   s"   | j  d¡ |  d| j  ¡ ¡ d S )NZ	myworksta)r
   Zset_workstationr   Zget_workstationr   r   r   r   Útest_workstationr   s    z!CredentialsTests.test_workstationc                 C   s:   |   tj| j ¡ ¡ | j tj¡ |   tj| j ¡ ¡ d S ©N)r   r   ZSEC_CHAN_NULLr
   Zget_secure_channel_typeZset_secure_channel_typeZSEC_CHAN_BDCr   r   r   r   Útest_secure_channel_typex   s    ÿÿz)CredentialsTests.test_secure_channel_typec                 C   sB   d}d}| j  |¡ |  || j  ¡ ¡ |  t |¡| j  ¡ ¡ d S )NZgeheimÚ c2ae1fe6e648846352453e816f2aeb93)r
   r   r   r   ÚbinasciiÚa2b_hexÚget_nt_hash)r   r   Ú
hex_nthashr   r   r   Útest_get_nt_hash   s    ÿz!CredentialsTests.test_get_nt_hashc                 C   s¼   d}d}d}d}d}| j  d¡ | j  d¡ | j  |¡ |  || j  ¡ ¡ |  t |¡| j  ¡ ¡ | j j	t
jt |¡d}|  |d	 t |¡¡ |  |d
 t |¡¡ |  |d t
j¡ d S )NZSecREt01Z0123456789abcdefZ cd06ca7c7e10c99b1d33b7485a2ed808Z 3f373ea8e4af954f14faa506f8eebdc4Z025a98c1c31e81847466b29b2df4680f39958fb8c213a9cc6ZfredZnurk)ÚflagsZ	challengeZnt_responseZnt_session_keyrB   )r
   r   r#   r   r   r   r=   r>   r?   Zget_ntlm_responser   ZCLI_CRED_NTLM_AUTH)r   r   Zhex_challenger@   Zhex_session_keyZhex_ntlm_responseZresponser   r   r   Útest_get_ntlm_response‡   s$    ÿ
ÿz'CredentialsTests.test_get_ntlm_responsec                 C   sJ   | j  d¡ d}| j  |¡ |  d | j  ¡ ¡ |  t |¡| j  ¡ ¡ d S )NTr<   )r
   Zset_password_will_be_nt_hashr   r   r   r=   r>   r?   )r   r@   r   r   r   Útest_get_nt_hash_stringš   s    ÿz(CredentialsTests.test_get_nt_hash_stringc                 C   s   | j  ¡  d S r:   )r
   Zset_cmdline_callbacksr   r   r   r   Útest_set_cmdline_callbacks¢   s    z+CredentialsTests.test_set_cmdline_callbacksc                 C   s<   | j  d¡ |  | j  ¡ ¡ | j  d¡ |  | j  ¡ ¡ d S r4   )r
   r   r6   Úauthentication_requestedr   r   r   r   r   Útest_authentication_requested¥   s    z.CredentialsTests.test_authentication_requestedc                 C   s   |   | j ¡ ¡ d S r:   )r6   r
   Zwrong_passwordr   r   r   r   Útest_wrong_password«   s    z$CredentialsTests.test_wrong_passwordc                 C   sœ   t  ¡ }tj ¡ }dtjd< | |¡ |  | 	¡ d¡ |  | 
¡ | d¡ ¡ ¡ |  | ¡ d ¡ |  | ¡ d| 
¡  ¡ |  | ¡ d¡ |  | ¡ d¡ d S )NÚenv_userÚUSERÚ	workgroupúenv_user@%sF)r   r	   ÚsambaÚtestsÚenv_loadparmÚosÚenvironÚguessr   r   r$   ÚgetÚupperr(   r%   r5   rF   ©r   r
   Úlpr   r   r   Ú
test_guess®   s    


zCredentialsTests.test_guessc                 C   s’   t  ¡ }tj ¡ }dtjd< | |¡ | ¡  |  	| 
¡ d¡ |  	| ¡ d¡ |  	| ¡ d ¡ |  	| ¡ d ¡ |  	| ¡ d¡ |  	| ¡ d¡ d S )NrI   rJ   r+   TF)r   r	   rM   rN   rO   rP   rQ   rR   r7   r   r   r$   r(   r%   r5   rF   rU   r   r   r   Útest_set_anonymousº   s    


z#CredentialsTests.test_set_anonymousc                 C   s  d}d}d}d}t j | jd¡}t|| jƒ}| d| ¡ | d| ¡ | d| ¡ | d	| ¡ | ¡  | j 	|¡ |  
| j ¡ |¡ |  
| j ¡ |¡ |  
| j ¡ | ¡ ¡ |  
| j ¡ | ¡ ¡ |  
| j ¡ d
|| ¡ f ¡ |  
| j ¡ d¡ |  
| j ¡ d¡ t  |¡ d S )Núrealm.example.comÚdomÚpassÚuserÚ
parse_fileú	realm=%s
ú
domain=%s
zusername=%s
úpassword=%s
ú%s@%sFT©rP   ÚpathÚjoinZtempdirÚopenr   ÚwriteÚcloser
   r]   r   r   r   r$   rT   r(   r%   r5   rF   Úunlink©r   ÚrealmÚdomainr   ÚusernameÚpasswd_file_nameÚpasswd_file_fdr   r   r   Útest_parse_file_1Ç   s(    z"CredentialsTests.test_parse_file_1c                 C   s  d}d}d}d}t j | jd¡}t|| jƒ}| d| ¡ | d| ¡ | d||f ¡ | d	| ¡ | ¡  | j 	|¡ |  
| j ¡ |¡ |  
| j ¡ |¡ |  
| j ¡ | ¡ ¡ |  
| j ¡ | ¡ ¡ |  
| j ¡ d
|| ¡ f ¡ |  
| j ¡ d¡ |  
| j ¡ d¡ t  |¡ d S )NrY   rZ   r[   r\   r]   r^   r_   zusername=%s\%s
r`   ra   FTrb   ri   r   r   r   Útest_parse_file_2Þ   s(    z"CredentialsTests.test_parse_file_2c                 C   s   d}d}d}d}d}t j | jd¡}t|| jƒ}| d| ¡ | d| ¡ | d	||f ¡ | d
| ¡ | ¡  | j 	|¡ |  
| j ¡ |¡ |  
| j ¡ |¡ |  
| j ¡ | ¡ ¡ |  
| j ¡ | ¡ ¡ |  
| j ¡ d|| ¡ f ¡ |  
| j ¡ d¡ |  
| j ¡ d¡ t  |¡ d S )NrY   rk   r   rl   Úuserdomr]   r^   r_   zusername=%s/%s
r`   ra   FTrb   )r   rj   rk   r   rl   rq   rm   rn   r   r   r   Útest_parse_file_3õ   s*    z"CredentialsTests.test_parse_file_3c                 C   s  d}d}d}t j | jd¡}t|| jƒ}| d|||f ¡ | d¡ | d¡ | d¡ | ¡  | j 	|¡ |  
| j ¡ |¡ |  
| j ¡ |¡ |  
| j ¡ | ¡ ¡ |  
| j ¡ | ¡ ¡ |  
| j ¡ d	|| ¡ f ¡ |  
| j ¡ d
¡ |  
| j ¡ d¡ t  |¡ d S )Nr   rl   rq   r]   úusername=%s\%s%%%s
úrealm=ignorerealm
údomain=ignoredomain
úpassword=ignorepassword
ra   FTrb   ©r   r   rl   rq   rm   rn   r   r   r   Útest_parse_file_4  s&    


z"CredentialsTests.test_parse_file_4c                 C   s  d}d}d}t j | jd¡}t|| jƒ}| d¡ | d|||f ¡ | d¡ | d¡ | ¡  | j 	|¡ |  
| j ¡ |¡ |  
| j ¡ |¡ |  
| j ¡ | ¡ ¡ |  
| j ¡ | ¡ ¡ |  
| j ¡ d	|| ¡ f ¡ |  
| j ¡ d
¡ |  
| j ¡ d¡ t  |¡ d S )Nr   rl   rq   r]   rt   rs   ru   rv   ra   FTrb   rw   r   r   r   Útest_parse_file_5%  s&    


z"CredentialsTests.test_parse_file_5c                 C   s¬   t  ¡ }tj ¡ }dtjd< | |¡ | d¡ |  	| 
¡ d¡ |  	| ¡ | d¡ ¡ ¡ |  	| ¡ d ¡ |  	| ¡ d| d¡ ¡  ¡ |  	| ¡ d¡ |  	| ¡ d¡ d S )NrI   rJ   r\   rK   úuser@%sFT)r   r	   rM   rN   rO   rP   rQ   rR   r-   r   r   r$   rS   rT   r(   r%   r5   rF   rU   r   r   r   Útest_parse_username_0<  s    



z&CredentialsTests.test_parse_username_0c                 C   s¼   t  ¡ }tj ¡ }dtjd< | |¡ d}| |t j	¡ | 
d¡ |  | ¡ d¡ |  | ¡ | d¡ ¡ ¡ |  | ¡ | ¡ ¡ |  | ¡ d| ¡  ¡ |  | ¡ d¡ |  | ¡ d¡ d S )	NrI   rJ   rY   r\   rK   rz   FT)r   r	   rM   rN   rO   rP   rQ   rR   r'   ÚUNINITIALISEDr-   r   r   r$   rS   rT   r(   r%   r5   rF   ©r   r
   rV   rj   r   r   r   Útest_parse_username_1I  s    



z&CredentialsTests.test_parse_username_1c                 C   s”   t  ¡ }tj ¡ }dtjd< | |¡ | d¡ |  	| 
¡ d¡ |  	| ¡ d¡ |  	| ¡ d ¡ |  	| ¡ d¡ |  	| ¡ d¡ |  	| ¡ d¡ d S )	NrI   rJ   údomain\userr\   ÚDOMAINúuser@DOMAINFT©r   r	   rM   rN   rO   rP   rQ   rR   r-   r   r   r$   r(   r%   r5   rF   rU   r   r   r   Ú!test_parse_username_with_domain_0X  s    



z2CredentialsTests.test_parse_username_with_domain_0c                 C   sX  t  ¡ }tj ¡ }dtjd< | |¡ d}| |t j	¡ |  
| ¡ d¡ |  
| ¡ | d¡ ¡ ¡ |  
| ¡ | ¡ ¡ |  
| ¡ d| ¡  ¡ | d¡ |  
| ¡ d¡ |  
| ¡ | d¡ ¡ ¡ |  
| ¡ | ¡ ¡ |  
| ¡ d¡ | d¡ |  
| ¡ d¡ |  
| ¡ d	¡ |  
| ¡ | ¡ ¡ |  
| ¡ d
¡ |  
| ¡ d¡ |  
| ¡ d¡ d S ©NrI   rJ   rY   rK   rL   zunknown@realm.example.comr   r\   r€   r   FT)r   r	   rM   rN   rO   rP   rQ   rR   r'   r|   r   r   r$   rS   rT   r(   r%   Úset_principalr-   r5   rF   r}   r   r   r   Ú!test_parse_username_with_domain_1e  s,    




z2CredentialsTests.test_parse_username_with_domain_1c                 C   sT  t  ¡ }tj ¡ }dtjd< | |¡ d}| |t j	¡ |  
| ¡ d¡ |  
| ¡ | d¡ ¡ ¡ |  
| ¡ | ¡ ¡ |  
| ¡ d| ¡  ¡ | d¡ |  
| ¡ d¡ |  
| ¡ | d¡ ¡ ¡ |  
| ¡ | ¡ ¡ |  
| ¡ d¡ | d¡ |  
| ¡ d¡ |  
| ¡ d	¡ |  
| ¡ d	¡ |  
| ¡ d
¡ |  
| ¡ d¡ |  
| ¡ d¡ d S r„   )r   r	   rM   rN   rO   rP   rQ   rR   r'   Z	SPECIFIEDr   r   r$   rS   rT   r(   r%   r…   r-   r5   rF   r}   r   r   r   Ú!test_parse_username_with_domain_2}  s,    




z2CredentialsTests.test_parse_username_with_domain_2c                 C   s”   t  ¡ }tj ¡ }dtjd< | |¡ | d¡ |  	| 
¡ d¡ |  	| ¡ d¡ |  	| ¡ d¡ |  	| ¡ d¡ |  	| ¡ d¡ |  	| ¡ d¡ d S )NrI   rJ   úuser@samba.orgr+   ú	SAMBA.ORGFTr‚   rU   r   r   r   Útest_parse_username_with_realm•  s    



z/CredentialsTests.test_parse_username_with_realmc                 C   s´   t  ¡ }tj ¡ }dtjd< | |¡ | d¡ |  	| 
¡ d¡ |  	| ¡ d¡ |  	| ¡ | d¡¡ |  	| ¡ d ¡ |  	| ¡ d| d¡ ¡ |  	| ¡ d¡ |  	| ¡ d	¡ d S )
NrI   rJ   z	user%passr\   r[   rK   rz   FT)r   r	   rM   rN   rO   rP   rQ   rR   r-   r   r   r   r$   rS   r(   r%   r5   rF   rU   r   r   r   Útest_parse_username_pw¢  s    



z'CredentialsTests.test_parse_username_pwc                 C   s¤   t  ¡ }tj ¡ }dtjd< | |¡ | d¡ |  	| 
¡ d¡ |  	| ¡ d¡ |  	| ¡ d¡ |  	| ¡ d ¡ |  	| ¡ d¡ |  	| ¡ d¡ |  	| ¡ d	¡ d S )
NrI   rJ   zdomain\user%passr\   r€   r[   r   FT©r   r	   rM   rN   rO   rP   rQ   rR   r-   r   r   r$   r   r(   r%   r5   rF   rU   r   r   r   Ú"test_parse_username_with_domain_pw°  s    



z3CredentialsTests.test_parse_username_with_domain_pwc                 C   s¤   t  ¡ }tj ¡ }dtjd< | |¡ | d¡ |  	| 
¡ d¡ |  	| ¡ d¡ |  	| ¡ d¡ |  	| ¡ d¡ |  	| ¡ d¡ |  	| ¡ d¡ |  	| ¡ d	¡ d S )
NrI   rJ   zuser@samba.org%passrˆ   r+   r[   r‰   FTrŒ   rU   r   r   r   Ú!test_parse_username_with_realm_pw¾  s    



z2CredentialsTests.test_parse_username_with_realm_pw)*Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r!   r"   r&   r)   r.   r0   r1   r2   r3   r8   r9   r;   rA   rC   rD   rE   rG   rH   rW   rX   ro   rp   rr   rx   ry   r{   r~   rƒ   r†   r‡   rŠ   r‹   r   rŽ   Ú__classcell__r   r   r   r   r       sL   r   )Ú__doc__rM   r   Zsamba.testsrP   r=   Zsamba.compatr   Zsamba.dcerpcr   rN   ZTestCaseInTempDirr   r   r   r   r   Ú<module>   s   