test_decoder.py 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973
  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 RealDecoderTestCase(BaseTestCase):
  375. def testChar(self):
  376. assert decoder.decode(
  377. ints2octs((9, 7, 3, 49, 50, 51, 69, 49, 49))
  378. ) == (univ.Real((123, 10, 11)), null)
  379. def testBin1(self): # check base = 2
  380. assert decoder.decode( # (0.5, 2, 0) encoded with base = 2
  381. ints2octs((9, 3, 128, 255, 1))
  382. ) == (univ.Real((1, 2, -1)), null)
  383. def testBin2(self): # check base = 2 and scale factor
  384. assert decoder.decode( # (3.25, 2, 0) encoded with base = 8
  385. ints2octs((9, 3, 148, 255, 13))
  386. ) == (univ.Real((26, 2, -3)), null)
  387. def testBin3(self): # check base = 16
  388. assert decoder.decode( # (0.00390625, 2, 0) encoded with base = 16
  389. ints2octs((9, 3, 160, 254, 1))
  390. ) == (univ.Real((1, 2, -8)), null)
  391. def testBin4(self): # check exponent = 0
  392. assert decoder.decode( # (1, 2, 0) encoded with base = 2
  393. ints2octs((9, 3, 128, 0, 1))
  394. ) == (univ.Real((1, 2, 0)), null)
  395. def testBin5(self): # case of 2 octs for exponent and negative exponent
  396. assert decoder.decode( # (3, 2, -1020) encoded with base = 16
  397. ints2octs((9, 4, 161, 255, 1, 3))
  398. ) == (univ.Real((3, 2, -1020)), null)
  399. # TODO: this requires Real type comparison fix
  400. # def testBin6(self):
  401. # assert decoder.decode(
  402. # ints2octs((9, 5, 162, 0, 255, 255, 1))
  403. # ) == (univ.Real((1, 2, 262140)), null)
  404. # def testBin7(self):
  405. # assert decoder.decode(
  406. # ints2octs((9, 7, 227, 4, 1, 35, 69, 103, 1))
  407. # ) == (univ.Real((-1, 2, 76354972)), null)
  408. def testPlusInf(self):
  409. assert decoder.decode(
  410. ints2octs((9, 1, 64))
  411. ) == (univ.Real('inf'), null)
  412. def testMinusInf(self):
  413. assert decoder.decode(
  414. ints2octs((9, 1, 65))
  415. ) == (univ.Real('-inf'), null)
  416. def testEmpty(self):
  417. assert decoder.decode(
  418. ints2octs((9, 0))
  419. ) == (univ.Real(0.0), null)
  420. def testTagFormat(self):
  421. try:
  422. decoder.decode(ints2octs((41, 0)))
  423. except error.PyAsn1Error:
  424. pass
  425. else:
  426. assert 0, 'wrong tagFormat worked out'
  427. def testShortEncoding(self):
  428. try:
  429. decoder.decode(ints2octs((9, 1, 131)))
  430. except error.PyAsn1Error:
  431. pass
  432. else:
  433. assert 0, 'accepted too-short real'
  434. class UniversalStringDecoderTestCase(BaseTestCase):
  435. def testDecoder(self):
  436. 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)
  437. class BMPStringDecoderTestCase(BaseTestCase):
  438. def testDecoder(self):
  439. 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)
  440. class UTF8StringDecoderTestCase(BaseTestCase):
  441. def testDecoder(self):
  442. assert decoder.decode(ints2octs((12, 3, 97, 98, 99))) == (char.UTF8String(sys.version_info[0] >= 3 and 'abc' or unicode('abc')), null)
  443. class SequenceOfDecoderTestCase(BaseTestCase):
  444. def setUp(self):
  445. BaseTestCase.setUp(self)
  446. self.s = univ.SequenceOf(componentType=univ.OctetString())
  447. self.s.setComponentByPosition(0, univ.OctetString('quick brown'))
  448. def testDefMode(self):
  449. assert decoder.decode(
  450. ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  451. ) == (self.s, null)
  452. def testIndefMode(self):
  453. assert decoder.decode(
  454. ints2octs((48, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
  455. ) == (self.s, null)
  456. def testDefModeChunked(self):
  457. assert decoder.decode(
  458. ints2octs((48, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
  459. ) == (self.s, null)
  460. def testIndefModeChunked(self):
  461. assert decoder.decode(
  462. 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))
  463. ) == (self.s, null)
  464. def testSchemalessDecoder(self):
  465. assert decoder.decode(
  466. ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=univ.SequenceOf()
  467. ) == (self.s, null)
  468. class ExpTaggedSequenceOfDecoderTestCase(BaseTestCase):
  469. def testWithSchema(self):
  470. s = univ.SequenceOf().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 3))
  471. s2, r = decoder.decode(
  472. ints2octs((163, 15, 48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=s
  473. )
  474. assert not r
  475. assert s2 == [str2octs('quick brown')]
  476. assert s.tagSet == s2.tagSet
  477. def testWithoutSchema(self):
  478. s = univ.SequenceOf().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 3))
  479. s2, r = decoder.decode(
  480. ints2octs((163, 15, 48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  481. )
  482. assert not r
  483. assert s2 == [str2octs('quick brown')]
  484. assert s.tagSet == s2.tagSet
  485. class SequenceOfDecoderWithSchemaTestCase(BaseTestCase):
  486. def setUp(self):
  487. BaseTestCase.setUp(self)
  488. self.s = univ.SequenceOf(componentType=univ.OctetString())
  489. self.s.setComponentByPosition(0, univ.OctetString('quick brown'))
  490. def testDefMode(self):
  491. assert decoder.decode(
  492. ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
  493. ) == (self.s, null)
  494. def testIndefMode(self):
  495. assert decoder.decode(
  496. ints2octs((48, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0)), asn1Spec=self.s
  497. ) == (self.s, null)
  498. def testDefModeChunked(self):
  499. assert decoder.decode(
  500. 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
  501. ) == (self.s, null)
  502. def testIndefModeChunked(self):
  503. assert decoder.decode(
  504. 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
  505. ) == (self.s, null)
  506. class SetOfDecoderTestCase(BaseTestCase):
  507. def setUp(self):
  508. BaseTestCase.setUp(self)
  509. self.s = univ.SetOf(componentType=univ.OctetString())
  510. self.s.setComponentByPosition(0, univ.OctetString('quick brown'))
  511. def testDefMode(self):
  512. assert decoder.decode(
  513. ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  514. ) == (self.s, null)
  515. def testIndefMode(self):
  516. assert decoder.decode(
  517. ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
  518. ) == (self.s, null)
  519. def testDefModeChunked(self):
  520. assert decoder.decode(
  521. ints2octs((49, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
  522. ) == (self.s, null)
  523. def testIndefModeChunked(self):
  524. assert decoder.decode(
  525. 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))
  526. ) == (self.s, null)
  527. def testSchemalessDecoder(self):
  528. assert decoder.decode(
  529. ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=univ.SetOf()
  530. ) == (self.s, null)
  531. class SetOfDecoderWithSchemaTestCase(BaseTestCase):
  532. def setUp(self):
  533. BaseTestCase.setUp(self)
  534. self.s = univ.SetOf(componentType=univ.OctetString())
  535. self.s.setComponentByPosition(0, univ.OctetString('quick brown'))
  536. def testDefMode(self):
  537. assert decoder.decode(
  538. ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
  539. ) == (self.s, null)
  540. def testIndefMode(self):
  541. assert decoder.decode(
  542. ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0)), asn1Spec=self.s
  543. ) == (self.s, null)
  544. def testDefModeChunked(self):
  545. assert decoder.decode(
  546. 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
  547. ) == (self.s, null)
  548. def testIndefModeChunked(self):
  549. assert decoder.decode(
  550. 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
  551. ) == (self.s, null)
  552. class SequenceDecoderTestCase(BaseTestCase):
  553. def setUp(self):
  554. BaseTestCase.setUp(self)
  555. self.s = univ.Sequence(
  556. componentType=namedtype.NamedTypes(
  557. namedtype.NamedType('place-holder', univ.Null(null)),
  558. namedtype.NamedType('first-name', univ.OctetString(null)),
  559. namedtype.NamedType('age', univ.Integer(33))
  560. )
  561. )
  562. self.s.setComponentByPosition(0, univ.Null(null))
  563. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  564. self.s.setComponentByPosition(2, univ.Integer(1))
  565. def testWithOptionalAndDefaultedDefMode(self):
  566. assert decoder.decode(
  567. ints2octs((48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
  568. ) == (self.s, null)
  569. def testWithOptionalAndDefaultedIndefMode(self):
  570. assert decoder.decode(
  571. 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))
  572. ) == (self.s, null)
  573. def testWithOptionalAndDefaultedDefModeChunked(self):
  574. assert decoder.decode(
  575. ints2octs(
  576. (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))
  577. ) == (self.s, null)
  578. def testWithOptionalAndDefaultedIndefModeChunked(self):
  579. assert decoder.decode(
  580. 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))
  581. ) == (self.s, null)
  582. def testWithOptionalAndDefaultedDefModeSubst(self):
  583. assert decoder.decode(
  584. ints2octs((48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)),
  585. substrateFun=lambda a, b, c, d: streaming.readFromStream(b, c)
  586. ) == (ints2octs((5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)), str2octs(''))
  587. def testWithOptionalAndDefaultedDefModeSubstV04(self):
  588. assert decoder.decode(
  589. ints2octs((48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)),
  590. substrateFun=lambda a, b, c: (b, b[c:])
  591. ) == (ints2octs((5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)), str2octs(''))
  592. def testWithOptionalAndDefaultedIndefModeSubst(self):
  593. assert decoder.decode(
  594. 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)),
  595. substrateFun=lambda a, b, c, d: streaming.readFromStream(b, c)
  596. ) == (ints2octs(
  597. (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(''))
  598. def testWithOptionalAndDefaultedIndefModeSubstV04(self):
  599. assert decoder.decode(
  600. 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)),
  601. substrateFun=lambda a, b, c: (b, b[c:])
  602. ) == (ints2octs(
  603. (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(''))
  604. def testTagFormat(self):
  605. try:
  606. decoder.decode(
  607. ints2octs((16, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
  608. )
  609. except error.PyAsn1Error:
  610. pass
  611. else:
  612. assert 0, 'wrong tagFormat worked out'
  613. class SequenceDecoderWithSchemaTestCase(BaseTestCase):
  614. def setUp(self):
  615. BaseTestCase.setUp(self)
  616. self.s = univ.Sequence(
  617. componentType=namedtype.NamedTypes(
  618. namedtype.NamedType('place-holder', univ.Null(null)),
  619. namedtype.OptionalNamedType('first-name', univ.OctetString()),
  620. namedtype.DefaultedNamedType('age', univ.Integer(33)),
  621. )
  622. )
  623. def __init(self):
  624. self.s.clear()
  625. self.s.setComponentByPosition(0, univ.Null(null))
  626. def __initWithOptional(self):
  627. self.s.clear()
  628. self.s.setComponentByPosition(0, univ.Null(null))
  629. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  630. def __initWithDefaulted(self):
  631. self.s.clear()
  632. self.s.setComponentByPosition(0, univ.Null(null))
  633. self.s.setComponentByPosition(2, univ.Integer(1))
  634. def __initWithOptionalAndDefaulted(self):
  635. self.s.clear()
  636. self.s.setComponentByPosition(0, univ.Null(null))
  637. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  638. self.s.setComponentByPosition(2, univ.Integer(1))
  639. def testDefMode(self):
  640. self.__init()
  641. assert decoder.decode(
  642. ints2octs((48, 2, 5, 0)), asn1Spec=self.s
  643. ) == (self.s, null)
  644. def testIndefMode(self):
  645. self.__init()
  646. assert decoder.decode(
  647. ints2octs((48, 128, 5, 0, 0, 0)), asn1Spec=self.s
  648. ) == (self.s, null)
  649. def testDefModeChunked(self):
  650. self.__init()
  651. assert decoder.decode(
  652. ints2octs((48, 2, 5, 0)), asn1Spec=self.s
  653. ) == (self.s, null)
  654. def testIndefModeChunked(self):
  655. self.__init()
  656. assert decoder.decode(
  657. ints2octs((48, 128, 5, 0, 0, 0)), asn1Spec=self.s
  658. ) == (self.s, null)
  659. def testWithOptionalDefMode(self):
  660. self.__initWithOptional()
  661. assert decoder.decode(
  662. ints2octs((48, 15, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
  663. ) == (self.s, null)
  664. def testWithOptionaIndefMode(self):
  665. self.__initWithOptional()
  666. assert decoder.decode(
  667. ints2octs((48, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 0, 0)),
  668. asn1Spec=self.s
  669. ) == (self.s, null)
  670. def testWithOptionalDefModeChunked(self):
  671. self.__initWithOptional()
  672. assert decoder.decode(
  673. ints2octs((48, 21, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110)),
  674. asn1Spec=self.s
  675. ) == (self.s, null)
  676. def testWithOptionalIndefModeChunked(self):
  677. self.__initWithOptional()
  678. assert decoder.decode(
  679. 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,
  680. 0, 0, 0)),
  681. asn1Spec=self.s
  682. ) == (self.s, null)
  683. def testWithDefaultedDefMode(self):
  684. self.__initWithDefaulted()
  685. assert decoder.decode(
  686. ints2octs((48, 5, 5, 0, 2, 1, 1)), asn1Spec=self.s
  687. ) == (self.s, null)
  688. def testWithDefaultedIndefMode(self):
  689. self.__initWithDefaulted()
  690. assert decoder.decode(
  691. ints2octs((48, 128, 5, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s
  692. ) == (self.s, null)
  693. def testWithDefaultedDefModeChunked(self):
  694. self.__initWithDefaulted()
  695. assert decoder.decode(
  696. ints2octs((48, 5, 5, 0, 2, 1, 1)), asn1Spec=self.s
  697. ) == (self.s, null)
  698. def testWithDefaultedIndefModeChunked(self):
  699. self.__initWithDefaulted()
  700. assert decoder.decode(
  701. ints2octs((48, 128, 5, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s
  702. ) == (self.s, null)
  703. def testWithOptionalAndDefaultedDefMode(self):
  704. self.__initWithOptionalAndDefaulted()
  705. assert decoder.decode(
  706. ints2octs((48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)),
  707. asn1Spec=self.s
  708. ) == (self.s, null)
  709. def testWithOptionalAndDefaultedIndefMode(self):
  710. self.__initWithOptionalAndDefaulted()
  711. assert decoder.decode(
  712. 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,
  713. 0, 0)), asn1Spec=self.s
  714. ) == (self.s, null)
  715. def testWithOptionalAndDefaultedDefModeChunked(self):
  716. self.__initWithOptionalAndDefaulted()
  717. assert decoder.decode(
  718. ints2octs(
  719. (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)),
  720. asn1Spec=self.s
  721. ) == (self.s, null)
  722. def testWithOptionalAndDefaultedIndefModeChunked(self):
  723. self.__initWithOptionalAndDefaulted()
  724. assert decoder.decode(
  725. 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,
  726. 0, 2, 1, 1, 0, 0)), asn1Spec=self.s
  727. ) == (self.s, null)
  728. class SequenceDecoderWithUntaggedOpenTypesTestCase(BaseTestCase):
  729. def setUp(self):
  730. openType = opentype.OpenType(
  731. 'id',
  732. {1: univ.Integer(),
  733. 2: univ.OctetString()}
  734. )
  735. self.s = univ.Sequence(
  736. componentType=namedtype.NamedTypes(
  737. namedtype.NamedType('id', univ.Integer()),
  738. namedtype.NamedType('blob', univ.Any(), openType=openType)
  739. )
  740. )
  741. def testDecodeOpenTypesChoiceOne(self):
  742. s, r = decoder.decode(
  743. ints2octs((48, 6, 2, 1, 1, 2, 1, 12)), asn1Spec=self.s,
  744. decodeOpenTypes=True
  745. )
  746. assert not r
  747. assert s[0] == 1
  748. assert s[1] == 12
  749. def testDecodeOpenTypesChoiceTwo(self):
  750. s, r = decoder.decode(
  751. ints2octs((48, 16, 2, 1, 2, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s,
  752. decodeOpenTypes=True
  753. )
  754. assert not r
  755. assert s[0] == 2
  756. assert s[1] == univ.OctetString('quick brown')
  757. def testDecodeOpenTypesUnknownType(self):
  758. try:
  759. s, r = decoder.decode(
  760. ints2octs((48, 6, 2, 1, 2, 6, 1, 39)), asn1Spec=self.s,
  761. decodeOpenTypes=True
  762. )
  763. except error.PyAsn1Error:
  764. pass
  765. else:
  766. assert False, 'unknown open type tolerated'
  767. def testDecodeOpenTypesUnknownId(self):
  768. s, r = decoder.decode(
  769. ints2octs((48, 6, 2, 1, 3, 6, 1, 39)), asn1Spec=self.s,
  770. decodeOpenTypes=True
  771. )
  772. assert not r
  773. assert s[0] == 3
  774. assert s[1] == univ.OctetString(hexValue='060127')
  775. def testDontDecodeOpenTypesChoiceOne(self):
  776. s, r = decoder.decode(
  777. ints2octs((48, 6, 2, 1, 1, 2, 1, 12)), asn1Spec=self.s
  778. )
  779. assert not r
  780. assert s[0] == 1
  781. assert s[1] == ints2octs((2, 1, 12))
  782. def testDontDecodeOpenTypesChoiceTwo(self):
  783. s, r = decoder.decode(
  784. ints2octs((48, 16, 2, 1, 2, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
  785. )
  786. assert not r
  787. assert s[0] == 2
  788. assert s[1] == ints2octs((4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  789. class SequenceDecoderWithImplicitlyTaggedOpenTypesTestCase(BaseTestCase):
  790. def setUp(self):
  791. openType = opentype.OpenType(
  792. 'id',
  793. {1: univ.Integer(),
  794. 2: univ.OctetString()}
  795. )
  796. self.s = univ.Sequence(
  797. componentType=namedtype.NamedTypes(
  798. namedtype.NamedType('id', univ.Integer()),
  799. namedtype.NamedType(
  800. 'blob', univ.Any().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType
  801. )
  802. )
  803. )
  804. def testDecodeOpenTypesChoiceOne(self):
  805. s, r = decoder.decode(
  806. ints2octs((48, 8, 2, 1, 1, 131, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
  807. )
  808. assert not r
  809. assert s[0] == 1
  810. assert s[1] == 12
  811. def testDecodeOpenTypesUnknownId(self):
  812. s, r = decoder.decode(
  813. ints2octs((48, 8, 2, 1, 3, 131, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
  814. )
  815. assert not r
  816. assert s[0] == 3
  817. assert s[1] == univ.OctetString(hexValue='02010C')
  818. class SequenceDecoderWithExplicitlyTaggedOpenTypesTestCase(BaseTestCase):
  819. def setUp(self):
  820. openType = opentype.OpenType(
  821. 'id',
  822. {1: univ.Integer(),
  823. 2: univ.OctetString()}
  824. )
  825. self.s = univ.Sequence(
  826. componentType=namedtype.NamedTypes(
  827. namedtype.NamedType('id', univ.Integer()),
  828. namedtype.NamedType(
  829. 'blob', univ.Any().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType
  830. )
  831. )
  832. )
  833. def testDecodeOpenTypesChoiceOne(self):
  834. s, r = decoder.decode(
  835. ints2octs((48, 8, 2, 1, 1, 163, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
  836. )
  837. assert not r
  838. assert s[0] == 1
  839. assert s[1] == 12
  840. def testDecodeOpenTypesUnknownId(self):
  841. s, r = decoder.decode(
  842. ints2octs((48, 8, 2, 1, 3, 163, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
  843. )
  844. assert not r
  845. assert s[0] == 3
  846. assert s[1] == univ.OctetString(hexValue='02010C')
  847. class SequenceDecoderWithUnaggedSetOfOpenTypesTestCase(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('blob', univ.SetOf(componentType=univ.Any()),
  858. openType=openType)
  859. )
  860. )
  861. def testDecodeOpenTypesChoiceOne(self):
  862. s, r = decoder.decode(
  863. ints2octs((48, 8, 2, 1, 1, 49, 3, 2, 1, 12)), asn1Spec=self.s,
  864. decodeOpenTypes=True
  865. )
  866. assert not r
  867. assert s[0] == 1
  868. assert s[1][0] == 12
  869. def testDecodeOpenTypesChoiceTwo(self):
  870. s, r = decoder.decode(
  871. ints2octs((48, 18, 2, 1, 2, 49, 13, 4, 11, 113, 117, 105, 99,
  872. 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s,
  873. decodeOpenTypes=True
  874. )
  875. assert not r
  876. assert s[0] == 2
  877. assert s[1][0] == univ.OctetString('quick brown')
  878. def testDecodeOpenTypesUnknownType(self):
  879. try:
  880. s, r = decoder.decode(
  881. ints2octs((48, 6, 2, 1, 2, 6, 1, 39)), asn1Spec=self.s,
  882. decodeOpenTypes=True
  883. )
  884. except error.PyAsn1Error:
  885. pass
  886. else:
  887. assert False, 'unknown open type tolerated'
  888. def testDecodeOpenTypesUnknownId(self):
  889. s, r = decoder.decode(
  890. ints2octs((48, 8, 2, 1, 3, 49, 3, 2, 1, 12)), asn1Spec=self.s,
  891. decodeOpenTypes=True
  892. )
  893. assert not r
  894. assert s[0] == 3
  895. assert s[1][0] == univ.OctetString(hexValue='02010c')
  896. def testDontDecodeOpenTypesChoiceOne(self):
  897. s, r = decoder.decode(
  898. ints2octs((48, 8, 2, 1, 1, 49, 3, 2, 1, 12)), asn1Spec=self.s
  899. )
  900. assert not r
  901. assert s[0] == 1
  902. assert s[1][0] == ints2octs((2, 1, 12))
  903. def testDontDecodeOpenTypesChoiceTwo(self):
  904. s, r = decoder.decode(
  905. ints2octs((48, 18, 2, 1, 2, 49, 13, 4, 11, 113, 117, 105, 99,
  906. 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
  907. )
  908. assert not r
  909. assert s[0] == 2
  910. assert s[1][0] == ints2octs((4, 11, 113, 117, 105, 99, 107, 32, 98, 114,
  911. 111, 119, 110))
  912. class SequenceDecoderWithImplicitlyTaggedSetOfOpenTypesTestCase(BaseTestCase):
  913. def setUp(self):
  914. openType = opentype.OpenType(
  915. 'id',
  916. {1: univ.Integer(),
  917. 2: univ.OctetString()}
  918. )
  919. self.s = univ.Sequence(
  920. componentType=namedtype.NamedTypes(
  921. namedtype.NamedType('id', univ.Integer()),
  922. namedtype.NamedType(
  923. 'blob', univ.SetOf(
  924. componentType=univ.Any().subtype(
  925. implicitTag=tag.Tag(
  926. tag.tagClassContext, tag.tagFormatSimple, 3))),
  927. openType=openType
  928. )
  929. )
  930. )
  931. def testDecodeOpenTypesChoiceOne(self):
  932. s, r = decoder.decode(
  933. ints2octs((48, 10, 2, 1, 1, 49, 5, 131, 3, 2, 1, 12)),
  934. asn1Spec=self.s, decodeOpenTypes=True
  935. )
  936. assert not r
  937. assert s[0] == 1
  938. assert s[1][0] == 12
  939. def testDecodeOpenTypesUnknownId(self):
  940. s, r = decoder.decode(
  941. ints2octs((48, 10, 2, 1, 3, 49, 5, 131, 3, 2, 1, 12)),
  942. asn1Spec=self.s, decodeOpenTypes=True
  943. )
  944. assert not r
  945. assert s[0] == 3
  946. assert s[1][0] == univ.OctetString(hexValue='02010C')
  947. class SequenceDecoderWithExplicitlyTaggedSetOfOpenTypesTestCase(BaseTestCase):
  948. def setUp(self):
  949. openType = opentype.OpenType(
  950. 'id',
  951. {1: univ.Integer(),
  952. 2: univ.OctetString()}
  953. )
  954. self.s = univ.Sequence(
  955. componentType=namedtype.NamedTypes(
  956. namedtype.NamedType('id', univ.Integer()),
  957. namedtype.NamedType(
  958. 'blob', univ.SetOf(
  959. componentType=univ.Any().subtype(
  960. explicitTag=tag.Tag(
  961. tag.tagClassContext, tag.tagFormatSimple, 3))),
  962. openType=openType
  963. )
  964. )
  965. )
  966. def testDecodeOpenTypesChoiceOne(self):
  967. s, r = decoder.decode(
  968. ints2octs((48, 10, 2, 1, 1, 49, 5, 131, 3, 2, 1, 12)),
  969. asn1Spec=self.s, decodeOpenTypes=True
  970. )
  971. assert not r
  972. assert s[0] == 1
  973. assert s[1][0] == 12
  974. def testDecodeOpenTypesUnknownId(self):
  975. s, r = decoder.decode(
  976. ints2octs( (48, 10, 2, 1, 3, 49, 5, 131, 3, 2, 1, 12)),
  977. asn1Spec=self.s, decodeOpenTypes=True
  978. )
  979. assert not r
  980. assert s[0] == 3
  981. assert s[1][0] == univ.OctetString(hexValue='02010C')
  982. class SetDecoderTestCase(BaseTestCase):
  983. def setUp(self):
  984. BaseTestCase.setUp(self)
  985. self.s = univ.Set(
  986. componentType=namedtype.NamedTypes(
  987. namedtype.NamedType('place-holder', univ.Null(null)),
  988. namedtype.NamedType('first-name', univ.OctetString(null)),
  989. namedtype.NamedType('age', univ.Integer(33))
  990. )
  991. )
  992. self.s.setComponentByPosition(0, univ.Null(null))
  993. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  994. self.s.setComponentByPosition(2, univ.Integer(1))
  995. def testWithOptionalAndDefaultedDefMode(self):
  996. assert decoder.decode(
  997. ints2octs((49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
  998. ) == (self.s, null)
  999. def testWithOptionalAndDefaultedIndefMode(self):
  1000. assert decoder.decode(
  1001. 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))
  1002. ) == (self.s, null)
  1003. def testWithOptionalAndDefaultedDefModeChunked(self):
  1004. assert decoder.decode(
  1005. ints2octs(
  1006. (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))
  1007. ) == (self.s, null)
  1008. def testWithOptionalAndDefaultedIndefModeChunked(self):
  1009. assert decoder.decode(
  1010. 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))
  1011. ) == (self.s, null)
  1012. def testWithOptionalAndDefaultedDefModeSubst(self):
  1013. assert decoder.decode(
  1014. ints2octs((49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)),
  1015. substrateFun=lambda a, b, c, d: streaming.readFromStream(b, c)
  1016. ) == (ints2octs((5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)), str2octs(''))
  1017. def testWithOptionalAndDefaultedDefModeSubstV04(self):
  1018. assert decoder.decode(
  1019. ints2octs((49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)),
  1020. substrateFun=lambda a, b, c: (b, b[c:])
  1021. ) == (ints2octs((5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)), str2octs(''))
  1022. def testWithOptionalAndDefaultedIndefModeSubst(self):
  1023. assert decoder.decode(
  1024. 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)),
  1025. substrateFun=lambda a, b, c, d: streaming.readFromStream(b, c)
  1026. ) == (ints2octs(
  1027. (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(''))
  1028. def testWithOptionalAndDefaultedIndefModeSubstV04(self):
  1029. assert decoder.decode(
  1030. 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)),
  1031. substrateFun=lambda a, b, c: (b, b[c:])
  1032. ) == (ints2octs(
  1033. (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(''))
  1034. def testTagFormat(self):
  1035. try:
  1036. decoder.decode(
  1037. ints2octs((16, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
  1038. )
  1039. except error.PyAsn1Error:
  1040. pass
  1041. else:
  1042. assert 0, 'wrong tagFormat worked out'
  1043. class SetDecoderWithSchemaTestCase(BaseTestCase):
  1044. def setUp(self):
  1045. BaseTestCase.setUp(self)
  1046. self.s = univ.Set(
  1047. componentType=namedtype.NamedTypes(
  1048. namedtype.NamedType('place-holder', univ.Null(null)),
  1049. namedtype.OptionalNamedType('first-name', univ.OctetString()),
  1050. namedtype.DefaultedNamedType('age', univ.Integer(33)),
  1051. )
  1052. )
  1053. def __init(self):
  1054. self.s.clear()
  1055. self.s.setComponentByPosition(0, univ.Null(null))
  1056. def __initWithOptional(self):
  1057. self.s.clear()
  1058. self.s.setComponentByPosition(0, univ.Null(null))
  1059. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  1060. def __initWithDefaulted(self):
  1061. self.s.clear()
  1062. self.s.setComponentByPosition(0, univ.Null(null))
  1063. self.s.setComponentByPosition(2, univ.Integer(1))
  1064. def __initWithOptionalAndDefaulted(self):
  1065. self.s.clear()
  1066. self.s.setComponentByPosition(0, univ.Null(null))
  1067. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  1068. self.s.setComponentByPosition(2, univ.Integer(1))
  1069. def testDefMode(self):
  1070. self.__init()
  1071. assert decoder.decode(
  1072. ints2octs((49, 128, 5, 0, 0, 0)), asn1Spec=self.s
  1073. ) == (self.s, null)
  1074. def testIndefMode(self):
  1075. self.__init()
  1076. assert decoder.decode(
  1077. ints2octs((49, 128, 5, 0, 0, 0)), asn1Spec=self.s
  1078. ) == (self.s, null)
  1079. def testDefModeChunked(self):
  1080. self.__init()
  1081. assert decoder.decode(
  1082. ints2octs((49, 2, 5, 0)), asn1Spec=self.s
  1083. ) == (self.s, null)
  1084. def testIndefModeChunked(self):
  1085. self.__init()
  1086. assert decoder.decode(
  1087. ints2octs((49, 128, 5, 0, 0, 0)), asn1Spec=self.s
  1088. ) == (self.s, null)
  1089. def testWithOptionalDefMode(self):
  1090. self.__initWithOptional()
  1091. assert decoder.decode(
  1092. ints2octs((49, 15, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
  1093. ) == (self.s, null)
  1094. def testWithOptionalIndefMode(self):
  1095. self.__initWithOptional()
  1096. assert decoder.decode(
  1097. 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
  1098. ) == (self.s, null)
  1099. def testWithOptionalDefModeChunked(self):
  1100. self.__initWithOptional()
  1101. assert decoder.decode(
  1102. 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
  1103. ) == (self.s, null)
  1104. def testWithOptionalIndefModeChunked(self):
  1105. self.__initWithOptional()
  1106. assert decoder.decode(
  1107. 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
  1108. ) == (self.s, null)
  1109. def testWithDefaultedDefMode(self):
  1110. self.__initWithDefaulted()
  1111. assert decoder.decode(
  1112. ints2octs((49, 5, 5, 0, 2, 1, 1)), asn1Spec=self.s
  1113. ) == (self.s, null)
  1114. def testWithDefaultedIndefMode(self):
  1115. self.__initWithDefaulted()
  1116. assert decoder.decode(
  1117. ints2octs((49, 128, 5, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s
  1118. ) == (self.s, null)
  1119. def testWithDefaultedDefModeChunked(self):
  1120. self.__initWithDefaulted()
  1121. assert decoder.decode(
  1122. ints2octs((49, 5, 5, 0, 2, 1, 1)), asn1Spec=self.s
  1123. ) == (self.s, null)
  1124. def testWithDefaultedIndefModeChunked(self):
  1125. self.__initWithDefaulted()
  1126. assert decoder.decode(
  1127. ints2octs((49, 128, 5, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s
  1128. ) == (self.s, null)
  1129. def testWithOptionalAndDefaultedDefMode(self):
  1130. self.__initWithOptionalAndDefaulted()
  1131. assert decoder.decode(
  1132. ints2octs((49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)), asn1Spec=self.s
  1133. ) == (self.s, null)
  1134. def testWithOptionalAndDefaultedDefModeReordered(self):
  1135. self.__initWithOptionalAndDefaulted()
  1136. assert decoder.decode(
  1137. ints2octs((49, 18, 2, 1, 1, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0)), asn1Spec=self.s
  1138. ) == (self.s, null)
  1139. def testWithOptionalAndDefaultedIndefMode(self):
  1140. self.__initWithOptionalAndDefaulted()
  1141. assert decoder.decode(
  1142. 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
  1143. ) == (self.s, null)
  1144. def testWithOptionalAndDefaultedIndefModeReordered(self):
  1145. self.__initWithOptionalAndDefaulted()
  1146. assert decoder.decode(
  1147. 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
  1148. ) == (self.s, null)
  1149. def testWithOptionalAndDefaultedDefModeChunked(self):
  1150. self.__initWithOptionalAndDefaulted()
  1151. assert decoder.decode(
  1152. 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
  1153. ) == (self.s, null)
  1154. def testWithOptionalAndDefaultedIndefModeChunked(self):
  1155. self.__initWithOptionalAndDefaulted()
  1156. assert decoder.decode(
  1157. 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
  1158. ) == (self.s, null)
  1159. class SequenceOfWithExpTaggedOctetStringDecoder(BaseTestCase):
  1160. def setUp(self):
  1161. BaseTestCase.setUp(self)
  1162. self.s = univ.SequenceOf(
  1163. componentType=univ.OctetString().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3))
  1164. )
  1165. self.s.setComponentByPosition(0, 'q')
  1166. self.s2 = univ.SequenceOf()
  1167. def testDefModeSchema(self):
  1168. s, r = decoder.decode(ints2octs((48, 5, 163, 3, 4, 1, 113)), asn1Spec=self.s)
  1169. assert not r
  1170. assert s == self.s
  1171. assert s.tagSet == self.s.tagSet
  1172. def testIndefModeSchema(self):
  1173. s, r = decoder.decode(ints2octs((48, 128, 163, 128, 4, 1, 113, 0, 0, 0, 0)), asn1Spec=self.s)
  1174. assert not r
  1175. assert s == self.s
  1176. assert s.tagSet == self.s.tagSet
  1177. def testDefModeNoComponent(self):
  1178. s, r = decoder.decode(ints2octs((48, 5, 163, 3, 4, 1, 113)), asn1Spec=self.s2)
  1179. assert not r
  1180. assert s == self.s
  1181. assert s.tagSet == self.s.tagSet
  1182. def testIndefModeNoComponent(self):
  1183. s, r = decoder.decode(ints2octs((48, 128, 163, 128, 4, 1, 113, 0, 0, 0, 0)), asn1Spec=self.s2)
  1184. assert not r
  1185. assert s == self.s
  1186. assert s.tagSet == self.s.tagSet
  1187. def testDefModeSchemaless(self):
  1188. s, r = decoder.decode(ints2octs((48, 5, 163, 3, 4, 1, 113)))
  1189. assert not r
  1190. assert s == self.s
  1191. assert s.tagSet == self.s.tagSet
  1192. def testIndefModeSchemaless(self):
  1193. s, r = decoder.decode(ints2octs((48, 128, 163, 128, 4, 1, 113, 0, 0, 0, 0)))
  1194. assert not r
  1195. assert s == self.s
  1196. assert s.tagSet == self.s.tagSet
  1197. class SequenceWithExpTaggedOctetStringDecoder(BaseTestCase):
  1198. def setUp(self):
  1199. BaseTestCase.setUp(self)
  1200. self.s = univ.Sequence(
  1201. componentType=namedtype.NamedTypes(
  1202. namedtype.NamedType(
  1203. 'x', univ.OctetString().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3))
  1204. )
  1205. )
  1206. )
  1207. self.s.setComponentByPosition(0, 'q')
  1208. self.s2 = univ.Sequence()
  1209. def testDefModeSchema(self):
  1210. s, r = decoder.decode(ints2octs((48, 5, 163, 3, 4, 1, 113)), asn1Spec=self.s)
  1211. assert not r
  1212. assert s == self.s
  1213. assert s.tagSet == self.s.tagSet
  1214. def testIndefModeSchema(self):
  1215. s, r = decoder.decode(ints2octs((48, 128, 163, 128, 4, 1, 113, 0, 0, 0, 0)), asn1Spec=self.s)
  1216. assert not r
  1217. assert s == self.s
  1218. assert s.tagSet == self.s.tagSet
  1219. def testDefModeNoComponent(self):
  1220. s, r = decoder.decode(ints2octs((48, 5, 163, 3, 4, 1, 113)), asn1Spec=self.s2)
  1221. assert not r
  1222. assert s == self.s
  1223. assert s.tagSet == self.s.tagSet
  1224. def testIndefModeNoComponent(self):
  1225. s, r = decoder.decode(ints2octs((48, 128, 163, 128, 4, 1, 113, 0, 0, 0, 0)), asn1Spec=self.s2)
  1226. assert not r
  1227. assert s == self.s
  1228. assert s.tagSet == self.s.tagSet
  1229. def testDefModeSchemaless(self):
  1230. s, r = decoder.decode(ints2octs((48, 5, 163, 3, 4, 1, 113)))
  1231. assert not r
  1232. assert s == self.s
  1233. assert s.tagSet == self.s.tagSet
  1234. def testIndefModeSchemaless(self):
  1235. s, r = decoder.decode(ints2octs((48, 128, 163, 128, 4, 1, 113, 0, 0, 0, 0)))
  1236. assert not r
  1237. assert s == self.s
  1238. assert s.tagSet == self.s.tagSet
  1239. class ChoiceDecoderTestCase(BaseTestCase):
  1240. def setUp(self):
  1241. BaseTestCase.setUp(self)
  1242. self.s = univ.Choice(
  1243. componentType=namedtype.NamedTypes(
  1244. namedtype.NamedType('place-holder', univ.Null(null)),
  1245. namedtype.NamedType('number', univ.Integer(0)),
  1246. namedtype.NamedType('string', univ.OctetString())
  1247. )
  1248. )
  1249. def testBySpec(self):
  1250. self.s.setComponentByPosition(0, univ.Null(null))
  1251. assert decoder.decode(
  1252. ints2octs((5, 0)), asn1Spec=self.s
  1253. ) == (self.s, null)
  1254. def testWithoutSpec(self):
  1255. self.s.setComponentByPosition(0, univ.Null(null))
  1256. assert decoder.decode(ints2octs((5, 0))) == (self.s, null)
  1257. assert decoder.decode(ints2octs((5, 0))) == (univ.Null(null), null)
  1258. def testUndefLength(self):
  1259. self.s.setComponentByPosition(2, univ.OctetString('abcdefgh'))
  1260. assert decoder.decode(ints2octs((36, 128, 4, 3, 97, 98, 99, 4, 3, 100, 101, 102, 4, 2, 103, 104, 0, 0)),
  1261. asn1Spec=self.s) == (self.s, null)
  1262. def testExplicitTag(self):
  1263. s = self.s.subtype(explicitTag=tag.Tag(tag.tagClassContext,
  1264. tag.tagFormatConstructed, 4))
  1265. s.setComponentByPosition(0, univ.Null(null))
  1266. assert decoder.decode(ints2octs((164, 2, 5, 0)), asn1Spec=s) == (s, null)
  1267. def testExplicitTagUndefLength(self):
  1268. s = self.s.subtype(explicitTag=tag.Tag(tag.tagClassContext,
  1269. tag.tagFormatConstructed, 4))
  1270. s.setComponentByPosition(0, univ.Null(null))
  1271. assert decoder.decode(ints2octs((164, 128, 5, 0, 0, 0)), asn1Spec=s) == (s, null)
  1272. class AnyDecoderTestCase(BaseTestCase):
  1273. def setUp(self):
  1274. BaseTestCase.setUp(self)
  1275. self.s = univ.Any()
  1276. def testByUntagged(self):
  1277. assert decoder.decode(
  1278. ints2octs((4, 3, 102, 111, 120)), asn1Spec=self.s
  1279. ) == (univ.Any('\004\003fox'), null)
  1280. def testTaggedEx(self):
  1281. s = univ.Any('\004\003fox').subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))
  1282. assert decoder.decode(ints2octs((164, 5, 4, 3, 102, 111, 120)), asn1Spec=s) == (s, null)
  1283. def testTaggedIm(self):
  1284. s = univ.Any('\004\003fox').subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))
  1285. assert decoder.decode(ints2octs((132, 5, 4, 3, 102, 111, 120)), asn1Spec=s) == (s, null)
  1286. def testByUntaggedIndefMode(self):
  1287. assert decoder.decode(
  1288. ints2octs((4, 3, 102, 111, 120)), asn1Spec=self.s
  1289. ) == (univ.Any('\004\003fox'), null)
  1290. def testTaggedExIndefMode(self):
  1291. s = univ.Any('\004\003fox').subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))
  1292. assert decoder.decode(ints2octs((164, 128, 4, 3, 102, 111, 120, 0, 0)), asn1Spec=s) == (s, null)
  1293. def testTaggedImIndefMode(self):
  1294. s = univ.Any('\004\003fox').subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))
  1295. assert decoder.decode(ints2octs((164, 128, 4, 3, 102, 111, 120, 0, 0)), asn1Spec=s) == (s, null)
  1296. def testByUntaggedSubst(self):
  1297. assert decoder.decode(
  1298. ints2octs((4, 3, 102, 111, 120)),
  1299. asn1Spec=self.s,
  1300. substrateFun=lambda a, b, c, d: streaming.readFromStream(b, c)
  1301. ) == (ints2octs((4, 3, 102, 111, 120)), str2octs(''))
  1302. def testByUntaggedSubstV04(self):
  1303. assert decoder.decode(
  1304. ints2octs((4, 3, 102, 111, 120)),
  1305. asn1Spec=self.s,
  1306. substrateFun=lambda a, b, c: (b, b[c:])
  1307. ) == (ints2octs((4, 3, 102, 111, 120)), str2octs(''))
  1308. def testTaggedExSubst(self):
  1309. assert decoder.decode(
  1310. ints2octs((164, 5, 4, 3, 102, 111, 120)),
  1311. asn1Spec=self.s,
  1312. substrateFun=lambda a, b, c, d: streaming.readFromStream(b, c)
  1313. ) == (ints2octs((164, 5, 4, 3, 102, 111, 120)), str2octs(''))
  1314. def testTaggedExSubstV04(self):
  1315. assert decoder.decode(
  1316. ints2octs((164, 5, 4, 3, 102, 111, 120)),
  1317. asn1Spec=self.s,
  1318. substrateFun=lambda a, b, c: (b, b[c:])
  1319. ) == (ints2octs((164, 5, 4, 3, 102, 111, 120)), str2octs(''))
  1320. class EndOfOctetsTestCase(BaseTestCase):
  1321. def testUnexpectedEoo(self):
  1322. try:
  1323. decoder.decode(ints2octs((0, 0)))
  1324. except error.PyAsn1Error:
  1325. pass
  1326. else:
  1327. assert 0, 'end-of-contents octets accepted at top level'
  1328. def testExpectedEoo(self):
  1329. result, remainder = decoder.decode(ints2octs((0, 0)), allowEoo=True)
  1330. assert eoo.endOfOctets.isSameTypeWith(result) and result == eoo.endOfOctets and result is eoo.endOfOctets
  1331. assert remainder == null
  1332. def testDefiniteNoEoo(self):
  1333. try:
  1334. decoder.decode(ints2octs((0x23, 0x02, 0x00, 0x00)))
  1335. except error.PyAsn1Error:
  1336. pass
  1337. else:
  1338. assert 0, 'end-of-contents octets accepted inside definite-length encoding'
  1339. def testIndefiniteEoo(self):
  1340. result, remainder = decoder.decode(ints2octs((0x23, 0x80, 0x00, 0x00)))
  1341. assert result == () and remainder == null, 'incorrect decoding of indefinite length end-of-octets'
  1342. def testNoLongFormEoo(self):
  1343. try:
  1344. decoder.decode(ints2octs((0x23, 0x80, 0x00, 0x81, 0x00)))
  1345. except error.PyAsn1Error:
  1346. pass
  1347. else:
  1348. assert 0, 'end-of-contents octets accepted with invalid long-form length'
  1349. def testNoConstructedEoo(self):
  1350. try:
  1351. decoder.decode(ints2octs((0x23, 0x80, 0x20, 0x00)))
  1352. except error.PyAsn1Error:
  1353. pass
  1354. else:
  1355. assert 0, 'end-of-contents octets accepted with invalid constructed encoding'
  1356. def testNoEooData(self):
  1357. try:
  1358. decoder.decode(ints2octs((0x23, 0x80, 0x00, 0x01, 0x00)))
  1359. except error.PyAsn1Error:
  1360. pass
  1361. else:
  1362. assert 0, 'end-of-contents octets accepted with unexpected data'
  1363. class NonStringDecoderTestCase(BaseTestCase):
  1364. def setUp(self):
  1365. BaseTestCase.setUp(self)
  1366. self.s = univ.Sequence(
  1367. componentType=namedtype.NamedTypes(
  1368. namedtype.NamedType('place-holder', univ.Null(null)),
  1369. namedtype.NamedType('first-name', univ.OctetString(null)),
  1370. namedtype.NamedType('age', univ.Integer(33))
  1371. )
  1372. )
  1373. self.s.setComponentByPosition(0, univ.Null(null))
  1374. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  1375. self.s.setComponentByPosition(2, univ.Integer(1))
  1376. self.substrate = ints2octs([48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1])
  1377. def testOctetString(self):
  1378. s = list(decoder.StreamingDecoder(
  1379. univ.OctetString(self.substrate), asn1Spec=self.s))
  1380. assert [self.s] == s
  1381. def testAny(self):
  1382. s = list(decoder.StreamingDecoder(
  1383. univ.Any(self.substrate), asn1Spec=self.s))
  1384. assert [self.s] == s
  1385. class ErrorOnDecodingTestCase(BaseTestCase):
  1386. def testErrorCondition(self):
  1387. decode = decoder.SingleItemDecoder(
  1388. tagMap=decoder.TAG_MAP, typeMap=decoder.TYPE_MAP)
  1389. substrate = ints2octs((00, 1, 2))
  1390. stream = streaming.asSeekableStream(substrate)
  1391. try:
  1392. asn1Object = next(decode(stream))
  1393. except error.PyAsn1Error:
  1394. exc = sys.exc_info()[1]
  1395. assert isinstance(exc, error.PyAsn1Error), (
  1396. 'Unexpected exception raised %r' % (exc,))
  1397. else:
  1398. assert False, 'Unexpected decoder result %r' % (asn1Object,)
  1399. def testRawDump(self):
  1400. substrate = ints2octs((31, 8, 2, 1, 1, 131, 3, 2, 1, 12))
  1401. stream = streaming.asSeekableStream(substrate)
  1402. class SingleItemEncoder(decoder.SingleItemDecoder):
  1403. defaultErrorState = decoder.stDumpRawValue
  1404. class StreamingDecoder(decoder.StreamingDecoder):
  1405. SINGLE_ITEM_DECODER = SingleItemEncoder
  1406. class OneShotDecoder(decoder.Decoder):
  1407. STREAMING_DECODER = StreamingDecoder
  1408. d = OneShotDecoder()
  1409. asn1Object, rest = d(stream)
  1410. assert isinstance(asn1Object, univ.Any), (
  1411. 'Unexpected raw dump type %r' % (asn1Object,))
  1412. assert asn1Object.asNumbers() == (31, 8, 2, 1, 1), (
  1413. 'Unexpected raw dump value %r' % (asn1Object,))
  1414. assert rest == ints2octs((131, 3, 2, 1, 12)), (
  1415. 'Unexpected rest of substrate after raw dump %r' % rest)
  1416. @unittest.skipIf(sys.version_info < (3,), "Unsupported on Python 2")
  1417. class BinaryFileTestCase(BaseTestCase):
  1418. """Assure that decode works on open binary files."""
  1419. def testOneObject(self):
  1420. _, path = tempfile.mkstemp()
  1421. try:
  1422. with open(path, "wb") as out:
  1423. out.write(ints2octs((2, 1, 12)))
  1424. with open(path, "rb") as source:
  1425. values = list(decoder.StreamingDecoder(source))
  1426. assert values == [12]
  1427. finally:
  1428. os.remove(path)
  1429. def testMoreObjects(self):
  1430. _, path = tempfile.mkstemp()
  1431. try:
  1432. with open(path, "wb") as out:
  1433. out.write(ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)))
  1434. with open(path, "rb") as source:
  1435. values = list(decoder.StreamingDecoder(source))
  1436. assert values == [12, (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1)]
  1437. finally:
  1438. os.remove(path)
  1439. def testInvalidFileContent(self):
  1440. _, path = tempfile.mkstemp()
  1441. try:
  1442. with open(path, "wb") as out:
  1443. out.write(ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0, 7)))
  1444. with open(path, "rb") as source:
  1445. list(decoder.StreamingDecoder(source))
  1446. except error.EndOfStreamError:
  1447. pass
  1448. finally:
  1449. os.remove(path)
  1450. class BytesIOTestCase(BaseTestCase):
  1451. def testRead(self):
  1452. source = ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0))
  1453. stream = io.BytesIO(source)
  1454. values = list(decoder.StreamingDecoder(stream))
  1455. assert values == [12, (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1)]
  1456. class UnicodeTestCase(BaseTestCase):
  1457. def testFail(self):
  1458. # This ensures that unicode objects in Python 2 & str objects in Python 3.7 cannot be parsed.
  1459. source = ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)).decode("latin-1")
  1460. try:
  1461. next(decoder.StreamingDecoder(source))
  1462. except error.UnsupportedSubstrateError:
  1463. pass
  1464. else:
  1465. assert False, 'Tolerated parsing broken unicode strings'
  1466. class RestartableDecoderTestCase(BaseTestCase):
  1467. class NonBlockingStream(io.BytesIO):
  1468. block = False
  1469. def read(self, size=-1):
  1470. self.block = not self.block
  1471. if self.block:
  1472. return # this is what non-blocking streams sometimes do
  1473. return io.BytesIO.read(self, size)
  1474. def setUp(self):
  1475. BaseTestCase.setUp(self)
  1476. self.s = univ.SequenceOf(componentType=univ.OctetString())
  1477. self.s.setComponentByPosition(0, univ.OctetString('quick brown'))
  1478. source = ints2octs(
  1479. (48, 26,
  1480. 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110,
  1481. 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  1482. self.stream = self.NonBlockingStream(source)
  1483. def testPartialReadingFromNonBlockingStream(self):
  1484. iterator = iter(decoder.StreamingDecoder(self.stream, asn1Spec=self.s))
  1485. res = next(iterator)
  1486. assert isinstance(res, error.SubstrateUnderrunError)
  1487. assert 'asn1Object' not in res.context
  1488. res = next(iterator)
  1489. assert isinstance(res, error.SubstrateUnderrunError)
  1490. assert 'asn1Object' not in res.context
  1491. res = next(iterator)
  1492. assert isinstance(res, error.SubstrateUnderrunError)
  1493. assert 'asn1Object' in res.context
  1494. assert isinstance(res.context['asn1Object'], univ.SequenceOf)
  1495. assert res.context['asn1Object'].isValue
  1496. assert len(res.context['asn1Object']) == 0
  1497. res = next(iterator)
  1498. assert isinstance(res, error.SubstrateUnderrunError)
  1499. assert 'asn1Object' in res.context
  1500. assert isinstance(res.context['asn1Object'], univ.SequenceOf)
  1501. assert res.context['asn1Object'].isValue
  1502. assert len(res.context['asn1Object']) == 0
  1503. res = next(iterator)
  1504. assert isinstance(res, error.SubstrateUnderrunError)
  1505. assert 'asn1Object' in res.context
  1506. assert isinstance(res.context['asn1Object'], univ.SequenceOf)
  1507. assert res.context['asn1Object'].isValue
  1508. assert len(res.context['asn1Object']) == 0
  1509. res = next(iterator)
  1510. assert isinstance(res, error.SubstrateUnderrunError)
  1511. assert 'asn1Object' in res.context
  1512. assert isinstance(res.context['asn1Object'], univ.SequenceOf)
  1513. assert res.context['asn1Object'].isValue
  1514. assert len(res.context['asn1Object']) == 1
  1515. res = next(iterator)
  1516. assert isinstance(res, error.SubstrateUnderrunError)
  1517. assert 'asn1Object' in res.context
  1518. assert isinstance(res.context['asn1Object'], univ.SequenceOf)
  1519. assert res.context['asn1Object'].isValue
  1520. assert len(res.context['asn1Object']) == 1
  1521. res = next(iterator)
  1522. assert isinstance(res, error.SubstrateUnderrunError)
  1523. assert 'asn1Object' in res.context
  1524. assert isinstance(res.context['asn1Object'], univ.SequenceOf)
  1525. assert res.context['asn1Object'].isValue
  1526. assert len(res.context['asn1Object']) == 1
  1527. res = next(iterator)
  1528. assert isinstance(res, univ.SequenceOf)
  1529. assert res.isValue
  1530. assert len(res) == 2
  1531. try:
  1532. next(iterator)
  1533. except StopIteration:
  1534. pass
  1535. else:
  1536. assert False, 'End of stream not raised'
  1537. class CompressedFilesTestCase(BaseTestCase):
  1538. def testGzip(self):
  1539. _, path = tempfile.mkstemp(suffix=".gz")
  1540. try:
  1541. with gzip.open(path, "wb") as out:
  1542. out.write(ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)))
  1543. with gzip.open(path, "rb") as source:
  1544. values = list(decoder.StreamingDecoder(source))
  1545. assert values == [12, (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1)]
  1546. finally:
  1547. os.remove(path)
  1548. def testZipfile(self):
  1549. # File from ZIP archive is a good example of non-seekable stream in Python 2.7
  1550. # In Python 3.7, it is a seekable stream.
  1551. _, path = tempfile.mkstemp(suffix=".zip")
  1552. try:
  1553. with zipfile.ZipFile(path, "w") as myzip:
  1554. myzip.writestr("data", ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)))
  1555. with zipfile.ZipFile(path, "r") as myzip:
  1556. with myzip.open("data", "r") as source:
  1557. values = list(decoder.StreamingDecoder(source))
  1558. assert values == [12, (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1)]
  1559. finally:
  1560. os.remove(path)
  1561. def testZipfileMany(self):
  1562. _, path = tempfile.mkstemp(suffix=".zip")
  1563. try:
  1564. with zipfile.ZipFile(path, "w") as myzip:
  1565. #for i in range(100):
  1566. myzip.writestr("data", ints2octs((2, 1, 12, 35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)) * 1000)
  1567. with zipfile.ZipFile(path, "r") as myzip:
  1568. with myzip.open("data", "r") as source:
  1569. values = list(decoder.StreamingDecoder(source))
  1570. assert values == [12, (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1)] * 1000
  1571. finally:
  1572. os.remove(path)
  1573. class NonStreamingCompatibilityTestCase(BaseTestCase):
  1574. def setUp(self):
  1575. from pyasn1 import debug
  1576. BaseTestCase.setUp(self)
  1577. debug.setLogger(None) # undo logger setup from BaseTestCase to work around unrelated issue
  1578. def testPartialDecodeWithCustomSubstrateFun(self):
  1579. snmp_req_substrate = ints2octs((
  1580. 0x30, 0x22, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0, 0x15, 0x02, 0x04, 0x69,
  1581. 0x30, 0xdb, 0xeb, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x07, 0x30, 0x05, 0x06, 0x01, 0x01, 0x05, 0x00))
  1582. seq, next_substrate = decoder.decode(
  1583. snmp_req_substrate, asn1Spec=univ.Sequence(),
  1584. recursiveFlag=False, substrateFun=lambda a, b, c: (a, b[:c])
  1585. )
  1586. assert seq.isSameTypeWith(univ.Sequence)
  1587. assert next_substrate == snmp_req_substrate[2:]
  1588. version, next_substrate = decoder.decode(
  1589. next_substrate, asn1Spec=univ.Integer(), recursiveFlag=False,
  1590. substrateFun=lambda a, b, c: (a, b[:c])
  1591. )
  1592. assert version == 1
  1593. def testPartialDecodeWithDefaultSubstrateFun(self):
  1594. substrate = ints2octs((
  1595. 0x04, 0x0e, 0x30, 0x0c, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x3c, 0x03, 0x02
  1596. ))
  1597. result, rest = decoder.decode(substrate, recursiveFlag=False)
  1598. assert result.isSameTypeWith(univ.OctetString)
  1599. assert rest == substrate[2:]
  1600. def testPropagateUserException(self):
  1601. substrate = io.BytesIO(ints2octs((0x04, 0x00)))
  1602. def userSubstrateFun(_asn1Object, _substrate, _length, _options):
  1603. raise TypeError("error inside user function")
  1604. try:
  1605. decoder.decode(substrate, asn1Spec=univ.OctetString, substrateFun=userSubstrateFun)
  1606. except TypeError as exc:
  1607. assert str(exc) == "error inside user function"
  1608. else:
  1609. raise AssertionError("decode() must not hide TypeError from inside user provided callback")
  1610. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  1611. if __name__ == '__main__':
  1612. unittest.TextTestRunner(verbosity=2).run(suite)