o
    ¯bD-  ã                   @   s2  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d
lmZ ddlmZ dZdZedƒrgedƒrgddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ndZdZeZ e !¡ rudZdZej" #¡ s~dZdZej$ #¡ s‡dZdZG dd„ deƒZ%G dd„ deƒZ&dS )z,
Tests for L{twisted.conch.client.default}.
é    N)ÚskipIf)Ú
ConchError)Úkeydata)ÚnativeString)ÚFilePath)ÚrequireModule)Úplatform)ÚStringTransport)ÚTestCaseFÚ ÚcryptographyÚpyasn1)Údefault)ÚSSHAgentClient)ÚSSHUserAuthClient)ÚConchOptions)ÚKeyTzBcryptography and PyASN1 required for twisted.conch.client.default.zlgenericAnswers and getPassword does not work on Windows. Should be fixed as part of fixing bug 6409 and 6410z#sys.stdin is not an interactive ttyz$sys.stdout is not an interactive ttyc                   @   s˜   e Zd 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„ Zeeeƒdd„ ƒZeeeƒdd„ ƒZeeeƒdd„ ƒZeeeƒdd„ ƒZdS )ÚSSHUserAuthClientTestszm
    Tests for L{SSHUserAuthClient}.

    @type rsaPublic: L{Key}
    @ivar rsaPublic: A public RSA key.
    c                 C   sZ   t  tj¡| _t|  ¡ ƒ| _| j ¡  | j 	d¡| _
| j
 tj¡ | j 	d¡ tj¡ d S )NÚid_rsaú
id_rsa.pub)r   Ú
fromStringr   ÚpublicRSA_opensshÚ	rsaPublicr   ÚmktempÚtmpdirÚmakedirsÚchildÚrsaFileÚ
setContentÚprivateRSA_openssh©Úself© r"   úA/usr/lib/python3/dist-packages/twisted/conch/test/test_default.pyÚsetUp:   s   
zSSHUserAuthClientTests.setUpc                 C   sf   t dtƒ dƒ}tƒ }tƒ }| |¡ ||_d}| | j|¡ |  | 	¡ d| j 
¡  d | d ¡ dS )z˜
        When connected to an agent, L{SSHUserAuthClient} can use it to
        request signatures of particular data with a particular L{Key}.
        ó   userNs	   Sign heres	     -  s      	s       )r   r   r   r	   ÚmakeConnectionÚkeyAgentÚsignDatar   ÚassertEqualÚvalueÚblob)r!   ÚclientÚagentÚ	transportÚ	cleartextr"   r"   r#   Útest_signDataWithAgentB   s&   
ÿþýüþz-SSHUserAuthClientTests.test_signDataWithAgentc                 C   sJ   t ƒ }| j ¡ g|_| ¡ }|  | ¡ ¡ |  || j¡ |  | ¡ ¡ dS )a  
        L{SSHUserAuthClient} looks up public keys from the agent using the
        L{SSHAgentClient} class.  That L{SSHAgentClient.getPublicKey} returns a
        L{Key} object with one of the public keys in the agent.  If no more
        keys are present, it returns L{None}.
        N)	r   r   r+   ÚblobsÚgetPublicKeyÚ
assertTrueÚisPublicr)   ÚassertIsNone)r!   r-   Úkeyr"   r"   r#   Útest_agentGetPublicKeyW   s   z-SSHUserAuthClientTests.test_agentGetPublicKeyc                 C   sF   t ƒ }| jjg|_td|dƒ}| ¡ }|  | ¡ ¡ |  || j	¡ dS )zÕ
        L{SSHUserAuthClient.getPublicKey()} is able to get a public key from
        the first file described by its options' C{identitys} list, and return
        the corresponding public L{Key} object.
        r%   N)
r   r   ÚpathÚ	identitysr   r2   r3   r4   r)   r   )r!   Úoptionsr,   r6   r"   r"   r#   Útest_getPublicKeyFromFilee   s   z0SSHUserAuthClientTests.test_getPublicKeyFromFilec                 C   sR   t ƒ }| jjg|_tƒ }td|dƒ}||_| ¡ }|  | 	¡ ¡ |  
|| j¡ dS )z’
        If an agent is present, but doesn't return a key,
        L{SSHUserAuthClient.getPublicKey} continue with the normal key lookup.
        r%   N)r   r   r8   r9   r   r   r'   r2   r3   r4   r)   r   )r!   r:   r-   r,   r6   r"   r"   r#   Útest_getPublicKeyAgentFallbackr   s   z5SSHUserAuthClientTests.test_getPublicKeyAgentFallbackc                 C   s¦   t ƒ }| j d¡ tj¡ | j d¡}| tj¡ | jj|jg|_	| j d¡ d¡ t
d|dƒ}| ¡ }|  | ¡ ¡ |  |t tj¡¡ |  |j| jj|jg¡ dS )z¼
        If L{keys.Key.fromFile} raises a L{keys.BadKeyError}, the
        L{SSHUserAuthClient.getPublicKey} tries again to get a public key by
        calling itself recursively.
        z
