From aba1c515534197cab1588ab0f3a2343be1f0a8e5 Mon Sep 17 00:00:00 2001 From: cclauss Date: Fri, 5 Apr 2019 01:32:39 +0200 Subject: tests: make Impacket (SMB server) Python 3 compatible Closes #3731 Fixes #3289 --- tests/python_dependencies/impacket/structure.py | 69 +++++++++++++------------ 1 file changed, 35 insertions(+), 34 deletions(-) (limited to 'tests/python_dependencies/impacket/structure.py') diff --git a/tests/python_dependencies/impacket/structure.py b/tests/python_dependencies/impacket/structure.py index 7a04117ff..68066a61f 100644 --- a/tests/python_dependencies/impacket/structure.py +++ b/tests/python_dependencies/impacket/structure.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Copyright (c) 2003-2016 CORE Security Technologies # # This software is provided under under a slightly modified version @@ -97,18 +98,18 @@ class Structure: def packField(self, fieldName, format = None): if self.debug: - print "packField( %s | %s )" % (fieldName, format) + print("packField( %s | %s )" % (fieldName, format)) if format is None: format = self.formatForField(fieldName) - if self.fields.has_key(fieldName): + if fieldName in self.fields: ans = self.pack(format, self.fields[fieldName], field = fieldName) else: ans = self.pack(format, None, field = fieldName) if self.debug: - print "\tanswer %r" % ans + print("\tanswer %r" % ans) return ans @@ -119,8 +120,8 @@ class Structure: for field in self.commonHdr+self.structure: try: data += self.packField(field[0], field[1]) - except Exception, e: - if self.fields.has_key(field[0]): + except Exception as e: + if field[0] in self.fields: e.args += ("When packing field '%s | %s | %r' in %s" % (field[0], field[1], self[field[0]], self.__class__),) else: e.args += ("When packing field '%s | %s' in %s" % (field[0], field[1], self.__class__),) @@ -136,16 +137,16 @@ class Structure: self.rawData = data for field in self.commonHdr+self.structure: if self.debug: - print "fromString( %s | %s | %r )" % (field[0], field[1], data) + print("fromString( %s | %s | %r )" % (field[0], field[1], data)) size = self.calcUnpackSize(field[1], data, field[0]) if self.debug: - print " size = %d" % size + print(" size = %d" % size) dataClassOrCode = str if len(field) > 2: dataClassOrCode = field[2] try: self[field[0]] = self.unpack(field[1], data[:size], dataClassOrCode = dataClassOrCode, field = field[0]) - except Exception,e: + except Exception as e: e.args += ("When unpacking field '%s | %s | %r[:%d]'" % (field[0], field[1], data, size),) raise @@ -175,7 +176,7 @@ class Structure: def pack(self, format, data, field = None): if self.debug: - print " pack( %s | %r | %s)" % (format, data, field) + print(" pack( %s | %r | %s)" % (format, data, field)) if field: addressField = self.findAddressFieldFor(field) @@ -206,7 +207,7 @@ class Structure: try: return self.pack(two[0], data) except: - if (self.fields.has_key(two[1])) and (self[two[1]] is not None): + if (two[1] in self.fields) and (self[two[1]] is not None): return self.pack(two[0], id(self[two[1]]) & ((1<<(calcsize(two[0])*8))-1) ) else: return self.pack(two[0], 0) @@ -228,7 +229,7 @@ class Structure: if two[0]: if two[0].isdigit(): if int(two[0]) != len(data): - raise Exception, "Array field has a constant size, and it doesn't match the actual value" + raise Exception("Array field has a constant size, and it doesn't match the actual value") else: return self.pack(two[0], len(data))+answer return answer @@ -256,7 +257,7 @@ class Structure: return '%s\0\0\0\0%s%s' % (l,l,data) if data is None: - raise Exception, "Trying to pack None" + raise Exception("Trying to pack None") # literal specifier if format[:1] == ':': @@ -267,7 +268,7 @@ class Structure: def unpack(self, format, data, dataClassOrCode = str, field = None): if self.debug: - print " unpack( %s | %r )" % (format, data) + print(" unpack( %s | %r )" % (format, data)) if field: addressField = self.findAddressFieldFor(field) @@ -288,7 +289,7 @@ class Structure: if format[:1] == "'" or format[:1] == '"': answer = format[1:] if answer != data: - raise Exception, "Unpacked data doesn't match constant value '%r' should be '%r'" % (data, answer) + raise Exception("Unpacked data doesn't match constant value '%r' should be '%r'" % (data, answer)) return answer # address specifier @@ -334,13 +335,13 @@ class Structure: # asciiz specifier if format == 'z': if data[-1] != '\x00': - raise Exception, ("%s 'z' field is not NUL terminated: %r" % (field, data)) + raise Exception("%s 'z' field is not NUL terminated: %r" % (field, data)) return data[:-1] # remove trailing NUL # unicode specifier if format == 'u': if data[-2:] != '\x00\x00': - raise Exception, ("%s 'u' field is not NUL-NUL terminated: %r" % (field, data)) + raise Exception("%s 'u' field is not NUL-NUL terminated: %r" % (field, data)) return data[:-2] # remove trailing NUL # DCE-RPC/NDR string specifier @@ -392,7 +393,7 @@ class Structure: answer = 0 if two[0].isdigit(): if int(two[0]) != len(data): - raise Exception, "Array field has a constant size, and it doesn't match the actual value" + raise Exception("Array field has a constant size, and it doesn't match the actual value") elif two[0]: answer += self.calcPackSize(two[0], len(data)) @@ -428,7 +429,7 @@ class Structure: def calcUnpackSize(self, format, data, field = None): if self.debug: - print " calcUnpackSize( %s | %s | %r)" % (field, format, data) + print(" calcUnpackSize( %s | %s | %r)" % (field, format, data)) # void specifier if format[:1] == '_': @@ -487,7 +488,7 @@ class Structure: # "printf" string specifier if format[:1] == '%': - raise Exception, "Can't guess the size of a printf like specifier for unpacking" + raise Exception("Can't guess the size of a printf like specifier for unpacking") # asciiz specifier if format[:1] == 'z': @@ -520,7 +521,7 @@ class Structure: for field in self.commonHdr+self.structure: if field[0] == fieldName: return field[1] - raise Exception, ("Field %s not found" % fieldName) + raise Exception("Field %s not found" % fieldName) def findAddressFieldFor(self, fieldName): descriptor = '&%s' % fieldName @@ -558,7 +559,7 @@ class Structure: def dump(self, msg = None, indent = 0): if msg is None: msg = self.__class__.__name__ ind = ' '*indent - print "\n%s" % msg + print("\n%s" % msg) fixedFields = [] for field in self.commonHdr+self.structure: i = field[0] @@ -566,18 +567,18 @@ class Structure: fixedFields.append(i) if isinstance(self[i], Structure): self[i].dump('%s%s:{' % (ind,i), indent = indent + 4) - print "%s}" % ind + print("%s}" % ind) else: - print "%s%s: {%r}" % (ind,i,self[i]) + print("%s%s: {%r}" % (ind,i,self[i])) # Do we have remaining fields not defined in the structures? let's # print them remainingFields = list(set(self.fields) - set(fixedFields)) for i in remainingFields: if isinstance(self[i], Structure): self[i].dump('%s%s:{' % (ind,i), indent = indent + 4) - print "%s}" % ind + print("%s}" % ind) else: - print "%s%s: {%r}" % (ind,i,self[i]) + print("%s%s: {%r}" % (ind,i,self[i])) class _StructureTest: @@ -589,23 +590,23 @@ class _StructureTest: return self.theClass(alignment = self.alignment) def run(self): - print - print "-"*70 + print() + print("-"*70) testName = self.__class__.__name__ - print "starting test: %s....." % testName + print("starting test: %s....." % testName) a = self.create() self.populate(a) a.dump("packing.....") a_str = str(a) - print "packed: %r" % a_str - print "unpacking....." + print("packed: %r" % a_str) + print("unpacking.....") b = self.create(a_str) b.dump("unpacked.....") - print "repacking....." + print("repacking.....") b_str = str(b) if b_str != a_str: - print "ERROR: original packed and repacked don't match" - print "packed: %r" % b_str + print("ERROR: original packed and repacked don't match") + print("packed: %r" % b_str) class _Test_simple(_StructureTest): class theClass(Structure): @@ -732,7 +733,7 @@ if __name__ == '__main__': try: _Test_fixedLength().run() except: - print "cannot repack because length is bogus" + print("cannot repack because length is bogus") _Test_simple_aligned4().run() _Test_nested().run() -- cgit v1.2.3