__author__ = 'Martin Pool <mbp@sourcefrog.net>'
import unittest
-# import tdbutil
+import oldtdbutil
import samba.tdbpack
-packer = samba.tdbpack.pack
-unpacker = samba.tdbpack.unpack
-
-
+both_unpackers = (samba.tdbpack.unpack, oldtdbutil.unpack)
+both_packers = (samba.tdbpack.pack, oldtdbutil.pack)
+
class PackTests(unittest.TestCase):
symm_cases = [('B', ['hello' * 51], '\xff\0\0\0' + 'hello' * 51),
('w', [42], '\x2a\0'),
def test_symmetric(self):
"""Cookbook of symmetric pack/unpack tests
"""
- for format, values, expected in self.symm_cases:
- self.assertEquals(packer(format, values), expected)
- out, rest = unpacker(format, expected)
- self.assertEquals(rest, '')
- self.assertEquals(list(values), list(out))
+ for packer in both_packers:
+ for unpacker in both_unpackers:
+ for format, values, expected in self.symm_cases:
+ self.assertEquals(packer(format, values), expected)
+ out, rest = unpacker(format, expected)
+ self.assertEquals(rest, '')
+ self.assertEquals(list(values), list(out))
def test_pack(self):
cases = [('w', (42,), '\x2a\0'),
('p', [None], '\0\0\0\0'),
('p', ['true'], '\x01\0\0\0'),
-
- ('w', {1: 'fruit'}, '\x01\0'),
- # passing a dictionary is dodgy, but it gets coerced to keys
- # as if you called list()
]
- for format, values, expected in cases:
- self.assertEquals(packer(format, values), expected)
+ for packer in both_packers:
+ for format, values, expected in cases:
+ self.assertEquals(packer(format, values), expected)
def test_unpack_extra(self):
# Test leftover data
- for format, values, packed in self.symm_cases:
- out, rest = unpacker(format, packed + 'hello sailor!')
- self.assertEquals(rest, 'hello sailor!')
- self.assertEquals(list(values), list(out))
+ for unpacker in both_unpackers:
+ for format, values, packed in self.symm_cases:
+ out, rest = unpacker(format, packed + 'hello sailor!')
+ self.assertEquals(rest, 'hello sailor!')
+ self.assertEquals(list(values), list(out))
def test_unpack(self):
"""Cookbook of tricky unpack tests"""
cases = [
+ # Apparently I couldn't think of any tests that weren't
+ # symmetric :-/
]
- for format, values, expected in cases:
- out, rest = unpacker(format, expected)
- self.assertEquals(rest, '')
- self.assertEquals(list(values), list(out))
+ for unpacker in both_unpackers:
+ for format, values, expected in cases:
+ out, rest = unpacker(format, expected)
+ self.assertEquals(rest, '')
+ self.assertEquals(list(values), list(out))
def test_pack_failures(self):
"""Expected errors for incorrect packing"""
- cases = [('w', [], IndexError),
- ('w', (), IndexError),
- ('w', {}, IndexError),
- ('ww', [2], IndexError),
- ('w', 2, TypeError),
- ('', [1, 2, 3], IndexError),
- ('w', None, TypeError),
- ('wwwwwwwwwwww', [], IndexError),
- ('w', [2, 3], IndexError),
- ('w', [0x60A15EC5L], TypeError),
- ('w', [None], TypeError),
- ('w', xrange(10000), IndexError),
- ('d', [], IndexError),
- ('d', [0L], TypeError),
- ('p', [], IndexError),
- ('f', [2], TypeError),
- ('P', [None], TypeError),
- ('P', (), IndexError),
- ('f', [packer], TypeError),
- ('fw', ['hello'], IndexError),
- ('f', [u'hello'], TypeError),
- ('B', [2], TypeError),
- (None, [2, 3, 4], TypeError),
- (ord('f'), [20], TypeError),
- (['w', 'w'], [2, 2], TypeError),
- ('Q', [2], ValueError),
- ('fQ', ['2', 3], ValueError),
- ('fQ', ['2'], IndexError),
- (2, [2], TypeError),
- ({}, {}, TypeError)]
- for format, values, throwable_class in cases:
- def do_pack():
- packer(format, values)
- self.assertRaises(throwable_class, do_pack)
+ cases = [('w', []),
+ ('w', ()),
+ ('w', {}),
+ ('ww', [2]),
+ ('w', 2),
+ ('', [1, 2, 3]),
+ ('w', None),
+ ('wwwwwwwwwwww', []),
+ ('w', [2, 3]),
+ ('w', [0x60A15EC5L]),
+ ('w', [None]),
+ ('w', xrange(10000)),
+ ('d', []),
+ ('d', [0L]),
+ ('p', []),
+ ('f', [2]),
+ ('P', [None]),
+ ('P', ()),
+ ('f', [hex]),
+ ('fw', ['hello']),
+ ('f', [u'hello']),
+ ('B', [2]),
+ (None, [2, 3, 4]),
+ (ord('f'), [20]),
+ (['w', 'w'], [2, 2]),
+ ('Q', [2]),
+ ('fQ', ['2', 3]),
+ ('fQ', ['2']),
+ (2, [2]),
+ ({}, {})]
+ for packer in both_packers:
+ for format, values in cases:
+ try:
+ packer(format, values)
+ except StandardError:
+ pass
+ else:
+ raise AssertionError("didn't get exception: format %s, values %s, packer %s"
+ % (`format`, `values`, `packer`))
def test_unpack_failures(self):
('B', 'foobar', IndexError),
('BB', '\x01\0\0\0a\x01', IndexError),
]
-
- for format, values, throwable_class in cases:
- def do_unpack():
- unpacker(format, values)
+
+ for unpacker in both_unpackers:
+ for format, values, throwable_class in cases:
+ def do_unpack():
+ unpacker(format, values)
self.assertRaises(throwable_class, do_unpack)