From d70d4ef34b21d86ede25f00ed1b9726c9e9444be Mon Sep 17 00:00:00 2001 From: Martin Pool Date: Mon, 21 Oct 2002 11:13:53 +0000 Subject: Import old pure Python version of tdbpack/unpack, so that we can do compatibility testing against the shiny new C version. This version is slightly modified to not call codepage conversion routines, and renamed tdbutil->oldtdbutil. (This used to be commit 2dc4373727f73a46cd6135413e50917fb7fa538b) --- source3/python/examples/tdbpack/oldtdbutil.py | 144 ++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 source3/python/examples/tdbpack/oldtdbutil.py (limited to 'source3/python/examples/tdbpack') diff --git a/source3/python/examples/tdbpack/oldtdbutil.py b/source3/python/examples/tdbpack/oldtdbutil.py new file mode 100644 index 0000000000..ac435b8bac --- /dev/null +++ b/source3/python/examples/tdbpack/oldtdbutil.py @@ -0,0 +1,144 @@ +#!/usr/bin/python +############################################################# +# tdbutil +# +# Purpose: +# Contains functions that are used to pack and unpack data +# from Samba's tdb databases. Samba sometimes represents complex +# data structures as a single value in a database. These functions +# allow other python scripts to package data types into a single python +# string and unpackage them. +# +# +# XXXXX: This code is no longer used; it's just here for testing +# compatibility with the new (much faster) C implementation. +# +############################################################## +import string + +def pack(format,list): + retstring = '' + listind = 0 + + # Cycle through format entries + for type in format: + # Null Terminated String + if (type == 'f' or type == 'P'): + retstring = retstring + list[listind] + "\000" + # 4 Byte Number + if (type == 'd'): + retstring = retstring + PackNum(list[listind],4) + # 2 Byte Number + if (type == 'w'): + retstring = retstring + PackNum(list[listind],2) + # Pointer Value + if (type == 'p'): + if (list[listind]): + retstring = retstring + PackNum(1,4) + else: + retstring = retstring + PackNum(0,4) + # Buffer and Length + if (type == 'B'): + # length + length = list[listind] + retstring = retstring + PackNum(length,4) + length = int(length) + listind = listind + 1 + # buffer + retstring = retstring + list[listind][:length] + + listind = listind + 1 + + return retstring + +def unpack(format,buffer): + retlist = [] + bufind = 0 + + lasttype = "" + for type in format: + # Pointer Value + if (type == 'p'): + newvalue = UnpackNum(buffer[bufind:bufind+4]) + bufind = bufind + 4 + if (newvalue): + newvalue = 1L + else: + newvalue = 0L + retlist.append(newvalue) + # Previous character till end of data + elif (type == '$'): + if (lasttype == 'f'): + while (bufind < len(buffer)): + newstring = '' + while (buffer[bufind] != '\000'): + newstring = newstring + buffer[bufind] + bufind = bufind + 1 + bufind = bufind + 1 + retlist.append(newstring) + # Null Terminated String + elif (type == 'f' or type == 'P'): + newstring = '' + while (buffer[bufind] != '\000'): + newstring = newstring + buffer[bufind] + bufind = bufind + 1 + bufind = bufind + 1 + retlist.append(newstring) + # 4 Byte Number + elif (type == 'd'): + newvalue = UnpackNum(buffer[bufind:bufind+4]) + bufind = bufind + 4 + retlist.append(newvalue) + # 2 Byte Number + elif (type == 'w'): + newvalue = UnpackNum(buffer[bufind:bufind+2]) + bufind = bufind + 2 + retlist.append(newvalue) + # Length and Buffer + elif (type == 'B'): + # Length + length = UnpackNum(buffer[bufind:bufind+4]) + bufind = bufind + 4 + retlist.append(length) + length = int(length) + # Buffer + retlist.append(buffer[bufind:bufind+length]) + bufind = bufind + length + + lasttype = type + + return ((retlist,buffer[bufind:])) + +def PackNum(myint,size): + retstring = '' + size = size * 2 + hint = hex(myint)[2:] + + # Check for long notation + if (hint[-1:] == 'L'): + hint = hint[:-1] + + addon = size - len(hint) + for i in range(0,addon): + hint = '0' + hint + + while (size > 0): + val = string.atoi(hint[size-2:size],16) + retstring = retstring + chr(val) + size = size - 2 + + return retstring + +def UnpackNum(buffer): + size = len(buffer) + mystring = '' + + for i in range(size-1,-1,-1): + val = hex(ord(buffer[i]))[2:] + if (len(val) == 1): + val = '0' + val + mystring = mystring + val + if (len(mystring) > 4): + return string.atol(mystring,16) + else: + return string.atoi(mystring,16) -- cgit From 23e608435895a39a4da52c3f649b130140f09662 Mon Sep 17 00:00:00 2001 From: Martin Pool Date: Mon, 21 Oct 2002 11:44:26 +0000 Subject: Test both new samba.tdbpack and oldtdbutil pack/unpack routines. This makes the test suite fail because at the moment they are in fact not behaving the same way. (This used to be commit 44dd7746ede7f7f9efcf7dabcd351b1d800e535c) --- source3/python/examples/tdbpack/test_tdbpack.py | 66 ++++++++++++++----------- 1 file changed, 37 insertions(+), 29 deletions(-) (limited to 'source3/python/examples/tdbpack') diff --git a/source3/python/examples/tdbpack/test_tdbpack.py b/source3/python/examples/tdbpack/test_tdbpack.py index 36fed881e3..659dc0efed 100755 --- a/source3/python/examples/tdbpack/test_tdbpack.py +++ b/source3/python/examples/tdbpack/test_tdbpack.py @@ -17,13 +17,12 @@ string, with one character per field.""" __author__ = 'Martin Pool ' 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'), @@ -78,11 +77,13 @@ class PackTests(unittest.TestCase): 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): @@ -100,25 +101,30 @@ class PackTests(unittest.TestCase): # 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): @@ -141,7 +147,7 @@ class PackTests(unittest.TestCase): ('f', [2], TypeError), ('P', [None], TypeError), ('P', (), IndexError), - ('f', [packer], TypeError), + ('f', [hex], TypeError), ('fw', ['hello'], IndexError), ('f', [u'hello'], TypeError), ('B', [2], TypeError), @@ -153,10 +159,11 @@ class PackTests(unittest.TestCase): ('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) + for packer in both_packers: + for format, values, throwable_class in cases: + def do_pack(): + packer(format, values) + self.assertRaises(throwable_class, do_pack) def test_unpack_failures(self): @@ -182,10 +189,11 @@ class PackTests(unittest.TestCase): ('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) -- cgit