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$# sd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