
    Vhfk                       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'y)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.ckeygenc                 *    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}.
    c                    t              S N)next)_passphrasesIters    O/home/dcms/DCMS/lib/python3.12/site-packages/twisted/conch/test/test_ckeygen.pyfakeGetpassz makeGetpass.<locals>.fakeGetpass<   s    O$$    )r   objectreturnstr)iter)passphrasesr!   r   s     @r    makeGetpassr(   0   s     ;'O% r"   c                  d   e Zd ZdZd+dZ	 	 d,	 	 	 	 	 	 	 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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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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(d+d(Z)d+d)Z*d+d*Z+y).KeyGenTestszN
    Tests for various functions used to implement the I{ckeygen} script.
    c                d    t               | _        | j                  t        d| j                         y)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!     j

3$++.r"   Nc                >   | j                         }dd|d|dg}||j                  d|g       ||j                  d|g       t        j                  |       t	        j
                  |      }t	        j
                  |dz         }|dk(  r!| j                  |j                         d	       nT|d
k(  r!| j                  |j                         d       n.| j                  |j                         |j                                | j                  |j                                y )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)r0   keyTypekeySizeprivateKeySubtypefilenameargsprivKeypubKeys           r    _testrunzKeyGenTests._testrunN   s     ;;=4$:KLKKw((KK02CDE,,x(h/0gW\\^T2	!W\\^Y7W\\^W]]_=)*r"   c                   | j                  dd       | j                  ddd       | j                  d       | j                  dd       | j                  d       | j                  dd       | j                  ddd       | j                  d       | j                  dd       | j                  dd       | j                  ddd       | j                  d       | j                  dd       y )	Nr7   384v1)rG   r9   dsa2048rsa)rL   r/   s    r    test_keygenerationzKeyGenTests.test_keygeneratione   s    gu%gu=gg6i eV$eVt<eet4eV$eVt<eet4r"   c                    | j                         }| j                  t        j                        5  t        j                  dddd|g       d d d        y # 1 sw Y   y xY w)Nr3   r4   foor5   )r;   assertRaisesr=   CalledProcessError
check_callr0   rH   s     r    test_runBadKeytypezKeyGenTests.test_runBadKeytypet   sS    ;;=z<<= 	L!!9dE4"JK	L 	L 	Ls   AAc                d    t        ddi      }| j                  |d   t        j                         y)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r0   optionss     r    test_enumrepresentationz#KeyGenTests.test_enumrepresentationy   s-    
 %h	%:;gh');)C)CDr"   c                d    t        ddi      }| j                  |d   t        j                         y)zF
        Test for format L{FingerprintFormats.SHA256-BASE64}.
        r\   sha256-base64N)r   r^   r   SHA256_BASE64r`   s     r    test_enumrepresentationsha256z)KeyGenTests.test_enumrepresentationsha256   s-     %h%@Agh');)I)IJr"   c                    | j                  t              5 }t        ddi       ddd       | j                  dj                  j
                  d          y# 1 sw Y   3xY w)z9
        Test for unsupported fingerprint format
        r\   
sha-base64N*Unsupported fingerprint format: sha-base64r   )rV   r   r   r@   	exceptionrI   )r0   ems     r     test_enumrepresentationBadFormatz,KeyGenTests.test_enumrepresentationBadFormat   sY     34 	9,78	98",,:K:KA:N	
	9 	9s   AAc                    | j                         }t        |      j                  t               t	        |dd       | j                  | j                  j                         d       y)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]   rH   r\   z:2048 85:25:04:32:58:55:96:9f:57:ee:fb:a8:1a:ea:69:da temp
Nr;   r   
setContentr   r   r@   r,   getvaluerY   s     r    test_printFingerprintz!KeyGenTests.test_printFingerprint   sQ     ;;=%%&78h)DEKK  "I	
r"   c                    | j                         }t        |      j                  t               t	        |dd       | j                  | j                  j                         d       y)z
        L{printFigerprint} will print key fingerprint in
        L{FingerprintFormats.SHA256-BASE64} format if explicitly specified.
        rd   rn   z72048 FBTCOoknq0mHy+kpfnY9tDdcAJuWtCpuQMaV3EsvbUI= temp
Nro   rY   s     r    test_printFingerprintsha256z'KeyGenTests.test_printFingerprintsha256   sQ    
 ;;=%%&78h/JKKK  "F	
r"   c                "   | j                         }t        |      j                  t               | j	                  t
              5 }t        |dd       ddd       | j                  dj                  j                  d          y# 1 sw Y   3xY w)zx
        L{printFigerprint} raises C{keys.BadFingerprintFormat} when unsupported
        formats are requested.
        rh   rn   Nri   r   )
r;   r   rp   r   rV   r   r   r@   rj   rI   )r0   rH   rk   s      r    )test_printFingerprintBadFingerPrintFormatz5KeyGenTests.test_printFingerprintBadFingerPrintFormat   s|    
 ;;=%%&7834 	M(lKL	M8",,:K:KA:N	
	M 	Ms   BBc                    | j                         }t        |dz         j                  t               t	        |dd       | j                  | j                  j                         d       y)zn
        L{printFingerprint} checks if the filename with the  '.pub' suffix
        exists in ~/.ssh.
        r6   r]   rn   z>2048 85:25:04:32:58:55:96:9f:57:ee:fb:a8:1a:ea:69:da temp.pub
Nro   rY   s     r    #test_printFingerprintSuffixAppendedz/KeyGenTests.test_printFingerprintSuffixAppended   sV    
 ;;=F"#../@Ah)DEKK  "M	
r"   c                `   t        | j                               }|j                          |j                  d      j                  }t        j                  t              }t        ||ddd       | j                  | j                  j                         d|d|d       | j                  |j                  |j                  d      j                         dd      |       | j                  t        j                  |j                  d	      j                               |j                                y)
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]   rH   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   r;   makedirschildpathr   
fromStringr	   r   r@   r,   rq   
getContentpublicr0   baserH   keys       r    test_saveKeyzKeyGenTests.test_saveKey   s    
 &::h',,nn/08\YWXKK  " DLXW	
 	NN4::h/::<dLQSV	
 	NN4::l3>>@A3::<	
