B
    u9a½  ã               @   sn   d dl Z 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ej	ƒZ
edkrje ¡  dS )é    N)Úmock)Úsupportc               @   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d„Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Ze ejdkd¡d d!„ ƒZd"d#„ Zd$d%„ Zd&d'„ Zd(S )*ÚNetrcTestCasec          	   C   sh   t  |¡}d}tjdkr |d7 }t ¡ \}}tj||d}| |¡ W d Q R X |  	tj
|¡ t |¡S )NÚwÚcygwinÚt)Úmode)ÚtextwrapÚdedentÚsysÚplatformÚtempfileZmkstempÚosÚfdopenÚwriteÚ
addCleanupÚunlinkÚnetrc)ÚselfZ	test_datar   Ztemp_fdZtemp_filenameÚfp© r   ú /usr/lib/python3.7/test_netrc.pyÚmake_nrc   s    

zNetrcTestCase.make_nrcc             C   sP   |   d¡}|  |jd d¡ |  |jd d¡ |   | ¡ ¡}|  |j|j¡ d S )Nz‡            machine host1.domain.com login log1 password pass1 account acct1
            default login log2 password pass2
            zhost1.domain.com)Zlog1Zacct1Zpass1Údefault)Zlog2NZpass2)r   ÚassertEqualÚhostsÚ__repr__)r   ÚnrcZnrc2r   r   r   Útest_default   s    zNetrcTestCase.test_defaultc             C   s*   |   d¡}|  |jddgddgdœ¡ d S )Nz‰            macdef macro1
            line1
            line2

            macdef macro2
            line3
            line4
            zline1
zline2
zline3
zline4
)Zmacro1Zmacro2)r   r   Zmacros)r   r   r   r   r   Útest_macros   s    zNetrcTestCase.test_macrosc             C   s&   |   |¡}|  |jd dd|f¡ d S )Nzhost.domain.comÚlogZacct)r   r   r   )r   r   Úpasswdr   r   r   Ú_test_passwords,   s    
zNetrcTestCase._test_passwordsc             C   s   |   dd¡ d S )NzV            machine host.domain.com login log password #pass account acct
            z#pass)r"   )r   r   r   r   Útest_password_with_leading_hash0   s    z-NetrcTestCase.test_password_with_leading_hashc             C   s   |   dd¡ d S )NzV            machine host.domain.com login log password pass# account acct
            zpass#)r"   )r   r   r   r   Ú test_password_with_trailing_hash5   s    z.NetrcTestCase.test_password_with_trailing_hashc             C   s   |   dd¡ d S )NzV            machine host.domain.com login log password pa#ss account acct
            zpa#ss)r"   )r   r   r   r   Ú test_password_with_internal_hash:   s    z.NetrcTestCase.test_password_with_internal_hashÚpassc             C   s8   |   |¡}|  |jd dd |f¡ |  |jd d¡ d S )Nzfoo.domain.comÚbarzbar.domain.com)ZfooNr&   )r   r   r   )r   r   r!   r   r   r   Ú_test_comment?   s    
