
    Yffk                       d Z ddlmZ ddlZddlZddl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mZmZmZmZ ddlmZ dd	lmZ dd
lmZ  ed          rddlmZmZmZmZmZmZ ddl m!Z!m"Z"m#Z#m$Z$ ndZ%ddZ& G d de          Z'dS )z-
Tests for L{twisted.conch.scripts.ckeygen}.
    )annotationsN)StringIO)Callable)NoReturn)privateECDSA_opensshprivateEd25519_openssh_newprivateRSA_opensshprivateRSA_openssh_encryptedpublicRSA_openssh)FilePath)requireModule)TestCasecryptography)_getKeyOrDefault_saveKeychangePassPhrasedisplayPublicKeyenumrepresentationprintFingerprint)BadFingerPrintFormatBadKeyErrorFingerprintFormatsKeyz7cryptography required for twisted.conch.scripts.ckeygenpassphrasesstrreturnCallable[[object], str]c                 2    t          |           dfd}|S )a@  
    Return a callable to patch C{getpass.getpass}.  Yields a passphrase each
    time called. Use case is to provide an old, then new passphrase(s) as if
    requested interactively.

    @param passphrases: The list of passphrases returned, one per each call.

    @return: A callable to patch C{getpass.getpass}.
    _objectr   r   c                "    t                    S N)next)r   passphrasesIters    Q/var/www/html/env/lib/python3.11/site-packages/twisted/conch/test/test_ckeygen.pyfakeGetpassz makeGetpass.<locals>.fakeGetpass<   s    O$$$    )r   r    r   r   )iter)r   r&   r$   s     @r%   makeGetpassr)   0   s8     ;''O% % % % % % r'   c                  X   e Zd ZdZd2dZ	 	 d3d4dZd2dZd2dZd2dZd2dZ	d2dZ
d2dZd2dZd2dZd2dZd2dZd2dZd2dZd2dZd2dZd2dZd2dZd2dZd2dZd2dZd2dZd2d Zd2d!Zd2d"Zd2d#Zd2d$Zd2d%Zd2d&Z d2d'Z!d2d(Z"d2d)Z#d2d*Z$d2d+Z%d2d,Z&d2d-Z'd2d.Z(d2d/Z)d2d0Z*d2d1Z+dS )5KeyGenTestszN
    Tests for various functions used to implement the I{ckeygen} script.
    r   Nonec                n    t                      | _        |                     t          d| j                   dS )zX
        Patch C{sys.stdout} so tests can make assertions about what's printed.
        stdoutN)r   r.   patchsysselfs    r%   setUpzKeyGenTests.setUpG   s-     jj

3$+.....r'   NkeyTyper   keySize
str | NoneprivateKeySubtypec                   |                                  }dd|d|dg}||                    d|g           ||                    d|g           t          j        |           t	          j        |          }t	          j        |dz             }|dk    r)|                     |                                d	           ni|d
k    r)|                     |                                d           n:|                     |                                |                                           | 	                    |
                                           d S )Nckeygen-t-fz--no-passphrasez-bz--private-key-subtype.pubecdsaECed25519Ed25519)mktempextend
