22
33
44from functools import partial
5+ from tempfile import TemporaryFile
56
67from nose .plugins .skip import SkipTest
78from nose .tools import (assert_equal , assert_is_instance ,
@@ -107,16 +108,40 @@ def test_rsa_sign_without_key_returns_none():
107108 assert_is_none (cipher .sign (message , hash .md5 ))
108109
109110
110- class TestRsa :
111+ class _TestRsaBase :
111112
112113 def setup (self ):
113114 key_size = 2048
114115 self .cipher = RSA ()
115116 self .cipher .generate (key_size )
116117
118+
119+ class TestRsa (_TestRsaBase ):
120+
117121 def test_keypair (self ):
118122 assert_true (check_pair (self .cipher , self .cipher ))
119123
124+ def test_has_private_and_has_public_with_private_key (self ):
125+ cipher = RSA ()
126+ assert_false (cipher .has_private ())
127+ assert_false (cipher .has_public ())
128+
129+ cipher .import_ (self .cipher ._write_private_key_der ())
130+ assert_true (cipher .has_private ())
131+ assert_true (cipher .has_public ())
132+
133+ def test_has_private_and_has_public_with_public_key (self ):
134+ cipher = RSA ()
135+ assert_false (cipher .has_private ())
136+ assert_false (cipher .has_public ())
137+
138+ cipher .import_ (self .cipher ._write_public_key_der ())
139+ assert_false (cipher .has_private ())
140+ assert_true (cipher .has_public ())
141+
142+
143+ class TestRsaWriteParse (_TestRsaBase ):
144+
120145 def test_write_and_parse_private_key_der (self ):
121146 prv = self .cipher ._write_private_key_der ()
122147 cipher = RSA ()
@@ -161,6 +186,9 @@ def test_write_private_der_in_public_raises(self):
161186 cipher = RSA ()
162187 cipher ._parse_public_key (prv )
163188
189+
190+ class TestRsaImportExport (_TestRsaBase ):
191+
164192 def test_import_public_key (self ):
165193 cipher = RSA ()
166194 cipher .import_ (self .cipher ._write_public_key_der ())
@@ -175,23 +203,8 @@ def test_import_private_key(self):
175203 assert_true (check_pair (cipher , self .cipher )) # Test public half.
176204 assert_true (check_pair (cipher , cipher ))
177205
178- def test_has_private_and_has_public_with_private_key (self ):
179- cipher = RSA ()
180- assert_false (cipher .has_private ())
181- assert_false (cipher .has_public ())
182-
183- cipher .import_ (self .cipher ._write_private_key_der ())
184- assert_true (cipher .has_private ())
185- assert_true (cipher .has_public ())
186-
187- def test_has_private_and_has_public_with_public_key (self ):
188- cipher = RSA ()
189- assert_false (cipher .has_private ())
190- assert_false (cipher .has_public ())
191206
192- cipher .import_ (self .cipher ._write_public_key_der ())
193- assert_false (cipher .has_private ())
194- assert_true (cipher .has_public ())
207+ class TestRsaSignature (_TestRsaBase ):
195208
196209 def test_sign_verify (self ):
197210 message = _rnd (4096 )
0 commit comments