test_rfc3274.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_modules import pem
  13. from pyasn1_modules import rfc3274
  14. from pyasn1_modules import rfc5652
  15. class CompressedDataTestCase(unittest.TestCase):
  16. compressed_data_pem_text = """\
  17. MIIB7wYLKoZIhvcNAQkQAQmgggHeMIIB2gIBADANBgsqhkiG9w0BCRADCDCCAcQG
  18. CSqGSIb3DQEHAaCCAbUEggGxeJxVksGO1DAQRO/+ir4xK4VlNSAhcUPRrgRiLgw/
  19. 0Il7Egu7bdntMOHraSezMJyixOWq19XpIwuxvP2xJvoEQld5lzw6Nub7Sw/vjx8/
  20. dJDq4F2ZyYJj+FqZ4Pj0dOzA0sUxFUC4xBxQ2gNqcTzBGEPKVApZY1EQsKn6vCaJ
  21. U8Y0uxFOeowTwXllwSsc+tP5Qe9tOCCK8wjQ32zUcvcZSDMIJCOX4PQgMqQcF2c3
  22. Dq5hoAzxAmgXVN+JSqfUo6+2YclMhrwLjlHaVRVutplsZYs8rvBL2WblqN7CTD4B
  23. MqAIjj8pd1ASUXMyNbXccWeDYd0sxlsGYIhVp3i1l6jgr3qtUeUehbIpQqnAoVSN
  24. 1IqKm7hZaI3EY2tLIR86RbD//ONCGb2HsPdnivvdqvrsZY51mlu+NjTjQhpKWz0p
  25. FvRlWw9ae7+fVgKKie0SeFpIZYemoyuG5HUS2QY6fTk9N6zz+dsuUyr9Xghs5Ddi
  26. 1LbZbVoNHDyFNv19jL7qiv9uuLK/XTD3Kqct1JS822vS8vWXpMzYBtal/083rMap
  27. XQ7u2qbaKFtZ7V96NH8ApkUFkg==
  28. """
  29. def setUp(self):
  30. self.asn1Spec = rfc5652.ContentInfo()
  31. def testDerCodec(self):
  32. substrate = pem.readBase64fromText(self.compressed_data_pem_text)
  33. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  34. self.assertFalse(rest)
  35. self.assertTrue(asn1Object.prettyPrint())
  36. self.assertEqual(substrate, der_encoder(asn1Object))
  37. self.assertEqual(rfc3274.id_ct_compressedData, asn1Object['contentType'])
  38. cd, rest = der_decoder(
  39. asn1Object['content'], asn1Spec=rfc3274.CompressedData())
  40. self.assertFalse(rest)
  41. self.assertTrue(cd.prettyPrint())
  42. self.assertEqual(asn1Object['content'], der_encoder(cd))
  43. self.assertEqual(rfc3274.id_alg_zlibCompress,
  44. cd['compressionAlgorithm']['algorithm'])
  45. self.assertEqual(rfc5652.id_data, cd['encapContentInfo']['eContentType'])
  46. def testOpenTypes(self):
  47. substrate = pem.readBase64fromText(self.compressed_data_pem_text)
  48. asn1Object, rest = der_decoder(substrate,
  49. asn1Spec=self.asn1Spec,
  50. decodeOpenTypes=True)
  51. self.assertFalse(rest)
  52. self.assertTrue(asn1Object.prettyPrint())
  53. self.assertEqual(substrate, der_encoder(asn1Object))
  54. self.assertEqual(
  55. rfc3274.id_ct_compressedData, asn1Object['contentType'])
  56. cd = asn1Object['content']
  57. self.assertEqual(rfc3274.id_alg_zlibCompress,
  58. cd['compressionAlgorithm']['algorithm'])
  59. self.assertEqual(rfc5652.id_data, cd['encapContentInfo']['eContentType'])
  60. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  61. if __name__ == '__main__':
  62. result = unittest.TextTestRunner(verbosity=2).run(suite)
  63. sys.exit(not result.wasSuccessful())