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