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 11except ImportError: 12 import unittest 13 14from tests.base import BaseTestCase 15 16from pyasn1.type import tag 17from pyasn1.type import namedtype 18from pyasn1.type import opentype 19from pyasn1.type import univ 20from pyasn1.codec.cer import decoder 21from pyasn1.compat.octets import ints2octs, str2octs, null 22from pyasn1.error import PyAsn1Error 23 24 25class BooleanDecoderTestCase(BaseTestCase): 26 def testTrue(self): 27 assert decoder.decode(ints2octs((1, 1, 255))) == (1, null) 28 29 def testFalse(self): 30 assert decoder.decode(ints2octs((1, 1, 0))) == (0, null) 31 32 def testEmpty(self): 33 try: 34 decoder.decode(ints2octs((1, 0))) 35 except PyAsn1Error: 36 pass 37 38 def testOverflow(self): 39 try: 40 decoder.decode(ints2octs((1, 2, 0, 0))) 41 except PyAsn1Error: 42 pass 43 44class BitStringDecoderTestCase(BaseTestCase): 45 def testShortMode(self): 46 assert decoder.decode( 47 ints2octs((3, 3, 6, 170, 128)) 48 ) == (((1, 0) * 5), null) 49 50 def testLongMode(self): 51 assert decoder.decode( 52 ints2octs((3, 127, 6) + (170,) * 125 + (128,)) 53 ) == (((1, 0) * 501), null) 54 55 # TODO: test failures on short chunked and long unchunked substrate samples 56 57 58class OctetStringDecoderTestCase(BaseTestCase): 59 def testShortMode(self): 60 assert decoder.decode( 61 ints2octs((4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120)), 62 ) == (str2octs('Quick brown fox'), null) 63 64 def testLongMode(self): 65 assert decoder.decode( 66 ints2octs((36, 128, 4, 130, 3, 232) + (81,) * 1000 + (4, 1, 81, 0, 0)) 67 ) == (str2octs('Q' * 1001), null) 68 69 # TODO: test failures on short chunked and long unchunked substrate samples 70 71 72class SequenceDecoderWithUntaggedOpenTypesTestCase(BaseTestCase): 73 def setUp(self): 74 openType = opentype.OpenType( 75 'id', 76 {1: univ.Integer(), 77 2: univ.OctetString()} 78 ) 79 self.s = univ.Sequence( 80 componentType=namedtype.NamedTypes( 81 namedtype.NamedType('id', univ.Integer()), 82 namedtype.NamedType('blob', univ.Any(), openType=openType) 83 ) 84 ) 85 86 def testDecodeOpenTypesChoiceOne(self): 87 s, r = decoder.decode( 88 ints2octs((48, 128, 2, 1, 1, 2, 1, 12, 0, 0)), 89 asn1Spec=self.s, 90 decodeOpenTypes=True 91 ) 92 assert not r 93 assert s[0] == 1 94 assert s[1] == 12 95 96 def testDecodeOpenTypesChoiceTwo(self): 97 s, r = decoder.decode( 98 ints2octs((48, 128, 2, 1, 2, 4, 11, 113, 117, 105, 99, 107, 32, 98, 99 114, 111, 119, 110, 0, 0)), asn1Spec=self.s, 100 decodeOpenTypes=True 101 ) 102 assert not r 103 assert s[0] == 2 104 assert s[1] == univ.OctetString('quick brown') 105 106 def testDecodeOpenTypesUnknownType(self): 107 try: 108 s, r = decoder.decode( 109 ints2octs((48, 128, 6, 1, 1, 2, 1, 12, 0, 0)), asn1Spec=self.s, 110 decodeOpenTypes=True 111 ) 112 113 except PyAsn1Error: 114 pass 115 116 else: 117 assert False, 'unknown open type tolerated' 118 119 def testDecodeOpenTypesUnknownId(self): 120 s, r = decoder.decode( 121 ints2octs((48, 128, 2, 1, 3, 6, 1, 12, 0, 0)), asn1Spec=self.s, 122 decodeOpenTypes=True 123 ) 124 assert not r 125 assert s[0] == 3 126 assert s[1] == univ.OctetString(hexValue='06010c') 127 128 def testDontDecodeOpenTypesChoiceOne(self): 129 s, r = decoder.decode( 130 ints2octs((48, 128, 2, 1, 1, 2, 1, 12, 0, 0)), asn1Spec=self.s 131 ) 132 assert not r 133 assert s[0] == 1 134 assert s[1] == ints2octs((2, 1, 12)) 135 136 def testDontDecodeOpenTypesChoiceTwo(self): 137 s, r = decoder.decode( 138 ints2octs((48, 128, 2, 1, 2, 4, 11, 113, 117, 105, 99, 107, 32, 98, 139 114, 111, 119, 110, 0, 0)), asn1Spec=self.s 140 ) 141 assert not r 142 assert s[0] == 2 143 assert s[1] == ints2octs((4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)) 144 145 146class SequenceDecoderWithImplicitlyTaggedOpenTypesTestCase(BaseTestCase): 147 def setUp(self): 148 openType = opentype.OpenType( 149 'id', 150 {1: univ.Integer(), 151 2: univ.OctetString()} 152 ) 153 self.s = univ.Sequence( 154 componentType=namedtype.NamedTypes( 155 namedtype.NamedType('id', univ.Integer()), 156 namedtype.NamedType( 157 'blob', univ.Any().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType 158 ) 159 ) 160 ) 161 162 def testDecodeOpenTypesChoiceOne(self): 163 s, r = decoder.decode( 164 ints2octs((48, 128, 2, 1, 1, 163, 128, 2, 1, 12, 0, 0, 0, 0)), 165 asn1Spec=self.s, decodeOpenTypes=True 166 ) 167 assert not r 168 assert s[0] == 1 169 assert s[1] == 12 170 171 def testDecodeOpenTypesUnknownId(self): 172 s, r = decoder.decode( 173 ints2octs((48, 128, 2, 1, 3, 163, 128, 2, 1, 12, 0, 0, 0, 0)), 174 asn1Spec=self.s, decodeOpenTypes=True 175 ) 176 assert not r 177 assert s[0] == 3 178 assert s[1] == univ.OctetString(hexValue='02010C') 179 180 181class SequenceDecoderWithExplicitlyTaggedOpenTypesTestCase(BaseTestCase): 182 def setUp(self): 183 openType = opentype.OpenType( 184 'id', 185 {1: univ.Integer(), 186 2: univ.OctetString()} 187 ) 188 self.s = univ.Sequence( 189 componentType=namedtype.NamedTypes( 190 namedtype.NamedType('id', univ.Integer()), 191 namedtype.NamedType( 192 'blob', univ.Any().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType 193 ) 194 ) 195 ) 196 197 def testDecodeOpenTypesChoiceOne(self): 198 s, r = decoder.decode( 199 ints2octs((48, 128, 2, 1, 1, 163, 128, 2, 1, 12, 0, 0, 0, 0)), 200 asn1Spec=self.s, decodeOpenTypes=True 201 ) 202 assert not r 203 assert s[0] == 1 204 assert s[1] == 12 205 206 def testDecodeOpenTypesUnknownId(self): 207 s, r = decoder.decode( 208 ints2octs((48, 128, 2, 1, 3, 163, 128, 2, 1, 12, 0, 0, 0, 0)), 209 asn1Spec=self.s, decodeOpenTypes=True 210 ) 211 assert not r 212 assert s[0] == 3 213 assert s[1] == univ.OctetString(hexValue='02010C') 214 215 216class SequenceDecoderWithUntaggedSetOfOpenTypesTestCase(BaseTestCase): 217 def setUp(self): 218 openType = opentype.OpenType( 219 'id', 220 {1: univ.Integer(), 221 2: univ.OctetString()} 222 ) 223 self.s = univ.Sequence( 224 componentType=namedtype.NamedTypes( 225 namedtype.NamedType('id', univ.Integer()), 226 namedtype.NamedType('blob', univ.SetOf(componentType=univ.Any()), 227 openType=openType) 228 ) 229 ) 230 231 def testDecodeOpenTypesChoiceOne(self): 232 s, r = decoder.decode( 233 ints2octs((48, 128, 2, 1, 1, 49, 128, 2, 1, 12, 0, 0, 0, 0)), 234 asn1Spec=self.s, decodeOpenTypes=True 235 ) 236 assert not r 237 assert s[0] == 1 238 assert s[1][0] == 12 239 240 def testDecodeOpenTypesChoiceTwo(self): 241 s, r = decoder.decode( 242 ints2octs((48, 128, 2, 1, 2, 49, 128, 4, 11, 113, 117, 105, 99, 243 107, 32, 98, 114, 111, 119, 110, 0, 0, 0, 0)), 244 asn1Spec=self.s, decodeOpenTypes=True 245 ) 246 assert not r 247 assert s[0] == 2 248 assert s[1][0] == univ.OctetString('quick brown') 249 250 def testDecodeOpenTypesUnknownType(self): 251 try: 252 s, r = decoder.decode( 253 ints2octs((48, 128, 6, 1, 1, 49, 128, 2, 1, 12, 0, 0, 0, 0)), 254 asn1Spec=self.s, decodeOpenTypes=True 255 ) 256 257 except PyAsn1Error: 258 pass 259 260 else: 261 assert False, 'unknown open type tolerated' 262 263 def testDecodeOpenTypesUnknownId(self): 264 s, r = decoder.decode( 265 ints2octs((48, 128, 2, 1, 3, 49, 128, 2, 1, 12, 0, 0, 0, 0)), 266 asn1Spec=self.s, decodeOpenTypes=True 267 ) 268 assert not r 269 assert s[0] == 3 270 assert s[1][0] == univ.OctetString(hexValue='02010c') 271 272 def testDontDecodeOpenTypesChoiceOne(self): 273 s, r = decoder.decode( 274 ints2octs((48, 128, 2, 1, 1, 49, 128, 2, 1, 12, 0, 0, 0, 0)), 275 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, 128, 2, 1, 2, 49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 284 98, 114, 111, 119, 110, 0, 0, 0, 0)), 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