r"   c                `   t        | j                               }|j                          |j                  d      j                  }t        j                  t              }t        ||ddd       | j                  | j                  j                         d|d|d       | j                  |j                  |j                  d      j                         dd      |       | j                  t        j                  |j                  d	      j                               |j                                y)
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   r;   r   r   r   r   r   r   r   r@   r,   rq   r   r   r   s       r    test_saveKeyECDSAzKeyGenTests.test_saveKeyECDSA   s     &::j)..nn128\YWXKK  " DLXW	
 	NN4::j1<<>lSUX	
 	NN4::n5@@BCSZZ\	
r"   c                `   t        | j                               }|j                          |j                  d      j                  }t        j                  t              }t        ||ddd       | j                  | j                  j                         d|d|d       | j                  |j                  |j                  d      j                         dd      |       | j                  t        j                  |j                  d	      j                               |j                                y)
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   r;   r   r   r   r   r   r   r   r@   r,   rq   r   r   r   s       r    test_saveKeyEd25519zKeyGenTests.test_saveKeyEd25519   s     &::l+00nn788\YWXKK  " DLXW	
 	NN4::l3>>@$U	
 	NN4::&67BBDEszz|	
r"   c                `   t        | j                               }|j                          |j                  d      j                  }t        j                  t              }t        ||ddd       | j                  | j                  j                         d|d|d       | j                  |j                  |j                  d      j                         dd      |       | j                  t        j                  |j                  d	      j                               |j                                y)
z
        L{_saveKey} will generate key fingerprint in
        L{FingerprintFormats.SHA256-BASE64} format if explicitly specified.
        rz   r{   rd   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  s    
 &::h',,nn/0hX	
 	KK  " AI(T	
 	NN4::h/::<dLQSV	
 	NN4::l3>>@A3::<	
r"   c                   t        | j                               }|j                          |j                  d      j                  }t        j                  t              }| j                  t              5 }t        ||ddd       ddd       | j                  dj                  j                  d          y# 1 sw Y   3xY w)zq
        L{_saveKey} raises C{keys.BadFingerprintFormat} when unsupported
        formats are requested.
        rz   r{   rh   r|   Nri   r   )r   r;   r   r   r   r   r   r	   rV   r   r   r@   rj   rI   )r0   r   rH   r   rk   s        r     test_saveKeyBadFingerPrintformatz,KeyGenTests.test_saveKeyBadFingerPrintformat,  s    
 &::h',,nn/034 	%||T	
 	8",,:K:KA:N	
	 	s   3B66B?c                ^   t        | j                               }|j                          |j                  d      j                  }t        j                  t              }t        ||ddd       | j                  |j                  |j                  d      j                         dd      |       y)q
        L{_saveKey} will choose an empty string for the passphrase if
        no-passphrase is C{True}.
        rz   Tr]   rH   zno-passphraser\   Nr"   )r   r;   r   r   r   r   r   r	   r   r@   r   r   s       r    test_saveKeyEmptyPassphrasez'KeyGenTests.test_saveKeyEmptyPassphrase>  s    
 &::h',,nn/0hS	
 	NN4::h/::<dCH#	
