Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement __hash__ on CertificatePolicies and its child classes #3914

Merged
merged 1 commit into from
Sep 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/cryptography/x509/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,9 @@ def __ne__(self, other):
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):
Expand Down Expand Up @@ -624,6 +627,14 @@ def __eq__(self, other):
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")

Expand Down Expand Up @@ -658,6 +669,9 @@ def __eq__(self, other):
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")

Expand Down Expand Up @@ -691,6 +705,9 @@ def __eq__(self, other):
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")

Expand Down
45 changes: 45 additions & 0 deletions tests/x509/test_x509_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,13 @@ def test_ne(self):
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):
Expand Down Expand Up @@ -410,6 +417,15 @@ def test_ne(self):
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):
Expand Down Expand Up @@ -477,6 +493,19 @@ def test_ne(self):
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):
Expand Down Expand Up @@ -571,6 +600,22 @@ def test_long_oid(self, backend):

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)
Expand Down