U
    
W[@-                     @   s*  d Z ddlmZmZ ddlZddlmZ edrzedrzd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nd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 e  rdZ!neZ!dZ"ej#$ sdZ"ej%$ sdZ"G dd deZ&G dd deZ'dS )z,
Tests for L{twisted.conch.client.default}.
    )absolute_importdivisionN)requireModuleZcryptographyZpyasn1)SSHAgentClient)SSHUserAuthClient)ConchOptions)default)KeyzBcryptography and PyASN1 required for twisted.conch.client.default.)TestCase)FilePath)
ConchError)keydata)StringTransport)nativeString)platformzlgenericAnswers 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dd ZepZee_dd Zeplee_dd Zep~ee_dd Zepee_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 )NZid_rsa
id_rsa.pub)r	   
fromStringr   ZpublicRSA_openssh	rsaPublicr   ZmktemptmpdirmakedirschildrsaFile
setContentprivateRSA_opensshself r   A/usr/lib/python3/dist-packages/twisted/conch/test/test_default.pysetUp7   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   ZmakeConnectionkeyAgentZsignDatar   assertEqualvalueblob)r   clientagent	transportZ	cleartextr   r   r   test_signDataWithAgent@   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$   ZblobsgetPublicKey
assertTrueisPublicr"   ZassertIsNone)r   r&   keyr   r   r   test_agentGetPublicKeyS   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   r)   r*   r+   r"   r   )r   optionsr%   r,   r   r   r   test_getPublicKeyFromFileb   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   r.   r/   r   r   r!   r)   r*   r+   r"   r   )r   r0   r&   r%   r,   r   r   r   test_getPublicKeyAgentFallbackp   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.pubZid_dsar   s
   not a key!r    N)r   r   r   r   r   ZpublicDSA_opensshZprivateDSA_opensshr   r.   r/   r   r)   r*   r+   r"   r	   r   Z	usedFiles)r   r0   ZdsaFiler%   r,   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                    s    |   |   d S NZassertFalser+   r"   r,   
rsaPrivater   r   r   _cbGetPrivateKey   s    zCSSHUserAuthClientTests.test_getPrivateKey.<locals>._cbGetPrivateKey)r	   r   r   r   r   r   r.   r/   r   r)   getPrivateKeyaddCallback)r   r0   r%   r9   r   r7   r   test_getPrivateKey   s    z)SSHUserAuthClientTests.test_getPrivateKeyc                    s   t tjd j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 passphraseZopensshr    Nc                    s    | djjf  t S )NzEnter passphrase for key '%s': )r"   r   r.   r   prompt)
passphraser   r   r   _getPassword   s
    zISSHUserAuthClientTests.test_getPrivateKeyPassphrase.<locals>._getPasswordc                    s    |   |   d S r4   r5   r6   r7   r   r   r9      s    zMSSHUserAuthClientTests.test_getPrivateKeyPassphrase.<locals>._cbGetPrivateKeyr@   )r	   r   r   r   r   r   ZtoStringr   r.   r/   r   r)   patchr:   r;   )r   r0   r%   r@   r9   r   )r?   r8   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 r4   )r'   host)r   rC   r   r   r   __init__   s    zGSSHUserAuthClientTests.test_getPassword.<locals>.FakeTransport.__init__c                 S   s   | S r4   r   r   r   r   r   getPeer   s    zFSSHUserAuthClientTests.test_getPassword.<locals>.FakeTransport.getPeerN)__name__
__module____qualname__rD   rE   r   r   r   r   FakeTransport   s   rI   r    Nz	127.0.0.1c                    s     | d dS )Nzuser@127.0.0.1's password: bad passwordr"   r=   r   r   r   getpass   s    z8SSHUserAuthClientTests.test_getPassword.<locals>.getpassrL      bad password)	r   r   r'   rA   r   rL   getPasswordr;   r"   )r   rI   r0   r%   rL   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 )NrJ   )r"   r   )pr>   r   r   r   rL      s    z>SSHUserAuthClientTests.test_getPasswordPrompt.<locals>.getpassrL   rM   )r   r   rA   r   rL   rN   r;   r"   )r   r0   r%   rL   rO   r   rR   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d S )NzUser pressed CTRL-C)KeyboardInterruptr=   r   r   r   rL      s    zBSSHUserAuthClientTests.test_getPasswordConchError.<locals>.getpassrL      ?c                    s     gtjtjg | S r4   )r"   sysstdoutstdin)Zfailr   rX   rW   r   r   	check_sys  s
     
zDSSHUserAuthClientTests.test_getPasswordConchError.<locals>.check_sys)r   r   rA   r   rL   rV   rW   rX   rN   Z
addErrbackZassertFailurer   )r   r0   r%   rL   rO   rZ   r   rY   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                    s     | d dS )Nzpass promptrL   rK   r=   r   r   r   rL     s    z>SSHUserAuthClientTests.test_getGenericAnswers.<locals>.getpassrL   c                    s     | d dS )Nzraw_input prompt	raw_inputrK   r=   r   r   r   r\     s    z@SSHUserAuthClientTests.test_getGenericAnswers.<locals>.raw_inputr\   s   Names   Instruction)s   pass promptF)s   raw_input promptT)r   r   rA   r   rL   ZgetGenericAnswersr;   ZassertListEqual)r   r0   r%   rL   r\   rO   r   r   r   test_getGenericAnswers  s$       z-SSHUserAuthClientTests.test_getGenericAnswersN)rF   rG   rH   __doc__r   r(   r-   r1   r2   r3   r<   rB   rP   windowsSkipttySkipskiprS   r[   r]   r   r   r   r   r   /   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 ddd	g d
S )z)
        Specify MAC algorithms.
        zinvalid-maczUnknown mac typezhmac-sha2-512Zmacss   hmac-sha2-512z hmac-sha2-256,hmac-sha1,hmac-md5s   hmac-sha2-256s	   hmac-sha1s   hmac-md5N)r   assertRaises
SystemExitZopt_macsassertIncoder"   r   Zoptser   r   r   	test_macs0  s    


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   rc   rd   Zopt_host_key_algorithmsre   rf   r"   rg   r   r   r   test_host_key_algorithms@  s    


z,ConchOptionsParsing.test_host_key_algorithmsN)rF   rG   rH   r^   ri   rj   r   r   r   r   rb   ,  s   rb   )(r^   Z
__future__r   r   rV   Ztwisted.python.reflectr   Ztwisted.conch.client.agentr   Ztwisted.conch.client.defaultr   Ztwisted.conch.client.optionsr   Ztwisted.conch.clientr   Ztwisted.conch.ssh.keysr	   ra   Ztwisted.trial.unittestr
   Ztwisted.python.filepathr   Ztwisted.conch.errorr   Ztwisted.conch.testr   Ztwisted.test.proto_helpersr   Ztwisted.python.compatr   Ztwisted.python.runtimer   Z	isWindowsr_   r`   rX   isattyrW   r   rb   r   r   r   r   <module>   s<   
 ~