id_dsa.pubÚid_dsar   s
   not a key!r%   N)r   r   r   r   r   ÚpublicDSA_opensshÚprivateDSA_opensshr   r8   r9   r   r2   r3   r4   r)   r   r   Ú	usedFiles)r!   r:   ÚdsaFiler,   r6   r"   r"   r#   Útest_getPublicKeyBadKeyError€   s   z3SSHUserAuthClientTests.test_getPublicKeyBadKeyErrorc                    sN   t  tj¡‰ tƒ }ˆjjg|_td|dƒ}| 	¡  ‡ ‡fdd„}| 
¡  |¡S )zð
        L{SSHUserAuthClient.getPrivateKey} will load a private key from the
        last used file populated by L{SSHUserAuthClient.getPublicKey}, and
        return a L{Deferred} which fires with the corresponding private L{Key}.
        r%   Nc                    ó   ˆ  |  ¡ ¡ ˆ | ˆ ¡ d S ©N©ÚassertFalser4   r)   ©r6   ©Ú
rsaPrivater!   r"   r#   Ú_cbGetPrivateKeyŸ   ó   zCSSHUserAuthClientTests.test_getPrivateKey.<locals>._cbGetPrivateKey)r   r   r   r   r   r   r8   r9   r   r2   ÚgetPrivateKeyÚaddCallback)r!   r:   r,   rJ   r"   rH   r#   Útest_getPrivateKey’   s   z)SSHUserAuthClientTests.test_getPrivateKeyc                    s„   t  tj¡‰d‰ ˆj ˆjdˆ d�¡ tƒ }ˆjjg|_	t
d|dƒ}| ¡  ‡ ‡fdd„}‡‡fdd	„}ˆ |d
|¡ | ¡  |¡S )z·
        L{SSHUserAuthClient} can get a private key from a file, and return a
        Deferred called back with a private L{Key} object, even if the key is
        encrypted.
        s   this is the passphraseÚopenssh)Ú
passphraser%   Nc                    s    ˆ  | dˆjj› d�¡ tˆ ƒS )NzEnter passphrase for key 'z': )r)   r   r8   r   ©Úprompt)rP   r!   r"   r#   Ú_getPassword´   s   ÿzISSHUserAuthClientTests.test_getPrivateKeyPassphrase.<locals>._getPasswordc                    rC   rD   rE   rG   rH   r"   r#   rJ   º   rK   zMSSHUserAuthClientTests.test_getPrivateKeyPassphrase.<locals>._cbGetPrivateKeyrS   )r   r   r   r   r   r   ÚtoStringr   r8   r9   r   r2   ÚpatchrL   rM   )r!   r:   r,   rS   rJ   r"   )rP   rI   r!   r#   Útest_getPrivateKeyPassphrase¥   s   z3SSHUserAuthClientTests.test_getPrivateKeyPassphrasec                    s`   G dd„ dƒ}t ƒ }td|dƒ}|dƒ|_‡ fdd„}ˆ  tjd|¡ | ¡ }| ˆ jd	¡ |S )
zn
        Get the password using
        L{twisted.conch.client.default.SSHUserAuthClient.getPassword}
        c                   @   s   e Zd Zdd„ Zdd„ ZdS )z>SSHUserAuthClientTests.test_getPassword.<locals>.FakeTransportc                 S   s   | | _ || _d S rD   )r.   Úhost)r!   rW   r"   r"   r#   Ú__init__É   s   
zGSSHUserAuthClientTests.test_getPassword.<locals>.FakeTransport.__init__c                 S   s   | S rD   r"   r    r"   r"   r#   ÚgetPeerÍ   s   zFSSHUserAuthClientTests.test_getPassword.<locals>.FakeTransport.getPeerN)Ú__name__Ú
__module__Ú__qualname__rX   rY   r"   r"   r"   r#   ÚFakeTransportÈ   s    r]   r%   Nz	127.0.0.1c                    ó   ˆ   | d¡ dS )Nzuser@127.0.0.1's password: úbad password©r)   rQ   r    r"   r#   ÚgetpassÔ   ó   z8SSHUserAuthClientTests.test_getPassword.<locals>.getpassra   ó   bad password)	r   r   r.   rU   r   ra   ÚgetPasswordrM   r)   )r!   r]   r:   r,   ra   Údr"   r    r#   Útest_getPasswordÁ   s   
z'SSHUserAuthClientTests.test_getPasswordc                    sP   t ƒ }td|dƒ}d‰ ‡ ‡fdd„}ˆ tjd|¡ | ˆ ¡}| ˆjd¡ |S )z�
        Get the password using
        L{twisted.conch.client.default.SSHUserAuthClient.getPassword}
        using a different prompt.
        r%   Ns   Give up your passwordc                    s   ˆ  | tˆ ƒ¡ dS )Nr_   )r)   r   )Úp©rR   r!   r"   r#   ra   è   s   z>SSHUserAuthClientTests.test_getPasswordPrompt.<locals>.getpassra   rc   )r   r   rU   r   ra   rd   rM   r)   )r!   r:   r,   ra   re   r"   rh   r#   Útest_getPasswordPromptÝ   s   
