test_decoder.py 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041
  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 gzip
  8. import io
  9. import os
  10. import sys
  11. import tempfile
  12. import unittest
  13. import zipfile
  14. from __tests__.base import BaseTestCase
  15. from pyasn1.type import tag
  16. from pyasn1.type import namedtype
  17. from pyasn1.type import opentype
  18. from pyasn1.type import univ
  19. from pyasn1.type import char
  20. from pyasn1.codec import streaming
  21. from pyasn1.codec.ber import decoder
  22. from pyasn1.codec.ber import eoo
  23. from pyasn1.compat.octets import ints2octs, str2octs, null
  24. from pyasn1 import error
  25. class LargeTagDecoderTestCase(BaseTestCase):
  26. def testLargeTag(self):
  27. assert decoder.decode(ints2octs((127, 141, 245, 182, 253, 47, 3, 2, 1, 1))) == (1, null)
  28. def testLongTag(self):
  29. assert decoder.decode(ints2octs((0x1f, 2, 1, 0)))[0].tagSet == univ.Integer.tagSet
  30. def testTagsEquivalence(self):
  31. integer = univ.Integer(2).subtype(implicitTag=tag.Tag(tag.tagClassContext, 0, 0))
  32. assert decoder.decode(ints2octs((0x9f, 0x80, 0x00, 0x02, 0x01, 0x02)), asn1Spec=integer) == decoder.decode(
  33. ints2octs((0x9f, 0x00, 0x02, 0x01, 0x02)), asn1Spec=integer)
  34. class DecoderCacheTestCase(BaseTestCase):
  35. def testCache(self):
  36. assert decoder.decode(ints2octs((0x1f, 2, 1, 0))) == decoder.decode(ints2octs((0x1f, 2, 1, 0)))
  37. class IntegerDecoderTestCase(BaseTestCase):
  38. def testPosInt(self):
  39. assert decoder.decode(ints2octs((2, 1, 12))) == (12, null)
  40. def testNegInt(self):
  41. assert decoder.decode(ints2octs((2, 1, 244))) == (-12, null)
  42. def testZero(self):
  43. assert decoder.decode(ints2octs((2, 0))) == (0, null)
  44. def testZeroLong(self):
  45. assert decoder.decode(ints2octs((2, 1, 0))) == (0, null)
  46. def testMinusOne(self):
  47. assert decoder.decode(ints2octs((2, 1, 255))) == (-1, null)
  48. def testPosLong(self):
  49. assert decoder.decode(
  50. ints2octs((2, 9, 0, 255, 255, 255, 255, 255, 255, 255, 255))
  51. ) == (0xffffffffffffffff, null)
  52. def testNegLong(self):
  53. assert decoder.decode(
  54. ints2octs((2, 9, 255, 0, 0, 0, 0, 0, 0, 0, 1))
  55. ) == (-0xffffffffffffffff, null)
  56. def testSpec(self):
  57. try:
  58. decoder.decode(
  59. ints2octs((2, 1, 12)), asn1Spec=univ.Null()
  60. ) == (12, null)
  61. except error.PyAsn1Error:
  62. pass
  63. else:
  64. assert 0, 'wrong asn1Spec worked out'
  65. assert decoder.decode(
  66. ints2octs((2, 1, 12)), asn1Spec=univ.Integer()
  67. ) == (12, null)
  68. def testTagFormat(self):
  69. try:
  70. decoder.decode(ints2octs((34, 1, 12)))
  71. except error.PyAsn1Error:
  72. pass
  73. else:
  74. assert 0, 'wrong tagFormat worked out'
  75. class BooleanDecoderTestCase(BaseTestCase):
  76. def testTrue(self):
  77. assert decoder.decode(ints2octs((1, 1, 1))) == (1, null)
  78. def testTrueNeg(self):
  79. assert decoder.decode(ints2octs((1, 1, 255))) == (1, null)
  80. def testExtraTrue(self):
  81. assert decoder.decode(ints2octs((1, 1, 1, 0, 120, 50, 50))) == (1, ints2octs((0, 120, 50, 50)))
  82. def testFalse(self):
  83. assert decoder.decode(ints2octs((1, 1, 0))) == (0, null)
  84. def testTagFormat(self):
  85. try:
  86. decoder.decode(ints2octs((33, 1, 1)))
  87. except error.PyAsn1Error:
  88. pass
  89. else:
  90. assert 0, 'wrong tagFormat worked out'
  91. class BitStringDecoderTestCase(BaseTestCase):
  92. def testDefMode(self):
  93. assert decoder.decode(
  94. ints2octs((3, 3, 1, 169, 138))
  95. ) == ((1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1), null)
  96. def testIndefMode(self):
  97. assert decoder.decode(
  98. ints2octs((3, 3, 1, 169, 138))
  99. ) == ((1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1), null)
  100. def testDefModeChunked(self):
  101. assert decoder.decode(
  102. ints2octs((35, 8, 3, 2, 0, 169, 3, 2, 1, 138))
  103. ) == ((1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1), null)
  104. def testIndefModeChunked(self):
  105. assert decoder.decode(
  106. ints2octs((35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0))
  107. ) == ((1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1), null)
  108. def testDefModeChunkedSubst(self):
  109. assert decoder.decode(
  110. ints2octs((35, 8, 3, 2, 0, 169, 3, 2, 1, 138)),
  111. substrateFun=lambda a, b, c, d: streaming.readFromStream(b, c)
  112. ) == (ints2octs((3, 2, 0, 169, 3, 2, 1, 138)), str2octs(''))
  113. def testDefModeChunkedSubstV04(self):
  114. assert decoder.decode(
  115. ints2octs((35, 8, 3, 2, 0, 169, 3, 2, 1, 138)),
  116. substrateFun=lambda a, b, c: (b, b[c:])
  117. ) == (ints2octs((3, 2, 0, 169, 3, 2, 1, 138)), str2octs(''))
  118. def testIndefModeChunkedSubst(self):
  119. assert decoder.decode(
  120. ints2octs((35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)),
  121. substrateFun=lambda a, b, c, d: streaming.readFromStream(b, c)
  122. ) == (ints2octs((3, 2, 0, 169, 3, 2, 1, 138, 0, 0)), str2octs(''))
  123. def testIndefModeChunkedSubstV04(self):
  124. assert decoder.decode(
  125. ints2octs((35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)),
  126. substrateFun=lambda a, b, c: (b, b[c:])
  127. ) == (ints2octs((3, 2, 0, 169, 3, 2, 1, 138, 0, 0)), str2octs(''))
  128. def testTypeChecking(self):
  129. try:
  130. decoder.decode(ints2octs((35, 4, 2, 2, 42, 42)))
  131. except error.PyAsn1Error:
  132. pass
  133. else:
  134. assert 0, 'accepted mis-encoded bit-string constructed out of an integer'
  135. class OctetStringDecoderTestCase(BaseTestCase):
  136. def testDefMode(self):
  137. assert decoder.decode(
  138. ints2octs((4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120))
  139. ) == (str2octs('Quick brown fox'), null)
  140. def testIndefMode(self):
  141. assert decoder.decode(
  142. ints2octs((36, 128, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 0, 0))
  143. ) == (str2octs('Quick brown fox'), null)
  144. def testDefModeChunked(self):
  145. assert decoder.decode(
  146. ints2octs(
  147. (36, 23, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120))
  148. ) == (str2octs('Quick brown fox'), null)
  149. def testIndefModeChunked(self):
  150. assert decoder.decode(
  151. ints2octs((36, 128, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120, 0, 0))
  152. ) == (str2octs('Quick brown fox'), null)
  153. def testDefModeChunkedSubst(self):
  154. assert decoder.decode(
  155. ints2octs(
  156. (36, 23, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120)),
  157. substrateFun=lambda a, b, c, d: streaming.readFromStream(b, c)
  158. ) == (ints2octs((4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120)), str2octs(''))
  159. def testDefModeChunkedSubstV04(self):
  160. assert decoder.decode(
  161. ints2octs(
  162. (36, 23, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120)),
  163. substrateFun=lambda a, b, c: (b, b[c:])
  164. ) == (ints2octs((4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120)), str2octs(''))
  165. def testIndefModeChunkedSubst(self):
  166. assert decoder.decode(
  167. ints2octs((36, 128, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111,
  168. 120, 0, 0)),
  169. substrateFun=lambda a, b, c, d: streaming.readFromStream(b, c)
  170. ) == (ints2octs(
  171. (4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120, 0, 0)), str2octs(''))
  172. def testIndefModeChunkedSubstV04(self):
  173. assert decoder.decode(
  174. ints2octs((36, 128, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111,
  175. 120, 0, 0)),
  176. substrateFun=lambda a, b, c: (b, b[c:])
  177. ) == (ints2octs(
  178. (4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120, 0, 0)), str2octs(''))
  179. class ExpTaggedOctetStringDecoderTestCase(BaseTestCase):
  180. def setUp(self):
  181. BaseTestCase.setUp(self)
  182. self.o = univ.OctetString(
  183. 'Quick brown fox',
  184. tagSet=univ.OctetString.tagSet.tagExplicitly(
  185. tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 5)
  186. ))
  187. def testDefMode(self):
  188. o, r = decoder.decode(
  189. ints2octs((101, 17, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120))
  190. )
  191. assert not r
  192. assert self.o == o
  193. assert self.o.tagSet == o.tagSet
  194. assert self.o.isSameTypeWith(o)
  195. def testIndefMode(self):
  196. o, r = decoder.decode(
  197. ints2octs((101, 128, 36, 128, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 0, 0, 0, 0))
  198. )
  199. assert not r
  200. assert self.o == o
  201. assert self.o.tagSet == o.tagSet
  202. assert self.o.isSameTypeWith(o)
  203. def testDefModeChunked(self):
  204. o, r = decoder.decode(
  205. ints2octs((101, 25, 36, 23, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120))
  206. )
  207. assert not r
  208. assert self.o == o
  209. assert self.o.tagSet == o.tagSet
  210. assert self.o.isSameTypeWith(o)
  211. def testIndefModeChunked(self):
  212. o, r = decoder.decode(
  213. ints2octs((101, 128, 36, 128, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120, 0, 0, 0, 0))
  214. )
  215. assert not r
  216. assert self.o == o
  217. assert self.o.tagSet == o.tagSet
  218. assert self.o.isSameTypeWith(o)
  219. def testDefModeSubst(self):
  220. assert decoder.decode(
  221. ints2octs((101, 17, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120)),
  222. substrateFun=lambda a, b, c, d: streaming.readFromStream(b, c)
  223. ) == (ints2octs((4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120)), str2octs(''))
  224. def testDefModeSubstV04(self):
  225. assert decoder.decode(
  226. ints2octs((101, 17, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120)),
  227. substrateFun=lambda a, b, c: (b, b[c:])
  228. ) == (ints2octs((4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120)), str2octs(''))
  229. def testIndefModeSubst(self):
  230. assert decoder.decode(
  231. ints2octs((
  232. 101, 128, 36, 128, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 0,
  233. 0, 0, 0)),
  234. substrateFun=lambda a, b, c, d: streaming.readFromStream(b, c)
  235. ) == (ints2octs(
  236. (36, 128, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 0, 0, 0, 0)), str2octs(''))
  237. def testIndefModeSubstV04(self):
  238. assert decoder.decode(
  239. ints2octs((
  240. 101, 128, 36, 128, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 0,
  241. 0, 0, 0)),
  242. substrateFun=lambda a, b, c: (b, b[c:])
  243. ) == (ints2octs(
  244. (36, 128, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 0, 0, 0, 0)), str2octs(''))
  245. class NullDecoderTestCase(BaseTestCase):
  246. def testNull(self):
  247. assert decoder.decode(ints2octs((5, 0))) == (null, null)
  248. def testTagFormat(self):
  249. try:
  250. decoder.decode(ints2octs((37, 0)))
  251. except error.PyAsn1Error:
  252. pass
  253. else:
  254. assert 0, 'wrong tagFormat worked out'
  255. # Useful analysis of OID encoding issues could be found here:
  256. # https://misc.daniel-marschall.de/asn.1/oid_facts.html
  257. class ObjectIdentifierDecoderTestCase(BaseTestCase):
  258. def testOne(self):
  259. assert decoder.decode(
  260. ints2octs((6, 6, 43, 6, 0, 191, 255, 126))
  261. ) == ((1, 3, 6, 0, 0xffffe), null)
  262. def testEdge1(self):
  263. assert decoder.decode(
  264. ints2octs((6, 1, 39))
  265. ) == ((0, 39), null)
  266. def testEdge2(self):
  267. assert decoder.decode(
  268. ints2octs((6, 1, 79))
  269. ) == ((1, 39), null)
  270. def testEdge3(self):
  271. assert decoder.decode(
  272. ints2octs((6, 1, 120))
  273. ) == ((2, 40), null)
  274. def testEdge4(self):
  275. assert decoder.decode(
  276. ints2octs((6, 5, 0x90, 0x80, 0x80, 0x80, 0x4F))
  277. ) == ((2, 0xffffffff), null)
  278. def testEdge5(self):
  279. assert decoder.decode(
  280. ints2octs((6, 1, 0x7F))
  281. ) == ((2, 47), null)
  282. def testEdge6(self):
  283. assert decoder.decode(
  284. ints2octs((6, 2, 0x81, 0x00))
  285. ) == ((2, 48), null)
  286. def testEdge7(self):
  287. assert decoder.decode(
  288. ints2octs((6, 3, 0x81, 0x34, 0x03))
  289. ) == ((2, 100, 3), null)
  290. def testEdge8(self):
  291. assert decoder.decode(
  292. ints2octs((6, 2, 133, 0))
  293. ) == ((2, 560), null)
  294. def testEdge9(self):
  295. assert decoder.decode(
  296. ints2octs((6, 4, 0x88, 0x84, 0x87, 0x02))
  297. ) == ((2, 16843570), null)
  298. def testNonLeading0x80(self):
  299. assert decoder.decode(
  300. ints2octs((6, 5, 85, 4, 129, 128, 0)),
  301. ) == ((2, 5, 4, 16384), null)
  302. def testLeading0x80Case1(self):
  303. try:
  304. decoder.decode(
  305. ints2octs((6, 5, 85, 4, 128, 129, 0))
  306. )
  307. except error.PyAsn1Error:
  308. pass
  309. else:
  310. assert 0, 'Leading 0x80 tolerated'
  311. def testLeading0x80Case2(self):
  312. try:
  313. decoder.decode(
  314. ints2octs((6, 7, 1, 0x80, 0x80, 0x80, 0x80, 0x80, 0x7F))
  315. )
  316. except error.PyAsn1Error:
  317. pass
  318. else:
  319. assert 0, 'Leading 0x80 tolerated'
  320. def testLeading0x80Case3(self):
  321. try:
  322. decoder.decode(
  323. ints2octs((6, 2, 0x80, 1))
  324. )
  325. except error.PyAsn1Error:
  326. pass
  327. else:
  328. assert 0, 'Leading 0x80 tolerated'
  329. def testLeading0x80Case4(self):
  330. try:
  331. decoder.decode(
  332. ints2octs((6, 2, 0x80, 0x7F))
  333. )
  334. except error.PyAsn1Error:
  335. pass
  336. else:
  337. assert 0, 'Leading 0x80 tolerated'
  338. def testTagFormat(self):
  339. try:
  340. decoder.decode(ints2octs((38, 1, 239)))
  341. except error.PyAsn1Error:
  342. pass
  343. else:
  344. assert 0, 'wrong tagFormat worked out'
  345. def testZeroLength(self):
  346. try:
  347. decoder.decode(ints2octs((6, 0, 0)))
  348. except error.PyAsn1Error:
  349. pass
  350. else:
  351. assert 0, 'zero length tolerated'
  352. def testIndefiniteLength(self):
  353. try:
  354. decoder.decode(ints2octs((6, 128, 0)))
  355. except error.PyAsn1Error:
  356. pass
  357. else:
  358. assert 0, 'indefinite length tolerated'
  359. def testReservedLength(self):
  360. try:
  361. decoder.decode(ints2octs((6, 255, 0)))
  362. except error.PyAsn1Error:
  363. pass
  364. else:
  365. assert 0, 'reserved length tolerated'
  366. def testLarge1(self):
  367. assert decoder.decode(
  368. ints2octs((0x06, 0x11, 0x83, 0xC6, 0xDF, 0xD4, 0xCC, 0xB3, 0xFF, 0xFF, 0xFE, 0xF0, 0xB8, 0xD6, 0xB8, 0xCB, 0xE2, 0xB7, 0x17))
  369. ) == ((2, 18446744073709551535184467440737095), null)
  370. def testLarge2(self):
  371. assert decoder.decode(
  372. ints2octs((0x06, 0x13, 0x88, 0x37, 0x83, 0xC6, 0xDF, 0xD4, 0xCC, 0xB3, 0xFF, 0xFF, 0xFE, 0xF0, 0xB8, 0xD6, 0xB8, 0xCB, 0xE2, 0xB6, 0x47))
  373. ) == ((2, 999, 18446744073709551535184467440737095), null)
  374. class RelativeOIDDecoderTestCase(BaseTestCase):
  375. def testOne(self):
  376. obj, rest = decoder.decode(ints2octs((13, 1, 39)))
  377. assert str(obj) == '39'
  378. assert rest == null
  379. def testTwo(self):
  380. assert decoder.decode(
  381. ints2octs((13, 2, 5, 6))
  382. ) == ((5, 6), null)
  383. def testThree(self):
  384. assert decoder.decode(
  385. ints2octs((13, 3, 5, 6, 7))
  386. ) == ((5, 6, 7), null)
  387. def testNonLeading0x80(self):
  388. assert decoder.decode(
  389. ints2octs((13, 5, 85, 4, 129, 128, 0)),
  390. ) == ((85, 4, 16384), null)
  391. def testLeading0x80(self):
  392. try:
  393. decoder.decode(
  394. ints2octs((13, 5, 85, 4, 128, 129, 0))
  395. )
  396. except error.PyAsn1Error:
  397. pass
  398. else:
  399. assert 0, 'Leading 0x80 tolerated'
  400. def testTagFormat(self):
  401. try:
  402. decoder.decode(ints2octs((38, 1, 239)))
  403. except error.PyAsn1Error:
  404. pass
  405. else:
  406. assert 0, 'wrong tagFormat worked out'
  407. def testZeroLength(self):
  408. try:
  409. decoder.decode(ints2octs((13, 0, 0)))
  410. except error.PyAsn1Error:
  411. pass
  412. else:
  413. assert 0, 'zero length tolerated'
  414. def testIndefiniteLength(self):
  415. try:
  416. decoder.decode(ints2octs((13, 128, 0)))
  417. except error.PyAsn1Error:
  418. pass
  419. else:
  420. assert 0, 'indefinite length tolerated'
  421. def testReservedLength(self):
  422. try:
  423. decoder.decode(ints2octs((13, 255, 0)))
  424. except error.PyAsn1Error:
  425. pass
  426. else:
  427. assert 0, 'reserved length tolerated'
  428. def testLarge(self):
  429. assert decoder.decode(
  430. ints2octs((0x0D, 0x13, 0x88, 0x37, 0x83, 0xC6, 0xDF, 0xD4, 0xCC, 0xB3, 0xFF, 0xFF, 0xFE, 0xF0, 0xB8, 0xD6, 0xB8, 0xCB, 0xE2, 0xB6, 0x47))
  431. ) == ((1079, 18446744073709551535184467440737095), null)
  432. class RealDecoderTestCase(BaseTestCase):
  433. def testChar(self):
  434. assert decoder.decode(
  435. ints2octs((9, 7, 3, 49, 50, 51, 69, 49, 49))
  436. ) == (univ.Real((123, 10, 11)), null)
  437. def testBin1(self): # check base = 2
  438. assert decoder.decode( # (0.5, 2, 0) encoded with base = 2
  439. ints2octs((9, 3, 128, 255, 1))
  440. ) == (univ.Real((1, 2, -1)), null)
  441. def testBin2(self): # check base = 2 and scale factor
  442. assert decoder.decode( # (3.25, 2, 0) encoded with base = 8
  443. ints2octs((9, 3, 148, 255, 13))
  444. ) == (univ.Real((26, 2, -3)), null)
  445. def testBin3(self): # check base = 16
  446. assert decoder.decode( # (0.00390625, 2, 0) encoded with base = 16
  447. ints2octs((9, 3, 160, 254, 1))
  448. ) == (univ.Real((1, 2, -8)), null)
  449. def testBin4(self): # check exponent = 0
  450. assert decoder.decode( # (1, 2, 0) encoded with base = 2
  451. ints2octs((9, 3, 128, 0, 1))
  452. ) == (univ.Real((1, 2, 0)), null)
  453. def testBin5(self): # case of 2 octs for exponent and negative exponent
  454. assert decoder.decode( # (3, 2, -1020) encoded with base = 16
  455. ints2octs((9, 4, 161, 255, 1, 3))
  456. ) == (univ.Real((3, 2, -1020)), null)
  457. # TODO: this requires Real type comparison fix
  458. # def testBin6(self):
  459. # assert decoder.decode(
  460. # ints2octs((9, 5, 162, 0, 255, 255, 1))
  461. # ) == (univ.Real((1, 2, 262140)), null)
  462. # def testBin7(self):
  463. # assert decoder.decode(
  464. # ints2octs((9, 7, 227, 4, 1, 35, 69, 103, 1))
  465. # ) == (univ.Real((-1, 2, 76354972)), null)
  466. def testPlusInf(self):
  467. assert decoder.decode(
  468. ints2octs((9, 1, 64))
  469. ) == (univ.Real('inf'), null)
  470. def testMinusInf(self):
  471. assert decoder.decode(
  472. ints2octs((9, 1, 65))
  473. ) == (univ.Real('-inf'), null)
  474. def testEmpty(self):
  475. assert decoder.decode(
  476. ints2octs((9, 0))
  477. ) == (univ.Real(0.0), null)
  478. def testTagFormat(self):
  479. try:
  480. decoder.decode(ints2octs((41, 0)))
  481. except error.PyAsn1Error:
  482. pass
  483. else:
  484. assert 0, 'wrong tagFormat worked out'
  485. def testShortEncoding(self):
  486. try:
  487. decoder.decode(ints2octs((9, 1, 131)))
  488. except error.PyAsn1Error:
  489. pass
  490. else:
  491. assert 0, 'accepted too-short real'
  492. class UniversalStringDecoderTestCase(BaseTestCase):
  493. def testDecoder(self):
  494. assert decoder.decode(ints2octs((28, 12, 0, 0, 0, 97, 0, 0, 0, 98, 0, 0, 0, 99))) == (char.UniversalString(sys.version_info[0] >= 3 and 'abc' or unicode('abc')), null)
  495. class BMPStringDecoderTestCase(BaseTestCase):
  496. def testDecoder(self):
  497. assert decoder.decode(ints2octs((30, 6, 0, 97, 0, 98, 0, 99))) == (char.BMPString(sys.version_info[0] >= 3 and 'abc' or unicode('abc')), null)
  498. class UTF8StringDecoderTestCase(BaseTestCase):
  499. def testDecoder(self):
  500. assert decoder.decode(ints2octs((12, 3, 97, 98, 99))) == (char.UTF8String(sys.version_info[0] >= 3 and 'abc' or unicode('abc')), null)
  501. class SequenceOfDecoderTestCase(BaseTestCase):
  502. def setUp(self):
  503. BaseTestCase.setUp(self)
  504. self.s = univ.SequenceOf(componentType=univ.OctetString())
  505. self.s.setComponentByPosition(0, univ.OctetString('quick brown'))
  506. def testDefMode(self):
  507. assert decoder.decode(
  508. ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  509. ) == (self.s, null)
  510. def testIndefMode(self):
  511. assert decoder.decode(
  512. ints2octs((48, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
  513. ) == (self.s, null)
  514. def testDefModeChunked(self):
  515. assert decoder.decode(
  516. ints2octs((48, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
  517. ) == (self.s, null)
  518. def testIndefModeChunked(self):
  519. assert decoder.decode(
  520. ints2octs((48, 128, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 0, 0))
  521. ) == (self.s, null)
  522. def testSchemalessDecoder(self):
  523. assert decoder.decode(
  524. ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=univ.SequenceOf()
  525. ) == (self.s, null)
  526. class ExpTaggedSequenceOfDecoderTestCase(BaseTestCase):
  527. def testWithSchema(self):
  528. s = univ.SequenceOf().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 3))
  529. s2, r = decoder.decode(
  530. ints2octs((163, 15, 48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=s
  531. )
  532. assert not r
  533. assert s2 == [str2octs('quick brown')]
  534. assert s.tagSet == s2.tagSet
  535. def testWithoutSchema(self):
  536. s = univ.SequenceOf().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 3))
  537. s2, r = decoder.decode(
  538. ints2octs((163, 15, 48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  539. )
  540. assert not r
  541. assert s2 == [str2octs('quick brown')]
  542. assert s.tagSet == s2.tagSet
  543. class SequenceOfDecoderWithSchemaTestCase(BaseTestCase):
  544. def setUp(self):
  545. BaseTestCase.setUp(self)
  546. self.s = univ.SequenceOf(componentType=univ.OctetString())
  547. self.s.setComponentByPosition(0, univ.OctetString('quick brown'))
  548. def testDefMode(self):
  549. assert decoder.decode(
  550. ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
  551. ) == (self.s, null)
  552. def testIndefMode(self):
  553. assert decoder.decode(
  554. ints2octs((48, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0)), asn1Spec=self.s
  555. ) == (self.s, null)
  556. def testDefModeChunked(self):
  557. assert decoder.decode(
  558. ints2octs((48, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110)), asn1Spec=self.s
  559. ) == (self.s, null)
  560. def testIndefModeChunked(self):
  561. assert decoder.decode(
  562. ints2octs((48, 128, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 0, 0)), asn1Spec=self.s
  563. ) == (self.s, null)
  564. class SetOfDecoderTestCase(BaseTestCase):
  565. def setUp(self):
  566. BaseTestCase.setUp(self)
  567. self.s = univ.SetOf(componentType=univ.OctetString())
  568. self.s.setComponentByPosition(0, univ.OctetString('quick brown'))
  569. def testDefMode(self):
  570. assert decoder.decode(
  571. ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  572. ) == (self.s, null)
  573. def testIndefMode(self):
  574. assert decoder.decode(
  575. ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
  576. ) == (self.s, null)
  577. def testDefModeChunked(self):
  578. assert decoder.decode(
  579. ints2octs((49, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
  580. ) == (self.s, null)
  581. def testIndefModeChunked(self):
  582. assert decoder.decode(
  583. ints2octs((49, 128, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 0, 0))
  584. ) == (self.s, null)
  585. def testSchemalessDecoder(self):
  586. assert decoder.decode(
  587. ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=univ.SetOf()
  588. ) == (self.s, null)
  589. class SetOfDecoderWithSchemaTestCase(BaseTestCase):
  590. def setUp(self):
  591. BaseTestCase.setUp(self)
  592. self.s = univ.SetOf(componentType=univ.OctetString())
  593. self.s.setComponentByPosition(0, univ.OctetString('quick brown'))
  594. def testDefMode(self):
  595. assert decoder.decode(
  596. ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
  597. ) == (self.s, null)
  598. def testIndefMode(self):
  599. assert decoder.decode(
  600. ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0)), asn1Spec=self.s
  601. ) == (self.s, null)
  602. def testDefModeChunked(self):
  603. assert decoder.decode(
  604. ints2octs((49, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110)), asn1Spec=self.s
  605. ) == (self.s, null)
  606. def testIndefModeChunked(self):
  607. assert decoder.decode(
  608. ints2octs((49, 128, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 0, 0)), asn1Spec=self.s
  609. ) == (self.s, null)
  610. class SequenceDecoderTestCase(BaseTestCase):
  611. def setUp(self):
  612. BaseTestCase.setUp(self)
  613. self.s = univ.Sequence(
  614. componentType=namedtype.NamedTypes(
  615. namedtype.NamedType('place-holder', univ.Null(null)),
  616. namedtype.NamedType('first-name', univ.OctetString(null)),
  617. namedtype.NamedType('age', univ.Integer(33))
  618. )
  619. )
  620. self.s.setComponentByPosition(0, univ.Null(null))
  621. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  622. self.s.setComponentByPosition(2, univ.Integer(1))
  623. def testWithOptionalAndDefaultedDefMode(self):
  624. assert decoder.decode(
  625. ints2octs((48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
  626. ) == (self.s, null)
  627. def testWithOptionalAndDefaultedIndefMode(self):
  628. assert decoder.decode(
  629. ints2octs((48, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0))
  630. ) == (self.s, null)
  631. def testWithOptionalAndDefaultedDefModeChunked(self):
  632. assert decoder.decode(
  633. ints2octs(
  634. (48, 24, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 2, 1, 1))
  635. ) == (self.s, null)
  636. def testWithOptionalAndDefaultedIndefModeChunked(self):
  637. assert decoder.decode(
  638. ints2octs((48, 128, 5, 0, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0))
  639. ) == (self.s, null)
  640. def testWithOptionalAndDefaultedDefModeSubst(self):
  641. assert decoder.decode(
  642. ints2octs((48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)),
  643. substrateFun=lambda a, b, c, d: streaming.readFromStream(b, c)
  644. ) == (ints2octs((5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)), str2octs(''))
  645. def testWithOptionalAndDefaultedDefModeSubstV04(self):
  646. assert decoder.decode(
  647. ints2octs((48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)),
  648. substrateFun=lambda a, b, c: (b, b[c:])
  649. ) == (ints2octs((5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)), str2octs(''))
  650. def testWithOptionalAndDefaultedIndefModeSubst(self):
  651. assert decoder.decode(
  652. ints2octs((48, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)),
  653. substrateFun=lambda a, b, c, d: streaming.readFromStream(b, c)
  654. ) == (ints2octs(
  655. (5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)), str2octs(''))
  656. def testWithOptionalAndDefaultedIndefModeSubstV04(self):
  657. assert decoder.decode(
  658. ints2octs((48, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)),
  659. substrateFun=lambda a, b, c: (b, b[c:])
  660. ) == (ints2octs(
  661. (5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)), str2octs(''))
  662. def testTagFormat(self):
  663. try:
  664. decoder.decode(
  665. ints2octs((16, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
  666. )
  667. except error.PyAsn1Error:
  668. pass
  669. else:
  670. assert 0, 'wrong tagFormat worked out'
  671. class SequenceDecoderWithSchemaTestCase(BaseTestCase):
  672. def setUp(self):
  673. BaseTestCase.setUp(self)
  674. self.s = univ.Sequence(
  675. componentType=namedtype.NamedTypes(
  676. namedtype.NamedType('place-holder', univ.Null(null)),
  677. namedtype.OptionalNamedType('first-name', univ.OctetString()),
  678. namedtype.DefaultedNamedType('age', univ.Integer(33)),
  679. )
  680. )
  681. def __init(self):
  682. self.s.clear()
  683. self.s.setComponentByPosition(0, univ.Null(null))
  684. def __initWithOptional(self):
  685. self.s.clear()
  686. self.s.setComponentByPosition(0, univ.Null(null))
  687. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  688. def __initWithDefaulted(self):
  689. self.s.clear()
  690. self.s.setComponentByPosition(0, univ.Null(null))
  691. self.s.setComponentByPosition(2, univ.Integer(1))
  692. def __initWithOptionalAndDefaulted(self):
  693. self.s.clear()
  694. self.s.setComponentByPosition(0, univ.Null(null))
  695. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  696. self.s.setComponentByPosition(2, univ.Integer(1))
  697. def testDefMode(self):
  698. self.__init()
  699. assert decoder.decode(
  700. ints2octs((48, 2, 5, 0)), asn1Spec=self.s
  701. ) == (self.s, null)
  702. def testIndefMode(self):
  703. self.__init()
  704. assert decoder.decode(
  705. ints2octs((48, 128, 5, 0, 0, 0)), asn1Spec=self.s
  706. ) == (self.s, null)
  707. def testDefModeChunked(self):
  708. self.__init()
  709. assert decoder.decode(
  710. ints2octs((48, 2, 5, 0)), asn1Spec=self.s
  711. ) == (self.s, null)
  712. def testIndefModeChunked(self):
  713. self.__init()
  714. assert decoder.decode(
  715. ints2octs((48, 128, 5, 0, 0, 0)), asn1Spec=self.s
  716. ) == (self.s, null)
  717. def testWithOptionalDefMode(self):
  718. self.__initWithOptional()
  719. assert decoder.decode(
  720. ints2octs((48, 15, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
  721. ) == (self.s, null)
  722. def testWithOptionaIndefMode(self):
  723. self.__initWithOptional()
  724. assert decoder.decode(
  725. ints2octs((48, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 0, 0)),
  726. asn1Spec=self.s
  727. ) == (self.s, null)
  728. def testWithOptionalDefModeChunked(self):
  729. self.__initWithOptional()
  730. assert decoder.decode(
  731. ints2octs((48, 21, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110)),
  732. asn1Spec=self.s
  733. ) == (self.s, null)
  734. def testWithOptionalIndefModeChunked(self):
  735. self.__initWithOptional()
  736. assert decoder.decode(
  737. ints2octs((48, 128, 5, 0, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0,
  738. 0, 0, 0)),
  739. asn1Spec=self.s
  740. ) == (self.s, null)
  741. def testWithDefaultedDefMode(self):
  742. self.__initWithDefaulted()
  743. assert decoder.decode(
  744. ints2octs((48, 5, 5, 0, 2, 1, 1)), asn1Spec=self.s
  745. ) == (self.s, null)
  746. def testWithDefaultedIndefMode(self):
  747. self.__initWithDefaulted()
  748. assert decoder.decode(
  749. ints2octs((48, 128, 5, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s
  750. ) == (self.s, null)
  751. def testWithDefaultedDefModeChunked(self):
  752. self.__initWithDefaulted()
  753. assert decoder.decode(
  754. ints2octs((48, 5, 5, 0, 2, 1, 1)), asn1Spec=self.s
  755. ) == (self.s, null)
  756. def testWithDefaultedIndefModeChunked(self):
  757. self.__initWithDefaulted()
  758. assert decoder.decode(
  759. ints2octs((48, 128, 5, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s
  760. ) == (self.s, null)
  761. def testWithOptionalAndDefaultedDefMode(self):
  762. self.__initWithOptionalAndDefaulted()
  763. assert decoder.decode(
  764. ints2octs((48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)),
  765. asn1Spec=self.s
  766. ) == (self.s, null)
  767. def testWithOptionalAndDefaultedIndefMode(self):
  768. self.__initWithOptionalAndDefaulted()
  769. assert decoder.decode(
  770. ints2octs((48, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1,
  771. 0, 0)), asn1Spec=self.s
  772. ) == (self.s, null)
  773. def testWithOptionalAndDefaultedDefModeChunked(self):
  774. self.__initWithOptionalAndDefaulted()
  775. assert decoder.decode(
  776. ints2octs(
  777. (48, 24, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 2, 1, 1)),
  778. asn1Spec=self.s
  779. ) == (self.s, null)
  780. def testWithOptionalAndDefaultedIndefModeChunked(self):
  781. self.__initWithOptionalAndDefaulted()
  782. assert decoder.decode(
  783. ints2octs((48, 128, 5, 0, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0,
  784. 0, 2, 1, 1, 0, 0)), asn1Spec=self.s
  785. ) == (self.s, null)
  786. class SequenceDecoderWithUntaggedOpenTypesTestCase(BaseTestCase):
  787. def setUp(self):
  788. openType = opentype.OpenType(
  789. 'id',
  790. {1: univ.Integer(),
  791. 2: univ.OctetString()}
  792. )
  793. self.s = univ.Sequence(
  794. componentType=namedtype.NamedTypes(
  795. namedtype.NamedType('id', univ.Integer()),
  796. namedtype.NamedType('blob', univ.Any(), openType=openType)
  797. )
  798. )
  799. def testDecodeOpenTypesChoiceOne(self):
  800. s, r = decoder.decode(
  801. ints2octs((48, 6, 2, 1, 1, 2, 1, 12)), asn1Spec=self.s,
  802. decodeOpenTypes=True
  803. )
  804. assert not r
  805. assert s[0] == 1
  806. assert s[1] == 12
  807. def testDecodeOpenTypesChoiceTwo(self):
  808. s, r = decoder.decode(
  809. ints2octs((48, 16, 2, 1, 2, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s,
  810. decodeOpenTypes=True
  811. )
  812. assert not r
  813. assert s[0] == 2
  814. assert s[1] == univ.OctetString('quick brown')
  815. def testDecodeOpenTypesUnknownType(self):
  816. try:
  817. s, r = decoder.decode(
  818. ints2octs((48, 6, 2, 1, 2, 6, 1, 39)), asn1Spec=self.s,
  819. decodeOpenTypes=True
  820. )
  821. except error.PyAsn1Error:
  822. pass
  823. else:
  824. assert False, 'unknown open type tolerated'
  825. def testDecodeOpenTypesUnknownId(self):
  826. s, r = decoder.decode(
  827. ints2octs((48, 6, 2, 1, 3, 6, 1, 39)), asn1Spec=self.s,
  828. decodeOpenTypes=True
  829. )
  830. assert not r
  831. assert s[0] == 3
  832. assert s[1] == univ.OctetString(hexValue='060127')
  833. def testDontDecodeOpenTypesChoiceOne(self):
  834. s, r = decoder.decode(
  835. ints2octs((48, 6, 2, 1, 1, 2, 1, 12)), asn1Spec=self.s
  836. )
  837. assert not r
  838. assert s[0] == 1
  839. assert s[1] == ints2octs((2, 1, 12))
  840. def testDontDecodeOpenTypesChoiceTwo(self):
  841. s, r = decoder.decode(
  842. ints2octs((48, 16, 2, 1, 2, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
  843. )
  844. assert not r
  845. assert s[0] == 2
  846. assert s[1] == ints2octs((4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  847. class SequenceDecoderWithImplicitlyTaggedOpenTypesTestCase(BaseTestCase):
  848. def setUp(self):
  849. openType = opentype.OpenType(
  850. 'id',
  851. {1: univ.Integer(),
  852. 2: univ.OctetString()}
  853. )
  854. self.s = univ.Sequence(
  855. componentType=namedtype.NamedTypes(
  856. namedtype.NamedType('id', univ.Integer()),
  857. namedtype.NamedType(
  858. 'blob', univ.Any().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType
  859. )
  860. )
  861. )
  862. def testDecodeOpenTypesChoiceOne(self):
  863. s, r = decoder.decode(
  864. ints2octs((48, 8, 2, 1, 1, 131, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
  865. )
  866. assert not r
  867. assert s[0] == 1
  868. assert s[1] == 12
  869. def testDecodeOpenTypesUnknownId(self):
  870. s, r = decoder.decode(
  871. ints2octs((48, 8, 2, 1, 3, 131, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
  872. )
  873. assert not r
  874. assert s[0] == 3
  875. assert s[1] == univ.OctetString(hexValue='02010C')
  876. class SequenceDecoderWithExplicitlyTaggedOpenTypesTestCase(BaseTestCase):
  877. def setUp(self):
  878. openType = opentype.OpenType(
  879. 'id',
  880. {1: univ.Integer(),
  881. 2: univ.OctetString()}
  882. )
  883. self.s = univ.Sequence(
  884. componentType=namedtype.NamedTypes(
  885. namedtype.NamedType('id', univ.Integer()),
  886. namedtype.NamedType(
  887. 'blob', univ.Any().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType
  888. )
  889. )
  890. )
  891. def testDecodeOpenTypesChoiceOne(self):
  892. s, r = decoder.decode(
  893. ints2octs((48, 8, 2, 1, 1, 163, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
  894. )
  895. assert not r
  896. assert s[0] == 1
  897. assert s[1] == 12
  898. def testDecodeOpenTypesUnknownId(self):
  899. s, r = decoder.decode(
  900. ints2octs((48, 8, 2, 1, 3, 163, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
  901. )
  902. assert not r
  903. assert s[0] == 3
  904. assert s[1] == univ.OctetString(hexValue='02010C')
  905. class SequenceDecoderWithUnaggedSetOfOpenTypesTestCase(BaseTestCase):
  906. def setUp(self):
  907. openType = opentype.OpenType(
  908. 'id',
  909. {1: univ.Integer(),
  910. 2: univ.OctetString()}
  911. )
  912. self.s = univ.Sequence(
  913. componentType=namedtype.NamedTypes(
  914. namedtype.NamedType('id', univ.Integer()),
  915. namedtype.NamedType('blob', univ.SetOf(componentType=univ.Any()),
  916. openType=openType)
  917. )
  918. )
  919. def testDecodeOpenTypesChoiceOne(self):
  920. s, r = decoder.decode(
  921. ints2octs((48, 8, 2, 1, 1, 49, 3, 2, 1, 12)), asn1Spec=self.s,
  922. decodeOpenTypes=True
  923. )
  924. assert not r
  925. assert s[0] == 1
  926. assert s[1][0] == 12
  927. def testDecodeOpenTypesChoiceTwo(self):
  928. s, r = decoder.decode(
  929. ints2octs((48, 18, 2, 1, 2, 49, 13, 4, 11, 113, 117, 105, 99,
  930. 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s,
  931. decodeOpenTypes=True
  932. )
  933. assert not r
  934. assert s[0] == 2
  935. assert s[1][0] == univ.OctetString('quick brown')
  936. def testDecodeOpenTypesUnknownType(self):
  937. try:
  938. s, r = decoder.decode(
  939. ints2octs((48, 6, 2, 1, 2, 6, 1, 39)), asn1Spec=self.s,
  940. decodeOpenTypes=True
  941. )
  942. except error.PyAsn1Error:
  943. pass
  944. else:
  945. assert False, 'unknown open type tolerated'
  946. def testDecodeOpenTypesUnknownId(self):
  947. s, r = decoder.decode(
  948. ints2octs((48, 8, 2, 1, 3, 49, 3, 2, 1, 12)), asn1Spec=self.s,
  949. decodeOpenTypes=True
  950. )
  951. assert not r
  952. assert s[0] == 3
  953. assert s[1][0] == univ.OctetString(hexValue='02010c')
  954. def testDontDecodeOpenTypesChoiceOne(self):
  955. s, r = decoder.decode(
  956. ints2octs((48, 8, 2, 1, 1, 49, 3, 2, 1, 12)), asn1Spec=self.s
  957. )
  958. assert not r
  959. assert s[0] == 1
  960. assert s[1][0] == ints2octs((2, 1, 12))
  961. def testDontDecodeOpenTypesChoiceTwo(self):
  962. s, r = decoder.decode(
  963. ints2octs((48, 18, 2, 1, 2, 49, 13, 4, 11, 113, 117, 105, 99,
  964. 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
  965. )
  966. assert not r
  967. assert s[0] == 2
  968. assert s[1][0] == ints2octs((4, 11, 113, 117, 105, 99, 107, 32, 98, 114,
  969. 111, 119, 110))
  970. class SequenceDecoderWithImplicitlyTaggedSetOfOpenTypesTestCase(BaseTestCase):
  971. def setUp(self):
  972. openType = opentype.OpenType(
  973. 'id',
  974. {1: univ.Integer(),
  975. 2: univ.OctetString()}
  976. )
  977. self.s = univ.Sequence(
  978. componentType=namedtype.NamedTypes(
  979. namedtype.NamedType('id', univ.Integer()),
  980. namedtype.NamedType(
  981. 'blob', univ.SetOf(
  982. componentType=univ.Any().subtype(
  983. implicitTag=tag.Tag(
  984. tag.tagClassContext, tag.tagFormatSimple, 3))),
  985. openType=openType
  986. )
  987. )
  988. )
  989. def testDecodeOpenTypesChoiceOne(self):
  990. s, r = decoder.decode(
  991. ints2octs((48, 10, 2, 1, 1, 49, 5, 131, 3, 2, 1, 12)),
  992. asn1Spec=self.s, decodeOpenTypes=True
  993. )
  994. assert not r
  995. assert s[0] == 1
  996. assert s[1][0] == 12
  997. def testDecodeOpenTypesUnknownId(self):
  998. s, r = decoder.decode(
  999. ints2octs((48, 10, 2, 1, 3, 49, 5, 131, 3, 2, 1, 12)),
  1000. asn1Spec=self.s, decodeOpenTypes=True
  1001. )
  1002. assert not r
  1003. assert s[0] == 3
  1004. assert s[1][0] == univ.OctetString(hexValue='02010C')
  1005. class SequenceDecoderWithExplicitlyTaggedSetOfOpenTypesTestCase(BaseTestCase):
  1006. def setUp(self):
  1007. openType = opentype.OpenType(
  1008. 'id',
  1009. {1: univ.Integer(),
  1010. 2: univ.OctetString()}
  1011. )
  1012. self.s = univ.Sequence(
  1013. componentType=namedtype.NamedTypes(
  1014. namedtype.NamedType('id', univ.Integer()),
  1015. namedtype.NamedType(
  1016. 'blob', univ.SetOf(
  1017. componentType=univ.Any().subtype(
  1018. explicitTag=tag.Tag(
  1019. tag.tagClassContext, tag.tagFormatSimple, 3))),
  1020. openType=openType
  1021. )
  1022. )
  1023. )
  1024. def testDecodeOpenTypesChoiceOne(self):
  1025. s, r = decoder.decode(
  1026. ints2octs((48, 10, 2, 1, 1, 49, 5, 131, 3, 2, 1, 12)),
  1027. asn1Spec=self.s, decodeOpenTypes=True
  1028. )
  1029. assert not r
  1030. assert s[0] == 1
  1031. assert s[1][0] == 12
  1032. def testDecodeOpenTypesUnknownId(self):
  1033. s, r = decoder.decode(
  1034. ints2octs( (48, 10, 2, 1, 3, 49, 5, 131, 3, 2, 1, 12)),
  1035. asn1Spec=self.s, decodeOpenTypes=True
  1036. )
  1037. assert not r
  1038. assert s[0] == 3
  1039. assert s[1][0] == univ.OctetString(hexValue='02010C')
  1040. class SetDecoderTestCase(BaseTestCase):
  1041. def setUp(self):
  1042. BaseTestCase.setUp(self)
  1043. self.s = univ.Set(
  1044. componentType=namedtype.NamedTypes(
  1045. namedtype.NamedType('place-holder', univ.Null(null)),
  1046. namedtype.NamedType('first-name', univ.OctetString(null)),
  1047. namedtype.NamedType('age', univ.Integer(33))
  1048. )
  1049. )
  1050. self.s.setComponentByPosition(0, univ.Null(null))
  1051. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  1052. self.s.setComponentByPosition(2, univ.Integer(1))
  1053. def testWithOptionalAndDefaultedDefMode(self):
  1054. assert decoder.decode(
  1055. ints2octs((49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
  1056. ) == (self.s, null)
  1057. def testWithOptionalAndDefaultedIndefMode(self):
  1058. assert decoder.decode(
  1059. ints2octs((49, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0))
  1060. ) == (self.s, null)
  1061. def testWithOptionalAndDefaultedDefModeChunked(self):
  1062. assert decoder.decode(
  1063. ints2octs(
  1064. (49, 24, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 2, 1, 1))
  1065. ) == (self.s, null)
  1066. def testWithOptionalAndDefaultedIndefModeChunked(self):
  1067. assert decoder.decode(
  1068. ints2octs((49, 128, 5, 0, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0))
  1069. ) == (self.s, null)
  1070. def testWithOptionalAndDefaultedDefModeSubst(self):
  1071. assert decoder.decode(
  1072. ints2octs((49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)),
  1073. substrateFun=lambda a, b, c, d: streaming.readFromStream(b, c)
  1074. ) == (ints2octs((5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)), str2octs(''))
  1075. def testWithOptionalAndDefaultedDefModeSubstV04(self):
  1076. assert decoder.decode(
  1077. ints2octs((49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)),
  1078. substrateFun=lambda a, b, c: (b, b[c:])
  1079. ) == (ints2octs((5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)), str2octs(''))
  1080. def testWithOptionalAndDefaultedIndefModeSubst(self):
  1081. assert decoder.decode(
  1082. ints2octs((49, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)),
  1083. substrateFun=lambda a, b, c, d: streaming.readFromStream(b, c)
  1084. ) == (ints2octs(
  1085. (5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)), str2octs(''))
  1086. def testWithOptionalAndDefaultedIndefModeSubstV04(self):
  1087. assert decoder.decode(
  1088. ints2octs((49, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)),
  1089. substrateFun=lambda a, b, c: (b, b[c:])
  1090. ) == (ints2octs(
  1091. (5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)), str2octs(''))
  1092. def testTagFormat(self):
  1093. try:
  1094. decoder.decode(
  1095. ints2octs((16, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
  1096. )
  1097. except error.PyAsn1Error:
  1098. pass
  1099. else:
  1100. assert 0, 'wrong tagFormat worked out'
  1101. class SetDecoderWithSchemaTestCase(BaseTestCase):
  1102. def setUp(self):
  1103. BaseTestCase.setUp(self)
  1104. self.s = univ.Set(
  1105. componentType=namedtype.NamedTypes(
  1106. namedtype.NamedType('place-holder', univ.Null(null)),
  1107. namedtype.OptionalNamedType('first-name', univ.OctetString()),
  1108. namedtype.DefaultedNamedType('age', univ.Integer(33)),
  1109. )
  1110. )
  1111. def __init(self):
  1112. self.s.clear()
  1113. self.s.setComponentByPosition(0, univ.Null(null))
  1114. def __initWithOptional(self):
  1115. self.s.clear()
  1116. self.s.setComponentByPosition(0, univ.Null(null))
  1117. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  1118. def __initWithDefaulted(self):
  1119. self.s.clear()
  1120. self.s.setComponentByPosition(0, univ.Null(null))
  1121. self.s.setComponentByPosition(2, univ.Integer(1))
  1122. def __initWithOptionalAndDefaulted(self):
  1123. self.s.clear()
  1124. self.s.setComponentByPosition(0, univ.Null(null))
  1125. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  1126. self.s.setComponentByPosition(2, univ.Integer(1))
  1127. def testDefMode(self):
  1128. self.__init()
  1129. assert decoder.decode(
  1130. ints2octs((49, 128, 5, 0, 0, 0)), asn1Spec=self.s
  1131. ) == (self.s, null)
  1132. def testIndefMode(self):
  1133. self.__init()
  1134. assert decoder.decode(
  1135. ints2octs((49, 128, 5, 0, 0, 0)), asn1Spec=self.s
  1136. ) == (self.s, null)
  1137. def testDefModeChunked(self):
  1138. self.__init()
  1139. assert decoder.decode(
  1140. ints2octs((49, 2, 5, 0)), asn1Spec=self.s
  1141. ) == (self.s, null)
  1142. def testIndefModeChunked(self):
  1143. self.__init()
  1144. assert decoder.decode(
  1145. ints2octs((49, 128, 5, 0, 0, 0)), asn1Spec=self.s
  1146. ) == (self.s, null)
  1147. def testWithOptionalDefMode(self):
  1148. self.__initWithOptional()
  1149. assert decoder.decode(
  1150. ints2octs((49, 15, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
  1151. ) == (self.s, null)
  1152. def testWithOptionalIndefMode(self):
  1153. self.__initWithOptional()
  1154. assert decoder.decode(
  1155. ints2octs((49, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 0, 0)), asn1Spec=self.s
  1156. ) == (self.s, null)
  1157. def testWithOptionalDefModeChunked(self):
  1158. self.__initWithOptional()
  1159. assert decoder.decode(
  1160. ints2octs((49, 21, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110)), asn1Spec=self.s
  1161. ) == (self.s, null)
  1162. def testWithOptionalIndefModeChunked(self):
  1163. self.__initWithOptional()
  1164. assert decoder.decode(
  1165. ints2octs((49, 128, 5, 0, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 0, 0)), asn1Spec=self.s
  1166. ) == (self.s, null)
  1167. def testWithDefaultedDefMode(self):
  1168. self.__initWithDefaulted()
  1169. assert decoder.decode(
  1170. ints2octs((49, 5, 5, 0, 2, 1, 1)), asn1Spec=self.s
  1171. ) == (self.s, null)
  1172. def testWithDefaultedIndefMode(self):
  1173. self.__initWithDefaulted()
  1174. assert decoder.decode(
  1175. ints2octs((49, 128, 5, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s
  1176. ) == (self.s, null)
  1177. def testWithDefaultedDefModeChunked(self):
  1178. self.__initWithDefaulted()
  1179. assert decoder.decode(
  1180. ints2octs((49, 5, 5, 0, 2, 1, 1)), asn1Spec=self.s
  1181. ) == (self.s, null)
  1182. def testWithDefaultedIndefModeChunked(self):
  1183. self.__initWithDefaulted()
  1184. assert decoder.decode(
  1185. ints2octs((49, 128, 5, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s
  1186. ) == (self.s, null)
  1187. def testWithOptionalAndDefaultedDefMode(self):
  1188. self.__initWithOptionalAndDefaulted()
  1189. assert decoder.decode(
  1190. ints2octs((49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)), asn1Spec=self.s
  1191. ) == (self.s, null)
  1192. def testWithOptionalAndDefaultedDefModeReordered(self):
  1193. self.__initWithOptionalAndDefaulted()
  1194. assert decoder.decode(
  1195. ints2octs((49, 18, 2, 1, 1, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0)), asn1Spec=self.s
  1196. ) == (self.s, null)
  1197. def testWithOptionalAndDefaultedIndefMode(self):
  1198. self.__initWithOptionalAndDefaulted()
  1199. assert decoder.decode(
  1200. ints2octs((49, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s
  1201. ) == (self.s, null)
  1202. def testWithOptionalAndDefaultedIndefModeReordered(self):
  1203. self.__initWithOptionalAndDefaulted()
  1204. assert decoder.decode(
  1205. ints2octs((49, 128, 2, 1, 1, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 0, 0)), asn1Spec=self.s
  1206. ) == (self.s, null)
  1207. def testWithOptionalAndDefaultedDefModeChunked(self):
  1208. self.__initWithOptionalAndDefaulted()
  1209. assert decoder.decode(
  1210. ints2octs((49, 24, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 2, 1, 1)), asn1Spec=self.s
  1211. ) == (self.s, null)
  1212. def testWithOptionalAndDefaultedIndefModeChunked(self):
  1213. self.__initWithOptionalAndDefaulted()
  1214. assert decoder.decode(
  1215. ints2octs((49, 128, 5, 0, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s
  1216. ) == (self.s, null)
  1217. class SequenceOfWithExpTaggedOctetStringDecoder(BaseTestCase):
  1218. def setUp(self):
  1219. BaseTestCase.setUp(self)
  1220. self.s = univ.SequenceOf(
  1221. componentType=univ.OctetString().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3))
  1222. )
  1223. self.s.setComponentByPosition(0, 'q')
  1224. self.s2 = univ.SequenceOf()
  1225. def testDefModeSchema(self):
  1226. s, r = decoder.decode(ints2octs((48, 5, 163, 3, 4, 1, 113)), asn1Spec=self.s)
  1227. assert not r
  1228. assert s == self.s
  1229. assert s.tagSet == self.s.tagSet
  1230. def testIndefModeSchema(self):
  1231. s, r = decoder.decode(ints2octs((48, 128, 163, 128, 4, 1, 113, 0, 0, 0, 0)), asn1Spec=self.s)
  1232. assert not r
  1233. assert s == self.s
  1234. assert s.tagSet == self.s.tagSet
  1235. def testDefModeNoComponent(self):
  1236. s, r = decoder.decode(ints2octs((48, 5, 163, 3, 4, 1, 113)), asn1Spec=self.s2)
  1237. assert not r
  1238. assert s == self.s
  1239. assert s.tagSet == self.s.tagSet
  1240. def testIndefModeNoComponent(self):
  1241. s, r = decoder.decode(ints2octs((48, 128, 163, 128, 4, 1, 113, 0, 0, 0, 0)), asn1Spec=self.s2)
  1242. assert not r
  1243. assert s == self.s
  1244. assert s.tagSet == self.s.tagSet
  1245. def testDefModeSchemaless(self):
  1246. s, r = decoder.decode(ints2octs((48, 5, 163, 3, 4, 1, 113)))
  1247. assert not r
  1248. assert s == self.s
  1249. assert s.tagSet == self.s.tagSet
  1250. def testIndefModeSchemaless(self):
  1251. s, r = decoder.decode(ints2octs((48, 128, 163, 128, 4, 1, 113, 0, 0, 0, 0)))
  1252. assert not r
  1253. assert s == self.s
  1254. assert s.tagSet == self.s.tagSet
  1255. class SequenceWithExpTaggedOctetStringDecoder(BaseTestCase):
  1256. def setUp(self):
  1257. BaseTestCase.setUp(self)
  1258. self.s = univ.Sequence(
  1259. componentType=namedtype.NamedTypes(
  1260. namedtype.NamedType(
  1261. 'x', univ.OctetString().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3))
  1262. )
  1263. )
  1264. )
  1265. self.s.setComponentByPosition(0, 'q')
  1266. self.s2 = univ.Sequence()
  1267. def testDefModeSchema(self):
  1268. s, r = decoder.decode(ints2octs((48, 5, 163, 3, 4, 1, 113)), asn1Spec=self.s)
  1269. assert not r
  1270. assert s == self.s
  1271. assert s.tagSet == self.s.tagSet
  1272. def testIndefModeSchema(self):
  1273. s, r = decoder.decode(ints2octs((48, 128, 163, 128, 4, 1, 113, 0, 0, 0, 0)), asn1Spec=self.s)
  1274. assert not r
  1275. assert s == self.s
  1276. assert s.tagSet == self.s.tagSet
  1277. def testDefModeNoComponent(self):
  1278. s, r = decoder.decode(ints2octs((48, 5, 163, 3, 4, 1, 113)), asn1Spec=self.s2)
  1279. assert not r
  1280. assert s == self.s
  1281. assert s.tagSet == self.s.tagSet
  1282. def testIndefModeNoComponent(self):
  1283. s, r = decoder.decode(ints2octs((48, 128, 163, 128, 4, 1, 113, 0, 0, 0, 0)), asn1Spec=self.s2)
  1284. assert not r
  1285. assert s == self.s
  1286. assert s.tagSet == self.s.tagSet
  1287. def testDefModeSchemaless(self):
  1288. s, r = decoder.decode(ints2octs((48, 5, 163, 3, 4, 1, 113)))
  1289. assert not r
  1290. assert s == self.s
  1291. assert s.tagSet == self.s.tagSet
  1292. def testIndefModeSchemaless(self):
  1293. s, r = decoder.decode(ints2octs((48, 128, 163, 128, 4, 1, 113, 0, 0, 0, 0)))
  1294. assert not r
  1295. assert s == self.s
  1296. assert s.tagSet == self.s.tagSet
  1297. class ChoiceDecoderTestCase(BaseTestCase):
  1298. def setUp(self):
  1299. BaseTestCase.setUp(self)
  1300. self.s = univ.Choice(
  1301. componentType=namedtype.NamedTypes(
  1302. namedtype.NamedType('place-holder', univ.Null(null)),
  1303. namedtype.NamedType('number', univ.Integer(0)),
  1304. namedtype.NamedType('string', univ.OctetString())
  1305. )
  1306. )
  1307. def testBySpec(self):
  1308. self.s.setComponentByPosition(0, univ.Null(null))
  1309. assert decoder.decode(
  1310. ints2octs((5, 0)), asn1Spec=self.s
  1311. ) == (self.s, null)
  1312. def testWithoutSpec(self):
  1313. self.s.setComponentByPosition(0, univ.Null(null))
  1314. assert decoder.decode(ints2octs((5, 0))) == (self.s, null)
  1315. assert decoder.decode(ints2octs((5, 0))) == (univ.Null(null), null)
  1316. def testUndefLength(self):
  1317. self.s.setComponentByPosition(2, univ.OctetString('abcdefgh'))
  1318. assert decoder.decode(ints2octs((36, 128, 4, 3, 97, 98, 99, 4, 3, 100, 101, 102, 4, 2, 103, 104, 0, 0)),
  1319. asn1Spec=self.s) == (self.s, null)
  1320. def testExplicitTag(self):
  1321. s = self.s.subtype(explicitTag=tag.Tag(tag.tagClassContext,
  1322. tag.tagFormatConstructed, 4))
  1323. s.setComponentByPosition(0, univ.Null(null))
  1324. assert decoder.decode(ints2octs((164, 2, 5, 0)), asn1Spec=s) == (s, null)
  1325. def testExplicitTagUndefLength(self):
  1326. s = self.s.subtype(explicitTag=tag.Tag(tag.tagClassContext,
  1327. tag.tagFormatConstructed, 4))
  1328. s.setComponentByPosition(0, univ.Null(null))
  1329. assert decoder.decode(ints2octs((164, 128, 5, 0, 0, 0)), asn1Spec=s) == (s, null)
  1330. class AnyDecoderTestCase(BaseTestCase):
  1331. def setUp(self):
  1332. BaseTestCase.setUp(self)
  1333. self.s = univ.Any()
  1334. def testByUntagged(self):
  1335. assert decoder.decode(
  1336. ints2octs((4, 3, 102, 111, 120)), asn1Spec=self.s
  1337. ) == (univ.Any('\004\003fox'), null)
  1338. def testTaggedEx(self):
  1339. s = univ.Any('\004\003fox').subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))
  1340. assert decoder.decode(ints2octs((164, 5, 4, 3, 102, 111, 120)), asn1Spec=s) == (s, null)
  1341. def testTaggedIm(self):
  1342. s = univ.Any('\004\003fox').subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))
  1343. assert decoder.decode(ints2octs((132, 5, 4, 3, 102, 111, 120)), asn1Spec=s) == (s, null)
  1344. def testByUntaggedIndefMode(self):
  1345. assert decoder.decode(
  1346. ints2octs((4, 3, 102, 111, 120)), asn1Spec=self.s
  1347. ) == (univ.Any('\004\003fox'), null)
  1348. def testTaggedExIndefMode(self):
  1349. s = univ.Any('\004\003fox').subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))
  1350. assert decoder.decode(ints2octs((164, 128, 4, 3, 102, 111, 120, 0, 0)), asn1Spec=s) == (s, null)
  1351. def testTaggedImIndefMode(self):
  1352. s = univ.Any('\004\003fox').subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))
  1353. assert decoder.decode(ints2octs((164, 128, 4, 3, 102, 111, 120, 0, 0)), asn1Spec=s) == (s, null)
  1354. def testByUntaggedSubst(self):
  1355. assert decoder.decode(
  1356. ints2octs((4, 3, 102, 111, 120)),
  1357. asn1Spec=self.s,
  1358. substrateFun=lambda a, b, c, d: streaming.readFromStream(b, c)
  1359. ) == (ints2octs((4, 3, 102, 111, 120)), str2octs(''))
  1360. def testByUntaggedSubstV04(self):
  1361. assert decoder.decode(
  1362. ints2octs((4, 3, 102, 111, 120)),
  1363. asn1Spec=self.s,
  1364. substrateFun=lambda a, b, c: (b, b[c:])
  1365. ) == (ints2octs((4, 3, 102, 111, 120)), str2octs(''))
  1366. def testTaggedExSubst(self):
  1367. assert decoder.decode(
  1368. ints2octs((164, 5, 4, 3, 102, 111, 120)),
  1369. asn1Spec=self.s,
  1370. substrateFun=lambda a, b, c, d: streaming.readFromStream(b, c)
  1371. ) == (ints2octs((164, 5, 4, 3, 102, 111, 120)), str2octs(''))
  1372. def testTaggedExSubstV04(self):
  1373. assert decoder.decode(
  1374. ints2octs((164, 5, 4, 3, 102, 111, 120)),
  1375. asn1Spec=self.s,
  1376. substrateFun=lambda a, b, c: (b, b[c:])
  1377. ) == (ints2octs((164, 5, 4, 3, 102, 111, 120)), str2octs(''))
  1378. class EndOfOctetsTestCase(BaseTestCase):
  1379. def testUnexpectedEoo(self):
  1380. try:
  1381. decoder.decode(ints2octs((0, 0)))
  1382. except error.PyAsn1Error:
  1383. pass
  1384. else:
  1385. assert 0, 'end-of-contents octets accepted at top level'
  1386. def testExpectedEoo(self):
  1387. result, remainder = decoder.decode(ints2octs((0, 0)), allowEoo=True)
  1388. assert eoo.endOfOctets.isSameTypeWith(result) and result == eoo.endOfOctets and result is eoo.endOfOctets
  1389. assert remainder == null
  1390. def testDefiniteNoEoo(self):
  1391. try:
  1392. decoder.decode(ints2octs((0x23, 0x02, 0x00, 0x00)))
  1393. except error.PyAsn1Error:
  1394. pass
  1395. else:
  1396. assert 0, 'end-of-contents octets accepted inside definite-length encoding'
  1397. def testIndefiniteEoo(self):
  1398. result, remainder = decoder.decode(ints2octs((0x23, 0x80, 0x00, 0x00)))
  1399. assert result == () and remainder == null, 'incorrect decoding of indefinite length end-of-octets'
  1400. def testNoLongFormEoo(self):
  1401. try:
  1402. decoder.decode(ints2octs((0x23, 0x80, 0x00, 0x81, 0x00)))
  1403. except error.PyAsn1Error:
  1404. pass
  1405. else:
  1406. assert 0, 'end-of-contents octets accepted with invalid long-form length'
  1407. def testNoConstructedEoo(self):
  1408. try:
  1409. decoder.decode(ints2octs((0x23, 0x80, 0x20, 0x00)))
  1410. except error.PyAsn1Error:
  1411. pass
  1412. else:
  1413. assert 0, 'end-of-contents octets accepted with invalid constructed encoding'
  1414. def testNoEooData(self):
  1415. try:
  1416. decoder.decode(ints2octs((0x23, 0x80, 0x00, 0x01, 0x00)))
  1417. except error.PyAsn1Error:
  1418. pass
  1419. else:
  1420. assert 0, 'end-of-contents octets accepted with unexpected data'
  1421. class NonStringDecoderTestCase(BaseTestCase):
  1422. def setUp(self):
  1423. BaseTestCase.setUp(self)
  1424. self.s = univ.Sequence(
  1425. componentType=namedtype.NamedTypes(
  1426. namedtype.NamedType('place-holder', univ.Null(null)),
  1427. namedtype.NamedType('first-name', univ.OctetString(null)),
  1428. namedtype.NamedType('age', univ.Integer(33))
  1429. )
  1430. )
  1431. self.s.setComponentByPosition(0, univ.Null(null))
  1432. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  1433. self.s.setComponentByPosition(2, univ.Integer(1))
  1434. self.substrate = ints2octs([48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1])
  1435. def testOctetString(self):
  1436. s = list(decoder.StreamingDecoder(
  1437. univ.OctetString(self.substrate), asn1Spec=self.s))
  1438. assert [self.s] == s
  1439. def testAny(self):
  1440. s = list(decoder.StreamingDecoder(
  1441. univ.Any(self.substrate), asn1Spec=self.s))
  1442. assert [self.s] == s
  1443. class ErrorOnDecodingTestCase(BaseTestCase):
  1444. def testErrorCondition(self):
  1445. decode = decoder.SingleItemDecoder(
  1446. tagMap=decoder.TAG_MAP, typeMap=decoder.TYPE_MAP)
  1447. substrate = ints2octs((00, 1, 2))
  1448. stream = streaming.asSeekableStream(substrate)
  1449. try:
  1450. asn1Object = next(decode(stream))
  1451. except error.PyAsn1Error:
  1452. exc = sys.exc_info()[1]
  1453. assert isinstance(exc, error.PyAsn1Error), (
  1454. 'Unexpected exception raised %r' % (exc,))
  1455. else:
  1456. assert False, 'Unexpected decoder result %r' % (asn1Object,)
  1457. def testRawDump(self):
  1458. substrate = ints2octs((31, 8, 2, 1, 1, 131, 3, 2, 1, 12))
  1459. stream = streaming.asSeekableStream(substrate)
  1460. class SingleItemEncoder(decoder.SingleItemDecoder):
  1461. defaultErrorState = decoder.stDumpRawValue
  1462. class StreamingDecoder(decoder.StreamingDecoder):
  1463. SINGLE_ITEM_DECODER = SingleItemEncoder
  1464. class OneShotDecoder(decoder.Decoder):
  1465. STREAMING_DECODER = StreamingDecoder
  1466. d = OneShotDecoder()
  1467. asn1Object, rest = d(stream)
  1468. assert isinstance(asn1Object, univ.Any), (
  1469. 'Unexpected raw dump type %r' % (asn1Object,))
  1470. assert asn1Object.asNumbers() == (31, 8, 2, 1, 1), (
  1471. 'Unexpected raw dump value %r' % (asn1Object,))
  1472. assert rest == ints2octs((131, 3, 2, 1, 12)), (
  1473. 'Unexpected rest of substrate after raw dump %r' % rest)
  1474. @unittest.skipIf(sys.version_info < (3,), "Unsupported on Python 2")
  1475. class BinaryFileTestCase(BaseTestCase):
  1476. """Assure that decode works on open binary files."""
  1477. def testOneObject(self):
  1478. _, path = tempfile.mkstemp()
  1479. try:
  1480. with open(path, "wb") as out:
  1481. out.write(ints2octs((2, 1, 12)))
  1482. with open(path, "rb") as source:
  1483. values = list(decoder.StreamingDecoder(source))
  1484. assert values == [12]
  1485. finally:
  1486. os.remove(path)
  1487. def testMoreObjects(self):
  1488. _, path = tempfile.mkstemp()
  1489. try:
  1490. with open(path, "wb") as out:
  1491. out.write(ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)))
  1492. with open(path, "rb") as source:
  1493. values = list(decoder.StreamingDecoder(source))
  1494. assert values == [12, (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1)]
  1495. finally:
  1496. os.remove(path)
  1497. def testInvalidFileContent(self):
  1498. _, path = tempfile.mkstemp()
  1499. try:
  1500. with open(path, "wb") as out:
  1501. out.write(ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0, 7)))
  1502. with open(path, "rb") as source:
  1503. list(decoder.StreamingDecoder(source))
  1504. except error.EndOfStreamError:
  1505. pass
  1506. finally:
  1507. os.remove(path)
  1508. class BytesIOTestCase(BaseTestCase):
  1509. def testRead(self):
  1510. source = ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0))
  1511. stream = io.BytesIO(source)
  1512. values = list(decoder.StreamingDecoder(stream))
  1513. assert values == [12, (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1)]
  1514. class UnicodeTestCase(BaseTestCase):
  1515. def testFail(self):
  1516. # This ensures that str objects in Python 3.7 cannot be parsed.
  1517. source = ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)).decode("latin-1")
  1518. try:
  1519. next(decoder.StreamingDecoder(source))
  1520. except error.UnsupportedSubstrateError:
  1521. pass
  1522. else:
  1523. assert False, 'Tolerated parsing broken unicode strings'
  1524. class RestartableDecoderTestCase(BaseTestCase):
  1525. class NonBlockingStream(io.BytesIO):
  1526. block = False
  1527. def read(self, size=-1):
  1528. self.block = not self.block
  1529. if self.block:
  1530. return # this is what non-blocking streams sometimes do
  1531. return io.BytesIO.read(self, size)
  1532. def setUp(self):
  1533. BaseTestCase.setUp(self)
  1534. self.s = univ.SequenceOf(componentType=univ.OctetString())
  1535. self.s.setComponentByPosition(0, univ.OctetString('quick brown'))
  1536. source = ints2octs(
  1537. (48, 26,
  1538. 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110,
  1539. 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  1540. self.stream = self.NonBlockingStream(source)
  1541. def testPartialReadingFromNonBlockingStream(self):
  1542. iterator = iter(decoder.StreamingDecoder(self.stream, asn1Spec=self.s))
  1543. res = next(iterator)
  1544. assert isinstance(res, error.SubstrateUnderrunError)
  1545. assert 'asn1Object' not in res.context
  1546. res = next(iterator)
  1547. assert isinstance(res, error.SubstrateUnderrunError)
  1548. assert 'asn1Object' not in res.context
  1549. res = next(iterator)
  1550. assert isinstance(res, error.SubstrateUnderrunError)
  1551. assert 'asn1Object' in res.context
  1552. assert isinstance(res.context['asn1Object'], univ.SequenceOf)
  1553. assert res.context['asn1Object'].isValue
  1554. assert len(res.context['asn1Object']) == 0
  1555. res = next(iterator)
  1556. assert isinstance(res, error.SubstrateUnderrunError)
  1557. assert 'asn1Object' in res.context
  1558. assert isinstance(res.context['asn1Object'], univ.SequenceOf)
  1559. assert res.context['asn1Object'].isValue
  1560. assert len(res.context['asn1Object']) == 0
  1561. res = next(iterator)
  1562. assert isinstance(res, error.SubstrateUnderrunError)
  1563. assert 'asn1Object' in res.context
  1564. assert isinstance(res.context['asn1Object'], univ.SequenceOf)
  1565. assert res.context['asn1Object'].isValue
  1566. assert len(res.context['asn1Object']) == 0
  1567. res = next(iterator)
  1568. assert isinstance(res, error.SubstrateUnderrunError)
  1569. assert 'asn1Object' in res.context
  1570. assert isinstance(res.context['asn1Object'], univ.SequenceOf)
  1571. assert res.context['asn1Object'].isValue
  1572. assert len(res.context['asn1Object']) == 1
  1573. res = next(iterator)
  1574. assert isinstance(res, error.SubstrateUnderrunError)
  1575. assert 'asn1Object' in res.context
  1576. assert isinstance(res.context['asn1Object'], univ.SequenceOf)
  1577. assert res.context['asn1Object'].isValue
  1578. assert len(res.context['asn1Object']) == 1
  1579. res = next(iterator)
  1580. assert isinstance(res, error.SubstrateUnderrunError)
  1581. assert 'asn1Object' in res.context
  1582. assert isinstance(res.context['asn1Object'], univ.SequenceOf)
  1583. assert res.context['asn1Object'].isValue
  1584. assert len(res.context['asn1Object']) == 1
  1585. res = next(iterator)
  1586. assert isinstance(res, univ.SequenceOf)
  1587. assert res.isValue
  1588. assert len(res) == 2
  1589. try:
  1590. next(iterator)
  1591. except StopIteration:
  1592. pass
  1593. else:
  1594. assert False, 'End of stream not raised'
  1595. class CompressedFilesTestCase(BaseTestCase):
  1596. def testGzip(self):
  1597. _, path = tempfile.mkstemp(suffix=".gz")
  1598. try:
  1599. with gzip.open(path, "wb") as out:
  1600. out.write(ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)))
  1601. with gzip.open(path, "rb") as source:
  1602. values = list(decoder.StreamingDecoder(source))
  1603. assert values == [12, (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1)]
  1604. finally:
  1605. os.remove(path)
  1606. def testZipfile(self):
  1607. # It is a seekable stream.
  1608. _, path = tempfile.mkstemp(suffix=".zip")
  1609. try:
  1610. with zipfile.ZipFile(path, "w") as myzip:
  1611. myzip.writestr("data", ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)))
  1612. with zipfile.ZipFile(path, "r") as myzip:
  1613. with myzip.open("data", "r") as source:
  1614. values = list(decoder.StreamingDecoder(source))
  1615. assert values == [12, (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1)]
  1616. finally:
  1617. os.remove(path)
  1618. def testZipfileMany(self):
  1619. _, path = tempfile.mkstemp(suffix=".zip")
  1620. try:
  1621. with zipfile.ZipFile(path, "w") as myzip:
  1622. #for i in range(100):
  1623. myzip.writestr("data", ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)) * 1000)
  1624. with zipfile.ZipFile(path, "r") as myzip:
  1625. with myzip.open("data", "r") as source:
  1626. values = list(decoder.StreamingDecoder(source))
  1627. assert values == [12, (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1)] * 1000
  1628. finally:
  1629. os.remove(path)
  1630. class NonStreamingCompatibilityTestCase(BaseTestCase):
  1631. def setUp(self):
  1632. from pyasn1 import debug
  1633. BaseTestCase.setUp(self)
  1634. debug.setLogger(None) # undo logger setup from BaseTestCase to work around unrelated issue
  1635. def testPartialDecodeWithCustomSubstrateFun(self):
  1636. snmp_req_substrate = ints2octs((
  1637. 0x30, 0x22, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0, 0x15, 0x02, 0x04, 0x69,
  1638. 0x30, 0xdb, 0xeb, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x07, 0x30, 0x05, 0x06, 0x01, 0x01, 0x05, 0x00))
  1639. seq, next_substrate = decoder.decode(
  1640. snmp_req_substrate, asn1Spec=univ.Sequence(),
  1641. recursiveFlag=False, substrateFun=lambda a, b, c: (a, b[:c])
  1642. )
  1643. assert seq.isSameTypeWith(univ.Sequence)
  1644. assert next_substrate == snmp_req_substrate[2:]
  1645. version, next_substrate = decoder.decode(
  1646. next_substrate, asn1Spec=univ.Integer(), recursiveFlag=False,
  1647. substrateFun=lambda a, b, c: (a, b[:c])
  1648. )
  1649. assert version == 1
  1650. def testPartialDecodeWithDefaultSubstrateFun(self):
  1651. substrate = ints2octs((
  1652. 0x04, 0x0e, 0x30, 0x0c, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x3c, 0x03, 0x02
  1653. ))
  1654. result, rest = decoder.decode(substrate, recursiveFlag=False)
  1655. assert result.isSameTypeWith(univ.OctetString)
  1656. assert rest == substrate[2:]
  1657. def testPropagateUserException(self):
  1658. substrate = io.BytesIO(ints2octs((0x04, 0x00)))
  1659. def userSubstrateFun(_asn1Object, _substrate, _length, _options):
  1660. raise TypeError("error inside user function")
  1661. try:
  1662. decoder.decode(substrate, asn1Spec=univ.OctetString, substrateFun=userSubstrateFun)
  1663. except TypeError as exc:
  1664. assert str(exc) == "error inside user function"
  1665. else:
  1666. raise AssertionError("decode() must not hide TypeError from inside user provided callback")
  1667. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  1668. if __name__ == '__main__':
  1669. unittest.TextTestRunner(verbosity=2).run(suite)