subprocesscallr   fromFileassertEqualtypeupper
assertTrueisPublic)r2   r4   r5   r7   filenameargsprivKeypubKeys           r%   _testrunzKeyGenTests._testrunN   s2    ;;==4$:KLKKw((((KK02CDEEE,x((h/00gW\\^^T2222	!!W\\^^Y7777W\\^^W]]__===))*****r'   c                L   |                      dd           |                      ddd           |                      d           |                      dd           |                      d           |                      dd           |                      ddd           |                      d           |                      dd           |                      dd           |                      ddd           |                      d           |                      dd           d S )	Nr=   384v1)r7   r?   dsa2048rsa)rO   r1   s    r%   test_keygenerationzKeyGenTests.test_keygeneratione   s   gu%%%gu===gg666i   eV$$$eVt<<<eet444eV$$$eVt<<<eet44444r'   c                    |                                  }|                     t          j                  5  t          j        dddd|g           d d d            d S # 1 swxY w Y   d S )Nr9   r:   foor;   )rA   assertRaisesrC   CalledProcessError
check_callr2   rK   s     r%   test_runBadKeytypezKeyGenTests.test_runBadKeytypet   s    ;;==z<== 	L 	L!9dE4"JKKK	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	Ls   AA"Ac                t    t          ddi          }|                     |d         t          j                   dS )z
        L{enumrepresentation} takes a dictionary as input and returns a
        dictionary with its attributes changed to enum representation.
        formatmd5-hexN)r   assertIsr   MD5_HEXr2   optionss     r%   test_enumrepresentationz#KeyGenTests.test_enumrepresentationy   s9    
 %h	%:;;gh');)CDDDDDr'   c                t    t          ddi          }|                     |d         t          j                   dS )zF
        Test for format L{FingerprintFormats.SHA256-BASE64}.
        r_   sha256-base64N)r   ra   r   SHA256_BASE64rc   s     r%   test_enumrepresentationsha256z)KeyGenTests.test_enumrepresentationsha256   s9     %h%@AAgh');)IJJJJJr'   c                    |                      t                    5 }t          ddi           ddd           n# 1 swxY w Y   |                     d|j        j        d                    dS )z9
        Test for unsupported fingerprint format
        r_   
sha-base64N*Unsupported fingerprint format: sha-base64r   )rY   r   r   rF   	exceptionrL   )r2   ems     r%    test_enumrepresentationBadFormatz,KeyGenTests.test_enumrepresentationBadFormat   s     344 	9,7888	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	98",:KA:N	
 	
 	
 	
 	
s   9= =c                    |                                  }t          |                              t                     t	          |dd           |                     | j                                        d           dS )z
        L{printFingerprint} writes a line to standard out giving the number of
        bits of the key, its fingerprint, and the basename of the file from it
        was read.
        r`   rK   r_   z:2048 85:25:04:32:58:55:96:9f:57:ee:fb:a8:1a:ea:69:da temp
NrA   r   
setContentr   r   rF   r.   getvaluer\   s     r%   test_printFingerprintz!KeyGenTests.test_printFingerprint   sz     ;;==%%&7888h)DDEEEK  ""I	
 	
 	
 	
 	
r'   c                    |                                  }t          |                              t                     t	          |dd           |                     | j                                        d           dS )z
        L{printFigerprint} will print key fingerprint in
        L{FingerprintFormats.SHA256-BASE64} format if explicitly specified.
        rg   rq   z72048 FBTCOoknq0mHy+kpfnY9tDdcAJuWtCpuQMaV3EsvbUI= temp
Nrr   r\   s     r%   test_printFingerprintsha256z'KeyGenTests.test_printFingerprintsha256   sz    
 ;;==%%&7888h/JJKKKK  ""F	
 	
 	
 	
 	
r'   c                P   |                                  }t          |                              t                     |                     t
                    5 }t          |dd           ddd           n# 1 swxY w Y   |                     d|j        j	        d                    dS )zx
        L{printFigerprint} raises C{keys.BadFingerprintFormat} when unsupported
        formats are requested.
        rk   rq   Nrl   r   )
rA   r   rs   r   rY   r   r   rF   rm   rL   )r2   rK   rn   s      r%   )test_printFingerprintBadFingerPrintFormatz5KeyGenTests.test_printFingerprintBadFingerPrintFormat   s    
 ;;==%%&7888344 	M(lKKLLL	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M8",:KA:N	
 	
 	
 	
 	
s   A55A9<A9c                    |                                  }t          |dz                                 t                     t	          |dd           |                     | j                                        d           dS )zn
        L{printFingerprint} checks if the filename with the  '.pub' suffix
        exists in ~/.ssh.
        r<   r`   rq   z>2048 85:25:04:32:58:55:96:9f:57:ee:fb:a8:1a:ea:69:da temp.pub
