From e6907b9acc28b458d78d7f58570fea6e14e169e6 Mon Sep 17 00:00:00 2001 From: Pyrox Date: Wed, 31 Jul 2024 11:31:57 -0400 Subject: [PATCH] nose -> pytest (#76) --- python/.coveragerc | 2 - python/.noserc | 12 ----- python/http_ece/tests/test_ece.py | 74 +++++++++++++++---------------- python/pytest.ini | 4 ++ python/setup.cfg | 2 - python/setup.py | 11 +++-- python/tox.ini | 14 +++--- 7 files changed, 58 insertions(+), 61 deletions(-) delete mode 100644 python/.noserc create mode 100644 python/pytest.ini diff --git a/python/.coveragerc b/python/.coveragerc index 0d853ef..cc9c803 100644 --- a/python/.coveragerc +++ b/python/.coveragerc @@ -1,4 +1,2 @@ [report] -omit = - *noseplugin* show_missing = true diff --git a/python/.noserc b/python/.noserc deleted file mode 100644 index c86834a..0000000 --- a/python/.noserc +++ /dev/null @@ -1,12 +0,0 @@ -# this file's explicitly loaded from setup.cfg (.noserc isn't a -# standard config path), separated out due to '%(...)s' -[nosetests] -verbose=True -verbosity=1 -detailed-errors=True -with-coverage=True -cover-erase=True -cover-package=http_ece -cover-tests=True -logging-format=%(asctime)s,%(msecs)03d %(name)s: %(levelname)s: %(message)s -logging-datefmt=%H:%M:%S diff --git a/python/http_ece/tests/test_ece.py b/python/http_ece/tests/test_ece.py index 1dce005..1eb6c58 100644 --- a/python/http_ece/tests/test_ece.py +++ b/python/http_ece/tests/test_ece.py @@ -7,7 +7,7 @@ from cryptography.hazmat.primitives.asymmetric import ec from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat -from nose.tools import eq_, assert_raises +from pytest import raises import http_ece as ece from http_ece import ECEException @@ -59,7 +59,7 @@ def setUp(self): self.m_salt = os.urandom(16) def test_derive_key_invalid_mode(self): - with assert_raises(ECEException) as ex: + with raises(ECEException) as ex: ece.derive_key( "invalid", version="aes128gcm", @@ -70,10 +70,10 @@ def test_derive_key_invalid_mode(self): auth_secret=None, keyid="valid", ) - eq_(ex.exception.message, "unknown 'mode' specified: invalid") + assert ex.value.message == "unknown 'mode' specified: invalid" def test_derive_key_invalid_salt(self): - with assert_raises(ECEException) as ex: + with raises(ECEException) as ex: ece.derive_key( "encrypt", version="aes128gcm", @@ -84,10 +84,10 @@ def test_derive_key_invalid_salt(self): auth_secret=None, keyid="valid", ) - eq_(ex.exception.message, "'salt' must be a 16 octet value") + assert ex.value.message == "'salt' must be a 16 octet value" def test_derive_key_invalid_version(self): - with assert_raises(ECEException) as ex: + with raises(ECEException) as ex: ece.derive_key( "encrypt", version="invalid", @@ -98,10 +98,10 @@ def test_derive_key_invalid_version(self): auth_secret=None, keyid="valid", ) - eq_(ex.exception.message, "Invalid version") + assert ex.value.message == "Invalid version" def test_derive_key_no_private_key(self): - with assert_raises(ECEException) as ex: + with raises(ECEException) as ex: ece.derive_key( "encrypt", version="aes128gcm", @@ -112,10 +112,10 @@ def test_derive_key_no_private_key(self): auth_secret=None, keyid="valid", ) - eq_(ex.exception.message, "DH requires a private_key") + assert ex.value.message == "DH requires a private_key" def test_derive_key_no_secret(self): - with assert_raises(ECEException) as ex: + with raises(ECEException) as ex: ece.derive_key( "encrypt", version="aes128gcm", @@ -126,12 +126,12 @@ def test_derive_key_no_secret(self): auth_secret=None, keyid="valid", ) - eq_(ex.exception.message, "unable to determine the secret") + assert ex.value.message == "unable to determine the secret" def test_iv_bad_counter(self): - with assert_raises(ECEException) as ex: + with raises(ECEException) as ex: ece.iv(os.urandom(8), pow(2, 64) + 1) - eq_(ex.exception.message, "Counter too big") + assert ex.value.message == "Counter too big" class TestEceChecking(unittest.TestCase): @@ -144,65 +144,65 @@ def setUp(self): self.m_header += struct.pack("!L", 32) + b"\0" def test_encrypt_small_rs(self): - with assert_raises(ECEException) as ex: + with raises(ECEException) as ex: ece.encrypt( self.m_input, version="aes128gcm", key=self.m_key, rs=1, ) - eq_(ex.exception.message, "Record size too small") + assert ex.value.message == "Record size too small" def test_decrypt_small_rs(self): header = os.urandom(16) + struct.pack("!L", 2) + b"\0" - with assert_raises(ECEException) as ex: + with raises(ECEException) as ex: ece.decrypt( header + self.m_input, version="aes128gcm", key=self.m_key, rs=1, ) - eq_(ex.exception.message, "Record size too small") + assert ex.value.message == "Record size too small" def test_encrypt_bad_version(self): - with assert_raises(ECEException) as ex: + with raises(ECEException) as ex: ece.encrypt( self.m_input, version="bogus", key=self.m_key, ) - eq_(ex.exception.message, "Invalid version") + assert ex.value.message == "Invalid version" def test_decrypt_bad_version(self): - with assert_raises(ECEException) as ex: + with raises(ECEException) as ex: ece.decrypt( self.m_input, version="bogus", key=self.m_key, ) - eq_(ex.exception.message, "Invalid version") + assert ex.value.message == "Invalid version" def test_decrypt_bad_header(self): - with assert_raises(ECEException) as ex: + with raises(ECEException) as ex: ece.decrypt( os.urandom(4), version="aes128gcm", key=self.m_key, ) - eq_(ex.exception.message, "Could not parse the content header") + assert ex.value.message == "Could not parse the content header" def test_encrypt_long_keyid(self): - with assert_raises(ECEException) as ex: + with raises(ECEException) as ex: ece.encrypt( self.m_input, version="aes128gcm", key=self.m_key, keyid=b64e(os.urandom(192)), # 256 bytes ) - eq_(ex.exception.message, "keyid is too long") + assert ex.value.message == "keyid is too long" def test_overlong_padding(self): - with assert_raises(ECEException) as ex: + with raises(ECEException) as ex: ece.decrypt( self.m_header + b"\xbb\xc7\xb9ev\x0b\xf0f+\x93\xf4" b"\xe5\xd6\x94\xb7e\xf0\xcd\x15\x9b(\x01\xa5", @@ -210,10 +210,10 @@ def test_overlong_padding(self): key=b"d\xc7\x0ed\xa7%U\x14Q\xf2\x08\xdf\xba\xa0\xb9r", keyid=b64e(os.urandom(192)), # 256 bytes ) - eq_(ex.exception.message, "all zero record plaintext") + assert ex.value.message == "all zero record plaintext" def test_bad_early_delimiter(self): - with assert_raises(ECEException) as ex: + with raises(ECEException) as ex: ece.decrypt( self.m_header + b"\xb9\xc7\xb9ev\x0b\xf0\x9eB\xb1\x08C8u" b"\xa3\x06\xc9x\x06\n\xfc|}\xe9R\x85\x91" @@ -224,10 +224,10 @@ def test_bad_early_delimiter(self): key=b"d\xc7\x0ed\xa7%U\x14Q\xf2\x08\xdf\xba\xa0\xb9r", keyid=b64e(os.urandom(192)), # 256 bytes ) - eq_(ex.exception.message, "record delimiter != 1") + assert ex.value.message == "record delimiter != 1" def test_bad_final_delimiter(self): - with assert_raises(ECEException) as ex: + with raises(ECEException) as ex: ece.decrypt( self.m_header + b"\xba\xc7\xb9ev\x0b\xf0\x9eB\xb1\x08Ji" b"\xe4P\x1b\x8dI\xdb\xc6y#MG\xc2W\x16", @@ -235,10 +235,10 @@ def test_bad_final_delimiter(self): key=b"d\xc7\x0ed\xa7%U\x14Q\xf2\x08\xdf\xba\xa0\xb9r", keyid=b64e(os.urandom(192)), # 256 bytes ) - eq_(ex.exception.message, "last record delimiter != 2") + assert ex.value.message == "last record delimiter != 2" def test_damage(self): - with assert_raises(ECEException) as ex: + with raises(ECEException) as ex: ece.decrypt( self.m_header + b"\xbb\xc6\xb1\x1dF:~\x0f\x07+\xbe\xaaD" b"\xe0\xd6.K\xe5\xf9]%\xe3\x86q\xe0}", @@ -246,7 +246,7 @@ def test_damage(self): key=b"d\xc7\x0ed\xa7%U\x14Q\xf2\x08\xdf\xba\xa0\xb9r", keyid=b64e(os.urandom(192)), # 256 bytes ) - eq_(ex.exception.message, "Decryption error: InvalidTag()") + assert ex.value.message == "Decryption error: InvalidTag()" class TestEceIntegration(unittest.TestCase): @@ -319,7 +319,7 @@ def encrypt_decrypt(self, input, encrypt_params, decrypt_params=None, version=No version=version, ) logbuf("Decrypted", decrypted) - eq_(input, decrypted) + assert input == decrypted def use_explicit_key(self, version=None): params = { @@ -350,9 +350,9 @@ def detect_truncation(self, version): chunk = encrypted[0 : 21 + rs] else: chunk = encrypted[0 : rs + 16] - with assert_raises(ECEException) as ex: + with raises(ECEException) as ex: ece.decrypt(chunk, salt=salt, key=key, rs=rs, version=version) - eq_(ex.exception.message, "Message truncated") + assert ex.value.message == "Message truncated" def use_dh(self, version): def pubbytes(k): @@ -487,7 +487,7 @@ def _run(self, mode): rs=p.get("rs", 4096), version=p["version"], ) - eq_(b64d(data[outp]), result) + assert b64d(data[outp]) == result def test_decrypt(self): self._run("decrypt") diff --git a/python/pytest.ini b/python/pytest.ini new file mode 100644 index 0000000..c105b53 --- /dev/null +++ b/python/pytest.ini @@ -0,0 +1,4 @@ +[pytest] +addopts= --verbose --verbosity=1 --cov=http_ece +log_format = %(asctime)s,%(msecs)03d %(name)s: %(levelname)s: %(message)s +log_file_date_format = %H:%M:%S diff --git a/python/setup.cfg b/python/setup.cfg index bd3da7e..3c6e79c 100644 --- a/python/setup.cfg +++ b/python/setup.cfg @@ -1,4 +1,2 @@ [bdist_wheel] universal=1 -[nosetests] -config=.noserc diff --git a/python/setup.py b/python/setup.py index e41cddf..5a63f1d 100755 --- a/python/setup.py +++ b/python/setup.py @@ -24,16 +24,21 @@ "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", ], keywords="crypto http", install_requires=[ "cryptography>=2.5", ], tests_require=[ - "nose", - "coverage", + "pytest", + "pytest-cov", ], - test_suite="nose.collector", url="https://github.com/martinthomson/encrypted-content-encoding", license="MIT", ) diff --git a/python/tox.ini b/python/tox.ini index 5d25160..8be121d 100644 --- a/python/tox.ini +++ b/python/tox.ini @@ -4,18 +4,22 @@ # and then run "tox" from this directory. [tox] -envlist = py27,py34,py35 +envlist = py27,py34,py35,py38,py39,py310,py311,py312 [testenv] basepython = py27: python2.7 py34: python3.4 py35: python3.5 + py38: python3.8 + py39: python3.9 + py310: python3.10 + py311: python3.11 + py312: python3.12 commands = - nosetests \ + pytest \ [] deps = - nose - mock - coverage + pytest + pytest-cov flake8