
    Vh-                        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      r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 ndZdZeZ  ejB                         rdZdZejD                  jG                         sdZdZejH                  jG                         sdZdZ G d de      Z% G d de      Z&y)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                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Z eee      d
        Z eee      d        Z eee      d        Z eee      d        Zy)SSHUserAuthClientTestszm
    Tests for L{SSHUserAuthClient}.

    @type rsaPublic: L{Key}
    @ivar rsaPublic: A public RSA key.
    c                    t        j                  t        j                        | _        t        | j                               | _        | j                  j                          | j                  j                  d      | _
        | j                  j                  t        j                         | j                  j                  d      j                  t        j                         y )Nid_rsa
id_rsa.pub)r   
fromStringr   publicRSA_openssh	rsaPublicr   mktemptmpdirmakedirschildrsaFile
setContentprivateRSA_opensshselfs    O/home/dcms/DCMS/lib/python3.12/site-packages/twisted/conch/test/test_default.pysetUpzSSHUserAuthClientTests.setUp:   s    (A(ABt{{}-{{((2 : :;,'2273L3LM    c                 J   t        dt               d      }t               }t               }|j	                  |       ||_        d}|j                  | j                  |       | j                  |j                         d| j                  j                         z   dz   |z   dz          y)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	cleartexts        r$   test_signDataWithAgentz-SSHUserAuthClientTests.test_signDataWithAgentB   s    
 #7LNDA #%	Y' 		2OO1nn!!#$   "	"	
r&   c                 ,   t               }| j                  j                         g|_        |j	                         }| j                  |j                                | j                  || j                         | j                  |j	                                y)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#   r0   keys      r$   test_agentGetPublicKeyz-SSHUserAuthClientTests.test_agentGetPublicKeyW   sn      ~~**,-  "'dnn-%,,./r&   c                     t               }| j                  j                  g|_        t	        d|d      }|j                         }| j                  |j                                | j                  || j                         y)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   r6   r7   r8   r,   r   )r#   optionsr/   r:   s       r$   test_getPublicKeyFromFilez0SSHUserAuthClientTests.test_getPublicKeyFromFilee   sb     .!\\../"7GT:!!#'dnn-r&   c                 "   t               }| j                  j                  g|_        t	               }t        d|d      }||_        |j                         }| j                  |j                                | j                  || j                         y)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*   r6   r7   r8   r,   r   )r#   r?   r0   r/   r:   s        r$   test_getPublicKeyAgentFallbackz5SSHUserAuthClientTests.test_getPublicKeyAgentFallbackr   sq    
 .!\\../ "7GT:!!#'dnn-r&   c                    t               }| j                  j                  d      j                  t        j
                         | j                  j                  d      }|j                  t        j                         | j                  j                  |j                  g|_	        | j                  j                  d      j                  d       t        d|d      }|j                         }| j                  |j                                | j                  |t        j                   t        j
                               | j                  |j"                  | j                  j                  |j                  g       y)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   r=   r>   r   r6   r7   r8   r,   r   r   	usedFiles)r#   r?   dsaFiler/   r:   s        r$   test_getPublicKeyBadKeyErrorz3SSHUserAuthClientTests.test_getPublicKeyBadKeyError   s    .,'2273L3LM++##H-7556!\\..=,'22=A"7GT:!!#'cnnW-F-FGH))DLL,=,=w||+LMr&   c                     t        j                  t        j                        t	               } j
                  j                  g|_        t        d|d      }|j                           fd}|j                         j                  |      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                 h    j                  | j                                j                  |        y NassertFalser8   r,   r:   
rsaPrivater#   s    r$   _cbGetPrivateKeyzCSSHUserAuthClientTests.test_getPrivateKey.<locals>._cbGetPrivateKey   '    S\\^,S*-r&   )r   r   r   r!   r   r   r=   r>   r   r6   getPrivateKeyaddCallback)r#   r?   r/   rQ   rP   s   `   @r$   test_getPrivateKeyz)SSHUserAuthClientTests.test_getPrivateKey   sq     ^^G$>$>?
.!\\../"7GT:	. ##%112BCCr&   c                     t        j                  t        j                        d j                  j                  j                  d             t               } j                  j                  g|_	        t        d|d      }|j                           fd} fd} j                  |d|       |j                         j                  |      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                 n    j                  | dj                  j                   d       t              S )NzEnter passphrase for key 'z': )r,   r   r=   r   )promptrX   r#   s    r$   _getPasswordzISSHUserAuthClientTests.test_getPrivateKeyPassphrase.<locals>._getPassword   s8    4T\\5F5F4GsK  
++r&   c                 h    j                  | j                                j                  |        y rL   rM   rO   s    r$   rQ   zMSSHUserAuthClientTests.test_getPrivateKeyPassphrase.<locals>._cbGetPrivateKey   rR   r&   r[   )r   r   r   r!   r   r    toStringr   r=   r>   r   r6   patchrS   rT   )r#   r?   r/   r[   rQ   rX   rP   s   `    @@r$   test_getPrivateKeyPassphrasez3SSHUserAuthClientTests.test_getPrivateKeyPassphrase   s     ^^G$>$>?
.

 3 3I* 3 UV.!\\../"7GT:	,	. 	

6><8##%112BCCr&   c                      G d d      }t               }t        d|d      } |d      |_         fd} j                  t        j
                  d|       |j                         }|j                   j                  d       |S )	zn
        Get the password using
        L{twisted.conch.client.default.SSHUserAuthClient.getPassword}
        c                       e Zd Zd Zd Zy)>SSHUserAuthClientTests.test_getPassword.<locals>.FakeTransportc                      | | _         || _        y rL   )r1   host)r#   rd   s     r$   __init__zGSSHUserAuthClientTests.test_getPassword.<locals>.FakeTransport.__init__   s    !% 	r&   c                     | S rL    r"   s    r$   getPeerzFSSHUserAuthClientTests.test_getPassword.<locals>.FakeTransport.getPeer   s    r&   N)__name__