Nrr   r\   s     r%   #test_printFingerprintSuffixAppendedz/KeyGenTests.test_printFingerprintSuffixAppended   s    
 ;;==F"##../@AAAh)DDEEEK  ""M	
 	
 	
 	
 	
r'   c                   t          |                                           }|                                 |                    d          j        }t          j        t                    }t          ||ddd           | 	                    | j
                                        d|d|d           | 	                    |                    |                    d                                          dd          |           | 	                    t          j        |                    d	                                                    |                                           dS )
z
        L{_saveKey} writes the private and public parts of a key to two
        different files and writes a report of this to standard out.
        id_rsa
passphraser`   rK   passr_   &Your identification has been saved in #
Your public key has been saved in m.pub
The key fingerprint in <FingerprintFormats=MD5_HEX> is:
85:25:04:32:58:55:96:9f:57:ee:fb:a8:1a:ea:69:da
N
id_rsa.pubr   rA   makedirschildpathr   
fromStringr	   r   rF   r.   rt   
getContentpublicr2   baserK   keys       r%   test_saveKeyzKeyGenTests.test_saveKey   sB   
 &&::h'',n/008\YWWXXXK  """ DL88XXXW	
 	
 	
 	NN4::h//::<<dLQQSV	
 	
 	
 	N4::l33>>@@AA3::<<	
 	
 	
 	
 	
r'   c                   t          |                                           }|                                 |                    d          j        }t          j        t                    }t          ||ddd           | 	                    | j
                                        d|d|d           | 	                    |                    |                    d                                          dd          |           | 	                    t          j        |                    d	                                                    |                                           dS )
z
        L{_saveKey} writes the private and public parts of a key to two
        different files and writes a report of this to standard out.
        Test with ECDSA key.
        id_ecdsar~   r`   r   r   r   zm.pub
The key fingerprint in <FingerprintFormats=MD5_HEX> is:
1e:ab:83:a6:f2:04:22:99:7c:64:14:d2:ab:fa:f5:16
Nzid_ecdsa.pub)r   rA   r   r   r   r   r   r   r   rF   r.   rt   r   r   r   s       r%   test_saveKeyECDSAzKeyGenTests.test_saveKeyECDSA   sB    &&::j)).n1228\YWWXXXK  """ DL88XXXW	
 	
 	
 	NN4::j11<<>>lSSUX	
 	
 	
 	N4::n55@@BBCCSZZ\\	
 	
 	
 	
 	
r'   c                   t          |                                           }|                                 |                    d          j        }t          j        t                    }t          ||ddd           | 	                    | j
                                        d|d|d           | 	                    |                    |                    d                                          dd          |           | 	                    t          j        |                    d	                                                    |                                           dS )
z
        L{_saveKey} writes the private and public parts of a key to two
        different files and writes a report of this to standard out.
        Test with Ed25519 key.
        
id_ed25519r~   r`   r   r   r   zm.pub
The key fingerprint in <FingerprintFormats=MD5_HEX> is:
ab:ee:c8:ed:e5:01:1b:45:b7:8d:b2:f0:8f:61:1c:14
Nzid_ed25519.pub)r   rA   r   r   r   r   r   r   r   rF   r.   rt   r   r   r   s       r%   test_saveKeyEd25519zKeyGenTests.test_saveKeyEd25519   sC    &&::l++0n7888\YWWXXXK  """ DL88XXXW	
 	
 	
 	NN4::l33>>@@$UU	
 	
 	
 	N4::&677BBDDEEszz||	
 	
 	
 	
 	
r'   c                   t          |                                           }|                                 |                    d          j        }t          j        t                    }t          ||ddd           | 	                    | j
                                        d|d|d           | 	                    |                    |                    d                                          dd          |           | 	                    t          j        |                    d	                                                    |                                           dS )
z
        L{_saveKey} will generate key fingerprint in
        L{FingerprintFormats.SHA256-BASE64} format if explicitly specified.
        r}   r~   rg   r   r   r   zp.pub
