Ticket #6607: 6607_3.patch

File 6607_3.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':''}, default_file=filename, _raw_input=fake_input)
     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        displayPublicKey(
     161                    {'filename': ''}, default_file=filename,
     162                    _raw_input=fake_input)
     163        self.assertEqual(
     164            self.stdout.getvalue().strip('\n'),
     165            pubKey.toString('openssh'))
     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': ''}, default_file=filename,
     393                    _raw_input=fake_input)
     394        except IOError:
     395            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 _getFilename(default_file, _raw_input):
     103    filename = os.path.expanduser(default_file)
     104    filename = _raw_input(
     105                'Enter file in which the key is (%s): ' % filename
     106                ).strip() or filename
     107    return filename
     108
     109
     110
     111def printFingerprint(options, default_file='~/.ssh/id_rsa', _raw_input=raw_input):
    103112    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)
     113        options['filename'] = _getFilename(default_file, _raw_input)
    106114    if os.path.exists(options['filename']+'.pub'):
    107115        options['filename'] += '.pub'
    108116    try:
     
    118126
    119127
    120128
    121 def changePassPhrase(options):
     129def changePassPhrase(options, default_file='~/.ssh/id_rsa', _raw_input=raw_input):
    122130    if not options['filename']:
    123         filename = os.path.expanduser('~/.ssh/id_rsa')
    124         options['filename'] = raw_input(
    125             'Enter file in which the key is (%s): ' % filename)
     131        options['filename'] = _getFilename(default_file, _raw_input)
    126132    try:
    127133        key = keys.Key.fromFile(options['filename']).keyObject
    128134    except keys.EncryptedKeyError as e:
     
    168174
    169175
    170176
    171 def displayPublicKey(options):
     177def displayPublicKey(options, default_file='~/.ssh/id_rsa', _raw_input=raw_input):
    172178    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)
     179        options['filename'] = _getFilename(default_file, _raw_input)
    175180    try:
    176181        key = keys.Key.fromFile(options['filename']).keyObject
    177182    except keys.EncryptedKeyError: