2012-06-07 16:08:47 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2011-06-17 09:47:08 +00:00
|
|
|
import time
|
|
|
|
|
2011-05-21 14:41:14 +00:00
|
|
|
from django.core import signing
|
|
|
|
from django.test import TestCase
|
2012-08-25 11:02:52 +00:00
|
|
|
from django.utils.encoding import force_str
|
2012-08-09 18:08:47 +00:00
|
|
|
from django.utils import six
|
2011-05-21 14:41:14 +00:00
|
|
|
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2011-05-21 14:41:14 +00:00
|
|
|
class TestSigner(TestCase):
|
|
|
|
|
|
|
|
def test_signature(self):
|
|
|
|
"signature() method should generate a signature"
|
|
|
|
signer = signing.Signer('predictable-secret')
|
|
|
|
signer2 = signing.Signer('predictable-secret2')
|
|
|
|
for s in (
|
2012-05-19 15:43:34 +00:00
|
|
|
b'hello',
|
|
|
|
b'3098247:529:087:',
|
2012-06-07 16:08:47 +00:00
|
|
|
'\u2019'.encode('utf-8'),
|
2011-05-21 14:41:14 +00:00
|
|
|
):
|
|
|
|
self.assertEqual(
|
|
|
|
signer.signature(s),
|
|
|
|
signing.base64_hmac(signer.salt + 'signer', s,
|
2012-08-25 11:02:52 +00:00
|
|
|
'predictable-secret').decode()
|
2011-05-21 14:41:14 +00:00
|
|
|
)
|
|
|
|
self.assertNotEqual(signer.signature(s), signer2.signature(s))
|
|
|
|
|
|
|
|
def test_signature_with_salt(self):
|
|
|
|
"signature(value, salt=...) should work"
|
|
|
|
signer = signing.Signer('predictable-secret', salt='extra-salt')
|
|
|
|
self.assertEqual(
|
|
|
|
signer.signature('hello'),
|
2013-10-19 23:33:10 +00:00
|
|
|
signing.base64_hmac('extra-salt' + 'signer',
|
|
|
|
'hello', 'predictable-secret').decode()
|
2013-10-18 09:02:43 +00:00
|
|
|
)
|
2011-05-21 14:41:14 +00:00
|
|
|
self.assertNotEqual(
|
|
|
|
signing.Signer('predictable-secret', salt='one').signature('hello'),
|
|
|
|
signing.Signer('predictable-secret', salt='two').signature('hello'))
|
|
|
|
|
|
|
|
def test_sign_unsign(self):
|
|
|
|
"sign/unsign should be reversible"
|
|
|
|
signer = signing.Signer('predictable-secret')
|
2012-08-25 11:02:52 +00:00
|
|
|
examples = [
|
2011-05-21 14:41:14 +00:00
|
|
|
'q;wjmbk;wkmb',
|
|
|
|
'3098247529087',
|
|
|
|
'3098247:529:087:',
|
|
|
|
'jkw osanteuh ,rcuh nthu aou oauh ,ud du',
|
2012-06-07 16:08:47 +00:00
|
|
|
'\u2019',
|
2012-08-25 11:02:52 +00:00
|
|
|
]
|
2013-09-02 10:06:32 +00:00
|
|
|
if six.PY2:
|
2012-08-25 11:02:52 +00:00
|
|
|
examples.append(b'a byte string')
|
2011-05-21 14:41:14 +00:00
|
|
|
for example in examples:
|
2012-08-25 11:02:52 +00:00
|
|
|
signed = signer.sign(example)
|
|
|
|
self.assertIsInstance(signed, str)
|
|
|
|
self.assertNotEqual(force_str(example), signed)
|
|
|
|
self.assertEqual(example, signer.unsign(signed))
|
2011-05-21 14:41:14 +00:00
|
|
|
|
|
|
|
def unsign_detects_tampering(self):
|
|
|
|
"unsign should raise an exception if the value has been tampered with"
|
|
|
|
signer = signing.Signer('predictable-secret')
|
|
|
|
value = 'Another string'
|
|
|
|
signed_value = signer.sign(value)
|
|
|
|
transforms = (
|
|
|
|
lambda s: s.upper(),
|
|
|
|
lambda s: s + 'a',
|
|
|
|
lambda s: 'a' + s[1:],
|
|
|
|
lambda s: s.replace(':', ''),
|
|
|
|
)
|
|
|
|
self.assertEqual(value, signer.unsign(signed_value))
|
|
|
|
for transform in transforms:
|
|
|
|
self.assertRaises(
|
|
|
|
signing.BadSignature, signer.unsign, transform(signed_value))
|
|
|
|
|
|
|
|
def test_dumps_loads(self):
|
|
|
|
"dumps and loads be reversible for any JSON serializable object"
|
2012-08-09 18:08:47 +00:00
|
|
|
objects = [
|
2011-05-21 14:41:14 +00:00
|
|
|
['a', 'list'],
|
2012-06-07 16:08:47 +00:00
|
|
|
'a unicode string \u2019',
|
2011-05-21 14:41:14 +00:00
|
|
|
{'a': 'dictionary'},
|
2012-08-09 18:08:47 +00:00
|
|
|
]
|
2013-09-02 10:06:32 +00:00
|
|
|
if six.PY2:
|
2012-08-09 18:08:47 +00:00
|
|
|
objects.append(b'a byte string')
|
2011-05-21 14:41:14 +00:00
|
|
|
for o in objects:
|
|
|
|
self.assertNotEqual(o, signing.dumps(o))
|
|
|
|
self.assertEqual(o, signing.loads(signing.dumps(o)))
|
2012-08-09 18:08:47 +00:00
|
|
|
self.assertNotEqual(o, signing.dumps(o, compress=True))
|
|
|
|
self.assertEqual(o, signing.loads(signing.dumps(o, compress=True)))
|
2011-05-21 14:41:14 +00:00
|
|
|
|
|
|
|
def test_decode_detects_tampering(self):
|
|
|
|
"loads should raise exception for tampered objects"
|
|
|
|
transforms = (
|
|
|
|
lambda s: s.upper(),
|
|
|
|
lambda s: s + 'a',
|
|
|
|
lambda s: 'a' + s[1:],
|
|
|
|
lambda s: s.replace(':', ''),
|
|
|
|
)
|
|
|
|
value = {
|
|
|
|
'foo': 'bar',
|
|
|
|
'baz': 1,
|
|
|
|
}
|
|
|
|
encoded = signing.dumps(value)
|
|
|
|
self.assertEqual(value, signing.loads(encoded))
|
|
|
|
for transform in transforms:
|
|
|
|
self.assertRaises(
|
|
|
|
signing.BadSignature, signing.loads, transform(encoded))
|
|
|
|
|
|
|
|
class TestTimestampSigner(TestCase):
|
|
|
|
|
|
|
|
def test_timestamp_signer(self):
|
2012-06-07 16:08:47 +00:00
|
|
|
value = 'hello'
|
2011-06-17 09:47:08 +00:00
|
|
|
_time = time.time
|
|
|
|
time.time = lambda: 123456789
|
|
|
|
try:
|
|
|
|
signer = signing.TimestampSigner('predictable-key')
|
|
|
|
ts = signer.sign(value)
|
|
|
|
self.assertNotEqual(ts,
|
|
|
|
signing.Signer('predictable-key').sign(value))
|
2011-05-21 14:41:14 +00:00
|
|
|
|
2011-06-17 09:47:08 +00:00
|
|
|
self.assertEqual(signer.unsign(ts), value)
|
|
|
|
time.time = lambda: 123456800
|
|
|
|
self.assertEqual(signer.unsign(ts, max_age=12), value)
|
|
|
|
self.assertEqual(signer.unsign(ts, max_age=11), value)
|
|
|
|
self.assertRaises(
|
|
|
|
signing.SignatureExpired, signer.unsign, ts, max_age=10)
|
|
|
|
finally:
|
|
|
|
time.time = _time
|