The key fingerprint in <FingerprintFormats=SHA256_BASE64> is:
FBTCOoknq0mHy+kpfnY9tDdcAJuWtCpuQMaV3EsvbUI=
Nr   r   r   s       r%   test_saveKeysha256zKeyGenTests.test_saveKeysha256  sK   
 &&::h'',n/00hXX	
 	
 	
 	K  """ AI(((T	
 	
 	
 	NN4::h//::<<dLQQSV	
 	
 	
 	N4::l33>>@@AA3::<<	
 	
 	
 	
 	
r'   c                   t          |                                           }|                                 |                    d          j        }t          j        t                    }|                     t                    5 }t          ||ddd           ddd           n# 1 swxY w Y   |                     d|j        j        d                    dS )zq
        L{_saveKey} raises C{keys.BadFingerprintFormat} when unsupported
        formats are requested.
        r}   r~   rk   r   Nrl   r   )r   rA   r   r   r   r   r   r	   rY   r   r   rF   rm   rL   )r2   r   rK   r   rn   s        r%    test_saveKeyBadFingerPrintformatz,KeyGenTests.test_saveKeyBadFingerPrintformat,  s   
 &&::h'',n/00344 	%||TT  	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 	8",:KA:N	
 	
 	
 	
 	
s   B$$B(+B(c                   t          |                                           }|                                 |                    d          j        }t          j        t                    }t          ||ddd           | 	                    |                    |                    d          
                                dd          |           dS )q
        L{_saveKey} will choose an empty string for the passphrase if
        no-passphrase is C{True}.
        r}   Tr`   rK   zno-passphraser_   Nr'   )r   rA   r   r   r   r   r   r	   r   rF   r   r   s       r%   test_saveKeyEmptyPassphrasez'KeyGenTests.test_saveKeyEmptyPassphrase>  s    
 &&::h'',n/00hSS	
 	
 	
 	NN4::h//::<<dCHH#	
 	
 	
 	
 	
r'   c                   t          |                                           }|                                 |                    d          j        }t          j        t                    }t          ||ddd           | 	                    |                    |                    d          
                                d          |           dS )r   r   Tr`   r   N)r   rA   r   r   r   r   r   r   r   rF   r   r   s       r%    test_saveKeyECDSAEmptyPassphrasez,KeyGenTests.test_saveKeyECDSAEmptyPassphraseN  s    
 &&::j)).n122hSS	
 	
 	
 	

:(>(>(I(I(K(KTRRTWXXXXXr'   c                   t          |                                           }|                                 |                    d          j        }t          j        t                    }t          ||ddd           | 	                    |                    |                    d          
                                d          |           dS )r   r   Tr`   r   N)r   rA   r   r   r   r   r   r   r   rF   r   r   s       r%   "test_saveKeyEd25519EmptyPassphrasez.KeyGenTests.test_saveKeyEd25519EmptyPassphrase\  s    
 &&::l++0n788hSS	
 	
 	
 	NN4::l33>>@@$GG	
 	
 	
 	
 	
r'   c                   t          |                                           }|                                 |                    d          j        g ddl}dfd}|                     |j        j        j	        d	fd
           t          j        t                    }t          |dddd|           |                    d                                          }|                    |dd          }|                     ||           dS )zd
        When no path is specified, it will ask for the path used to store the
        key.
        
custom_keyr   NrL   r    r   r   c                 2                         d           dS N appendrL   input_promptss    r%   
mock_inputz6KeyGenTests.test_saveKeyNoFilename.<locals>.mock_inputx        $$$2r'   _inputSaveFilec                    S r"    )r   keyPaths    r%   <lambda>z4KeyGenTests.test_saveKeyNoFilename.<locals>.<lambda>|  s    g r'   Tr`   r   r'   rL   r    r   r   )r   rA   r   r   r   twisted.conch.scripts.ckeygenr/   conchscriptsr9   r   r   r	   r   r   rF   )	r2   r   twistedr   r   persistedKeyContentpersistedKeyr   r   s	          @@r%   test_saveKeyNoFilenamez"KeyGenTests.test_saveKeyNoFilenamel  s   
 &&**\**/#%,,,,	 	 	 	 	 	 	

7=(02BDUDUDUDUVVVn/00	JJ	
 	
 	
 #jj66AACC~~&94EEl+++++r'   c                t   dd}t          |                                           }|                                 |                    d          j        }|                     t          j        dd            t          j        t                    }|d	d
d}| 
                    t          t          |||           dS )zs
        When the specified file exists, it will ask the user for confirmation
        before overwriting.
        rL   r    r   	list[str]c                     dgS )Nnr   )rL   s    r%   r   z6KeyGenTests.test_saveKeyFileExists.<locals>.mock_input  s	    5Lr'   r   existsc                    dS )NTr   )r   s    r%   r   z4KeyGenTests.test_saveKeyFileExists.<locals>.<lambda>  s     r'   Tr`   r   N)rL   r    r   r   )r   rA   r   r   r   r/   osr   r   r	   rY   
SystemExitr   )r2   r   r   r   r   rd   s         r%   test_saveKeyFileExistsz"KeyGenTests.test_saveKeyFileExists  s    	 	 	 	 &&**\**/

27Hnn555n/00&SS*hWjIIIIIr'   c                   t          |                                           }|                                 |                    d          j        }t          j        t                    }t          ||dddd           | 	                    | j
                                        d|d|d           |                    d                                          }| 	                    |                    |d	d          |           |                     |                    d
                     | 	                    t          j        |                    d                                                    |                                           d	S )zi
        L{_saveKey} can be told to write the new private key file in OpenSSH
        v1 format.
        r}   r~   r`   rR   )rK   r   r_   private-key-subtyper   r   r   N$   -----BEGIN OPENSSH PRIVATE KEY-----
r   )r   rA   r   r   r   r   r   r	   r   rF   r.   rt   r   rI   
startswithr   )r2   r   rK   r   privateKeyContents        r%   test_saveKeySubtypeV1z!KeyGenTests.test_saveKeySubtypeV1  s}   
 &&::h'',n/00$$#'+	 	
 	
 	
 	K  """ DL88XXXW	
 	
 	
 !JJx00;;==(94NNPSTTT(()QRR	
 	
 	
 	N4::l33>>@@AA3::<<	
 	
 	
 	
 	
r'   c                   |                                  }t          j        t                    }t	          |                              t                     t          d|i           | j        	                                
                    d                              d          }|                     ||                    d                     dS )zl
        L{displayPublicKey} prints out the public key associated with a given
        private key.
        rK   
