a
    )&i@                     @   sX   d dl Z d dlZd dlmZ d dlmZ d dlmZmZ d dl	m
Z
 G dd deZdS )    N)SambaToolCmdTest)dsdb)
ndr_unpackndr_pack)dnspc                       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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  ZS )#ComputerCmdTestCasez)Tests for samba-tool computer subcommandsNc           
         s  t t   dtjd tjd f  _ ddtjd   j _d _d _	 
  }|d	  jgd
|d  j	gd| d gd|d  j j	gd| d d| d gdd|d ig} fdd|D  _ jD ]} |\}}} |||  d|d  d|d  |  |d } | |d d}|d }	|d dshd|d  }	 d|d |  d|d |	  d|d |d  qd S )Nz-U%s%%%sZDC_USERNAMEZDC_PASSWORDz-Hz	ldap://%sZ	DC_SERVERz10.10.10.10z'2001:0db8:0a0b:12f0:0000:0000:0000:0001Zcmp1)nameip_address_listZcmp2zhost/ZSPN20)r   r	   service_principal_name_listzcmp3$ZSPN30ZSPN31r   zcmp4$c                    s   g | ]} j |d qS ))base)_randomComputer).0itemself A/usr/lib/python3/dist-packages/samba/tests/samba_tool/computer.py
<listcomp>C       z-ComputerCmdTestCase.setUp.<locals>.<listcomp>ZERROR$There shouldn't be any error message"Computer '%s' created successfully$%s$%ssAMAccountNamedescription)superr   setUposenvironcredsZgetSamDBsamdbZipv4Zipv6
randomNamelower	computers_create_computerassertCmdSuccessZassertNotInassertIn_find_computerZassertIsNotNonerstripendswithassertEqualget)
r   Zcomputer_basenamedatacomputerresultouterrfoundexpectednameexpectedsamaccountname	__class__r   r   r   #   s`    



	

