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# 7import sys 8 9from pyasn1.codec.der.decoder import decode as der_decode 10from pyasn1.codec.der.encoder import encode as der_encode 11 12from pyasn1_modules import pem 13from pyasn1_modules import rfc5280 14from pyasn1_modules import rfc8398 15 16try: 17 import unittest2 as unittest 18except ImportError: 19 import unittest 20 21 22class EAITestCase(unittest.TestCase): 23 pem_text = "oCAGCCsGAQUFBwgJoBQMEuiAgeW4q0BleGFtcGxlLmNvbQ==" 24 25 def setUp(self): 26 self.asn1Spec = rfc5280.GeneralName() 27 28 def testDerCodec(self): 29 substrate = pem.readBase64fromText(self.pem_text) 30 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 31 assert not rest 32 assert asn1Object.prettyPrint() 33 assert der_encode(asn1Object) == substrate 34 35 assert asn1Object['otherName']['type-id'] in rfc5280.anotherNameMap.keys() 36 assert asn1Object['otherName']['type-id'] == rfc8398.id_on_SmtpUTF8Mailbox 37 38 eai, rest = der_decode(asn1Object['otherName']['value'], 39 asn1Spec=rfc5280.anotherNameMap[asn1Object['otherName']['type-id']]) 40 assert not rest 41 assert eai.prettyPrint() 42 assert der_encode(eai) == asn1Object['otherName']['value'] 43 44 assert eai[0] == u'\u8001' 45 assert eai[1] == u'\u5E2B' 46 47 def testOpenTypes(self): 48 substrate = pem.readBase64fromText(self.pem_text) 49 asn1Object, rest = der_decode(substrate, 50 asn1Spec=self.asn1Spec, 51 decodeOpenTypes=True) 52 assert not rest 53 assert asn1Object.prettyPrint() 54 assert der_encode(asn1Object) == substrate 55 56 assert asn1Object['otherName']['type-id'] == rfc8398.id_on_SmtpUTF8Mailbox 57 assert asn1Object['otherName']['value'][0] == u'\u8001' 58 assert asn1Object['otherName']['value'][1] == u'\u5E2B' 59 60 61suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 62 63if __name__ == '__main__': 64 import sys 65 66 result = unittest.TextTestRunner(verbosity=2).run(suite) 67 sys.exit(not result.wasSuccessful()) 68