a
    W×a§N  ã                   @   sH   d dl Z d dlZd dlZd dlmZ d dlmZmZ G dd„ deƒZdS )é    N)ÚSambaToolCmdTest)Únttime2unixÚdsdbc                       sà   e Zd ZdZg ZdZ‡ fd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i fdd„Zi fdd„Zi 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‡  ZS )/ÚGroupCmdTestCasez&Tests for samba-tool group subcommandsNc                    s\  t t| ƒ ¡  |  ddtjd  dtjd tjd f ¡| _g | _| j |  	ddi¡¡ | j |  	dd	i¡¡ | j |  	dd
i¡¡ | j |  	ddi¡¡ | j |  	ddi¡¡ | j |  
ddi¡¡ | j |  
ddi¡¡ | j |  
ddi¡¡ | j |  
ddi¡¡ | j |  
ddi¡¡ | j |  ddi¡¡ | j |  ddi¡¡ | j |  ddi¡¡ | j |  ddi¡¡ | j |  ddi¡¡ | jD ]Ä}|d |ƒ\}}}|  |||¡ |  |dd¡ d|d v rè|  d|d  |¡ n|  d|d  |¡ |d |ƒ |  |d ¡}|  |¡ |  d| d¡ |d ¡ |  d| d¡ |d ¡ q’d S ) Nú-Hú	ldap://%sÚ	DC_SERVERú-U%s%%%sÚDC_USERNAMEÚDC_PASSWORDÚnameZ
testgroup1Z
testgroup2Z
testgroup3Z
testgroup4ztestgroup5 (with brackets)Zposixgroup1Zposixgroup2Zposixgroup3Zposixgroup4zposixgroup5 (with brackets)Z
unixgroup1Z
unixgroup2Z
unixgroup3Z
unixgroup4zunixgroup5 (with brackets)ÚcreateGroupFnÚ ú$There shouldn't be any error messageZunixz Modified Group '%s' successfullyúAdded group %sÚcheckGroupFnú%sÚdescription)Úsuperr   ÚsetUpZgetSamDBÚosÚenvironÚsamdbÚgroupsÚappendÚ_randomGroupÚ_randomPosixGroupÚ_randomUnixGroupÚassertCmdSuccessÚassertEqualÚassertInÚ_find_groupZassertIsNotNoneÚget©ÚselfÚgroupÚresultÚoutÚerrÚfound©Ú	__class__© ú>/usr/lib/python3/dist-packages/samba/tests/samba_tool/group.pyr   !   sH    ÿ
ÿÿ
zGroupCmdTestCase.setUpc                    s>   t t| ƒ ¡  | jD ]$}|  |d ¡r|  dd|d ¡ qd S )Nr   r%   Údelete)r   r   ÚtearDownr   r!   Ú	runsubcmd©r$   r%   r*   r,   r-   r/   L   s    
zGroupCmdTestCase.tearDownc                 C   sD  | j D ],}|  |¡\}}}|  |d¡ |  d|¡ q| j D ]V}|  dd|d ¡\}}}|  |||d|d  ¡ |  |d ¡}|  |d|d  ¡ q:| j D ]¦}|  dd|d d	|d
  ddtj	d  dtj	d tj	d f ¡\}}}|  |||¡ |  
|dd¡ |  d|d  |¡ |  |d ¡}|  
d| d¡ d|d  ¡ q˜dS )z6This tests the "group add" and "group delete" commandsz"Succeeded to create existing groupz'LDAP error 68 LDAP_ENTRY_ALREADY_EXISTSr%   r.   r   zFailed to delete group '%s'zDeleted group '%s' still existsÚaddú--description=%sr   r   r   r   r	   r
   r   r   r   r   r   ÚsamaccountnameN)r   Ú_create_groupÚassertCmdFailr    r0   r   r!   ZassertIsNoner   r   r   r"   r#   r,   r,   r-   Útest_newgroupS   s8    



ÿ
ÿ


ÿý

ÿzGroupCmdTestCase.test_newgroupc           	   
   C   s®   |   ddddtjd  dtjd tjd f ¡\}}}|  |||d	¡ d
}| jj| j ¡ tj|dgd}|  	t
|ƒdkd¡ |D ](}t|jdddƒ}|  ||d| ¡}q€d S )Nr%   Úlistr   r   r   r	   r
   r   úError running listú(objectClass=group)r4   ©ÚbaseÚscopeÚ
expressionÚattrsr   úno groups found in samdb©Úidxúgroup '%s' not found©r0   r   r   r   r   ÚsearchÚ	domain_dnÚldbÚSCOPE_SUBTREEÚ
assertTrueÚlenÚstrr"   ÚassertMatch©	r$   r&   r'   r(   Úsearch_filterÚ	grouplistÚgroupobjr   r)   r,   r,   r-   Ú	test_listu   s&    
ÿþ
ýÿzGroupCmdTestCase.test_listc                 C   s   |   dddddtjd  dtjd tjd	 f ¡\}}}|  |||d
¡ i }| d¡dd… }|D ]*}| d¡}|d }t|d ƒ}	|	||< qbd}
| jj| j ¡ t	j
|
ddgd}|  t|ƒdkd¡ i }|D ]0}t|jdddƒ}t|jdg dƒ}	|	||< qÎ|  ||kdd||f  ¡ d S )Nr%   r8   z	--verboser   r   r   r	   r
   r   zError running list --verboseÚ
