test_rfc8398.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Copyright (c) 2019, Vigil Security, LLC
  5. # License: http://snmplabs.com/pyasn1/license.html
  6. #
  7. import sys
  8. import unittest
  9. from pyasn1.codec.der.decoder import decode as der_decoder
  10. from pyasn1.codec.der.encoder import encode as der_encoder
  11. from pyasn1_modules import pem
  12. from pyasn1_modules import rfc5280
  13. from pyasn1_modules import rfc8398
  14. class EAITestCase(unittest.TestCase):
  15. pem_text = "oCAGCCsGAQUFBwgJoBQMEuiAgeW4q0BleGFtcGxlLmNvbQ=="
  16. def setUp(self):
  17. self.asn1Spec = rfc5280.GeneralName()
  18. def testDerCodec(self):
  19. substrate = pem.readBase64fromText(self.pem_text)
  20. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  21. self.assertFalse(rest)
  22. self.assertTrue(asn1Object.prettyPrint())
  23. self.assertEqual(substrate, der_encoder(asn1Object))
  24. self.assertIn(asn1Object['otherName']['type-id'],
  25. rfc5280.anotherNameMap)
  26. self.assertEqual(rfc8398.id_on_SmtpUTF8Mailbox,
  27. asn1Object['otherName']['type-id'])
  28. eai, rest = der_decoder(
  29. asn1Object['otherName']['value'],
  30. asn1Spec=rfc5280.anotherNameMap[asn1Object['otherName']['type-id']])
  31. self.assertFalse(rest)
  32. self.assertTrue(eai.prettyPrint())
  33. self.assertEqual(asn1Object['otherName']['value'], der_encoder(eai))
  34. self.assertEqual(u'\u8001', eai[0])
  35. self.assertEqual(u'\u5E2B', eai[1])
  36. def testOpenTypes(self):
  37. substrate = pem.readBase64fromText(self.pem_text)
  38. asn1Object, rest = der_decoder(
  39. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  40. self.assertFalse(rest)
  41. self.assertTrue(asn1Object.prettyPrint())
  42. self.assertEqual(substrate, der_encoder(asn1Object))
  43. self.assertEqual(
  44. rfc8398.id_on_SmtpUTF8Mailbox, asn1Object['otherName']['type-id'])
  45. self.assertEqual(u'\u8001', asn1Object['otherName']['value'][0])
  46. self.assertEqual(u'\u5E2B', asn1Object['otherName']['value'][1])
  47. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  48. if __name__ == '__main__':
  49. result = unittest.TextTestRunner(verbosity=2).run(suite)
  50. sys.exit(not result.wasSuccessful())