1# 2# This file is part of pyasn1 software. 3# 4# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com> 5# License: http://snmplabs.com/pyasn1/license.html 6# 7import sys 8 9try: 10 import unittest2 as unittest 11 12except ImportError: 13 import unittest 14 15from tests.base import BaseTestCase 16 17from pyasn1.type import namedtype 18from pyasn1.type import univ 19from pyasn1.codec.native import decoder 20from pyasn1.error import PyAsn1Error 21 22 23class BadAsn1SpecTestCase(BaseTestCase): 24 def testBadSpec(self): 25 try: 26 decoder.decode('', asn1Spec='not an Asn1Item') 27 except PyAsn1Error: 28 pass 29 else: 30 assert 0, 'Invalid asn1Spec accepted' 31 32 33class IntegerDecoderTestCase(BaseTestCase): 34 def testPosInt(self): 35 assert decoder.decode(12, asn1Spec=univ.Integer()) == univ.Integer(12) 36 37 def testNegInt(self): 38 assert decoder.decode(-12, asn1Spec=univ.Integer()) == univ.Integer(-12) 39 40 41class BooleanDecoderTestCase(BaseTestCase): 42 def testTrue(self): 43 assert decoder.decode(True, asn1Spec=univ.Boolean()) == univ.Boolean(True) 44 45 def testTrueNeg(self): 46 assert decoder.decode(False, asn1Spec=univ.Boolean()) == univ.Boolean(False) 47 48 49class BitStringDecoderTestCase(BaseTestCase): 50 def testSimple(self): 51 assert decoder.decode('11111111', asn1Spec=univ.BitString()) == univ.BitString(hexValue='ff') 52 53 54class OctetStringDecoderTestCase(BaseTestCase): 55 def testSimple(self): 56 assert decoder.decode('Quick brown fox', asn1Spec=univ.OctetString()) == univ.OctetString('Quick brown fox') 57 58 59class NullDecoderTestCase(BaseTestCase): 60 def testNull(self): 61 assert decoder.decode(None, asn1Spec=univ.Null()) == univ.Null('') 62 63 64class ObjectIdentifierDecoderTestCase(BaseTestCase): 65 def testOne(self): 66 assert decoder.decode('1.3.6.11', asn1Spec=univ.ObjectIdentifier()) == univ.ObjectIdentifier('1.3.6.11') 67 68 69class RealDecoderTestCase(BaseTestCase): 70 def testSimple(self): 71 assert decoder.decode(1.33, asn1Spec=univ.Real()) == univ.Real(1.33) 72 73 74class SequenceDecoderTestCase(BaseTestCase): 75 def setUp(self): 76 BaseTestCase.setUp(self) 77 78 self.s = univ.Sequence( 79 componentType=namedtype.NamedTypes( 80 namedtype.NamedType('place-holder', univ.Null()), 81 namedtype.NamedType('first-name', univ.OctetString()), 82 namedtype.NamedType('age', univ.Integer(33)) 83 ) 84 ) 85 86 def testSimple(self): 87 s = self.s.clone() 88 s[0] = univ.Null('') 89 s[1] = univ.OctetString('xx') 90 s[2] = univ.Integer(33) 91 assert decoder.decode({'place-holder': None, 'first-name': 'xx', 'age': 33}, asn1Spec=self.s) == s 92 93 94class ChoiceDecoderTestCase(BaseTestCase): 95 def setUp(self): 96 BaseTestCase.setUp(self) 97 98 self.s = univ.Choice( 99 componentType=namedtype.NamedTypes( 100 namedtype.NamedType('place-holder', univ.Null()), 101 namedtype.NamedType('first-name', univ.OctetString()), 102 namedtype.NamedType('age', univ.Integer(33)) 103 ) 104 ) 105 106 def testSimple(self): 107 s = self.s.clone() 108 s[1] = univ.OctetString('xx') 109 assert decoder.decode({'first-name': 'xx'}, asn1Spec=self.s) == s 110 111 112class AnyDecoderTestCase(BaseTestCase): 113 def setUp(self): 114 BaseTestCase.setUp(self) 115 116 self.s = univ.Any() 117 118 def testSimple(self): 119 assert decoder.decode('fox', asn1Spec=univ.Any()) == univ.Any('fox') 120 121 122suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 123 124if __name__ == '__main__': 125 unittest.TextTestRunner(verbosity=2).run(suite) 126