Commit 51f9e6ea authored by Sam Rushing's avatar Sam Rushing

more zero-length BER encodings, plus unit tests.

parent 6117cf14
...@@ -561,6 +561,9 @@ cdef object decode_structured (unsigned char * s, long * pos, long length): ...@@ -561,6 +561,9 @@ cdef object decode_structured (unsigned char * s, long * pos, long length):
cdef object decode_objid (unsigned char * s, long * pos, long length): cdef object decode_objid (unsigned char * s, long * pos, long length):
cdef long i, m, n, hi, lo cdef long i, m, n, hi, lo
cdef list r cdef list r
if length == 0:
raise InsufficientData (pos[0])
else:
m = s[pos[0]] m = s[pos[0]]
# first * 40 + second # first * 40 + second
r = [m // 40, m % 40] r = [m // 40, m % 40]
...@@ -579,7 +582,7 @@ cdef object decode_objid (unsigned char * s, long * pos, long length): ...@@ -579,7 +582,7 @@ cdef object decode_objid (unsigned char * s, long * pos, long length):
cdef object decode_boolean (unsigned char * s, long * pos, long length): cdef object decode_boolean (unsigned char * s, long * pos, long length):
if length == 0: if length == 0:
return False: return False
else: else:
pos[0] += 1 pos[0] += 1
if s[pos[0]-1] == 0xff: if s[pos[0]-1] == 0xff:
......
...@@ -137,6 +137,24 @@ class bignum_test_3 (ber_test_case): ...@@ -137,6 +137,24 @@ class bignum_test_3 (ber_test_case):
print n print n
self.assertEquals (decode (INTEGER (n))[0], n) self.assertEquals (decode (INTEGER (n))[0], n)
class zero_length_tests (ber_test_case):
# tests for Issue #71.
def runTest (self):
self.assertEquals (
# zero-length boolean
decode (SEQUENCE ('\x01\x00', INTEGER (3141))),
([False, 3141], 8)
)
self.assertEquals (
# zero-length bitstring
decode (SEQUENCE ('\x03\x00', INTEGER (3141))),
([('bitstring', (0, '')), 3141], 8)
)
# zero-length OBJID
with self.assertRaises (InsufficientData):
decode ('\x06\x00')
def suite(): def suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest (simple_test()) suite.addTest (simple_test())
...@@ -144,6 +162,7 @@ def suite(): ...@@ -144,6 +162,7 @@ def suite():
suite.addTest (bignum_test()) suite.addTest (bignum_test())
suite.addTest (bignum_test_2()) suite.addTest (bignum_test_2())
suite.addTest (bignum_test_3()) suite.addTest (bignum_test_3())
suite.addTest (zero_length_tests())
return suite return suite
if __name__ == '__main__': if __name__ == '__main__':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment