~ubuntu-branches/debian/sid/pyasn1/sid

« back to all changes in this revision

Viewing changes to test/type/test_univ.py

  • Committer: Package Import Robot
  • Author(s): Vincent Bernat
  • Date: 2015-07-28 08:02:59 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20150728080259-egxi2y7l5s8npdxv
Tags: 0.1.8-1
* New upstream release
* Bump Standards-Version to 3.9.6.
* Run unittests with Python 3 too.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
from pyasn1.compat.octets import str2octs, ints2octs
3
3
from pyasn1.error import PyAsn1Error
4
4
from sys import version_info
 
5
import math
5
6
if version_info[0:2] < (2, 7) or \
6
7
   version_info[0:2] in ( (3, 0), (3, 1) ):
7
8
    try:
13
14
 
14
15
class IntegerTestCase(unittest.TestCase):
15
16
    def testStr(self): assert str(univ.Integer(1)) in ('1','1L'),'str() fails'
 
17
    def testRepr(self):
 
18
        assert eval(repr(univ.Integer(123)), { 'Integer': univ.Integer}) == univ.Integer(123), 'repr() fails'
16
19
    def testAnd(self): assert univ.Integer(1) & 0 == 0, '__and__() fails'
17
20
    def testOr(self): assert univ.Integer(1) | 0 == 1, '__or__() fails'
18
21
    def testXor(self): assert univ.Integer(1) ^ 0 == 1, '__xor__() fails'
36
39
    def testInt(self): assert int(univ.Integer(3)) == 3, '__int__() fails'
37
40
    def testLong(self): assert int(univ.Integer(8)) == 8, '__long__() fails'
38
41
    def testFloat(self): assert float(univ.Integer(4))==4.0,'__float__() fails'
 
42
    def testPos(self): assert +univ.Integer(1) == 1, '__pos__() fails'
 
43
    def testNeg(self): assert -univ.Integer(1) == -1, '__neg__() fails'
 
44
    def testInvert(self): assert ~univ.Integer(1) == -2, '__invert__() fails'
 
45
    def testRound(self): 
 
46
        assert round(univ.Integer(1), 3) == 1.0,'__round__() fails'
 
47
    def testFloor(self): 
 
48
        assert math.floor(univ.Integer(1)) == 1,'__floor__() fails'
 
49
    def testCeil(self): 
 
50
        assert math.ceil(univ.Integer(1)) == 1,'__ceil__() fails'
 
51
    if version_info[0:2] > (2, 5):
 
52
        def testTrunc(self): 
 
53
            assert math.trunc(univ.Integer(1)) == 1,'__trunc__() fails'
39
54
    def testPrettyIn(self): assert univ.Integer('3') == 3, 'prettyIn() fails'
40
55
    def testTag(self):
