test_encoder.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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 useful
  15. from pyasn1.codec.cer import encoder
  16. from pyasn1.compat.octets import ints2octs
  17. from pyasn1.error import PyAsn1Error
  18. class BooleanEncoderTestCase(BaseTestCase):
  19. def testTrue(self):
  20. assert encoder.encode(univ.Boolean(1)) == ints2octs((1, 1, 255))
  21. def testFalse(self):
  22. assert encoder.encode(univ.Boolean(0)) == ints2octs((1, 1, 0))
  23. class BitStringEncoderTestCase(BaseTestCase):
  24. def testShortMode(self):
  25. assert encoder.encode(
  26. univ.BitString((1, 0) * 5)
  27. ) == ints2octs((3, 3, 6, 170, 128))
  28. def testLongMode(self):
  29. assert encoder.encode(univ.BitString((1, 0) * 501)) == ints2octs((3, 127, 6) + (170,) * 125 + (128,))
  30. class OctetStringEncoderTestCase(BaseTestCase):
  31. def testShortMode(self):
  32. assert encoder.encode(
  33. univ.OctetString('Quick brown fox')
  34. ) == ints2octs((4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120))
  35. def testLongMode(self):
  36. assert encoder.encode(
  37. univ.OctetString('Q' * 1001)
  38. ) == ints2octs((36, 128, 4, 130, 3, 232) + (81,) * 1000 + (4, 1, 81, 0, 0))
  39. class GeneralizedTimeEncoderTestCase(BaseTestCase):
  40. # def testExtraZeroInSeconds(self):
  41. # try:
  42. # assert encoder.encode(
  43. # useful.GeneralizedTime('20150501120112.10Z')
  44. # )
  45. # except PyAsn1Error:
  46. # pass
  47. # else:
  48. # assert 0, 'Meaningless trailing zero in fraction part tolerated'
  49. def testLocalTimezone(self):
  50. try:
  51. assert encoder.encode(
  52. useful.GeneralizedTime('20150501120112.1+0200')
  53. )
  54. except PyAsn1Error:
  55. pass
  56. else:
  57. assert 0, 'Local timezone tolerated'
  58. def testMissingTimezone(self):
  59. try:
  60. assert encoder.encode(
  61. useful.GeneralizedTime('20150501120112.1')
  62. )
  63. except PyAsn1Error:
  64. pass
  65. else:
  66. assert 0, 'Missing timezone tolerated'
  67. def testDecimalCommaPoint(self):
  68. try:
  69. assert encoder.encode(
  70. useful.GeneralizedTime('20150501120112,1Z')
  71. )
  72. except PyAsn1Error:
  73. pass
  74. else:
  75. assert 0, 'Decimal comma tolerated'
  76. def testWithSubseconds(self):
  77. assert encoder.encode(
  78. useful.GeneralizedTime('20170801120112.59Z')
  79. ) == ints2octs((24, 18, 50, 48, 49, 55, 48, 56, 48, 49, 49, 50, 48, 49, 49, 50, 46, 53, 57, 90))
  80. def testWithSubsecondsWithZeros(self):
  81. assert encoder.encode(
  82. useful.GeneralizedTime('20170801120112.099Z')
  83. ) == ints2octs((24, 18, 50, 48, 49, 55, 48, 56, 48, 49, 49, 50, 48, 49, 49, 50, 46, 57, 57, 90))
  84. def testWithSubsecondsMax(self):
  85. assert encoder.encode(
  86. useful.GeneralizedTime('20170801120112.999Z')
  87. ) == ints2octs((24, 19, 50, 48, 49, 55, 48, 56, 48, 49, 49, 50, 48, 49, 49, 50, 46, 57, 57, 57, 90))
  88. def testWithSubsecondsMin(self):
  89. assert encoder.encode(
  90. useful.GeneralizedTime('20170801120112.000Z')
  91. ) == ints2octs((24, 15, 50, 48, 49, 55, 48, 56, 48, 49, 49, 50, 48, 49, 49, 50, 90))
  92. def testWithSubsecondsDanglingDot(self):
  93. assert encoder.encode(
  94. useful.GeneralizedTime('20170801120112.Z')
  95. ) == ints2octs((24, 15, 50, 48, 49, 55, 48, 56, 48, 49, 49, 50, 48, 49, 49, 50, 90))
  96. def testWithSeconds(self):
  97. assert encoder.encode(
  98. useful.GeneralizedTime('20170801120112Z')
  99. ) == ints2octs((24, 15, 50, 48, 49, 55, 48, 56, 48, 49, 49, 50, 48, 49, 49, 50, 90))
  100. def testWithMinutes(self):
  101. assert encoder.encode(
  102. useful.GeneralizedTime('201708011201Z')
  103. ) == ints2octs((24, 13, 50, 48, 49, 55, 48, 56, 48, 49, 49, 50, 48, 49, 90))
  104. class UTCTimeEncoderTestCase(BaseTestCase):
  105. def testFractionOfSecond(self):
  106. try:
  107. assert encoder.encode(
  108. useful.UTCTime('150501120112.10Z')
  109. )
  110. except PyAsn1Error:
  111. pass
  112. else:
  113. assert 0, 'Decimal point tolerated'
  114. def testMissingTimezone(self):
  115. try:
  116. assert encoder.encode(
  117. useful.UTCTime('150501120112')
  118. ) == ints2octs((23, 13, 49, 53, 48, 53, 48, 49, 49, 50, 48, 49, 49, 50, 90))
  119. except PyAsn1Error:
  120. pass
  121. else:
  122. assert 0, 'Missing timezone tolerated'
  123. def testLocalTimezone(self):
  124. try:
  125. assert encoder.encode(
  126. useful.UTCTime('150501120112+0200')
  127. )
  128. except PyAsn1Error:
  129. pass
  130. else:
  131. assert 0, 'Local timezone tolerated'
  132. def testWithSeconds(self):
  133. assert encoder.encode(
  134. useful.UTCTime('990801120112Z')
  135. ) == ints2octs((23, 13, 57, 57, 48, 56, 48, 49, 49, 50, 48, 49, 49, 50, 90))
  136. def testWithMinutes(self):
  137. assert encoder.encode(
  138. useful.UTCTime('9908011201Z')
  139. ) == ints2octs((23, 11, 57, 57, 48, 56, 48, 49, 49, 50, 48, 49, 90))
  140. class SequenceOfEncoderTestCase(BaseTestCase):
  141. def testEmpty(self):
  142. s = univ.SequenceOf()
  143. s.clear()
  144. assert encoder.encode(s) == ints2octs((48, 128, 0, 0))
  145. def testDefMode1(self):
  146. s = univ.SequenceOf()
  147. s.append(univ.OctetString('a'))
  148. s.append(univ.OctetString('ab'))
  149. assert encoder.encode(s) == ints2octs((48, 128, 4, 1, 97, 4, 2, 97, 98, 0, 0))
  150. def testDefMode2(self):
  151. s = univ.SequenceOf()
  152. s.append(univ.OctetString('ab'))
  153. s.append(univ.OctetString('a'))
  154. assert encoder.encode(s) == ints2octs((48, 128, 4, 2, 97, 98, 4, 1, 97, 0, 0))
  155. def testDefMode3(self):
  156. s = univ.SequenceOf()
  157. s.append(univ.OctetString('b'))
  158. s.append(univ.OctetString('a'))
  159. assert encoder.encode(s) == ints2octs((48, 128, 4, 1, 98, 4, 1, 97, 0, 0))
  160. def testDefMode4(self):
  161. s = univ.SequenceOf()
  162. s.append(univ.OctetString('a'))
  163. s.append(univ.OctetString('b'))
  164. assert encoder.encode(s) == ints2octs((48, 128, 4, 1, 97, 4, 1, 98, 0, 0))
  165. class SequenceOfEncoderWithSchemaTestCase(BaseTestCase):
  166. def setUp(self):
  167. BaseTestCase.setUp(self)
  168. self.s = univ.SequenceOf(componentType=univ.OctetString())
  169. def testEmpty(self):
  170. self.s.clear()
  171. assert encoder.encode(self.s) == ints2octs((48, 128, 0, 0))
  172. def testIndefMode1(self):
  173. self.s.clear()
  174. self.s.append('a')
  175. self.s.append('ab')
  176. assert encoder.encode(self.s) == ints2octs((48, 128, 4, 1, 97, 4, 2, 97, 98, 0, 0))
  177. def testIndefMode2(self):
  178. self.s.clear()
  179. self.s.append('ab')
  180. self.s.append('a')
  181. assert encoder.encode(self.s) == ints2octs((48, 128, 4, 2, 97, 98, 4, 1, 97, 0, 0))
  182. def testIndefMode3(self):
  183. self.s.clear()
  184. self.s.append('b')
  185. self.s.append('a')
  186. assert encoder.encode(self.s) == ints2octs((48, 128, 4, 1, 98, 4, 1, 97, 0, 0))
  187. def testIndefMode4(self):
  188. self.s.clear()
  189. self.s.append('a')
  190. self.s.append('b')
  191. assert encoder.encode(self.s) == ints2octs((48, 128, 4, 1, 97, 4, 1, 98, 0, 0))
  192. class SetOfEncoderTestCase(BaseTestCase):
  193. def testEmpty(self):
  194. s = univ.SetOf()
  195. s.clear()
  196. assert encoder.encode(s) == ints2octs((49, 128, 0, 0))
  197. def testDefMode1(self):
  198. s = univ.SetOf()
  199. s.append(univ.OctetString('a'))
  200. s.append(univ.OctetString('ab'))
  201. assert encoder.encode(s) == ints2octs((49, 128, 4, 1, 97, 4, 2, 97, 98, 0, 0))
  202. def testDefMode2(self):
  203. s = univ.SetOf()
  204. s.append(univ.OctetString('ab'))
  205. s.append(univ.OctetString('a'))
  206. assert encoder.encode(s) == ints2octs((49, 128, 4, 1, 97, 4, 2, 97, 98, 0, 0))
  207. def testDefMode3(self):
  208. s = univ.SetOf()
  209. s.append(univ.OctetString('b'))
  210. s.append(univ.OctetString('a'))
  211. assert encoder.encode(s) == ints2octs((49, 128, 4, 1, 97, 4, 1, 98, 0, 0))
  212. def testDefMode4(self):
  213. s = univ.SetOf()
  214. s.append(univ.OctetString('a'))
  215. s.append(univ.OctetString('b'))
  216. assert encoder.encode(s) == ints2octs((49, 128, 4, 1, 97, 4, 1, 98, 0, 0))
  217. class SetOfEncoderWithSchemaTestCase(BaseTestCase):
  218. def setUp(self):
  219. BaseTestCase.setUp(self)
  220. self.s = univ.SetOf(componentType=univ.OctetString())
  221. def testEmpty(self):
  222. self.s.clear()
  223. assert encoder.encode(self.s) == ints2octs((49, 128, 0, 0))
  224. def testIndefMode1(self):
  225. self.s.clear()
  226. self.s.append('a')
  227. self.s.append('ab')
  228. assert encoder.encode(self.s) == ints2octs((49, 128, 4, 1, 97, 4, 2, 97, 98, 0, 0))
  229. def testIndefMode2(self):
  230. self.s.clear()
  231. self.s.append('ab')
  232. self.s.append('a')
  233. assert encoder.encode(self.s) == ints2octs((49, 128, 4, 1, 97, 4, 2, 97, 98, 0, 0))
  234. def testIndefMode3(self):
  235. self.s.clear()
  236. self.s.append('b')
  237. self.s.append('a')
  238. assert encoder.encode(self.s) == ints2octs((49, 128, 4, 1, 97, 4, 1, 98, 0, 0))
  239. def testIndefMode4(self):
  240. self.s.clear()
  241. self.s.append('a')
  242. self.s.append('b')
  243. assert encoder.encode(self.s) == ints2octs((49, 128, 4, 1, 97, 4, 1, 98, 0, 0))
  244. class SetEncoderTestCase(BaseTestCase):
  245. def setUp(self):
  246. BaseTestCase.setUp(self)
  247. self.s = univ.Set()
  248. self.s.setComponentByPosition(0, univ.Null(''))
  249. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  250. self.s.setComponentByPosition(2, univ.Integer(1))
  251. def testIndefMode(self):
  252. assert encoder.encode(self.s) == ints2octs((49, 128, 2, 1, 1, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0, 0, 0))
  253. def testWithOptionalIndefMode(self):
  254. assert encoder.encode(
  255. self.s
  256. ) == ints2octs((49, 128, 2, 1, 1, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0, 0, 0))
  257. def testWithDefaultedIndefMode(self):
  258. assert encoder.encode(
  259. self.s
  260. ) == ints2octs((49, 128, 2, 1, 1, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0, 0, 0))
  261. def testWithOptionalAndDefaultedIndefMode(self):
  262. assert encoder.encode(
  263. self.s
  264. ) == ints2octs((49, 128, 2, 1, 1, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0, 0, 0))
  265. class SetEncoderWithSchemaTestCase(BaseTestCase):
  266. def setUp(self):
  267. BaseTestCase.setUp(self)
  268. self.s = univ.Set(componentType=namedtype.NamedTypes(
  269. namedtype.NamedType('place-holder', univ.Null('')),
  270. namedtype.OptionalNamedType('first-name', univ.OctetString()),
  271. namedtype.DefaultedNamedType('age', univ.Integer(33))
  272. ))
  273. def __init(self):
  274. self.s.clear()
  275. self.s.setComponentByPosition(0)
  276. def __initWithOptional(self):
  277. self.s.clear()
  278. self.s.setComponentByPosition(0)
  279. self.s.setComponentByPosition(1, 'quick brown')
  280. def __initWithDefaulted(self):
  281. self.s.clear()
  282. self.s.setComponentByPosition(0)
  283. self.s.setComponentByPosition(2, 1)
  284. def __initWithOptionalAndDefaulted(self):
  285. self.s.clear()
  286. self.s.setComponentByPosition(0, univ.Null(''))
  287. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  288. self.s.setComponentByPosition(2, univ.Integer(1))
  289. def testIndefMode(self):
  290. self.__init()
  291. assert encoder.encode(self.s) == ints2octs((49, 128, 5, 0, 0, 0))
  292. def testWithOptionalIndefMode(self):
  293. self.__initWithOptional()
  294. assert encoder.encode(
  295. self.s
  296. ) == ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0, 0, 0))
  297. def testWithDefaultedIndefMode(self):
  298. self.__initWithDefaulted()
  299. assert encoder.encode(
  300. self.s
  301. ) == ints2octs((49, 128, 2, 1, 1, 5, 0, 0, 0))
  302. def testWithOptionalAndDefaultedIndefMode(self):
  303. self.__initWithOptionalAndDefaulted()
  304. assert encoder.encode(
  305. self.s
  306. ) == ints2octs((49, 128, 2, 1, 1, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0, 0, 0))
  307. class SetEncoderWithChoiceWithSchemaEncoderTestCase(BaseTestCase):
  308. def setUp(self):
  309. BaseTestCase.setUp(self)
  310. c = univ.Choice(componentType=namedtype.NamedTypes(
  311. namedtype.NamedType('actual', univ.Boolean(0))
  312. ))
  313. self.s = univ.Set(componentType=namedtype.NamedTypes(
  314. namedtype.NamedType('place-holder', univ.Null('')),
  315. namedtype.NamedType('status', c)
  316. ))
  317. def testIndefMode(self):
  318. self.s.setComponentByPosition(0)
  319. self.s.setComponentByName('status')
  320. self.s.getComponentByName('status').setComponentByPosition(0, 1)
  321. assert encoder.encode(self.s) == ints2octs((49, 128, 1, 1, 255, 5, 0, 0, 0))
  322. class SetEncoderWithTaggedChoiceEncoderTestCase(BaseTestCase):
  323. def testWithUntaggedChoice(self):
  324. c = univ.Choice(
  325. componentType=namedtype.NamedTypes(
  326. namedtype.NamedType('premium', univ.Boolean())
  327. )
  328. )
  329. s = univ.Set(
  330. componentType=namedtype.NamedTypes(
  331. namedtype.NamedType('name', univ.OctetString()),
  332. namedtype.NamedType('customer', c)
  333. )
  334. )
  335. s.setComponentByName('name', 'A')
  336. s.getComponentByName('customer').setComponentByName('premium', True)
  337. assert encoder.encode(s) == ints2octs((49, 128, 1, 1, 255, 4, 1, 65, 0, 0))
  338. def testWithTaggedChoice(self):
  339. c = univ.Choice(
  340. componentType=namedtype.NamedTypes(
  341. namedtype.NamedType('premium', univ.Boolean())
  342. )
  343. ).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 7))
  344. s = univ.Set(
  345. componentType=namedtype.NamedTypes(
  346. namedtype.NamedType('name', univ.OctetString()),
  347. namedtype.NamedType('customer', c)
  348. )
  349. )
  350. s.setComponentByName('name', 'A')
  351. s.getComponentByName('customer').setComponentByName('premium', True)
  352. assert encoder.encode(s) == ints2octs((49, 128, 4, 1, 65, 167, 128, 1, 1, 255, 0, 0, 0, 0))
  353. class SequenceEncoderTestCase(BaseTestCase):
  354. def setUp(self):
  355. BaseTestCase.setUp(self)
  356. self.s = univ.Sequence()
  357. self.s.setComponentByPosition(0, univ.Null(''))
  358. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  359. self.s.setComponentByPosition(2, univ.Integer(1))
  360. def testIndefMode(self):
  361. assert encoder.encode(self.s) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
  362. def testWithOptionalIndefMode(self):
  363. assert encoder.encode(
  364. self.s
  365. ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
  366. def testWithDefaultedIndefMode(self):
  367. assert encoder.encode(
  368. self.s
  369. ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
  370. def testWithOptionalAndDefaultedIndefMode(self):
  371. assert encoder.encode(
  372. self.s
  373. ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
  374. class SequenceEncoderWithSchemaTestCase(BaseTestCase):
  375. def setUp(self):
  376. BaseTestCase.setUp(self)
  377. self.s = univ.Sequence(
  378. componentType=namedtype.NamedTypes(
  379. namedtype.NamedType('place-holder', univ.Null('')),
  380. namedtype.OptionalNamedType('first-name', univ.OctetString()),
  381. namedtype.DefaultedNamedType('age', univ.Integer(33))
  382. )
  383. )
  384. def __init(self):
  385. self.s.clear()
  386. self.s.setComponentByPosition(0)
  387. def __initWithOptional(self):
  388. self.s.clear()
  389. self.s.setComponentByPosition(0)
  390. self.s.setComponentByPosition(1, 'quick brown')
  391. def __initWithDefaulted(self):
  392. self.s.clear()
  393. self.s.setComponentByPosition(0)
  394. self.s.setComponentByPosition(2, 1)
  395. def __initWithOptionalAndDefaulted(self):
  396. self.s.clear()
  397. self.s.setComponentByPosition(0, univ.Null(''))
  398. self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
  399. self.s.setComponentByPosition(2, univ.Integer(1))
  400. def testIndefMode(self):
  401. self.__init()
  402. assert encoder.encode(self.s) == ints2octs((48, 128, 5, 0, 0, 0))
  403. def testWithOptionalIndefMode(self):
  404. self.__initWithOptional()
  405. assert encoder.encode(
  406. self.s
  407. ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
  408. def testWithDefaultedIndefMode(self):
  409. self.__initWithDefaulted()
  410. assert encoder.encode(
  411. self.s
  412. ) == ints2octs((48, 128, 5, 0, 2, 1, 1, 0, 0))
  413. def testWithOptionalAndDefaultedIndefMode(self):
  414. self.__initWithOptionalAndDefaulted()
  415. assert encoder.encode(
  416. self.s
  417. ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
  418. class SequenceEncoderWithUntaggedOpenTypesTestCase(BaseTestCase):
  419. def setUp(self):
  420. BaseTestCase.setUp(self)
  421. openType = opentype.OpenType(
  422. 'id',
  423. {1: univ.Integer(),
  424. 2: univ.OctetString()}
  425. )
  426. self.s = univ.Sequence(
  427. componentType=namedtype.NamedTypes(
  428. namedtype.NamedType('id', univ.Integer()),
  429. namedtype.NamedType('blob', univ.Any(), openType=openType)
  430. )
  431. )
  432. def testEncodeOpenTypeChoiceOne(self):
  433. self.s.clear()
  434. self.s[0] = 1
  435. self.s[1] = univ.Integer(12)
  436. assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
  437. (48, 128, 2, 1, 1, 49, 50, 0, 0)
  438. )
  439. def testEncodeOpenTypeChoiceTwo(self):
  440. self.s.clear()
  441. self.s[0] = 2
  442. self.s[1] = univ.OctetString('quick brown')
  443. assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
  444. (48, 128, 2, 1, 2, 113, 117, 105, 99, 107, 32, 98, 114,
  445. 111, 119, 110, 0, 0)
  446. )
  447. def testEncodeOpenTypeUnknownId(self):
  448. self.s.clear()
  449. self.s[0] = 2
  450. self.s[1] = univ.ObjectIdentifier('1.3.6')
  451. try:
  452. encoder.encode(self.s, asn1Spec=self.s)
  453. except PyAsn1Error:
  454. assert False, 'incompatible open type tolerated'
  455. def testEncodeOpenTypeIncompatibleType(self):
  456. self.s.clear()
  457. self.s[0] = 2
  458. self.s[1] = univ.ObjectIdentifier('1.3.6')
  459. try:
  460. encoder.encode(self.s, asn1Spec=self.s)
  461. except PyAsn1Error:
  462. assert False, 'incompatible open type tolerated'
  463. class SequenceEncoderWithImplicitlyTaggedOpenTypesTestCase(BaseTestCase):
  464. def setUp(self):
  465. BaseTestCase.setUp(self)
  466. openType = opentype.OpenType(
  467. 'id',
  468. {1: univ.Integer(),
  469. 2: univ.OctetString()}
  470. )
  471. self.s = univ.Sequence(
  472. componentType=namedtype.NamedTypes(
  473. namedtype.NamedType('id', univ.Integer()),
  474. namedtype.NamedType('blob', univ.Any().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType)
  475. )
  476. )
  477. def testEncodeOpenTypeChoiceOne(self):
  478. self.s.clear()
  479. self.s[0] = 1
  480. self.s[1] = univ.Integer(12)
  481. assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
  482. (48, 128, 2, 1, 1, 163, 128, 163, 128, 49, 50, 0, 0, 0, 0, 0, 0)
  483. )
  484. class SequenceEncoderWithExplicitlyTaggedOpenTypesTestCase(BaseTestCase):
  485. def setUp(self):
  486. BaseTestCase.setUp(self)
  487. openType = opentype.OpenType(
  488. 'id',
  489. {1: univ.Integer(),
  490. 2: univ.OctetString()}
  491. )
  492. self.s = univ.Sequence(
  493. componentType=namedtype.NamedTypes(
  494. namedtype.NamedType('id', univ.Integer()),
  495. namedtype.NamedType('blob', univ.Any().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType)
  496. )
  497. )
  498. def testEncodeOpenTypeChoiceOne(self):
  499. self.s.clear()
  500. self.s[0] = 1
  501. self.s[1] = univ.Integer(12)
  502. assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
  503. (48, 128, 2, 1, 1, 163, 128, 163, 128, 49, 50, 0, 0, 0, 0, 0, 0)
  504. )
  505. class SequenceEncoderWithUntaggedSetOfOpenTypesTestCase(BaseTestCase):
  506. def setUp(self):
  507. BaseTestCase.setUp(self)
  508. openType = opentype.OpenType(
  509. 'id',
  510. {1: univ.Integer(),
  511. 2: univ.OctetString()}
  512. )
  513. self.s = univ.Sequence(
  514. componentType=namedtype.NamedTypes(
  515. namedtype.NamedType('id', univ.Integer()),
  516. namedtype.NamedType('blob', univ.SetOf(
  517. componentType=univ.Any()), openType=openType)
  518. )
  519. )
  520. def testEncodeOpenTypeChoiceOne(self):
  521. self.s.clear()
  522. self.s[0] = 1
  523. self.s[1].append(univ.Integer(12))
  524. assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
  525. (48, 128, 2, 1, 1, 49, 128, 49, 50, 0, 0, 0, 0)
  526. )
  527. def testEncodeOpenTypeChoiceTwo(self):
  528. self.s.clear()
  529. self.s[0] = 2
  530. self.s[1].append(univ.OctetString('quick brown'))
  531. assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
  532. (48, 128, 2, 1, 2, 49, 128, 113, 117, 105, 99, 107, 32, 98, 114,
  533. 111, 119, 110, 0, 0, 0, 0)
  534. )
  535. def testEncodeOpenTypeUnknownId(self):
  536. self.s.clear()
  537. self.s[0] = 2
  538. self.s[1].append(univ.ObjectIdentifier('1.3.6'))
  539. try:
  540. encoder.encode(self.s, asn1Spec=self.s)
  541. except PyAsn1Error:
  542. assert False, 'incompatible open type tolerated'
  543. def testEncodeOpenTypeIncompatibleType(self):
  544. self.s.clear()
  545. self.s[0] = 2
  546. self.s[1].append(univ.ObjectIdentifier('1.3.6'))
  547. try:
  548. encoder.encode(self.s, asn1Spec=self.s)
  549. except PyAsn1Error:
  550. assert False, 'incompatible open type tolerated'
  551. class SequenceEncoderWithImplicitlyTaggedSetOfOpenTypesTestCase(BaseTestCase):
  552. def setUp(self):
  553. BaseTestCase.setUp(self)
  554. openType = opentype.OpenType(
  555. 'id',
  556. {1: univ.Integer(),
  557. 2: univ.OctetString()}
  558. )
  559. self.s = univ.Sequence(
  560. componentType=namedtype.NamedTypes(
  561. namedtype.NamedType('id', univ.Integer()),
  562. namedtype.NamedType('blob', univ.SetOf(
  563. componentType=univ.Any().subtype(
  564. implicitTag=tag.Tag(
  565. tag.tagClassContext, tag.tagFormatSimple, 3))),
  566. openType=openType)
  567. )
  568. )
  569. def testEncodeOpenTypeChoiceOne(self):
  570. self.s.clear()
  571. self.s[0] = 1
  572. self.s[1].append(univ.Integer(12))
  573. assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
  574. (48, 128, 2, 1, 1, 49, 128, 163, 128, 163, 128, 49, 50, 0, 0,
  575. 0, 0, 0, 0, 0, 0)
  576. )
  577. class SequenceEncoderWithExplicitlyTaggedSetOfOpenTypesTestCase(BaseTestCase):
  578. def setUp(self):
  579. BaseTestCase.setUp(self)
  580. openType = opentype.OpenType(
  581. 'id',
  582. {1: univ.Integer(),
  583. 2: univ.OctetString()}
  584. )
  585. self.s = univ.Sequence(
  586. componentType=namedtype.NamedTypes(
  587. namedtype.NamedType('id', univ.Integer()),
  588. namedtype.NamedType('blob', univ.SetOf(
  589. componentType=univ.Any().subtype(
  590. explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3))),
  591. openType=openType)
  592. )
  593. )
  594. def testEncodeOpenTypeChoiceOne(self):
  595. self.s.clear()
  596. self.s[0] = 1
  597. self.s[1].append(univ.Integer(12))
  598. assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
  599. (48, 128, 2, 1, 1, 49, 128, 163, 128, 163, 128, 49, 50, 0, 0,
  600. 0, 0, 0, 0, 0, 0)
  601. )
  602. class NestedOptionalSequenceEncoderTestCase(BaseTestCase):
  603. def setUp(self):
  604. BaseTestCase.setUp(self)
  605. inner = univ.Sequence(
  606. componentType=namedtype.NamedTypes(
  607. namedtype.OptionalNamedType('first-name', univ.OctetString()),
  608. namedtype.DefaultedNamedType('age', univ.Integer(33)),
  609. )
  610. )
  611. outerWithOptional = univ.Sequence(
  612. componentType=namedtype.NamedTypes(
  613. namedtype.OptionalNamedType('inner', inner),
  614. )
  615. )
  616. outerWithDefault = univ.Sequence(
  617. componentType=namedtype.NamedTypes(
  618. namedtype.DefaultedNamedType('inner', inner),
  619. )
  620. )
  621. self.s1 = outerWithOptional
  622. self.s2 = outerWithDefault
  623. def __initOptionalWithDefaultAndOptional(self):
  624. self.s1.clear()
  625. self.s1[0][0] = 'test'
  626. self.s1[0][1] = 123
  627. return self.s1
  628. def __initOptionalWithDefault(self):
  629. self.s1.clear()
  630. self.s1[0][1] = 123
  631. return self.s1
  632. def __initOptionalWithOptional(self):
  633. self.s1.clear()
  634. self.s1[0][0] = 'test'
  635. return self.s1
  636. def __initOptional(self):
  637. self.s1.clear()
  638. return self.s1
  639. def __initDefaultWithDefaultAndOptional(self):
  640. self.s2.clear()
  641. self.s2[0][0] = 'test'
  642. self.s2[0][1] = 123
  643. return self.s2
  644. def __initDefaultWithDefault(self):
  645. self.s2.clear()
  646. self.s2[0][0] = 'test'
  647. return self.s2
  648. def __initDefaultWithOptional(self):
  649. self.s2.clear()
  650. self.s2[0][1] = 123
  651. return self.s2
  652. def testOptionalWithDefaultAndOptional(self):
  653. s = self.__initOptionalWithDefaultAndOptional()
  654. assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 4, 4, 116, 101, 115, 116, 2, 1, 123, 0, 0, 0, 0))
  655. def testOptionalWithDefault(self):
  656. s = self.__initOptionalWithDefault()
  657. assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 2, 1, 123, 0, 0, 0, 0))
  658. def testOptionalWithOptional(self):
  659. s = self.__initOptionalWithOptional()
  660. assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 4, 4, 116, 101, 115, 116, 0, 0, 0, 0))
  661. def testOptional(self):
  662. s = self.__initOptional()
  663. assert encoder.encode(s) == ints2octs((48, 128, 0, 0))
  664. def testDefaultWithDefaultAndOptional(self):
  665. s = self.__initDefaultWithDefaultAndOptional()
  666. assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 4, 4, 116, 101, 115, 116, 2, 1, 123, 0, 0, 0, 0))
  667. def testDefaultWithDefault(self):
  668. s = self.__initDefaultWithDefault()
  669. assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 4, 4, 116, 101, 115, 116, 0, 0, 0, 0))
  670. def testDefaultWithOptional(self):
  671. s = self.__initDefaultWithOptional()
  672. assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 2, 1, 123, 0, 0, 0, 0))
  673. class NestedOptionalChoiceEncoderTestCase(BaseTestCase):
  674. def setUp(self):
  675. BaseTestCase.setUp(self)
  676. layer3 = univ.Sequence(
  677. componentType=namedtype.NamedTypes(
  678. namedtype.OptionalNamedType('first-name', univ.OctetString()),
  679. namedtype.DefaultedNamedType('age', univ.Integer(33)),
  680. )
  681. )
  682. layer2 = univ.Choice(
  683. componentType=namedtype.NamedTypes(
  684. namedtype.NamedType('inner', layer3),
  685. namedtype.NamedType('first-name', univ.OctetString())
  686. )
  687. )
  688. layer1 = univ.Sequence(
  689. componentType=namedtype.NamedTypes(
  690. namedtype.OptionalNamedType('inner', layer2),
  691. )
  692. )
  693. self.s = layer1
  694. def __initOptionalWithDefaultAndOptional(self):
  695. self.s.clear()
  696. self.s[0][0][0] = 'test'
  697. self.s[0][0][1] = 123
  698. return self.s
  699. def __initOptionalWithDefault(self):
  700. self.s.clear()
  701. self.s[0][0][1] = 123
  702. return self.s
  703. def __initOptionalWithOptional(self):
  704. self.s.clear()
  705. self.s[0][0][0] = 'test'
  706. return self.s
  707. def __initOptional(self):
  708. self.s.clear()
  709. return self.s
  710. def testOptionalWithDefaultAndOptional(self):
  711. s = self.__initOptionalWithDefaultAndOptional()
  712. assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 4, 4, 116, 101, 115, 116, 2, 1, 123, 0, 0, 0, 0))
  713. def testOptionalWithDefault(self):
  714. s = self.__initOptionalWithDefault()
  715. assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 2, 1, 123, 0, 0, 0, 0))
  716. def testOptionalWithOptional(self):
  717. s = self.__initOptionalWithOptional()
  718. assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 4, 4, 116, 101, 115, 116, 0, 0, 0, 0))
  719. def testOptional(self):
  720. s = self.__initOptional()
  721. assert encoder.encode(s) == ints2octs((48, 128, 0, 0))
  722. class NestedOptionalSequenceOfEncoderTestCase(BaseTestCase):
  723. def setUp(self):
  724. BaseTestCase.setUp(self)
  725. layer2 = univ.SequenceOf(
  726. componentType=univ.OctetString()
  727. )
  728. layer1 = univ.Sequence(
  729. componentType=namedtype.NamedTypes(
  730. namedtype.OptionalNamedType('inner', layer2),
  731. )
  732. )
  733. self.s = layer1
  734. def __initOptionalWithValue(self):
  735. self.s.clear()
  736. self.s[0][0] = 'test'
  737. return self.s
  738. def __initOptional(self):
  739. self.s.clear()
  740. return self.s
  741. def testOptionalWithValue(self):
  742. s = self.__initOptionalWithValue()
  743. assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 4, 4, 116, 101, 115, 116, 0, 0, 0, 0))
  744. def testOptional(self):
  745. s = self.__initOptional()
  746. assert encoder.encode(s) == ints2octs((48, 128, 0, 0))
  747. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  748. if __name__ == '__main__':
  749. unittest.TextTestRunner(verbosity=2).run(suite)