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 |
| 18 | |
| 19 | import iso8601 |
| 20 | |
| 21 | import pytest |
Alex Gaynor | 2b21b12 | 2013-10-31 09:39:25 -0700 | [diff] [blame] | 22 | |
Alex Gaynor | de36e90 | 2013-10-31 10:10:44 -0700 | [diff] [blame] | 23 | import six |
| 24 | |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 25 | from cryptography.fernet import Fernet, InvalidToken |
Alex Gaynor | 2b21b12 | 2013-10-31 09:39:25 -0700 | [diff] [blame] | 26 | |
| 27 | |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 28 | def json_parametrize(keys, fname): |
| 29 | path = os.path.join(os.path.dirname(__file__), "vectors", "fernet", fname) |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 30 | with open(path) as f: |
| 31 | data = json.load(f) |
| 32 | return pytest.mark.parametrize(keys, [ |
| 33 | tuple([entry[k] for k in keys]) |
| 34 | for entry in data |
| 35 | ]) |
Alex Gaynor | 2b21b12 | 2013-10-31 09:39:25 -0700 | [diff] [blame] | 36 | |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 37 | |
| 38 | class TestFernet(object): |
| 39 | @json_parametrize( |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 40 | ("secret", "now", "iv", "src", "token"), "generate.json", |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 41 | ) |
| 42 | def test_generate(self, secret, now, iv, src, token): |
Alex Gaynor | 898fe0f | 2013-11-20 16:38:32 -0800 | [diff] [blame] | 43 | f = Fernet(secret.encode("ascii")) |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 44 | actual_token = f._encrypt_from_parts( |
| 45 | src.encode("ascii"), |
| 46 | calendar.timegm(iso8601.parse_date(now).utctimetuple()), |
| 47 | b"".join(map(six.int2byte, iv)) |
Alex Gaynor | 5e87dfd | 2013-10-31 09:46:03 -0700 | [diff] [blame] | 48 | ) |
Alex Gaynor | 7ecd314 | 2013-10-31 16:29:18 -0700 | [diff] [blame] | 49 | assert actual_token == token.encode("ascii") |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 50 | |
| 51 | @json_parametrize( |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 52 | ("secret", "now", "src", "ttl_sec", "token"), "verify.json", |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 53 | ) |
| 54 | def test_verify(self, secret, now, src, ttl_sec, token): |
Alex Gaynor | 898fe0f | 2013-11-20 16:38:32 -0800 | [diff] [blame] | 55 | f = Fernet(secret.encode("ascii")) |
Alex Gaynor | c1ea0a0 | 2013-10-31 15:03:53 -0700 | [diff] [blame] | 56 | current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple()) |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 57 | payload = f.decrypt( |
Alex Gaynor | c1ea0a0 | 2013-10-31 15:03:53 -0700 | [diff] [blame] | 58 | token.encode("ascii"), ttl=ttl_sec, current_time=current_time |
Alex Gaynor | fb8adfc | 2013-10-31 14:16:24 -0700 | [diff] [blame] | 59 | ) |
Alex Gaynor | 7ecd314 | 2013-10-31 16:29:18 -0700 | [diff] [blame] | 60 | assert payload == src.encode("ascii") |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 61 | |
| 62 | @json_parametrize(("secret", "token", "now", "ttl_sec"), "invalid.json") |
| 63 | def test_invalid(self, secret, token, now, ttl_sec): |
Alex Gaynor | 898fe0f | 2013-11-20 16:38:32 -0800 | [diff] [blame] | 64 | f = Fernet(secret.encode("ascii")) |
Alex Gaynor | c1ea0a0 | 2013-10-31 15:03:53 -0700 | [diff] [blame] | 65 | current_time = calendar.timegm(iso8601.parse_date(now).utctimetuple()) |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 66 | with pytest.raises(InvalidToken): |
| 67 | f.decrypt( |
Alex Gaynor | c1ea0a0 | 2013-10-31 15:03:53 -0700 | [diff] [blame] | 68 | token.encode("ascii"), ttl=ttl_sec, current_time=current_time |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 69 | ) |
| 70 | |
| 71 | def test_unicode(self): |
Alex Gaynor | 898fe0f | 2013-11-20 16:38:32 -0800 | [diff] [blame] | 72 | f = Fernet(base64.b64encode(b"\x00" * 32)) |
Alex Gaynor | 38f3455 | 2013-10-31 14:50:00 -0700 | [diff] [blame] | 73 | with pytest.raises(TypeError): |
| 74 | f.encrypt(six.u("")) |
| 75 | with pytest.raises(TypeError): |
| 76 | f.decrypt(six.u("")) |
Alex Gaynor | ce8f9a4 | 2013-10-31 15:23:15 -0700 | [diff] [blame] | 77 | |
| 78 | @pytest.mark.parametrize("message", [b"", b"Abc!", b"\x00\xFF\x00\x80"]) |
| 79 | def test_roundtrips(self, message): |
Alex Gaynor | 898fe0f | 2013-11-20 16:38:32 -0800 | [diff] [blame] | 80 | f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32)) |
Alex Gaynor | 6b9770b | 2013-10-31 16:07:35 -0700 | [diff] [blame] | 81 | assert f.decrypt(f.encrypt(message)) == message |