mirror of
https://github.com/samba-team/samba.git
synced 2024-12-22 13:34:15 +03:00
python/samba/tests: Py2/Py3 port for hexdump
Signed-off-by: Noel Power <noel.power@suse.com> Reviewed-by: Andreas Schneider <asn@samba.org> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
This commit is contained in:
parent
5b96e34981
commit
d5cd9af7b5
@ -36,6 +36,7 @@ import re
|
||||
import samba.auth
|
||||
import samba.dcerpc.base
|
||||
from samba.compat import PY3, text_type
|
||||
from samba.compat import string_types
|
||||
from random import randint
|
||||
if not PY3:
|
||||
# Py2 only
|
||||
@ -50,7 +51,7 @@ except ImportError:
|
||||
class SkipTest(Exception):
|
||||
"""Test skipped."""
|
||||
|
||||
HEXDUMP_FILTER=''.join([(len(repr(chr(x)))==3) and chr(x) or '.' for x in range(256)])
|
||||
HEXDUMP_FILTER=bytearray([x if ((len(repr(chr(x)))==3) and (x < 127)) else ord('.') for x in range(256)])
|
||||
|
||||
class TestCase(unittest.TestCase):
|
||||
"""A Samba test case."""
|
||||
@ -80,14 +81,21 @@ class TestCase(unittest.TestCase):
|
||||
def hexdump(self, src):
|
||||
N = 0
|
||||
result = ''
|
||||
is_string = isinstance(src, string_types)
|
||||
while src:
|
||||
ll = src[:8]
|
||||
lr = src[8:16]
|
||||
src = src[16:]
|
||||
hl = ' '.join(["%02X" % ord(x) for x in ll])
|
||||
hr = ' '.join(["%02X" % ord(x) for x in lr])
|
||||
ll = ll.translate(HEXDUMP_FILTER)
|
||||
lr = lr.translate(HEXDUMP_FILTER)
|
||||
if is_string:
|
||||
hl = ' '.join(["%02X" % ord(x) for x in ll])
|
||||
hr = ' '.join(["%02X" % ord(x) for x in lr])
|
||||
ll = ll.translate(HEXDUMP_FILTER)
|
||||
lr = lr.translate(HEXDUMP_FILTER)
|
||||
else:
|
||||
hl = ' '.join(["%02X" % x for x in ll])
|
||||
hr = ' '.join(["%02X" % x for x in lr])
|
||||
ll = ll.translate(HEXDUMP_FILTER).decode('utf8')
|
||||
lr = lr.translate(HEXDUMP_FILTER).decode('utf8')
|
||||
result += "[%04X] %-*s %-*s %s %s\n" % (N, 8*3, hl, 8*3, hr, ll, lr)
|
||||
N += 16
|
||||
return result
|
||||
|
Loading…
Reference in New Issue
Block a user