summaryrefslogtreecommitdiff
path: root/tests/contrib
diff options
context:
space:
mode:
authorMark Adams <mark@markadams.me>2015-03-15 22:47:33 -0500
committerMark Adams <mark@markadams.me>2015-03-15 22:47:33 -0500
commit975fa0f96b356bca74b0cc6933b3cd987fa72417 (patch)
tree6b2475dc7ce450531fa3dc0bc11c3e1823dcc7e0 /tests/contrib
parent35196656379369be467f23919702b174fa38034c (diff)
downloadpyjwt-975fa0f96b356bca74b0cc6933b3cd987fa72417.tar.gz
Added some more tests to improve coverage for jwt.contrib.algorithms
Diffstat (limited to 'tests/contrib')
-rw-r--r--tests/contrib/test_algorithms.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/tests/contrib/test_algorithms.py b/tests/contrib/test_algorithms.py
index c32390b..2b156f8 100644
--- a/tests/contrib/test_algorithms.py
+++ b/tests/contrib/test_algorithms.py
@@ -39,6 +39,29 @@ class TestPycryptoAlgorithms(unittest.TestCase):
with self.assertRaises(TypeError):
algo.prepare_key(None)
+ def test_rsa_sign_should_generate_correct_signature_value(self):
+ algo = RSAAlgorithm(RSAAlgorithm.SHA256)
+
+ jwt_message = ensure_bytes('Hello World!')
+
+ expected_sig = base64.b64decode(ensure_bytes(
+ 'yS6zk9DBkuGTtcBzLUzSpo9gGJxJFOGvUqN01iLhWHrzBQ9ZEz3+Ae38AXp'
+ '10RWwscp42ySC85Z6zoN67yGkLNWnfmCZSEv+xqELGEvBJvciOKsrhiObUl'
+ '2mveSc1oeO/2ujkGDkkkJ2epn0YliacVjZF5+/uDmImUfAAj8lzjnHlzYix'
+ 'sn5jGz1H07jYYbi9diixN8IUhXeTafwFg02IcONhum29V40Wu6O5tAKWlJX'
+ 'fHJnNUzAEUOXS0WahHVb57D30pcgIji9z923q90p5c7E2cU8V+E1qe8NdCA'
+ 'APCDzZZ9zQ/dgcMVaBrGrgimrcLbPjueOKFgSO+SSjIElKA=='))
+
+ with open(key_path('testkey_rsa'), 'r') as keyfile:
+ jwt_key = algo.prepare_key(keyfile.read())
+
+ with open(key_path('testkey_rsa.pub'), 'r') as keyfile:
+ jwt_pub_key = algo.prepare_key(keyfile.read())
+
+ algo.sign(jwt_message, jwt_key)
+ result = algo.verify(jwt_message, jwt_pub_key, expected_sig)
+ self.assertTrue(result)
+
def test_rsa_verify_should_return_false_if_signature_invalid(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
@@ -79,6 +102,15 @@ class TestPycryptoAlgorithms(unittest.TestCase):
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
self.assertTrue(result)
+ def test_rsa_prepare_key_should_be_idempotent(self):
+ algo = RSAAlgorithm(RSAAlgorithm.SHA256)
+
+ with open(key_path('testkey_rsa.pub'), 'r') as keyfile:
+ jwt_pub_key_first = algo.prepare_key(keyfile.read())
+ jwt_pub_key_second = algo.prepare_key(jwt_pub_key_first)
+
+ self.assertEqual(jwt_pub_key_first, jwt_pub_key_second)
+
@unittest.skipIf(not has_ecdsa, 'Not supported without ecdsa library')
class TestEcdsaAlgorithms(unittest.TestCase):
@@ -94,6 +126,27 @@ class TestEcdsaAlgorithms(unittest.TestCase):
with open(key_path('testkey_ec'), 'r') as ec_key:
algo.prepare_key(ensure_unicode(ec_key.read()))
+ def test_ec_sign_should_generate_correct_signature_value(self):
+ algo = ECAlgorithm(ECAlgorithm.SHA256)
+
+ jwt_message = ensure_bytes('Hello World!')
+
+ expected_sig = base64.b64decode(ensure_bytes(
+ 'MIGIAkIB9vYz+inBL8aOTA4auYz/zVuig7TT1bQgKROIQX9YpViHkFa4DT5'
+ '5FuFKn9XzVlk90p6ldEj42DC9YecXHbC2t+cCQgCicY+8f3f/KCNtWK7cif'
+ '6vdsVwm6Lrjs0Ag6ZqCf+olN11hVt1qKBC4lXppqB1gNWEmNQaiz1z2QRyc'
+ 'zJ8hSJmbw=='))
+
+ with open(key_path('testkey_ec'), 'r') as keyfile:
+ jwt_key = algo.prepare_key(keyfile.read())
+
+ with open(key_path('testkey_ec.pub'), 'r') as keyfile:
+ jwt_pub_key = algo.prepare_key(keyfile.read())
+
+ algo.sign(jwt_message, jwt_key)
+ result = algo.verify(jwt_message, jwt_pub_key, expected_sig)
+ self.assertTrue(result)
+
def test_ec_verify_should_return_false_if_signature_invalid(self):
algo = ECAlgorithm(ECAlgorithm.SHA256)
@@ -129,3 +182,12 @@ class TestEcdsaAlgorithms(unittest.TestCase):
result = algo.verify(jwt_message, jwt_pub_key, jwt_sig)
self.assertTrue(result)
+
+ def test_ec_prepare_key_should_be_idempotent(self):
+ algo = ECAlgorithm(ECAlgorithm.SHA256)
+
+ with open(key_path('testkey_ec.pub'), 'r') as keyfile:
+ jwt_pub_key_first = algo.prepare_key(keyfile.read())
+ jwt_pub_key_second = algo.prepare_key(jwt_pub_key_first)
+
+ self.assertEqual(jwt_pub_key_first, jwt_pub_key_second)