Ticket #6607: 6607_2.patch

File 6607_2.patch, 4.9 KB (added by Saurabh, 9 years ago)
  • twisted/conch/test/test_ckeygen.py

     
    6868        self.assertEqual(
    6969            self.stdout.getvalue(),
    7070            '768 3d:13:5f:cb:c9:79:8a:93:06:27:65:bc:3d:0b:8f:af temp\n')
     71   
    7172
     73    def test_printFingerprintDefultKeyfile(self):
     74        """
     75        L{printFingerprint} should provide a default keyfile if nothing is
     76        specified.
     77        """
     78        def fake_input(str):
     79            return ''
     80        filename = self.mktemp()
     81        FilePath(filename).setContent(publicRSA_openssh)
     82        printFingerprint(
     83                {'filename':''},raw_input=fake_input,default_file = filename)
     84        self.assertEqual(
     85            self.stdout.getvalue(),
     86            '768 3d:13:5f:cb:c9:79:8a:93:06:27:65:bc:3d:0b:8f:af temp\n')
    7287
     88
    7389    def test_saveKey(self):
    7490        """
    7591        L{_saveKey} writes the private and public parts of a key to two
     
    131147            pubKey.toString('openssh'))
    132148
    133149
     150    def test_displayPublicKeyDefaultKeyfile(self):
     151        """
     152        L{displayPublicKey} should provide a default keyfile if nothing is
     153        specified.
     154        """
     155        def fake_input(str):
     156            return ''
     157        filename = self.mktemp()
     158        pubKey = Key.fromString(publicRSA_openssh)
     159        FilePath(filename).setContent(privateRSA_openssh)
     160        try:
     161            displayPublicKey(
     162                    {'filename': ''},raw_input=fake_input,
     163                    default_file=filename)
     164        except:
     165            self.fail()
     166
     167
    134168    def test_displayPublicKeyEncrypted(self):
    135169        """
    136170        L{displayPublicKey} prints out the public key associated with a given
     
    338372        self.assertEqual(
    339373            'Could not change passphrase: key not encrypted', str(error))
    340374        self.assertEqual(publicRSA_openssh, FilePath(filename).getContent())
     375
     376
     377    def test_changePassphraseDefaultkeyfile(self):
     378        """
     379        L{changePassPhrase} should provide a default for keyfile if nothing
     380        is specified.
     381        """
     382        def fake_input(str):
     383            return ''
     384
     385        oldNewConfirm = makeGetpass('encrypted', 'newpass', 'newpass')
     386        self.patch(getpass, 'getpass', oldNewConfirm)
     387
     388        filename = self.mktemp()
     389        FilePath(filename).setContent(privateRSA_openssh_encrypted)
     390
     391        try:
     392            changePassPhrase({'filename': ''},raw_input=fake_input,default_file=filename)
     393        except:
     394            self.fail()
  • twisted/conch/topfiles/6607.feature

     
     1ckeygen now provides default keyfile for --fingerprint,--showpub and --changepass.
  • twisted/conch/scripts/ckeygen.py

     
    9999
    100100
    101101
    102 def printFingerprint(options):
     102def printFingerprint(options,raw_input=raw_input,default_file='~/.ssh/id_rsa'):
    103103    if not options['filename']:
    104         filename = os.path.expanduser('~/.ssh/id_rsa')
    105         options['filename'] = raw_input('Enter file in which the key is (%s): ' % filename)
     104        filename = os.path.expanduser(default_file)
     105        options['filename'] = raw_input(
     106                'Enter file in which the key is (%s): ' % filename
     107                ).strip() or filename
    106108    if os.path.exists(options['filename']+'.pub'):
    107109        options['filename'] += '.pub'
    108110    try:
     
    118120
    119121
    120122
    121 def changePassPhrase(options):
     123def changePassPhrase(options,raw_input=raw_input,default_file='~/.ssh/id_rsa'):
    122124    if not options['filename']:
    123         filename = os.path.expanduser('~/.ssh/id_rsa')
     125        filename = os.path.expanduser(default_file)
    124126        options['filename'] = raw_input(
    125             'Enter file in which the key is (%s): ' % filename)
     127            'Enter file in which the key is (%s): ' % filename
     128            ).strip() or filename
    126129    try:
    127130        key = keys.Key.fromFile(options['filename']).keyObject
    128131    except keys.EncryptedKeyError as e:
     
    168171
    169172
    170173
    171 def displayPublicKey(options):
     174def displayPublicKey(options,raw_input=raw_input,default_file='~/.ssh/id_rsa'):
    172175    if not options['filename']:
    173         filename = os.path.expanduser('~/.ssh/id_rsa')
    174         options['filename'] = raw_input('Enter file in which the key is (%s): ' % filename)
     176        filename = os.path.expanduser(default_file)
     177        options['filename'] = raw_input(
     178                'Enter file in which the key is (%s): ' % filename
     179                ).strip() or filename
    175180    try:
    176181        key = keys.Key.fromFile(options['filename']).keyObject
    177182    except keys.EncryptedKeyError: