1 # 2 # This file is part of pyasn1 software. 3 # 4 # Copyright (c) 2005-2018, Ilya Etingof <etingof (at] gmail.com> 5 # License: http://snmplabs.com/pyasn1/license.html 6 # 7 import math 8 import pickle 9 import sys 10 11 try: 12 import unittest2 as unittest 13 14 except ImportError: 15 import unittest 16 17 from tests.base import BaseTestCase 18 19 from pyasn1.type import univ 20 from pyasn1.type import tag 21 from pyasn1.type import constraint 22 from pyasn1.type import namedtype 23 from pyasn1.type import namedval 24 from pyasn1.type import error 25 from pyasn1.compat.octets import str2octs, ints2octs, octs2ints 26 from pyasn1.error import PyAsn1Error 27 28 29 class NoValueTestCase(BaseTestCase): 30 def testSingleton(self): 31 assert univ.NoValue() is univ.NoValue(), 'NoValue is not a singleton' 32 33 def testRepr(self): 34 try: 35 repr(univ.noValue) 36 37 except PyAsn1Error: 38 assert False, 'repr() on NoValue object fails' 39 40 def testIsInstance(self): 41 try: 42 assert isinstance(univ.noValue, univ.NoValue), 'isinstance() on NoValue() object fails' 43 44 except PyAsn1Error: 45 assert False, 'isinstance() on NoValue object fails' 46 47 def testStr(self): 48 try: 49 str(univ.noValue) 50 51 except PyAsn1Error: 52 pass 53 54 else: 55 assert False, 'str() works for NoValue object' 56 57 def testLen(self): 58 try: 59 len(univ.noValue) 60 61 except PyAsn1Error: 62 pass 63 64 else: 65 assert False, 'len() works for NoValue object' 66 67 def testCmp(self): 68 try: 69 univ.noValue == 1 70 71 except PyAsn1Error: 72 pass 73 74 else: 75 assert False, 'comparison works for NoValue object' 76 77 def testSubs(self): 78 try: 79 univ.noValue[0] 80 81 except PyAsn1Error: 82 pass 83 84 else: 85 assert False, '__getitem__() works for NoValue object' 86 87 def testKey(self): 88 try: 89 univ.noValue['key'] 90 91 except PyAsn1Error: 92 pass 93 94 else: 95 assert False, '__getitem__() works for NoValue object' 96 97 def testKeyAssignment(self): 98 try: 99 univ.noValue['key'] = 123 100 101 except PyAsn1Error: 102 pass 103 104 else: 105 assert False, '__setitem__() works for NoValue object' 106 107 def testInt(self): 108 try: 109 int(univ.noValue) 110 111 except PyAsn1Error: 112 pass 113 114 else: 115 assert False, 'integer conversion works for NoValue object' 116 117 def testAdd(self): 118 try: 119 univ.noValue + univ.noValue 120 121 except PyAsn1Error: 122 pass 123 124 else: 125 assert False, 'addition works for NoValue object' 126 127 def testBitShift(self): 128 try: 129 univ.noValue << 1 130 131 except PyAsn1Error: 132 pass 133 134 else: 135 assert False, 'bitshift works for NoValue object' 136 137 def testBooleanEvaluation(self): 138 try: 139 if univ.noValue: 140 pass 141 142 except PyAsn1Error: 143 pass 144 145 else: 146 assert False, 'boolean evaluation works for NoValue object' 147 148 def testSizeOf(self): 149 try: 150 if hasattr(sys, 'getsizeof'): 151 sys.getsizeof(univ.noValue) 152 else: 153 raise unittest.SkipTest("no sys.getsizeof() method") 154 155 except PyAsn1Error: 156 assert False, 'sizeof failed for NoValue object' 157 except TypeError: 158 raise unittest.SkipTest("sys.getsizeof() raises TypeError") 159 160 161 class IntegerTestCase(BaseTestCase): 162 def testStr(self): 163 assert str(univ.Integer(1)) in ('1', '1L'), 'str() fails' 164 165 def testRepr(self): 166 assert '123' in repr(univ.Integer(123)) 167 168 def testAnd(self): 169 assert univ.Integer(1) & 0 == 0, '__and__() fails' 170 171 def testOr(self): 172 assert univ.Integer(1) | 0 == 1, '__or__() fails' 173 174 def testXor(self): 175 assert univ.Integer(1) ^ 0 == 1, '__xor__() fails' 176 177 def testRand(self): 178 assert 0 & univ.Integer(1) == 0, '__rand__() fails' 179 180 def testRor(self): 181 assert 0 | univ.Integer(1) == 1, '__ror__() fails' 182 183 def testRxor(self): 184 assert 0 ^ univ.Integer(1) == 1, '__rxor__() fails' 185 186 def testAdd(self): 187 assert univ.Integer(-4) + 6 == 2, '__add__() fails' 188 189 def testRadd(self): 190 assert 4 + univ.Integer(5) == 9, '__radd__() fails' 191 192 def testSub(self): 193 assert univ.Integer(3) - 6 == -3, '__sub__() fails' 194 195 def testRsub(self): 196 assert 6 - univ.Integer(3) == 3, '__rsub__() fails' 197 198 def testMul(self): 199 assert univ.Integer(3) * -3 == -9, '__mul__() fails' 200 201 def testRmul(self): 202 assert 2 * univ.Integer(3) == 6, '__rmul__() fails' 203 204 def testDivInt(self): 205 assert univ.Integer(4) / 2 == 2, '__div__() fails' 206 207 if sys.version_info[0] > 2: 208 def testDivFloat(self): 209 assert univ.Integer(3) / 2 == 1.5, '__div__() fails' 210 211 def testRdivFloat(self): 212 assert 3 / univ.Integer(2) == 1.5, '__rdiv__() fails' 213 else: 214 def testDivFloat(self): 215 assert univ.Integer(3) / 2 == 1, '__div__() fails' 216 217 def testRdivFloat(self): 218 assert 3 / univ.Integer(2) == 1, '__rdiv__() fails' 219 220 def testRdivInt(self): 221 assert 6 / univ.Integer(3) == 2, '__rdiv__() fails' 222 223 if sys.version_info[0] > 2: 224 def testTrueDiv(self): 225 assert univ.Integer(3) / univ.Integer(2) == 1.5, '__truediv__() fails' 226 227 def testFloorDiv(self): 228 assert univ.Integer(3) // univ.Integer(2) == 1, '__floordiv__() fails' 229 230 def testMod(self): 231 assert univ.Integer(3) % 2 == 1, '__mod__() fails' 232 233 def testRmod(self): 234 assert 4 % univ.Integer(3) == 1, '__rmod__() fails' 235 236 def testPow(self): 237 assert univ.Integer(3) ** 2 == 9, '__pow__() fails' 238 239 def testRpow(self): 240 assert 2 ** univ.Integer(2) == 4, '__rpow__() fails' 241 242 def testLshift(self): 243 assert univ.Integer(1) << 1 == 2, '<< fails' 244 245 def testRshift(self): 246 assert univ.Integer(2) >> 1 == 1, '>> fails' 247 248 def testInt(self): 249 assert int(univ.Integer(3)) == 3, '__int__() fails' 250 251 def testLong(self): 252 assert int(univ.Integer(8)) == 8, '__long__() fails' 253 254 def testFloat(self): 255 assert float(univ.Integer(4)) == 4.0, '__float__() fails' 256 257 def testPos(self): 258 assert +univ.Integer(1) == 1, '__pos__() fails' 259 260 def testNeg(self): 261 assert -univ.Integer(1) == -1, '__neg__() fails' 262 263 def testInvert(self): 264 assert ~univ.Integer(1) == -2, '__invert__() fails' 265 266 def testRound(self): 267 assert round(univ.Integer(1), 3) == 1.0, '__round__() fails' 268 269 def testFloor(self): 270 assert math.floor(univ.Integer(1)) == 1, '__floor__() fails' 271 272 def testCeil(self): 273 assert math.ceil(univ.Integer(1)) == 1, '__ceil__() fails' 274 275 if sys.version_info[0:2] > (2, 5): 276 def testTrunc(self): 277 assert math.trunc(univ.Integer(1)) == 1, '__trunc__() fails' 278 279 def testPrettyIn(self): 280 assert univ.Integer('3') == 3, 'prettyIn() fails' 281 282 def testTag(self): 283 assert univ.Integer().tagSet == tag.TagSet( 284 (), 285 tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x02) 286 ) 287 288 def testNamedVals(self): 289 290 class Integer(univ.Integer): 291 namedValues = univ.Integer.namedValues.clone(('asn1', 1)) 292 293 assert Integer('asn1') == 1, 'named val fails' 294 assert int(Integer('asn1')) == 1, 'named val fails' 295 assert str(Integer('asn1')) == 'asn1', 'named val __str__() fails' 296 297 def testSubtype(self): 298 assert univ.Integer().subtype( 299 value=1, 300 implicitTag=tag.Tag(tag.tagClassPrivate, tag.tagFormatSimple, 2), 301 subtypeSpec=constraint.SingleValueConstraint(1, 3) 302 ) == univ.Integer( 303 value=1, 304 tagSet=tag.TagSet(tag.Tag(tag.tagClassPrivate, 305 tag.tagFormatSimple, 2)), 306 subtypeSpec=constraint.ConstraintsIntersection(constraint.SingleValueConstraint(1, 3)) 307 ) 308 309 310 class IntegerPicklingTestCase(unittest.TestCase): 311 312 def testSchemaPickling(self): 313 old_asn1 = univ.Integer() 314 serialised = pickle.dumps(old_asn1) 315 assert serialised 316 new_asn1 = pickle.loads(serialised) 317 assert type(new_asn1) == univ.Integer 318 assert old_asn1.isSameTypeWith(new_asn1) 319 320 def testValuePickling(self): 321 old_asn1 = univ.Integer(-123) 322 serialised = pickle.dumps(old_asn1) 323 assert serialised 324 new_asn1 = pickle.loads(serialised) 325 assert new_asn1 == -123 326 327 328 class BooleanTestCase(BaseTestCase): 329 def testTruth(self): 330 assert univ.Boolean(True) and univ.Boolean(1), 'Truth initializer fails' 331 332 def testFalse(self): 333 assert not univ.Boolean(False) and not univ.Boolean(0), 'False initializer fails' 334 335 def testStr(self): 336 assert str(univ.Boolean(1)) == 'True', 'str() fails' 337 338 def testInt(self): 339 assert int(univ.Boolean(1)) == 1, 'int() fails' 340 341 def testRepr(self): 342 assert 'Boolean' in repr(univ.Boolean(1)) 343 344 def testTag(self): 345 assert univ.Boolean().tagSet == tag.TagSet( 346 (), 347 tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x01) 348 ) 349 350 def testConstraints(self): 351 352 class Boolean(univ.Boolean): 353 pass 354 355 try: 356 Boolean(2) 357 except error.ValueConstraintError: 358 pass 359 else: 360 assert 0, 'constraint fail' 361 362 363 class BooleanPicklingTestCase(unittest.TestCase): 364 365 def testSchemaPickling(self): 366 old_asn1 = univ.Boolean() 367 serialised = pickle.dumps(old_asn1) 368 assert serialised 369 new_asn1 = pickle.loads(serialised) 370 assert type(new_asn1) == univ.Boolean 371 assert old_asn1.isSameTypeWith(new_asn1) 372 373 def testValuePickling(self): 374 old_asn1 = univ.Boolean(True) 375 serialised = pickle.dumps(old_asn1) 376 assert serialised 377 new_asn1 = pickle.loads(serialised) 378 assert new_asn1 == True 379 380 381 class BitStringTestCase(BaseTestCase): 382 def setUp(self): 383 BaseTestCase.setUp(self) 384 385 self.b = univ.BitString( 386 namedValues=namedval.NamedValues(('Active', 0), ('Urgent', 1)) 387 ) 388 389 def testBinDefault(self): 390 391 class BinDefault(univ.BitString): 392 defaultBinValue = '1010100110001010' 393 394 assert BinDefault() == univ.BitString(binValue='1010100110001010') 395 396 def testHexDefault(self): 397 398 class HexDefault(univ.BitString): 399 defaultHexValue = 'A98A' 400 401 assert HexDefault() == univ.BitString(hexValue='A98A') 402 403 def testSet(self): 404 assert self.b.clone('Active') == (1,) 405 assert self.b.clone('Urgent') == (0, 1) 406 assert self.b.clone('Urgent, Active') == (1, 1) 407 assert self.b.clone("'1010100110001010'B") == (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0) 408 assert self.b.clone("'A98A'H") == (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0) 409 assert self.b.clone(binValue='1010100110001010') == (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0) 410 assert self.b.clone(hexValue='A98A') == (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0) 411 assert self.b.clone('1010100110001010') == (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0) 412 assert self.b.clone((1, 0, 1)) == (1, 0, 1) 413 414 def testStr(self): 415 assert str(self.b.clone('Urgent')) == '01' 416 417 def testRepr(self): 418 assert 'BitString' in repr(self.b.clone('Urgent,Active')) 419 420 def testTag(self): 421 assert univ.BitString().tagSet == tag.TagSet( 422 (), 423 tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x03) 424 ) 425 426 def testLen(self): 427 assert len(self.b.clone("'A98A'H")) == 16 428 429 def testGetItem(self): 430 assert self.b.clone("'A98A'H")[0] == 1 431 assert self.b.clone("'A98A'H")[1] == 0 432 assert self.b.clone("'A98A'H")[2] == 1 433 434 if sys.version_info[:2] > (2, 4): 435 def testReverse(self): 436 assert list(reversed(univ.BitString([0, 0, 1]))) == list(univ.BitString([1, 0, 0])) 437 438 def testAsOctets(self): 439 assert self.b.clone(hexValue='A98A').asOctets() == ints2octs((0xa9, 0x8a)), 'testAsOctets() fails' 440 441 def testAsInts(self): 442 assert self.b.clone(hexValue='A98A').asNumbers() == (0xa9, 0x8a), 'testAsNumbers() fails' 443 444 def testMultipleOfEightPadding(self): 445 assert self.b.clone((1, 0, 1)).asNumbers() == (5,) 446 447 def testAsInteger(self): 448 assert self.b.clone('11000000011001').asInteger() == 12313 449 assert self.b.clone('1100110011011111').asInteger() == 52447 450 451 def testStaticDef(self): 452 453 class BitString(univ.BitString): 454 pass 455 456 assert BitString('11000000011001').asInteger() == 12313 457 458 459 class BitStringPicklingTestCase(unittest.TestCase): 460 461 def testSchemaPickling(self): 462 old_asn1 = univ.BitString() 463 serialised = pickle.dumps(old_asn1) 464 assert serialised 465 new_asn1 = pickle.loads(serialised) 466 assert type(new_asn1) == univ.BitString 467 assert old_asn1.isSameTypeWith(new_asn1) 468 469 def testValuePickling(self): 470 old_asn1 = univ.BitString((1, 0, 1, 0)) 471 serialised = pickle.dumps(old_asn1) 472 assert serialised 473 new_asn1 = pickle.loads(serialised) 474 assert new_asn1 == (1, 0, 1, 0) 475 476 477 class OctetStringWithUnicodeMixIn(object): 478 479 initializer = () 480 encoding = 'us-ascii' 481 482 def setUp(self): 483 self.pythonString = ints2octs(self.initializer).decode(self.encoding) 484 self.encodedPythonString = self.pythonString.encode(self.encoding) 485 self.numbersString = tuple(octs2ints(self.encodedPythonString)) 486 487 def testInit(self): 488 assert univ.OctetString(self.encodedPythonString) == self.encodedPythonString, '__init__() fails' 489 490 def testInitFromAsn1(self): 491 assert univ.OctetString(univ.OctetString(self.encodedPythonString)) == self.encodedPythonString 492 assert univ.OctetString(univ.Integer(123)) == univ.OctetString('123') 493 494 def testSerialised(self): 495 if sys.version_info[0] < 3: 496 assert str(univ.OctetString(self.encodedPythonString, encoding=self.encoding)) == self.encodedPythonString, '__str__() fails' 497 else: 498 assert bytes(univ.OctetString(self.encodedPythonString, encoding=self.encoding)) == self.encodedPythonString, '__str__() fails' 499 500 def testPrintable(self): 501 if sys.version_info[0] < 3: 502 assert str(univ.OctetString(self.encodedPythonString, encoding=self.encoding)) == self.encodedPythonString, '__str__() fails' 503 assert unicode(univ.OctetString(self.pythonString, encoding=self.encoding)) == self.pythonString, 'unicode init fails' 504 else: 505 assert str(univ.OctetString(self.pythonString, encoding=self.encoding)) == self.pythonString, 'unicode init fails' 506 507 def testSeq(self): 508 assert univ.OctetString(self.encodedPythonString)[0] == self.encodedPythonString[0], '__getitem__() fails' 509 510 def testRepr(self): 511 assert 'abc' in repr(univ.OctetString('abc')) 512 513 def testAsOctets(self): 514 assert univ.OctetString(self.encodedPythonString).asOctets() == self.encodedPythonString, 'testAsOctets() fails' 515 516 def testAsInts(self): 517 assert univ.OctetString(self.encodedPythonString).asNumbers() == self.numbersString, 'testAsNumbers() fails' 518 519 def testAdd(self): 520 assert univ.OctetString(self.encodedPythonString) + self.encodedPythonString == self.encodedPythonString + self.encodedPythonString, '__add__() fails' 521 522 def testRadd(self): 523 assert self.encodedPythonString + univ.OctetString(self.encodedPythonString) == self.encodedPythonString + self.encodedPythonString, '__radd__() fails' 524 525 def testMul(self): 526 assert univ.OctetString(self.encodedPythonString) * 2 == self.encodedPythonString * 2, '__mul__() fails' 527 528 def testRmul(self): 529 assert 2 * univ.OctetString(self.encodedPythonString) == 2 * self.encodedPythonString, '__rmul__() fails' 530 531 def testContains(self): 532 s = univ.OctetString(self.encodedPythonString) 533 assert self.encodedPythonString in s 534 assert self.encodedPythonString * 2 not in s 535 536 if sys.version_info[:2] > (2, 4): 537 def testReverse(self): 538 assert list(reversed(univ.OctetString(self.encodedPythonString))) == list(reversed(self.encodedPythonString)) 539 540 541 class OctetStringWithAsciiTestCase(OctetStringWithUnicodeMixIn, BaseTestCase): 542 initializer = (97, 102) 543 encoding = 'us-ascii' 544 545 546 class OctetStringWithUtf8TestCase(OctetStringWithUnicodeMixIn, BaseTestCase): 547 initializer = (208, 176, 208, 177, 208, 178) 548 encoding = 'utf-8' 549 550 551 class OctetStringWithUtf16TestCase(OctetStringWithUnicodeMixIn, BaseTestCase): 552 initializer = (4, 48, 4, 49, 4, 50) 553 encoding = 'utf-16-be' 554 555 556 if sys.version_info[0] > 2: 557 558 # Somehow comparison of UTF-32 encoded strings does not work in Py2 559 560 class OctetStringWithUtf32TestCase(OctetStringWithUnicodeMixIn, BaseTestCase): 561 initializer = (0, 0, 4, 48, 0, 0, 4, 49, 0, 0, 4, 50) 562 encoding = 'utf-32-be' 563 564 565 class OctetStringTestCase(BaseTestCase): 566 567 def testBinDefault(self): 568 569 class BinDefault(univ.OctetString): 570 defaultBinValue = '1000010111101110101111000000111011' 571 572 assert BinDefault() == univ.OctetString(binValue='1000010111101110101111000000111011') 573 574 def testHexDefault(self): 575 576 class HexDefault(univ.OctetString): 577 defaultHexValue = 'FA9823C43E43510DE3422' 578 579 assert HexDefault() == univ.OctetString(hexValue='FA9823C43E43510DE3422') 580 581 def testBinStr(self): 582 assert univ.OctetString(binValue="1000010111101110101111000000111011") == ints2octs((133, 238, 188, 14, 192)), 'bin init fails' 583 584 def testHexStr(self): 585 assert univ.OctetString(hexValue="FA9823C43E43510DE3422") == ints2octs((250, 152, 35, 196, 62, 67, 81, 13, 227, 66, 32)), 'hex init fails' 586 587 def testTuple(self): 588 assert univ.OctetString((1, 2, 3, 4, 5)) == ints2octs((1, 2, 3, 4, 5)), 'tuple init failed' 589 590 def testRepr(self): 591 assert 'abc' in repr(univ.OctetString('abc')) 592 593 def testEmpty(self): 594 try: 595 str(univ.OctetString()) 596 except PyAsn1Error: 597 pass 598 else: 599 assert 0, 'empty OctetString() not reported' 600 601 def testTag(self): 602 assert univ.OctetString().tagSet == tag.TagSet( 603 (), 604 tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x04) 605 ) 606 607 def testStaticDef(self): 608 609 class OctetString(univ.OctetString): 610 pass 611 612 assert OctetString(hexValue="FA9823C43E43510DE3422") == ints2octs((250, 152, 35, 196, 62, 67, 81, 13, 227, 66, 32)) 613 614 615 class OctetStringPicklingTestCase(unittest.TestCase): 616 617 def testSchemaPickling(self): 618 old_asn1 = univ.BitString() 619 serialised = pickle.dumps(old_asn1) 620 assert serialised 621 new_asn1 = pickle.loads(serialised) 622 assert type(new_asn1) == univ.BitString 623 assert old_asn1.isSameTypeWith(new_asn1) 624 625 def testValuePickling(self): 626 old_asn1 = univ.BitString((1, 0, 1, 0)) 627 serialised = pickle.dumps(old_asn1) 628 assert serialised 629 new_asn1 = pickle.loads(serialised) 630 assert new_asn1 == (1, 0, 1, 0) 631 632 633 class Null(BaseTestCase): 634 635 def testInit(self): 636 assert not univ.Null().isValue 637 assert univ.Null(0) == str2octs('') 638 assert univ.Null(False) == str2octs('') 639 assert univ.Null('') == str2octs('') 640 assert univ.Null(None) == str2octs('') 641 642 try: 643 assert univ.Null(True) 644 645 except PyAsn1Error: 646 pass 647 648 try: 649 assert univ.Null('xxx') 650 651 except PyAsn1Error: 652 pass 653 654 def testStr(self): 655 assert str(univ.Null('')) == '', 'str() fails' 656 657 def testRepr(self): 658 assert 'Null' in repr(univ.Null('')) 659 660 def testTag(self): 661 assert univ.Null().tagSet == tag.TagSet( 662 (), 663 tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x05) 664 ) 665 666 def testConstraints(self): 667 try: 668 univ.Null(2) 669 except error.ValueConstraintError: 670 pass 671 else: 672 assert 0, 'constraint fail' 673 674 def testStaticDef(self): 675 676 class Null(univ.Null): 677 pass 678 679 assert not Null('') 680 681 682 class NullPicklingTestCase(unittest.TestCase): 683 684 def testSchemaPickling(self): 685 old_asn1 = univ.Null() 686 serialised = pickle.dumps(old_asn1) 687 assert serialised 688 new_asn1 = pickle.loads(serialised) 689 assert type(new_asn1) == univ.Null 690 assert old_asn1.isSameTypeWith(new_asn1) 691 692 def testValuePickling(self): 693 old_asn1 = univ.Null('') 694 serialised = pickle.dumps(old_asn1) 695 assert serialised 696 new_asn1 = pickle.loads(serialised) 697 assert not new_asn1 698 699 700 class RealTestCase(BaseTestCase): 701 def testFloat4BinEnc(self): 702 assert univ.Real((0.25, 2, 3)) == 2.0, 'float initializer for binary encoding fails' 703 704 def testStr(self): 705 assert str(univ.Real(1.0)) == '1.0', 'str() fails' 706 707 def testRepr(self): 708 assert 'Real' in repr(univ.Real(-4.1)) 709 assert 'Real' in repr(univ.Real(-4.1)) 710 assert 'inf' in repr(univ.Real('inf')) 711 assert '-inf' in repr(univ.Real('-inf')) 712 713 def testAdd(self): 714 assert univ.Real(-4.1) + 1.4 == -2.7, '__add__() fails' 715 716 def testRadd(self): 717 assert 4 + univ.Real(0.5) == 4.5, '__radd__() fails' 718 719 def testSub(self): 720 assert univ.Real(3.9) - 1.7 == 2.2, '__sub__() fails' 721 722 def testRsub(self): 723 assert 6.1 - univ.Real(0.1) == 6, '__rsub__() fails' 724 725 def testMul(self): 726 assert univ.Real(3.0) * -3 == -9, '__mul__() fails' 727 728 def testRmul(self): 729 assert 2 * univ.Real(3.0) == 6, '__rmul__() fails' 730 731 def testDiv(self): 732 assert univ.Real(3.0) / 2 == 1.5, '__div__() fails' 733 734 def testRdiv(self): 735 assert 6 / univ.Real(3.0) == 2, '__rdiv__() fails' 736 737 def testMod(self): 738 assert univ.Real(3.0) % 2 == 1, '__mod__() fails' 739 740 def testRmod(self): 741 assert 4 % univ.Real(3.0) == 1, '__rmod__() fails' 742 743 def testPow(self): 744 assert univ.Real(3.0) ** 2 == 9, '__pow__() fails' 745 746 def testRpow(self): 747 assert 2 ** univ.Real(2.0) == 4, '__rpow__() fails' 748 749 def testInt(self): 750 assert int(univ.Real(3.0)) == 3, '__int__() fails' 751 752 def testLong(self): 753 assert int(univ.Real(8.0)) == 8, '__long__() fails' 754 755 def testFloat(self): 756 assert float(univ.Real(4.0)) == 4.0, '__float__() fails' 757 758 def testPrettyIn(self): 759 assert univ.Real((3, 10, 0)) == 3, 'prettyIn() fails' 760 761 # infinite float values 762 def testStrInf(self): 763 assert str(univ.Real('inf')) == 'inf', 'str() fails' 764 765 def testAddInf(self): 766 assert univ.Real('inf') + 1 == float('inf'), '__add__() fails' 767 768 def testRaddInf(self): 769 assert 1 + univ.Real('inf') == float('inf'), '__radd__() fails' 770 771 def testIntInf(self): 772 try: 773 assert int(univ.Real('inf')) 774 except OverflowError: 775 pass 776 else: 777 assert 0, '__int__() fails' 778 779 def testLongInf(self): 780 try: 781 assert int(univ.Real('inf')) 782 except OverflowError: 783 pass 784 else: 785 assert 0, '__long__() fails' 786 assert int(univ.Real(8.0)) == 8, '__long__() fails' 787 788 def testFloatInf(self): 789 assert float(univ.Real('-inf')) == float('-inf'), '__float__() fails' 790 791 def testPrettyInInf(self): 792 assert univ.Real(float('inf')) == float('inf'), 'prettyIn() fails' 793 794 def testPlusInf(self): 795 assert univ.Real('inf').isPlusInf, 'isPlusInfinity failed' 796 797 def testMinusInf(self): 798 assert univ.Real('-inf').isMinusInf, 'isMinusInfinity failed' 799 800 def testPos(self): 801 assert +univ.Real(1.0) == 1.0, '__pos__() fails' 802 803 def testNeg(self): 804 assert -univ.Real(1.0) == -1.0, '__neg__() fails' 805 806 def testRound(self): 807 assert round(univ.Real(1.123), 2) == 1.12, '__round__() fails' 808 809 def testFloor(self): 810 assert math.floor(univ.Real(1.6)) == 1.0, '__floor__() fails' 811 812 def testCeil(self): 813 assert math.ceil(univ.Real(1.2)) == 2.0, '__ceil__() fails' 814 815 if sys.version_info[0:2] > (2, 5): 816 def testTrunc(self): 817 assert math.trunc(univ.Real(1.1)) == 1.0, '__trunc__() fails' 818 819 def testTag(self): 820 assert univ.Real().tagSet == tag.TagSet( 821 (), 822 tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x09) 823 ) 824 825 def testStaticDef(self): 826 827 class Real(univ.Real): 828 pass 829 830 assert Real(1.0) == 1.0 831 832 833 class RealPicklingTestCase(unittest.TestCase): 834 835 def testSchemaPickling(self): 836 old_asn1 = univ.Real() 837 serialised = pickle.dumps(old_asn1) 838 assert serialised 839 new_asn1 = pickle.loads(serialised) 840 assert type(new_asn1) == univ.Real 841 assert old_asn1.isSameTypeWith(new_asn1) 842 843 def testValuePickling(self): 844 old_asn1 = univ.Real((1, 10, 3)) 845 serialised = pickle.dumps(old_asn1) 846 assert serialised 847 new_asn1 = pickle.loads(serialised) 848 assert new_asn1 == 1000 849 850 851 class ObjectIdentifier(BaseTestCase): 852 def testStr(self): 853 assert str(univ.ObjectIdentifier((1, 3, 6))) == '1.3.6', 'str() fails' 854 855 def testRepr(self): 856 assert '1.3.6' in repr(univ.ObjectIdentifier('1.3.6')) 857 858 def testEq(self): 859 assert univ.ObjectIdentifier((1, 3, 6)) == (1, 3, 6), '__cmp__() fails' 860 861 def testAdd(self): 862 assert univ.ObjectIdentifier((1, 3)) + (6,) == (1, 3, 6), '__add__() fails' 863 864 def testRadd(self): 865 assert (1,) + univ.ObjectIdentifier((3, 6)) == (1, 3, 6), '__radd__() fails' 866 867 def testLen(self): 868 assert len(univ.ObjectIdentifier((1, 3))) == 2, '__len__() fails' 869 870 def testPrefix(self): 871 o = univ.ObjectIdentifier('1.3.6') 872 assert o.isPrefixOf((1, 3, 6)), 'isPrefixOf() fails' 873 assert o.isPrefixOf((1, 3, 6, 1)), 'isPrefixOf() fails' 874 assert not o.isPrefixOf((1, 3)), 'isPrefixOf() fails' 875 876 def testInput1(self): 877 assert univ.ObjectIdentifier('1.3.6') == (1, 3, 6), 'prettyIn() fails' 878 879 def testInput2(self): 880 assert univ.ObjectIdentifier((1, 3, 6)) == (1, 3, 6), 'prettyIn() fails' 881 882 def testInput3(self): 883 assert univ.ObjectIdentifier(univ.ObjectIdentifier('1.3') + (6,)) == (1, 3, 6), 'prettyIn() fails' 884 885 def testUnicode(self): 886 s = '1.3.6' 887 if sys.version_info[0] < 3: 888 s = s.decode() 889 assert univ.ObjectIdentifier(s) == (1, 3, 6), 'unicode init fails' 890 891 def testTag(self): 892 assert univ.ObjectIdentifier().tagSet == tag.TagSet( 893 (), 894 tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x06) 895 ) 896 897 def testContains(self): 898 s = univ.ObjectIdentifier('1.3.6.1234.99999') 899 assert 1234 in s 900 assert 4321 not in s 901 902 def testStaticDef(self): 903 904 class ObjectIdentifier(univ.ObjectIdentifier): 905 pass 906 907 assert str(ObjectIdentifier((1, 3, 6))) == '1.3.6' 908 909 910 class ObjectIdentifierPicklingTestCase(unittest.TestCase): 911 912 def testSchemaPickling(self): 913 old_asn1 = univ.ObjectIdentifier() 914 serialised = pickle.dumps(old_asn1) 915 assert serialised 916 new_asn1 = pickle.loads(serialised) 917 assert type(new_asn1) == univ.ObjectIdentifier 918 assert old_asn1.isSameTypeWith(new_asn1) 919 920 def testValuePickling(self): 921 old_asn1 = univ.ObjectIdentifier('2.3.1.1.2') 922 serialised = pickle.dumps(old_asn1) 923 assert serialised 924 new_asn1 = pickle.loads(serialised) 925 assert new_asn1 == (2, 3, 1, 1, 2) 926 927 928 class SequenceOf(BaseTestCase): 929 def setUp(self): 930 BaseTestCase.setUp(self) 931 self.s1 = univ.SequenceOf( 932 componentType=univ.OctetString('') 933 ) 934 self.s2 = self.s1.clone() 935 936 def testRepr(self): 937 assert 'a' in repr(self.s1.clone().setComponents('a', 'b')) 938 939 def testTag(self): 940 assert self.s1.tagSet == tag.TagSet( 941 (), 942 tag.Tag(tag.tagClassUniversal, tag.tagFormatConstructed, 0x10) 943 ), 'wrong tagSet' 944 945 def testSeq(self): 946 self.s1.setComponentByPosition(0, univ.OctetString('abc')) 947 assert self.s1[0] == str2octs('abc'), 'set by idx fails' 948 self.s1[0] = 'cba' 949 assert self.s1[0] == str2octs('cba'), 'set by idx fails' 950 951 def testCmp(self): 952 self.s1.clear() 953 self.s1.setComponentByPosition(0, 'abc') 954 self.s2.clear() 955 self.s2.setComponentByPosition(0, univ.OctetString('abc')) 956 assert self.s1 == self.s2, '__cmp__() fails' 957 958 def testSubtypeSpec(self): 959 s = self.s1.clone(subtypeSpec=constraint.ConstraintsUnion( 960 constraint.SingleValueConstraint(str2octs('abc')) 961 )) 962 try: 963 s.setComponentByPosition(0, univ.OctetString('abc')) 964 except PyAsn1Error: 965 assert 0, 'constraint fails' 966 try: 967 s.setComponentByPosition(1, univ.OctetString('Abc')) 968 except PyAsn1Error: 969 try: 970 s.setComponentByPosition(1, univ.OctetString('Abc'), 971 verifyConstraints=False) 972 except PyAsn1Error: 973 assert 0, 'constraint failes with verifyConstraints=True' 974 else: 975 assert 0, 'constraint fails' 976 977 def testComponentTagsMatching(self): 978 s = self.s1.clone() 979 s.strictConstraints = True # This requires types equality 980 o = univ.OctetString('abc').subtype(explicitTag=tag.Tag(tag.tagClassPrivate, tag.tagFormatSimple, 12)) 981 try: 982 s.setComponentByPosition(0, o) 983 except PyAsn1Error: 984 pass 985 else: 986 assert 0, 'inner supertype tag allowed' 987 988 def testComponentConstraintsMatching(self): 989 s = self.s1.clone() 990 o = univ.OctetString().subtype( 991 subtypeSpec=constraint.ConstraintsUnion(constraint.SingleValueConstraint(str2octs('cba')))) 992 s.strictConstraints = True # This requires types equality 993 try: 994 s.setComponentByPosition(0, o.clone('cba')) 995 except PyAsn1Error: 996 pass 997 else: 998 assert 0, 'inner supertype constraint allowed' 999 s.strictConstraints = False # This requires subtype relationships 1000 try: 1001 s.setComponentByPosition(0, o.clone('cba')) 1002 except PyAsn1Error: 1003 assert 0, 'inner supertype constraint disallowed' 1004 else: 1005 pass 1006 1007 def testSizeSpec(self): 1008 s = self.s1.clone(sizeSpec=constraint.ConstraintsUnion( 1009 constraint.ValueSizeConstraint(1, 1) 1010 )) 1011 s.setComponentByPosition(0, univ.OctetString('abc')) 1012 try: 1013 s.verifySizeSpec() 1014 except PyAsn1Error: 1015 assert 0, 'size spec fails' 1016 s.setComponentByPosition(1, univ.OctetString('abc')) 1017 try: 1018 s.verifySizeSpec() 1019 except PyAsn1Error: 1020 pass 1021 else: 1022 assert 0, 'size spec fails' 1023 1024 def testGetComponentTagMap(self): 1025 assert self.s1.componentType.tagMap.presentTypes == { 1026 univ.OctetString.tagSet: univ.OctetString('') 1027 } 1028 1029 def testSubtype(self): 1030 self.s1.clear() 1031 assert self.s1.subtype( 1032 implicitTag=tag.Tag(tag.tagClassPrivate, tag.tagFormatSimple, 2), 1033 subtypeSpec=constraint.SingleValueConstraint(1, 3), 1034 sizeSpec=constraint.ValueSizeConstraint(0, 1) 1035 ) == self.s1.clone( 1036 tagSet=tag.TagSet(tag.Tag(tag.tagClassPrivate, 1037 tag.tagFormatSimple, 2)), 1038 subtypeSpec=constraint.ConstraintsIntersection(constraint.SingleValueConstraint(1, 3)), 1039 sizeSpec=constraint.ValueSizeConstraint(0, 1) 1040 ) 1041 1042 def testClone(self): 1043 self.s1.setComponentByPosition(0, univ.OctetString('abc')) 1044 s = self.s1.clone() 1045 assert len(s) == 0 1046 s = self.s1.clone(cloneValueFlag=1) 1047 assert len(s) == 1 1048 assert s.getComponentByPosition(0) == self.s1.getComponentByPosition(0) 1049 1050 def testSetComponents(self): 1051 assert self.s1.clone().setComponents('abc', 'def') == \ 1052 self.s1.setComponentByPosition(0, 'abc').setComponentByPosition(1, 'def') 1053 1054 def testGetItem(self): 1055 s = self.s1.clone() 1056 s.append('xxx') 1057 assert s[0] 1058 1059 try: 1060 s[2] 1061 1062 except IndexError: 1063 pass 1064 1065 else: 1066 assert False, 'IndexError not raised' 1067 1068 # this is a deviation from standart sequence protocol 1069 assert not s[1] 1070 1071 def testSetItem(self): 1072 s = self.s1.clone() 1073 s.append('xxx') 1074 1075 try: 1076 1077 s[2] = 'xxx' 1078 1079 except IndexError: 1080 pass 1081 1082 else: 1083 assert False, 'IndexError not raised' 1084 1085 def testAppend(self): 1086 self.s1.clear() 1087 self.s1.setComponentByPosition(0, univ.OctetString('abc')) 1088 assert len(self.s1) == 1 1089 self.s1.append('def') 1090 assert len(self.s1) == 2 1091 assert list(self.s1) == [str2octs(x) for x in ['abc', 'def']] 1092 1093 def testExtend(self): 1094 self.s1.clear() 1095 self.s1.setComponentByPosition(0, univ.OctetString('abc')) 1096 assert len(self.s1) == 1 1097 self.s1.extend(['def', 'ghi']) 1098 assert len(self.s1) == 3 1099 assert list(self.s1) == [str2octs(x) for x in ['abc', 'def', 'ghi']] 1100 1101 def testCount(self): 1102 self.s1.clear() 1103 for x in ['abc', 'def', 'abc']: 1104 self.s1.append(x) 1105 assert self.s1.count(str2octs('abc')) == 2 1106 assert self.s1.count(str2octs('def')) == 1 1107 assert self.s1.count(str2octs('ghi')) == 0 1108 1109 def testIndex(self): 1110 self.s1.clear() 1111 for x in ['abc', 'def', 'abc']: 1112 self.s1.append(x) 1113 assert self.s1.index(str2octs('abc')) == 0 1114 assert self.s1.index(str2octs('def')) == 1 1115 assert self.s1.index(str2octs('abc'), 1) == 2 1116 1117 def testSort(self): 1118 self.s1.clear() 1119 self.s1[0] = 'b' 1120 self.s1[1] = 'a' 1121 assert list(self.s1) == [str2octs('b'), str2octs('a')] 1122 self.s1.sort() 1123 assert list(self.s1) == [str2octs('a'), str2octs('b')] 1124 1125 def testStaticDef(self): 1126 1127 class SequenceOf(univ.SequenceOf): 1128 componentType = univ.OctetString('') 1129 1130 s = SequenceOf() 1131 s[0] = 'abc' 1132 assert len(s) == 1 1133 assert s == [str2octs('abc')] 1134 1135 def testLegacyInitializer(self): 1136 n = univ.SequenceOf( 1137 componentType=univ.OctetString() 1138 ) 1139 o = univ.SequenceOf( 1140 univ.OctetString() # this is the old way 1141 ) 1142 1143 assert n.isSameTypeWith(o) and o.isSameTypeWith(n) 1144 1145 n[0] = 'fox' 1146 o[0] = 'fox' 1147 1148 assert n == o 1149 1150 def testGetComponentWithDefault(self): 1151 1152 class SequenceOf(univ.SequenceOf): 1153 componentType = univ.OctetString() 1154 1155 s = SequenceOf() 1156 assert s.getComponentByPosition(0, default=None, instantiate=False) is None 1157 assert s.getComponentByPosition(0, default=None) is None 1158 s[0] = 'test' 1159 assert s.getComponentByPosition(0, default=None) is not None 1160 assert s.getComponentByPosition(0, default=None) == str2octs('test') 1161 s.clear() 1162 assert s.getComponentByPosition(0, default=None) is None 1163 1164 def testGetComponentNoInstantiation(self): 1165 1166 class SequenceOf(univ.SequenceOf): 1167 componentType = univ.OctetString() 1168 1169 s = SequenceOf() 1170 assert s.getComponentByPosition(0, instantiate=False) is univ.noValue 1171 s[0] = 'test' 1172 assert s.getComponentByPosition(0, instantiate=False) is not univ.noValue 1173 assert s.getComponentByPosition(0, instantiate=False) == str2octs('test') 1174 s.clear() 1175 assert s.getComponentByPosition(0, instantiate=False) is univ.noValue 1176 1177 1178 class SequenceOfPicklingTestCase(unittest.TestCase): 1179 1180 def testSchemaPickling(self): 1181 old_asn1 = univ.SequenceOf(componentType=univ.OctetString()) 1182 serialised = pickle.dumps(old_asn1) 1183 assert serialised 1184 new_asn1 = pickle.loads(serialised) 1185 assert type(new_asn1) == univ.SequenceOf 1186 assert old_asn1.isSameTypeWith(new_asn1) 1187 1188 def testValuePickling(self): 1189 old_asn1 = univ.SequenceOf(componentType=univ.OctetString()) 1190 old_asn1[0] = 'test' 1191 serialised = pickle.dumps(old_asn1) 1192 assert serialised 1193 new_asn1 = pickle.loads(serialised) 1194 assert new_asn1 1195 assert new_asn1 == [str2octs('test')] 1196 1197 1198 class Sequence(BaseTestCase): 1199 def setUp(self): 1200 BaseTestCase.setUp(self) 1201 self.s1 = univ.Sequence( 1202 componentType=namedtype.NamedTypes( 1203 namedtype.NamedType('name', univ.OctetString('')), 1204 namedtype.OptionalNamedType('nick', univ.OctetString('')), 1205 namedtype.DefaultedNamedType('age', univ.Integer(34)) 1206 ) 1207 ) 1208 1209 def testRepr(self): 1210 assert 'name' in repr(self.s1.clone().setComponents('a', 'b')) 1211 1212 def testTag(self): 1213 assert self.s1.tagSet == tag.TagSet( 1214 (), 1215 tag.Tag(tag.tagClassUniversal, tag.tagFormatConstructed, 0x10) 1216 ), 'wrong tagSet' 1217 1218 def testById(self): 1219 self.s1.setComponentByName('name', univ.OctetString('abc')) 1220 assert self.s1.getComponentByName('name') == str2octs('abc'), 'set by name fails' 1221 1222 def testByKey(self): 1223 self.s1['name'] = 'abc' 1224 assert self.s1['name'] == str2octs('abc'), 'set by key fails' 1225 1226 def testContains(self): 1227 assert 'name' in self.s1 1228 assert '<missing>' not in self.s1 1229 1230 def testGetNearPosition(self): 1231 assert self.s1.componentType.getTagMapNearPosition(1).presentTypes == { 1232 univ.OctetString.tagSet: univ.OctetString(''), 1233 univ.Integer.tagSet: univ.Integer(34) 1234 } 1235 assert self.s1.componentType.getPositionNearType( 1236 univ.OctetString.tagSet, 1 1237 ) == 1 1238 1239 def testSetDefaultComponents(self): 1240 self.s1.clear() 1241 self.s1.setComponentByPosition(0, univ.OctetString('Ping')) 1242 self.s1.setComponentByPosition(1, univ.OctetString('Pong')) 1243 assert self.s1.getComponentByPosition(2) == 34 1244 1245 def testClone(self): 1246 self.s1.setComponentByPosition(0, univ.OctetString('abc')) 1247 self.s1.setComponentByPosition(1, univ.OctetString('def')) 1248 self.s1.setComponentByPosition(2, univ.Integer(123)) 1249 s = self.s1.clone() 1250 assert s.getComponentByPosition(0) != self.s1.getComponentByPosition(0) 1251 assert s.getComponentByPosition(1) != self.s1.getComponentByPosition(1) 1252 assert s.getComponentByPosition(2) != self.s1.getComponentByPosition(2) 1253 s = self.s1.clone(cloneValueFlag=1) 1254 assert s.getComponentByPosition(0) == self.s1.getComponentByPosition(0) 1255 assert s.getComponentByPosition(1) == self.s1.getComponentByPosition(1) 1256 assert s.getComponentByPosition(2) == self.s1.getComponentByPosition(2) 1257 1258 def testComponentTagsMatching(self): 1259 s = self.s1.clone() 1260 s.strictConstraints = True # This requires types equality 1261 o = univ.OctetString('abc').subtype(explicitTag=tag.Tag(tag.tagClassPrivate, tag.tagFormatSimple, 12)) 1262 try: 1263 s.setComponentByName('name', o) 1264 except PyAsn1Error: 1265 pass 1266 else: 1267 assert 0, 'inner supertype tag allowed' 1268 1269 def testComponentConstraintsMatching(self): 1270 s = self.s1.clone() 1271 o = univ.OctetString().subtype( 1272 subtypeSpec=constraint.ConstraintsUnion(constraint.SingleValueConstraint(str2octs('cba')))) 1273 s.strictConstraints = True # This requires types equality 1274 try: 1275 s.setComponentByName('name', o.clone('cba')) 1276 except PyAsn1Error: 1277 pass 1278 else: 1279 assert 0, 'inner supertype constraint allowed' 1280 s.strictConstraints = False # This requires subtype relationships 1281 try: 1282 s.setComponentByName('name', o.clone('cba')) 1283 except PyAsn1Error: 1284 assert 0, 'inner supertype constraint disallowed' 1285 else: 1286 pass 1287 1288 def testSetComponents(self): 1289 assert self.s1.clone().setComponents(name='a', nick='b', age=1) == \ 1290 self.s1.setComponentByPosition(0, 'a').setComponentByPosition(1, 'b').setComponentByPosition(2, 1) 1291 1292 def testSetToDefault(self): 1293 s = self.s1.clone() 1294 s.setComponentByPosition(0, univ.noValue) 1295 s[2] = univ.noValue 1296 assert s[0] == univ.OctetString('') 1297 assert s[2] == univ.Integer(34) 1298 1299 def testGetItem(self): 1300 s = self.s1.clone() 1301 s['name'] = 'xxx' 1302 assert s['name'] 1303 assert s[0] 1304 1305 try: 1306 s['xxx'] 1307 1308 except KeyError: 1309 pass 1310 1311 else: 1312 assert False, 'KeyError not raised' 1313 1314 try: 1315 s[100] 1316 1317 except IndexError: 1318 pass 1319 1320 else: 1321 assert False, 'IndexError not raised' 1322 1323 def testSetItem(self): 1324 s = self.s1.clone() 1325 s['name'] = 'xxx' 1326 1327 try: 1328 1329 s['xxx'] = 'xxx' 1330 1331 except KeyError: 1332 pass 1333 1334 else: 1335 assert False, 'KeyError not raised' 1336 1337 try: 1338 1339 s[100] = 'xxx' 1340 1341 except IndexError: 1342 pass 1343 1344 else: 1345 assert False, 'IndexError not raised' 1346 1347 def testIter(self): 1348 assert list(self.s1) == ['name', 'nick', 'age'] 1349 1350 def testKeys(self): 1351 self.s1.setComponentByPosition(0, univ.OctetString('abc')) 1352 self.s1.setComponentByPosition(1, univ.OctetString('def')) 1353 self.s1.setComponentByPosition(2, univ.Integer(123)) 1354 assert list(self.s1.keys()) == ['name', 'nick', 'age'] 1355 1356 def testValues(self): 1357 self.s1.setComponentByPosition(0, univ.OctetString('abc')) 1358 self.s1.setComponentByPosition(1, univ.OctetString('def')) 1359 self.s1.setComponentByPosition(2, univ.Integer(123)) 1360 assert list(self.s1.values()) == [str2octs('abc'), str2octs('def'), 123] 1361 1362 def testItems(self): 1363 self.s1.setComponentByPosition(0, univ.OctetString('abc')) 1364 self.s1.setComponentByPosition(1, univ.OctetString('def')) 1365 self.s1.setComponentByPosition(2, univ.Integer(123)) 1366 assert list(self.s1.items()) == [(x[0], str2octs(x[1])) for x in [('name', 'abc'), ('nick', 'def')]] + [('age', 123)] 1367 1368 def testUpdate(self): 1369 self.s1.clear() 1370 assert list(self.s1.values()) == [str2octs(''), str2octs(''), 34] 1371 self.s1.update(**{'name': 'abc', 'nick': 'def', 'age': 123}) 1372 assert list(self.s1.items()) == [(x[0], str2octs(x[1])) for x in [('name', 'abc'), ('nick', 'def')]] + [('age', 123)] 1373 self.s1.update(('name', 'ABC')) 1374 assert list(self.s1.items()) == [(x[0], str2octs(x[1])) for x in [('name', 'ABC'), ('nick', 'def')]] + [('age', 123)] 1375 self.s1.update(name='CBA') 1376 assert list(self.s1.items()) == [(x[0], str2octs(x[1])) for x in [('name', 'CBA'), ('nick', 'def')]] + [('age', 123)] 1377 1378 def testStaticDef(self): 1379 1380 class Sequence(univ.Sequence): 1381 componentType = namedtype.NamedTypes( 1382 namedtype.NamedType('name', univ.OctetString('')), 1383 namedtype.OptionalNamedType('nick', univ.OctetString('')), 1384 namedtype.DefaultedNamedType('age', univ.Integer(34)) 1385 ) 1386 1387 s = Sequence() 1388 s['name'] = 'abc' 1389 assert s['name'] == str2octs('abc') 1390 1391 def testGetComponentWithDefault(self): 1392 1393 class Sequence(univ.Sequence): 1394 componentType = namedtype.NamedTypes( 1395 namedtype.NamedType('name', univ.OctetString('')), 1396 namedtype.OptionalNamedType('nick', univ.OctetString()), 1397 ) 1398 1399 s = Sequence() 1400 1401 assert s[0] == str2octs('') 1402 assert s.getComponentByPosition(1, default=None, instantiate=False) is None 1403 assert s.getComponentByName('nick', default=None) is None 1404 s[1] = 'test' 1405 assert s.getComponentByPosition(1, default=None) is not None 1406 assert s.getComponentByPosition(1, default=None) == str2octs('test') 1407 s.clear() 1408 assert s.getComponentByPosition(1, default=None) is None 1409 1410 def testGetComponentNoInstantiation(self): 1411 1412 class Sequence(univ.Sequence): 1413 componentType = namedtype.NamedTypes( 1414 namedtype.NamedType('name', univ.OctetString('')), 1415 namedtype.OptionalNamedType('nick', univ.OctetString()), 1416 ) 1417 1418 s = Sequence() 1419 assert s[0] == str2octs('') 1420 assert s.getComponentByPosition(1, instantiate=False) is univ.noValue 1421 assert s.getComponentByName('nick', instantiate=False) is univ.noValue 1422 s[1] = 'test' 1423 assert s.getComponentByPosition(1, instantiate=False) is not univ.noValue 1424 assert s.getComponentByPosition(1, instantiate=False) == str2octs('test') 1425 s.clear() 1426 assert s.getComponentByPosition(1, instantiate=False) is univ.noValue 1427 1428 1429 class SequenceWithoutSchema(BaseTestCase): 1430 1431 def testGetItem(self): 1432 s = univ.Sequence() 1433 s.setComponentByPosition(0, univ.OctetString('abc')) 1434 s[0] = 'abc' 1435 assert s['field-0'] 1436 assert s[0] 1437 1438 try: 1439 s['field-1'] 1440 1441 except KeyError: 1442 pass 1443 1444 else: 1445 assert False, 'KeyError not raised' 1446 1447 def testSetItem(self): 1448 s = univ.Sequence() 1449 s.setComponentByPosition(0, univ.OctetString('abc')) 1450 s['field-0'] = 'xxx' 1451 1452 try: 1453 1454 s['field-1'] = 'xxx' 1455 1456 except KeyError: 1457 pass 1458 1459 else: 1460 assert False, 'KeyError not raised' 1461 1462 def testIter(self): 1463 s = univ.Sequence() 1464 s.setComponentByPosition(0, univ.OctetString('abc')) 1465 s.setComponentByPosition(1, univ.Integer(123)) 1466 assert list(s) == ['field-0', 'field-1'] 1467 1468 def testKeys(self): 1469 s = univ.Sequence() 1470 s.setComponentByPosition(0, univ.OctetString('abc')) 1471 s.setComponentByPosition(1, univ.Integer(123)) 1472 assert list(s.keys()) == ['field-0', 'field-1'] 1473 1474 def testValues(self): 1475 s = univ.Sequence() 1476 s.setComponentByPosition(0, univ.OctetString('abc')) 1477 s.setComponentByPosition(1, univ.Integer(123)) 1478 assert list(s.values()) == [str2octs('abc'), 123] 1479 1480 def testItems(self): 1481 s = univ.Sequence() 1482 s.setComponentByPosition(0, univ.OctetString('abc')) 1483 s.setComponentByPosition(1, univ.Integer(123)) 1484 assert list(s.items()) == [('field-0', str2octs('abc')), ('field-1', 123)] 1485 1486 def testUpdate(self): 1487 s = univ.Sequence() 1488 assert not s 1489 s.setComponentByPosition(0, univ.OctetString('abc')) 1490 s.setComponentByPosition(1, univ.Integer(123)) 1491 assert s 1492 assert list(s.keys()) == ['field-0', 'field-1'] 1493 assert list(s.values()) == [str2octs('abc'), 123] 1494 assert list(s.items()) == [('field-0', str2octs('abc')), ('field-1', 123)] 1495 s['field-0'] = univ.OctetString('def') 1496 assert list(s.values()) == [str2octs('def'), 123] 1497 s['field-1'] = univ.OctetString('ghi') 1498 assert list(s.values()) == [str2octs('def'), str2octs('ghi')] 1499 try: 1500 s['field-2'] = univ.OctetString('xxx') 1501 except KeyError: 1502 pass 1503 else: 1504 assert False, 'unknown field at schema-less object tolerated' 1505 assert 'field-0' in s 1506 s.clear() 1507 assert 'field-0' not in s 1508 1509 1510 class SequencePicklingTestCase(unittest.TestCase): 1511 1512 def testSchemaPickling(self): 1513 old_asn1 = univ.Sequence( 1514 componentType=namedtype.NamedTypes( 1515 namedtype.NamedType('name', univ.OctetString()) 1516 ) 1517 ) 1518 serialised = pickle.dumps(old_asn1) 1519 assert serialised 1520 new_asn1 = pickle.loads(serialised) 1521 assert type(new_asn1) == univ.Sequence 1522 assert old_asn1.isSameTypeWith(new_asn1) 1523 1524 def testValuePickling(self): 1525 old_asn1 = univ.Sequence( 1526 componentType=namedtype.NamedTypes( 1527 namedtype.NamedType('name', univ.OctetString()) 1528 ) 1529 ) 1530 old_asn1['name'] = 'test' 1531 serialised = pickle.dumps(old_asn1) 1532 assert serialised 1533 new_asn1 = pickle.loads(serialised) 1534 assert new_asn1 1535 assert new_asn1['name'] == str2octs('test') 1536 1537 1538 class SetOf(BaseTestCase): 1539 def setUp(self): 1540 BaseTestCase.setUp(self) 1541 self.s1 = univ.SetOf(componentType=univ.OctetString('')) 1542 1543 def testTag(self): 1544 assert self.s1.tagSet == tag.TagSet( 1545 (), 1546 tag.Tag(tag.tagClassUniversal, tag.tagFormatConstructed, 0x11) 1547 ), 'wrong tagSet' 1548 1549 def testSeq(self): 1550 self.s1.setComponentByPosition(0, univ.OctetString('abc')) 1551 assert self.s1[0] == str2octs('abc'), 'set by idx fails' 1552 self.s1.setComponentByPosition(0, self.s1[0].clone('cba')) 1553 assert self.s1[0] == str2octs('cba'), 'set by idx fails' 1554 1555 def testStaticDef(self): 1556 1557 class SetOf(univ.SequenceOf): 1558 componentType = univ.OctetString('') 1559 1560 s = SetOf() 1561 s[0] = 'abc' 1562 assert len(s) == 1 1563 assert s == [str2octs('abc')] 1564 1565 1566 1567 class SetOfPicklingTestCase(unittest.TestCase): 1568 1569 def testSchemaPickling(self): 1570 old_asn1 = univ.SetOf(componentType=univ.OctetString()) 1571 serialised = pickle.dumps(old_asn1) 1572 assert serialised 1573 new_asn1 = pickle.loads(serialised) 1574 assert type(new_asn1) == univ.SetOf 1575 assert old_asn1.isSameTypeWith(new_asn1) 1576 1577 def testValuePickling(self): 1578 old_asn1 = univ.SetOf(componentType=univ.OctetString()) 1579 old_asn1[0] = 'test' 1580 serialised = pickle.dumps(old_asn1) 1581 assert serialised 1582 new_asn1 = pickle.loads(serialised) 1583 assert new_asn1 1584 assert new_asn1 == [str2octs('test')] 1585 1586 1587 class Set(BaseTestCase): 1588 def setUp(self): 1589 BaseTestCase.setUp(self) 1590 1591 self.s1 = univ.Set( 1592 componentType=namedtype.NamedTypes( 1593 namedtype.NamedType('name', univ.OctetString('')), 1594 namedtype.OptionalNamedType('null', univ.Null('')), 1595 namedtype.DefaultedNamedType('age', univ.Integer(34)) 1596 ) 1597 ) 1598 self.s2 = self.s1.clone() 1599 1600 def testTag(self): 1601 assert self.s1.tagSet == tag.TagSet( 1602 (), 1603 tag.Tag(tag.tagClassUniversal, tag.tagFormatConstructed, 0x11) 1604 ), 'wrong tagSet' 1605 1606 def testByTypeWithPythonValue(self): 1607 self.s1.setComponentByType(univ.OctetString.tagSet, 'abc') 1608 assert self.s1.getComponentByType( 1609 univ.OctetString.tagSet 1610 ) == str2octs('abc'), 'set by name fails' 1611 1612 def testByTypeWithInstance(self): 1613 self.s1.setComponentByType(univ.OctetString.tagSet, univ.OctetString('abc')) 1614 assert self.s1.getComponentByType( 1615 univ.OctetString.tagSet 1616 ) == str2octs('abc'), 'set by name fails' 1617 1618 def testGetTagMap(self): 1619 assert self.s1.tagMap.presentTypes == { 1620 univ.Set.tagSet: univ.Set() 1621 } 1622 1623 def testGetComponentTagMap(self): 1624 assert self.s1.componentType.tagMapUnique.presentTypes == { 1625 univ.OctetString.tagSet: univ.OctetString(''), 1626 univ.Null.tagSet: univ.Null(''), 1627 univ.Integer.tagSet: univ.Integer(34) 1628 } 1629 1630 def testGetPositionByType(self): 1631 assert self.s1.componentType.getPositionByType(univ.Null().tagSet) == 1 1632 1633 def testSetToDefault(self): 1634 self.s1.setComponentByName('name', univ.noValue) 1635 assert self.s1['name'] == univ.OctetString('') 1636 1637 def testIter(self): 1638 assert list(self.s1) == ['name', 'null', 'age'] 1639 1640 def testStaticDef(self): 1641 1642 class Set(univ.Set): 1643 componentType = namedtype.NamedTypes( 1644 namedtype.NamedType('name', univ.OctetString('')), 1645 namedtype.OptionalNamedType('nick', univ.OctetString('')), 1646 namedtype.DefaultedNamedType('age', univ.Integer(34)) 1647 ) 1648 1649 s = Set() 1650 s['name'] = 'abc' 1651 assert s['name'] == str2octs('abc') 1652 1653 def testGetComponentWithDefault(self): 1654 1655 class Set(univ.Set): 1656 componentType = namedtype.NamedTypes( 1657 namedtype.NamedType('id', univ.Integer(123)), 1658 namedtype.OptionalNamedType('nick', univ.OctetString()), 1659 ) 1660 1661 s = Set() 1662 assert s[0] == 123 1663 assert s.getComponentByPosition(1, default=None, instantiate=False) is None 1664 assert s.getComponentByName('nick', default=None) is None 1665 s[1] = 'test' 1666 assert s.getComponentByPosition(1, default=None) is not None 1667 assert s.getComponentByPosition(1, default=None) == str2octs('test') 1668 s.clear() 1669 assert s.getComponentByPosition(1, default=None) is None 1670 1671 def testGetComponentNoInstantiation(self): 1672 1673 class Set(univ.Set): 1674 componentType = namedtype.NamedTypes( 1675 namedtype.NamedType('id', univ.Integer(123)), 1676 namedtype.OptionalNamedType('nick', univ.OctetString()), 1677 ) 1678 1679 s = Set() 1680 assert s[0] == 123 1681 assert s.getComponentByPosition(1, instantiate=False) is univ.noValue 1682 assert s.getComponentByName('nick', instantiate=False) is univ.noValue 1683 assert s.getComponentByType(univ.OctetString.tagSet, instantiate=False) is univ.noValue 1684 s[1] = 'test' 1685 assert s.getComponentByPosition(1, instantiate=False) is not univ.noValue 1686 assert s.getComponentByPosition(1, instantiate=False) == str2octs('test') 1687 s.clear() 1688 assert s.getComponentByPosition(1, instantiate=False) is univ.noValue 1689 1690 1691 class SetPicklingTestCase(unittest.TestCase): 1692 1693 def testSchemaPickling(self): 1694 old_asn1 = univ.Set( 1695 componentType=namedtype.NamedTypes( 1696 namedtype.NamedType('name', univ.OctetString()) 1697 ) 1698 ) 1699 serialised = pickle.dumps(old_asn1) 1700 assert serialised 1701 new_asn1 = pickle.loads(serialised) 1702 assert type(new_asn1) == univ.Set 1703 assert old_asn1.isSameTypeWith(new_asn1) 1704 1705 def testValuePickling(self): 1706 old_asn1 = univ.Set( 1707 componentType=namedtype.NamedTypes( 1708 namedtype.NamedType('name', univ.OctetString()) 1709 ) 1710 ) 1711 old_asn1['name'] = 'test' 1712 serialised = pickle.dumps(old_asn1) 1713 assert serialised 1714 new_asn1 = pickle.loads(serialised) 1715 assert new_asn1 1716 assert new_asn1['name'] == str2octs('test') 1717 1718 1719 class Choice(BaseTestCase): 1720 def setUp(self): 1721 BaseTestCase.setUp(self) 1722 1723 innerComp = univ.Choice( 1724 componentType=namedtype.NamedTypes( 1725 namedtype.NamedType('count', univ.Integer()), 1726 namedtype.NamedType('flag', univ.Boolean()) 1727 ) 1728 ) 1729 self.s1 = univ.Choice( 1730 componentType=namedtype.NamedTypes( 1731 namedtype.NamedType('name', univ.OctetString()), 1732 namedtype.NamedType('sex', innerComp) 1733 ) 1734 ) 1735 1736 def testTag(self): 1737 assert self.s1.tagSet == tag.TagSet(), 'wrong tagSet' 1738 1739 def testRepr(self): 1740 assert 'Choice' in repr(self.s1.clone().setComponents('a')) 1741 s = self.s1.clone().setComponents( 1742 sex=self.s1.setComponentByPosition(1).getComponentByPosition(1).clone().setComponents(count=univ.Integer(123)) 1743 ) 1744 assert 'Choice' in repr(s) 1745 1746 def testContains(self): 1747 self.s1.setComponentByType(univ.OctetString.tagSet, 'abc') 1748 assert 'name' in self.s1 1749 assert 'sex' not in self.s1 1750 1751 self.s1.setComponentByType(univ.Integer.tagSet, 123, innerFlag=True) 1752 assert 'name' not in self.s1 1753 assert 'sex' in self.s1 1754 1755 def testIter(self): 1756 self.s1.setComponentByType(univ.OctetString.tagSet, 'abc') 1757 assert list(self.s1) == ['name'] 1758 self.s1.setComponentByType(univ.Integer.tagSet, 123, innerFlag=True) 1759 assert list(self.s1) == ['sex'] 1760 1761 def testOuterByTypeWithPythonValue(self): 1762 self.s1.setComponentByType(univ.OctetString.tagSet, 'abc') 1763 assert self.s1.getComponentByType( 1764 univ.OctetString.tagSet 1765 ) == str2octs('abc') 1766 1767 def testOuterByTypeWithInstanceValue(self): 1768 self.s1.setComponentByType( 1769 univ.OctetString.tagSet, univ.OctetString('abc') 1770 ) 1771 assert self.s1.getComponentByType( 1772 univ.OctetString.tagSet 1773 ) == str2octs('abc') 1774 1775 def testInnerByTypeWithPythonValue(self): 1776 self.s1.setComponentByType(univ.Integer.tagSet, 123, innerFlag=True) 1777 assert self.s1.getComponentByType( 1778 univ.Integer.tagSet, 1 1779 ) == 123 1780 1781 def testInnerByTypeWithInstanceValue(self): 1782 self.s1.setComponentByType( 1783 univ.Integer.tagSet, univ.Integer(123), innerFlag=True 1784 ) 1785 assert self.s1.getComponentByType( 1786 univ.Integer.tagSet, 1 1787 ) == 123 1788 1789 def testCmp(self): 1790 self.s1.setComponentByName('name', univ.OctetString('abc')) 1791 assert self.s1 == str2octs('abc'), '__cmp__() fails' 1792 1793 def testGetComponent(self): 1794 self.s1.setComponentByType(univ.OctetString.tagSet, 'abc') 1795 assert self.s1.getComponent() == str2octs('abc'), 'getComponent() fails' 1796 1797 def testGetName(self): 1798 self.s1.setComponentByType(univ.OctetString.tagSet, 'abc') 1799 assert self.s1.getName() == 'name', 'getName() fails' 1800 1801 def testSetComponentByPosition(self): 1802 self.s1.setComponentByPosition(0, univ.OctetString('Jim')) 1803 assert self.s1 == str2octs('Jim') 1804 1805 def testClone(self): 1806 self.s1.setComponentByPosition(0, univ.OctetString('abc')) 1807 s = self.s1.clone() 1808 assert len(s) == 0 1809 s = self.s1.clone(cloneValueFlag=1) 1810 assert len(s) == 1 1811 assert s.getComponentByPosition(0) == self.s1.getComponentByPosition(0) 1812 1813 def testSetToDefault(self): 1814 s = self.s1.clone() 1815 s.setComponentByName('sex', univ.noValue) 1816 assert s['sex'] is not univ.noValue 1817 1818 def testStaticDef(self): 1819 1820 class InnerChoice(univ.Choice): 1821 componentType = namedtype.NamedTypes( 1822 namedtype.NamedType('count', univ.Integer()), 1823 namedtype.NamedType('flag', univ.Boolean()) 1824 ) 1825 1826 class OuterChoice(univ.Choice): 1827 componentType = namedtype.NamedTypes( 1828 namedtype.NamedType('name', univ.OctetString()), 1829 namedtype.NamedType('sex', InnerChoice()) 1830 ) 1831 1832 c = OuterChoice() 1833 1834 c.setComponentByType(univ.OctetString.tagSet, 'abc') 1835 assert c.getName() == 'name' 1836 1837 def testGetComponentWithDefault(self): 1838 1839 s = univ.Choice( 1840 componentType=namedtype.NamedTypes( 1841 namedtype.NamedType('name', univ.OctetString()), 1842 namedtype.NamedType('id', univ.Integer()) 1843 ) 1844 ) 1845 1846 assert s.getComponentByPosition(0, default=None, instantiate=False) is None 1847 assert s.getComponentByPosition(1, default=None, instantiate=False) is None 1848 assert s.getComponentByName('name', default=None, instantiate=False) is None 1849 assert s.getComponentByName('id', default=None, instantiate=False) is None 1850 assert s.getComponentByType(univ.OctetString.tagSet, default=None) is None 1851 assert s.getComponentByType(univ.Integer.tagSet, default=None) is None 1852 s[1] = 123 1853 assert s.getComponentByPosition(1, default=None) is not None 1854 assert s.getComponentByPosition(1, univ.noValue) == 123 1855 s.clear() 1856 assert s.getComponentByPosition(1, default=None, instantiate=False) is None 1857 1858 def testGetComponentNoInstantiation(self): 1859 1860 s = univ.Choice( 1861 componentType=namedtype.NamedTypes( 1862 namedtype.NamedType('name', univ.OctetString()), 1863 namedtype.NamedType('id', univ.Integer()) 1864 ) 1865 ) 1866 1867 assert s.getComponentByPosition(0, instantiate=False) is univ.noValue 1868 assert s.getComponentByPosition(1, instantiate=False) is univ.noValue 1869 assert s.getComponentByName('name', instantiate=False) is univ.noValue 1870 assert s.getComponentByName('id', instantiate=False) is univ.noValue 1871 assert s.getComponentByType(univ.OctetString.tagSet, instantiate=False) is univ.noValue 1872 assert s.getComponentByType(univ.Integer.tagSet, instantiate=False) is univ.noValue 1873 s[1] = 123 1874 assert s.getComponentByPosition(1, instantiate=False) is not univ.noValue 1875 assert s.getComponentByPosition(1, instantiate=False) == 123 1876 s.clear() 1877 assert s.getComponentByPosition(1, instantiate=False) is univ.noValue 1878 1879 1880 class ChoicePicklingTestCase(unittest.TestCase): 1881 1882 def testSchemaPickling(self): 1883 old_asn1 = univ.Choice( 1884 componentType=namedtype.NamedTypes( 1885 namedtype.NamedType('name', univ.OctetString()), 1886 namedtype.NamedType('id', univ.Integer()) 1887 ) 1888 ) 1889 serialised = pickle.dumps(old_asn1) 1890 assert serialised 1891 new_asn1 = pickle.loads(serialised) 1892 assert type(new_asn1) == univ.Choice 1893 assert old_asn1.isSameTypeWith(new_asn1) 1894 1895 def testValuePickling(self): 1896 old_asn1 = univ.Choice( 1897 componentType=namedtype.NamedTypes( 1898 namedtype.NamedType('name', univ.OctetString()), 1899 namedtype.NamedType('id', univ.Integer()) 1900 ) 1901 ) 1902 old_asn1['name'] = 'test' 1903 serialised = pickle.dumps(old_asn1) 1904 assert serialised 1905 new_asn1 = pickle.loads(serialised) 1906 assert new_asn1 1907 assert new_asn1['name'] == str2octs('test') 1908 1909 1910 suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 1911 1912 if __name__ == '__main__': 1913 unittest.TextTestRunner(verbosity=2).run(suite) 1914