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 encoder 22from pyasn1.compat.octets import ints2octs 23 24 25class OctetStringEncoderTestCase(BaseTestCase): 26 def testDefModeShort(self): 27 assert encoder.encode( 28 univ.OctetString('Quick brown fox') 29 ) == ints2octs((4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120)) 30 31 def testDefModeLong(self): 32 assert encoder.encode( 33 univ.OctetString('Q' * 10000) 34 ) == ints2octs((4, 130, 39, 16) + (81,) * 10000) 35 36 37class BitStringEncoderTestCase(BaseTestCase): 38 def testDefModeShort(self): 39 assert encoder.encode( 40 univ.BitString((1,)) 41 ) == ints2octs((3, 2, 7, 128)) 42 43 def testDefModeLong(self): 44 assert encoder.encode( 45 univ.BitString((1,) * 80000) 46 ) == ints2octs((3, 130, 39, 17, 0) + (255,) * 10000) 47 48 49class SetOfEncoderTestCase(BaseTestCase): 50 def setUp(self): 51 BaseTestCase.setUp(self) 52 53 self.s = univ.SetOf(componentType=univ.OctetString()) 54 55 def testDefMode1(self): 56 self.s.clear() 57 self.s.append('a') 58 self.s.append('ab') 59 60 assert encoder.encode(self.s) == ints2octs((49, 7, 4, 1, 97, 4, 2, 97, 98)) 61 62 def testDefMode2(self): 63 self.s.clear() 64 self.s.append('ab') 65 self.s.append('a') 66 67 assert encoder.encode(self.s) == ints2octs((49, 7, 4, 1, 97, 4, 2, 97, 98)) 68 69 def testDefMode3(self): 70 self.s.clear() 71 self.s.append('b') 72 self.s.append('a') 73 74 assert encoder.encode(self.s) == ints2octs((49, 6, 4, 1, 97, 4, 1, 98)) 75 76 def testDefMode4(self): 77 self.s.clear() 78 self.s.append('a') 79 self.s.append('b') 80 81 assert encoder.encode(self.s) == ints2octs((49, 6, 4, 1, 97, 4, 1, 98)) 82 83 84class SetWithAlternatingChoiceEncoderTestCase(BaseTestCase): 85 def setUp(self): 86 BaseTestCase.setUp(self) 87 88 c = univ.Choice(componentType=namedtype.NamedTypes( 89 namedtype.NamedType('name', univ.OctetString()), 90 namedtype.NamedType('amount', univ.Boolean())) 91 ) 92 93 self.s = univ.Set(componentType=namedtype.NamedTypes( 94 namedtype.NamedType('value', univ.Integer(5)), 95 namedtype.NamedType('status', c)) 96 ) 97 98 def testComponentsOrdering1(self): 99 self.s.setComponentByName('status') 100 self.s.getComponentByName('status').setComponentByPosition(0, 'A') 101 assert encoder.encode(self.s) == ints2octs((49, 6, 2, 1, 5, 4, 1, 65)) 102 103 def testComponentsOrdering2(self): 104 self.s.setComponentByName('status') 105 self.s.getComponentByName('status').setComponentByPosition(1, True) 106 assert encoder.encode(self.s) == ints2octs((49, 6, 1, 1, 255, 2, 1, 5)) 107 108 109class SetWithTaggedChoiceEncoderTestCase(BaseTestCase): 110 111 def testWithUntaggedChoice(self): 112 113 c = univ.Choice( 114 componentType=namedtype.NamedTypes( 115 namedtype.NamedType('premium', univ.Boolean()) 116 ) 117 ) 118 119 s = univ.Set( 120 componentType=namedtype.NamedTypes( 121 namedtype.NamedType('name', univ.OctetString()), 122 namedtype.NamedType('customer', c) 123 ) 124 ) 125 126 s.setComponentByName('name', 'A') 127 s.getComponentByName('customer').setComponentByName('premium', True) 128 129 assert encoder.encode(s) == ints2octs((49, 6, 1, 1, 255, 4, 1, 65)) 130 131 def testWithTaggedChoice(self): 132 133 c = univ.Choice( 134 componentType=namedtype.NamedTypes( 135 namedtype.NamedType('premium', univ.Boolean()) 136 ) 137 ).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 7)) 138 139 s = univ.Set( 140 componentType=namedtype.NamedTypes( 141 namedtype.NamedType('name', univ.OctetString()), 142 namedtype.NamedType('customer', c) 143 ) 144 ) 145 146 s.setComponentByName('name', 'A') 147 s.getComponentByName('customer').setComponentByName('premium', True) 148 149 assert encoder.encode(s) == ints2octs((49, 8, 4, 1, 65, 167, 3, 1, 1, 255)) 150 151 152class SequenceEncoderWithUntaggedOpenTypesTestCase(BaseTestCase): 153 def setUp(self): 154 BaseTestCase.setUp(self) 155 156 openType = opentype.OpenType( 157 'id', 158 {1: univ.Integer(), 159 2: univ.OctetString()} 160 ) 161 self.s = univ.Sequence( 162 componentType=namedtype.NamedTypes( 163 namedtype.NamedType('id', univ.Integer()), 164 namedtype.NamedType('blob', univ.Any(), openType=openType) 165 ) 166 ) 167 168 def testEncodeOpenTypeChoiceOne(self): 169 self.s.clear() 170 171 self.s[0] = 1 172 self.s[1] = univ.Integer(12) 173 174 assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs( 175 (48, 5, 2, 1, 1, 49, 50) 176 ) 177 178 def testEncodeOpenTypeChoiceTwo(self): 179 self.s.clear() 180 181 self.s[0] = 2 182 self.s[1] = univ.OctetString('quick brown') 183 184 assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs( 185 (48, 14, 2, 1, 2, 113, 117, 105, 99, 107, 32, 186 98, 114, 111, 119, 110) 187 ) 188 189 def testEncodeOpenTypeUnknownId(self): 190 self.s.clear() 191 192 self.s[0] = 2 193 self.s[1] = univ.ObjectIdentifier('1.3.6') 194 195 try: 196 encoder.encode(self.s, asn1Spec=self.s) 197 198 except PyAsn1Error: 199 assert False, 'incompatible open type tolerated' 200 201 def testEncodeOpenTypeIncompatibleType(self): 202 self.s.clear() 203 204 self.s[0] = 2 205 self.s[1] = univ.ObjectIdentifier('1.3.6') 206 207 try: 208 encoder.encode(self.s, asn1Spec=self.s) 209 210 except PyAsn1Error: 211 assert False, 'incompatible open type tolerated' 212 213 214class SequenceEncoderWithImplicitlyTaggedOpenTypesTestCase(BaseTestCase): 215 def setUp(self): 216 BaseTestCase.setUp(self) 217 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.Any().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType) 227 ) 228 ) 229 230 def testEncodeOpenTypeChoiceOne(self): 231 self.s.clear() 232 233 self.s[0] = 1 234 self.s[1] = univ.Integer(12) 235 236 assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs( 237 (48, 9, 2, 1, 1, 131, 4, 131, 2, 49, 50) 238 ) 239 240 241class SequenceEncoderWithExplicitlyTaggedOpenTypesTestCase(BaseTestCase): 242 def setUp(self): 243 BaseTestCase.setUp(self) 244 245 openType = opentype.OpenType( 246 'id', 247 {1: univ.Integer(), 248 2: univ.OctetString()} 249 ) 250 self.s = univ.Sequence( 251 componentType=namedtype.NamedTypes( 252 namedtype.NamedType('id', univ.Integer()), 253 namedtype.NamedType('blob', univ.Any().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType) 254 ) 255 ) 256 257 def testEncodeOpenTypeChoiceOne(self): 258 self.s.clear() 259 260 self.s[0] = 1 261 self.s[1] = univ.Integer(12) 262 263 assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs( 264 (48, 9, 2, 1, 1, 163, 4, 163, 2, 49, 50) 265 ) 266 267 268class SequenceEncoderWithUntaggedSetOfOpenTypesTestCase(BaseTestCase): 269 def setUp(self): 270 BaseTestCase.setUp(self) 271 272 openType = opentype.OpenType( 273 'id', 274 {1: univ.Integer(), 275 2: univ.OctetString()} 276 ) 277 self.s = univ.Sequence( 278 componentType=namedtype.NamedTypes( 279 namedtype.NamedType('id', univ.Integer()), 280 namedtype.NamedType('blob', univ.SetOf( 281 componentType=univ.Any()), openType=openType) 282 ) 283 ) 284 285 def testEncodeOpenTypeChoiceOne(self): 286 self.s.clear() 287 288 self.s[0] = 1 289 self.s[1].append(univ.Integer(12)) 290 291 assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs( 292 (48, 7, 2, 1, 1, 49, 2, 49, 50) 293 ) 294 295 def testEncodeOpenTypeChoiceTwo(self): 296 self.s.clear() 297 298 self.s[0] = 2 299 self.s[1].append(univ.OctetString('quick brown')) 300 301 assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs( 302 (48, 16, 2, 1, 2, 49, 11, 113, 117, 105, 99, 107, 32, 98, 114, 303 111, 119, 110) 304 ) 305 306 def testEncodeOpenTypeUnknownId(self): 307 self.s.clear() 308 309 self.s[0] = 2 310 self.s[1].append(univ.ObjectIdentifier('1.3.6')) 311 312 try: 313 encoder.encode(self.s, asn1Spec=self.s) 314 315 except PyAsn1Error: 316 assert False, 'incompatible open type tolerated' 317 318 def testEncodeOpenTypeIncompatibleType(self): 319 self.s.clear() 320 321 self.s[0] = 2 322 self.s[1].append(univ.ObjectIdentifier('1.3.6')) 323 324 try: 325 encoder.encode(self.s, asn1Spec=self.s) 326 327 except PyAsn1Error: 328 assert False, 'incompatible open type tolerated' 329 330 331class SequenceEncoderWithImplicitlyTaggedSetOfOpenTypesTestCase(BaseTestCase): 332 def setUp(self): 333 BaseTestCase.setUp(self) 334 335 openType = opentype.OpenType( 336 'id', 337 {1: univ.Integer(), 338 2: univ.OctetString()} 339 ) 340 self.s = univ.Sequence( 341 componentType=namedtype.NamedTypes( 342 namedtype.NamedType('id', univ.Integer()), 343 namedtype.NamedType('blob', univ.SetOf( 344 componentType=univ.Any().subtype( 345 implicitTag=tag.Tag( 346 tag.tagClassContext, tag.tagFormatSimple, 3))), 347 openType=openType) 348 ) 349 ) 350 351 def testEncodeOpenTypeChoiceOne(self): 352 self.s.clear() 353 354 self.s[0] = 1 355 self.s[1].append(univ.Integer(12)) 356 357 assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs( 358 (48, 11, 2, 1, 1, 49, 6, 131, 4, 131, 2, 49, 50) 359 ) 360 361 362class SequenceEncoderWithExplicitlyTaggedSetOfOpenTypesTestCase(BaseTestCase): 363 def setUp(self): 364 BaseTestCase.setUp(self) 365 366 openType = opentype.OpenType( 367 'id', 368 {1: univ.Integer(), 369 2: univ.OctetString()} 370 ) 371 self.s = univ.Sequence( 372 componentType=namedtype.NamedTypes( 373 namedtype.NamedType('id', univ.Integer()), 374 namedtype.NamedType('blob', univ.SetOf( 375 componentType=univ.Any().subtype( 376 explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3))), 377 openType=openType) 378 ) 379 ) 380 381 def testEncodeOpenTypeChoiceOne(self): 382 self.s.clear() 383 384 self.s[0] = 1 385 self.s[1].append(univ.Integer(12)) 386 387 assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs( 388 (48, 11, 2, 1, 1, 49, 6, 163, 4, 163, 2, 49, 50) 389 ) 390 391 392class NestedOptionalSequenceEncoderTestCase(BaseTestCase): 393 def setUp(self): 394 BaseTestCase.setUp(self) 395 396 inner = univ.Sequence( 397 componentType=namedtype.NamedTypes( 398 namedtype.OptionalNamedType('first-name', univ.OctetString()), 399 namedtype.DefaultedNamedType('age', univ.Integer(33)), 400 ) 401 ) 402 403 outerWithOptional = univ.Sequence( 404 componentType=namedtype.NamedTypes( 405 namedtype.OptionalNamedType('inner', inner), 406 ) 407 ) 408 409 outerWithDefault = univ.Sequence( 410 componentType=namedtype.NamedTypes( 411 namedtype.DefaultedNamedType('inner', inner), 412 ) 413 ) 414 415 self.s1 = outerWithOptional 416 self.s2 = outerWithDefault 417 418 def __initOptionalWithDefaultAndOptional(self): 419 self.s1.clear() 420 self.s1[0][0] = 'test' 421 self.s1[0][1] = 123 422 return self.s1 423 424 def __initOptionalWithDefault(self): 425 self.s1.clear() 426 self.s1[0][1] = 123 427 return self.s1 428 429 def __initOptionalWithOptional(self): 430 self.s1.clear() 431 self.s1[0][0] = 'test' 432 return self.s1 433 434 def __initOptional(self): 435 self.s1.clear() 436 return self.s1 437 438 def __initDefaultWithDefaultAndOptional(self): 439 self.s2.clear() 440 self.s2[0][0] = 'test' 441 self.s2[0][1] = 123 442 return self.s2 443 444 def __initDefaultWithDefault(self): 445 self.s2.clear() 446 self.s2[0][0] = 'test' 447 return self.s2 448 449 def __initDefaultWithOptional(self): 450 self.s2.clear() 451 self.s2[0][1] = 123 452 return self.s2 453 454 def testDefModeOptionalWithDefaultAndOptional(self): 455 s = self.__initOptionalWithDefaultAndOptional() 456 assert encoder.encode(s) == ints2octs((48, 11, 48, 9, 4, 4, 116, 101, 115, 116, 2, 1, 123)) 457 458 def testDefModeOptionalWithDefault(self): 459 s = self.__initOptionalWithDefault() 460 assert encoder.encode(s) == ints2octs((48, 5, 48, 3, 2, 1, 123)) 461 462 def testDefModeOptionalWithOptional(self): 463 s = self.__initOptionalWithOptional() 464 assert encoder.encode(s) == ints2octs((48, 8, 48, 6, 4, 4, 116, 101, 115, 116)) 465 466 def testDefModeOptional(self): 467 s = self.__initOptional() 468 assert encoder.encode(s) == ints2octs((48, 0)) 469 470 def testDefModeDefaultWithDefaultAndOptional(self): 471 s = self.__initDefaultWithDefaultAndOptional() 472 assert encoder.encode(s) == ints2octs((48, 11, 48, 9, 4, 4, 116, 101, 115, 116, 2, 1, 123)) 473 474 def testDefModeDefaultWithDefault(self): 475 s = self.__initDefaultWithDefault() 476 assert encoder.encode(s) == ints2octs((48, 8, 48, 6, 4, 4, 116, 101, 115, 116)) 477 478 def testDefModeDefaultWithOptional(self): 479 s = self.__initDefaultWithOptional() 480 assert encoder.encode(s) == ints2octs((48, 5, 48, 3, 2, 1, 123)) 481 482 483class NestedOptionalChoiceEncoderTestCase(BaseTestCase): 484 def setUp(self): 485 BaseTestCase.setUp(self) 486 487 layer3 = univ.Sequence( 488 componentType=namedtype.NamedTypes( 489 namedtype.OptionalNamedType('first-name', univ.OctetString()), 490 namedtype.DefaultedNamedType('age', univ.Integer(33)), 491 ) 492 ) 493 494 layer2 = univ.Choice( 495 componentType=namedtype.NamedTypes( 496 namedtype.NamedType('inner', layer3), 497 namedtype.NamedType('first-name', univ.OctetString()) 498 ) 499 ) 500 501 layer1 = univ.Sequence( 502 componentType=namedtype.NamedTypes( 503 namedtype.OptionalNamedType('inner', layer2), 504 ) 505 ) 506 507 self.s = layer1 508 509 def __initOptionalWithDefaultAndOptional(self): 510 self.s.clear() 511 self.s[0][0][0] = 'test' 512 self.s[0][0][1] = 123 513 return self.s 514 515 def __initOptionalWithDefault(self): 516 self.s.clear() 517 self.s[0][0][1] = 123 518 return self.s 519 520 def __initOptionalWithOptional(self): 521 self.s.clear() 522 self.s[0][0][0] = 'test' 523 return self.s 524 525 def __initOptional(self): 526 self.s.clear() 527 return self.s 528 529 def testDefModeOptionalWithDefaultAndOptional(self): 530 s = self.__initOptionalWithDefaultAndOptional() 531 assert encoder.encode(s) == ints2octs((48, 11, 48, 9, 4, 4, 116, 101, 115, 116, 2, 1, 123)) 532 533 def testDefModeOptionalWithDefault(self): 534 s = self.__initOptionalWithDefault() 535 assert encoder.encode(s) == ints2octs((48, 5, 48, 3, 2, 1, 123)) 536 537 def testDefModeOptionalWithOptional(self): 538 s = self.__initOptionalWithOptional() 539 assert encoder.encode(s) == ints2octs((48, 8, 48, 6, 4, 4, 116, 101, 115, 116)) 540 541 def testDefModeOptional(self): 542 s = self.__initOptional() 543 assert encoder.encode(s) == ints2octs((48, 0)) 544 545 546class NestedOptionalSequenceOfEncoderTestCase(BaseTestCase): 547 def setUp(self): 548 BaseTestCase.setUp(self) 549 550 layer2 = univ.SequenceOf( 551 componentType=univ.OctetString() 552 ) 553 554 layer1 = univ.Sequence( 555 componentType=namedtype.NamedTypes( 556 namedtype.OptionalNamedType('inner', layer2), 557 ) 558 ) 559 560 self.s = layer1 561 562 def __initOptionalWithValue(self): 563 self.s.clear() 564 self.s[0][0] = 'test' 565 return self.s 566 567 def __initOptional(self): 568 self.s.clear() 569 return self.s 570 571 def testDefModeOptionalWithValue(self): 572 s = self.__initOptionalWithValue() 573 assert encoder.encode(s) == ints2octs((48, 8, 48, 6, 4, 4, 116, 101, 115, 116)) 574 575 def testDefModeOptional(self): 576 s = self.__initOptional() 577 assert encoder.encode(s) == ints2octs((48, 0)) 578 579 580class EmptyInnerFieldOfSequenceEncoderTestCase(BaseTestCase): 581 582 def testInitializedOptionalNullIsEncoded(self): 583 self.s = univ.Sequence( 584 componentType=namedtype.NamedTypes( 585 namedtype.OptionalNamedType('null', univ.Null()) 586 ) 587 ) 588 589 self.s.clear() 590 self.s[0] = '' 591 assert encoder.encode(self.s) == ints2octs((48, 2, 5, 0)) 592 593 def testUninitializedOptionalNullIsNotEncoded(self): 594 self.s = univ.Sequence( 595 componentType=namedtype.NamedTypes( 596 namedtype.OptionalNamedType('null', univ.Null()) 597 ) 598 ) 599 600 self.s.clear() 601 assert encoder.encode(self.s) == ints2octs((48, 0)) 602 603 def testInitializedDefaultNullIsNotEncoded(self): 604 self.s = univ.Sequence( 605 componentType=namedtype.NamedTypes( 606 namedtype.DefaultedNamedType('null', univ.Null('')) 607 ) 608 ) 609 610 self.s.clear() 611 self.s[0] = '' 612 assert encoder.encode(self.s) == ints2octs((48, 0)) 613 614 def testInitializedOptionalOctetStringIsEncoded(self): 615 self.s = univ.Sequence( 616 componentType=namedtype.NamedTypes( 617 namedtype.OptionalNamedType('str', univ.OctetString()) 618 ) 619 ) 620 621 self.s.clear() 622 self.s[0] = '' 623 assert encoder.encode(self.s) == ints2octs((48, 2, 4, 0)) 624 625 def testUninitializedOptionalOctetStringIsNotEncoded(self): 626 self.s = univ.Sequence( 627 componentType=namedtype.NamedTypes( 628 namedtype.OptionalNamedType('str', univ.OctetString()) 629 ) 630 ) 631 632 self.s.clear() 633 assert encoder.encode(self.s) == ints2octs((48, 0)) 634 635 def testInitializedDefaultOctetStringIsNotEncoded(self): 636 self.s = univ.Sequence( 637 componentType=namedtype.NamedTypes( 638 namedtype.DefaultedNamedType('str', univ.OctetString('')) 639 ) 640 ) 641 642 self.s.clear() 643 self.s[0] = '' 644 assert encoder.encode(self.s) == ints2octs((48, 0)) 645 646 647suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 648 649if __name__ == '__main__': 650 unittest.TextTestRunner(verbosity=2).run(suite) 651