a
     ’{aÀ  ã                   @   sØ   d Z ddlZddlZddlZddlmZmZ ddlm	Z	 ddlm
Z
mZ ddlmZmZmZmZmZ ddlmZ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Zdd„ ZG dd„ dejjƒZdS )zHTest whether various python calls segfault when given unexpected input.
é    N)ÚNetÚLIBNET_JOIN_AUTOMATIC)ÚDONT_USE_KERBEROS)ÚNTSTATUSErrorÚntstatus)ÚmiscÚdrsuapiÚsamrÚunixinfoÚ	dnsserver)ÚauthÚgensec)ÚSamDB)Únetbios)Úregistry)Úldb)Ú	messagingc                    s   ‡ fdd„}|S )Nc               
      sª   t  ¡ }|dkrnzˆ | i |¤Ž W n, tyN } zt ¡  W Y d }~n
d }~0 0 tj ¡  tj ¡  t  	d¡ t  
|d¡\}}|d@ }t  |¡r¦t  |¡}td| ƒ‚d S )Nr   éÿ   zFailed with signal %d)ÚosÚforkÚ	ExceptionÚ	tracebackÚ	print_excÚsysÚstderrÚflushÚstdoutÚ_exitÚwaitpidÚWIFSIGNALEDÚWTERMSIGÚAssertionError)ÚargsÚkwargsÚpidÚeZpid2ZstatusÚsignal©Úf© ú6/usr/lib/python3/dist-packages/samba/tests/segfault.pyÚwrapper(   s    




z"segfault_detector.<locals>.wrapperr)   )r(   r+   r)   r'   r*   Úsegfault_detector'   s    r,   c                   @   s  e Zd Zdd„ Zdd„ Zedd„ ƒZedd„ ƒZed	d
„ ƒZedd„ ƒZ	edd„ ƒZ
edd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd „ ƒZed!d"„ ƒZed#d$„ ƒZed%d&„ ƒZed'd(„ ƒZed)d*„ ƒZed+d,„ ƒZd-S ).ÚSegfaultTestsc                 C   s.   t jd }|  ¡ }| j|  ¡ td}|||fS )NZSERVER)ÚtemplateZkerberos_state)r   ÚenvironZget_loadparmZinsta_credsZget_credentialsr   )ÚselfÚserverÚlpÚcredsr)   r)   r*   Úget_lp_et_al=   s    

ÿzSegfaultTests.get_lp_et_alc                 C   s(   |   ¡ \}}}d| }t|||d}|S )Nzldap://)Úcredentialsr2   )r4   r   )r0   r2   r3   r1   Zurlr   r)   r)   r*   Ú	get_samdbE   s    zSegfaultTests.get_samdbc                 C   s4   |   ¡ \}}}t|||d}| d|d t ¡ ¡ d S ©N©r1   é*   )r4   r   Úreplicate_initr   ÚGUID)r0   r2   r3   r1   Únetr)   r)   r*   Útest_net_replicate_init__1K   s    z(SegfaultTests.test_net_replicate_init__1c                 C   s<   |   ¡ }|  ¡ \}}}t|||d}| ||dt ¡ ¡ d S r7   )r6   r4   r   r:   r   r;   )r0   Úsamdbr2   r3   r1   r<   r)   r)   r*   Útest_net_replicate_init__3Q   s    z(SegfaultTests.test_net_replicate_init__3c                 C   s6   |   ¡ \}}}t ¡ }t|||d}| dd|¡ d S )Nr8   r9   é   )r4   r   ZDsGetNCChangesCtr6r   Zreplicate_chunk)r0   r2   r3   r1   Zctrr<   r)   r)   r*   Útest_net_replicate_chunk_1Y   s    z(SegfaultTests.test_net_replicate_chunk_1c                 C   s"   t jddgd}tjj|d}d S )Nr9   Zsam)r   Úmethods©Zauth_context)r   ZAuthContextr   ÚSecurityÚstart_server)r0   ÚaÚgr)   r)   r*   Ú%test_auth_context_gensec_start_server`   s    z3SegfaultTests.test_auth_context_gensec_start_serverc                 C   s   t jddd}d S )Nr9   Úfoo)r   Z	principal)r   Zuser_session)r0   Úsr)   r)   r*   Útest_auth_user_sessionh   s    z$SegfaultTests.test_auth_user_sessionc                 C   s   t jjdd d S )Nr9   rC   )r   rD   rE   ©r0   r)   r)   r*   Útest_gensec_start_serverl   s    z&SegfaultTests.test_gensec_start_serverc                 C   s   t  ¡ }| dd¡}d S )N)r9   rI   Z	localhost)r   ZNodeZ
query_name)r0   ÚnÚtr)   r)   r*   Útest_netbios_query_namep   s    z%SegfaultTests.test_netbios_query_namec                 C   s   |   ¡ \}}}| d¡ d S )Nr9   )r4   Zencrypt_netr_crypt_password)r0   r2   r3   r1   r)   r)   r*   Ú test_encrypt_netr_crypt_passwordu   s    z.SegfaultTests.test_encrypt_netr_crypt_passwordc              
   C   sJ   zt jddd W n2 tjyD } ztd| ƒ W Y d }~n
