test_decoder.py 12 KB

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