r"   c                \   t        | j                               }|j                          |j                  d      j                  }t        j                  t              }t        ||ddd       | j                  |j                  |j                  d      j                         d      |       y)r   r   Tr]   r   N)r   r;   r   r   r   r   r   r   r   r@   r   r   s       r     test_saveKeyECDSAEmptyPassphrasez,KeyGenTests.test_saveKeyECDSAEmptyPassphraseN  s    
 &::j)..nn12hS	
 	

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

7==((002BDUVnn/0	J	
 #jj6AAC~~&94El+r"   c                P   d	d}t        | j                               }|j                          |j                  d      j                  }| j                  t        j                  dd        t        j                  t              }|ddd}| j                  t        t        |||       y)
zs
        When the specified file exists, it will ask the user for confirmation
        before overwriting.
        c                     dgS )Nnr   )rI   s    r    r   z6KeyGenTests.test_saveKeyFileExists.<locals>.mock_input  s	    5Lr"   r   existsc                     y)NTr   )r   s    r    r   z4KeyGenTests.test_saveKeyFileExists.<locals>.<lambda>      r"   Tr]   r   N)rI   r#   r$   z	list[str])r   r;   r   r   r   r-   osr   r   r	   rV   
SystemExitr   )r0   r   r   r   r   ra   s         r    test_saveKeyFileExistsz"KeyGenTests.test_saveKeyFileExists  s{    	 &**\*//

277Hn5nn/0&S*hWjIr"   c                   t        | j                               }|j                          |j                  d      j                  }t        j                  t              }t        ||dddd       | j                  | j                  j                         d|d|d       |j                  d      j                         }| j                  |j                  |d	d      |       | j                  |j                  d
             | j                  t        j                  |j                  d      j                               |j                                y	)zi
        L{_saveKey} can be told to write the new private key file in OpenSSH
        v1 format.
        rz   r{   r]   rO   )rH   r}   r\   private-key-subtyper~   r   r   N$   -----BEGIN OPENSSH PRIVATE KEY-----
