Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [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 | |
| 14 | from __future__ import absolute_import, division, print_function |
| 15 | |
Paul Kehrer | 7a2a191 | 2013-10-20 13:45:23 -0500 | [diff] [blame^] | 16 | import pretend |
| 17 | |
Paul Kehrer | d4cb34d | 2013-10-19 23:05:12 -0500 | [diff] [blame] | 18 | import pytest |
| 19 | |
| 20 | import six |
| 21 | |
Paul Kehrer | daac6d0 | 2013-10-20 13:15:28 -0500 | [diff] [blame] | 22 | from cryptography.bindings import _default_api |
| 23 | |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 24 | from cryptography.primitives import hashes |
| 25 | |
| 26 | from .utils import generate_base_hash_test |
| 27 | |
| 28 | |
Paul Kehrer | d4cb34d | 2013-10-19 23:05:12 -0500 | [diff] [blame] | 29 | class TestBaseHash(object): |
| 30 | def test_base_hash_reject_unicode(self, api): |
| 31 | m = hashes.SHA1(api=api) |
| 32 | with pytest.raises(TypeError): |
| 33 | m.update(six.u("\u00FC")) |
| 34 | |
Paul Kehrer | 0b2042b | 2013-10-22 14:28:24 -0500 | [diff] [blame] | 35 | def test_base_hash_hexdigest_string_type(self, api): |
| 36 | m = hashes.SHA1(api=api, data=b"") |
| 37 | assert isinstance(m.hexdigest(), str) |
| 38 | |
Paul Kehrer | d4cb34d | 2013-10-19 23:05:12 -0500 | [diff] [blame] | 39 | |
Paul Kehrer | 7a2a191 | 2013-10-20 13:45:23 -0500 | [diff] [blame^] | 40 | class TestCopyHash(object): |
| 41 | def test_copy_api_object(self): |
| 42 | pretend_api = pretend.stub(copy_hash_context=lambda a: "copiedctx") |
| 43 | pretend_ctx = pretend.stub() |
| 44 | h = hashes.SHA1(api=pretend_api, ctx=pretend_ctx) |
| 45 | assert h._api is pretend_api |
| 46 | assert h.copy()._api is h._api |
| 47 | |
| 48 | |
Paul Kehrer | daac6d0 | 2013-10-20 13:15:28 -0500 | [diff] [blame] | 49 | class TestDefaultAPISHA1(object): |
| 50 | def test_default_api_creation(self): |
| 51 | """ |
| 52 | This test assumes the presence of SHA1 in the default API. |
| 53 | """ |
| 54 | h = hashes.SHA1() |
| 55 | assert h._api == _default_api |
| 56 | |
| 57 | |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 58 | class TestSHA1(object): |
| 59 | test_SHA1 = generate_base_hash_test( |
Paul Kehrer | bb069c2 | 2013-10-18 19:51:01 -0500 | [diff] [blame] | 60 | hashes.SHA1, |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 61 | digest_size=20, |
| 62 | block_size=64, |
Paul Kehrer | ba3b471 | 2013-10-18 20:53:04 -0500 | [diff] [blame] | 63 | only_if=lambda api: api.supports_hash(hashes.SHA1), |
Paul Kehrer | bde6fb5 | 2013-10-18 18:08:49 -0500 | [diff] [blame] | 64 | skip_message="Does not support SHA1", |
| 65 | ) |
Paul Kehrer | 7e5697c | 2013-10-18 21:07:36 -0500 | [diff] [blame] | 66 | |
| 67 | |
| 68 | class TestSHA224(object): |
| 69 | test_SHA224 = generate_base_hash_test( |
| 70 | hashes.SHA224, |
| 71 | digest_size=28, |
| 72 | block_size=64, |
| 73 | only_if=lambda api: api.supports_hash(hashes.SHA224), |
| 74 | skip_message="Does not support SHA224", |
| 75 | ) |
| 76 | |
| 77 | |
| 78 | class TestSHA256(object): |
| 79 | test_SHA256 = generate_base_hash_test( |
| 80 | hashes.SHA256, |
| 81 | digest_size=32, |
| 82 | block_size=64, |
| 83 | only_if=lambda api: api.supports_hash(hashes.SHA256), |
| 84 | skip_message="Does not support SHA256", |
| 85 | ) |
| 86 | |
| 87 | |
| 88 | class TestSHA384(object): |
| 89 | test_SHA384 = generate_base_hash_test( |
| 90 | hashes.SHA384, |
| 91 | digest_size=48, |
| 92 | block_size=128, |
| 93 | only_if=lambda api: api.supports_hash(hashes.SHA384), |
| 94 | skip_message="Does not support SHA384", |
| 95 | ) |
| 96 | |
| 97 | |
| 98 | class TestSHA512(object): |
| 99 | test_SHA512 = generate_base_hash_test( |
| 100 | hashes.SHA512, |
| 101 | digest_size=64, |
| 102 | block_size=128, |
| 103 | only_if=lambda api: api.supports_hash(hashes.SHA512), |
| 104 | skip_message="Does not support SHA512", |
| 105 | ) |
Paul Kehrer | c179407 | 2013-10-18 21:42:57 -0500 | [diff] [blame] | 106 | |
| 107 | |
| 108 | class TestRIPEMD160(object): |
| 109 | test_RIPEMD160 = generate_base_hash_test( |
| 110 | hashes.RIPEMD160, |
| 111 | digest_size=20, |
| 112 | block_size=64, |
| 113 | only_if=lambda api: api.supports_hash(hashes.RIPEMD160), |
| 114 | skip_message="Does not support RIPEMD160", |
| 115 | ) |
Paul Kehrer | 79ff8bf | 2013-10-18 22:07:29 -0500 | [diff] [blame] | 116 | |
| 117 | |
| 118 | class TestWhirlpool(object): |
| 119 | test_Whirlpool = generate_base_hash_test( |
| 120 | hashes.Whirlpool, |
| 121 | digest_size=64, |
| 122 | block_size=64, |
| 123 | only_if=lambda api: api.supports_hash(hashes.Whirlpool), |
| 124 | skip_message="Does not support Whirlpool", |
| 125 | ) |
Paul Kehrer | 36e7d0d | 2013-10-18 18:54:40 -0500 | [diff] [blame] | 126 | |
| 127 | |
| 128 | class TestMD5(object): |
| 129 | test_MD5 = generate_base_hash_test( |
| 130 | hashes.MD5, |
| 131 | digest_size=16, |
| 132 | block_size=64, |
| 133 | only_if=lambda api: api.supports_hash(hashes.MD5), |
| 134 | skip_message="Does not support MD5", |
| 135 | ) |