From 80153b97aac432e7090f7bdd47affef29d1c1c4a Mon Sep 17 00:00:00 2001 From: Mauri Miettinen Date: Tue, 7 Feb 2017 10:43:13 +0200 Subject: [PATCH] autopepped UTscapy.py --- scapy-2.3.3/scapy/tools/UTscapy.py | 199 ++++++++++++++++++----------- 1 file changed, 121 insertions(+), 78 deletions(-) diff --git a/scapy-2.3.3/scapy/tools/UTscapy.py b/scapy-2.3.3/scapy/tools/UTscapy.py index c640146..7479bc0 100755 --- a/scapy-2.3.3/scapy/tools/UTscapy.py +++ b/scapy-2.3.3/scapy/tools/UTscapy.py @@ -1,14 +1,22 @@ -## This file is part of Scapy -## See http://www.secdev.org/projects/scapy for more informations -## Copyright (C) Philippe Biondi -## This program is published under a GPLv2 license +# This file is part of Scapy +# See http://www.secdev.org/projects/scapy for more informations +# Copyright (C) Philippe Biondi +# This program is published under a GPLv2 license """ Unit testing infrastructure for Scapy """ -import sys,getopt,imp -import bz2, base64, os.path, time, traceback, zlib, sha +import sys +import getopt +import imp +import bz2 +import base64 +import os.path +import time +import traceback +import zlib +import sha #### Import tool #### @@ -19,8 +27,8 @@ def import_module(name): name = os.path.basename(name) if name.endswith(".py"): name = name[:-3] - f,path,desc = imp.find_module(name,[thepath]) - + f, path, desc = imp.find_module(name, [thepath]) + try: return imp.load_module(name, f, path, desc) finally: @@ -31,25 +39,29 @@ def import_module(name): #### INTERNAL/EXTERNAL FILE EMBEDDING #### class File: + def __init__(self, name, URL, local): self.name = name self.local = local self.URL = URL + def get_local(self): return bz2.decompress(base64.decodestring(self.local)) + def get_URL(self): return self.URL + def write(self, dir): if dir: dir += "/" - open(dir+self.name,"w").write(self.get_local()) + open(dir + self.name, "w").write(self.get_local()) + - # Embed a base64 encoded bziped version of js and css files # to work if you can't reach Internet. class External_Files: UTscapy_js = File("UTscapy.js", "http://www.secdev.org/projects/UTscapy/UTscapy.js", -"""QlpoOTFBWSZTWWVijKQAAXxfgERUYOvAChIhBAC/79+qQAH8AFA0poANAMjQAAAG + """QlpoOTFBWSZTWWVijKQAAXxfgERUYOvAChIhBAC/79+qQAH8AFA0poANAMjQAAAG ABo0NGEZNBo00BhgAaNDRhGTQaNNAYFURJinplGaKbRkJiekzSenqmpA0Gm1LFMp RUklVQlK9WUTZYpNFI1IiEWEFT09Sfj5uO+qO6S5DQwKIxM92+Zku94wL6V/1KTK an2c66Ug6SmVKy1ZIrgauxMVLF5xLH0lJRQuKlqLF10iatlTzqvw7S9eS3+h4lu3 @@ -58,8 +70,8 @@ class External_Files: gueXo3kcYi94K6hSO3ldD2O/qJXOFqJ8o3TE2aQahxtQpCVUKQMvODHwu2YkaORY ZC6gihEallcHDIAtRPScBACAJnUggYhLDX6DEko7nC9GvAw5OcEkiyDUbLdiGCzD aXWMC2DuQ2Y6sGf6NcRuON7QSbhHsPc4KKmZ/xdyRThQkGVijKQ=""") - UTscapy_css = File("UTscapy.css","http://www.secdev.org/projects/UTscapy/UTscapy.css", -"""QlpoOTFBWSZTWTbBCNEAAE7fgHxwSB//+Cpj2QC//9/6UAR+63dxbNzO3ccmtGEk + UTscapy_css = File("UTscapy.css", "http://www.secdev.org/projects/UTscapy/UTscapy.css", + """QlpoOTFBWSZTWTbBCNEAAE7fgHxwSB//+Cpj2QC//9/6UAR+63dxbNzO3ccmtGEk pM0m1I9E/Qp6g9Q09TNQ9QDR6gMgAkiBFG9U9TEGRkGgABoABoBmpJkRAaAxD1AN Gh6gNADQBzAATJgATCYJhDAEYAEiQkwIyJk0n6qenpqeoaMUeo9RgIxp6pX78kfx Jx4MUhDHKEb2pJAYAelG1cybiZBBDipH8ocxNyHDAqTUxiQmIAEDE3ApIBUUECAT @@ -81,10 +93,12 @@ class External_Files: vzM985aHXOHAxQN2UQZbQkUv3D4Vc+lyvalAffv3Tyg4ks3a22kPXiyeCGweviNX 0K8TKasyOhGsVamTUAZBXfQVw1zmdS4rHDnbHgtIjX3DcCt6UIr0BHTYjdV0JbPj r1APYgXihjQwM2M83AKIhwQQJv/F3JFOFCQNsEI0QA==""") + def get_local_dict(cls): return dict((x, y.name) for (x, y) in cls.__dict__.iteritems() if isinstance(y, File)) get_local_dict = classmethod(get_local_dict) + def get_URL_dict(cls): return dict((x, y.URL) for (x, y) in cls.__dict__.iteritems() if isinstance(y, File)) @@ -94,14 +108,16 @@ def get_URL_dict(cls): #### HELPER CLASSES FOR PARAMETRING OUTPUT FORMAT #### class EnumClass: - def from_string(cls,x): + + def from_string(cls, x): return cls.__dict__[x.upper()] from_string = classmethod(from_string) - + + class Format(EnumClass): - TEXT = 1 - ANSI = 2 - HTML = 3 + TEXT = 1 + ANSI = 2 + HTML = 3 LATEX = 4 XUNIT = 5 @@ -109,15 +125,19 @@ class Format(EnumClass): #### TEST CLASSES #### class TestClass: + def __getitem__(self, item): return getattr(self, item) + def add_keywords(self, kw): if kw is str: self.keywords.append(kw) else: self.keywords += kw + class TestCampaign(TestClass): + def __init__(self, title): self.title = title self.filename = None @@ -128,16 +148,21 @@ def __init__(self, title): self.sha = None self.preexec = None self.preexec_output = None + def add_testset(self, testset): self.campaign.append(testset) + def __iter__(self): return self.campaign.__iter__() + def all_tests(self): for ts in self: for t in ts: yield t + class TestSet(TestClass): + def __init__(self, name): self.name = name self.tests = [] @@ -145,12 +170,16 @@ def __init__(self, name): self.keywords = [] self.crc = None self.expand = 1 + def add_test(self, test): self.tests.append(test) + def __iter__(self): return self.tests.__iter__() + class UnitTest(TestClass): + def __init__(self, name): self.name = name self.test = "" @@ -162,6 +191,7 @@ def __init__(self, name): self.keywords = [] self.crc = None self.expand = 1 + def __nonzero__(self): return self.res @@ -170,7 +200,7 @@ def __nonzero__(self): def parse_campaign_file(campaign_file): test_campaign = TestCampaign("Test campaign") - test_campaign.filename= campaign_file.name + test_campaign.filename = campaign_file.name testset = None test = None testnb = 0 @@ -193,7 +223,7 @@ def parse_campaign_file(campaign_file): testset.add_test(test) elif l[0] == "*": if test is not None: - + test.comments += l[1:] elif testset is not None: testset.comments += l[1:] @@ -207,19 +237,20 @@ def parse_campaign_file(campaign_file): test.test += l return test_campaign + def dump_campaign(test_campaign): - print "#"*(len(test_campaign.title)+6) + print "#" * (len(test_campaign.title) + 6) print "## %(title)s ##" % test_campaign - print "#"*(len(test_campaign.title)+6) + print "#" * (len(test_campaign.title) + 6) if test_campaign.sha and test_campaign.crc: print "CRC=[%(crc)s] SHA=[%(sha)s]" % test_campaign print "from file %(filename)s" % test_campaign print for ts in test_campaign: if ts.crc: - print "+--[%s]%s(%s)--" % (ts.name,"-"*max(2,80-len(ts.name)-18),ts.crc) + print "+--[%s]%s(%s)--" % (ts.name, "-" * max(2, 80 - len(ts.name) - 18), ts.crc) else: - print "+--[%s]%s" % (ts.name,"-"*max(2,80-len(ts.name)-6)) + print "+--[%s]%s" % (ts.name, "-" * max(2, 80 - len(ts.name) - 6)) if ts.keywords: print " kw=%s" % ",".join(ts.keywords) for t in ts: @@ -230,16 +261,19 @@ def dump_campaign(test_campaign): if t.crc: c = "[%(crc)s] " % t if c or k: - print " %s%s" % (c,k) + print " %s%s" % (c, k) #### COMPUTE CAMPAIGN DIGESTS #### + def crc32(x): return "%08X" % (0xffffffffL & zlib.crc32(x)) + def sha1(x): return sha.sha(x).hexdigest().upper() + def compute_campaign_digests(test_campaign): dc = "" for ts in test_campaign: @@ -247,9 +281,9 @@ def compute_campaign_digests(test_campaign): for t in ts: dt = t.test.strip() t.crc = crc32(dt) - dts += "\0"+dt + dts += "\0" + dt ts.crc = crc32(dts) - dc += "\0\x01"+dts + dc += "\0\x01" + dts test_campaign.crc = crc32(dc) test_campaign.sha = sha1(open(test_campaign.filename).read()) @@ -263,24 +297,26 @@ def filter_tests_on_numbers(test_campaign, num): test_campaign.campaign = [ts for ts in test_campaign.campaign if ts.tests] + def filter_tests_keep_on_keywords(test_campaign, kw): def kw_match(lst, kw): for k in lst: if k in kw: return True return False - + if kw: for ts in test_campaign: ts.tests = [t for t in ts.tests if kw_match(t.keywords, kw)] + def filter_tests_remove_on_keywords(test_campaign, kw): def kw_match(lst, kw): for k in kw: if k not in lst: return False return True - + if kw: for ts in test_campaign: ts.tests = [t for t in ts.tests if not kw_match(t.keywords, kw)] @@ -293,19 +329,21 @@ def remove_empty_testsets(test_campaign): #### RUN CAMPAIGN ##### def run_campaign(test_campaign, get_interactive_session, verb=2): - passed=failed=0 + passed = failed = 0 if test_campaign.preexec: - test_campaign.preexec_output = get_interactive_session(test_campaign.preexec.strip())[0] + test_campaign.preexec_output = get_interactive_session( + test_campaign.preexec.strip())[0] for testset in test_campaign: for t in testset: - t.output,res = get_interactive_session(t.test.strip()) + t.output, res = get_interactive_session(t.test.strip()) the_res = False try: if res is None or res: - the_res= True - except Exception,msg: - t.output+="UTscapy: Error during result interpretation:\n" - t.output+="".join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback,)) + the_res = True + except Exception, msg: + t.output += "UTscapy: Error during result interpretation:\n" + t.output += "".join(traceback.format_exception(sys.exc_type, + sys.exc_value, sys.exc_traceback,)) if the_res: t.res = True res = "passed" @@ -316,12 +354,12 @@ def run_campaign(test_campaign, get_interactive_session, verb=2): failed += 1 t.result = res if verb > 1: - print >>sys.stderr,"%(result)6s %(crc)s %(name)s" % t + print >>sys.stderr, "%(result)6s %(crc)s %(name)s" % t test_campaign.passed = passed test_campaign.failed = failed if verb: - print >>sys.stderr,"Campaign CRC=%(crc)s SHA=%(sha)s" % test_campaign - print >>sys.stderr,"PASSED=%i FAILED=%i" % (passed, failed) + print >>sys.stderr, "Campaign CRC=%(crc)s SHA=%(sha)s" % test_campaign + print >>sys.stderr, "PASSED=%i FAILED=%i" % (passed, failed) return failed @@ -334,6 +372,7 @@ def info_line(test_campaign): else: return "Run %s from [%s] by UTscapy" % (time.ctime(), filename) + def html_info_line(test_campaign): filename = test_campaign.filename if filename is None: @@ -345,10 +384,10 @@ def html_info_line(test_campaign): #### CAMPAIGN TO something #### def campaign_to_TEXT(test_campaign): - output="%(title)s\n" % test_campaign - output += "-- "+info_line(test_campaign)+"\n\n" + output = "%(title)s\n" % test_campaign + output += "-- " + info_line(test_campaign) + "\n\n" output += "Passed=%(passed)i\nFailed=%(failed)i\n\n%(headcomments)s\n" % test_campaign - + for testset in test_campaign: if any(t.expand for t in testset): output += "######\n## %(name)s\n######\n%(comments)s\n\n" % testset @@ -357,12 +396,13 @@ def campaign_to_TEXT(test_campaign): output += "###(%(num)03i)=[%(result)s] %(name)s\n%(comments)s\n%(output)s\n\n" % t return output - + + def campaign_to_ANSI(test_campaign): - output="%(title)s\n" % test_campaign - output += "-- "+info_line(test_campaign)+"\n\n" + output = "%(title)s\n" % test_campaign + output += "-- " + info_line(test_campaign) + "\n\n" output += "Passed=%(passed)i\nFailed=%(failed)i\n\n%(headcomments)s\n" % test_campaign - + for testset in test_campaign: if any(t.expand for t in testset): output += "######\n## %(name)s\n######\n%(comments)s\n\n" % testset @@ -372,12 +412,15 @@ def campaign_to_ANSI(test_campaign): return output + def campaign_to_xUNIT(test_campaign): - output='\n\n' + output = '\n\n' for testset in test_campaign: for t in testset: - output += ' " - output += test_campaign.headcomments + "\n

