Skip to content

Commit

Permalink
add __hash__ to PolicyConstraints and Extension (#3917)
Browse files Browse the repository at this point in the history
  • Loading branch information
reaperhulk authored and alex committed Sep 14, 2017
1 parent 7b6be92 commit 83bb406
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/cryptography/x509/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,11 @@ def __eq__(self, other):
def __ne__(self, other):
return not self == other

def __hash__(self):
return hash(
(self.require_explicit_policy, self.inhibit_policy_mapping)
)

require_explicit_policy = utils.read_only_property(
"_require_explicit_policy"
)
Expand Down Expand Up @@ -1063,6 +1068,9 @@ def __eq__(self, other):
def __ne__(self, other):
return not self == other

def __hash__(self):
return hash((self.oid, self.critical, self.value))


class GeneralNames(object):
def __init__(self, general_names):
Expand Down
26 changes: 26 additions & 0 deletions tests/x509/test_x509_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ def test_ne(self):
assert ext1 != ext4
assert ext1 != object()

def test_hash(self):
ext1 = x509.Extension(
ExtensionOID.BASIC_CONSTRAINTS,
False,
x509.BasicConstraints(ca=False, path_length=None)
)
ext2 = x509.Extension(
ExtensionOID.BASIC_CONSTRAINTS,
False,
x509.BasicConstraints(ca=False, path_length=None)
)
ext3 = x509.Extension(
ExtensionOID.BASIC_CONSTRAINTS,
False,
x509.BasicConstraints(ca=True, path_length=None)
)
assert hash(ext1) == hash(ext2)
assert hash(ext1) != hash(ext3)


class TestTLSFeature(object):
def test_not_enum_type(self):
Expand Down Expand Up @@ -2677,6 +2696,13 @@ def test_ne(self):
assert pc != pc3
assert pc != object()

def test_hash(self):
pc = x509.PolicyConstraints(2, 1)
pc2 = x509.PolicyConstraints(2, 1)
pc3 = x509.PolicyConstraints(2, None)
assert hash(pc) == hash(pc2)
assert hash(pc) != hash(pc3)


@pytest.mark.requires_backend_interface(interface=RSABackend)
@pytest.mark.requires_backend_interface(interface=X509Backend)
Expand Down

0 comments on commit 83bb406

Please sign in to comment.