Skip to content

Commit 7d83918

Browse files
author
Gasper Zejn
committed
Add more tests and enable Python 3.5 and 3.6 in .travis.yml.
1 parent 86d9c50 commit 7d83918

4 files changed

Lines changed: 75 additions & 9 deletions

File tree

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ python:
55
- "2.7"
66
- "3.3"
77
- "3.4"
8+
- "3.5"
9+
- "3.6"
810
- "pypy-5.3.1"
911
install:
1012
- pip install -U tox codecov tox-travis

tests/algorithms/test_EC.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
from jose.constants import ALGORITHMS
3-
from jose.exceptions import JOSEError
3+
from jose.exceptions import JOSEError, JWKError
44

55
from jose.backends.ecdsa_backend import ECDSAECKey
66
from jose.backends.cryptography_backend import CryptographyECKey
@@ -40,3 +40,16 @@ def test_object(self):
4040

4141
with pytest.raises(JOSEError):
4242
CryptographyECKey(key, ALGORITHMS.ES256)
43+
44+
def test_invalid_algorithm(self):
45+
with pytest.raises(JWKError):
46+
ECDSAECKey({'kty': 'bla'}, ALGORITHMS.ES256)
47+
48+
def test_verify(self):
49+
key = ECDSAECKey(private_key, ALGORITHMS.ES256)
50+
msg = b'test'
51+
signature = key.sign(msg)
52+
public_key = key.public_key()
53+
54+
assert public_key.verify(msg, signature) == True
55+
assert public_key.verify(msg, b'not a signature') == False

tests/algorithms/test_RSA.py

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from jose.backends.pycrypto_backend import RSAKey
55
from jose.backends.cryptography_backend import CryptographyRSAKey
66
from jose.constants import ALGORITHMS
7-
from jose.exceptions import JOSEError
7+
from jose.exceptions import JOSEError, JWKError
88

99
from Crypto.PublicKey import RSA
1010

@@ -15,7 +15,7 @@
1515
if sys.version_info > (3,):
1616
long = int
1717

18-
private_key = """-----BEGIN RSA PRIVATE KEY-----
18+
private_key = b"""-----BEGIN RSA PRIVATE KEY-----
1919
MIIJKwIBAAKCAgEAtSKfSeI0fukRIX38AHlKB1YPpX8PUYN2JdvfM+XjNmLfU1M7
2020
4N0VmdzIX95sneQGO9kC2xMIE+AIlt52Yf/KgBZggAlS9Y0Vx8DsSL2HvOjguAdX
2121
ir3vYLvAyyHin/mUisJOqccFKChHKjnk0uXy/38+1r17/cYTp76brKpU1I4kM20M
@@ -110,6 +110,13 @@ def test_RSA_key_instance(self):
110110
pem = pubkey.to_pem()
111111
assert pem.startswith(b'-----BEGIN PUBLIC KEY-----')
112112

113+
def test_invalid_algorithm(self):
114+
with pytest.raises(JWKError):
115+
CryptographyRSAKey(private_key, ALGORITHMS.ES256)
116+
117+
with pytest.raises(JWKError):
118+
CryptographyRSAKey({'kty': 'bla'}, ALGORITHMS.RS256)
119+
113120
def test_RSA_jwk(self):
114121
d = {
115122
"kty": "RSA",
@@ -133,14 +140,29 @@ def test_bad_cert(self):
133140
with pytest.raises(JOSEError):
134141
CryptographyRSAKey(key, ALGORITHMS.RS256)
135142

143+
def test_get_public_key(self):
144+
key = CryptographyRSAKey(private_key, ALGORITHMS.RS256)
145+
public_key = key.public_key()
146+
public_key2 = public_key.public_key()
147+
assert public_key == public_key2
148+
149+
key = RSAKey(private_key, ALGORITHMS.RS256)
150+
public_key = key.public_key()
151+
public_key2 = public_key.public_key()
152+
assert public_key == public_key2
153+
154+
def test_to_pem(self):
155+
key = CryptographyRSAKey(private_key, ALGORITHMS.RS256)
156+
assert key.to_pem().strip() == private_key.strip()
157+
158+
key = RSAKey(private_key, ALGORITHMS.RS256)
159+
assert key.to_pem().strip() == private_key.strip()
160+
136161
def test_signing_parity(self):
137162
key1 = RSAKey(private_key, ALGORITHMS.RS256)
138-
public_key = key1.public_key().to_pem()
139-
vkey1 = RSAKey(public_key, ALGORITHMS.RS256)
163+
vkey1 = key1.public_key()
140164
key2 = CryptographyRSAKey(private_key, ALGORITHMS.RS256)
141-
vkey2 = CryptographyRSAKey(public_key, ALGORITHMS.RS256)
142-
143-
assert key2.public_key().to_pem() == public_key
165+
vkey2 = key2.public_key()
144166

145167
msg = b'test'
146168
sig1 = key1.sign(msg)
@@ -153,3 +175,13 @@ def test_signing_parity(self):
153175

154176
# invalid signature
155177
assert not vkey2.verify(msg, b'n' * 64)
178+
179+
def test_pycrypto_invalid_signature(self):
180+
181+
key = RSAKey(private_key, ALGORITHMS.RS256)
182+
msg = b'test'
183+
signature = key.sign(msg)
184+
public_key = key.public_key()
185+
186+
assert public_key.verify(msg, signature) == True
187+
assert public_key.verify(msg, 1) == False

tests/algorithms/test_cryptography_EC.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
from jose.constants import ALGORITHMS
3-
from jose.exceptions import JOSEError
3+
from jose.exceptions import JOSEError, JWKError
44
from jose.backends.cryptography_backend import CryptographyECKey
55
from jose.backends.ecdsa_backend import ECDSAECKey
66

@@ -24,6 +24,25 @@ def test_EC_key(self):
2424
public_pem = k.public_key().to_pem()
2525
public_key = CryptographyECKey(public_pem, ALGORITHMS.ES256)
2626

27+
def test_invalid_algorithm(self):
28+
with pytest.raises(JWKError):
29+
CryptographyECKey(private_key, 'nonexistent')
30+
31+
with pytest.raises(JWKError):
32+
CryptographyECKey({'kty': 'bla'}, ALGORITHMS.ES256)
33+
34+
def test_key_too_short(self):
35+
priv_key = ecdsa.SigningKey.generate(curve=ecdsa.NIST256p).to_pem()
36+
key = CryptographyECKey(priv_key, ALGORITHMS.ES512)
37+
with pytest.raises(TypeError):
38+
key.sign('foo')
39+
40+
def test_get_public_key(self):
41+
key = CryptographyECKey(private_key, ALGORITHMS.ES256)
42+
pubkey = key.public_key()
43+
pubkey2 = pubkey.public_key()
44+
assert pubkey == pubkey2
45+
2746
def test_string_secret(self):
2847
key = 'secret'
2948
with pytest.raises(JOSEError):

0 commit comments

Comments
 (0)