r   )r   r;   r   r   r   r   r   r	   r   r@   r,   rq   r   rC   
startswithr   )r0   r   rH   r   privateKeyContents        r    test_saveKeySubtypeV1z!KeyGenTests.test_saveKeySubtypeV1  s   
 &::h',,nn/0$$#'+		
 	KK  " DLXW	
 !JJx0;;=(94NPST(()QR	
 	NN4::l3>>@A3::<	
r"   c                ^   | j                         }t        j                  t              }t	        |      j                  t               t        d|i       | j                  j                         j                  d      j                  d      }| j                  ||j                  d             y)zl
        L{displayPublicKey} prints out the public key associated with a given
        private key.
        rH   
asciiopensshN)r;   r   r   r   r   rp   r	   r   r,   rq   stripencoder@   toStringr0   rH   rK   	displayeds       r    test_displayPublicKeyz!KeyGenTests.test_displayPublicKey  s    
 ;;= 12%%&89*h/0KK((*006==gF	FOOI$>?r"   c                `   | j                         }t        j                  t              }t	        |      j                  t               t        |dd       | j                  j                         j                  d      j                  d      }| j                  ||j                  d             y)z
        L{displayPublicKey} prints out the public key associated with a given
        private key using the given passphrase when it's encrypted.
        	encryptedrH   r}   r   r   r   N)r;   r   r   r   r   rp   r
   r   r,   rq   r   r   r@   r   r   s       r    test_displayPublicKeyEncryptedz*KeyGenTests.test_displayPublicKeyEncrypted  s    
 ;;= 12%%&BChDEKK((*006==gF	FOOI$>?r"   c                   | j                         }t        j                  t              }t	        |      j                  t               | j                  t        dd        t        d|i       | j                  j                         j                  d      j                  d      }| j                  ||j                  d             y)z
        L{displayPublicKey} prints out the public key associated with a given
        private key, asking for the passphrase when it's encrypted.
        getpassc                     y)Nr   r   )xs    r    r   zLKeyGenTests.test_displayPublicKeyEncryptedPassphrasePrompt.<locals>.<lambda>  r   r"   rH   r   r   r   N)r;   r   r   r   r   rp   r
   r-   r   r   r,   rq   r   r   r@   r   r   s       r    .test_displayPublicKeyEncryptedPassphrasePromptz:KeyGenTests.test_displayPublicKeyEncryptedPassphrasePrompt  s    
 ;;= 12%%&BC

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

7I}5;;=%%&BC*h/0KK  "((.I	
 	((8*<*G*G*I	
r"   c                   t        dd      }| j                  t        d|       | j                         }t	        |      j                  t               t        |dd       | j                  | j                  j                         j                  d      d       | j                  t        t	        |      j                                y)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   )r0   
newConfirmrH   s      r    test_changePassphraseWithOldz(KeyGenTests.test_changePassphraseWithOld   s    
 !I6


7Iz2;;=%%&BChDEKK  "((.I	
 	((8*<*G*G*I	
r"   c                J   | j                         }t        |      j                  t               t	        |ddd       | j                  | j                  j                         j                  d      d       | j                  t        t        |      j                                y)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)rH   r}   r   r   r   N)r;   r   rp   r
   r   r@   r,   rq   r   r   r   rY   s     r    test_changePassphraseWithBothz)KeyGenTests.test_changePassphraseWithBoth  s    
 ;;=%%&BC!;<P	
 	KK  "((.I	
 	((8*<*G*G*I	
r"   c                ,   | j                         }t        |      j                  t               | j	                  t
        t        |dd      }| j                  dt        |             | j                  t        t        |      j                                y)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)
r;   r   rp   r
   rV   r   r   r@   r%   r   r0   rH   errors      r    $test_changePassphraseWrongPassphrasez0KeyGenTests.test_changePassphraseWrongPassphrase'  sz    
 ;;=%%&BC!!(x*Q
 	?U	
 	5x7I7T7T7VWr"   c                j   | j                  t        dt        d             | j                         }t	        |      j                  t               | j                  t        t        d|i      }| j                  dt        |             | j                  t        t	        |      j                                y)z
        L{changePassPhrase} exits if no passphrase is specified for the
        C{getpass} call and the key is encrypted.
        r   r   rH   zMCould not change passphrase: Passphrase must be provided for an encrypted keyN)r-   r   r(   r;   r   rp   r
   rV   r   r   r@   r%   r   r   s      r    !test_changePassphraseEmptyGetPassz-KeyGenTests.test_changePassphraseEmptyGetPass6  s    
 	

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

 	5x7I7T7T7VWr"   c                   | j                         }t        |      j                  d       | j                  t        t
        d|i      }d}| j                  |t        |             | j                  dt        |      j                                y)zc
        L{changePassPhrase} exits if the file specified points to an invalid
        key.
        s   foobarrH   z?Could not change passphrase: cannot guess the type of b'foobar'N)	r;   r   rp   rV   r   r   r@   r%   r   )r0   rH   r   expecteds       r    test_changePassphraseBadKeyz'KeyGenTests.test_changePassphraseBadKeyF  ss    
 ;;=%%i0!!*.>X@VWW3u:.HX$6$A$A$CDr"   c                b   | j                         }t        |      j                  t               dd}| j	                  t
        d|       | j                  t        t        |dd      }| j                  dt        |             | j                  t        t        |      j                                y)z
        L{changePassPhrase} doesn't modify the key file if an unexpected error
        happens when trying to create the key with the new passphrase.
        c                     t        d      )Noops)RuntimeErrorrI   kwargss     r    r   z>KeyGenTests.test_changePassphraseCreateError.<locals>.toString[  s    v&&r"   r   r   rH   r   z!Could not change passphrase: oopsN)rI   r#   r   r#   r$   r   r;   r   rp   r	   r-   r   rV   r   r   r@   r%   r   )r0   rH   r   r   s       r     test_changePassphraseCreateErrorz,KeyGenTests.test_changePassphraseCreateErrorS  s    
 ;;=%%&89	' 	

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

3
H-!!!l;
 O3u:.+Xh-?-J-J-LMr"   c                ,   | j                         }t        |      j                  t               | j	                  t
        t        |dd      }| j                  dt        |             | j                  t        t        |      j                                y)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)