asciiopensshN)rA   r   r   r   r   rs   r	   r   r.   rt   stripencoderF   toStringr2   rK   rN   	displayeds       r%   test_displayPublicKeyz!KeyGenTests.test_displayPublicKey  s    
 ;;== 122%%&8999*h/000K((**0066==gFF	FOOI$>$>?????r'   c                   |                                  }t          j        t                    }t	          |                              t                     t          |dd           | j        	                                
                    d                              d          }|                     ||                    d                     dS )z
        L{displayPublicKey} prints out the public key associated with a given
        private key using the given passphrase when it's encrypted.
        	encryptedrK   r   r   r   r   N)rA   r   r   r   r   rs   r
   r   r.   rt   r   r   rF   r   r   s       r%   test_displayPublicKeyEncryptedz*KeyGenTests.test_displayPublicKeyEncrypted  s    
 ;;== 122%%&BCCChDDEEEK((**0066==gFF	FOOI$>$>?????r'   c                   |                                  }t          j        t                    }t	          |                              t                     |                     t          dd            t          d|i           | j
                                                            d                              d          }|                     ||                    d                     dS )z
        L{displayPublicKey} prints out the public key associated with a given
        private key, asking for the passphrase when it's encrypted.
        getpassc                    dS )Nr   r   )xs    r%   r   zLKeyGenTests.test_displayPublicKeyEncryptedPassphrasePrompt.<locals>.<lambda>  s     r'   rK   r   r   r   N)rA   r   r   r   r   rs   r
   r/   r   r   r.   rt   r   r   rF   r   r   s       r%   .test_displayPublicKeyEncryptedPassphrasePromptz:KeyGenTests.test_displayPublicKeyEncryptedPassphrasePrompt  s    
 ;;== 122%%&BCCC

