Merge pull request #1928 from reaperhulk/cp-eq
add eq/ne methods for all certificate policies classes
diff --git a/src/cryptography/x509.py b/src/cryptography/x509.py
index 50fae71..cdddfb5 100644
--- a/src/cryptography/x509.py
+++ b/src/cryptography/x509.py
@@ -481,6 +481,15 @@
def __repr__(self):
return "<CertificatePolicies({0})>".format(self._policies)
+ def __eq__(self, other):
+ if not isinstance(other, CertificatePolicies):
+ return NotImplemented
+
+ return self._policies == other._policies
+
+ def __ne__(self, other):
+ return not self == other
+
class PolicyInformation(object):
def __init__(self, policy_identifier, policy_qualifiers):
@@ -506,6 +515,18 @@
"y_qualifiers={0.policy_qualifiers})>".format(self)
)
+ def __eq__(self, other):
+ if not isinstance(other, PolicyInformation):
+ return NotImplemented
+
+ return (
+ self.policy_identifier == other.policy_identifier and
+ self.policy_qualifiers == other.policy_qualifiers
+ )
+
+ def __ne__(self, other):
+ return not self == other
+
policy_identifier = utils.read_only_property("_policy_identifier")
policy_qualifiers = utils.read_only_property("_policy_qualifiers")
@@ -528,6 +549,18 @@
"{0.explicit_text!r})>".format(self)
)
+ def __eq__(self, other):
+ if not isinstance(other, UserNotice):
+ return NotImplemented
+
+ return (
+ self.notice_reference == other.notice_reference and
+ self.explicit_text == other.explicit_text
+ )
+
+ def __ne__(self, other):
+ return not self == other
+
notice_reference = utils.read_only_property("_notice_reference")
explicit_text = utils.read_only_property("_explicit_text")
@@ -550,6 +583,18 @@
"{0.notice_numbers})>".format(self)
)
+ def __eq__(self, other):
+ if not isinstance(other, NoticeReference):
+ return NotImplemented
+
+ return (
+ self.organization == other.organization and
+ self.notice_numbers == other.notice_numbers
+ )
+
+ def __ne__(self, other):
+ return not self == other
+
organization = utils.read_only_property("_organization")
notice_numbers = utils.read_only_property("_notice_numbers")
diff --git a/tests/test_x509_ext.py b/tests/test_x509_ext.py
index ae69f5f..f07792d 100644
--- a/tests/test_x509_ext.py
+++ b/tests/test_x509_ext.py
@@ -63,6 +63,19 @@
"4])>"
)
+ def test_eq(self):
+ nr = x509.NoticeReference("org", [1, 2])
+ nr2 = x509.NoticeReference("org", [1, 2])
+ assert nr == nr2
+
+ def test_ne(self):
+ nr = x509.NoticeReference("org", [1, 2])
+ nr2 = x509.NoticeReference("org", [1])
+ nr3 = x509.NoticeReference(None, [1, 2])
+ assert nr != nr2
+ assert nr != nr3
+ assert nr != object()
+
class TestUserNotice(object):
def test_notice_reference_invalid(self):
@@ -87,6 +100,23 @@
"'org', notice_numbers=None)>, explicit_text=u'text')>"
)
+ def test_eq(self):
+ nr = x509.NoticeReference("org", [1, 2])
+ nr2 = x509.NoticeReference("org", [1, 2])
+ un = x509.UserNotice(nr, "text")
+ un2 = x509.UserNotice(nr2, "text")
+ assert un == un2
+
+ def test_ne(self):
+ nr = x509.NoticeReference("org", [1, 2])
+ nr2 = x509.NoticeReference("org", [1])
+ un = x509.UserNotice(nr, "text")
+ un2 = x509.UserNotice(nr2, "text")
+ un3 = x509.UserNotice(nr, "text3")
+ assert un != un2
+ assert un != un3
+ assert un != object()
+
class TestPolicyInformation(object):
def test_invalid_policy_identifier(self):
@@ -124,6 +154,31 @@
"otice(notice_reference=None, explicit_text=u'hi')>])>"
)
+ def test_eq(self):
+ pi = x509.PolicyInformation(
+ x509.ObjectIdentifier("1.2.3"),
+ [u"string", x509.UserNotice(None, u"hi")]
+ )
+ pi2 = x509.PolicyInformation(
+ x509.ObjectIdentifier("1.2.3"),
+ [u"string", x509.UserNotice(None, u"hi")]
+ )
+ assert pi == pi2
+
+ def test_ne(self):
+ pi = x509.PolicyInformation(
+ x509.ObjectIdentifier("1.2.3"), [u"string"]
+ )
+ pi2 = x509.PolicyInformation(
+ x509.ObjectIdentifier("1.2.3"), [u"string2"]
+ )
+ pi3 = x509.PolicyInformation(
+ x509.ObjectIdentifier("1.2.3.4"), [u"string"]
+ )
+ assert pi != pi2
+ assert pi != pi3
+ assert pi != object()
+
class TestCertificatePolicies(object):
def test_invalid_policies(self):
@@ -157,6 +212,29 @@
"ers=[u'string'])>])>"
)
+ def test_eq(self):
+ pi = x509.PolicyInformation(
+ x509.ObjectIdentifier("1.2.3"), [u"string"]
+ )
+ cp = x509.CertificatePolicies([pi])
+ pi2 = x509.PolicyInformation(
+ x509.ObjectIdentifier("1.2.3"), [u"string"]
+ )
+ cp2 = x509.CertificatePolicies([pi2])
+ assert cp == cp2
+
+ def test_ne(self):
+ pi = x509.PolicyInformation(
+ x509.ObjectIdentifier("1.2.3"), [u"string"]
+ )
+ cp = x509.CertificatePolicies([pi])
+ pi2 = x509.PolicyInformation(
+ x509.ObjectIdentifier("1.2.3"), [u"string2"]
+ )
+ cp2 = x509.CertificatePolicies([pi2])
+ assert cp != cp2
+ assert cp != object()
+
class TestKeyUsage(object):
def test_key_agreement_false_encipher_decipher_true(self):