
    Yf-                        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j!                    rdZdZej"        #                                sdZdZej$        #                                sdZdZ G d de          Z% G 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                       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dS )SSHUserAuthClientTestszm
    Tests for L{SSHUserAuthClient}.

    @type rsaPublic: L{Key}
    @ivar rsaPublic: A public RSA key.
    c                    t          j        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selfs    Q/var/www/html/env/lib/python3.11/site-packages/twisted/conch/test/test_default.pysetUpzSSHUserAuthClientTests.setUp:   s    (ABBt{{}}--{((22 :;;;,''2273LMMMMM    c                    t          dt                      d          }t                      }t                      }|                    |           ||_        d}|                    | j        |           |                     |	                                d| j        
                                z   dz   |z   dz              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	cleartexts        r$   test_signDataWithAgentz-SSHUserAuthClientTests.test_signDataWithAgentB   s    
 #7LNNDAA  #%%	Y''' 		222OO1n!!##$   "	"	
 	
 	
 	
 	
r&   c                 Z   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#   r0   keys      r$   test_agentGetPublicKeyz-SSHUserAuthClientTests.test_agentGetPublicKeyW   s       ~**,,-  ""'''dn---%,,../////r&   c                    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   r6   r7   r8   r,   r   )r#   optionsr/   r:   s       r$   test_getPublicKeyFromFilez0SSHUserAuthClientTests.test_getPublicKeyFromFilee   sw     ..!\./"7GT::!!##'''dn-----r&   c                 >   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*   r6   r7   r8   r,   r   )r#   r?   r0   r/   r:   s        r$   test_getPublicKeyAgentFallbackz5SSHUserAuthClientTests.test_getPublicKeyAgentFallbackr   s    
 ..!\./  "7GT::!!##'''dn-----r&   c                    t                      }| j                            d                              t          j                   | j                            d          }|                    t          j                   | j        j        |j        g|_	        | j                            d                              d           t          d|d          }|                                }|                     |                                           |                     |t          j        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   r=   r>   r   r6   r7   r8   r,   r   r   	usedFiles)r#   r?   dsaFiler/   r:   s        r$   test_getPublicKeyBadKeyErrorz3SSHUserAuthClientTests.test_getPublicKeyBadKeyError   s"    ..,''2273LMMM+##H--75666!\.=,''22=AAA"7GT::!!##'''cnW-FGGHHH)DL,=w|+LMMMMMr&   c                 &    t          j        t          j                  t	                      } j        j        g|_        t          d|d          }|	                                  f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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   s     ^G$>??
..!\./"7GT::	. 	. 	. 	. 	. 	. ##%%112BCCCr&   c                     t          j        t          j                  d j                                                d                     t                      } j        j        g|_	        t          d|d          }|                                  fd} f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                 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   sA    KT\5FKKK    
+++r&   c                                          |                                                                |            d S 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 UVVV..!\./"7GT::	, 	, 	, 	, 	, 	,	. 	. 	. 	. 	. 	. 	

6><888##%%112BCCCr&   c                 *     G d d          }t                      }t          d|d          } |d          |_         fd}                     t          j        d|           |                                }|                     j        d           |S )	zn
        Get the password using
        L{twisted.conch.client.default.SSHUserAuthClient.getPassword}
        c                       e Zd Zd Zd ZdS )>SSHUserAuthClientTests.test_getPassword.<locals>.FakeTransportc                 "    | | _         || _        d S 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      s2        ! ! !    r&   rl   r(   Nz	127.0.0.1c                 4                         | d           dS )Nzuser@127.0.0.1's password: bad passwordr,   rZ   r#   s    r$   getpassz8SSHUserAuthClientTests.test_getPassword.<locals>.getpass   s    V%BCC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   s    	 	 	 	 	 	 	 	 .."7GT::(=55	" 	" 	" 	" 	" 	

7?Iw777  	d&888r&   c                      t                      }t          d|d          }d f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                 N                         | t                               dS )Nrn   )r,   r   )prZ   r#   s    r$   rq   z>SSHUserAuthClientTests.test_getPasswordPrompt.<locals>.getpass   s&    QV 4 455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   s     .."7GT::)	" 	" 	" 	" 	" 	" 	

7?Iw777v&&	d&888r&   c                 L    t                      }t          d|d          }d }                     t          j        d|           t
          j        t
          j        c|                    d          }|j	         f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                      t          d          )NzUser pressed CTRL-C)KeyboardInterrupt)rZ   s    r$   rq   zBSSHUserAuthClientTests.test_getPasswordConchError.<locals>.getpass   s    #$9:::r&   rq      ?c                 d                         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_sz39.EFF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77
CIt$$	
	 	 	 	 	 	 
	 	1j)))))r&   c                 @    t                      }t          d|d          } fd}                     t          j        d|            fd}                     t          d|           |                    ddd	d
g          }|                     j        ddg           |S )zU
        L{twisted.conch.client.default.SSHUserAuthClient.getGenericAnswers}
        r(   Nc                 4                         | d           dS )Nzpass promptrq   ro   rp   s    r$   rq   z>SSHUserAuthClientTests.test_getGenericAnswers.<locals>.getpass  s    V]3339r&   rq   c                 4                         | d           dS )Nzraw_input prompt	raw_inputro   rp   s    r$   r   z@SSHUserAuthClientTests.test_getGenericAnswers.<locals>.raw_input  s    V%788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77	 	 	 	 	 	

7Hi000$$$&AB
 

 	
d*Y,DEE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   sB        N N N
 
 
*0 0 0. . .. . .N N N$D D D&D D D8 VFJ   6 VFJ   & VFJ* *  *. VFJ     r&   r   c                       e Zd ZdZd Zd ZdS )ConchOptionsParsingz
    Options parsing.
    c                    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es      r$   	test_macszConchOptionsParsing.test_macs*  s     ~~j$-GG(!&111~~o&&&f(8'9:::&'''f(8'9:::8999f'T'T'TUUUUUr&   c                    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   s      r$   test_host_key_algorithmsz,ConchOptionsParsing.test_host_key_algorithms9  s     ~~j$*FVV-qv666~~$$Y///34zlCCC$$Z00034zlCCC$$%677734z:6NOOOOOr&   N)ri   rj   rk   r   r   r   rg   r&   r$   r   r   %  sD         V V VP P P P 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 4 4 4 4 4 . . . . . . , , , , , , 0 0 0 0 0 0 + + + + + + + + + + + +	
=   	,,,,,,999999>>>>>>999999*******FJJD8 F	? 
 y 7F6J
z 8F7Jp p p p pX p p pf!P !P !P !P !P( !P !P !P !P !Pr&   