blob: c1caaa05e953ef5ae0e385d1a2aadadddab41f21 [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 Gaynor2b21b122013-10-31 09:39:25 -070027
28
Alex Gaynor38f34552013-10-31 14:50:00 -070029def json_parametrize(keys, fname):
30 path = os.path.join(os.path.dirname(__file__), "vectors", "fernet", fname)
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070031 with open(path) as f:
32 data = json.load(f)
33 return pytest.mark.parametrize(keys, [
34 tuple([entry[k] for k in keys])
35 for entry in data
36 ])
Alex Gaynor2b21b122013-10-31 09:39:25 -070037
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070038
39class TestFernet(object):
40 @json_parametrize(
Alex Gaynor38f34552013-10-31 14:50:00 -070041 ("secret", "now", "iv", "src", "token"), "generate.json",
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070042 )
43 def test_generate(self, secret, now, iv, src, token):
Alex Gaynor898fe0f2013-11-20 16:38:32 -080044 f = Fernet(secret.encode("ascii"))
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070045 actual_token = f._encrypt_from_parts(
46 src.encode("ascii"),
47 calendar.timegm(iso8601.parse_date(now).utctimetuple()),
48 b"".join(map(six.int2byte, iv))
Alex Gaynor5e87dfd2013-10-31 09:46:03 -070049 )
Alex Gaynor7ecd3142013-10-31 16:29:18 -070050 assert actual_token == token.encode("ascii")
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070051
52 @json_parametrize(
Alex Gaynor38f34552013-10-31 14:50:00 -070053 ("secret", "now", "src", "ttl_sec", "token"), "verify.json",
Alex Gaynorfb8adfc2013-10-31 14:16:24 -070054 )
Alex Gaynor1d2901c2013-11-22 10:12:05 -080055 def test_verify(self, secret, now, src, ttl_sec, token, monkeypatch):
Alex Gaynor898fe0f2013-11-20 16:38:32 -080056 f = Fernet(secret.encode("ascii"))
Alex Gaynorc1ea0a02013-10-31 15:03:53 -070057 current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
Alex Gaynor1d2901c2013-11-22 10:12:05 -080058 monkeypatch.setattr(time, "time", lambda: current_time)
59 payload = f.decrypt(token.encode("ascii"), ttl=ttl_sec)
Alex Gaynor7ecd3142013-10-31 16:29:18 -070060 assert payload == src.encode("ascii")
Alex Gaynor38f34552013-10-31 14:50:00 -070061
62 @json_parametrize(("secret", "token", "now", "ttl_sec"), "invalid.json")
Alex Gaynor1d2901c2013-11-22 10:12:05 -080063 def test_invalid(self, secret, token, now, ttl_sec, monkeypatch):
Alex Gaynor898fe0f2013-11-20 16:38:32 -080064 f = Fernet(secret.encode("ascii"))
Alex Gaynorc1ea0a02013-10-31 15:03:53 -070065 current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple())
Alex Gaynor1d2901c2013-11-22 10:12:05 -080066 monkeypatch.setattr(time, "time", lambda: current_time)
Alex Gaynor38f34552013-10-31 14:50:00 -070067 with pytest.raises(InvalidToken):
Alex Gaynor1d2901c2013-11-22 10:12:05 -080068 f.decrypt(token.encode("ascii"), ttl=ttl_sec)
Alex Gaynor38f34552013-10-31 14:50:00 -070069
70 def test_unicode(self):
Alex Gaynor898fe0f2013-11-20 16:38:32 -080071 f = Fernet(base64.b64encode(b"\x00" * 32))
Alex Gaynor38f34552013-10-31 14:50:00 -070072 with pytest.raises(TypeError):
73 f.encrypt(six.u(""))
74 with pytest.raises(TypeError):
75 f.decrypt(six.u(""))
Alex Gaynorce8f9a42013-10-31 15:23:15 -070076
77 @pytest.mark.parametrize("message", [b"", b"Abc!", b"\x00\xFF\x00\x80"])
78 def test_roundtrips(self, message):
Alex Gaynor898fe0f2013-11-20 16:38:32 -080079 f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32))
Alex Gaynor6b9770b2013-10-31 16:07:35 -070080 assert f.decrypt(f.encrypt(message)) == message