z-SSHUserAuthClientTests.test_getPasswordPromptc                    sh   t ƒ }td|dƒ}dd„ }ˆ  tjd|¡ tjtj‰‰| d¡}|j	‡ ‡‡fdd„ƒ}ˆ  
|t¡ dS )	z­
        Get the password using
        L{twisted.conch.client.default.SSHUserAuthClient.getPassword}
        and trigger a {twisted.conch.error import ConchError}.
        r%   Nc                 S   s   t dƒ‚)NzUser pressed CTRL-C)ÚKeyboardInterruptrQ   r"   r"   r#   ra   û   s   zBSSHUserAuthClientTests.test_getPasswordConchError.<locals>.getpassra   ó   ?c                    s   ˆ   ˆˆgtjtjg¡ | S rD   )r)   ÚsysÚstdoutÚstdin)Úfail©r!   rn   rm   r"   r#   Ú	check_sys  s   zDSSHUserAuthClientTests.test_getPasswordConchError.<locals>.check_sys)r   r   rU   r   ra   rl   rm   rn   rd   Ú
addErrbackÚassertFailurer   )r!   r:   r,   ra   re   rq   r"   rp   r#   Útest_getPasswordConchErrorñ   s   
z1SSHUserAuthClientTests.test_getPasswordConchErrorc                    sp   t ƒ }td|dƒ}‡ fdd„}ˆ  tjd|¡ ‡ fdd„}ˆ  td|¡ | d	d
ddg¡}| ˆ jddg¡ |S )zU
        L{twisted.conch.client.default.SSHUserAuthClient.getGenericAnswers}
        r%   Nc                    r^   )Nzpass promptra   r`   rQ   r    r"   r#   ra     rb   z>SSHUserAuthClientTests.test_getGenericAnswers.<locals>.getpassra   c                    r^   )Nzraw_input promptÚ	raw_inputr`   rQ   r    r"   r#   ru     rb   z@SSHUserAuthClientTests.test_getGenericAnswers.<locals>.raw_inputÚ_inputs   Names   Instruction)s   pass promptF)s   raw_input promptTru   )r   r   rU   r   ra   ÚgetGenericAnswersrM   ÚassertListEqual)r!   r:   r,   ra   ru   re   r"   r    r#   Útest_getGenericAnswers	  s   ýz-SSHUserAuthClientTests.test_getGenericAnswersN)rZ   r[   r\   Ú__doc__r$   r0   r7   r;   r<   rB   rN   rV   r   ÚdoSkipÚ
skipReasonrf   ri   rt   ry   r"   r"   r"   r#   r   2   s$    


r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚConchOptionsParsingz
    Options parsing.
    c                 C   s„   t ƒ }|  t|jd¡}|  d|j¡ t ƒ }| d¡ |  |d dg¡ | d¡ |  |d dg¡ | d¡ |  |d g d¢¡ dS )	z)
        Specify MAC algorithms.
        zinvalid-maczUnknown mac typezhmac-sha2-512Úmacss   hmac-sha2-512z hmac-sha2-256,hmac-sha1,hmac-md5)s   hmac-sha2-256s	   hmac-sha1s   hmac-md5N)r   ÚassertRaisesÚ
SystemExitÚopt_macsÚassertInÚcoder)   ©r!   ÚoptsÚer"   r"   r#   Ú	test_macs*  ó   


zConchOptionsParsing.test_macsc                 C   s„   t ƒ }|  t|jd¡}|  d|j¡ t ƒ }| d¡ |  |d dg¡ | d¡ |  |d dg¡ | d¡ |  |d ddg¡ dS )	z.
        Specify host key algorithms.
        zinvalid-keyzUnknown host key typezssh-rsazhost-key-algorithmss   ssh-rsas   ssh-dsszssh-rsa,ssh-dssN)r   r   r€   Úopt_host_key_algorithmsr‚   rƒ   r)   r„   r"   r"   r#   Útest_host_key_algorithms9  rˆ   z,ConchOptionsParsing.test_host_key_algorithmsN)rZ   r[   r\   rz   r‡   rŠ   r"   r"   r"   r#   r}   %  s    r}   )'rz   rl   Úunittestr   Útwisted.conch.errorr   Útwisted.conch.testr   Útwisted.python.compatr   Útwisted.python.filepathr   Útwisted.python.reflectr   Útwisted.python.runtimer   Útwisted.test.proto_helpersr	   Útwisted.trial.unittestr
   r{   r|   Útwisted.conch.clientr   Útwisted.conch.client.agentr   Útwisted.conch.client.defaultr   Útwisted.conch.client.optionsr   Útwisted.conch.ssh.keysr   ÚskipÚ	isWindowsrn   Úisattyrm   r   r}   r"   r"   r"   r#   Ú<module>   sF   ÿ

 t