test_rfc3537.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Created by Russ Housley
  5. # Copyright (c) 2019, Vigil Security, LLC
  6. # License: http://snmplabs.com/pyasn1/license.html
  7. #
  8. import sys
  9. import unittest
  10. from pyasn1.codec.der.decoder import decode as der_decoder
  11. from pyasn1.codec.der.encoder import encode as der_encoder
  12. from pyasn1.type import univ
  13. from pyasn1_modules import pem
  14. from pyasn1_modules import rfc3537
  15. from pyasn1_modules import rfc5751
  16. class SMIMECapabilitiesTestCase(unittest.TestCase):
  17. smime_capabilities_pem_text = "MCIwDwYLKoZIhvcNAQkQAwwFADAPBgsqhkiG9w0BCRADCwUA"
  18. def setUp(self):
  19. self.asn1Spec = rfc5751.SMIMECapabilities()
  20. def testDerCodec(self):
  21. alg_oid_list = [
  22. rfc3537.id_alg_HMACwithAESwrap,
  23. rfc3537.id_alg_HMACwith3DESwrap,
  24. ]
  25. substrate = pem.readBase64fromText(self.smime_capabilities_pem_text)
  26. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  27. self.assertFalse(rest)
  28. self.assertTrue(asn1Object.prettyPrint())
  29. self.assertEqual(substrate, der_encoder(asn1Object))
  30. count = 0
  31. for cap in asn1Object:
  32. self.assertEqual(der_encoder(univ.Null("")), cap['parameters'])
  33. self.assertTrue(cap['capabilityID'] in alg_oid_list)
  34. count += 1
  35. self.assertEqual(count, 2)
  36. def testOpenTypes(self):
  37. openTypesMap = {
  38. rfc3537.id_alg_HMACwithAESwrap: univ.Null(""),
  39. rfc3537.id_alg_HMACwith3DESwrap: univ.Null(""),
  40. }
  41. asn1Spec=rfc5751.SMIMECapabilities()
  42. substrate = pem.readBase64fromText(self.smime_capabilities_pem_text)
  43. asn1Object, rest = der_decoder(
  44. substrate, asn1Spec=self.asn1Spec,
  45. openTypes=openTypesMap, decodeOpenTypes=True)
  46. self.assertFalse(rest)
  47. self.assertTrue(asn1Object.prettyPrint())
  48. self.assertEqual(substrate, der_encoder(asn1Object))
  49. count = 0
  50. for cap in asn1Object:
  51. self.assertEqual(univ.Null(""), cap['parameters'])
  52. self.assertTrue(cap['capabilityID'] in openTypesMap.keys())
  53. count += 1
  54. self.assertEqual(count, 2)
  55. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  56. if __name__ == '__main__':
  57. result = unittest.TextTestRunner(verbosity=2).run(suite)
  58. sys.exit(not result.wasSuccessful())