é   éÿÿÿÿz   r   r:   r4   Úmemberr;   r@   rA   )Údefaultz+Command output doesn't match LDAP results.
zCommand='%s'
LDAP='%s')r0   r   r   r   ÚsplitÚintr   rE   rF   rG   rH   rI   rJ   rK   r"   )r$   r&   r'   r(   Zoutput_membershipsZgroup_linesÚlineÚvaluesr   Znum_membersrN   rO   Zldap_membershipsrP   r,   r,   r-   Útest_list_verboseŠ   sB    

ÿþ


ý

ÿÿÿz"GroupCmdTestCase.test_list_verbosec           	      C   s®   |   dddddtjd  dtjd tjd	 f ¡\}}}|  |||d
¡ d}| jj| j ¡ tj|g d}|  	t
|ƒdkd¡ |D ](}t|jdddƒ}|  ||d| ¡}q€d S )Nr%   r8   ú	--full-dnr   r   r   r	   r
   r   r9   r:   r;   r   r@   ÚdnrA   rC   rD   rM   r,   r,   r-   Útest_list_full_dn³   s&    

ÿþ
ýÿz"GroupCmdTestCase.test_list_full_dnc           
      C   s¸   d}|   ddd|ddtjd  dtjd	 tjd
 f ¡\}}}|  |||d¡ d}| jj| j |¡tj|dgd}|  	t
|ƒdkd¡ |D ](}t|jdddƒ}|  ||d| ¡}	qŠd S )NzCN=Usersr%   r8   z	--base-dnr   r   r   r	   r
   r   r9   r:   r   r;   r   r@   rA   rC   )r0   r   r   r   r   rE   Únormalize_dn_in_domainrG   rH   rI   rJ   rK   r"   rL   )
r$   Zbase_dnr&   r'   r(   rN   rO   rP   r   r)   r,   r,   r-   Útest_list_base_dnÈ   s(    
ÿþ
ýÿz"GroupCmdTestCase.test_list_base_dnc           	      C   sº   |   dddddtjd  dtjd tjd	 f ¡\}}}|  |||d
¡ d| j ¡  }| jj| j ¡ tj|dgd}|  	t
|ƒdkd¡ |D ](}t|jdddƒ}|  ||d| ¡}qŒd S )Nr%   ÚlistmembersúDomain Usersr   r   r   r	   r
   r   úError running listmembersú=(|(primaryGroupID=513)(memberOf=CN=Domain Users,CN=Users,%s))ZsamAccountNamer;   r   r@   rA   rC   ©r0   r   r   r   r   rF   rE   rG   rH   rI   rJ   rK   r"   rL   rM   r,   r,   r-   Útest_listmembersÞ   s"    

ÿþ
ýz!GroupCmdTestCase.test_listmembersc           	      C   s¼   |   ddddddtjd  dtjd	 tjd
 f ¡\}}}|  |||d¡ d| j ¡  }| jj| j ¡ tj|dgd}|  	t
|ƒdkd¡ |D ](}t|jdddƒ}|  ||d| ¡}qŽd S )Nr%   ra   rb   r\   r   r   r   r	   r
   r   rc   rd   r]   r;   r   r@   rA   rC   re   rM   r,   r,   r-   Útest_listmembers_full_dnó   s$    

ÿý
ýz)GroupCmdTestCase.test_listmembers_full_dnc                 C   sZ  t | j d¡ƒ}|  dd|¡\}}}|  |||¡ |  |dd¡ |  d| |¡ | jD ]F}|  dd|d	 |¡\}}}|  |||d
¡ |  d|d	 |f |¡ qV|  dd|¡\}}}|  |¡ |  dt	| jƒ |¡ | jD ]T}d| j 
¡  }|  dd|d	 |¡\}}}|  |||d
¡ |  d|d	 |f |¡ qØ|  dd|¡\}}}|  |||d| ¡ d S )NzOU=movetestZouZcreater   r   zCreated ou "%s"r%   Zmover   zError running movezMoved group "%s" into "%s"r.   zFsubtree_delete: Unable to delete a non-leaf node (it has %d children)!zCN=Users,%szFailed to delete ou '%s')rK   r   r_   r0   r   r   r    r   r6   rJ   rF   )r$   Z
full_ou_dnr&   r'   r(   r%   Znew_dnr,   r,   r-   Ú	test_move	  sJ    
ÿ

ÿÿ
ÿÿ
ÿ

ÿÿ
ÿzGroupCmdTestCase.test_movec                 C   sd   |   dddddtjd  dtjd tjd	 f ¡\}}}|  |||¡ |  |d
d¡ |  d|¡ dS )z*Assert that we can show a group correctly.r%   Zshowrb   r   r   r   r	   r
   r   r   zShouldn't be any error messagesz@dn: CN=Domain Users,CN=Users,DC=addom,DC=samba,DC=example,DC=comN)r0   r   r   r   r   r    )r$   r&   r'   r(   r,   r,   r-   Ú	test_show)  s    

