From 1a9bdf0db620c3ea6cdcf2ebd64e8b00edf46ecf Mon Sep 17 00:00:00 2001 From: gpotter2 Date: Thu, 11 Jan 2018 12:31:50 +0100 Subject: [PATCH] Python 3 tests (IPython + BytesIO) (#1031) --- test/mock_windows.uts | 32 +++++++++++++------------------- test/regression.uts | 17 +++++++++++------ 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/test/mock_windows.uts b/test/mock_windows.uts index 12086be8b..bd24a226c 100644 --- a/test/mock_windows.uts +++ b/test/mock_windows.uts @@ -167,21 +167,24 @@ test_read_routes6_windows() from scapy.config import conf assert conf.prog.powershell != None -= Test all VBS fallback += Store powershell results import mock from scapy.config import conf ps_ip = get_ip_from_name(conf.iface.name) +ps_ip ps_if_list = get_windows_if_list() +ps_if_list ps_read_routes = read_routes() +ps_read_routes # Turn on VBS mode conf.prog.powershell = None -# Test get_ip_from_name += Test get_ip_from_name with VBS assert get_ip_from_name(conf.iface.name) == ps_ip -# Test get_windows_if_list += Test get_windows_if_list with VBS def is_in_if_list(i, list): for j in list: @@ -190,6 +193,7 @@ def is_in_if_list(i, list): return False vbs_if_list = get_windows_if_list() +vbs_if_list _correct = True for i in ps_if_list: if not is_in_if_list(i, vbs_if_list): @@ -198,7 +202,7 @@ for i in ps_if_list: assert _correct -# Test read_routes += Test read_routes with VBS def is_in_route_list(i, list): for j in list: @@ -210,6 +214,7 @@ def is_in_route_list(i, list): return False vbs_read_routes = read_routes() +vbs_if_list _correct = True for i in ps_read_routes: if not is_in_route_list(i, vbs_read_routes): @@ -223,25 +228,14 @@ conf.prog._reload() = show_interfaces from scapy.arch import show_interfaces -from io import StringIO, BytesIO -storage = BytesIO if six.PY2 else StringIO - -@mock.patch('sys.stdout', new_callable=storage) -def test_show_interfaces(mock_stdout): +with ContextManagerCaptureOutput() as cmco: show_interfaces() - lines = mock_stdout.getvalue().split("\n")[1:] + lines = cmco.get_output().split("\n")[1:] for l in lines: if not l.strip(): continue - try: - int(l[:2]) - except: - sys.stdout.write(l) - return False - return True - -assert test_show_interfaces() + int(l[:2]) = dev_from_pcapname @@ -278,4 +272,4 @@ def _test_autostart_ui(mocked_getiflist): _test_autostart_ui() -IFACES.data = old_ifaces +IFACES.data = old_ifaces \ No newline at end of file diff --git a/test/regression.uts b/test/regression.uts index 37040f2fc..fff29abf7 100644 --- a/test/regression.uts +++ b/test/regression.uts @@ -213,25 +213,30 @@ assert(len(conf.temp_files) == 0) = Emulate interact() import mock, sys +from scapy.main import interact +# Detect IPython try: import IPython except: code_interact_import = "scapy.main.code.interact" else: - code_interact_import = "scapy.main.IPython.terminal.embed.InteractiveShellEmbed" + code_interact_import = "IPython.terminal.embed.InteractiveShellEmbed" @mock.patch(code_interact_import) -def interact_emulator(code_int): +def interact_emulator(code_int, extra_args=[]): try: - code_int = lambda *args, **kwargs: lambda: None - interact(argv=["-s scapy1"], mybanner="What a test") + code_int.side_effect = lambda *args, **kwargs: lambda *args, **kwargs: None + interact(argv=["-s scapy1"] + extra_args, mybanner="What a test") return True except: raise return False + finally: + sys.ps1 = ">>> " -assert interact_emulator() -sys.ps1 = ">>> " +assert interact_emulator() # Default +assert not interact_emulator(extra_args=["-?"]) # Failing +assert interact_emulator(extra_args=["-d"]) # Extended = Test sane function sane("A\x00\xFFB") == "A..B"