summaryrefslogtreecommitdiff
path: root/tests/test_crypto.py
diff options
context:
space:
mode:
authorAlex Chan <alex@alexwlchan.net>2016-12-19 12:02:06 +0000
committerHynek Schlawack <hs@ox.cx>2016-12-19 13:02:06 +0100
commit63ef9bc5f98261ca9585919b929c13d015fc5d1f (patch)
treebff447ffd274bf5663a2e78e2a2d1f8ed1d774ce /tests/test_crypto.py
parent6d97756da3bf9de5ac5632eed6a5c6856830276b (diff)
downloadpyopenssl-git-63ef9bc5f98261ca9585919b929c13d015fc5d1f.tar.gz
Convert the elliptic curve tests to be pytest-style (#576)
Diffstat (limited to 'tests/test_crypto.py')
-rw-r--r--tests/test_crypto.py93
1 files changed, 35 insertions, 58 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