r46957 - docs and tests

hawkowl-TA+aISz0psMTMxyoc4vAAJOcrHinNvQL0E9HWUfgJXw@public.gmane.org Wed, 9 Mar 2016 00:42:47 -0700 (MST)
Newsgroups gmane.comp.python.twisted.commits
Message-ID <[email protected]>
Author: hawkowl
Date: Wed Mar  9 00:42:43 2016
New Revision: 46957

Modified:
   branches/manhole-hardcodedkey-8229/twisted/conch/ssh/keys.py
   branches/manhole-hardcodedkey-8229/twisted/conch/test/test_keys.py

Log:
docs and tests

Modified: branches/manhole-hardcodedkey-8229/twisted/conch/ssh/keys.py
==============================================================================
--- branches/manhole-hardcodedkey-8229/twisted/conch/ssh/keys.py	(original)
+++ branches/manhole-hardcodedkey-8229/twisted/conch/ssh/keys.py	Wed Mar  9 00:42:43 2016
@@ -1247,6 +1247,9 @@
     @param filename: The filename of the key file.
     @type filename: L{str} or L{bytes}
 
+    @param keySize: The size of the key, if it needs to be generated.
+    @type keySize: L{int}
+
     @returns: A persistent key.
     @rtype: L{Key}
     """

Modified: branches/manhole-hardcodedkey-8229/twisted/conch/test/test_keys.py
==============================================================================
--- branches/manhole-hardcodedkey-8229/twisted/conch/test/test_keys.py	(original)
+++ branches/manhole-hardcodedkey-8229/twisted/conch/test/test_keys.py	Wed Mar  9 00:42:43 2016
@@ -29,12 +29,15 @@
 if cryptography and pyasn1:
     from twisted.conch.ssh import keys, common, sexpy
 
-import os, base64
+import base64
+import os
+
 from twisted.conch.test import keydata
 from twisted.python import randbytes
 from twisted.trial import unittest
 from twisted.python.compat import long, _PY3
 from twisted.python.versions import Version
+from twisted.python.filepath import FilePath
 
 
 
@@ -1169,3 +1172,56 @@
 
     if cryptography is None:
         skip = skipCryptography
+
+
+    def test_providedArguments(self):
+        """
+        L{keys._getPersistentRSAKey} will put the key in
+        C{directory}/C{filename}, with the key length of C{keySize}.
+        """
+        tempDir = self.mktemp()
+
+        key = keys._getPersistentRSAKey(directory=tempDir,
+                                        filename="mykey.pem", keySize=512)
+        self.assertEqual(key.size(), 512)
+        self.assertTrue(FilePath(tempDir).child("mykey.pem").exists())
+
+
+    def test_defaultArguments(self):
+        """
+        L{keys._getPersistentRSAKey}, when no arguments are given, will put the
+        key in the user's data directory.
+        """
+        tempDir = self.mktemp()
+
+        class FakeAppdirs(object):
+            @classmethod
+            def user_data_dir(cls, maker, product):
+                return tempDir
+
+        key = keys._getPersistentRSAKey(_appdirs=FakeAppdirs)
+
+        self.assertEqual(key.size(), 4096)
+        self.assertTrue(FilePath(tempDir).child("server.pem"))
+
+
+    def test_noRegeneration(self):
+        """
+        L{keys._getPersistentRSAKey} will not regenerate the key if the key
+        already exists.
+        """
+        tempDir = self.mktemp()
+
+        key = keys._getPersistentRSAKey(directory=tempDir,
+                                        filename="mykey.pem", keySize=512)
+        self.assertEqual(key.size(), 512)
+        self.assertTrue(FilePath(tempDir).child("mykey.pem").exists())
+        keyContent = FilePath(tempDir).child("mykey.pem").getContent()
+
+        # Set the key size to 1024 bits. Since it exists already, it will find
+        # the 512 bit key, and not generate a 1024 bit key.
+        key = keys._getPersistentRSAKey(directory=tempDir,
+                                        filename="mykey.pem", keySize=1024)
+        self.assertEqual(key.size(), 512)
+        self.assertEqual(FilePath(tempDir).child("mykey.pem").getContent(),
+                         keyContent)