From ab96a53bc9020a4d605d57dc972e45df5c8c4862 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Thu, 14 Sep 2017 07:42:33 +0800 Subject: implement __hash__ on CertificatePolicies and its child classes (#3914) --- src/cryptography/x509/extensions.py | 17 ++++++++++++++ tests/x509/test_x509_ext.py | 45 +++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/cryptography/x509/extensions.py b/src/cryptography/x509/extensions.py index 8880f744..9d6b3e79 100644 --- a/src/cryptography/x509/extensions.py +++ b/src/cryptography/x509/extensions.py @@ -597,6 +597,9 @@ class CertificatePolicies(object): def __getitem__(self, idx): return self._policies[idx] + def __hash__(self): + return hash(tuple(self._policies)) + class PolicyInformation(object): def __init__(self, policy_identifier, policy_qualifiers): @@ -636,6 +639,14 @@ class PolicyInformation(object): def __ne__(self, other): return not self == other + def __hash__(self): + if self.policy_qualifiers is not None: + pq = tuple(self.policy_qualifiers) + else: + pq = None + + return hash((self.policy_identifier, pq)) + policy_identifier = utils.read_only_property("_policy_identifier") policy_qualifiers = utils.read_only_property("_policy_qualifiers") @@ -670,6 +681,9 @@ class UserNotice(object): def __ne__(self, other): return not self == other + def __hash__(self): + return hash((self.notice_reference, self.explicit_text)) + notice_reference = utils.read_only_property("_notice_reference") explicit_text = utils.read_only_property("_explicit_text") @@ -703,6 +717,9 @@ class NoticeReference(object): def __ne__(self, other): return not self == other + def __hash__(self): + return hash((self.organization, tuple(self.notice_numbers))) + organization = utils.read_only_property("_organization") notice_numbers = utils.read_only_property("_notice_numbers") diff --git a/tests/x509/test_x509_ext.py b/tests/x509/test_x509_ext.py index 22b4cc5b..8d13753d 100644 --- a/tests/x509/test_x509_ext.py +++ b/tests/x509/test_x509_ext.py @@ -369,6 +369,13 @@ class TestNoticeReference(object): assert nr != nr3 assert nr != object() + def test_hash(self): + nr = x509.NoticeReference("org", [1, 2]) + nr2 = x509.NoticeReference("org", [1, 2]) + nr3 = x509.NoticeReference(None, [1, 2]) + assert hash(nr) == hash(nr2) + assert hash(nr) != hash(nr3) + class TestUserNotice(object): def test_notice_reference_invalid(self): @@ -410,6 +417,15 @@ class TestUserNotice(object): assert un != un3 assert un != object() + def test_hash(self): + nr = x509.NoticeReference("org", [1, 2]) + nr2 = x509.NoticeReference("org", [1, 2]) + un = x509.UserNotice(nr, "text") + un2 = x509.UserNotice(nr2, "text") + un3 = x509.UserNotice(None, "text") + assert hash(un) == hash(un2) + assert hash(un) != hash(un3) + class TestPolicyInformation(object): def test_invalid_policy_identifier(self): @@ -477,6 +493,19 @@ class TestPolicyInformation(object): assert pi != pi3 assert pi != object() + def test_hash(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")] + ) + pi3 = x509.PolicyInformation(x509.ObjectIdentifier("1.2.3"), None) + assert hash(pi) == hash(pi2) + assert hash(pi) != hash(pi3) + @pytest.mark.requires_backend_interface(interface=X509Backend) class TestCertificatePolicies(object): @@ -571,6 +600,22 @@ class TestCertificatePolicies(object): assert ext.value[0].policy_identifier == oid + def test_hash(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]) + pi3 = x509.PolicyInformation( + x509.ObjectIdentifier("1.2.3"), [x509.UserNotice(None, b"text")] + ) + cp3 = x509.CertificatePolicies([pi3]) + assert hash(cp) == hash(cp2) + assert hash(cp) != hash(cp3) + @pytest.mark.requires_backend_interface(interface=RSABackend) @pytest.mark.requires_backend_interface(interface=X509Backend) -- cgit v1.2.3