d }~0 0 d S )NÚ r9   )r5   úfailed with %s)r   Zopen_ldbr   ÚLdbErrorÚprint©r0   r%   r)   r)   r*   Útest_hive_open_ldbz   s    z SegfaultTests.test_hive_open_ldbc              
   C   sL   zt  dddd¡ W n2 tjyF } ztd| ƒ W Y d }~n
d }~0 0 d S )NrJ   rS   )r   Z	open_hiver   rT   rU   rV   r)   r)   r*   Útest_hive_open_hive‚   s    z!SegfaultTests.test_hive_open_hivec                 C   sB   t  ¡ }t  d¡}z| |¡ W n t jy4   Y n0 t|ƒ d S )NÚq)r   ÚMessageZMessageElementÚaddrT   Ústr)r0   Úmr%   r)   r)   r*   Útest_ldb_add_nameless_elementŠ   s    
z+SegfaultTests.test_ldb_add_nameless_elementc                 C   s   t  d¡ d S ©NrR   )r   Zregister_modulerL   r)   r)   r*   Útest_ldb_register_module”   s    z&SegfaultTests.test_ldb_register_modulec                 C   s   t  dddd¡ d S )NrJ   F)r   Z
deregisterrL   r)   r)   r*   Útest_messaging_deregister˜   s    z'SegfaultTests.test_messaging_deregisterc                 C   s   ddl m} | d¡ d S )Nr   )ÚechorR   )Zdcerpcrb   Zrpcecho)r0   rb   r)   r)   r*   Útest_rpcechoœ   s    zSegfaultTests.test_rpcechoc                 C   s   dt  ¡ _dS )z–There are many pidl generated functions that crashed on this
        pattern, where a NULL pointer was created rather than an empty
        structure.r@   N)r	   ZConnect5Zout_info_outrL   r)   r)   r*   Útest_dcerpc_idl_ref_elements¡   s    z*SegfaultTests.test_dcerpc_idl_ref_elementsc                 C   s   t  ¡ j dS )z$Dereferencing is sufficient to crashN)r
   ZGetPWUidZ	out_infosrL   r)   r)   r*   Ú!test_dcerpc_idl_unixinfo_elements¨   s    z/SegfaultTests.test_dcerpc_idl_unixinfo_elementsc                 C   s   t  ¡ j dS )z'Inline arrays were incorrectly handled.N)r   ZDNS_RPC_SERVER_INFO_DOTNETZpExtensionsrL   r)   r)   r*   Útest_dcerpc_idl_inline_arrays­   s    z+SegfaultTests.test_dcerpc_idl_inline_arraysc                 C   s6   |   ¡ }t ¡ }t |d¡|_| ||¡}~|j d S r_   )r6   r   rZ   ZDnÚdnZmsg_diff)r0   r>   ÚmsgZdiffr)   r)   r*   Útest_ldb_msg_diff²   s    zSegfaultTests.test_ldb_msg_diffc                 C   s   t  ¡ }|`d S )N)r   rZ   rg   )r0   rh   r)   r)   r*   Útest_ldb_msg_del_dn½   s    z!SegfaultTests.test_ldb_msg_del_dnc                 C   s   |   ¡ }t |d¡}|`d S )Nzrelax:1)r6   r   ZControlZcritical)r0   r>   Úcr)   r)   r*   Útest_ldb_control_del_criticalÂ   s    z+SegfaultTests.test_ldb_control_del_criticalN)Ú__name__Ú
__module__Ú__qualname__r4   r6   r,   r=   r?   rA   rH   rK   rM   rP   rQ   rW   rX   r^   r`   ra   rc   rd   re   rf   ri   rj   rl   r)   r)   r)   r*   r-   <   sT   










	








r-   )Ú__doc__Zsamba.testsZsambar   r   Z	samba.netr   r   Zsamba.credentialsr   r   r   Zsamba.dcerpcr   r   r	   r
   r   r   r   Zsamba.samdbr   r   r   r   r   r   r,   ZtestsZTestCaser-   r)   r)   r)   r*   Ú<module>   s    