1 from pyasn1.type import univ, tag, constraint, namedtype, namedval, error 2 from pyasn1.compat.octets import str2octs, ints2octs 3 from pyasn1.error import PyAsn1Error 4 from sys import version_info 5 if version_info[0:2] < (2, 7) or \ 6 version_info[0:2] in ( (3, 0), (3, 1) ): 7 try: 8 import unittest2 as unittest 9 except ImportError: 10 import unittest 11 else: 12 import unittest 13 14 class IntegerTestCase(unittest.TestCase): 15 def testStr(self): assert str(univ.Integer(1)) in ('1','1L'),'str() fails' 16 def testAnd(self): assert univ.Integer(1) & 0 == 0, '__and__() fails' 17 def testOr(self): assert univ.Integer(1) | 0 == 1, '__or__() fails' 18 def testXor(self): assert univ.Integer(1) ^ 0 == 1, '__xor__() fails' 19 def testRand(self): assert 0 & univ.Integer(1) == 0, '__rand__() fails' 20 def testRor(self): assert 0 | univ.Integer(1) == 1, '__ror__() fails' 21 def testRxor(self): assert 0 ^ univ.Integer(1) == 1, '__rxor__() fails' 22 def testAdd(self): assert univ.Integer(-4) + 6 == 2, '__add__() fails' 23 def testRadd(self): assert 4 + univ.Integer(5) == 9, '__radd__() fails' 24 def testSub(self): assert univ.Integer(3) - 6 == -3, '__sub__() fails' 25 def testRsub(self): assert 6 - univ.Integer(3) == 3, '__rsub__() fails' 26 def testMul(self): assert univ.Integer(3) * -3 == -9, '__mul__() fails' 27 def testRmul(self): assert 2 * univ.Integer(3) == 6, '__rmul__() fails' 28 def testDiv(self): assert univ.Integer(3) / 2 == 1, '__div__() fails' 29 def testRdiv(self): assert 6 / univ.Integer(3) == 2, '__rdiv__() fails' 30 def testMod(self): assert univ.Integer(3) % 2 == 1, '__mod__() fails' 31 def testRmod(self): assert 4 % univ.Integer(3) == 1, '__rmod__() fails' 32 def testPow(self): assert univ.Integer(3) ** 2 == 9, '__pow__() fails' 33 def testRpow(self): assert 2 ** univ.Integer(2) == 4, '__rpow__() fails' 34 def testLshift(self): assert univ.Integer(1) << 1 == 2, '<< fails' 35 def testRshift(self): assert univ.Integer(2) >> 1 == 1, '>> fails' 36 def testInt(self): assert int(univ.Integer(3)) == 3, '__int__() fails' 37 def testLong(self): assert int(univ.Integer(8)) == 8, '__long__() fails' 38 def testFloat(self): assert float(univ.Integer(4))==4.0,'__float__() fails' 39 def testPrettyIn(self): assert univ.Integer('3') == 3, 'prettyIn() fails' 40 def testTag(self): 41 assert univ.Integer().getTagSet() == tag.TagSet( 42 (), 43 tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x02) 44 ) 45 def testNamedVals(self): 46 i = univ.Integer( 47 'asn1', namedValues=univ.Integer.namedValues.clone(('asn1', 1)) 48 ) 49 assert i == 1, 'named val fails' 50 assert str(i) != 'asn1', 'named val __str__() fails' 51 52 class BooleanTestCase(unittest.TestCase): 53 def testTruth(self): 54 assert univ.Boolean(True) and univ.Boolean(1), 'Truth initializer fails' 55 def testFalse(self): 56 assert not univ.Boolean(False) and not univ.Boolean(0), 'False initializer fails' 57 def testStr(self): 58 assert str(univ.Boolean(1)) in ('1', '1L'), 'str() fails' 59 def testTag(self): 60 assert univ.Boolean().getTagSet() == tag.TagSet( 61 (), 62 tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x01) 63 ) 64 def testConstraints(self): 65 try: 66 univ.Boolean(2) 67 except error.ValueConstraintError: 68 pass 69 else: 70 assert 0, 'constraint fail' 71 def testSubtype(self): 72 assert univ.Integer().subtype( 73 value=1, 74 implicitTag=tag.Tag(tag.tagClassPrivate,tag.tagFormatSimple,2), 75 subtypeSpec=constraint.SingleValueConstraint(1,3) 76 ) == univ.Integer( 77 value=1, 78 tagSet=tag.TagSet(tag.Tag(tag.tagClassPrivate, 79 tag.tagFormatSimple,2)), 80 subtypeSpec=constraint.ConstraintsIntersection(constraint.SingleValueConstraint(1,3)) 81 ) 82 83 class BitStringTestCase(unittest.TestCase): 84 def setUp(self): 85 self.b = univ.BitString( 86 namedValues=namedval.NamedValues(('Active', 0), ('Urgent', 1)) 87 ) 88 def testSet(self): 89 assert self.b.clone('Active') == (1,) 90 assert self.b.clone("'1010100110001010'B") == (1,0,1,0,1,0,0,1,1,0,0,0,1,0,1,0) 91 assert self.b.clone("'A98A'H") == (1,0,1,0,1,0,0,1,1,0,0,0,1,0,1,0) 92 assert self.b.clone((1,0,1)) == (1,0,1) 93 def testStr(self): 94 assert str(self.b.clone('Urgent,Active')) == '(1, 1)' 95 def testRepr(self): 96 assert repr(self.b.clone('Urgent,Active')) == 'BitString("\'11\'B")' 97 def testTag(self): 98 assert univ.BitString().getTagSet() == tag.TagSet( 99 (), 100 tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x03) 101 ) 102 def testLen(self): assert len(self.b.clone("'A98A'H")) == 16 103 def testIter(self): 104 assert self.b.clone("'A98A'H")[0] == 1 105 assert self.b.clone("'A98A'H")[1] == 0 106 assert self.b.clone("'A98A'H")[2] == 1 107 108 class OctetStringTestCase(unittest.TestCase): 109 def testInit(self): 110 assert univ.OctetString(str2octs('abcd')) == str2octs('abcd'), '__init__() fails' 111 def testBinStr(self): 112 assert univ.OctetString(binValue="1000010111101110101111000000111011") == ints2octs((133, 238, 188, 14, 192)), 'bin init fails' 113 def testHexStr(self): 114 assert univ.OctetString(hexValue="FA9823C43E43510DE3422") == ints2octs((250, 152, 35, 196, 62, 67, 81, 13, 227, 66, 32)), 'hex init fails' 115 def testTuple(self): 116 assert univ.OctetString((1,2,3,4,5)) == ints2octs((1,2,3,4,5)), 'tuple init failed' 117 def testStr(self): 118 assert str(univ.OctetString('q')) == 'q', '__str__() fails' 119 def testSeq(self): 120 assert univ.OctetString('q')[0] == str2octs('q')[0],'__getitem__() fails' 121 def testAsOctets(self): 122 assert univ.OctetString('abcd').asOctets() == str2octs('abcd'), 'testAsOctets() fails' 123 def testAsInts(self): 124 assert univ.OctetString('abcd').asNumbers() == (97, 98, 99, 100), 'testAsNumbers() fails' 125 126 def testEmpty(self): 127 try: 128 str(univ.OctetString()) 129 except PyAsn1Error: 130 pass 131 else: 132 assert 0, 'empty OctetString() not reported' 133 134 def testAdd(self): 135 assert univ.OctetString('') + 'q' == str2octs('q'), '__add__() fails' 136 def testRadd(self): 137 assert 'b' + univ.OctetString('q') == str2octs('bq'), '__radd__() fails' 138 def testMul(self): 139 assert univ.OctetString('a') * 2 == str2octs('aa'), '__mul__() fails' 140 def testRmul(self): 141 assert 2 * univ.OctetString('b') == str2octs('bb'), '__rmul__() fails' 142 def testTag(self): 143 assert univ.OctetString().getTagSet() == tag.TagSet( 144 (), 145 tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x04) 146 ) 147 148 class Null(unittest.TestCase): 149 def testStr(self): assert str(univ.Null('')) == '', 'str() fails' 150 def testTag(self): 151 assert univ.Null().getTagSet() == tag.TagSet( 152 (), 153 tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x05) 154 ) 155 def testConstraints(self): 156 try: 157 univ.Null(2) 158 except error.ValueConstraintError: 159 pass 160 else: 161 assert 0, 'constraint fail' 162 163 class RealTestCase(unittest.TestCase): 164 def testStr(self): assert str(univ.Real(1.0)) == '1.0','str() fails' 165 def testRepr(self): assert repr(univ.Real(-4.1)) == 'Real((-41, 10, -1))','repr() fails' 166 def testAdd(self): assert univ.Real(-4.1) + 1.4 == -2.7, '__add__() fails' 167 def testRadd(self): assert 4 + univ.Real(0.5) == 4.5, '__radd__() fails' 168 def testSub(self): assert univ.Real(3.9) - 1.7 == 2.2, '__sub__() fails' 169 def testRsub(self): assert 6.1 - univ.Real(0.1) == 6, '__rsub__() fails' 170 def testMul(self): assert univ.Real(3.0) * -3 == -9, '__mul__() fails' 171 def testRmul(self): assert 2 * univ.Real(3.0) == 6, '__rmul__() fails' 172 def testDiv(self): assert univ.Real(3.0) / 2 == 1.5, '__div__() fails' 173 def testRdiv(self): assert 6 / univ.Real(3.0) == 2, '__rdiv__() fails' 174 def testMod(self): assert univ.Real(3.0) % 2 == 1, '__mod__() fails' 175 def testRmod(self): assert 4 % univ.Real(3.0) == 1, '__rmod__() fails' 176 def testPow(self): assert univ.Real(3.0) ** 2 == 9, '__pow__() fails' 177 def testRpow(self): assert 2 ** univ.Real(2.0) == 4, '__rpow__() fails' 178 def testInt(self): assert int(univ.Real(3.0)) == 3, '__int__() fails' 179 def testLong(self): assert int(univ.Real(8.0)) == 8, '__long__() fails' 180 def testFloat(self): assert float(univ.Real(4.0))==4.0,'__float__() fails' 181 def testPrettyIn(self): assert univ.Real((3,10,0)) == 3, 'prettyIn() fails' 182 # infinite float values 183 def testStrInf(self): 184 assert str(univ.Real('inf')) == 'inf','str() fails' 185 def testReprInf(self): 186 assert repr(univ.Real('inf')) == 'Real(\'inf\')','repr() fails' 187 def testAddInf(self): 188 assert univ.Real('inf') + 1 == float('inf'), '__add__() fails' 189 def testRaddInf(self): 190 assert 1 + univ.Real('inf') == float('inf'), '__radd__() fails' 191 def testIntInf(self): 192 try: 193 assert int(univ.Real('inf')) 194 except OverflowError: 195 pass 196 else: 197 assert 0, '__int__() fails' 198 def testLongInf(self): 199 try: 200 assert int(univ.Real('inf')) 201 except OverflowError: 202 pass 203 else: 204 assert 0, '__long__() fails' 205 assert int(univ.Real(8.0)) == 8, '__long__() fails' 206 def testFloatInf(self): 207 assert float(univ.Real('-inf')) == float('-inf'),'__float__() fails' 208 def testPrettyInInf(self): 209 assert univ.Real(float('inf')) == float('inf'), 'prettyIn() fails' 210 def testPlusInf(self): 211 assert univ.Real('inf').isPlusInfinity(), 'isPlusInfinity failed' 212 def testMinusInf(self): 213 assert univ.Real('-inf').isMinusInfinity(), 'isMinusInfinity failed' 214 215 def testTag(self): 216 assert univ.Real().getTagSet() == tag.TagSet( 217 (), 218 tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x09) 219 ) 220 221 class ObjectIdentifier(unittest.TestCase): 222 def testStr(self): 223 assert str(univ.ObjectIdentifier((1,3,6))) == '1.3.6' 224 def testEq(self): 225 assert univ.ObjectIdentifier((1,3,6)) == (1,3,6), '__cmp__() fails' 226 def testAdd(self): 227 assert univ.ObjectIdentifier((1,3)) + (6,)==(1,3,6),'__add__() fails' 228 def testRadd(self): 229 assert (1,) + univ.ObjectIdentifier((3,6))==(1,3,6),'__radd__() fails' 230 def testLen(self): 231 assert len(univ.ObjectIdentifier((1,3))) == 2,'__len__() fails' 232 def testPrefix(self): 233 o = univ.ObjectIdentifier('1.3.6') 234 assert o.isPrefixOf((1,3,6)), 'isPrefixOf() fails' 235 assert o.isPrefixOf((1,3,6,1)), 'isPrefixOf() fails' 236 assert not o.isPrefixOf((1,3)), 'isPrefixOf() fails' 237 def testInput(self): 238 assert univ.ObjectIdentifier('1.3.6')==(1,3,6),'prettyIn() fails' 239 def testTag(self): 240 assert univ.ObjectIdentifier().getTagSet() == tag.TagSet( 241 (), 242 tag.Tag(tag.tagClassUniversal, tag.tagFormatSimple, 0x06) 243 ) 244 245 class SequenceOf(unittest.TestCase): 246 def setUp(self): 247 self.s1 = univ.SequenceOf( 248 componentType=univ.OctetString('') 249 ) 250 self.s2 = self.s1.clone() 251 def testTag(self): 252 assert self.s1.getTagSet() == tag.TagSet( 253 (), 254 tag.Tag(tag.tagClassUniversal, tag.tagFormatConstructed, 0x10) 255 ), 'wrong tagSet' 256 def testSeq(self): 257 self.s1.setComponentByPosition(0, univ.OctetString('abc')) 258 assert self.s1[0] == str2octs('abc'), 'set by idx fails' 259 self.s1[0] = 'cba' 260 assert self.s1[0] == str2octs('cba'), 'set by idx fails' 261 def testCmp(self): 262 self.s1.clear() 263 self.s1.setComponentByPosition(0, 'abc') 264 self.s2.clear() 265 self.s2.setComponentByPosition(0, univ.OctetString('abc')) 266 assert self.s1 == self.s2, '__cmp__() fails' 267 def testSubtypeSpec(self): 268 s = self.s1.clone(subtypeSpec=constraint.ConstraintsUnion( 269 constraint.SingleValueConstraint(str2octs('abc')) 270 )) 271 try: 272 s.setComponentByPosition(0, univ.OctetString('abc')) 273 except: 274 assert 0, 'constraint fails' 275 try: 276 s.setComponentByPosition(1, univ.OctetString('Abc')) 277 except: 278 pass 279 else: 280 assert 0, 'constraint fails' 281 def testSizeSpec(self): 282 s = self.s1.clone(sizeSpec=constraint.ConstraintsUnion( 283 constraint.ValueSizeConstraint(1,1) 284 )) 285 s.setComponentByPosition(0, univ.OctetString('abc')) 286 try: 287 s.verifySizeSpec() 288 except: 289 assert 0, 'size spec fails' 290 s.setComponentByPosition(1, univ.OctetString('abc')) 291 try: 292 s.verifySizeSpec() 293 except: 294 pass 295 else: 296 assert 0, 'size spec fails' 297 def testGetComponentTagMap(self): 298 assert self.s1.getComponentTagMap().getPosMap() == { 299 univ.OctetString.tagSet: univ.OctetString('') 300 } 301 def testSubtype(self): 302 self.s1.clear() 303 assert self.s1.subtype( 304 implicitTag=tag.Tag(tag.tagClassPrivate,tag.tagFormatSimple,2), 305 subtypeSpec=constraint.SingleValueConstraint(1,3), 306 sizeSpec=constraint.ValueSizeConstraint(0,1) 307 ) == self.s1.clone( 308 tagSet=tag.TagSet(tag.Tag(tag.tagClassPrivate, 309 tag.tagFormatSimple,2)), 310 subtypeSpec=constraint.ConstraintsIntersection(constraint.SingleValueConstraint(1,3)), 311 sizeSpec=constraint.ValueSizeConstraint(0,1) 312 ) 313 def testClone(self): 314 self.s1.setComponentByPosition(0, univ.OctetString('abc')) 315 s = self.s1.clone() 316 assert len(s) == 0 317 s = self.s1.clone(cloneValueFlag=1) 318 assert len(s) == 1 319 assert s.getComponentByPosition(0) == self.s1.getComponentByPosition(0) 320 321 class Sequence(unittest.TestCase): 322 def setUp(self): 323 self.s1 = univ.Sequence(componentType=namedtype.NamedTypes( 324 namedtype.NamedType('name', univ.OctetString('')), 325 namedtype.OptionalNamedType('nick', univ.OctetString('')), 326 namedtype.DefaultedNamedType('age', univ.Integer(34)) 327 )) 328 def testTag(self): 329 assert self.s1.getTagSet() == tag.TagSet( 330 (), 331 tag.Tag(tag.tagClassUniversal, tag.tagFormatConstructed, 0x10) 332 ), 'wrong tagSet' 333 def testById(self): 334 self.s1.setComponentByName('name', univ.OctetString('abc')) 335 assert self.s1.getComponentByName('name') == str2octs('abc'), 'set by name fails' 336 def testByKey(self): 337 self.s1['name'] = 'abc' 338 assert self.s1['name'] == str2octs('abc'), 'set by key fails' 339 def testGetNearPosition(self): 340 assert self.s1.getComponentTagMapNearPosition(1).getPosMap() == { 341 univ.OctetString.tagSet: univ.OctetString(''), 342 univ.Integer.tagSet: univ.Integer(34) 343 } 344 assert self.s1.getComponentPositionNearType( 345 univ.OctetString.tagSet, 1 346 ) == 1 347 def testGetDefaultComponentByPosition(self): 348 self.s1.clear() 349 assert self.s1.getDefaultComponentByPosition(0) == None 350 assert self.s1.getDefaultComponentByPosition(2) == univ.Integer(34) 351 def testSetDefaultComponents(self): 352 self.s1.clear() 353 assert self.s1.getComponentByPosition(2) == None 354 self.s1.setComponentByPosition(0, univ.OctetString('Ping')) 355 self.s1.setComponentByPosition(1, univ.OctetString('Pong')) 356 self.s1.setDefaultComponents() 357 assert self.s1.getComponentByPosition(2) == 34 358 def testClone(self): 359 self.s1.setComponentByPosition(0, univ.OctetString('abc')) 360 self.s1.setComponentByPosition(1, univ.OctetString('def')) 361 self.s1.setComponentByPosition(2, univ.Integer(123)) 362 s = self.s1.clone() 363 assert s.getComponentByPosition(0) != self.s1.getComponentByPosition(0) 364 assert s.getComponentByPosition(1) != self.s1.getComponentByPosition(1) 365 assert s.getComponentByPosition(2) != self.s1.getComponentByPosition(2) 366 s = self.s1.clone(cloneValueFlag=1) 367 assert s.getComponentByPosition(0) == self.s1.getComponentByPosition(0) 368 assert s.getComponentByPosition(1) == self.s1.getComponentByPosition(1) 369 assert s.getComponentByPosition(2) == self.s1.getComponentByPosition(2) 370 371 class SetOf(unittest.TestCase): 372 def setUp(self): 373 self.s1 = univ.SetOf(componentType=univ.OctetString('')) 374 def testTag(self): 375 assert self.s1.getTagSet() == tag.TagSet( 376 (), 377 tag.Tag(tag.tagClassUniversal, tag.tagFormatConstructed, 0x11) 378 ), 'wrong tagSet' 379 def testSeq(self): 380 self.s1.setComponentByPosition(0, univ.OctetString('abc')) 381 assert self.s1[0] == str2octs('abc'), 'set by idx fails' 382 self.s1.setComponentByPosition(0, self.s1[0].clone('cba')) 383 assert self.s1[0] == str2octs('cba'), 'set by idx fails' 384 385 class Set(unittest.TestCase): 386 def setUp(self): 387 self.s1 = univ.Set(componentType=namedtype.NamedTypes( 388 namedtype.NamedType('name', univ.OctetString('')), 389 namedtype.OptionalNamedType('null', univ.Null('')), 390 namedtype.DefaultedNamedType('age', univ.Integer(34)) 391 )) 392 self.s2 = self.s1.clone() 393 def testTag(self): 394 assert self.s1.getTagSet() == tag.TagSet( 395 (), 396 tag.Tag(tag.tagClassUniversal, tag.tagFormatConstructed, 0x11) 397 ), 'wrong tagSet' 398 def testByTypeWithPythonValue(self): 399 self.s1.setComponentByType(univ.OctetString.tagSet, 'abc') 400 assert self.s1.getComponentByType( 401 univ.OctetString.tagSet 402 ) == str2octs('abc'), 'set by name fails' 403 def testByTypeWithInstance(self): 404 self.s1.setComponentByType(univ.OctetString.tagSet, univ.OctetString('abc')) 405 assert self.s1.getComponentByType( 406 univ.OctetString.tagSet 407 ) == str2octs('abc'), 'set by name fails' 408 def testGetTagMap(self): 409 assert self.s1.getTagMap().getPosMap() == { 410 univ.Set.tagSet: univ.Set() 411 } 412 def testGetComponentTagMap(self): 413 assert self.s1.getComponentTagMap().getPosMap() == { 414 univ.OctetString.tagSet: univ.OctetString(''), 415 univ.Null.tagSet: univ.Null(''), 416 univ.Integer.tagSet: univ.Integer(34) 417 } 418 def testGetPositionByType(self): 419 assert self.s1.getComponentPositionByType( 420 univ.Null().getTagSet() 421 ) == 1 422 423 class Choice(unittest.TestCase): 424 def setUp(self): 425 innerComp = univ.Choice(componentType=namedtype.NamedTypes( 426 namedtype.NamedType('count', univ.Integer()), 427 namedtype.NamedType('flag', univ.Boolean()) 428 )) 429 self.s1 = univ.Choice(componentType=namedtype.NamedTypes( 430 namedtype.NamedType('name', univ.OctetString()), 431 namedtype.NamedType('sex', innerComp) 432 )) 433 def testTag(self): 434 assert self.s1.getTagSet() == tag.TagSet(), 'wrong tagSet' 435 def testOuterByTypeWithPythonValue(self): 436 self.s1.setComponentByType(univ.OctetString.tagSet, 'abc') 437 assert self.s1.getComponentByType( 438 univ.OctetString.tagSet 439 ) == str2octs('abc') 440 def testOuterByTypeWithInstanceValue(self): 441 self.s1.setComponentByType( 442 univ.OctetString.tagSet, univ.OctetString('abc') 443 ) 444 assert self.s1.getComponentByType( 445 univ.OctetString.tagSet 446 ) == str2octs('abc') 447 def testInnerByTypeWithPythonValue(self): 448 self.s1.setComponentByType(univ.Integer.tagSet, 123, 1) 449 assert self.s1.getComponentByType( 450 univ.Integer.tagSet, 1 451 ) == 123 452 def testInnerByTypeWithInstanceValue(self): 453 self.s1.setComponentByType( 454 univ.Integer.tagSet, univ.Integer(123), 1 455 ) 456 assert self.s1.getComponentByType( 457 univ.Integer.tagSet, 1 458 ) == 123 459 def testCmp(self): 460 self.s1.setComponentByName('name', univ.OctetString('abc')) 461 assert self.s1 == str2octs('abc'), '__cmp__() fails' 462 def testGetComponent(self): 463 self.s1.setComponentByType(univ.OctetString.tagSet, 'abc') 464 assert self.s1.getComponent() == str2octs('abc'), 'getComponent() fails' 465 def testGetName(self): 466 self.s1.setComponentByType(univ.OctetString.tagSet, 'abc') 467 assert self.s1.getName() == 'name', 'getName() fails' 468 def testSetComponentByPosition(self): 469 self.s1.setComponentByPosition(0, univ.OctetString('Jim')) 470 assert self.s1 == str2octs('Jim') 471 def testClone(self): 472 self.s1.setComponentByPosition(0, univ.OctetString('abc')) 473 s = self.s1.clone() 474 assert len(s) == 0 475 s = self.s1.clone(cloneValueFlag=1) 476 assert len(s) == 1 477 assert s.getComponentByPosition(0) == self.s1.getComponentByPosition(0) 478 479 if __name__ == '__main__': unittest.main() 480