Skip to content

Commit

Permalink
fix(admissions): allow profession_oids field being none (#11908)
Browse files Browse the repository at this point in the history
* fix: allow profession_oids to be none

Signed-off-by: oleg.hoefling <[email protected]>

* chore: provide explicit type hints for profession oids in hash calculation

Signed-off-by: oleg.hoefling <[email protected]>

* chore: remove unused ignore in profession info init test

Signed-off-by: oleg.hoefling <[email protected]>

* fix(profession info): simplify profession oids handling in hash calculation

Signed-off-by: oleg.hoefling <[email protected]>

---------

Signed-off-by: oleg.hoefling <[email protected]>
  • Loading branch information
hoefling authored Nov 6, 2024
1 parent 916fd46 commit 81d98f4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
27 changes: 16 additions & 11 deletions src/cryptography/x509/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2227,7 +2227,7 @@ def __init__(
self,
naming_authority: NamingAuthority | None,
profession_items: typing.Iterable[str],
profession_oids: typing.Iterable[ObjectIdentifier],
profession_oids: typing.Iterable[ObjectIdentifier] | None,
registration_number: str | None,
add_profession_info: bytes | None,
) -> None:
Expand All @@ -2242,14 +2242,15 @@ def __init__(
"Every item in the profession_items list must be a str"
)

profession_oids = list(profession_oids)
if not all(
isinstance(oid, ObjectIdentifier) for oid in profession_oids
):
raise TypeError(
"Every item in the profession_oids list must be an "
"ObjectIdentifier"
)
if profession_oids is not None:
profession_oids = list(profession_oids)
if not all(
isinstance(oid, ObjectIdentifier) for oid in profession_oids
):
raise TypeError(
"Every item in the profession_oids list must be an "
"ObjectIdentifier"
)

if registration_number is not None and not isinstance(
registration_number, str
Expand All @@ -2276,7 +2277,7 @@ def profession_items(self) -> list[str]:
return self._profession_items

@property
def profession_oids(self) -> list[ObjectIdentifier]:
def profession_oids(self) -> list[ObjectIdentifier] | None:
return self._profession_oids

@property
Expand Down Expand Up @@ -2309,11 +2310,15 @@ def __eq__(self, other: object) -> bool:
)

def __hash__(self) -> int:
if self.profession_oids is None:
profession_oids = None
else:
profession_oids = tuple(self.profession_oids)
return hash(
(
self.naming_authority,
*tuple(self.profession_items),
*tuple(self.profession_oids),
profession_oids,
self.registration_number,
self.add_profession_info,
)
Expand Down
24 changes: 23 additions & 1 deletion tests/x509/test_x509_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -6443,7 +6443,7 @@ def test_invalid_init(self):
x509.ProfessionInfo(
None,
None, # type:ignore[arg-type]
None, # type:ignore[arg-type]
None,
None,
None,
)
Expand Down Expand Up @@ -6493,6 +6493,10 @@ def test_eq(self):
info2 = x509.ProfessionInfo(None, [], [], None, None)
assert info1 == info2

info1 = x509.ProfessionInfo(None, [], None, None, None)
info2 = x509.ProfessionInfo(None, [], None, None, None)
assert info1 == info2

info1 = x509.ProfessionInfo(
x509.NamingAuthority(
x509.ObjectIdentifier("1.2.3"), "https://example.com", "spam"
Expand Down Expand Up @@ -6566,6 +6570,7 @@ def test_ne(self):
info8 = x509.ProfessionInfo(None, [], [], "spam", None)
info9 = x509.ProfessionInfo(None, [], [], None, b"\x01\x02\x03")
info10 = x509.ProfessionInfo(None, [], [], None, None)
info11 = x509.ProfessionInfo(None, [], None, None, None)

assert info1 != info2
assert info1 != info2
Expand All @@ -6577,6 +6582,7 @@ def test_ne(self):
assert info1 != info8
assert info1 != info9
assert info1 != info10
assert info1 != info11
assert info1 != object()

def test_repr(self):
Expand All @@ -6590,6 +6596,16 @@ def test_repr(self):
"add_profession_info=None)>"
)

info = x509.ProfessionInfo(None, [], None, None, None)
assert repr(info) == (
"<ProfessionInfo("
"naming_authority=None, "
"profession_items=[], "
"profession_oids=None, "
"registration_number=None, "
"add_profession_info=None)>"
)

info = x509.ProfessionInfo(
x509.NamingAuthority(
x509.ObjectIdentifier("1.2.3"), "https://example.com", "spam"
Expand Down Expand Up @@ -6659,13 +6675,19 @@ def test_hash(self):
info7 = x509.ProfessionInfo(
x509.NamingAuthority(None, None, None), [], [], None, None
)
info8 = x509.ProfessionInfo(
x509.NamingAuthority(None, None, None), [], None, None, None
)
info9 = x509.ProfessionInfo(None, [], None, None, None)

assert hash(info1) == hash(info2)
assert hash(info1) != hash(info3)
assert hash(info1) != hash(info4)
assert hash(info1) != hash(info5)
assert hash(info1) != hash(info6)
assert hash(info1) != hash(info7)
assert hash(info1) != hash(info8)
assert hash(info1) != hash(info9)


class TestAdmission:
Expand Down

0 comments on commit 81d98f4

Please sign in to comment.