test_decoder.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 tag
  11. from pyasn1.type import namedtype
  12. from pyasn1.type import opentype
  13. from pyasn1.type import univ
  14. from pyasn1.codec.der import decoder
  15. from pyasn1.compat.octets import ints2octs, null
  16. from pyasn1.error import PyAsn1Error
  17. class BitStringDecoderTestCase(BaseTestCase):
  18. def testShortMode(self):
  19. assert decoder.decode(
  20. ints2octs((3, 127, 6) + (170,) * 125 + (128,))
  21. ) == (((1, 0) * 501), null)
  22. def testIndefMode(self):
  23. try:
  24. decoder.decode(
  25. ints2octs((35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0))
  26. )
  27. except PyAsn1Error:
  28. pass
  29. else:
  30. assert 0, 'indefinite length encoding tolerated'
  31. def testDefModeChunked(self):
  32. try:
  33. assert decoder.decode(
  34. ints2octs((35, 8, 3, 2, 0, 169, 3, 2, 1, 138))
  35. )
  36. except PyAsn1Error:
  37. pass
  38. else:
  39. assert 0, 'chunked encoding tolerated'
  40. class OctetStringDecoderTestCase(BaseTestCase):
  41. def testShortMode(self):
  42. assert decoder.decode(
  43. '\004\017Quick brown fox'.encode()
  44. ) == ('Quick brown fox'.encode(), ''.encode())
  45. def testIndefMode(self):
  46. try:
  47. decoder.decode(
  48. ints2octs((36, 128, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 0, 0))
  49. )
  50. except PyAsn1Error:
  51. pass
  52. else:
  53. assert 0, 'indefinite length encoding tolerated'
  54. def testChunkedMode(self):
  55. try:
  56. decoder.decode(
  57. ints2octs((36, 23, 4, 2, 81, 117, 4, 2, 105, 99, 4, 2, 107, 32, 4, 2, 98, 114, 4, 2, 111, 119, 4, 1, 110))
  58. )
  59. except PyAsn1Error:
  60. pass
  61. else:
  62. assert 0, 'chunked encoding tolerated'
  63. class SequenceDecoderWithUntaggedOpenTypesTestCase(BaseTestCase):
  64. def setUp(self):
  65. openType = opentype.OpenType(
  66. 'id',
  67. {1: univ.Integer(),
  68. 2: univ.OctetString()}
  69. )
  70. self.s = univ.Sequence(
  71. componentType=namedtype.NamedTypes(
  72. namedtype.NamedType('id', univ.Integer()),
  73. namedtype.NamedType('blob', univ.Any(), openType=openType)
  74. )
  75. )
  76. def testDecodeOpenTypesChoiceOne(self):
  77. s, r = decoder.decode(
  78. ints2octs((48, 6, 2, 1, 1, 2, 1, 12)), asn1Spec=self.s,
  79. decodeOpenTypes=True
  80. )
  81. assert not r
  82. assert s[0] == 1
  83. assert s[1] == 12
  84. def testDecodeOpenTypesChoiceTwo(self):
  85. s, r = decoder.decode(
  86. ints2octs((48, 16, 2, 1, 2, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s,
  87. decodeOpenTypes=True
  88. )
  89. assert not r
  90. assert s[0] == 2
  91. assert s[1] == univ.OctetString('quick brown')
  92. def testDecodeOpenTypesUnknownType(self):
  93. try:
  94. s, r = decoder.decode(
  95. ints2octs((48, 6, 2, 1, 2, 6, 1, 39)), asn1Spec=self.s,
  96. decodeOpenTypes=True
  97. )
  98. except PyAsn1Error:
  99. pass
  100. else:
  101. assert False, 'unknown open type tolerated'
  102. def testDecodeOpenTypesUnknownId(self):
  103. s, r = decoder.decode(
  104. ints2octs((48, 6, 2, 1, 3, 6, 1, 39)), asn1Spec=self.s,
  105. decodeOpenTypes=True
  106. )
  107. assert not r
  108. assert s[0] == 3
  109. assert s[1] == univ.OctetString(hexValue='060127')
  110. def testDontDecodeOpenTypesChoiceOne(self):
  111. s, r = decoder.decode(
  112. ints2octs((48, 6, 2, 1, 1, 2, 1, 12)), asn1Spec=self.s
  113. )
  114. assert not r
  115. assert s[0] == 1
  116. assert s[1] == ints2octs((2, 1, 12))
  117. def testDontDecodeOpenTypesChoiceTwo(self):
  118. s, r = decoder.decode(
  119. ints2octs((48, 16, 2, 1, 2, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
  120. )
  121. assert not r
  122. assert s[0] == 2
  123. assert s[1] == ints2octs((4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  124. class SequenceDecoderWithImplicitlyTaggedOpenTypesTestCase(BaseTestCase):
  125. def setUp(self):
  126. openType = opentype.OpenType(
  127. 'id',
  128. {1: univ.Integer(),
  129. 2: univ.OctetString()}
  130. )
  131. self.s = univ.Sequence(
  132. componentType=namedtype.NamedTypes(
  133. namedtype.NamedType('id', univ.Integer()),
  134. namedtype.NamedType(
  135. 'blob', univ.Any().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType
  136. )
  137. )
  138. )
  139. def testDecodeOpenTypesChoiceOne(self):
  140. s, r = decoder.decode(
  141. ints2octs((48, 8, 2, 1, 1, 131, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
  142. )
  143. assert not r
  144. assert s[0] == 1
  145. assert s[1] == 12
  146. def testDecodeOpenTypesUnknownId(self):
  147. s, r = decoder.decode(
  148. ints2octs((48, 8, 2, 1, 3, 131, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
  149. )
  150. assert not r
  151. assert s[0] == 3
  152. assert s[1] == univ.OctetString(hexValue='02010C')
  153. class SequenceDecoderWithExplicitlyTaggedOpenTypesTestCase(BaseTestCase):
  154. def setUp(self):
  155. openType = opentype.OpenType(
  156. 'id',
  157. {1: univ.Integer(),
  158. 2: univ.OctetString()}
  159. )
  160. self.s = univ.Sequence(
  161. componentType=namedtype.NamedTypes(
  162. namedtype.NamedType('id', univ.Integer()),
  163. namedtype.NamedType(
  164. 'blob', univ.Any().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType
  165. )
  166. )
  167. )
  168. def testDecodeOpenTypesChoiceOne(self):
  169. s, r = decoder.decode(
  170. ints2octs((48, 8, 2, 1, 1, 163, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
  171. )
  172. assert not r
  173. assert s[0] == 1
  174. assert s[1] == 12
  175. def testDecodeOpenTypesUnknownId(self):
  176. s, r = decoder.decode(
  177. ints2octs((48, 8, 2, 1, 3, 163, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
  178. )
  179. assert not r
  180. assert s[0] == 3
  181. assert s[1] == univ.OctetString(hexValue='02010C')
  182. class SequenceDecoderWithUnaggedSetOfOpenTypesTestCase(BaseTestCase):
  183. def setUp(self):
  184. openType = opentype.OpenType(
  185. 'id',
  186. {1: univ.Integer(),
  187. 2: univ.OctetString()}
  188. )
  189. self.s = univ.Sequence(
  190. componentType=namedtype.NamedTypes(
  191. namedtype.NamedType('id', univ.Integer()),
  192. namedtype.NamedType('blob', univ.SetOf(componentType=univ.Any()),
  193. openType=openType)
  194. )
  195. )
  196. def testDecodeOpenTypesChoiceOne(self):
  197. s, r = decoder.decode(
  198. ints2octs((48, 8, 2, 1, 1, 49, 3, 2, 1, 12)), asn1Spec=self.s,
  199. decodeOpenTypes=True
  200. )
  201. assert not r
  202. assert s[0] == 1
  203. assert s[1][0] == 12
  204. def testDecodeOpenTypesChoiceTwo(self):
  205. s, r = decoder.decode(
  206. ints2octs((48, 18, 2, 1, 2, 49, 13, 4, 11, 113, 117, 105, 99,
  207. 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s,
  208. decodeOpenTypes=True
  209. )
  210. assert not r
  211. assert s[0] == 2
  212. assert s[1][0] == univ.OctetString('quick brown')
  213. def testDecodeOpenTypesUnknownType(self):
  214. try:
  215. s, r = decoder.decode(
  216. ints2octs((48, 6, 2, 1, 2, 6, 1, 39)), asn1Spec=self.s,
  217. decodeOpenTypes=True
  218. )
  219. except PyAsn1Error:
  220. pass
  221. else:
  222. assert False, 'unknown open type tolerated'
  223. def testDecodeOpenTypesUnknownId(self):
  224. s, r = decoder.decode(
  225. ints2octs((48, 8, 2, 1, 3, 49, 3, 2, 1, 12)), asn1Spec=self.s,
  226. decodeOpenTypes=True
  227. )
  228. assert not r
  229. assert s[0] == 3
  230. assert s[1][0] == univ.OctetString(hexValue='02010c')
  231. def testDontDecodeOpenTypesChoiceOne(self):
  232. s, r = decoder.decode(
  233. ints2octs((48, 8, 2, 1, 1, 49, 3, 2, 1, 12)), asn1Spec=self.s
  234. )
  235. assert not r
  236. assert s[0] == 1
  237. assert s[1][0] == ints2octs((2, 1, 12))
  238. def testDontDecodeOpenTypesChoiceTwo(self):
  239. s, r = decoder.decode(
  240. ints2octs((48, 18, 2, 1, 2, 49, 13, 4, 11, 113, 117, 105, 99,
  241. 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
  242. )
  243. assert not r
  244. assert s[0] == 2
  245. assert s[1][0] == ints2octs((4, 11, 113, 117, 105, 99, 107, 32, 98, 114,
  246. 111, 119, 110))
  247. class SequenceDecoderWithImplicitlyTaggedSetOfOpenTypesTestCase(BaseTestCase):
  248. def setUp(self):
  249. openType = opentype.OpenType(
  250. 'id',
  251. {1: univ.Integer(),
  252. 2: univ.OctetString()}
  253. )
  254. self.s = univ.Sequence(
  255. componentType=namedtype.NamedTypes(
  256. namedtype.NamedType('id', univ.Integer()),
  257. namedtype.NamedType(
  258. 'blob', univ.SetOf(
  259. componentType=univ.Any().subtype(
  260. implicitTag=tag.Tag(
  261. tag.tagClassContext, tag.tagFormatSimple, 3))),
  262. openType=openType
  263. )
  264. )
  265. )
  266. def testDecodeOpenTypesChoiceOne(self):
  267. s, r = decoder.decode(
  268. ints2octs((48, 10, 2, 1, 1, 49, 5, 131, 3, 2, 1, 12)),
  269. asn1Spec=self.s, decodeOpenTypes=True
  270. )
  271. assert not r
  272. assert s[0] == 1
  273. assert s[1][0] == 12
  274. def testDecodeOpenTypesUnknownId(self):
  275. s, r = decoder.decode(
  276. ints2octs((48, 10, 2, 1, 3, 49, 5, 131, 3, 2, 1, 12)),
  277. asn1Spec=self.s, decodeOpenTypes=True
  278. )
  279. assert not r
  280. assert s[0] == 3
  281. assert s[1][0] == univ.OctetString(hexValue='02010C')
  282. class SequenceDecoderWithExplicitlyTaggedSetOfOpenTypesTestCase(BaseTestCase):
  283. def setUp(self):
  284. openType = opentype.OpenType(
  285. 'id',
  286. {1: univ.Integer(),
  287. 2: univ.OctetString()}
  288. )
  289. self.s = univ.Sequence(
  290. componentType=namedtype.NamedTypes(
  291. namedtype.NamedType('id', univ.Integer()),
  292. namedtype.NamedType(
  293. 'blob', univ.SetOf(
  294. componentType=univ.Any().subtype(
  295. explicitTag=tag.Tag(
  296. tag.tagClassContext, tag.tagFormatSimple, 3))),
  297. openType=openType
  298. )
  299. )
  300. )
  301. def testDecodeOpenTypesChoiceOne(self):
  302. s, r = decoder.decode(
  303. ints2octs((48, 10, 2, 1, 1, 49, 5, 131, 3, 2, 1, 12)),
  304. asn1Spec=self.s, decodeOpenTypes=True
  305. )
  306. assert not r
  307. assert s[0] == 1
  308. assert s[1][0] == 12
  309. def testDecodeOpenTypesUnknownId(self):
  310. s, r = decoder.decode(
  311. ints2octs( (48, 10, 2, 1, 3, 49, 5, 131, 3, 2, 1, 12)),
  312. asn1Spec=self.s, decodeOpenTypes=True
  313. )
  314. assert not r
  315. assert s[0] == 3
  316. assert s[1][0] == univ.OctetString(hexValue='02010C')
  317. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  318. if __name__ == '__main__':
  319. unittest.TextTestRunner(verbosity=2).run(suite)