o
    >h-                     @   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d
lmZ ddlmZ dZdZedrcd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! rqdZdZej"# szd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)StringTransport)nativeString)FilePath)requireModule)platform)TestCaseF cryptography)default)SSHAgentClient)SSHUserAuthClient)ConchOptions)KeyTz7cryptography 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!   /var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-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   r1   r2   r3   r(   r   )r    optionsr+   r5   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   r7   r8   r   r   r&   r1   r2   r3   r(   r   )r    r9   r,   r+   r5   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   r7   r8   r   r1   r2   r3   r(   r   r   	usedFiles)r    r9   dsaFiler+   r5   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assertFalser3   r(   r5   
rsaPrivater    r!   r"   _cbGetPrivateKey      zCSSHUserAuthClientTests.test_getPrivateKey.<locals>._cbGetPrivateKey)r   r   r   r   r   r   r7   r8   r   r1   getPrivateKeyaddCallback)r    r9   r+   rI   r!   rG   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   r7   r   prompt)rO   r    r!   r"   _getPassword   s   zISSHUserAuthClientTests.test_getPrivateKeyPassphrase.<locals>._getPasswordc                    rB   rC   rD   rF   rG   r!   r"   rI      rJ   zMSSHUserAuthClientTests.test_getPrivateKeyPassphrase.<locals>._cbGetPrivateKeyrR   )r   r   r   r   r   r   toStringr   r7   r8   r   r1   patchrK   rL   )r    r9   r+   rR   rI   r!   )rO   rH   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 rC   )r-   host)r    rV   r!   r!   r"   __init__   s   
zGSSHUserAuthClientTests.test_getPassword.<locals>.FakeTransport.__init__c                 S   s   | S rC   r!   r   r!   r!   r"   getPeer   s   zFSSHUserAuthClientTests.test_getPassword.<locals>.FakeTransport.getPeerN)__name__
__module____qualname__rW   rX   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(   rP   r   r!   r"   getpass      z8SSHUserAuthClientTests.test_getPassword.<locals>.getpassr`      bad password)	r   r   r-   rT   r   r`   getPasswordrL   r(   )r    r\   r9   r+   r`   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rQ   r    r!   r"   r`      s   z>SSHUserAuthClientTests.test_getPasswordPrompt.<locals>.getpassr`   rb   )r   r   rT   r   r`   rc   rL   r(   )r    r9   r+   r`   rd   r!   rg   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)KeyboardInterruptrP   r!   r!   r"   r`      s   zBSSHUserAuthClientTests.test_getPasswordConchError.<locals>.getpassr`      ?c                    s     gtjtjg | S rC   )r(   sysstdoutstdin)failr    rm   rl   r!   r"   	check_sys  s   zDSSHUserAuthClientTests.test_getPasswordConchError.<locals>.check_sys)r   r   rT   r   r`   rk   rl   rm   rc   
addErrbackassertFailurer   )r    r9   r+   r`   rd   rp   r!   ro   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 promptr`   r_   rP   r   r!   r"   r`     ra   z>SSHUserAuthClientTests.test_getGenericAnswers.<locals>.getpassr`   c                    r]   )Nzraw_input prompt	raw_inputr_   rP   r   r!   r"   rt     ra   z@SSHUserAuthClientTests.test_getGenericAnswers.<locals>.raw_input_inputs   Names   Instruction)s   pass promptF)s   raw_input promptTrt   )r   r   rT   r   r`   getGenericAnswersrL   assertListEqual)r    r9   r+   r`   rt   rd   r!   r   r"   test_getGenericAnswers	  s   z-SSHUserAuthClientTests.test_getGenericAnswersN)rY   rZ   r[   __doc__r#   r/   r6   r:   r;   rA   rM   rU   r   doSkip
skipReasonre   rh   rs   rx   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)rY   rZ   r[   ry   r   r   r!   r!   r!   r"   r|   %  s    r|   )'ry   rk   unittestr   twisted.conch.errorr   twisted.conch.testr   twisted.internet.testingr   twisted.python.compatr   twisted.python.filepathr   twisted.python.reflectr   twisted.python.runtimer	   twisted.trial.unittestr
   rz   r{   twisted.conch.clientr   twisted.conch.client.agentr   twisted.conch.client.defaultr   twisted.conch.client.optionsr   twisted.conch.ssh.keysr   skip	isWindowsrm   isattyrl   r   r|   r!   r!   r!   r"   <module>   sF   

 t