ÿþ
zGroupCmdTestCase.test_showc                 C   s,   |   ¡ | j dd| j| jdœ}| |¡ |S )zMcreate a group with random attribute values, you can specify base
 attributeséd   )Úcount)r   r   r   r   )Ú
randomNamer5   Ú_check_groupÚupdate)r$   r<   r%   r,   r,   r-   r   3  s    
ü
zGroupCmdTestCase._randomGroupc                 C   sF   |   i ¡}| |¡ |  ¡ |  ¡ | j| jdœ}| |¡ | |¡ |S )úvcreate a group with random attribute values and additional RFC2307
        attributes, you can specify base attributes)Ú
unixdomainÚ	gidNumberr   r   )r   rn   rl   Ú	randomXidÚ_create_posix_groupÚ_check_posix_group©r$   r<   r%   ZposixAttributesr,   r,   r-   r   ?  s    

ü

z"GroupCmdTestCase._randomPosixGroupc                 C   s@   |   i ¡}| |¡ |  ¡ | j| jdœ}| |¡ | |¡ |S )ro   )rq   r   r   )r   rn   rr   Ú_create_unix_groupÚ_check_unix_groupru   r,   r,   r-   r   N  s    

ý

z!GroupCmdTestCase._randomUnixGroupc                 C   sF   |   |d ¡}|  d| d¡ |d ¡ |  d| d¡ |d ¡ dS )zF check if a group from SamDB has the same attributes as
 its template r   r   r   N)r!   r   r"   ©r$   r%   r)   r,   r,   r-   rm   \  s    zGroupCmdTestCase._check_groupc                 C   s:   |   |d ¡}|  d| d¡ d|d  ¡ |  |¡ dS )zL check if a posix_group from SamDB has the same attributes as
 its template r   r   rq   N©r!   r   r"   rm   rx   r,   r,   r-   rt   d  s
    ÿz#GroupCmdTestCase._check_posix_groupc                 C   s:   |   |d ¡}|  d| d¡ d|d  ¡ |  |¡ dS )zJ check if a unix_group from SamDB has the same attributes as its
template r   r   rq   Nry   rx   r,   r,   r-   rw   m  s
    ÿz"GroupCmdTestCase._check_unix_groupc                 C   s@   |   dd|d d|d  ddtjd  d	tjd
 tjd f ¡S )Nr%   r2   r   r3   r   r   r   r   r	   r
   r   ©r0   r   r   r1   r,   r,   r-   r5   v  s    

ÿýzGroupCmdTestCase._create_groupc                 C   sT   |   dd|d d|d  d|d  d|d	  d
dtjd  dtjd tjd f ¡	S )z, create a new group with RFC2307 attributes r%   r2   r   r3   r   z--nis-domain=%srp   z--gid-number=%srq   r   r   r   r	   r
   r   rz   r1   r,   r,   r-   rs   }  s    



ÿûz$GroupCmdTestCase._create_posix_groupc                 C   sJ   |   |¡ |  dd|d d|d  ddtjd  d	tjd
 tjd f ¡S )z" Add RFC2307 attributes to a groupr%   Zaddunixattrsr   r   rq   r   r   r   r	   r
   r   )r5   r0   r   r   r1   r,   r,   r-   rv   ‡  s    


ÿýz#GroupCmdTestCase._create_unix_groupc                 C   sH   dt  |¡d| j ¡ f }| jj| j ¡ t j|d}|r@|d S d S d S )Nz,(&(sAMAccountName=%s)(objectCategory=%s,%s))z#CN=Group,CN=Schema,CN=Configuration)r<   r=   r>   r   )rG   Zbinary_encoder   rF   rE   rH   )r$   r   rN   rO   r,   r,   r-   r!     s    þÿþzGroupCmdTestCase._find_groupc              
   C   sˆ   |   ddddtjd  dtjd tjd f ¡\}}}|  |||d	¡ d
}| jj| j ¡ tj|g d}t	|ƒ}|  
d |¡|v d¡ d S )Nr%   Zstatsr   r   r   r	   r
   r   zError running statsr:   r;   zTotal groups: {0}z#Total groups not reported correctly)r0   r   r   r   r   rE   rF   rG   rH   rJ   rI   Úformat)r$   r&   r'   r(   rN   rO   Ztotal_groupsr,   r,   r-   Ú
test_stats  s"    
ÿþ
ýÿzGroupCmdTestCase.test_stats)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r/   r7   rQ   r[   r^   r`   rf   rg   rh   ri   r   r   r   rm   rt   rw   r5   rs   rv   r!   r|   Ú__classcell__r,   r,   r*   r-   r      s2   +") 
		
	r   )	r   ÚtimerG   Zsamba.tests.samba_tool.baser   Zsambar   r   r   r,   r,   r,   r-   Ú<module>   s
   