From c3d38b5d80a955aee4b160bb97464a20c4992da7 Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Sat, 8 Dec 2018 03:26:07 +0200 Subject: Add RFC 4514 Distinguished Name formatting for Name, RDN and NameAttribute (#4304) --- tests/x509/test_x509.py | 72 +++++++++++++++++++++------------------------ tests/x509/test_x509_ext.py | 41 ++++++++------------------ 2 files changed, 46 insertions(+), 67 deletions(-) (limited to 'tests') diff --git a/tests/x509/test_x509.py b/tests/x509/test_x509.py index 15cfe43d..f4520811 100644 --- a/tests/x509/test_x509.py +++ b/tests/x509/test_x509.py @@ -1138,30 +1138,11 @@ class TestRSACertificate(object): x509.load_pem_x509_certificate, backend ) - if not six.PY2: - assert repr(cert) == ( - ", value='GT487" - "42965')>, , value='See www.rapidssl.com/re" - "sources/cps (c)14')>, , value='Domain Cont" - "rol Validated - RapidSSL(R)')>, , value='www.cryptograp" - "hy.io')>])>, ...)>" - ) - else: - assert repr(cert) == ( - ", value=u'GT48" - "742965')>, , value=u'See www.rapidssl.com/" - "resources/cps (c)14')>, , value=u'Domain C" - "ontrol Validated - RapidSSL(R)')>, , value=u'www.crypto" - "graphy.io')>])>, ...)>" - ) + assert repr(cert) == ( + ", ...)>" + ) def test_parse_tls_feature_extension(self, backend): cert = _load_cert( @@ -3933,6 +3914,18 @@ class TestNameAttribute(object): "nName)>, value=u'value')>" ) + def test_distinugished_name(self): + # Escaping + na = x509.NameAttribute(NameOID.COMMON_NAME, u'James "Jim" Smith, III') + assert na.rfc4514_string() == r'CN=James \"Jim\" Smith\, III' + na = x509.NameAttribute(NameOID.USER_ID, u'# escape+,;\0this ') + assert na.rfc4514_string() == r'UID=\# escape\+\,\;\00this\ ' + + # Nonstandard attribute OID + na = x509.NameAttribute(NameOID.EMAIL_ADDRESS, u'somebody@example.com') + assert (na.rfc4514_string() == + '1.2.840.113549.1.9.1=somebody@example.com') + class TestRelativeDistinguishedName(object): def test_init_empty(self): @@ -4120,20 +4113,23 @@ class TestName(object): x509.NameAttribute(NameOID.ORGANIZATION_NAME, u'PyCA'), ]) - if not six.PY2: - assert repr(name) == ( - ", value='cryptography.io')>, , valu" - "e='PyCA')>])>" - ) - else: - assert repr(name) == ( - ", value=u'cryptography.io')>, , val" - "ue=u'PyCA')>])>" - ) + assert repr(name) == "" + + def test_rfc4514_string(self): + n = x509.Name([ + x509.RelativeDistinguishedName([ + x509.NameAttribute(NameOID.ORGANIZATIONAL_UNIT_NAME, u'Sales'), + x509.NameAttribute(NameOID.COMMON_NAME, u'J. Smith'), + ]), + x509.RelativeDistinguishedName([ + x509.NameAttribute(NameOID.DOMAIN_COMPONENT, u'example'), + ]), + x509.RelativeDistinguishedName([ + x509.NameAttribute(NameOID.DOMAIN_COMPONENT, u'net'), + ]), + ]) + assert (n.rfc4514_string() == + 'OU=Sales+CN=J. Smith, DC=example, DC=net') def test_not_nameattribute(self): with pytest.raises(TypeError): diff --git a/tests/x509/test_x509_ext.py b/tests/x509/test_x509_ext.py index 152db964..6de105fa 100644 --- a/tests/x509/test_x509_ext.py +++ b/tests/x509/test_x509_ext.py @@ -1135,16 +1135,14 @@ class TestAuthorityKeyIdentifier(object): if not six.PY2: assert repr(aki) == ( ", value='myC" - "N')>])>)>], authority_cert_serial_number=1234)>" + "cert_issuer=[)>], author" + "ity_cert_serial_number=1234)>" ) else: assert repr(aki) == ( - ", value=u'myCN')" - ">])>)>], authority_cert_serial_number=1234)>" + ")>], author" + "ity_cert_serial_number=1234)>" ) def test_eq(self): @@ -1719,16 +1717,7 @@ class TestDirectoryName(object): def test_repr(self): name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, u'value1')]) gn = x509.DirectoryName(name) - if not six.PY2: - assert repr(gn) == ( - ", value='value1')>])>)>" - ) - else: - assert repr(gn) == ( - ", value=u'value1')>])>)>" - ) + assert repr(gn) == ")>" def test_eq(self): name = x509.Name([ @@ -3656,22 +3645,16 @@ class TestDistributionPoint(object): if not six.PY2: assert repr(dp) == ( ", value='myCN')>])>, reasons=frozenset(" - "{}), crl_issuer=[<" - "DirectoryName(value=, value='Important CA')>])>)" - ">])>" + "tinguishedName(CN=myCN)>, reasons=frozenset({}), crl_issuer=[)>])>" ) else: assert repr(dp) == ( ", value=u'myCN')>])>, reasons=frozenset" - "([]), crl_issuer=[" - ", value=u'Important CA')>])" - ">)>])>" + "tinguishedName(CN=myCN)>, reasons=frozenset([]), crl_issuer=[)>])>" ) def test_hash(self): -- cgit v1.2.3