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

more zero-length BER encodings, plus unit tests.

parent 6117cf14
...@@ -561,25 +561,28 @@ cdef object decode_structured (unsigned char * s, long * pos, long length): ...@@ -561,25 +561,28 @@ 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
m = s[pos[0]] if length == 0:
# first * 40 + second raise InsufficientData (pos[0])
r = [m // 40, m % 40] else:
n = 0
pos[0] = pos[0] + 1
for i from 1 <= i < length:
m = s[pos[0]] m = s[pos[0]]
hi = m & 0x80 # first * 40 + second
lo = m & 0x7f r = [m // 40, m % 40]
n = (n << 7) | lo n = 0
if not hi:
r.append (n)
n = 0
pos[0] = pos[0] + 1 pos[0] = pos[0] + 1
return r for i from 1 <= i < length:
m = s[pos[0]]
hi = m & 0x80
lo = m & 0x7f
n = (n << 7) | lo
if not hi:
r.append (n)
n = 0
pos[0] = pos[0] + 1
return r
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