Skip to content

Commit

Permalink
enable mypy over tests (#5721)
Browse files Browse the repository at this point in the history
* clean mypy with tests dir

* remove most no_type_check annotations

* le sigh

* remove unneeded comments
  • Loading branch information
reaperhulk authored Feb 1, 2021
1 parent 4883791 commit 343ac13
Show file tree
Hide file tree
Showing 35 changed files with 309 additions and 186 deletions.
20 changes: 8 additions & 12 deletions tests/doubles.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,36 @@
# for complete details.


from cryptography import utils
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives.ciphers import CipherAlgorithm
from cryptography.hazmat.primitives.ciphers.modes import Mode


@utils.register_interface(CipherAlgorithm)
class DummyCipherAlgorithm(object):
class DummyCipherAlgorithm(CipherAlgorithm):
name = "dummy-cipher"
block_size = 128
key_size = None


@utils.register_interface(Mode)
class DummyMode(object):
class DummyMode(Mode):
name = "dummy-mode"

def validate_for_algorithm(self, algorithm: CipherAlgorithm) -> None:
pass


@utils.register_interface(hashes.HashAlgorithm)
class DummyHashAlgorithm(object):
class DummyHashAlgorithm(hashes.HashAlgorithm):
name = "dummy-hash"
block_size = None
digest_size = None
digest_size = 32


@utils.register_interface(serialization.KeySerializationEncryption)
class DummyKeySerializationEncryption(object):
class DummyKeySerializationEncryption(
serialization.KeySerializationEncryption
):
pass


@utils.register_interface(padding.AsymmetricPadding)
class DummyAsymmetricPadding(object):
class DummyAsymmetricPadding(padding.AsymmetricPadding):
name = "dummy-padding"
2 changes: 2 additions & 0 deletions tests/hazmat/backends/test_openssl_memleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ def assert_no_memory_leaks(s, argv=[]):
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
assert proc.stdout is not None
assert proc.stderr is not None
try:
proc.wait()
if proc.returncode == 255:
Expand Down
14 changes: 7 additions & 7 deletions tests/hazmat/primitives/test_aead.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
)


class FakeData(object):
class FakeData(bytes):
def __len__(self):
return 2 ** 32 + 1

Expand Down Expand Up @@ -71,7 +71,7 @@ def test_generate_key(self):

def test_bad_key(self, backend):
with pytest.raises(TypeError):
ChaCha20Poly1305(object())
ChaCha20Poly1305(object()) # type:ignore[arg-type]

with pytest.raises(ValueError):
ChaCha20Poly1305(b"0" * 31)
Expand Down Expand Up @@ -215,7 +215,7 @@ def test_invalid_tag_length(self, backend):
AESCCM(key, tag_length=2)

with pytest.raises(TypeError):
AESCCM(key, tag_length="notanint")
AESCCM(key, tag_length="notanint") # type:ignore[arg-type]

def test_invalid_nonce_length(self, backend):
key = AESCCM.generate_key(128)
Expand Down Expand Up @@ -298,14 +298,14 @@ def test_params_not_bytes(self, nonce, data, associated_data, backend):

def test_bad_key(self, backend):
with pytest.raises(TypeError):
AESCCM(object())
AESCCM(object()) # type:ignore[arg-type]

with pytest.raises(ValueError):
AESCCM(b"0" * 31)

def test_bad_generate_key(self, backend):
with pytest.raises(TypeError):
AESCCM.generate_key(object())
AESCCM.generate_key(object()) # type:ignore[arg-type]

with pytest.raises(ValueError):
AESCCM.generate_key(129)
Expand Down Expand Up @@ -430,14 +430,14 @@ def test_invalid_nonce_length(self, length, backend):

def test_bad_key(self, backend):
with pytest.raises(TypeError):
AESGCM(object())
AESGCM(object()) # type:ignore[arg-type]

with pytest.raises(ValueError):
AESGCM(b"0" * 31)

def test_bad_generate_key(self, backend):
with pytest.raises(TypeError):
AESGCM.generate_key(object())
AESGCM.generate_key(object()) # type:ignore[arg-type]

