Alex Gaynor | 8912d3a | 2013-11-02 14:04:19 -0700 | [diff] [blame] | 1 | # 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 Gaynor | 2b21b12 | 2013-10-31 09:39:25 -0700 | [diff] [blame] | 14 | import base64 |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 15 | import calendar |
| 16 | import json |
| 17 | import os |
Alex Gaynor | 1d2901c | 2013-11-22 10:12:05 -0800 | [diff] [blame] | 18 | import time |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 19 | |
| 20 | import iso8601 |
| 21 | |
| 22 | import pytest |
Alex Gaynor | 2b21b12 | 2013-10-31 09:39:25 -0700 | [diff] [blame] | 23 | |
Alex Gaynor | de36e90 | 2013-10-31 10:10:44 -0700 | [diff] [blame] | 24 | import six |
| 25 | |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 26 | from cryptography.fernet import Fernet, InvalidToken |
Alex Gaynor | fae2071 | 2013-12-16 15:29:30 -0800 | [diff] [blame] | 27 | from cryptography.hazmat.backends import default_backend |
Alex Gaynor | 2b21b12 | 2013-10-31 09:39:25 -0700 | [diff] [blame] | 28 | |
| 29 | |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 30 | def json_parametrize(keys, fname): |
| 31 | path = os.path.join(os.path.dirname(__file__), "vectors", "fernet", fname) |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 32 | 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 Gaynor | 2b21b12 | 2013-10-31 09:39:25 -0700 | [diff] [blame] | 38 | |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 39 | |
| 40 | class TestFernet(object): |
| 41 | @json_parametrize( |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 42 | ("secret", "now", "iv", "src", "token"), "generate.json", |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 43 | ) |
Alex Gaynor | fae2071 | 2013-12-16 15:29:30 -0800 | [diff] [blame] | 44 | def test_generate(self, secret, now, iv, src, token, backend): |
| 45 | f = Fernet(secret.encode("ascii"), backend=backend) |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 46 | 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 Gaynor | 5e87dfd | 2013-10-31 09:46:03 -0700 | [diff] [blame] | 50 | ) |
Alex Gaynor | 7ecd314 | 2013-10-31 16:29:18 -0700 | [diff] [blame] | 51 | assert actual_token == token.encode("ascii") |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 52 | |
| 53 | @json_parametrize( |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 54 | ("secret", "now", "src", "ttl_sec", "token"), "verify.json", |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 55 | ) |
Alex Gaynor | fae2071 | 2013-12-16 15:29:30 -0800 | [diff] [blame] | 56 | def test_verify(self, secret, now, src, ttl_sec, token, backend, |
| 57 | monkeypatch): |
| 58 | f = Fernet(secret.encode("ascii"), backend=backend) |
Alex Gaynor | c1ea0a0 | 2013-10-31 15:03:53 -0700 | [diff] [blame] | 59 | current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple()) |
Alex Gaynor | 1d2901c | 2013-11-22 10:12:05 -0800 | [diff] [blame] | 60 | monkeypatch.setattr(time, "time", lambda: current_time) |
| 61 | payload = f.decrypt(token.encode("ascii"), ttl=ttl_sec) |
Alex Gaynor | 7ecd314 | 2013-10-31 16:29:18 -0700 | [diff] [blame] | 62 | assert payload == src.encode("ascii") |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 63 | |
| 64 | @json_parametrize(("secret", "token", "now", "ttl_sec"), "invalid.json") |
Alex Gaynor | fae2071 | 2013-12-16 15:29:30 -0800 | [diff] [blame] | 65 | def test_invalid(self, secret, token, now, ttl_sec, backend, monkeypatch): |
| 66 | f = Fernet(secret.encode("ascii"), backend=backend) |
Alex Gaynor | c1ea0a0 | 2013-10-31 15:03:53 -0700 | [diff] [blame] | 67 | current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple()) |
Alex Gaynor | 1d2901c | 2013-11-22 10:12:05 -0800 | [diff] [blame] | 68 | monkeypatch.setattr(time, "time", lambda: current_time) |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 69 | with pytest.raises(InvalidToken): |
Alex Gaynor | 1d2901c | 2013-11-22 10:12:05 -0800 | [diff] [blame] | 70 | f.decrypt(token.encode("ascii"), ttl=ttl_sec) |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 71 | |
Alex Gaynor | a8f0b63 | 2013-12-16 15:44:06 -0800 | [diff] [blame] | 72 | 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 Gaynor | e78960f | 2013-12-20 11:02:33 -0800 | [diff] [blame^] | 77 | def test_timestamp_too_short(self, backend): |
| 78 | f = Fernet(Fernet.generate_key(), backend=backend) |
| 79 | with pytest.raises(InvalidToken): |
| 80 | f.decrypt(base64.urlsafe_b64encode(b"\x80abc")) |
| 81 | |
Alex Gaynor | fae2071 | 2013-12-16 15:29:30 -0800 | [diff] [blame] | 82 | def test_unicode(self, backend): |
| 83 | f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend) |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 84 | with pytest.raises(TypeError): |
| 85 | f.encrypt(six.u("")) |
| 86 | with pytest.raises(TypeError): |
| 87 | f.decrypt(six.u("")) |
Alex Gaynor | ce8f9a4 | 2013-10-31 15:23:15 -0700 | [diff] [blame] | 88 | |
| 89 | @pytest.mark.parametrize("message", [b"", b"Abc!", b"\x00\xFF\x00\x80"]) |
Alex Gaynor | fae2071 | 2013-12-16 15:29:30 -0800 | [diff] [blame] | 90 | def test_roundtrips(self, message, backend): |
| 91 | f = Fernet(Fernet.generate_key(), backend=backend) |
Alex Gaynor | 6b9770b | 2013-10-31 16:07:35 -0700 | [diff] [blame] | 92 | assert f.decrypt(f.encrypt(message)) == message |
Alex Gaynor | fae2071 | 2013-12-16 15:29:30 -0800 | [diff] [blame] | 93 | |
| 94 | def test_default_backend(self): |
| 95 | f = Fernet(Fernet.generate_key()) |
| 96 | assert f._backend is default_backend() |
Alex Gaynor | a8f0b63 | 2013-12-16 15:44:06 -0800 | [diff] [blame] | 97 | |
| 98 | def test_bad_key(self, backend): |
| 99 | with pytest.raises(ValueError): |
| 100 | Fernet(base64.urlsafe_b64encode(b"abc"), backend=backend) |