41
56
        assert univ.Integer().getTagSet() == tag.TagSet(
56
71
        assert not univ.Boolean(False) and not univ.Boolean(0), 'False initializer fails'
57
72
    def testStr(self):
58
73
        assert str(univ.Boolean(1)) in ('1', '1L'), 'str() fails'
 
74
    def testRepr(self):
 
75
        assert eval(repr(univ.Boolean(1)), { 'Boolean': univ.Boolean}) == univ.Boolean(1), 'repr() fails'
59
76
    def testTag(self):
60
77
        assert univ.Boolean().getTagSet() == tag.TagSet(
61
78
            (),
93
110
    def testStr(self):
94
111
        assert str(self.b.clone('Urgent,Active')) == '(1, 1)'
95
112
    def testRepr(self):
96
 
        assert repr(self.b.clone('Urgent,Active')) == 'BitString("\'11\'B")'
 
113
        assert eval(repr(self.b.clone('Urgent,Active')), { 'BitString': univ.BitString }) == self.b.clone('Urgent,Active'), 'repr() fails'
97
114
    def testTag(self):
98
115
        assert univ.BitString().getTagSet() == tag.TagSet(
99
116
            (),
114
131
        assert univ.OctetString(hexValue="FA9823C43E43510DE3422") == ints2octs((250, 152, 35, 196, 62, 67, 81, 13, 227, 66, 32)), 'hex init fails'
115
132
    def testTuple(self):
116
133
        assert univ.OctetString((1,2,3,4,5)) == ints2octs((1,2,3,4,5)), 'tuple init failed'
 
134
    if version_info[0] <= 2:
 
135
        def testUnicode(self):
 
136
            assert univ.OctetString(unicode('q')) == 'q', 'unicode init fails'
 
137
    else:
 
138
        def testUnicode(self):
 
139
            assert univ.OctetString('q') == str2octs('q'), 'unicode init fails'
117
140
    def testStr(self):
118
141
        assert str(univ.OctetString('q')) == 'q', '__str__() fails'
119
142
    def testSeq(self):
120
143
        assert univ.OctetString('q')[0] == str2octs('q')[0],'__getitem__() fails'
 
144
    def testRepr(self):
 
145
        assert eval(repr(univ.OctetString('abc')), { 'OctetString': univ.OctetString}) == univ.OctetString('abc'), 'repr() fails'
121
146
    def testAsOctets(self):
122
147
        assert univ.OctetString('abcd').asOctets() == str2octs('abcd'), 'testAsOctets() fails'
123
148
    def testAsInts(self):
147
172
 
148
173
class Null(unittest.TestCase):
149
174
    def testStr(self): assert str(univ.Null('')) == '', 'str() fails'
 
175
    def testRepr(self):
 
176
        assert eval(repr(univ.Null()), { 'Null': univ.Null}) == univ.Null(), 'repr() fails'
150
177
    def testTag(self):
151
178
        assert univ.Null().getTagSet() == tag.TagSet(
152
179
            (),
161
188
            assert 0, 'constraint fail'
162
189
 
163
190
class RealTestCase(unittest.TestCase):
 
191
    def testFloat4BinEnc(self): assert univ.Real((0.25, 2, 3)) == 2.0, 'float initializer for binary encoding fails'
164
192
    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'
 
193
    def testRepr(self):
 
194
        assert eval(repr(univ.Real(-4.1)), { 'Real': univ.Real}) == univ.Real(-4.1), 'repr() fails'
 
195
        assert repr(univ.Real(-4.1)) == 'Real((-41, 10, -1))','repr() fails'
 
196
        assert eval(repr(univ.Real('inf')), { 'Real': univ.Real}) == univ.Real('inf'), 'repr() fails'
 
197
        assert repr(univ.Real('inf')) == 'Real(\'inf\')','repr() fails'
166
198
    def testAdd(self): assert univ.Real(-4.1) + 1.4 == -2.7, '__add__() fails'
167
199
    def testRadd(self): assert 4 + univ.Real(0.5) == 4.5, '__radd__() fails'
168
200
    def testSub(self): assert univ.Real(3.9) - 1.7 == 2.2, '__sub__() fails'
182
214
    # infinite float values
183
215
    def testStrInf(self):
184
216
        assert str(univ.Real('inf')) == 'inf','str() fails'
185
 
    def testReprInf(self):
186
 
        assert repr(univ.Real('inf')) == 'Real(\'inf\')','repr() fails'
187
217
    def testAddInf(self):
188
218
        assert univ.Real('inf') + 1 == float('inf'), '__add__() fails'
189
219
    def testRaddInf(self):
212
242
    def testMinusInf(self):
213
243
        assert univ.Real('-inf').isMinusInfinity(), 'isMinusInfinity failed'
214
244
 
 
245
    def testPos(self): assert +univ.Real(1.0) == 1.0, '__pos__() fails'
 
246
    def testNeg(self): assert -univ.Real(1.0) == -1.0, '__neg__() fails'
 
247
    def testRound(self): 
 
248
        assert round(univ.Real(1.123), 2) == 1.12,'__round__() fails'
 
249
    def testFloor(self): 
 
250
        assert math.floor(univ.Real(1.6)) == 1.0,'__floor__() fails'
 
251
    def testCeil(self): 
 
252
        assert math.ceil(univ.Real(1.2)) == 2.0,'__ceil__() fails'
 
253
    if version_info[0:2] > (2, 5):
 
254
        def testTrunc(self): 
 
255
            assert math.trunc(univ.Real(1.1)) == 1.0,'__trunc__() fails'
 
256
 
215
257
    def testTag(self):
216
258
        assert univ.Real().getTagSet() == tag.TagSet(
217
259
            (),
220
262
 
221
263
class ObjectIdentifier(unittest.TestCase):
222
264
    def testStr(self):
223
 
        assert str(univ.ObjectIdentifier((1,3,6))) == '1.3.6'
 
265
        assert str(univ.ObjectIdentifier((1,3,6))) == '1.3.6', 'str() fails'
 
266
    def testRepr(self):
 
267
        assert eval(repr(univ.ObjectIdentifier('1.3.6')), { 'ObjectIdentifier': univ.ObjectIdentifier}) == univ.ObjectIdentifier('1.3.6'), 'repr() fails'
224
268
    def testEq(self):
225
269
        assert univ.ObjectIdentifier((1,3,6)) == (1,3,6), '__cmp__() fails'
226
270
    def testAdd(self):
234
278
        assert o.isPrefixOf((1,3,6)), 'isPrefixOf() fails'
235
279
        assert o.isPrefixOf((1,3,6,1)), 'isPrefixOf() fails'
236
280
        assert not o.isPrefixOf((1,3)), 'isPrefixOf() fails'        
237
 
    def testInput(self):
 
281
    def testInput1(self):
238
282
        assert univ.ObjectIdentifier('1.3.6')==(1,3,6),'prettyIn() fails'
 
283
    def testInput2(self):
 
284
        assert univ.ObjectIdentifier((1,3,6))==(1,3,6),'prettyIn() fails'
 
285
    def testInput3(self):
 
286
        assert univ.ObjectIdentifier(univ.ObjectIdentifier('1.3') + (6,))==(1,3,6),'prettyIn() fails'
239
287
    def testTag(self):
240
288
        assert univ.ObjectIdentifier().getTagSet() == tag.TagSet(
241
289
            (),
248
296
            componentType=univ.OctetString('')
249
297
            )
250
298
        self.s2 = self.s1.clone()
 
299
    def testRepr(self):
 
300
        assert eval(repr(self.s1.clone().setComponents('a', 'b')), { 'SequenceOf': univ.SequenceOf, 'OctetString': univ.OctetString }) == self.s1.clone().setComponents('a', 'b'), 'repr() fails'
251
301
    def testTag(self):
252
302
        assert self.s1.getTagSet() == tag.TagSet(
253
303
            (),
275
325
        try:
276
326
            s.setComponentByPosition(1, univ.OctetString('Abc'))
277
327
        except:
278
 
            pass
 
328
            try:
 
329
                s.setComponentByPosition(1, univ.OctetString('Abc'),
 
330
                                         verifyConstraints=False)
 
331
            except:
 
332
                assert 0, 'constraint failes with verifyConstraints=True'
279
333
        else:
280
334
            assert 0, 'constraint fails'
 
335
    def testComponentTagsMatching(self):
 
336
        s = self.s1.clone()
 
337
        o = univ.OctetString('abc').subtype(explicitTag=tag.Tag(tag.tagClassPrivate, tag.tagFormatSimple, 12))
 
338
        try:
 
339
            s.setComponentByPosition(0, o)
 
340
        except:
 
341
            pass
 
342
        else:
 
343
            assert 0, 'inner supertype tag allowed'
 
344
    def testComponentConstraintsMatching(self):
 
345
        s = self.s1.clone()
 
346
        o = univ.OctetString().subtype(subtypeSpec=constraint.ConstraintsUnion(constraint.SingleValueConstraint(str2octs('cba'))))
 
347
        s.strictConstraints = True
 
348
        try:
 
349
            s.setComponentByPosition(0, o.clone('cba'))
 
350
        except:
 
351
            pass
 
352
        else:
 
353
            assert 0, 'inner supertype constraint allowed'
 
354
        s.strictConstraints = False
 
355
        try:
 
356
            s.setComponentByPosition(0, o.clone('cba'))
 
357
        except:
 
358
            assert 0, 'inner supertype constraint disallowed'
 
359
        else:
 
360
            pass
281
361
    def testSizeSpec(self):
282
362
        s = self.s1.clone(sizeSpec=constraint.ConstraintsUnion(
283
363
            constraint.ValueSizeConstraint(1,1)
317
397
        s = self.s1.clone(cloneValueFlag=1)
318
398
        assert len(s) == 1
319
399
        assert s.getComponentByPosition(0) == self.s1.getComponentByPosition(0)
320
 
        
 
400
    def testSetComponents(self):
 
401
        assert self.s1.clone().setComponents('abc', 'def') == \
 
402
            self.s1.setComponentByPosition(0, 'abc').setComponentByPosition(1, 'def')
 
403
                
321
404
class Sequence(unittest.TestCase):
322
405
    def setUp(self):
323
406
        self.s1 = univ.Sequence(componentType=namedtype.NamedTypes(
325
408
            namedtype.OptionalNamedType('nick', univ.OctetString('')),
326
409
            namedtype.DefaultedNamedType('age', univ.Integer(34))
327
410
            ))
 
411
    def testRepr(self):
 
412
        assert eval(repr(self.s1.clone().setComponents('a', 'b')), { 'Sequence': univ.Sequence, 'OctetString': univ.OctetString, 'Integer': univ.Integer, 'NamedTypes': namedtype.NamedTypes, 'NamedType': namedtype.NamedType, 'OptionalNamedType': namedtype.OptionalNamedType, 'DefaultedNamedType': namedtype.DefaultedNamedType }) == self.s1.clone().setComponents('a', 'b'), 'repr() fails'
328
413
    def testTag(self):
329
414
        assert self.s1.getTagSet() == tag.TagSet(
330
415
            (),
367
452
        assert s.getComponentByPosition(0) == self.s1.getComponentByPosition(0)
368
453
        assert s.getComponentByPosition(1) == self.s1.getComponentByPosition(1)
369
454
        assert s.getComponentByPosition(2) == self.s1.getComponentByPosition(2)
 
455
    def testComponentTagsMatching(self):
 
456
        s = self.s1.clone()
 
457
        o = univ.OctetString('abc').subtype(explicitTag=tag.Tag(tag.tagClassPrivate, tag.tagFormatSimple, 12))
 
458
        try:
 
459
            s.setComponentByName('name', o)
 
460
        except:
 
461
            pass
 
462
        else:
 
463
            assert 0, 'inner supertype tag allowed'
 
464
    def testComponentConstraintsMatching(self):
 
465
        s = self.s1.clone()
 
466
        o = univ.OctetString().subtype(subtypeSpec=constraint.ConstraintsUnion(constraint.SingleValueConstraint(str2octs('cba'))))
 
467
        s.strictConstraints = True
 
468
        try:
 
469
            s.setComponentByName('name', o.clone('cba'))
 
470
        except:
 
471
            pass
 
472
        else:
 
473
            assert 0, 'inner supertype constraint allowed'
 
474
        s.strictConstraints = False
 
475
        try:
 
476
            s.setComponentByName('name', o.clone('cba'))
 
477
        except:
 
478
            assert 0, 'inner supertype constraint disallowed'
 
479
        else:
 
480
            pass
 
481
    def testSetComponents(self):
 
482
        assert self.s1.clone().setComponents(name='a', nick='b', age=1) == \
 
483
            self.s1.setComponentByPosition(0, 'a').setComponentByPosition(1, 'b').setComponentByPosition(2, 1)
370
484
 
371
485
class SetOf(unittest.TestCase):
372
486
    def setUp(self):
432
546
            ))
433
547
    def testTag(self):
434
548
        assert self.s1.getTagSet() == tag.TagSet(), 'wrong tagSet'
 
549
    def testRepr(self):
 
550
        assert eval(repr(self.s1.clone().setComponents('a')), { 'Choice': univ.Choice, 'OctetString': univ.OctetString, 'Integer': univ.Integer, 'Boolean': univ.Boolean, 'NamedTypes': namedtype.NamedTypes, 'NamedType': namedtype.NamedType }) == self.s1.clone().setComponents('a'), 'repr() fails'
 
551
        assert eval(repr(self.s1.clone().setComponents(sex=self.s1.setComponentByPosition(1).getComponentByPosition(1).clone().setComponents(count=univ.Integer(123)))), { 'Choice': univ.Choice, 'OctetString': univ.OctetString, 'Integer': univ.Integer, 'Boolean': univ.Boolean, 'NamedTypes': namedtype.NamedTypes, 'NamedType': namedtype.NamedType }) == self.s1.clone().setComponents(sex=self.s1.setComponentByPosition(1).getComponentByPosition(1).clone().setComponents(count=univ.Integer(123))), 'repr() fails'
435
552
    def testOuterByTypeWithPythonValue(self):
436
553
        self.s1.setComponentByType(univ.OctetString.tagSet, 'abc')
437
554
        assert self.s1.getComponentByType(