with pytest.raises(ValueError):
AESGCM.generate_key(129)
Expand Down
14 changes: 7 additions & 7 deletions tests/hazmat/primitives/test_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,28 +194,28 @@ def test_gcm(self):
class TestModesRequireBytes(object):
def test_cbc(self):
with pytest.raises(TypeError):
modes.CBC([1] * 16)
modes.CBC([1] * 16) # type:ignore[arg-type]

def test_cfb(self):
with pytest.raises(TypeError):
modes.CFB([1] * 16)
modes.CFB([1] * 16) # type:ignore[arg-type]

def test_cfb8(self):
with pytest.raises(TypeError):
modes.CFB8([1] * 16)
modes.CFB8([1] * 16) # type:ignore[arg-type]

def test_ofb(self):
with pytest.raises(TypeError):
modes.OFB([1] * 16)
modes.OFB([1] * 16) # type:ignore[arg-type]

def test_ctr(self):
with pytest.raises(TypeError):
modes.CTR([1] * 16)
modes.CTR([1] * 16) # type:ignore[arg-type]

def test_gcm_iv(self):
with pytest.raises(TypeError):
modes.GCM([1] * 16)
modes.GCM([1] * 16) # type:ignore[arg-type]

def test_gcm_tag(self):
with pytest.raises(TypeError):
modes.GCM(b"\x00" * 16, [1] * 16)
modes.GCM(b"\x00" * 16, [1] * 16) # type:ignore[arg-type]
4 changes: 2 additions & 2 deletions tests/hazmat/primitives/test_chacha20.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def test_invalid_nonce(self):
algorithms.ChaCha20(b"0" * 32, b"0")

with pytest.raises(TypeError):
algorithms.ChaCha20(b"0" * 32, object())
algorithms.ChaCha20(b"0" * 32, object()) # type:ignore[arg-type]

def test_invalid_key_type(self):
with pytest.raises(TypeError, match="key must be bytes"):
algorithms.ChaCha20("0" * 32, b"0" * 16)
algorithms.ChaCha20("0" * 32, b"0" * 16) # type:ignore[arg-type]
18 changes: 9 additions & 9 deletions tests/hazmat/primitives/test_ciphers.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_invalid_key_size(self):

def test_invalid_key_type(self):
with pytest.raises(TypeError, match="key must be bytes"):
AES("0" * 32)
AES("0" * 32) # type: ignore[arg-type]


class TestAESXTS(object):
Expand All @@ -59,7 +59,7 @@ def test_invalid_key_size_with_mode(self, mode, backend):

def test_xts_tweak_not_bytes(self):
with pytest.raises(TypeError):
modes.XTS(32)
modes.XTS(32) # type: ignore[arg-type]

def test_xts_tweak_too_small(self):
with pytest.raises(ValueError):
Expand Down Expand Up @@ -93,7 +93,7 @@ def test_invalid_key_size(self):

def test_invalid_key_type(self):
with pytest.raises(TypeError, match="key must be bytes"):
Camellia("0" * 32)
Camellia("0" * 32) # type: ignore[arg-type]


class TestTripleDES(object):
Expand All @@ -108,7 +108,7 @@ def test_invalid_key_size(self):

def test_invalid_key_type(self):
with pytest.raises(TypeError, match="key must be bytes"):
TripleDES("0" * 16)
TripleDES("0" * 16) # type: ignore[arg-type]


class TestBlowfish(object):
Expand All @@ -126,7 +126,7 @@ def test_invalid_key_size(self):

def test_invalid_key_type(self):
with pytest.raises(TypeError, match="key must be bytes"):
Blowfish("0" * 8)
Blowfish("0" * 8) # type: ignore[arg-type]


class TestCAST5(object):
Expand All @@ -144,7 +144,7 @@ def test_invalid_key_size(self):

def test_invalid_key_type(self):
with pytest.raises(TypeError, match="key must be bytes"):
CAST5("0" * 10)
CAST5("0" * 10) # type: ignore[arg-type]


class TestARC4(object):
Expand All @@ -170,7 +170,7 @@ def test_invalid_key_size(self):

def test_invalid_key_type(self):
with pytest.raises(TypeError, match="key must be bytes"):
ARC4("0" * 10)
ARC4("0" * 10) # type: ignore[arg-type]


