test_encoder.py 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  1. #
  2. # This file is part of pyasn1 software.
  3. #
  4. # Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com>
  5. # License: https://pyasn1.readthedocs.io/en/latest/license.html
  6. #
  7. import sys
  8. import unittest
  9. from __tests__.base import BaseTestCase
  10. from pyasn1.type import tag
  11. from pyasn1.type import namedtype
  12. from pyasn1.type import opentype
  13. from pyasn1.type import univ
  14. from pyasn1.type import char
  15. from pyasn1.codec.ber import encoder
  16. from pyasn1.compat.octets import ints2octs
  17. from pyasn1.error import PyAsn1Error
  18. class LargeTagEncoderTestCase(BaseTestCase):
  19. def setUp(self):
  20. BaseTestCase.setUp(self)
  21. self.o = univ.Integer().subtype(
  22. value=1, explicitTag=tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 0xdeadbeaf)
  23. )
  24. def testEncoder(self):
  25. assert encoder.encode(self.o) == ints2octs((127, 141, 245, 182, 253, 47, 3, 2, 1, 1))
  26. class IntegerEncoderTestCase(BaseTestCase):
  27. def testPosInt(self):
  28. assert encoder.encode(univ.Integer(12)) == ints2octs((2, 1, 12))
  29. def testNegInt(self):
  30. assert encoder.encode(univ.Integer(-12)) == ints2octs((2, 1, 244))
  31. def testZero(self):
  32. assert encoder.encode(univ.Integer(0)) == ints2octs((2, 1, 0))
  33. def testCompactZero(self):
  34. encoder.IntegerEncoder.supportCompactZero = True
  35. substrate = encoder.encode(univ.Integer(0))
  36. encoder.IntegerEncoder.supportCompactZero = False
  37. assert substrate == ints2octs((2, 0))
  38. def testMinusOne(self):
  39. assert encoder.encode(univ.Integer(-1)) == ints2octs((2, 1, 255))
  40. def testPosLong(self):
  41. assert encoder.encode(
  42. univ.Integer(0xffffffffffffffff)
  43. ) == ints2octs((2, 9, 0, 255, 255, 255, 255, 255, 255, 255, 255))
  44. def testNegLong(self):
  45. assert encoder.encode(
  46. univ.Integer(-0xffffffffffffffff)
  47. ) == ints2octs((2, 9, 255, 0, 0, 0, 0, 0, 0, 0, 1))
  48. class IntegerEncoderWithSchemaTestCase(BaseTestCase):
  49. def testPosInt(self):
  50. assert encoder.encode(12, asn1Spec=univ.Integer()) == ints2octs((2, 1, 12))
  51. def testNegInt(self):
  52. assert encoder.encode(-12, asn1Spec=univ.Integer()) == ints2octs((2, 1, 244))
  53. def testZero(self):
  54. assert encoder.encode(0, asn1Spec=univ.Integer()) == ints2octs((2, 1, 0))
  55. def testPosLong(self):
  56. assert encoder.encode(
  57. 0xffffffffffffffff, asn1Spec=univ.Integer()
  58. ) == ints2octs((2, 9, 0, 255, 255, 255, 255, 255, 255, 255, 255))
  59. class BooleanEncoderTestCase(BaseTestCase):
  60. def testTrue(self):
  61. assert encoder.encode(univ.Boolean(1)) == ints2octs((1, 1, 1))
  62. def testFalse(self):
  63. assert encoder.encode(univ.Boolean(0)) == ints2octs((1, 1, 0))
  64. class BooleanEncoderWithSchemaTestCase(BaseTestCase):
  65. def testTrue(self):
  66. assert encoder.encode(True, asn1Spec=univ.Boolean()) == ints2octs((1, 1, 1))
  67. def testFalse(self):
  68. assert encoder.encode(False, asn1Spec=univ.Boolean()) == ints2octs((1, 1, 0))
  69. class BitStringEncoderTestCase(BaseTestCase):
  70. def setUp(self):
  71. BaseTestCase.setUp(self)
  72. self.b = univ.BitString((1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1))
  73. def testDefMode(self):
  74. assert encoder.encode(self.b) == ints2octs((3, 3, 1, 169, 138))
  75. def testIndefMode(self):
  76. assert encoder.encode(
  77. self.b, defMode=False
  78. ) == ints2octs((3, 3, 1, 169, 138))
  79. def testDefModeChunked(self):
  80. assert encoder.encode(
  81. self.b, maxChunkSize=1
  82. ) == ints2octs((35, 8, 3, 2, 0, 169, 3, 2, 1, 138))
  83. def testIndefModeChunked(self):
  84. assert encoder.encode(
  85. self.b, defMode=False, maxChunkSize=1
  86. ) == ints2octs((35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0))
  87. def testEmptyValue(self):
  88. assert encoder.encode(univ.BitString([])) == ints2octs((3, 1, 0))
  89. class BitStringEncoderWithSchemaTestCase(BaseTestCase):
  90. def setUp(self):
  91. BaseTestCase.setUp(self)
  92. self.b = (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1)
  93. self.s = univ.BitString()
  94. def testDefMode(self):
  95. assert encoder.encode(self.b, asn1Spec=self.s) == ints2octs((3, 3, 1, 169, 138))
  96. def testIndefMode(self):
  97. assert encoder.encode(
  98. self.b, asn1Spec=self.s, defMode=False
  99. ) == ints2octs((3, 3, 1, 169, 138))
  100. def testDefModeChunked(self):
  101. assert encoder.encode(
  102. self.b, asn1Spec=self.s, maxChunkSize=1
  103. ) == ints2octs((35, 8, 3, 2, 0, 169, 3, 2, 1, 138))
  104. def testIndefModeChunked(self):
  105. assert encoder.encode(
  106. self.b, asn1Spec=self.s, defMode=False, maxChunkSize=1
  107. ) == ints2octs((35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0))
  108. def testEmptyValue(self):
  109. assert encoder.encode([], asn1Spec=self.s) == ints2octs((3, 1, 0))
  110. class OctetStringEncoderTestCase(BaseTestCase):
  111. def setUp(self):
  112. BaseTestCase.setUp(self)
  113. self.o = univ.OctetString('Quick brown fox')
  114. def testDefMode(self):
  115. assert encoder.encode(self.o) == ints2octs(
  116. (4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120))
  117. def testIndefMode(self):
  118. assert encoder.encode(
  119. self.o, defMode=False
  120. ) == ints2octs((4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120))
  121. def testDefModeChunked(self):
  122. assert encoder.encode(
  123. self.o, maxChunkSize=4
  124. ) == ints2octs((36, 23, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119,
  125. 110, 32, 4, 3, 102, 111, 120))
  126. def testIndefModeChunked(self):
  127. assert encoder.encode(
  128. self.o, defMode=False, maxChunkSize=4
  129. ) == ints2octs((36, 128, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110,
  130. 32, 4, 3, 102, 111, 120, 0, 0))
  131. class OctetStringEncoderWithSchemaTestCase(BaseTestCase):
  132. def setUp(self):
  133. BaseTestCase.setUp(self)
  134. self.s = univ.OctetString()
  135. self.o = 'Quick brown fox'
  136. def testDefMode(self):
  137. assert encoder.encode(self.o, asn1Spec=self.s) == ints2octs(
  138. (4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120))
  139. def testIndefMode(self):
  140. assert encoder.encode(
  141. self.o, asn1Spec=self.s, defMode=False
  142. ) == ints2octs((4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120))
  143. def testDefModeChunked(self):
  144. assert encoder.encode(
  145. self.o, asn1Spec=self.s, maxChunkSize=4
  146. ) == ints2octs((36, 23, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119,
  147. 110, 32, 4, 3, 102, 111, 120))
  148. def testIndefModeChunked(self):
  149. assert encoder.encode(
  150. self.o, asn1Spec=self.s, defMode=False, maxChunkSize=4
  151. ) == ints2octs((36, 128, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110,
  152. 32, 4, 3, 102, 111, 120, 0, 0))
  153. class ExpTaggedOctetStringEncoderTestCase(BaseTestCase):
  154. def setUp(self):
  155. BaseTestCase.setUp(self)
  156. self.o = univ.OctetString().subtype(
  157. value='Quick brown fox',
  158. explicitTag=tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 5)
  159. )
  160. def testDefMode(self):
  161. assert encoder.encode(self.o) == ints2octs(
  162. (101, 17, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120))
  163. def testIndefMode(self):
  164. assert encoder.encode(
  165. self.o, defMode=False
  166. ) == ints2octs((101, 128, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 0, 0))
  167. def testDefModeChunked(self):
  168. assert encoder.encode(
  169. self.o, defMode=True, maxChunkSize=4
  170. ) == 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,
  171. 102, 111, 120))
  172. def testIndefModeChunked(self):
  173. assert encoder.encode(
  174. self.o, defMode=False, maxChunkSize=4
  175. ) == 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))
  176. class NullEncoderTestCase(BaseTestCase):
  177. def testNull(self):
  178. assert encoder.encode(univ.Null('')) == ints2octs((5, 0))
  179. class NullEncoderWithSchemaTestCase(BaseTestCase):
  180. def testNull(self):
  181. assert encoder.encode(None, univ.Null()) == ints2octs((5, 0))
  182. class ObjectIdentifierEncoderTestCase(BaseTestCase):
  183. def testOne(self):
  184. assert encoder.encode(
  185. univ.ObjectIdentifier((1, 3, 6, 0, 0xffffe))
  186. ) == ints2octs((6, 6, 43, 6, 0, 191, 255, 126))
  187. def testEdge1(self):
  188. assert encoder.encode(
  189. univ.ObjectIdentifier((0, 39))
  190. ) == ints2octs((6, 1, 39))
  191. def testEdge2(self):
  192. assert encoder.encode(
  193. univ.ObjectIdentifier((1, 39))
  194. ) == ints2octs((6, 1, 79))
  195. def testEdge3(self):
  196. # 01111111
  197. assert encoder.encode(
  198. univ.ObjectIdentifier((2, 40))
  199. ) == ints2octs((6, 1, 120))
  200. def testEdge4(self):
  201. # 10010000|10000000|10000000|10000000|01001111
  202. assert encoder.encode(
  203. univ.ObjectIdentifier((2, 0xffffffff))
  204. ) == ints2octs((6, 5, 0x90, 0x80, 0x80, 0x80, 0x4F))
  205. def testEdge5(self):
  206. # 01111111
  207. assert encoder.encode(
  208. univ.ObjectIdentifier((2, 47))
  209. ) == ints2octs((6, 1, 0x7F))
  210. def testEdge6(self):
  211. # 10000001|00000000
  212. assert encoder.encode(
  213. univ.ObjectIdentifier((2, 48))
  214. ) == ints2octs((6, 2, 0x81, 0x00))
  215. def testEdge7(self):
  216. # 10000001|00110100|00000003
  217. assert encoder.encode(
  218. univ.ObjectIdentifier((2, 100, 3))
  219. ) == ints2octs((6, 3, 0x81, 0x34, 0x03))
  220. def testEdge8(self):
  221. # 10000101|00000000
  222. assert encoder.encode(
  223. univ.ObjectIdentifier((2, 560))
  224. ) == ints2octs((6, 2, 133, 0))
  225. def testEdge9(self):
  226. # 10001000|10000100|10000111|0000010
  227. assert encoder.encode(
  228. univ.ObjectIdentifier((2, 16843570))
  229. ) == ints2octs((6, 4, 0x88, 0x84, 0x87, 0x02))
  230. def testEdgeA(self):
  231. assert encoder.encode(
  232. univ.ObjectIdentifier((2, 5))
  233. ) == ints2octs((6, 1, 85))
  234. def testImpossible1(self):
  235. try:
  236. encoder.encode(univ.ObjectIdentifier((3, 1, 2)))
  237. except PyAsn1Error:
  238. pass
  239. else:
  240. assert 0, 'impossible leading arc tolerated'
  241. def testImpossible2(self):
  242. try:
  243. encoder.encode(univ.ObjectIdentifier((0,)))
  244. except PyAsn1Error:
  245. pass
  246. else:
  247. assert 0, 'single arc OID tolerated'
  248. def testImpossible3(self):
  249. try:
  250. encoder.encode(univ.ObjectIdentifier((0, 40)))
  251. except PyAsn1Error:
  252. pass
  253. else:
  254. assert 0, 'second arc overflow tolerated'
  255. def testImpossible4(self):
  256. try:
  257. encoder.encode(univ.ObjectIdentifier((1, 40)))
  258. except PyAsn1Error:
  259. pass
  260. else:
  261. assert 0, 'second arc overflow tolerated'
  262. def testLarge1(self):
  263. assert encoder.encode(
  264. univ.ObjectIdentifier((2, 18446744073709551535184467440737095))
  265. ) == ints2octs((0x06, 0x11, 0x83, 0xC6, 0xDF, 0xD4, 0xCC, 0xB3, 0xFF, 0xFF, 0xFE, 0xF0, 0xB8, 0xD6, 0xB8, 0xCB,
  266. 0xE2, 0xB7, 0x17))
  267. def testLarge2(self):
  268. assert encoder.encode(
  269. univ.ObjectIdentifier((2, 999, 18446744073709551535184467440737095))
  270. ) == ints2octs((0x06, 0x13, 0x88, 0x37, 0x83, 0xC6, 0xDF, 0xD4, 0xCC, 0xB3, 0xFF, 0xFF, 0xFE, 0xF0, 0xB8, 0xD6,
  271. 0xB8, 0xCB, 0xE2, 0xB6, 0x47))
  272. class ObjectIdentifierWithSchemaEncoderTestCase(BaseTestCase):
  273. def testOne(self):
  274. assert encoder.encode(
  275. (1, 3, 6, 0, 0xffffe), asn1Spec=univ.ObjectIdentifier()
  276. ) == ints2octs((6, 6, 43, 6, 0, 191, 255, 126))
  277. class RealEncoderTestCase(BaseTestCase):
  278. def testChar(self):
  279. assert encoder.encode(
  280. univ.Real((123, 10, 11))
  281. ) == ints2octs((9, 7, 3, 49, 50, 51, 69, 49, 49))
  282. def testBin1(self):
  283. assert encoder.encode( # default binEncBase = 2
  284. univ.Real((0.5, 2, 0)) # check encbase = 2 and exponent = -1
  285. ) == ints2octs((9, 3, 128, 255, 1))
  286. def testBin2(self):
  287. r = univ.Real((3.25, 2, 0))
  288. r.binEncBase = 8 # change binEncBase only for this instance of Real
  289. assert encoder.encode(
  290. r # check encbase = 8
  291. ) == ints2octs((9, 3, 148, 255, 13))
  292. def testBin3(self):
  293. # change binEncBase in the RealEncoder instance => for all further Real
  294. binEncBase, encoder.TYPE_MAP[univ.Real.typeId].binEncBase = encoder.TYPE_MAP[univ.Real.typeId].binEncBase, 16
  295. assert encoder.encode(
  296. univ.Real((0.00390625, 2, 0)) # check encbase = 16
  297. ) == ints2octs((9, 3, 160, 254, 1))
  298. encoder.TYPE_MAP[univ.Real.typeId].binEncBase = binEncBase
  299. def testBin4(self):
  300. # choose binEncBase automatically for all further Real (testBin[4-7])
  301. binEncBase, encoder.TYPE_MAP[univ.Real.typeId].binEncBase = encoder.TYPE_MAP[univ.Real.typeId].binEncBase, None
  302. assert encoder.encode(
  303. univ.Real((1, 2, 0)) # check exponent = 0
  304. ) == ints2octs((9, 3, 128, 0, 1))
  305. encoder.TYPE_MAP[univ.Real.typeId].binEncBase = binEncBase
  306. def testBin5(self):
  307. assert encoder.encode(
  308. univ.Real((3, 2, -1020)) # case of 2 octs for exponent and
  309. # negative exponent and abs(exponent) is
  310. # all 1's and fills the whole octet(s)
  311. ) == ints2octs((9, 4, 129, 252, 4, 3))
  312. def testBin6(self):
  313. assert encoder.encode(
  314. univ.Real((1, 2, 262140)) # case of 3 octs for exponent and
  315. # check that first 9 bits for exponent
  316. # are not all 1's
  317. ) == ints2octs((9, 5, 130, 3, 255, 252, 1))
  318. def testBin7(self):
  319. assert encoder.encode(
  320. univ.Real((-1, 2, 76354972)) # case of >3 octs for exponent and
  321. # mantissa < 0
  322. ) == ints2octs((9, 7, 195, 4, 4, 141, 21, 156, 1))
  323. def testPlusInf(self):
  324. assert encoder.encode(univ.Real('inf')) == ints2octs((9, 1, 64))
  325. def testMinusInf(self):
  326. assert encoder.encode(univ.Real('-inf')) == ints2octs((9, 1, 65))
  327. def testZero(self):
  328. assert encoder.encode(univ.Real(0)) == ints2octs((9, 0))
  329. class RealEncoderWithSchemaTestCase(BaseTestCase):
  330. def testChar(self):
  331. assert encoder.encode(
  332. (123, 10, 11), asn1Spec=univ.Real()
  333. ) == ints2octs((9, 7, 3, 49, 50, 51, 69, 49, 49))
  334. class UniversalStringEncoderTestCase(BaseTestCase):
  335. def testEncoding(self):
  336. assert encoder.encode(char.UniversalString(sys.version_info[0] >= 3 and 'abc' or unicode('abc'))) == ints2octs(
  337. (28, 12, 0, 0, 0, 97, 0, 0, 0, 98, 0, 0, 0, 99)), 'Incorrect encoding'
  338. class UniversalStringEncoderWithSchemaTestCase(BaseTestCase):
  339. def testEncoding(self):
  340. assert encoder.encode(
  341. sys.version_info[0] >= 3 and 'abc' or unicode('abc'), asn1Spec=char.UniversalString()
  342. ) == ints2octs((28, 12, 0, 0, 0, 97, 0, 0, 0, 98, 0, 0, 0, 99)), 'Incorrect encoding'
  343. class BMPStringEncoderTestCase(BaseTestCase):
  344. def testEncoding(self):
  345. assert encoder.encode(char.BMPString(sys.version_info[0] >= 3 and 'abc' or unicode('abc'))) == ints2octs(
  346. (30, 6, 0, 97, 0, 98, 0, 99)), 'Incorrect encoding'
  347. class BMPStringEncoderWithSchemaTestCase(BaseTestCase):
  348. def testEncoding(self):
  349. assert encoder.encode(
  350. sys.version_info[0] >= 3 and 'abc' or unicode('abc'), asn1Spec=char.BMPString()
  351. ) == ints2octs((30, 6, 0, 97, 0, 98, 0, 99)), 'Incorrect encoding'
  352. class UTF8StringEncoderTestCase(BaseTestCase):
  353. def testEncoding(self):
  354. assert encoder.encode(char.UTF8String(sys.version_info[0] >= 3 and 'abc' or unicode('abc'))) == ints2octs(
  355. (12, 3, 97, 98, 99)), 'Incorrect encoding'
  356. class UTF8StringEncoderWithSchemaTestCase(BaseTestCase):
  357. def testEncoding(self):
  358. assert encoder.encode(
  359. sys.version_info[0] >= 3 and 'abc' or unicode('abc'), asn1Spec=char.UTF8String()
  360. ) == ints2octs((12, 3, 97, 98, 99)), 'Incorrect encoding'
  361. class SequenceOfEncoderTestCase(BaseTestCase):
  362. def testEmpty(self):
  363. s = univ.SequenceOf()
  364. s.clear()
  365. assert encoder.encode(s) == ints2octs((48, 0))
  366. def testDefMode(self):
  367. s = univ.SequenceOf()
  368. s.setComponentByPosition(0, univ.OctetString('quick brown'))
  369. assert encoder.encode(s) == ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  370. def testIndefMode(self):
  371. s = univ.SequenceOf()
  372. s.setComponentByPosition(0, univ.OctetString('quick brown'))
  373. assert encoder.encode(
  374. s, defMode=False
  375. ) == ints2octs((48, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
  376. def testDefModeChunked(self):
  377. s = univ.SequenceOf()
  378. s.setComponentByPosition(0, univ.OctetString('quick brown'))
  379. assert encoder.encode(
  380. s, defMode=True, maxChunkSize=4
  381. ) == ints2octs((48, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
  382. def testIndefModeChunked(self):
  383. s = univ.SequenceOf()
  384. s.setComponentByPosition(0, univ.OctetString('quick brown'))
  385. assert encoder.encode(
  386. s, defMode=False, maxChunkSize=4
  387. ) == 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))
  388. class SequenceOfEncoderWithSchemaTestCase(BaseTestCase):
  389. def setUp(self):
  390. BaseTestCase.setUp(self)
  391. self.s = univ.SequenceOf(componentType=univ.OctetString())
  392. self.v = ['quick brown']
  393. def testEmpty(self):
  394. assert encoder.encode([], asn1Spec=self.s) == ints2octs((48, 0))
  395. def testDefMode(self):
  396. assert encoder.encode(
  397. self.v, asn1Spec=self.s
  398. ) == ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  399. def testIndefMode(self):
  400. assert encoder.encode(
  401. self.v, asn1Spec=self.s, defMode=False
  402. ) == ints2octs((48, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
  403. def testDefModeChunked(self):
  404. assert encoder.encode(
  405. self.v, asn1Spec=self.s, defMode=True, maxChunkSize=4
  406. ) == ints2octs((48, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
  407. def testIndefModeChunked(self):
  408. assert encoder.encode(
  409. self.v, asn1Spec=self.s, defMode=False, maxChunkSize=4
  410. ) == 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))
  411. class SequenceOfEncoderWithComponentsSchemaTestCase(BaseTestCase):
  412. def setUp(self):
  413. BaseTestCase.setUp(self)
  414. self.s = univ.SequenceOf(componentType=univ.OctetString())
  415. def __init(self):
  416. self.s.clear()
  417. self.s.setComponentByPosition(0, 'quick brown')
  418. def testDefMode(self):
  419. self.__init()
  420. assert encoder.encode(self.s) == ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  421. def testIndefMode(self):
  422. self.__init()
  423. assert encoder.encode(
  424. self.s, defMode=False
  425. ) == ints2octs((48, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
  426. def testDefModeChunked(self):
  427. self.__init()
  428. assert encoder.encode(
  429. self.s, defMode=True, maxChunkSize=4
  430. ) == ints2octs((48, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
  431. def testIndefModeChunked(self):
  432. self.__init()
  433. assert encoder.encode(
  434. self.s, defMode=False, maxChunkSize=4
  435. ) == 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))
  436. class SetOfEncoderTestCase(BaseTestCase):
  437. def testEmpty(self):
  438. s = univ.SetOf()
  439. s.clear()
  440. assert encoder.encode(s) == ints2octs((49, 0))
  441. def testDefMode(self):
  442. s = univ.SetOf()
  443. s.setComponentByPosition(0, univ.OctetString('quick brown'))
  444. assert encoder.encode(s) == ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  445. def testIndefMode(self):
  446. s = univ.SetOf()
  447. s.setComponentByPosition(0, univ.OctetString('quick brown'))
  448. assert encoder.encode(
  449. s, defMode=False
  450. ) == ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
  451. def testDefModeChunked(self):
  452. s = univ.SetOf()
  453. s.setComponentByPosition(0, univ.OctetString('quick brown'))
  454. assert encoder.encode(
  455. s, defMode=True, maxChunkSize=4
  456. ) == ints2octs((49, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
  457. def testIndefModeChunked(self):
  458. s = univ.SetOf()
  459. s.setComponentByPosition(0, univ.OctetString('quick brown'))
  460. assert encoder.encode(
  461. s, defMode=False, maxChunkSize=4
  462. ) == 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))
  463. class SetOfEncoderWithSchemaTestCase(BaseTestCase):
  464. def setUp(self):
  465. BaseTestCase.setUp(self)
  466. self.s = univ.SetOf(componentType=univ.OctetString())
  467. self.v = ['quick brown']
  468. def testEmpty(self):
  469. s = univ.SetOf()
  470. assert encoder.encode([], asn1Spec=self.s) == ints2octs((49, 0))
  471. def testDefMode(self):
  472. assert encoder.encode(
  473. self.v, asn1Spec=self.s
  474. ) == ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  475. def testIndefMode(self):
  476. assert encoder.encode(
  477. self.v, asn1Spec=self.s, defMode=False
  478. ) == ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
  479. def testDefModeChunked(self):
  480. assert encoder.encode(
  481. self.v, asn1Spec=self.s, defMode=True, maxChunkSize=4
  482. ) == ints2octs((49, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
  483. def testIndefModeChunked(self):
  484. assert encoder.encode(
  485. self.v, asn1Spec=self.s, defMode=False, maxChunkSize=4
  486. ) == ints2octs(
  487. (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))
  488. class SetOfEncoderWithComponentsSchemaTestCase(BaseTestCase):
  489. def setUp(self):
  490. BaseTestCase.setUp(self)
  491. self.s = univ.SetOf(componentType=univ.OctetString())
  492. def __init(self):
  493. self.s.clear()
  494. self.s.setComponentByPosition(0, 'quick brown')
  495. def testDefMode(self):
  496. self.__init()
  497. assert encoder.encode(self.s) == ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  498. def testIndefMode(self):
  499. self.__init()
  500. assert encoder.encode(
  501. self.s, defMode=False
  502. ) == ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
  503. def testDefModeChunked(self):
  504. self.__init()
  505. assert encoder.encode(
  506. self.s, defMode=True, maxChunkSize=4
  507. ) == ints2octs((49, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
  508. def testIndefModeChunked(self):
  509. self.__init()
  510. assert encoder.encode(
  511. self.s, defMode=False, maxChunkSize=4
  512. ) == 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))
  513. class SequenceEncoderTestCase(BaseTestCase):
  514. def setUp(self):
  515. BaseTestCase.setUp(self)
  516. self.s = univ.Sequence()
  517. self.s.setComponentByPosition(0, univ.Null(''))
  518. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  519. self.s.setComponentByPosition(2, univ.Integer(1))
  520. def testDefMode(self):
  521. assert encoder.encode(self.s) == ints2octs((48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
  522. def testIndefMode(self):
  523. assert encoder.encode(
  524. self.s, defMode=False
  525. ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
  526. def testDefModeChunked(self):
  527. assert encoder.encode(
  528. self.s, defMode=True, maxChunkSize=4
  529. ) == ints2octs((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))
  530. def testIndefModeChunked(self):
  531. assert encoder.encode(
  532. self.s, defMode=False, maxChunkSize=4
  533. ) == 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))
  534. class SequenceEncoderWithSchemaTestCase(BaseTestCase):
  535. def setUp(self):
  536. BaseTestCase.setUp(self)
  537. self.s = univ.Sequence(
  538. componentType=namedtype.NamedTypes(
  539. namedtype.NamedType('place-holder', univ.Null()),
  540. namedtype.OptionalNamedType('first-name', univ.OctetString()),
  541. namedtype.DefaultedNamedType('age', univ.Integer(33)),
  542. )
  543. )
  544. self.v = {
  545. 'place-holder': None,
  546. 'first-name': 'quick brown',
  547. 'age': 1
  548. }
  549. def testEmpty(self):
  550. try:
  551. assert encoder.encode({}, asn1Spec=self.s)
  552. except PyAsn1Error:
  553. pass
  554. else:
  555. assert False, 'empty bare sequence tolerated'
  556. def testDefMode(self):
  557. assert encoder.encode(
  558. self.v, asn1Spec=self.s
  559. ) == ints2octs((48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
  560. def testIndefMode(self):
  561. assert encoder.encode(
  562. self.v, asn1Spec=self.s, defMode=False
  563. ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
  564. def testDefModeChunked(self):
  565. assert encoder.encode(
  566. self.v, asn1Spec=self.s, defMode=True, maxChunkSize=4
  567. ) == ints2octs((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))
  568. def testIndefModeChunked(self):
  569. assert encoder.encode(
  570. self.v, asn1Spec=self.s, defMode=False, maxChunkSize=4
  571. ) == 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))
  572. class SequenceEncoderWithUntaggedOpenTypesTestCase(BaseTestCase):
  573. def setUp(self):
  574. BaseTestCase.setUp(self)
  575. openType = opentype.OpenType(
  576. 'id',
  577. {1: univ.Integer(),
  578. 2: univ.OctetString()}
  579. )
  580. self.s = univ.Sequence(
  581. componentType=namedtype.NamedTypes(
  582. namedtype.NamedType('id', univ.Integer()),
  583. namedtype.NamedType('blob', univ.Any(), openType=openType)
  584. )
  585. )
  586. def testEncodeOpenTypeChoiceOne(self):
  587. self.s.clear()
  588. self.s[0] = 1
  589. self.s[1] = univ.Integer(12)
  590. assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
  591. (48, 5, 2, 1, 1, 49, 50)
  592. )
  593. def testEncodeOpenTypeChoiceTwo(self):
  594. self.s.clear()
  595. self.s[0] = 2
  596. self.s[1] = univ.OctetString('quick brown')
  597. assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
  598. (48, 14, 2, 1, 2, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)
  599. )
  600. def testEncodeOpenTypeUnknownId(self):
  601. self.s.clear()
  602. self.s[0] = 2
  603. self.s[1] = univ.ObjectIdentifier('1.3.6')
  604. try:
  605. encoder.encode(self.s, asn1Spec=self.s)
  606. except PyAsn1Error:
  607. assert False, 'incompatible open type tolerated'
  608. def testEncodeOpenTypeIncompatibleType(self):
  609. self.s.clear()
  610. self.s[0] = 2
  611. self.s[1] = univ.ObjectIdentifier('1.3.6')
  612. try:
  613. encoder.encode(self.s, asn1Spec=self.s)
  614. except PyAsn1Error:
  615. assert False, 'incompatible open type tolerated'
  616. class SequenceEncoderWithImplicitlyTaggedOpenTypesTestCase(BaseTestCase):
  617. def setUp(self):
  618. BaseTestCase.setUp(self)
  619. openType = opentype.OpenType(
  620. 'id',
  621. {1: univ.Integer(),
  622. 2: univ.OctetString()}
  623. )
  624. self.s = univ.Sequence(
  625. componentType=namedtype.NamedTypes(
  626. namedtype.NamedType('id', univ.Integer()),
  627. namedtype.NamedType('blob', univ.Any().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType)
  628. )
  629. )
  630. def testEncodeOpenTypeChoiceOne(self):
  631. self.s.clear()
  632. self.s[0] = 1
  633. self.s[1] = univ.Integer(12)
  634. assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
  635. (48, 9, 2, 1, 1, 131, 4, 131, 2, 49, 50)
  636. )
  637. class SequenceEncoderWithExplicitlyTaggedOpenTypesTestCase(BaseTestCase):
  638. def setUp(self):
  639. BaseTestCase.setUp(self)
  640. openType = opentype.OpenType(
  641. 'id',
  642. {1: univ.Integer(),
  643. 2: univ.OctetString()}
  644. )
  645. self.s = univ.Sequence(
  646. componentType=namedtype.NamedTypes(
  647. namedtype.NamedType('id', univ.Integer()),
  648. namedtype.NamedType('blob', univ.Any().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType)
  649. )
  650. )
  651. def testEncodeOpenTypeChoiceOne(self):
  652. self.s.clear()
  653. self.s[0] = 1
  654. self.s[1] = univ.Integer(12)
  655. assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
  656. (48, 9, 2, 1, 1, 163, 4, 163, 2, 49, 50)
  657. )
  658. class SequenceEncoderWithUntaggedSetOfOpenTypesTestCase(BaseTestCase):
  659. def setUp(self):
  660. BaseTestCase.setUp(self)
  661. openType = opentype.OpenType(
  662. 'id',
  663. {1: univ.Integer(),
  664. 2: univ.OctetString()}
  665. )
  666. self.s = univ.Sequence(
  667. componentType=namedtype.NamedTypes(
  668. namedtype.NamedType('id', univ.Integer()),
  669. namedtype.NamedType('blob', univ.SetOf(
  670. componentType=univ.Any()), openType=openType)
  671. )
  672. )
  673. def testEncodeOpenTypeChoiceOne(self):
  674. self.s.clear()
  675. self.s[0] = 1
  676. self.s[1].append(univ.Integer(12))
  677. assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
  678. (48, 7, 2, 1, 1, 49, 2, 49, 50)
  679. )
  680. def testEncodeOpenTypeChoiceTwo(self):
  681. self.s.clear()
  682. self.s[0] = 2
  683. self.s[1].append(univ.OctetString('quick brown'))
  684. assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
  685. (48, 16, 2, 1, 2, 49, 11, 113, 117, 105, 99, 107, 32, 98, 114,
  686. 111, 119, 110)
  687. )
  688. def testEncodeOpenTypeUnknownId(self):
  689. self.s.clear()
  690. self.s[0] = 2
  691. self.s[1].append(univ.ObjectIdentifier('1.3.6'))
  692. try:
  693. encoder.encode(self.s, asn1Spec=self.s)
  694. except PyAsn1Error:
  695. assert False, 'incompatible open type tolerated'
  696. def testEncodeOpenTypeIncompatibleType(self):
  697. self.s.clear()
  698. self.s[0] = 2
  699. self.s[1].append(univ.ObjectIdentifier('1.3.6'))
  700. try:
  701. encoder.encode(self.s, asn1Spec=self.s)
  702. except PyAsn1Error:
  703. assert False, 'incompatible open type tolerated'
  704. class SequenceEncoderWithImplicitlyTaggedSetOfOpenTypesTestCase(BaseTestCase):
  705. def setUp(self):
  706. BaseTestCase.setUp(self)
  707. openType = opentype.OpenType(
  708. 'id',
  709. {1: univ.Integer(),
  710. 2: univ.OctetString()}
  711. )
  712. self.s = univ.Sequence(
  713. componentType=namedtype.NamedTypes(
  714. namedtype.NamedType('id', univ.Integer()),
  715. namedtype.NamedType('blob', univ.SetOf(
  716. componentType=univ.Any().subtype(
  717. implicitTag=tag.Tag(
  718. tag.tagClassContext, tag.tagFormatSimple, 3))),
  719. openType=openType)
  720. )
  721. )
  722. def testEncodeOpenTypeChoiceOne(self):
  723. self.s.clear()
  724. self.s[0] = 1
  725. self.s[1].append(univ.Integer(12))
  726. assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
  727. (48, 11, 2, 1, 1, 49, 6, 131, 4, 131, 2, 49, 50)
  728. )
  729. class SequenceEncoderWithExplicitlyTaggedSetOfOpenTypesTestCase(BaseTestCase):
  730. def setUp(self):
  731. BaseTestCase.setUp(self)
  732. openType = opentype.OpenType(
  733. 'id',
  734. {1: univ.Integer(),
  735. 2: univ.OctetString()}
  736. )
  737. self.s = univ.Sequence(
  738. componentType=namedtype.NamedTypes(
  739. namedtype.NamedType('id', univ.Integer()),
  740. namedtype.NamedType('blob', univ.SetOf(
  741. componentType=univ.Any().subtype(
  742. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3))),
  743. openType=openType)
  744. )
  745. )
  746. def testEncodeOpenTypeChoiceOne(self):
  747. self.s.clear()
  748. self.s[0] = 1
  749. self.s[1].append(univ.Integer(12))
  750. assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
  751. (48, 11, 2, 1, 1, 49, 6, 163, 4, 163, 2, 49, 50)
  752. )
  753. class SequenceEncoderWithComponentsSchemaTestCase(BaseTestCase):
  754. def setUp(self):
  755. BaseTestCase.setUp(self)
  756. self.s = univ.Sequence(
  757. componentType=namedtype.NamedTypes(
  758. namedtype.NamedType('place-holder', univ.Null()),
  759. namedtype.OptionalNamedType('first-name', univ.OctetString()),
  760. namedtype.DefaultedNamedType('age', univ.Integer(33)),
  761. )
  762. )
  763. def __init(self):
  764. self.s.clear()
  765. self.s.setComponentByPosition(0, '')
  766. def __initWithOptional(self):
  767. self.s.clear()
  768. self.s.setComponentByPosition(0, '')
  769. self.s.setComponentByPosition(1, 'quick brown')
  770. def __initWithDefaulted(self):
  771. self.s.clear()
  772. self.s.setComponentByPosition(0, '')
  773. self.s.setComponentByPosition(2, 1)
  774. def __initWithOptionalAndDefaulted(self):
  775. self.s.clear()
  776. self.s.setComponentByPosition(0, univ.Null(''))
  777. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  778. self.s.setComponentByPosition(2, univ.Integer(1))
  779. def testDefMode(self):
  780. self.__init()
  781. assert encoder.encode(self.s) == ints2octs((48, 2, 5, 0))
  782. def testIndefMode(self):
  783. self.__init()
  784. assert encoder.encode(
  785. self.s, defMode=False
  786. ) == ints2octs((48, 128, 5, 0, 0, 0))
  787. def testDefModeChunked(self):
  788. self.__init()
  789. assert encoder.encode(
  790. self.s, defMode=True, maxChunkSize=4
  791. ) == ints2octs((48, 2, 5, 0))
  792. def testIndefModeChunked(self):
  793. self.__init()
  794. assert encoder.encode(
  795. self.s, defMode=False, maxChunkSize=4
  796. ) == ints2octs((48, 128, 5, 0, 0, 0))
  797. def testWithOptionalDefMode(self):
  798. self.__initWithOptional()
  799. assert encoder.encode(self.s) == ints2octs(
  800. (48, 15, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  801. def testWithOptionalIndefMode(self):
  802. self.__initWithOptional()
  803. assert encoder.encode(
  804. self.s, defMode=False
  805. ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
  806. def testWithOptionalDefModeChunked(self):
  807. self.__initWithOptional()
  808. assert encoder.encode(
  809. self.s, defMode=True, maxChunkSize=4
  810. ) == ints2octs((48, 21, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
  811. def testWithOptionalIndefModeChunked(self):
  812. self.__initWithOptional()
  813. assert encoder.encode(
  814. self.s, defMode=False, maxChunkSize=4
  815. ) == ints2octs(
  816. (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, 0, 0))
  817. def testWithDefaultedDefMode(self):
  818. self.__initWithDefaulted()
  819. assert encoder.encode(self.s) == ints2octs((48, 5, 5, 0, 2, 1, 1))
  820. def testWithDefaultedIndefMode(self):
  821. self.__initWithDefaulted()
  822. assert encoder.encode(
  823. self.s, defMode=False
  824. ) == ints2octs((48, 128, 5, 0, 2, 1, 1, 0, 0))
  825. def testWithDefaultedDefModeChunked(self):
  826. self.__initWithDefaulted()
  827. assert encoder.encode(
  828. self.s, defMode=True, maxChunkSize=4
  829. ) == ints2octs((48, 5, 5, 0, 2, 1, 1))
  830. def testWithDefaultedIndefModeChunked(self):
  831. self.__initWithDefaulted()
  832. assert encoder.encode(
  833. self.s, defMode=False, maxChunkSize=4
  834. ) == ints2octs((48, 128, 5, 0, 2, 1, 1, 0, 0))
  835. def testWithOptionalAndDefaultedDefMode(self):
  836. self.__initWithOptionalAndDefaulted()
  837. assert encoder.encode(self.s) == ints2octs(
  838. (48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
  839. def testWithOptionalAndDefaultedIndefMode(self):
  840. self.__initWithOptionalAndDefaulted()
  841. assert encoder.encode(
  842. self.s, defMode=False
  843. ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
  844. def testWithOptionalAndDefaultedDefModeChunked(self):
  845. self.__initWithOptionalAndDefaulted()
  846. assert encoder.encode(
  847. self.s, defMode=True, maxChunkSize=4
  848. ) == ints2octs(
  849. (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))
  850. def testWithOptionalAndDefaultedIndefModeChunked(self):
  851. self.__initWithOptionalAndDefaulted()
  852. assert encoder.encode(
  853. self.s, defMode=False, maxChunkSize=4
  854. ) == 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,
  855. 0, 2, 1, 1, 0, 0))
  856. class ExpTaggedSequenceEncoderTestCase(BaseTestCase):
  857. def setUp(self):
  858. BaseTestCase.setUp(self)
  859. s = univ.Sequence(
  860. componentType=namedtype.NamedTypes(
  861. namedtype.NamedType('number', univ.Integer()),
  862. )
  863. )
  864. s = s.subtype(
  865. explicitTag=tag.Tag(tag.tagClassApplication, tag.tagFormatConstructed, 5)
  866. )
  867. s[0] = 12
  868. self.s = s
  869. def testDefMode(self):
  870. assert encoder.encode(self.s) == ints2octs((101, 5, 48, 3, 2, 1, 12))
  871. def testIndefMode(self):
  872. assert encoder.encode(
  873. self.s, defMode=False
  874. ) == ints2octs((101, 128, 48, 128, 2, 1, 12, 0, 0, 0, 0))
  875. class ExpTaggedSequenceComponentEncoderTestCase(BaseTestCase):
  876. def setUp(self):
  877. BaseTestCase.setUp(self)
  878. self.s = univ.Sequence(
  879. componentType=namedtype.NamedTypes(
  880. namedtype.NamedType('number', univ.Boolean().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
  881. )
  882. )
  883. self.s[0] = True
  884. def testDefMode(self):
  885. assert encoder.encode(self.s) == ints2octs((48, 5, 160, 3, 1, 1, 1))
  886. def testIndefMode(self):
  887. assert encoder.encode(
  888. self.s, defMode=False
  889. ) == ints2octs((48, 128, 160, 3, 1, 1, 1, 0, 0, 0, 0))
  890. class SetEncoderTestCase(BaseTestCase):
  891. def setUp(self):
  892. BaseTestCase.setUp(self)
  893. self.s = univ.Set()
  894. self.s.setComponentByPosition(0, univ.Null(''))
  895. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  896. self.s.setComponentByPosition(2, univ.Integer(1))
  897. def testDefMode(self):
  898. assert encoder.encode(self.s) == ints2octs((49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
  899. def testIndefMode(self):
  900. assert encoder.encode(
  901. self.s, defMode=False
  902. ) == ints2octs((49, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
  903. def testDefModeChunked(self):
  904. assert encoder.encode(
  905. self.s, defMode=True, maxChunkSize=4
  906. ) == 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))
  907. def testIndefModeChunked(self):
  908. assert encoder.encode(
  909. self.s, defMode=False, maxChunkSize=4
  910. ) == 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))
  911. class SetEncoderWithSchemaTestCase(BaseTestCase):
  912. def setUp(self):
  913. BaseTestCase.setUp(self)
  914. self.s = univ.Set(
  915. componentType=namedtype.NamedTypes(
  916. namedtype.NamedType('place-holder', univ.Null()),
  917. namedtype.OptionalNamedType('first-name', univ.OctetString()),
  918. namedtype.DefaultedNamedType('age', univ.Integer(33)),
  919. )
  920. )
  921. self.v = {
  922. 'place-holder': None,
  923. 'first-name': 'quick brown',
  924. 'age': 1
  925. }
  926. def testEmpty(self):
  927. try:
  928. assert encoder.encode({}, asn1Spec=self.s)
  929. except PyAsn1Error:
  930. pass
  931. else:
  932. assert False, 'empty bare SET tolerated'
  933. def testDefMode(self):
  934. assert encoder.encode(
  935. self.v, asn1Spec=self.s
  936. ) == ints2octs((49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
  937. def testIndefMode(self):
  938. assert encoder.encode(
  939. self.v, asn1Spec=self.s, defMode=False
  940. ) == ints2octs((49, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
  941. def testDefModeChunked(self):
  942. assert encoder.encode(
  943. self.v, asn1Spec=self.s, defMode=True, maxChunkSize=4
  944. ) == 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))
  945. def testIndefModeChunked(self):
  946. assert encoder.encode(
  947. self.v, asn1Spec=self.s, defMode=False, maxChunkSize=4
  948. ) == 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))
  949. class SetEncoderWithComponentsSchemaTestCase(BaseTestCase):
  950. def setUp(self):
  951. BaseTestCase.setUp(self)
  952. self.s = univ.Set(
  953. componentType=namedtype.NamedTypes(
  954. namedtype.NamedType('place-holder', univ.Null()),
  955. namedtype.OptionalNamedType('first-name', univ.OctetString()),
  956. namedtype.DefaultedNamedType('age', univ.Integer(33)),
  957. )
  958. )
  959. def __init(self):
  960. self.s.clear()
  961. self.s.setComponentByPosition(0, '')
  962. def __initWithOptional(self):
  963. self.s.clear()
  964. self.s.setComponentByPosition(0, '')
  965. self.s.setComponentByPosition(1, 'quick brown')
  966. def __initWithDefaulted(self):
  967. self.s.clear()
  968. self.s.setComponentByPosition(0, '')
  969. self.s.setComponentByPosition(2, 1)
  970. def __initWithOptionalAndDefaulted(self):
  971. self.s.clear()
  972. self.s.setComponentByPosition(0, univ.Null(''))
  973. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  974. self.s.setComponentByPosition(2, univ.Integer(1))
  975. def testDefMode(self):
  976. self.__init()
  977. assert encoder.encode(self.s) == ints2octs((49, 2, 5, 0))
  978. def testIndefMode(self):
  979. self.__init()
  980. assert encoder.encode(
  981. self.s, defMode=False
  982. ) == ints2octs((49, 128, 5, 0, 0, 0))
  983. def testDefModeChunked(self):
  984. self.__init()
  985. assert encoder.encode(
  986. self.s, defMode=True, maxChunkSize=4
  987. ) == ints2octs((49, 2, 5, 0))
  988. def testIndefModeChunked(self):
  989. self.__init()
  990. assert encoder.encode(
  991. self.s, defMode=False, maxChunkSize=4
  992. ) == ints2octs((49, 128, 5, 0, 0, 0))
  993. def testWithOptionalDefMode(self):
  994. self.__initWithOptional()
  995. assert encoder.encode(self.s) == ints2octs(
  996. (49, 15, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  997. def testWithOptionalIndefMode(self):
  998. self.__initWithOptional()
  999. assert encoder.encode(
  1000. self.s, defMode=False
  1001. ) == ints2octs((49, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
  1002. def testWithOptionalDefModeChunked(self):
  1003. self.__initWithOptional()
  1004. assert encoder.encode(
  1005. self.s, defMode=True, maxChunkSize=4
  1006. ) == ints2octs((49, 21, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
  1007. def testWithOptionalIndefModeChunked(self):
  1008. self.__initWithOptional()
  1009. assert encoder.encode(
  1010. self.s, defMode=False, maxChunkSize=4
  1011. ) == ints2octs(
  1012. (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))
  1013. def testWithDefaultedDefMode(self):
  1014. self.__initWithDefaulted()
  1015. assert encoder.encode(self.s) == ints2octs((49, 5, 5, 0, 2, 1, 1))
  1016. def testWithDefaultedIndefMode(self):
  1017. self.__initWithDefaulted()
  1018. assert encoder.encode(
  1019. self.s, defMode=False
  1020. ) == ints2octs((49, 128, 5, 0, 2, 1, 1, 0, 0))
  1021. def testWithDefaultedDefModeChunked(self):
  1022. self.__initWithDefaulted()
  1023. assert encoder.encode(
  1024. self.s, defMode=True, maxChunkSize=4
  1025. ) == ints2octs((49, 5, 5, 0, 2, 1, 1))
  1026. def testWithDefaultedIndefModeChunked(self):
  1027. self.__initWithDefaulted()
  1028. assert encoder.encode(
  1029. self.s, defMode=False, maxChunkSize=4
  1030. ) == ints2octs((49, 128, 5, 0, 2, 1, 1, 0, 0))
  1031. def testWithOptionalAndDefaultedDefMode(self):
  1032. self.__initWithOptionalAndDefaulted()
  1033. assert encoder.encode(self.s) == ints2octs(
  1034. (49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
  1035. def testWithOptionalAndDefaultedIndefMode(self):
  1036. self.__initWithOptionalAndDefaulted()
  1037. assert encoder.encode(
  1038. self.s, defMode=False
  1039. ) == ints2octs((49, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
  1040. def testWithOptionalAndDefaultedDefModeChunked(self):
  1041. self.__initWithOptionalAndDefaulted()
  1042. assert encoder.encode(
  1043. self.s, defMode=True, maxChunkSize=4
  1044. ) == ints2octs(
  1045. (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))
  1046. def testWithOptionalAndDefaultedIndefModeChunked(self):
  1047. self.__initWithOptionalAndDefaulted()
  1048. assert encoder.encode(
  1049. self.s, defMode=False, maxChunkSize=4
  1050. ) == 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))
  1051. class ChoiceEncoderTestCase(BaseTestCase):
  1052. def testEmpty(self):
  1053. s = univ.Choice()
  1054. try:
  1055. encoder.encode(s)
  1056. except PyAsn1Error:
  1057. pass
  1058. else:
  1059. assert 0, 'encoded unset choice'
  1060. def testDefModeOptionOne(self):
  1061. s = univ.Choice()
  1062. s.setComponentByPosition(0, univ.Null(''))
  1063. assert encoder.encode(s) == ints2octs((5, 0))
  1064. def testDefModeOptionTwo(self):
  1065. s = univ.Choice()
  1066. s.setComponentByPosition(0, univ.OctetString('quick brown'))
  1067. assert encoder.encode(s) == ints2octs((4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  1068. def testIndefMode(self):
  1069. s = univ.Choice()
  1070. s.setComponentByPosition(0, univ.OctetString('quick brown'))
  1071. assert encoder.encode(
  1072. s, defMode=False
  1073. ) == ints2octs((4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
  1074. def testDefModeChunked(self):
  1075. s = univ.Choice()
  1076. s.setComponentByPosition(0, univ.OctetString('quick brown'))
  1077. assert encoder.encode(
  1078. s, defMode=True, maxChunkSize=4
  1079. ) == ints2octs((36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
  1080. def testIndefModeChunked(self):
  1081. s = univ.Choice()
  1082. s.setComponentByPosition(0, univ.OctetString('quick brown'))
  1083. assert encoder.encode(
  1084. s, defMode=False, maxChunkSize=4
  1085. ) == ints2octs((36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0))
  1086. class ChoiceEncoderWithSchemaTestCase(BaseTestCase):
  1087. def setUp(self):
  1088. BaseTestCase.setUp(self)
  1089. self.s = univ.Choice(
  1090. componentType=namedtype.NamedTypes(
  1091. namedtype.NamedType('place-holder', univ.Null('')),
  1092. namedtype.NamedType('number', univ.Integer(0)),
  1093. namedtype.NamedType('string', univ.OctetString())
  1094. )
  1095. )
  1096. self.v = {
  1097. 'place-holder': None
  1098. }
  1099. def testFilled(self):
  1100. assert encoder.encode(
  1101. self.v, asn1Spec=self.s
  1102. ) == ints2octs((5, 0))
  1103. class ChoiceEncoderWithComponentsSchemaTestCase(BaseTestCase):
  1104. def setUp(self):
  1105. BaseTestCase.setUp(self)
  1106. self.s = univ.Choice(
  1107. componentType=namedtype.NamedTypes(
  1108. namedtype.NamedType('place-holder', univ.Null('')),
  1109. namedtype.NamedType('number', univ.Integer(0)),
  1110. namedtype.NamedType('string', univ.OctetString())
  1111. )
  1112. )
  1113. def testEmpty(self):
  1114. try:
  1115. encoder.encode(self.s)
  1116. except PyAsn1Error:
  1117. pass
  1118. else:
  1119. assert 0, 'encoded unset choice'
  1120. def testFilled(self):
  1121. self.s.setComponentByPosition(0, univ.Null(''))
  1122. assert encoder.encode(self.s) == ints2octs((5, 0))
  1123. def testTagged(self):
  1124. s = self.s.subtype(
  1125. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 4)
  1126. )
  1127. s.setComponentByPosition(0, univ.Null(''))
  1128. assert encoder.encode(s) == ints2octs((164, 2, 5, 0))
  1129. def testUndefLength(self):
  1130. self.s.setComponentByPosition(2, univ.OctetString('abcdefgh'))
  1131. assert encoder.encode(self.s, defMode=False, maxChunkSize=3) == ints2octs(
  1132. (36, 128, 4, 3, 97, 98, 99, 4, 3, 100, 101, 102, 4, 2, 103, 104, 0, 0))
  1133. def testTaggedUndefLength(self):
  1134. s = self.s.subtype(
  1135. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 4)
  1136. )
  1137. s.setComponentByPosition(2, univ.OctetString('abcdefgh'))
  1138. assert encoder.encode(s, defMode=False, maxChunkSize=3) == ints2octs(
  1139. (164, 128, 36, 128, 4, 3, 97, 98, 99, 4, 3, 100, 101, 102, 4, 2, 103, 104, 0, 0, 0, 0))
  1140. class AnyEncoderTestCase(BaseTestCase):
  1141. def setUp(self):
  1142. BaseTestCase.setUp(self)
  1143. self.s = univ.Any(encoder.encode(univ.OctetString('fox')))
  1144. def testUntagged(self):
  1145. assert encoder.encode(self.s) == ints2octs((4, 3, 102, 111, 120))
  1146. def testTaggedEx(self):
  1147. s = self.s.subtype(
  1148. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4)
  1149. )
  1150. assert encoder.encode(s) == ints2octs((164, 5, 4, 3, 102, 111, 120))
  1151. def testTaggedIm(self):
  1152. s = self.s.subtype(
  1153. implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4)
  1154. )
  1155. assert encoder.encode(s) == ints2octs((132, 5, 4, 3, 102, 111, 120))
  1156. class AnyEncoderWithSchemaTestCase(BaseTestCase):
  1157. def setUp(self):
  1158. BaseTestCase.setUp(self)
  1159. self.s = univ.Any()
  1160. self.v = encoder.encode(univ.OctetString('fox'))
  1161. def testUntagged(self):
  1162. assert encoder.encode(self.v, asn1Spec=self.s) == ints2octs((4, 3, 102, 111, 120))
  1163. def testTaggedEx(self):
  1164. s = self.s.subtype(
  1165. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4)
  1166. )
  1167. assert encoder.encode(self.v, asn1Spec=s) == ints2octs((164, 5, 4, 3, 102, 111, 120))
  1168. def testTaggedIm(self):
  1169. s = self.s.subtype(
  1170. implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4)
  1171. )
  1172. assert encoder.encode(self.v, asn1Spec=s) == ints2octs((132, 5, 4, 3, 102, 111, 120))
  1173. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  1174. if __name__ == '__main__':
  1175. unittest.TextTestRunner(verbosity=2).run(suite)