zComputerCmdTestCase.setUpc              	      s`   t t|   | jD ]F}| |d r| ddd|d  \}}}| |||d|d   qd S )Nr   r.   deleter   Failed to delete computer '%s')r   r   tearDownr$   r(   	runsubcmdr&   )r   r.   r/   r0   r1   r5   r   r   r9   ]   s    



zComputerCmdTestCase.tearDownc                 C   s6   | j D ]*}|dg }| |d |}| | qd S )Nr
   r   )r$   r,   _find_service_principal_name
assertTrue)r   r.   Zexpected_namesr2   r   r   r   ,test_newcomputer_with_service_principal_nameh   s    
z@ComputerCmdTestCase.test_newcomputer_with_service_principal_namec              	   C   s   | j D ]0}|dg D ]}| |d |}| | qq| j D ]Z}| ddd|d  \}}}| |||d|d   | |d }| |d|d   q>| j D ]0}|dg D ]}| |d |}| | qqd S )Nr	   r   r.   r7   r   r8   "Deleted computer '%s' still exists)	r$   r,   _find_dns_recordr<   r:   r&   r(   assertIsNoneZassertFalse)r   r.   
ip_addressr2   r/   r0   r1   r   r   r   !test_newcomputer_with_dns_recordso   s.    





z5ComputerCmdTestCase.test_newcomputer_with_dns_recordsc              	   C   s  | j D ],}| |\}}}| |d | d| q| j D ]Z}| ddd|d  \}}}| |||d|d   | |d }| |d|d   q:| j D ]}| dd	d|d  d
|d  \}}}| ||| | |dd | d|d  | | |d }|d 	d}|d }|d 
ds6d|d  }| d|d | | d|d | | d|d |d  qdS )z?This tests the "computer create" and "computer delete" commandsz%Succeeded to create existing computerzalready existsr.   r7   r   r   r8   r>   create--description=%sr    r   r   r   r   r   N)r$   r%   ZassertCmdFailr'   r:   r&   r(   r@   r+   r)   r*   r,   )r   r.   r/   r0   r1   r2   r3   r4   r   r   r   test_newcomputer   sV    






z$ComputerCmdTestCase.test_newcomputerc           	      C   s   |  dd\}}}| |||d dtj }| jj| j tj|dgd}| 	t
|dkd |D ](}|jddd	}| |t|d
| }qbd S )Nr.   listError running list(sAMAccountType=%u)samaccountnamer   scope
expressionattrsr   no computers found in samdbidxcomputer '%s' not foundr:   r&   r   ATYPE_WORKSTATION_TRUSTr!   search	domain_dnldbSCOPE_SUBTREEr<   lenr,   assertMatchstr	r   r/   r0   r1   search_filtercomputerlistcomputerobjr   r2   r   r   r   	test_list   s     zComputerCmdTestCase.test_listc           	      C   s   |  ddd\}}}| |||d dtj }| jj| j tj|g d}| 	t
|dkd |D ](}|jd	dd
}| |t|d| }qbd S )Nr.   rG   z	--full-dnrH   rI   rK   r   rO   dnrP   rR   rS   r\   r   r   r   test_list_full_dn   s     z%ComputerCmdTestCase.test_list_full_dnc           
      C   s   t | j }| ddd|\}}}| |||d dtj }| jj|tj	|dgd}| 
t|dkd	 |D ](}|jddd
}| |t |d| }	qnd S )Nr.   rG   z-brH   rI   r   rK   r   rO   rP   rR   )r[   r!   rV   r:   r&   r   rT   rU   rW   rX   r<   rY   r,   rZ   )
r   Zbase_dnr/   r0   r1   r]   r^   r_   r   r2   r   r   r   test_list_base_dn   s"    z%ComputerCmdTestCase.test_list_base_dnc           
   	   C   s  |  ddi}| |\}}}| ||| | jD ],}| |d d}| ddd|d  d|d  \}}}| |||d|d   | |d	d
 | d|d  | | |d }| 	|d|d|d   |d 
d}t| jd||d | j f }	| |d|	d|d   | ddd|d  d|  \}}}| |||d|d   q2| ddd|d  \}}}| |||d|d   d S )Nr   ZparentOUra   r.   Zmover   OU=%szFailed to move computer '%s'rE   r   zMoved computer "%s"z1Moved computer '%s' still exists with the same dnr   zCN=%s,OU=%s,%sz"Moved computer '%s' does not existour7   zFailed to delete ou '%s')	_randomOU
_create_our&   r$   r(   r,   r:   r+   r'   ZassertNotEqualsr)   rW   ZDnr!   rV   parent)
r   Zparentour/   r0   r1   r.   Zolddnr2   ZcomputernameZnewexpecteddnr   r   r   	test_move   sd    











zComputerCmdTestCase.test_movec                 C   s$   |   | j ddd}|| |S )zWcreate a computer with random attribute values, you can specify base
        attributesd   countr   r   r"   update)r   r   r.   r   r   r   r     s
    

z#ComputerCmdTestCase._randomComputerc                 C   s$   |   | j ddd}|| |S )zRcreate an ou with random attribute values, you can specify base
        attributesrj   rk   rm   rn   )r   r   re   r   r   r   rf   #  s
    

zComputerCmdTestCase._randomOUc                 C   st   d |d | j|d }|dg D ]}|d |7 }q$|dg D ]}|d |7 }qD| }| jdd	g|R  S )
Nz{0} {1} --description={2}r   r   r	   z --ip-address={0}r
   z --service-principal-name={0}r.   rC   )formatr    r,   splitr:   )r   r.   argsrA   Zservice_principal_namer   r   r   r%   .  s    z$ComputerCmdTestCase._create_computerc                 C   s    |  ddd|d  d|d  S )Nre   rC   rd   r   rD   r   )r:   )r   re   r   r   r   rg   <  s    
zComputerCmdTestCase._create_ouc                 C   s^   |}| dsd| }dt|d| j f }| jj| j tj|d}|rV|d S d S d S )Nr   r   ,(&(sAMAccountName=%s)(objectCategory=%s,%s))&CN=Computer,CN=Schema,CN=Configuration)r   rL   rM   r   )r*   rW   binary_encoder!   rV   rU   rX   )r   r   rJ   r]   r^   r   r   r   r(   @  s     
z"ComputerCmdTestCase._find_computerc                 C   s   | d}| jjd| j tjd|ddgd}|D ]Z}d|v rXt|d dkrXq:|d D ]2}tt	j
|}t|j}t|t|kr`  dS q`q:d	S )
Nr   zDC=DomainDnsZones,{0}z"(&(objectClass=dnsNode)(name={0}))Z	dnsRecordZdNSTombstonedrK   ZTRUETF)r)   r!   rU   rp   Zget_default_basednrW   rX   r[   r   r   ZDnssrvRpcRecordr-   )r   r   rA   ZrecordsrecordZdns_record_binZdns_record_objZipr   r   r   r?   P  s     

z$ComputerCmdTestCase._find_dns_recordc                 C   s   | dd }dt|d| j f }| jj| j tj|dgd}t }|D ]$}|dg D ]}|	t
| q`qP|t|kS )zVFind all servicePrincipalName values and compare with expected_service_principal_namesr   rs   rt   ZservicePrincipalNamerK   )striprW   ru   r!   rV   rU   rX   setr,   addr[   )r   r   Z expected_service_principal_namesrJ   r]   Zcomputer_listnamesr.   r   r   r   r;   f  s$    z0ComputerCmdTestCase._find_service_principal_name)__name__
__module____qualname____doc__r$   r!   r   r9   r=   rB   rF   r`   rb   rc   ri   r   rf   r%   rg   r(   r?   r;   __classcell__r   r   r5   r   r      s&   :+*r   )r   rW   Zsamba.tests.samba_tool.baser   Zsambar   Z	samba.ndrr   r   Zsamba.dcerpcr   r   r   r   r   r   <module>   s   