From b3ef12b743ebbd117158eab8349d405e2a6ede20 Mon Sep 17 00:00:00 2001 From: gpotter2 <gabriel@potter.fr> Date: Tue, 17 Jan 2017 22:37:30 +0100 Subject: [PATCH] Add main.py tests --- scapy/main.py | 96 +++++++++++++++++++++++---------------------- test/regression.uts | 13 ++++++ 2 files changed, 63 insertions(+), 46 deletions(-) diff --git a/scapy/main.py b/scapy/main.py index e788a2fc..6ed53826 100644 --- a/scapy/main.py +++ b/scapy/main.py @@ -166,6 +166,55 @@ def update_session(fname=None): scapy_session = __builtin__.__dict__["scapy_session"] scapy_session.update(s) +def init_session(mydict=None, session_name="", STARTUP_FILE=DEFAULT_STARTUP_FILE): + global session + scapy_builtins = __import__("all",globals(),locals(),".").__dict__ + for name, sym in scapy_builtins.iteritems(): + if name [0] != '_': + __builtin__.__dict__[name] = sym + globkeys = scapy_builtins.keys() + globkeys.append("scapy_session") + scapy_builtins=None # XXX replace with "with" statement + if mydict is not None: + __builtin__.__dict__.update(mydict) + globkeys += mydict.keys() + + + conf.color_theme = DefaultTheme() + if STARTUP_FILE: + _read_config_file(STARTUP_FILE) + + if session_name: + try: + os.stat(session_name) + except OSError: + log_loading.info("New session [%s]" % session_name) + else: + try: + try: + session = cPickle.load(gzip.open(session_name,"rb")) + except IOError: + session = cPickle.load(open(session_name,"rb")) + log_loading.info("Using session [%s]" % session_name) + except EOFError: + log_loading.error("Error opening session [%s]" % session_name) + except AttributeError: + log_loading.error("Error opening session [%s]. Attribute missing" % session_name) + + if session: + if "conf" in session: + conf.configure(session["conf"]) + session["conf"] = conf + else: + conf.session = session_name + session={"conf":conf} + + else: + session={"conf": conf} + + __builtin__.__dict__["scapy_session"] = session + + return globkeys ################ ##### Main ##### @@ -299,52 +348,7 @@ def interact(mydict=None,argv=None,mybanner=None,loglevel=20): if PRESTART_FILE: _read_config_file(PRESTART_FILE) - scapy_builtins = __import__("all",globals(),locals(),".").__dict__ - for name, sym in scapy_builtins.iteritems(): - if name [0] != '_': - __builtin__.__dict__[name] = sym - globkeys = scapy_builtins.keys() - globkeys.append("scapy_session") - scapy_builtins=None # XXX replace with "with" statement - if mydict is not None: - __builtin__.__dict__.update(mydict) - globkeys += mydict.keys() - - - conf.color_theme = DefaultTheme() - if STARTUP_FILE: - _read_config_file(STARTUP_FILE) - - if session_name: - try: - os.stat(session_name) - except OSError: - log_loading.info("New session [%s]" % session_name) - else: - try: - try: - session = cPickle.load(gzip.open(session_name,"rb")) - except IOError: - session = cPickle.load(open(session_name,"rb")) - log_loading.info("Using session [%s]" % session_name) - except EOFError: - log_loading.error("Error opening session [%s]" % session_name) - except AttributeError: - log_loading.error("Error opening session [%s]. Attribute missing" % session_name) - - if session: - if "conf" in session: - conf.configure(session["conf"]) - session["conf"] = conf - else: - conf.session = session_name - session={"conf":conf} - - else: - session={"conf": conf} - - __builtin__.__dict__["scapy_session"] = session - + globkeys = init_session(mydict, session_name, STARTUP_FILE) if READLINE: if conf.histfile: diff --git a/test/regression.uts b/test/regression.uts index 2ab4eeaf..e603351a 100644 --- a/test/regression.uts +++ b/test/regression.uts @@ -75,6 +75,19 @@ else: conf.route6.ifchange(LOOPBACK_NAME, "::1/128") True +############ +############ ++ Main.py tests + += Test save_session +init_session() +# TODO: Remove the comments once the pickling bug has been fixed +#test_value = IP(dst="192.168.0.10") +#test_value +save_session(fname="scapySession1") + += Test load_session +load_session(fname="scapySession1") ############ ############ -- GitLab