diff options
author | Alex Chan <alex@alexwlchan.net> | 2016-12-19 12:02:06 +0000 |
---|---|---|
committer | Hynek Schlawack <hs@ox.cx> | 2016-12-19 13:02:06 +0100 |
commit | 63ef9bc5f98261ca9585919b929c13d015fc5d1f (patch) | |
tree | bff447ffd274bf5663a2e78e2a2d1f8ed1d774ce | |
parent | 6d97756da3bf9de5ac5632eed6a5c6856830276b (diff) | |
download | pyopenssl-63ef9bc5f98261ca9585919b929c13d015fc5d1f.tar.gz |
Convert the elliptic curve tests to be pytest-style (#576)
-rw-r--r-- | tests/test_crypto.py | 93 | ||||
-rw-r--r-- | tests/util.py | 20 |
2 files changed, 45 insertions, 68 deletions
diff --git a/tests/test_crypto.py b/tests/test_crypto.py index e4cf496..710f737 100644 --- a/tests/test_crypto.py +++ b/tests/test_crypto.py @@ -43,7 +43,7 @@ from OpenSSL.crypto import CRL, Revoked, dump_crl, load_crl from OpenSSL.crypto import NetscapeSPKI, NetscapeSPKIType from OpenSSL.crypto import ( sign, verify, get_elliptic_curve, get_elliptic_curves) -from OpenSSL._util import native, lib +from OpenSSL._util import native from .util import ( EqualityTestsMixin, is_consistent_type, TestCase, WARNING_TYPE_EXPECTED @@ -3864,52 +3864,34 @@ class SignVerifyTests(TestCase): sign(priv_key, content, "sha1") -class EllipticCurveTests(TestCase): +class TestEllipticCurve(object): """ - Tests for :py:class:`_EllipticCurve`, :py:obj:`get_elliptic_curve`, and - :py:obj:`get_elliptic_curves`. + Tests for `_EllipticCurve`, `get_elliptic_curve`, and + `get_elliptic_curves`. """ def test_set(self): """ - :py:obj:`get_elliptic_curves` returns a :py:obj:`set`. + `get_elliptic_curves` returns a `set`. """ - self.assertIsInstance(get_elliptic_curves(), set) - - def test_some_curves(self): - """ - If :py:mod:`cryptography` has elliptic curve support then the set - returned by :py:obj:`get_elliptic_curves` has some elliptic curves in - it. - - There could be an OpenSSL that violates this assumption. If so, this - test will fail and we'll find out. - """ - curves = get_elliptic_curves() - if lib.Cryptography_HAS_EC: - self.assertTrue(curves) - else: - self.assertFalse(curves) + assert isinstance(get_elliptic_curves(), set) def test_a_curve(self): """ - :py:obj:`get_elliptic_curve` can be used to retrieve a particular - supported curve. + `get_elliptic_curve` can be used to retrieve a particular supported + curve. """ curves = get_elliptic_curves() - if curves: - curve = next(iter(curves)) - self.assertEqual(curve.name, get_elliptic_curve(curve.name).name) - else: - self.assertRaises(ValueError, get_elliptic_curve, u"prime256v1") + curve = next(iter(curves)) + assert curve.name == get_elliptic_curve(curve.name).name def test_not_a_curve(self): """ - :py:obj:`get_elliptic_curve` raises :py:class:`ValueError` if called - with a name which does not identify a supported curve. + `get_elliptic_curve` raises `ValueError` if called with a name which + does not identify a supported curve. """ - self.assertRaises( - ValueError, get_elliptic_curve, u"this curve was just invented") + with pytest.raises(ValueError): + get_elliptic_curve(u"this curve was just invented") def test_repr(self): """ @@ -3917,22 +3899,20 @@ class EllipticCurveTests(TestCase): object is a curve and what its name is. """ curves = get_elliptic_curves() - if curves: - curve = next(iter(curves)) - self.assertEqual("<Curve %r>" % (curve.name,), repr(curve)) + curve = next(iter(curves)) + assert "<Curve %r>" % (curve.name,) == repr(curve) def test_to_EC_KEY(self): """ The curve object can export a version of itself as an EC_KEY* via the - private :py:meth:`_EllipticCurve._to_EC_KEY`. + private `_EllipticCurve._to_EC_KEY`. """ curves = get_elliptic_curves() - if curves: - curve = next(iter(curves)) - # It's not easy to assert anything about this object. However, see - # leakcheck/crypto.py for a test that demonstrates it at least does - # not leak memory. - curve._to_EC_KEY() + curve = next(iter(curves)) + # It's not easy to assert anything about this object. However, see + # leakcheck/crypto.py for a test that demonstrates it at least does + # not leak memory. + curve._to_EC_KEY() class EllipticCurveFactory(object): @@ -3942,16 +3922,13 @@ class EllipticCurveFactory(object): def __init__(self): curves = iter(get_elliptic_curves()) - try: - self.curve_name = next(curves).name - self.another_curve_name = next(curves).name - except StopIteration: - self.curve_name = self.another_curve_name = None + self.curve_name = next(curves).name + self.another_curve_name = next(curves).name -class EllipticCurveEqualityTests(TestCase, EqualityTestsMixin): +class TestEllipticCurveEquality(EqualityTestsMixin): """ - Tests :py:type:`_EllipticCurve`\ 's implementation of ``==`` and ``!=``. + Tests `_EllipticCurve`\ 's implementation of ``==`` and ``!=``. """ curve_factory = EllipticCurveFactory() @@ -3972,10 +3949,10 @@ class EllipticCurveEqualityTests(TestCase, EqualityTestsMixin): return get_elliptic_curve(self.curve_factory.another_curve_name) -class EllipticCurveHashTests(TestCase): +class TestEllipticCurveHash(object): """ - Tests for :py:type:`_EllipticCurve`\ 's implementation of hashing (thus use - as an item in a :py:type:`dict` or :py:type:`set`). + Tests for `_EllipticCurve`'s implementation of hashing (thus use as + an item in a `dict` or `set`). """ curve_factory = EllipticCurveFactory() @@ -3984,20 +3961,20 @@ class EllipticCurveHashTests(TestCase): def test_contains(self): """ - The ``in`` operator reports that a :py:type:`set` containing a curve - does contain that curve. + The ``in`` operator reports that a `set` containing a curve does + contain that curve. """ curve = get_elliptic_curve(self.curve_factory.curve_name) curves = set([curve]) - self.assertIn(curve, curves) + assert curve in curves def test_does_not_contain(self): """ - The ``in`` operator reports that a :py:type:`set` not containing a - curve does not contain that curve. + The ``in`` operator reports that a `set` not containing a curve + does not contain that curve. """ curve = get_elliptic_curve(self.curve_factory.curve_name) curves = set([ get_elliptic_curve(self.curve_factory.another_curve_name) ]) - self.assertNotIn(curve, curves) + assert curve not in curves diff --git a/tests/util.py b/tests/util.py index 7520a28..e1b8656 100644 --- a/tests/util.py +++ b/tests/util.py @@ -354,14 +354,14 @@ class EqualityTestsMixin(object): An object compares equal to itself using the C{==} operator. """ o = self.anInstance() - self.assertTrue(o == o) + assert (o == o) def test_identicalNe(self): """ An object doesn't compare not equal to itself using the C{!=} operator. """ o = self.anInstance() - self.assertFalse(o != o) + assert not (o != o) def test_sameEq(self): """ @@ -370,7 +370,7 @@ class EqualityTestsMixin(object): """ a = self.anInstance() b = self.anInstance() - self.assertTrue(a == b) + assert (a == b) def test_sameNe(self): """ @@ -379,7 +379,7 @@ class EqualityTestsMixin(object): """ a = self.anInstance() b = self.anInstance() - self.assertFalse(a != b) + assert not (a != b) def test_differentEq(self): """ @@ -388,7 +388,7 @@ class EqualityTestsMixin(object): """ a = self.anInstance() b = self.anotherInstance() - self.assertFalse(a == b) + assert not (a == b) def test_differentNe(self): """ @@ -397,7 +397,7 @@ class EqualityTestsMixin(object): """ a = self.anInstance() b = self.anotherInstance() - self.assertTrue(a != b) + assert (a != b) def test_anotherTypeEq(self): """ @@ -406,7 +406,7 @@ class EqualityTestsMixin(object): """ a = self.anInstance() b = object() - self.assertFalse(a == b) + assert not (a == b) def test_anotherTypeNe(self): """ @@ -415,7 +415,7 @@ class EqualityTestsMixin(object): """ a = self.anInstance() b = object() - self.assertTrue(a != b) + assert (a != b) def test_delegatedEq(self): """ @@ -429,7 +429,7 @@ class EqualityTestsMixin(object): a = self.anInstance() b = Delegate() - self.assertEqual(a == b, [b]) + assert (a == b) == [b] def test_delegateNe(self): """ @@ -443,7 +443,7 @@ class EqualityTestsMixin(object): a = self.anInstance() b = Delegate() - self.assertEqual(a != b, [b]) + assert (a != b) == [b] # The type name expected in warnings about using the wrong string type. |