I have a testing environment like this
class KeyGenTests(TestCase):
"""
Tests for various functions used to implement the I{ckeygen} script.
"""
def setUp(self):
"""
Patch C{sys.stdout} with a L{StringIO} instance to tests can make
assertions about what's printed.
"""
self.stdout = StringIO()
self.patch(sys, 'stdout', self.stdout)
def test_printFingerprint(self):
"""
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.
"""
filename = self.mktemp()
FilePath(filename).setContent(publicRSA_openssh)
printFingerprint({'filename': filename})
self.assertEqual(
self.stdout.getvalue(),
'768 3d:13:5f:cb:c9:79:8a:93:06:27:65:bc:3d:0b:8f:af temp\n')
and the corresponding function is
def printFingerprint(options):
if not options['filename']:
filename = os.path.expanduser('~/.ssh/id_rsa')
options['filename'] = raw_input('Enter file in which the key is (%s): ' % filename)
if os.path.exists(options['filename']+'.pub'):
options['filename'] += '.pub'
try:
key = keys.Key.fromFile(options['filename'])
print('%s %s %s' % (
key.size(),
key.fingerprint(),
os.path.basename(options['filename'])))
except:
sys.exit('bad key')
now I am adding support for key fingerprint generation with different formats, something like
def printFingerprint(options, fingerPrintformat='md5-hex'):
"""
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.
"""
if not options['filename']:
filename = os.path.expanduser('~/.ssh/id_rsa')
options['filename'] = raw_input('Enter file in which the key is (%s): ' % filename)
if os.path.exists(options['filename']+'.pub'):
options['filename'] += '.pub'
try:
key = keys.Key.fromFile(options['filename'])
print('%s %s %s' % (
key.size(),
key.fingerprint(fingerPrintformat),
os.path.basename(options['filename'])))
except:
sys.exit('bad key')
The fingerprint function works properly.
My question is how do I modify the test method to cover all the algorithms for fingerprint generation in a single test method. As the test uses temp files for reading and writing the key fingerprints. Writing a different test method for each hash function is repetitive coding and doesn't match the coding standards of the project.
Aucun commentaire:
Enregistrer un commentaire