7I'<'<===*h/000K((**0066==gFF	FOOI$>$>?????r'   c                    |                                  }t          |                              t                     |                     t
          t          |dd           dS )z
        L{displayPublicKey} fails with a L{BadKeyError} when trying to decrypt
        an encrypted key with the wrong password.
        wrongr   N)rA   r   rs   r
   rY   r   r   r\   s     r%   $test_displayPublicKeyWrongPassphrasez0KeyGenTests.test_displayPublicKeyWrongPassphrase  s`    
 ;;==%%&BCCC)'+R+R	
 	
 	
 	
 	
r'   c                   t          ddd          }|                     t          d|           |                                 }t	          |                              t                     t          d|i           |                     | j	        
                                                    d          d           |                     t          t	          |                                                     dS )zt
        L{changePassPhrase} allows a user to change the passphrase of a
        private key interactively.
        r   newpassr   rK   r   ;Your identification has been saved with the new passphrase.Nr)   r/   r   rA   r   rs   r
   r   rF   r.   rt   r   assertNotEqualr   )r2   oldNewConfirmrK   s      r%   test_changePassphrasez!KeyGenTests.test_changePassphrase  s    
 $KIFF

7I}555;;==%%&BCCC*h/000K  ""((..I	
 	
 	
 	((8*<*<*G*G*I*I	
 	
 	
 	
 	
r'   c                   t          dd          }|                     t          d|           |                                 }t	          |                              t                     t          |dd           |                     | j	        
                                                    d          d           |                     t          t	          |                                                     dS )z
        L{changePassPhrase} allows a user to change the passphrase of a
        private key, providing the old passphrase and prompting for new one.
        r   r   r   r   r   r   Nr   )r2   
newConfirmrK   s      r%   test_changePassphraseWithOldz(KeyGenTests.test_changePassphraseWithOld   s    
 !I66


7Iz222;;==%%&BCCChDDEEEK  ""((..I	
 	
 	
 	((8*<*<*G*G*I*I	
 	
 	
 	
 	
r'   c                   |                                  }t          |                              t                     t	          |ddd           |                     | j                                                            d          d           | 	                    t          t          |          
                                           dS )z
        L{changePassPhrase} allows a user to change the passphrase of a private
        key by providing both old and new passphrases without prompting.
        r   
newencrypt)rK   r   r   r   r   N)rA   r   rs   r
   r   rF   r.   rt   r   r   r   r\   s     r%   test_changePassphraseWithBothz)KeyGenTests.test_changePassphraseWithBoth  s    
 ;;==%%&BCCC!;<PP	
 	
 	
 	K  ""((..I	
 	
 	
 	((8*<*<*G*G*I*I	
 	
 	
 	
 	
r'   c                ~   |                                  }t          |                              t                     |                     t
          t          |dd          }|                     dt          |                     |                     t          t          |          	                                           dS )z
        L{changePassPhrase} exits if passed an invalid old passphrase when
        trying to change the passphrase of a private key.
        r   r   z1Could not change passphrase: old passphrase errorN)
rA   r   rs   r
   rY   r   r   rF   r   r   r2   rK   errors      r%   $test_changePassphraseWrongPassphrasez0KeyGenTests.test_changePassphraseWrongPassphrase'  s    
 ;;==%%&BCCC!!(x*Q*Q
 
 	?U	
 	
 	
 	5x7I7I7T7T7V7VWWWWWr'   c                   |                      t          dt          d                     |                                 }t	          |                              t                     |                     t          t          d|i          }| 
                    dt          |                     | 
                    t          t	          |                                                     dS )z
        L{changePassPhrase} exits if no passphrase is specified for the
        C{getpass} call and the key is encrypted.
        r   r   rK   zMCould not change passphrase: Passphrase must be provided for an encrypted keyN)r/   r   r)   rA   r   rs   r
   rY   r   r   rF   r   r   r   s      r%   !test_changePassphraseEmptyGetPassz-KeyGenTests.test_changePassphraseEmptyGetPass6  s    
 	

7I{2777;;==%%&BCCC!!*.>X@VWW#JJ	
 	
 	

 	5x7I7I7T7T7V7VWWWWWr'   c                l   |                                  }t          |                              d           |                     t          t
          d|i          }d}|                     |t          |                     |                     dt          |                                                     dS )zc
        L{changePassPhrase} exits if the file specified points to an invalid
        key.
        s   foobarrK   z?Could not change passphrase: cannot guess the type of b'foobar'N)	rA   r   rs   rY   r   r   rF   r   r   )r2   rK   r   expecteds       r%   test_changePassphraseBadKeyz'KeyGenTests.test_changePassphraseBadKeyF  s    
 ;;==%%i000!!*.>X@VWWW3u::...HX$6$6$A$A$C$CDDDDDr'   c                   |                                  }t          |                              t                     dd}|                     t
          d|           |                     t          t          |dd	          }| 	                    d
t          |                     | 	                    t          t          |                                                     dS )z
        L{changePassPhrase} doesn't modify the key file if an unexpected error
        happens when trying to create the key with the new passphrase.
        rL   r    kwargsr   r   c                      t          d          )Noops)RuntimeErrorrL   r   s     r%   r   z>KeyGenTests.test_changePassphraseCreateError.<locals>.toString[  s    v&&&r'   r   r   rK   r   z!Could not change passphrase: oopsN)rL   r    r   r    r   r   rA   r   rs   r	   r/   r   rY   r   r   rF   r   r   )r2   rK   r   r   s       r%    test_changePassphraseCreateErrorz,KeyGenTests.test_changePassphraseCreateErrorS  s    
 ;;==%%&8999	' 	' 	' 	' 	

3
H---!!!l;;
 
 	<c%jjIII+Xh-?-?-J-J-L-LMMMMMr'   c                   |                                  }t          |                              t                     dd}|                     t
          d|           |                     t          t          |dd	          }d
}| 	                    |t          |                     | 	                    t          t          |                                                     dS )zq
        L{changePassPhrase} doesn't modify the key file if C{toString} returns
        an empty string.
        rL   r    r   r   r   c                     dS r   r   r   s     r%   r   zCKeyGenTests.test_changePassphraseEmptyStringError.<locals>.toStringr  s    2r'   r   r   r   z9Could not change passphrase: cannot guess the type of b''N)rL   r    r   r    r   r   r   )r2   rK   r   r   r   s        r%   %test_changePassphraseEmptyStringErrorz1KeyGenTests.test_changePassphraseEmptyStringErrorj  s    
 ;;==%%&8999	 	 	 	 	

3
H---!!!l;;
 
 O3u::...+Xh-?-?-J-J-L-LMMMMMr'   c                ~   |                                  }t          |                              t                     |                     t
          t          |dd          }|                     dt          |                     |                     t          t          |          	                                           dS )z
        L{changePassPhrase} exits when trying to change the passphrase on a
        public key, and doesn't change the file.
        r   r   z.Could not change passphrase: key not encryptedN)
rA   r   rs   r   rY   r   r   rF   r   r   r   s      r%   test_changePassphrasePublicKeyz*KeyGenTests.test_changePassphrasePublicKey  s    
 ;;==%%&7888!!(xF*S*S
 
 	I3u::VVV*HX,>,>,I,I,K,KLLLLLr'   c                B   t          ddd          }|                     t          d|           |                                 }t	          |                              t                     t          |dd           |                     | j	        
                                                    d          d           t	          |                                          }|                     t          |           |                     |                    d                     d	S )
zq
        L{changePassPhrase} can be told to write the new private key file in
        OpenSSH v1 format.
        r   r   r   rR   )rK   r   r   r   r   N)r)   r/   r   rA   r   rs   r
   r   rF   r.   rt   r   r   r   rI   r   )r2   r   rK   r   s       r%   test_changePassphraseSubtypeV1z*KeyGenTests.test_changePassphraseSubtypeV1  s   
 $KIFF

7I}555;;==%%&BCCChtLLMMMK  ""((..I	
 	
 	
 %X..99;;8:KLLL(()QRR	
 	
 	
 	
 	
r'   c                x   g dfd}ddi}t          ||          }|                     |d         d           |                     |                    t          j                            dd	                               |                     d
t                               |                     dg           dS )zg
        L{options} will default to "~/.ssh/id_rsa" if the user doesn't
        specify a key.
        rL   r    r   r   c                 2                         d           dS r   r   r   s    r%   r   z5KeyGenTests.test_useDefaultForKey.<locals>.mock_input  r   r'   rK   r   z.sshr}      Nr   )r   rF   rI   endswithr   r   joinlen)r2   r   rd   rK   r   s       @r%   test_useDefaultForKeyz!KeyGenTests.test_useDefaultForKey  s    
 $&	 	 	 	 	 	 r"#GZ88J	
 	
 	

 	))"',,vx*H*HIIJJJ 	C..///"}-----r'   c                    ddi}|                      t          t          |          }|                     d|j        d                    dS )z
        Ensure FileNotFoundError is handled, whether the user has supplied
        a bad path, or has no key at the default path.
        rK   /foo/bar+could not be opened, please specify a file.r   N)rY   r   r   assertInrL   r2   rd   excs      r%   'test_displayPublicKeyHandleFileNotFoundz3KeyGenTests.test_displayPublicKeyHandleFileNotFound  sG    
 z*
,<gFFCSXa[QQQQQr'   c                    ddi}|                      t          t          |          }|                     d|j        d                    dS )N
        Ensure FileNotFoundError is handled for an invalid filename.
        rK   r  r  r   N)rY   r   r   r  rL   r  s      r%   'test_changePassPhraseHandleFileNotFoundz3KeyGenTests.test_changePassPhraseHandleFileNotFound  sG     z*
,<gFFCSXa[QQQQQr'   c                    ddd}|                      t          t          |          }|                     d|j        d                    dS )r  r  r`   rq   r  r   N)rY   r   r   r  rL   r  s      r%   'test_printFingerprintHandleFileNotFoundz3KeyGenTests.test_printFingerprintHandleFileNotFound  sJ      *Y??
,<gFFCSXa[QQQQQr'   )r   r,   )NN)r4   r   r5   r6   r7   r6   r   r,   ),__name__
__module____qualname____doc__r3   rO   rV   r]   re   ri   ro   ru   rw   ry   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r   r'   r%   r+   r+   B   s        / / / / #(,	+ + + + +.5 5 5 5L L L L
E E E EK K K K
 
 
 

 
 
 

 
 
 

 
 
 

 
 
 

 
 
 
0
 
 
 
2
 
 
 
4
 
 
 
4
 
 
 
$
 
 
 
 Y Y Y Y
 
 
 
 , , , ,8J J J J$ 
  
  
  
D
@ 
@ 
@ 
@
@ 
@ 
@ 
@@ @ @ @	
 	
 	
 	

 
 
 
(
 
 
 
(
 
 
 
&X X X XX X X X E E E EN N N N.N N N N0M M M M
 
 
 
,. . . .2R R R RR R R RR R R R R Rr'   r+   )r   r   r   r   )(r  
__future__r   r   r   rC   r0   ior   typingr   typing_extensionsr   twisted.conch.test.keydatar   r   r	   r
   r   twisted.python.filepathr   twisted.python.reflectr   twisted.trial.unittestr   r   r   r   r   r   r   r   twisted.conch.ssh.keysr   r   r   r   skipr)   r+   r   r'   r%   <module>r&     s    # " " " " "  				     



             & & & & & &              - , , , , , 0 0 0 0 0 0 + + + + + +=   E                            ED   $S
R S
R S
R S
R S
R( S
R S
R S
R S
R S
Rr'   