blob: 7766118008bf63dea596df7d4b0cfc3746d9135f [file] [log] [blame]
Alex Gaynor8912d3a2013-11-02 14:04:19 -07001# Licensed under the Apache License, Version 2.0 (the "License");
2# you may not use this file except in compliance with the License.
3# You may obtain a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS,
9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10# implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
Alex Gaynor2b21b122013-10-31 09:39:25 -070014import base64
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070015import calendar
16import json
17import os
Alex Gaynor1d2901c2013-11-22 10:12:05 -080018import time
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070019
20import iso8601
21
22import pytest
Alex Gaynor2b21b122013-10-31 09:39:25 -070023
Alex Gaynorde36e902013-10-31 10:10:44 -070024import six
25
Alex Gaynor38f34552013-10-31 14:50:00 -070026from cryptography.fernet import Fernet, InvalidToken
Alex Gaynorfae20712013-12-16 15:29:30 -080027from cryptography.hazmat.backends import default_backend
Alex Gaynor2b21b122013-10-31 09:39:25 -070028
29
Alex Gaynor38f34552013-10-31 14:50:00 -070030def json_parametrize(keys, fname):
31 path = os.path.join(os.path.dirname(__file__), "vectors", "fernet", fname)
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070032 with open(path) as f:
33 data = json.load(f)
34 return pytest.mark.parametrize(keys, [
35 tuple([entry[k] for k in keys])
36 for entry in data
37 ])
Alex Gaynor2b21b122013-10-31 09:39:25 -070038
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070039
40class TestFernet(object):
41 @json_parametrize(
Alex Gaynor38f34552013-10-31 14:50:00 -070042 ("secret", "now", "iv", "src", "token"), "generate.json",
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070043 )
Alex Gaynorfae20712013-12-16 15:29:30 -080044 def test_generate(self, secret, now, iv, src, token, backend):
45 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070046 actual_token = f._encrypt_from_parts(
47 src.encode("ascii"),
48 calendar.timegm(iso8601.parse_date(now).utctimetuple()),
49 b"".join(map(six.int2byte, iv))
Alex Gaynor5e87dfd2013-10-31 09:46:03 -070050 )
Alex Gaynor7ecd3142013-10-31 16:29:18 -070051 assert actual_token == token.encode("ascii")
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070052
53 @json_parametrize(
Alex Gaynor38f34552013-10-31 14:50:00 -070054 ("secret", "now", "src", "ttl_sec", "token"), "verify.json",
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070055 )
Alex Gaynorfae20712013-12-16 15:29:30 -080056 def test_verify(self, secret, now, src, ttl_sec, token, backend,
57 monkeypatch):
58 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorc1ea0a02013-10-31 15:03:53 -070059 current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
Alex Gaynor1d2901c2013-11-22 10:12:05 -080060 monkeypatch.setattr(time, "time", lambda: current_time)
61 payload = f.decrypt(token.encode("ascii"), ttl=ttl_sec)
Alex Gaynor7ecd3142013-10-31 16:29:18 -070062 assert payload == src.encode("ascii")
Alex Gaynor38f34552013-10-31 14:50:00 -070063
64 @json_parametrize(("secret", "token", "now", "ttl_sec"), "invalid.json")
Alex Gaynorfae20712013-12-16 15:29:30 -080065 def test_invalid(self, secret, token, now, ttl_sec, backend, monkeypatch):
66 f = Fernet(secret.encode("ascii"), backend=backend)
Alex Gaynorc1ea0a02013-10-31 15:03:53 -070067 current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
Alex Gaynor1d2901c2013-11-22 10:12:05 -080068 monkeypatch.setattr(time, "time", lambda: current_time)
Alex Gaynor38f34552013-10-31 14:50:00 -070069 with pytest.raises(InvalidToken):
Alex Gaynor1d2901c2013-11-22 10:12:05 -080070 f.decrypt(token.encode("ascii"), ttl=ttl_sec)
Alex Gaynor38f34552013-10-31 14:50:00 -070071
Alex Gaynora8f0b632013-12-16 15:44:06 -080072 def test_invalid_start_byte(self, backend):
73 f = Fernet(Fernet.generate_key(), backend=backend)
74 with pytest.raises(InvalidToken):
75 f.decrypt(base64.urlsafe_b64encode(b"\x81"))
76
Alex Gaynorfae20712013-12-16 15:29:30 -080077 def test_unicode(self, backend):
78 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend)
Alex Gaynor38f34552013-10-31 14:50:00 -070079 with pytest.raises(TypeError):
80 f.encrypt(six.u(""))
81 with pytest.raises(TypeError):
82 f.decrypt(six.u(""))
Alex Gaynorce8f9a42013-10-31 15:23:15 -070083
84 @pytest.mark.parametrize("message", [b"", b"Abc!", b"\x00\xFF\x00\x80"])
Alex Gaynorfae20712013-12-16 15:29:30 -080085 def test_roundtrips(self, message, backend):
86 f = Fernet(Fernet.generate_key(), backend=backend)
Alex Gaynor6b9770b2013-10-31 16:07:35 -070087 assert f.decrypt(f.encrypt(message)) == message
Alex Gaynorfae20712013-12-16 15:29:30 -080088
89 def test_default_backend(self):
90 f = Fernet(Fernet.generate_key())
91 assert f._backend is default_backend()
Alex Gaynora8f0b632013-12-16 15:44:06 -080092
93 def test_bad_key(self, backend):
94 with pytest.raises(ValueError):
95 Fernet(base64.urlsafe_b64encode(b"abc"), backend=backend)