• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# This file is part of pyasn1-modules software.
3#
4# Copyright (c) 2005-2017, Ilya Etingof <etingof@gmail.com>
5# License: http://pyasn1.sf.net/license.html
6#
7import sys
8
9from pyasn1.codec.der import decoder as der_decoder
10from pyasn1.codec.der import encoder as der_encoder
11
12from pyasn1_modules import pem
13from pyasn1_modules import rfc5652
14from pyasn1_modules import rfc6402
15
16try:
17    import unittest2 as unittest
18
19except ImportError:
20    import unittest
21
22
23class ContentInfoTestCase(unittest.TestCase):
24    pem_text = """\
25MIIEJQYJKoZIhvcNAQcCoIIEFjCCBBICAQMxCzAJBgUrDgMCGgUAMIIDAgYIKwYBBQUHDAKgggL0
26BIIC8DCCAuwweDB2AgECBgorBgEEAYI3CgoBMWUwYwIBADADAgEBMVkwVwYJKwYBBAGCNxUUMUow
27SAIBBQwZcGl0dWNoYTEuZW1lYS5ocHFjb3JwLm5ldAwMRU1FQVxwaXR1Y2hhDBpDTUNSZXFHZW5l
28cmF0b3IudnNob3N0LmV4ZTCCAmqgggJmAgEBMIICXzCCAcgCAQAwADCBnzANBgkqhkiG9w0BAQEF
29AAOBjQAwgYkCgYEA0jm7SSSm2wyEAzuNKtFZFJKo91SrJq9wQwEhEKHDavZwMQOm1rZ2PF8NWCEb
30PqrhToQ7rtiGLSZa4dF4bzgmBqQ9aoSfEX4jISt31Vy+skHidXjHHpbsjT24NPhrZgANivL7CxD6
31Ft+s7qS1gL4HRm2twQkqSwOLrE/q2QeXl2UCAwEAAaCCAR0wGgYKKwYBBAGCNw0CAzEMFgo2LjIu
32OTIwMC4yMD4GCSqGSIb3DQEJDjExMC8wHQYDVR0OBBYEFMW2skn88gxhONWZQA4sWGBDb68yMA4G
33A1UdDwEB/wQEAwIHgDBXBgkrBgEEAYI3FRQxSjBIAgEFDBlwaXR1Y2hhMS5lbWVhLmhwcWNvcnAu
34bmV0DAxFTUVBXHBpdHVjaGEMGkNNQ1JlcUdlbmVyYXRvci52c2hvc3QuZXhlMGYGCisGAQQBgjcN
35AgIxWDBWAgECHk4ATQBpAGMAcgBvAHMAbwBmAHQAIABTAHQAcgBvAG4AZwAgAEMAcgB5AHAAdABv
36AGcAcgBhAHAAaABpAGMAIABQAHIAbwB2AGkAZABlAHIDAQAwDQYJKoZIhvcNAQEFBQADgYEAJZlu
37mxjtCxSOQi27jsVdd3y8NSIlzNv0b3LqmzvAly6L+CstXcnuG2MPQqPH9R7tbJonGUniBQO9sQ7C
38KhYWj2gfhiEkSID82lV5chINVUFKoUlSiEhWr0tPGgvOaqdsKQcrHfzrsBbFkhDqrFSVy7Yivbnh
39qYszKrOjJKiiCPMwADAAMYH5MIH2AgEDgBTFtrJJ/PIMYTjVmUAOLFhgQ2+vMjAJBgUrDgMCGgUA
40oD4wFwYJKoZIhvcNAQkDMQoGCCsGAQUFBwwCMCMGCSqGSIb3DQEJBDEWBBTFTkK/OifaFjwqHiJu
41xM7qXcg/VzANBgkqhkiG9w0BAQEFAASBgKfC6jOi1Wgy4xxDCQVK9+e5tktL8wE/j2cb9JSqq+aU
425UxEgXEw7q7BoYZCAzcxMRriGzakXr8aXHcgkRJ7XcFvLPUjpmGg9SOZ2sGW4zQdWAwImN/i8loc
43xicQmJP+VoMHo/ZpjFY9fYCjNZUArgKsEwK/s+p9yrVVeB1Nf8Mn
44"""
45
46    def setUp(self):
47        self.asn1Spec = rfc5652.ContentInfo()
48
49    def testDerCodec(self):
50
51        substrate = pem.readBase64fromText(self.pem_text)
52
53        layers = {
54            rfc5652.id_ct_contentInfo: rfc5652.ContentInfo(),
55            rfc5652.id_signedData: rfc5652.SignedData(),
56            rfc6402.id_cct_PKIData: rfc6402.PKIData()
57        }
58
59        getNextLayer = {
60            rfc5652.id_ct_contentInfo: lambda x: x['contentType'],
61            rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContentType'],
62            rfc6402.id_cct_PKIData: lambda x: None
63        }
64
65        getNextSubstrate = {
66            rfc5652.id_ct_contentInfo: lambda x: x['content'],
67            rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContent'],
68            rfc6402.id_cct_PKIData: lambda x: None
69        }
70
71
72        next_layer = rfc5652.id_ct_contentInfo
73
74        while next_layer:
75
76            asn1Object, rest = der_decoder.decode(
77                substrate, asn1Spec=layers[next_layer]
78            )
79
80            assert not rest
81            assert asn1Object.prettyPrint()
82            assert der_encoder.encode(asn1Object) == substrate
83
84            substrate = getNextSubstrate[next_layer](asn1Object)
85            next_layer = getNextLayer[next_layer](asn1Object)
86
87
88suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
89
90if __name__ == '__main__':
91    unittest.TextTestRunner(verbosity=2).run(suite)
92