__module____qualname__re   rh   rg   r&   r$   FakeTransportrb      s    !r&   rl   r(   Nz	127.0.0.1c                 *    j                  | d       y)Nzuser@127.0.0.1's password: bad passwordr,   rZ   r#   s    r$   getpassz8SSHUserAuthClientTests.test_getPassword.<locals>.getpass   s    V%BC!r&   rq      bad password)	r   r   r1   r^   r   rq   getPasswordrT   r,   )r#   rl   r?   r/   rq   ds   `     r$   test_getPasswordz'SSHUserAuthClientTests.test_getPassword   ss    	 	 ."7GT:(5	" 	

7??Iw7 	d&&8r&   c                      t               }t        d|d      }d fd} j                  t        j                  d|       |j                        }|j                   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                 <    j                  | t                     y)Nrn   )r,   r   )prZ   r#   s    r$   rq   z>SSHUserAuthClientTests.test_getPasswordPrompt.<locals>.getpass   s    QV 45!r&   rq   rr   )r   r   r^   r   rq   rs   rT   r,   )r#   r?   r/   rq   rt   rZ   s   `    @r$   test_getPasswordPromptz-SSHUserAuthClientTests.test_getPasswordPrompt   sc     ."7GT:)	" 	

7??Iw7v&	d&&8r&   c                 <    t               }t        d|d      }d } j                  t        j                  d|       t
        j                  t
        j                  c|j                  d      }|j                   fd       } j                  |t               y)z
        Get the password using
        L{twisted.conch.client.default.SSHUserAuthClient.getPassword}
        and trigger a {twisted.conch.error import ConchError}.
        r(   Nc                     t        d      )NzUser pressed CTRL-C)KeyboardInterrupt)rZ   s    r$   rq   zBSSHUserAuthClientTests.test_getPasswordConchError.<locals>.getpass   s    #$9::r&   rq      ?c                 l    j                  gt        j                  t        j                  g       | S rL   )r,   sysstdoutstdin)failr#   r   r   s    r$   	check_syszDSSHUserAuthClientTests.test_getPasswordConchError.<locals>.check_sys  s)    fe_szz399.EFKr&   )r   r   r^   r   rq   r   r   r   rs   
addErrbackassertFailurer   )r#   r?   r/   rq   rt   r   r   r   s   `     @@r$   test_getPasswordConchErrorz1SSHUserAuthClientTests.test_getPasswordConchError   s     ."7GT:	; 	

7??Iw7

CIIt$	
	 
	 	1j)r&   c                      t               }t        d|d      } fd} j                  t        j                  d|        fd} j                  t        d|       |j                  ddd	d
g      }|j                   j                  ddg       |S )zU
        L{twisted.conch.client.default.SSHUserAuthClient.getGenericAnswers}
        r(   Nc                 *    j                  | d       y)Nzpass promptrq   ro   rp   s    r$   rq   z>SSHUserAuthClientTests.test_getGenericAnswers.<locals>.getpass  s    V]3r&   rq   c                 *    j                  | d       y)Nzraw_input prompt	raw_inputro   rp   s    r$   r   z@SSHUserAuthClientTests.test_getGenericAnswers.<locals>.raw_input  s    V%78r&   _inputs   Names   Instruction)s   pass promptF)s   raw_input promptTr   )r   r   r^   r   rq   getGenericAnswersrT   assertListEqual)r#   r?   r/   rq   r   rt   s   `     r$   test_getGenericAnswersz-SSHUserAuthClientTests.test_getGenericAnswers	  s    
 ."7GT:	 	

7??Iw7	 	

7Hi0$$$&AB

 	
d**Y,DEr&   N)ri   rj   rk   __doc__r%   r3   r;   r@   rB   rI   rU   r_   r   doSkip
skipReasonru   ry   r   r   rg   r&   r$   r   r   2   s    N
*0..N$D&D8 FJ  6 FJ  & FJ*  *. FJ  r&   r   c                       e Zd ZdZd Zd Zy)ConchOptionsParsingz
    Options parsing.
    c                    t               }| j                  t        |j                  d      }| j	                  d|j
                         t               }|j                  d       | j                  |d   dg       |j                  d       | j                  |d   dg       |j                  d       | j                  |d   g d       y)	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es      r$   	test_macszConchOptionsParsing.test_macs*  s     ~j$--G(!&&1~o&f(8'9:&'f(8'9:89f'TUr&   c                    t               }| j                  t        |j                  d      }| j	                  d|j
                         t               }|j                  d       | j                  |d   dg       |j                  d       | j                  |d   dg       |j                  d       | j                  |d   ddg       y)	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   s      r$   test_host_key_algorithmsz,ConchOptionsParsing.test_host_key_algorithms9  s     ~j$*F*FV-qvv6~$$Y/34zlC$$Z034zlC$$%6734z:6NOr&   N)ri   rj   rk   r   r   r   rg   r&   r$   r   r   %  s    VPr&   r   )'r   r   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   r   r   twisted.conch.clientr   twisted.conch.client.agentr   twisted.conch.client.defaultr   twisted.conch.client.optionsr   twisted.conch.ssh.keysr   skip	isWindowsr   isattyr   r   r   rg   r&   r$   <module>r      s   
   * & 4 . , 0 + +	
 ,9>9*FJJD8F	? 
 yyF6J
zzF7JpX pf!P( !Pr&   