Skip to content

Commit

Permalink
nose -> pytest (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrox0 authored Jul 31, 2024
1 parent 831b6bf commit e6907b9
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 61 deletions.
2 changes: 0 additions & 2 deletions python/.coveragerc
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
[report]
omit =
*noseplugin*
show_missing = true
12 changes: 0 additions & 12 deletions python/.noserc

This file was deleted.

74 changes: 37 additions & 37 deletions python/http_ece/tests/test_ece.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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):
Expand All @@ -144,76 +144,76 @@ 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",
version="aes128gcm",
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"
Expand All @@ -224,29 +224,29 @@ 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",
version="aes128gcm",
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}",
version="aes128gcm",
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):
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 4 additions & 0 deletions python/pytest.ini
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions python/setup.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
[bdist_wheel]
universal=1
[nosetests]
config=.noserc
11 changes: 8 additions & 3 deletions python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
14 changes: 9 additions & 5 deletions python/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e6907b9

Please sign in to comment.