zNetrcTestCase._test_commentc             C   s   |   d¡ d S )Nz˜            # comment
            machine foo.domain.com login bar password pass
            machine bar.domain.com login foo password pass
            )r(   )r   r   r   r   Ú test_comment_before_machine_lineD   s    z.NetrcTestCase.test_comment_before_machine_linec             C   s   |   d¡ d S )Nz—            #comment
            machine foo.domain.com login bar password pass
            machine bar.domain.com login foo password pass
            )r(   )r   r   r   r   Ú)test_comment_before_machine_line_no_spaceK   s    z7NetrcTestCase.test_comment_before_machine_line_no_spacec             C   s   |   d¡ d S )Nz            #
            machine foo.domain.com login bar password pass
            machine bar.domain.com login foo password pass
            )r(   )r   r   r   r   Ú*test_comment_before_machine_line_hash_onlyR   s    z8NetrcTestCase.test_comment_before_machine_line_hash_onlyc             C   s   |   d¡ d S )NzŒ            machine foo.domain.com login bar password pass # comment
            machine bar.domain.com login foo password pass
            )r(   )r   r   r   r   Ú#test_comment_at_end_of_machine_lineY   s    z1NetrcTestCase.test_comment_at_end_of_machine_linec             C   s   |   d¡ d S )Nz‹            machine foo.domain.com login bar password pass #comment
            machine bar.domain.com login foo password pass
            )r(   )r   r   r   r   Ú,test_comment_at_end_of_machine_line_no_space_   s    z:NetrcTestCase.test_comment_at_end_of_machine_line_no_spacec             C   s   |   dd¡ d S )NzŒ            machine foo.domain.com login bar password #pass #comment
            machine bar.domain.com login foo password pass
            z#pass)r(   )r   r   r   r   Ú1test_comment_at_end_of_machine_line_pass_has_hashe   s    z?NetrcTestCase.test_comment_at_end_of_machine_line_pass_has_hashÚposixzPOSIX only testc          	   C   s²   t j}t |¡ |  t j|¡ tj |d¡}t|dƒ}| 	d¡ W d Q R X t  
¡ T}| d|¡ t |d¡ t ¡ }|  |jd d¡ t |d¡ |  tjtj¡ W d Q R X d S )	Nz.netrcZwtz                machine foo.domain.com login bar password pass
                default login foo password pass
                ÚHOMEi€  zfoo.domain.com)r'   Nr&   i’  )r   ÚTESTFNr   Úmkdirr   ÚrmtreeÚpathÚjoinÚopenr   ÚEnvironmentVarGuardÚsetÚchmodr   r   r   ÚassertRaisesZNetrcParseError)r   ÚdÚfnÚfÚenvironr   r   r   r   Útest_securityl   s    

zNetrcTestCase.test_securityc          	   C   sP   t j}t |¡ |  t j|¡ t  ¡  }| d|¡ |  t	t
j
¡ W d Q R X d S )Nr0   )r   r1   r   r2   r   r3   r7   r8   r:   ÚFileNotFoundErrorr   )r   r;   r>   r   r   r   Útest_file_not_found_in_home‚   s    

z)NetrcTestCase.test_file_not_found_in_homec             C   s   | j ttjdd d S )NZunlikely_netrc)Úfile)r:   r@   r   )r   r   r   r   Útest_file_not_found_explicitŠ   s    
z*NetrcTestCase.test_file_not_found_explicitc          	      sÂ   t j‰t ˆ¡ |  t jˆ¡ tj ˆd¡}t|dƒ}| 	d¡ W d Q R X t 
|d¡ tjj‰g ‰ ‡ ‡‡fdd„}t  tjd|¡* t ¡ }| d¡\}}}|  |d	¡ W d Q R X |  ˆ ¡ d S )
Nz.netrcr   z.machine foo.domain.com login bar password passi€  c          	      s6   ˆ   | ¡ t ¡ }| dˆ¡ ˆ| ƒ}|S Q R X d S )Nr0   )Úappendr   r7   r8   )Úsr>   Úresult)ÚcalledÚ	fake_homeÚorig_expanduserr   r   Úfake_expanduserš   s
    

z8NetrcTestCase.test_home_not_set.<locals>.fake_expanduserÚ
expanduserzfoo.domain.comr'   )r   r1   r   r2   r   r3   r4   r5   r6   r   r9   rK   Z	swap_attrr   Zauthenticatorsr   Z
assertTrue)r   Zfake_netrc_pathr=   rJ   r   ZloginZaccountZpasswordr   )rG   rH   rI   r   Útest_home_not_setŽ   s    
zNetrcTestCase.test_home_not_setN)r&   )Ú__name__Ú
__module__Ú__qualname__r   r   r   r"   r#   r$   r%   r(   r)   r*   r+   r,   r-   r.   ÚunittestZ
skipUnlessr   Únamer?   rA   rC   rL   r   r   r   r   r      s$   
r   Ú__main__)r   r   rP   r   r   r	   r   Ztestr   ZTestCaser   rM   Úmainr   r   r   r   Ú<module>   s   0 $