class TestIDEA(object):
Expand All @@ -184,7 +184,7 @@ def test_invalid_key_size(self):

def test_invalid_key_type(self):
with pytest.raises(TypeError, match="key must be bytes"):
IDEA("0" * 16)
IDEA("0" * 16) # type: ignore[arg-type]


class TestSEED(object):
Expand All @@ -198,7 +198,7 @@ def test_invalid_key_size(self):

def test_invalid_key_type(self):
with pytest.raises(TypeError, match="key must be bytes"):
SEED("0" * 16)
SEED("0" * 16) # type: ignore[arg-type]


def test_invalid_backend():
Expand Down
10 changes: 5 additions & 5 deletions tests/hazmat/primitives/test_cmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_aes_verify(self, backend, params):

cmac = CMAC(AES(binascii.unhexlify(key)), backend)
cmac.update(binascii.unhexlify(message))
assert cmac.verify(binascii.unhexlify(output)) is None
cmac.verify(binascii.unhexlify(output))

@pytest.mark.supported(
only_if=lambda backend: backend.cmac_algorithm_supported(
Expand Down Expand Up @@ -122,7 +122,7 @@ def test_3des_verify(self, backend, params):

cmac = CMAC(TripleDES(binascii.unhexlify(key)), backend)
cmac.update(binascii.unhexlify(message))
assert cmac.verify(binascii.unhexlify(output)) is None
cmac.verify(binascii.unhexlify(output))

@pytest.mark.supported(
only_if=lambda backend: backend.cmac_algorithm_supported(
Expand All @@ -145,7 +145,7 @@ def test_invalid_verify(self, backend):
def test_invalid_algorithm(self, backend):
key = b"0102030405"
with pytest.raises(TypeError):
CMAC(ARC4(key), backend)
CMAC(ARC4(key), backend) # type: ignore[arg-type]

@pytest.mark.supported(
only_if=lambda backend: backend.cmac_algorithm_supported(
Expand Down Expand Up @@ -181,10 +181,10 @@ def test_verify_reject_unicode(self, backend):
cmac = CMAC(AES(key), backend)

with pytest.raises(TypeError):
cmac.update("")
cmac.update("") # type: ignore[arg-type]

with pytest.raises(TypeError):
cmac.verify("")
cmac.verify("") # type: ignore[arg-type]

@pytest.mark.supported(
only_if=lambda backend: backend.cmac_algorithm_supported(
Expand Down
6 changes: 3 additions & 3 deletions tests/hazmat/primitives/test_constant_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
class TestConstantTimeBytesEq(object):
def test_reject_unicode(self):
with pytest.raises(TypeError):
constant_time.bytes_eq(b"foo", "foo")
constant_time.bytes_eq(b"foo", "foo") # type: ignore[arg-type]

with pytest.raises(TypeError):
constant_time.bytes_eq("foo", b"foo")
constant_time.bytes_eq("foo", b"foo") # type: ignore[arg-type]

with pytest.raises(TypeError):
constant_time.bytes_eq("foo", "foo")
constant_time.bytes_eq("foo", "foo") # type: ignore[arg-type]

def test_compares(self):
assert constant_time.bytes_eq(b"foo", b"foo") is True
Expand Down
4 changes: 3 additions & 1 deletion tests/hazmat/primitives/test_dh.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import binascii
import itertools
import os
import typing

import pytest

Expand Down Expand Up @@ -203,7 +204,7 @@ def test_convert_to_numbers(self, backend, with_q):
)[0]
p = int(vector["p"], 16)
g = int(vector["g"], 16)
q = int(vector["q"], 16)
q: typing.Optional[int] = int(vector["q"], 16)
else:
parameters = backend.generate_dh_private_key_and_parameters(2, 512)

Expand Down Expand Up @@ -388,6 +389,7 @@ def test_load_256bit_key_from_pkcs8(self, backend):
mode="rb",
)
key = serialization.load_pem_private_key(data, None, backend)
assert isinstance(key, dh.DHPrivateKey)
assert key.key_size == 256

@pytest.mark.parametrize(
Expand Down
Loading

0 comments on commit 343ac13

Please sign in to comment.