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 11 12except ImportError: 13 import unittest 14 15from tests.base import BaseTestCase 16 17from pyasn1.codec.der import decoder 18from pyasn1.compat.octets import ints2octs, null 19from pyasn1.error import PyAsn1Error 20 21 22class BitStringDecoderTestCase(BaseTestCase): 23 def testShortMode(self): 24 assert decoder.decode( 25 ints2octs((3, 127, 6) + (170,) * 125 + (128,)) 26 ) == (((1, 0) * 501), null) 27 28 def testIndefMode(self): 29 try: 30 decoder.decode( 31 ints2octs((35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)) 32 ) 33 except PyAsn1Error: 34 pass 35 else: 36 assert 0, 'indefinite length encoding tolerated' 37 38 def testDefModeChunked(self): 39 try: 40 assert decoder.decode( 41 ints2octs((35, 8, 3, 2, 0, 169, 3, 2, 1, 138)) 42 ) 43 except PyAsn1Error: 44 pass 45 else: 46 assert 0, 'chunked encoding tolerated' 47 48 49class OctetStringDecoderTestCase(BaseTestCase): 50 def testShortMode(self): 51 assert decoder.decode( 52 '\004\017Quick brown fox'.encode() 53 ) == ('Quick brown fox'.encode(), ''.encode()) 54 55 def testIndefMode(self): 56 try: 57 decoder.decode( 58 ints2octs((36, 128, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 0, 0)) 59 ) 60 except PyAsn1Error: 61 pass 62 else: 63 assert 0, 'indefinite length encoding tolerated' 64 65 def testChunkedMode(self): 66 try: 67 decoder.decode( 68 ints2octs((36, 23, 4, 2, 81, 117, 4, 2, 105, 99, 4, 2, 107, 32, 4, 2, 98, 114, 4, 2, 111, 119, 4, 1, 110)) 69 ) 70 except PyAsn1Error: 71 pass 72 else: 73 assert 0, 'chunked encoding tolerated' 74 75 76suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 77 78if __name__ == '__main__': 79 unittest.TextTestRunner(verbosity=2).run(suite) 80