r;   r   rp   r   rV   r   r   r@   r%   r   r   s      r    test_changePassphrasePublicKeyz*KeyGenTests.test_changePassphrasePublicKey  sv    
 ;;=%%&78!!(xF*S
 	I3u:V*HX,>,I,I,KLr"   c                   t        ddd      }| j                  t        d|       | j                         }t	        |      j                  t               t        |dd       | j                  | j                  j                         j                  d      d       t	        |      j                         }| j                  t        |       | j                  |j                  d             y	)
zq
        L{changePassPhrase} can be told to write the new private key file in
        OpenSSH v1 format.
        r   r   r   rO   )rH   r   r   r   r   N)r(   r-   r   r;   r   rp   r
   r   r@   r,   rq   r   r   r   rC   r   )r0   r   rH   r   s       r    test_changePassphraseSubtypeV1z*KeyGenTests.test_changePassphraseSubtypeV1  s    
 $KIF

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

 	))"'',,vx*HIJ 	C./"}-r"   c                    ddi}| j                  t        t        |      }| j                  d|j                  d          y)z
        Ensure FileNotFoundError is handled, whether the user has supplied
        a bad path, or has no key at the default path.
        rH   /foo/bar+could not be opened, please specify a file.r   N)rV   r   r   assertInrI   r0   ra   excs      r    'test_displayPublicKeyHandleFileNotFoundz3KeyGenTests.test_displayPublicKeyHandleFileNotFound  s:    
 z*
,<gFCSXXa[Qr"   c                    ddi}| j                  t        t        |      }| j                  d|j                  d          y)N
        Ensure FileNotFoundError is handled for an invalid filename.
        rH   r
  r  r   N)rV   r   r   r  rI   r  s      r    'test_changePassPhraseHandleFileNotFoundz3KeyGenTests.test_changePassPhraseHandleFileNotFound  s:     z*
,<gFCSXXa[Qr"   c                    ddd}| j                  t        t        |      }| j                  d|j                  d          y)r  r
  r]   rn   r  r   N)rV   r   r   r  rI   r  s      r    'test_printFingerprintHandleFileNotFoundz3KeyGenTests.test_printFingerprintHandleFileNotFound  s:      *Y?
,<gFCSXXa[Qr"   )r$   None)NN)rE   r%   rF   
str | NonerG   r  r$   r  ),__name__
__module____qualname____doc__r1   rL   rS   rZ   rb   rf   rl   rr   rt   rv   rx   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L
EK





0
2
4
4
$
 Y
 ,8J$ 
D
@
@@	

(
(
&XX EN.N0M
,.2RRRr"   r*   )r'   r%   r$   zCallable[[object], str])(r  
__future__r   r   r   r=   r.   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%     sr    #  	  
   &  - 0 +    ED$S
R( S
Rr"   