#
# This file is part of pyasn1-modules software.
#
# Copyright (c) 2005-2020, <NAME> <<EMAIL>>
# License: http://snmplabs.com/pyasn1/license.html
#
import sys
import unittest

from pyasn1.codec.der.decoder import decode as der_decoder
from pyasn1.codec.der.encoder import encode as der_encoder

from pyasn1_modules import pem
from pyasn1_modules import rfc5280
from pyasn1_modules import rfc3279
from pyasn1_modules import rfc4210

from pyasn1.type import univ


class PKIMessageTestCase(unittest.TestCase):
    pem_text = """\
<KEY>
"""

    def setUp(self):
        self.asn1Spec = rfc4210.PKIMessage()

    def testDerCodec(self):
        substrate = pem.readBase64fromText(self.pem_text)
        asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))

    def testOpenTypes(self):
        substrate = pem.readBase64fromText(self.pem_text)
        asn1Object, rest = der_decoder(substrate, 
                                       asn1Spec=self.asn1Spec,
                                       decodeOpenTypes=True)
        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))

class PKIMessageGenPTestCase(unittest.TestCase):
    pem_text = """\
<KEY>FIED<KEY>
"""

    def setUp(self):
        self.asn1Spec = rfc4210.PKIMessage()

    def testDerCodec(self):
        substrate = pem.readBase64fromText(self.pem_text)
        asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))

        found = False
        for gpc in asn1Object['body']['gen']:
            self.assertIn(gpc['infoType'], rfc4210.infoTypeAndValueMap)
            spec = rfc4210.infoTypeAndValueMap[gpc['infoType']]
            gpcv, rest = der_decoder(gpc['infoValue'], asn1Spec=spec)
            self.assertFalse(rest)
            self.assertTrue(gpcv.prettyPrint())
            self.assertEqual(gpc['infoValue'], der_encoder(gpcv))
            if gpc['infoType'] == rfc4210.id_it_preferredSymmAlg:
                ede3_oid = univ.ObjectIdentifier('1.2.840.113549.3.7')
                self.assertEqual(ede3_oid, gpcv['algorithm'])
                found = True

        self.assertTrue(found)

    def testOpenTypes(self):
        ede3_oid = univ.ObjectIdentifier('1.2.840.113549.3.7')
        openTypesMap = rfc4210.infoTypeAndValueMap.copy()
        # To handle unexpected NULL parameter values
        openTypesMap.update(rfc5280.algorithmIdentifierMap)
        openTypesMap.update({rfc3279.id_ecPublicKey: rfc3279.EcpkParameters()})
        openTypesMap.update({ede3_oid: univ.Null()})
        substrate = pem.readBase64fromText(self.pem_text)
        asn1Object, rest = der_decoder(substrate, 
                                       asn1Spec=self.asn1Spec,
                                       openTypes=openTypesMap,
                                       decodeOpenTypes=True)
        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))

        found = False
        for gpc in asn1Object['body']['gen']:
            if gpc['infoType'] == rfc4210.id_it_preferredSymmAlg:
                self.assertEqual(ede3_oid, gpc['infoValue']['algorithm'])
                found = True

        assert found


suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])

if __name__ == '__main__':
    result = unittest.TextTestRunner(verbosity=2).run(suite)
    sys.exit(not result.wasSuccessful())

#
# This file is part of pyasn1-modules software.
#
# Copyright (c) 2005-2020, <NAME> <<EMAIL>>
# License: http://snmplabs.com/pyasn1/license.html
#
import sys
import unittest

from pyasn1.codec.der.decoder import decode as der_decoder
from pyasn1.codec.der.encoder import encode as der_encoder

from pyasn1_modules import pem
from pyasn1_modules import rfc5280
from pyasn1_modules import rfc3279
from pyasn1_modules import rfc4210

from pyasn1.type import univ


class PKIMessageTestCase(unittest.TestCase):
    pem_text = """\
<KEY>
"""

    def setUp(self):
        self.asn1Spec = rfc4210.PKIMessage()

    def testDerCodec(self):
        substrate = pem.readBase64fromText(self.pem_text)
        asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))

    def testOpenTypes(self):
        substrate = pem.readBase64fromText(self.pem_text)
        asn1Object, rest = der_decoder(substrate, 
                                       asn1Spec=self.asn1Spec,
                                       decodeOpenTypes=True)
        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))

class PKIMessageGenPTestCase(unittest.TestCase):
    pem_text = """\
<KEY>FIED<KEY>
"""

    def setUp(self):
        self.asn1Spec = rfc4210.PKIMessage()

    def testDerCodec(self):
        substrate = pem.readBase64fromText(self.pem_text)
        asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))

        found = False
        for gpc in asn1Object['body']['gen']:
            self.assertIn(gpc['infoType'], rfc4210.infoTypeAndValueMap)
            spec = rfc4210.infoTypeAndValueMap[gpc['infoType']]
            gpcv, rest = der_decoder(gpc['infoValue'], asn1Spec=spec)
            self.assertFalse(rest)
            self.assertTrue(gpcv.prettyPrint())
            self.assertEqual(gpc['infoValue'], der_encoder(gpcv))
            if gpc['infoType'] == rfc4210.id_it_preferredSymmAlg:
                ede3_oid = univ.ObjectIdentifier('1.2.840.113549.3.7')
                self.assertEqual(ede3_oid, gpcv['algorithm'])
                found = True

        self.assertTrue(found)

    def testOpenTypes(self):
        ede3_oid = univ.ObjectIdentifier('1.2.840.113549.3.7')
        openTypesMap = rfc4210.infoTypeAndValueMap.copy()
        # To handle unexpected NULL parameter values
        openTypesMap.update(rfc5280.algorithmIdentifierMap)
        openTypesMap.update({rfc3279.id_ecPublicKey: rfc3279.EcpkParameters()})
        openTypesMap.update({ede3_oid: univ.Null()})
        substrate = pem.readBase64fromText(self.pem_text)
        asn1Object, rest = der_decoder(substrate, 
                                       asn1Spec=self.asn1Spec,
                                       openTypes=openTypesMap,
                                       decodeOpenTypes=True)
        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))

        found = False
        for gpc in asn1Object['body']['gen']:
            if gpc['infoType'] == rfc4210.id_it_preferredSymmAlg:
                self.assertEqual(ede3_oid, gpc['infoValue']['algorithm'])
                found = True

        assert found


suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])

if __name__ == '__main__':
    result = unittest.TextTestRunner(verbosity=2).run(suite)
    sys.exit(not result.wasSuccessful())

# # This file is part of pyasn1-modules software. # # Copyright (c) 2005-2020, <NAME> <<EMAIL>> # License: http://snmplabs.com/pyasn1/license.html # \ <KEY> \ <KEY>FIED<KEY> # To handle unexpected NULL parameter values