PASSED=%(passed)i FAILED=%(failed)i

\n\n" % test_campaign + output += "" + html_info_line(test_campaign) + "" + output += test_campaign.headcomments + \ + "\n

PASSED=%(passed)i FAILED=%(failed)i

\n\n" % test_campaign for ts in test_campaign: for t in ts: output += """%(num)03i\n""" % t output += "\n\n" - + for testset in test_campaign: output += "

" % testset if testset.crc is not None: @@ -428,7 +474,7 @@ def campaign_to_HTML(test_campaign, local=0): for t in testset: output += """
  • \n""" % t if t.expand == 2: - output +=""" + output += """ -%(num)03i- """ % t @@ -451,6 +497,7 @@ def campaign_to_HTML(test_campaign, local=0): output += "" return output + def campaign_to_LATEX(test_campaign): output = r"""\documentclass{report} \usepackage{alltt} @@ -474,7 +521,7 @@ def campaign_to_LATEX(test_campaign): """ % test_campaign output %= info_line(test_campaign) - + for testset in test_campaign: output += "\\chapter{%(name)s}\n\n%(comments)s\n\n" % testset for t in testset: @@ -494,11 +541,10 @@ def campaign_to_LATEX(test_campaign): return output - #### USAGE #### - + def usage(): - print >>sys.stderr,"""Usage: UTscapy [-m module] [-f {text|ansi|HTML|LaTeX}] [-o output_file] + print >>sys.stderr, """Usage: UTscapy [-m module] [-f {text|ansi|HTML|LaTeX}] [-o output_file] [-t testfile] [-k keywords [-k ...]] [-K keywords [-K ...]] [-l] [-d|-D] [-F] [-q[q]] [-P preexecute_python_code] [-s /path/to/scpay] @@ -525,24 +571,24 @@ def main(argv): import __builtin__ # Parse arguments - + FORMAT = Format.ANSI TESTFILE = sys.stdin OUTPUTFILE = sys.stdout LOCAL = 0 - NUM=None + NUM = None KW_OK = [] KW_KO = [] DUMP = 0 CRC = 1 ONLYFAILED = 0 - VERB=2 - PREEXEC="" - SCAPY="scapy" + VERB = 2 + PREEXEC = "" + SCAPY = "scapy" MODULES = [] try: opts = getopt.getopt(argv, "o:t:f:hln:m:k:K:DdCFqP:s:") - for opt,optarg in opts[0]: + for opt, optarg in opts[0]: if opt == "-h": usage() elif opt == "-F": @@ -558,11 +604,11 @@ def main(argv): elif opt == "-s": SCAPY = optarg elif opt == "-P": - PREEXEC += "\n"+optarg + PREEXEC += "\n" + optarg elif opt == "-f": try: FORMAT = Format.from_string(optarg) - except KeyError,msg: + except KeyError, msg: raise getopt.GetoptError("Unknown output format %s" % msg) elif opt == "-t": TESTFILE = open(optarg) @@ -585,21 +631,20 @@ def main(argv): elif opt == "-K": KW_KO.append(optarg.split(",")) - try: from scapy import all as scapy - except ImportError,e: - raise getopt.GetoptError("cannot import [%s]: %s" % (SCAPY,e)) + except ImportError, e: + raise getopt.GetoptError("cannot import [%s]: %s" % (SCAPY, e)) for m in MODULES: try: mod = import_module(m) __builtin__.__dict__.update(mod.__dict__) - except ImportError,e: - raise getopt.GetoptError("cannot import [%s]: %s" % (m,e)) - - except getopt.GetoptError,msg: - print >>sys.stderr,"ERROR:",msg + except ImportError, e: + raise getopt.GetoptError("cannot import [%s]: %s" % (m, e)) + + except getopt.GetoptError, msg: + print >>sys.stderr, "ERROR:", msg raise SystemExit autorun_func = { @@ -608,7 +653,7 @@ def main(argv): Format.HTML: scapy.autorun_get_html_interactive_session, Format.LATEX: scapy.autorun_get_latex_interactive_session, Format.XUNIT: scapy.autorun_get_text_interactive_session, - } + } # Parse test file test_campaign = parse_campaign_file(TESTFILE) @@ -616,7 +661,6 @@ def main(argv): # Report parameters if PREEXEC: test_campaign.preexec = PREEXEC - # Compute campaign CRC and SHA if CRC: @@ -631,7 +675,6 @@ def main(argv): remove_empty_testsets(test_campaign) - # Dump campaign if DUMP: dump_campaign(test_campaign)