test_encoder.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 encoder
  13. from pyasn1.compat.octets import str2octs
  14. from pyasn1.error import PyAsn1Error
  15. class BadAsn1SpecTestCase(BaseTestCase):
  16. def testBadValueType(self):
  17. try:
  18. encoder.encode('not an Asn1Item')
  19. except PyAsn1Error:
  20. pass
  21. else:
  22. assert 0, 'Invalid value type accepted'
  23. class IntegerEncoderTestCase(BaseTestCase):
  24. def testPosInt(self):
  25. assert encoder.encode(univ.Integer(12)) == 12
  26. def testNegInt(self):
  27. assert encoder.encode(univ.Integer(-12)) == -12
  28. class BooleanEncoderTestCase(BaseTestCase):
  29. def testTrue(self):
  30. assert encoder.encode(univ.Boolean(1)) is True
  31. def testFalse(self):
  32. assert encoder.encode(univ.Boolean(0)) is False
  33. class BitStringEncoderTestCase(BaseTestCase):
  34. def setUp(self):
  35. BaseTestCase.setUp(self)
  36. self.b = univ.BitString((1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1))
  37. def testValue(self):
  38. assert encoder.encode(self.b) == '101010011000101'
  39. class OctetStringEncoderTestCase(BaseTestCase):
  40. def setUp(self):
  41. BaseTestCase.setUp(self)
  42. self.o = univ.OctetString('Quick brown fox')
  43. def testValue(self):
  44. assert encoder.encode(self.o) == str2octs('Quick brown fox')
  45. class NullEncoderTestCase(BaseTestCase):
  46. def testNull(self):
  47. assert encoder.encode(univ.Null('')) is None
  48. class ObjectIdentifierEncoderTestCase(BaseTestCase):
  49. def testOne(self):
  50. assert encoder.encode(univ.ObjectIdentifier((1, 3, 6, 0, 12345))) == '1.3.6.0.12345'
  51. class RealEncoderTestCase(BaseTestCase):
  52. def testChar(self):
  53. assert encoder.encode(univ.Real((123, 10, 11))) == 1.23e+13
  54. def testPlusInf(self):
  55. assert encoder.encode(univ.Real('inf')) == float('inf')
  56. def testMinusInf(self):
  57. assert encoder.encode(univ.Real('-inf')) == float('-inf')
  58. class SequenceEncoderTestCase(BaseTestCase):
  59. def setUp(self):
  60. BaseTestCase.setUp(self)
  61. self.s = univ.Sequence(componentType=namedtype.NamedTypes(
  62. namedtype.NamedType('place-holder', univ.Null('')),
  63. namedtype.OptionalNamedType('first-name', univ.OctetString('')),
  64. namedtype.DefaultedNamedType('age', univ.Integer(33)),
  65. ))
  66. def testSimple(self):
  67. s = self.s.clone()
  68. s[0] = univ.Null('')
  69. s[1] = 'abc'
  70. s[2] = 123
  71. assert encoder.encode(s) == {'place-holder': None, 'first-name': str2octs('abc'), 'age': 123}
  72. class ChoiceEncoderTestCase(BaseTestCase):
  73. def setUp(self):
  74. BaseTestCase.setUp(self)
  75. self.s = univ.Choice(
  76. componentType=namedtype.NamedTypes(
  77. namedtype.NamedType('place-holder', univ.Null('')),
  78. namedtype.NamedType('number', univ.Integer(0)),
  79. namedtype.NamedType('string', univ.OctetString())
  80. )
  81. )
  82. def testEmpty(self):
  83. try:
  84. encoder.encode(self.s)
  85. except PyAsn1Error:
  86. pass
  87. else:
  88. assert False, 'encoded unset choice'
  89. def testFilled(self):
  90. self.s.setComponentByPosition(0, univ.Null(''))
  91. assert encoder.encode(self.s) == {'place-holder': None}
  92. class AnyEncoderTestCase(BaseTestCase):
  93. def setUp(self):
  94. BaseTestCase.setUp(self)
  95. self.s = univ.Any(encoder.encode(univ.OctetString('fox')))
  96. def testSimple(self):
  97. assert encoder.encode(self.s) == str2octs('fox')
  98. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  99. if __name__ == '__main__':
  100. unittest.TextTestRunner(verbosity=2).run(suite)