test_decoder.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #
  2. # This file is part of pyasn1 software.
  3. #
  4. # Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com>
  5. # License: https://pyasn1.readthedocs.io/en/latest/license.html
  6. #
  7. import sys
  8. import unittest
  9. from __tests__.base import BaseTestCase
  10. from pyasn1.type import namedtype
  11. from pyasn1.type import univ
  12. from pyasn1.codec.native import decoder
  13. from pyasn1.error import PyAsn1Error
  14. class BadAsn1SpecTestCase(BaseTestCase):
  15. def testBadSpec(self):
  16. try:
  17. decoder.decode('', asn1Spec='not an Asn1Item')
  18. except PyAsn1Error:
  19. pass
  20. else:
  21. assert 0, 'Invalid asn1Spec accepted'
  22. class IntegerDecoderTestCase(BaseTestCase):
  23. def testPosInt(self):
  24. assert decoder.decode(12, asn1Spec=univ.Integer()) == univ.Integer(12)
  25. def testNegInt(self):
  26. assert decoder.decode(-12, asn1Spec=univ.Integer()) == univ.Integer(-12)
  27. class BooleanDecoderTestCase(BaseTestCase):
  28. def testTrue(self):
  29. assert decoder.decode(True, asn1Spec=univ.Boolean()) == univ.Boolean(True)
  30. def testTrueNeg(self):
  31. assert decoder.decode(False, asn1Spec=univ.Boolean()) == univ.Boolean(False)
  32. class BitStringDecoderTestCase(BaseTestCase):
  33. def testSimple(self):
  34. assert decoder.decode('11111111', asn1Spec=univ.BitString()) == univ.BitString(hexValue='ff')
  35. class OctetStringDecoderTestCase(BaseTestCase):
  36. def testSimple(self):
  37. assert decoder.decode('Quick brown fox', asn1Spec=univ.OctetString()) == univ.OctetString('Quick brown fox')
  38. class NullDecoderTestCase(BaseTestCase):
  39. def testNull(self):
  40. assert decoder.decode(None, asn1Spec=univ.Null()) == univ.Null('')
  41. class ObjectIdentifierDecoderTestCase(BaseTestCase):
  42. def testOne(self):
  43. assert decoder.decode('1.3.6.11', asn1Spec=univ.ObjectIdentifier()) == univ.ObjectIdentifier('1.3.6.11')
  44. class RealDecoderTestCase(BaseTestCase):
  45. def testSimple(self):
  46. assert decoder.decode(1.33, asn1Spec=univ.Real()) == univ.Real(1.33)
  47. class SequenceDecoderTestCase(BaseTestCase):
  48. def setUp(self):
  49. BaseTestCase.setUp(self)
  50. self.s = univ.Sequence(
  51. componentType=namedtype.NamedTypes(
  52. namedtype.NamedType('place-holder', univ.Null()),
  53. namedtype.NamedType('first-name', univ.OctetString()),
  54. namedtype.NamedType('age', univ.Integer(33))
  55. )
  56. )
  57. def testSimple(self):
  58. s = self.s.clone()
  59. s[0] = univ.Null('')
  60. s[1] = univ.OctetString('xx')
  61. s[2] = univ.Integer(33)
  62. assert decoder.decode({'place-holder': None, 'first-name': 'xx', 'age': 33}, asn1Spec=self.s) == s
  63. class ChoiceDecoderTestCase(BaseTestCase):
  64. def setUp(self):
  65. BaseTestCase.setUp(self)
  66. self.s = univ.Choice(
  67. componentType=namedtype.NamedTypes(
  68. namedtype.NamedType('place-holder', univ.Null()),
  69. namedtype.NamedType('first-name', univ.OctetString()),
  70. namedtype.NamedType('age', univ.Integer(33))
  71. )
  72. )
  73. def testSimple(self):
  74. s = self.s.clone()
  75. s[1] = univ.OctetString('xx')
  76. assert decoder.decode({'first-name': 'xx'}, asn1Spec=self.s) == s
  77. class AnyDecoderTestCase(BaseTestCase):
  78. def setUp(self):
  79. BaseTestCase.setUp(self)
  80. self.s = univ.Any()
  81. def testSimple(self):
  82. assert decoder.decode('fox', asn1Spec=univ.Any()) == univ.Any('fox')
  83. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  84. if __name__ == '__main__':
  85. unittest.TextTestRunner(verbosity=2).run(suite)