1# 2# This file is part of pyasn1 software. 3# 4# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com> 5# License: http://snmplabs.com/pyasn1/license.html 6# 7import sys 8 9try: 10 import unittest2 as unittest 11 12except ImportError: 13 import unittest 14 15from tests.base import BaseTestCase 16 17from pyasn1.type import tag 18from pyasn1.type import namedtype 19from pyasn1.type import opentype 20from pyasn1.type import univ 21from pyasn1.codec.der import decoder 22from pyasn1.compat.octets import ints2octs, null 23from pyasn1.error import PyAsn1Error 24 25 26class BitStringDecoderTestCase(BaseTestCase): 27 def testShortMode(self): 28 assert decoder.decode( 29 ints2octs((3, 127, 6) + (170,) * 125 + (128,)) 30 ) == (((1, 0) * 501), null) 31 32 def testIndefMode(self): 33 try: 34 decoder.decode( 35 ints2octs((35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)) 36 ) 37 except PyAsn1Error: 38 pass 39 else: 40 assert 0, 'indefinite length encoding tolerated' 41 42 def testDefModeChunked(self): 43 try: 44 assert decoder.decode( 45 ints2octs((35, 8, 3, 2, 0, 169, 3, 2, 1, 138)) 46 ) 47 except PyAsn1Error: 48 pass 49 else: 50 assert 0, 'chunked encoding tolerated' 51 52 53class OctetStringDecoderTestCase(BaseTestCase): 54 def testShortMode(self): 55 assert decoder.decode( 56 '\004\017Quick brown fox'.encode() 57 ) == ('Quick brown fox'.encode(), ''.encode()) 58 59 def testIndefMode(self): 60 try: 61 decoder.decode( 62 ints2octs((36, 128, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 0, 0)) 63 ) 64 except PyAsn1Error: 65 pass 66 else: 67 assert 0, 'indefinite length encoding tolerated' 68 69 def testChunkedMode(self): 70 try: 71 decoder.decode( 72 ints2octs((36, 23, 4, 2, 81, 117, 4, 2, 105, 99, 4, 2, 107, 32, 4, 2, 98, 114, 4, 2, 111, 119, 4, 1, 110)) 73 ) 74 except PyAsn1Error: 75 pass 76 else: 77 assert 0, 'chunked encoding tolerated' 78 79 80class SequenceDecoderWithUntaggedOpenTypesTestCase(BaseTestCase): 81 def setUp(self): 82 openType = opentype.OpenType( 83 'id', 84 {1: univ.Integer(), 85 2: univ.OctetString()} 86 ) 87 self.s = univ.Sequence( 88 componentType=namedtype.NamedTypes( 89 namedtype.NamedType('id', univ.Integer()), 90 namedtype.NamedType('blob', univ.Any(), openType=openType) 91 ) 92 ) 93 94 def testDecodeOpenTypesChoiceOne(self): 95 s, r = decoder.decode( 96 ints2octs((48, 6, 2, 1, 1, 2, 1, 12)), asn1Spec=self.s, 97 decodeOpenTypes=True 98 ) 99 assert not r 100 assert s[0] == 1 101 assert s[1] == 12 102 103 def testDecodeOpenTypesChoiceTwo(self): 104 s, r = decoder.decode( 105 ints2octs((48, 16, 2, 1, 2, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s, 106 decodeOpenTypes=True 107 ) 108 assert not r 109 assert s[0] == 2 110 assert s[1] == univ.OctetString('quick brown') 111 112 def testDecodeOpenTypesUnknownType(self): 113 try: 114 s, r = decoder.decode( 115 ints2octs((48, 6, 2, 1, 2, 6, 1, 39)), asn1Spec=self.s, 116 decodeOpenTypes=True 117 ) 118 119 except PyAsn1Error: 120 pass 121 122 else: 123 assert False, 'unknown open type tolerated' 124 125 def testDecodeOpenTypesUnknownId(self): 126 s, r = decoder.decode( 127 ints2octs((48, 6, 2, 1, 3, 6, 1, 39)), asn1Spec=self.s, 128 decodeOpenTypes=True 129 ) 130 assert not r 131 assert s[0] == 3 132 assert s[1] == univ.OctetString(hexValue='060127') 133 134 def testDontDecodeOpenTypesChoiceOne(self): 135 s, r = decoder.decode( 136 ints2octs((48, 6, 2, 1, 1, 2, 1, 12)), asn1Spec=self.s 137 ) 138 assert not r 139 assert s[0] == 1 140 assert s[1] == ints2octs((2, 1, 12)) 141 142 def testDontDecodeOpenTypesChoiceTwo(self): 143 s, r = decoder.decode( 144 ints2octs((48, 16, 2, 1, 2, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s 145 ) 146 assert not r 147 assert s[0] == 2 148 assert s[1] == ints2octs((4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)) 149 150 151class SequenceDecoderWithImplicitlyTaggedOpenTypesTestCase(BaseTestCase): 152 def setUp(self): 153 openType = opentype.OpenType( 154 'id', 155 {1: univ.Integer(), 156 2: univ.OctetString()} 157 ) 158 self.s = univ.Sequence( 159 componentType=namedtype.NamedTypes( 160 namedtype.NamedType('id', univ.Integer()), 161 namedtype.NamedType( 162 'blob', univ.Any().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType 163 ) 164 ) 165 ) 166 167 def testDecodeOpenTypesChoiceOne(self): 168 s, r = decoder.decode( 169 ints2octs((48, 8, 2, 1, 1, 131, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True 170 ) 171 assert not r 172 assert s[0] == 1 173 assert s[1] == 12 174 175 def testDecodeOpenTypesUnknownId(self): 176 s, r = decoder.decode( 177 ints2octs((48, 8, 2, 1, 3, 131, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True 178 ) 179 assert not r 180 assert s[0] == 3 181 assert s[1] == univ.OctetString(hexValue='02010C') 182 183 184class SequenceDecoderWithExplicitlyTaggedOpenTypesTestCase(BaseTestCase): 185 def setUp(self): 186 openType = opentype.OpenType( 187 'id', 188 {1: univ.Integer(), 189 2: univ.OctetString()} 190 ) 191 self.s = univ.Sequence( 192 componentType=namedtype.NamedTypes( 193 namedtype.NamedType('id', univ.Integer()), 194 namedtype.NamedType( 195 'blob', univ.Any().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType 196 ) 197 ) 198 ) 199 200 def testDecodeOpenTypesChoiceOne(self): 201 s, r = decoder.decode( 202 ints2octs((48, 8, 2, 1, 1, 163, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True 203 ) 204 assert not r 205 assert s[0] == 1 206 assert s[1] == 12 207 208 def testDecodeOpenTypesUnknownId(self): 209 s, r = decoder.decode( 210 ints2octs((48, 8, 2, 1, 3, 163, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True 211 ) 212 assert not r 213 assert s[0] == 3 214 assert s[1] == univ.OctetString(hexValue='02010C') 215 216 217class SequenceDecoderWithUnaggedSetOfOpenTypesTestCase(BaseTestCase): 218 def setUp(self): 219 openType = opentype.OpenType( 220 'id', 221 {1: univ.Integer(), 222 2: univ.OctetString()} 223 ) 224 self.s = univ.Sequence( 225 componentType=namedtype.NamedTypes( 226 namedtype.NamedType('id', univ.Integer()), 227 namedtype.NamedType('blob', univ.SetOf(componentType=univ.Any()), 228 openType=openType) 229 ) 230 ) 231 232 def testDecodeOpenTypesChoiceOne(self): 233 s, r = decoder.decode( 234 ints2octs((48, 8, 2, 1, 1, 49, 3, 2, 1, 12)), asn1Spec=self.s, 235 decodeOpenTypes=True 236 ) 237 assert not r 238 assert s[0] == 1 239 assert s[1][0] == 12 240 241 def testDecodeOpenTypesChoiceTwo(self): 242 s, r = decoder.decode( 243 ints2octs((48, 18, 2, 1, 2, 49, 13, 4, 11, 113, 117, 105, 99, 244 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s, 245 decodeOpenTypes=True 246 ) 247 assert not r 248 assert s[0] == 2 249 assert s[1][0] == univ.OctetString('quick brown') 250 251 def testDecodeOpenTypesUnknownType(self): 252 try: 253 s, r = decoder.decode( 254 ints2octs((48, 6, 2, 1, 2, 6, 1, 39)), asn1Spec=self.s, 255 decodeOpenTypes=True 256 ) 257 258 except PyAsn1Error: 259 pass 260 261 else: 262 assert False, 'unknown open type tolerated' 263 264 def testDecodeOpenTypesUnknownId(self): 265 s, r = decoder.decode( 266 ints2octs((48, 8, 2, 1, 3, 49, 3, 2, 1, 12)), asn1Spec=self.s, 267 decodeOpenTypes=True 268 ) 269 assert not r 270 assert s[0] == 3 271 assert s[1][0] == univ.OctetString(hexValue='02010c') 272 273 def testDontDecodeOpenTypesChoiceOne(self): 274 s, r = decoder.decode( 275 ints2octs((48, 8, 2, 1, 1, 49, 3, 2, 1, 12)), asn1Spec=self.s 276 ) 277 assert not r 278 assert s[0] == 1 279 assert s[1][0] == ints2octs((2, 1, 12)) 280 281 def testDontDecodeOpenTypesChoiceTwo(self): 282 s, r = decoder.decode( 283 ints2octs((48, 18, 2, 1, 2, 49, 13, 4, 11, 113, 117, 105, 99, 284 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s 285 ) 286 assert not r 287 assert s[0] == 2 288 assert s[1][0] == ints2octs((4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 289 111, 119, 110)) 290 291 292class SequenceDecoderWithImplicitlyTaggedSetOfOpenTypesTestCase(BaseTestCase): 293 def setUp(self): 294 openType = opentype.OpenType( 295 'id', 296 {1: univ.Integer(), 297 2: univ.OctetString()} 298 ) 299 self.s = univ.Sequence( 300 componentType=namedtype.NamedTypes( 301 namedtype.NamedType('id', univ.Integer()), 302 namedtype.NamedType( 303 'blob', univ.SetOf( 304 componentType=univ.Any().subtype( 305 implicitTag=tag.Tag( 306 tag.tagClassContext, tag.tagFormatSimple, 3))), 307 openType=openType 308 ) 309 ) 310 ) 311 312 def testDecodeOpenTypesChoiceOne(self): 313 s, r = decoder.decode( 314 ints2octs((48, 10, 2, 1, 1, 49, 5, 131, 3, 2, 1, 12)), 315 asn1Spec=self.s, decodeOpenTypes=True 316 ) 317 assert not r 318 assert s[0] == 1 319 assert s[1][0] == 12 320 321 def testDecodeOpenTypesUnknownId(self): 322 s, r = decoder.decode( 323 ints2octs((48, 10, 2, 1, 3, 49, 5, 131, 3, 2, 1, 12)), 324 asn1Spec=self.s, decodeOpenTypes=True 325 ) 326 assert not r 327 assert s[0] == 3 328 assert s[1][0] == univ.OctetString(hexValue='02010C') 329 330 331class SequenceDecoderWithExplicitlyTaggedSetOfOpenTypesTestCase(BaseTestCase): 332 def setUp(self): 333 openType = opentype.OpenType( 334 'id', 335 {1: univ.Integer(), 336 2: univ.OctetString()} 337 ) 338 self.s = univ.Sequence( 339 componentType=namedtype.NamedTypes( 340 namedtype.NamedType('id', univ.Integer()), 341 namedtype.NamedType( 342 'blob', univ.SetOf( 343 componentType=univ.Any().subtype( 344 explicitTag=tag.Tag( 345 tag.tagClassContext, tag.tagFormatSimple, 3))), 346 openType=openType 347 ) 348 ) 349 ) 350 351 def testDecodeOpenTypesChoiceOne(self): 352 s, r = decoder.decode( 353 ints2octs((48, 10, 2, 1, 1, 49, 5, 131, 3, 2, 1, 12)), 354 asn1Spec=self.s, decodeOpenTypes=True 355 ) 356 assert not r 357 assert s[0] == 1 358 assert s[1][0] == 12 359 360 def testDecodeOpenTypesUnknownId(self): 361 s, r = decoder.decode( 362 ints2octs( (48, 10, 2, 1, 3, 49, 5, 131, 3, 2, 1, 12)), 363 asn1Spec=self.s, decodeOpenTypes=True 364 ) 365 assert not r 366 assert s[0] == 3 367 assert s[1][0] == univ.OctetString(hexValue='02010C') 368 369 370suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 371 372if __name__ == '__main__': 373 unittest.TextTestRunner(verbosity=2).run(suite) 374