o
    ¯b  ã                   @   s¾   d Z ddl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mZ dd	lmZ d
ZdZedƒrCedƒrCddlmZ ndZdZeedƒsPdZdZeeeƒG dd„ deƒƒZdS )z,
Tests for L{twisted.conch.openssh_compat}.
é    N)ÚskipIf)ÚgetDHGeneratorAndPrime)Úkeydata)ÚFilePath)ÚrequireModule)ÚMockOS)ÚTestCaseFÚ ÚcryptographyÚpyasn1)ÚOpenSSHFactoryTz)Cannot run without cryptography or PyASN1Úgeteuidzgeteuid/seteuid not availablec                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚOpenSSHFactoryTestsz&
    Tests for L{OpenSSHFactory}.
    c                 C   s  t ƒ | _t|  ¡ ƒ| _| j ¡  | jj| j_t|  ¡ ƒ| _| j ¡  | jj| j_	| j 
d¡ d¡ | j 
d¡ d¡ | j 
d¡ tj¡ | j 
d¡ tj¡ | j 
d¡ d¡ | j 
d¡ tj¡ | j 
d	¡ d
¡ tƒ | _|  td| jj¡ |  td| jj¡ d S )NÚssh_host_foos   fooÚbar_keyÚssh_host_one_keyÚssh_host_two_keyÚssh_host_three_keys   not a key contentzssh_host_one_key.pubÚmodulis  
#    $OpenBSD: moduli,v 1.xx 2016/07/26 12:34:56 jhacker Exp $i
# Time Type Tests Tries Size Generator Modulus
20030501000000 2 6 100 2047 2 FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF
ÚseteuidÚsetegid)r   Úfactoryr   ÚmktempÚkeysDirÚmakedirsÚpathÚdataRootÚ	moduliDirÚ
moduliRootÚchildÚ
setContentr   ÚprivateRSA_opensshÚprivateDSA_opensshÚpublicRSA_opensshr   ÚmockosÚpatchÚosr   r   )Úself© r(   úH/usr/lib/python3/dist-packages/twisted/conch/test/test_openssh_compat.pyÚsetUp%   s&   

ÿzOpenSSHFactoryTests.setUpc                 C   s8   | j  ¡ }|  t|ƒd¡ | ¡ }|  t|ƒdg¡ dS )zw
        L{OpenSSHFactory.getPublicKeys} should return the available public keys
        in the data directory
        é   ó   ssh-rsaN)r   ÚgetPublicKeysÚassertEqualÚlenÚkeysÚlist©r'   r0   ÚkeyTypesr(   r(   r)   Útest_getPublicKeysJ   s   
z&OpenSSHFactoryTests.test_getPublicKeysc                 C   sZ   | j  ¡ }|  t|ƒd¡ | ¡ }|  t|ƒddh¡ |  | jjg ¡ |  | jjg ¡ dS )z…
        Will return the available private keys in the data directory, ignoring
        key files which failed to be loaded.
        é   r,   ó   ssh-dssN)	r   ÚgetPrivateKeysr.   r/   r0   Úsetr$   ÚseteuidCallsÚsetegidCallsr2   r(   r(   r)   Útest_getPrivateKeysT   s   
z'OpenSSHFactoryTests.test_getPrivateKeysc                    s°   | j  d¡‰ ˆ  d¡ |  ˆ jd¡ tj‰‡ ‡fdd„}|  td|¡ | j ¡ }|  	t
|ƒd¡ | ¡ }|  	t|ƒdd	h¡ |  	| jjdt ¡ g¡ |  	| jjdt ¡ g¡ d
S )z
        L{OpenSSHFactory.getPrivateKeys} should switch to root if the keys
        aren't readable by the current user.
        r   r   éÿ  c                    s   ˆ   d¡ ˆ| ƒS )Nr<   )Úchmod)Úeuid©ÚkeyFileÚsavedSeteuidr(   r)   r   l   s   
z>OpenSSHFactoryTests.test_getPrivateKeysAsRoot.<locals>.seteuidr   r5   r,   r6   N)r   r   r=   Ú
addCleanupr&   r   r%   r   r7   r.   r/   r0   r8   r$   r9   r   r:   Úgetegid)r'   r   r0   r3   r(   r?   r)   Útest_getPrivateKeysAsRoot`   s   

z-OpenSSHFactoryTests.test_getPrivateKeysAsRootc                 C   s$   | j  ¡ }|  |dtdƒgi¡ dS )zq
        L{OpenSSHFactory.getPrimes} should return the available primes
        in the moduli directory.
        i   s   diffie-hellman-group14-sha1N)r   Ú	getPrimesr.   r   )r'   Úprimesr(   r(   r)   Útest_getPrimesx   s   

ÿþz"OpenSSHFactoryTests.test_getPrimesN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r*   r4   r;   rD   rG   r(   r(   r(   r)   r      s    %
r   )rK   r&   Úunittestr   Útwisted.conch.ssh._kexr   Útwisted.conch.testr   Útwisted.python.filepathr   Útwisted.python.reflectr   Útwisted.test.test_processr   Útwisted.trial.unittestr   ÚdoSkipÚ
skipReasonÚ$twisted.conch.openssh_compat.factoryr   Úhasattrr   r(   r(   r(   r)   Ú<module>   s(   
