1#
2# This file is part of pyasn1 software.
3#
4# Copyright (c) 2005-2018, Ilya Etingof <etingof@gmail.com>
5# License: http://snmplabs.com/pyasn1/license.html
6#
7import sys
8
9try:
10    import unittest2 as unittest
11except ImportError:
12    import unittest
13
14from tests.base import BaseTestCase
15
16from pyasn1.codec.cer import decoder
17from pyasn1.compat.octets import ints2octs, str2octs, null
18from pyasn1.error import PyAsn1Error
19
20
21class BooleanDecoderTestCase(BaseTestCase):
22    def testTrue(self):
23        assert decoder.decode(ints2octs((1, 1, 255))) == (1, null)
24
25    def testFalse(self):
26        assert decoder.decode(ints2octs((1, 1, 0))) == (0, null)
27
28    def testEmpty(self):
29        try:
30            decoder.decode(ints2octs((1, 0)))
31        except PyAsn1Error:
32            pass
33
34    def testOverflow(self):
35        try:
36            decoder.decode(ints2octs((1, 2, 0, 0)))
37        except PyAsn1Error:
38            pass
39
40class BitStringDecoderTestCase(BaseTestCase):
41    def testShortMode(self):
42        assert decoder.decode(
43            ints2octs((3, 3, 6, 170, 128))
44        ) == (((1, 0) * 5), null)
45
46    def testLongMode(self):
47        assert decoder.decode(
48            ints2octs((3, 127, 6) + (170,) * 125 + (128,))
49        ) == (((1, 0) * 501), null)
50
51    # TODO: test failures on short chunked and long unchunked substrate samples
52
53
54class OctetStringDecoderTestCase(BaseTestCase):
55    def testShortMode(self):
56        assert decoder.decode(
57            ints2octs((4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120)),
58        ) == (str2octs('Quick brown fox'), null)
59
60    def testLongMode(self):
61        assert decoder.decode(
62            ints2octs((36, 128, 4, 130, 3, 232) + (81,) * 1000 + (4, 1, 81, 0, 0))
63        ) == (str2octs('Q' * 1001), null)
64
65    # TODO: test failures on short chunked and long unchunked substrate samples
66
67
68suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
69
70if __name__ == '__main__':
71    unittest.TextTestRunner(verbosity=2).run(suite)
72