From b1bdae3d1cbcf7c4c427cac5163faf75150f4cff Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Tue, 25 Oct 2016 20:45:48 -0700 Subject: typecheck options, fix current inconsistencies --- test/mitmproxy/test_utils_data.py | 7 --- test/mitmproxy/test_utils_debug.py | 23 ------- test/mitmproxy/test_utils_human.py | 46 -------------- test/mitmproxy/test_utils_strutils.py | 96 ------------------------------ test/mitmproxy/test_utils_version_check.py | 25 -------- test/mitmproxy/utils/__init__.py | 0 test/mitmproxy/utils/test_data.py | 8 +++ test/mitmproxy/utils/test_debug.py | 23 +++++++ test/mitmproxy/utils/test_human.py | 46 ++++++++++++++ test/mitmproxy/utils/test_strutils.py | 96 ++++++++++++++++++++++++++++++ test/mitmproxy/utils/test_typecheck.py | 48 +++++++++++++++ test/mitmproxy/utils/test_version_check.py | 25 ++++++++ 12 files changed, 246 insertions(+), 197 deletions(-) delete mode 100644 test/mitmproxy/test_utils_data.py delete mode 100644 test/mitmproxy/test_utils_debug.py delete mode 100644 test/mitmproxy/test_utils_human.py delete mode 100644 test/mitmproxy/test_utils_strutils.py delete mode 100644 test/mitmproxy/test_utils_version_check.py create mode 100644 test/mitmproxy/utils/__init__.py create mode 100644 test/mitmproxy/utils/test_data.py create mode 100644 test/mitmproxy/utils/test_debug.py create mode 100644 test/mitmproxy/utils/test_human.py create mode 100644 test/mitmproxy/utils/test_strutils.py create mode 100644 test/mitmproxy/utils/test_typecheck.py create mode 100644 test/mitmproxy/utils/test_version_check.py (limited to 'test') diff --git a/test/mitmproxy/test_utils_data.py b/test/mitmproxy/test_utils_data.py deleted file mode 100644 index c6e4420e..00000000 --- a/test/mitmproxy/test_utils_data.py +++ /dev/null @@ -1,7 +0,0 @@ -from mitmproxy.utils import data -from . import tutils - - -def test_pkg_data(): - assert data.pkg_data.path("tools/console") - tutils.raises("does not exist", data.pkg_data.path, "nonexistent") diff --git a/test/mitmproxy/test_utils_debug.py b/test/mitmproxy/test_utils_debug.py deleted file mode 100644 index 9acf8192..00000000 --- a/test/mitmproxy/test_utils_debug.py +++ /dev/null @@ -1,23 +0,0 @@ -import io - -from mitmproxy.utils import debug - - -def test_dump_info(): - cs = io.StringIO() - debug.dump_info(None, None, file=cs, testing=True) - assert cs.getvalue() - - -def test_dump_stacks(): - cs = io.StringIO() - debug.dump_stacks(None, None, file=cs, testing=True) - assert cs.getvalue() - - -def test_sysinfo(): - assert debug.sysinfo() - - -def test_register_info_dumpers(): - debug.register_info_dumpers() diff --git a/test/mitmproxy/test_utils_human.py b/test/mitmproxy/test_utils_human.py deleted file mode 100644 index 443c8f66..00000000 --- a/test/mitmproxy/test_utils_human.py +++ /dev/null @@ -1,46 +0,0 @@ -import time -from mitmproxy.utils import human -from mitmproxy.test import tutils - - -def test_format_timestamp(): - assert human.format_timestamp(time.time()) - - -def test_format_timestamp_with_milli(): - assert human.format_timestamp_with_milli(time.time()) - - -def test_parse_size(): - assert human.parse_size("0") == 0 - assert human.parse_size("0b") == 0 - assert human.parse_size("1") == 1 - assert human.parse_size("1k") == 1024 - assert human.parse_size("1m") == 1024**2 - assert human.parse_size("1g") == 1024**3 - tutils.raises(ValueError, human.parse_size, "1f") - tutils.raises(ValueError, human.parse_size, "ak") - - -def test_pretty_size(): - assert human.pretty_size(0) == "0b" - assert human.pretty_size(100) == "100b" - assert human.pretty_size(1024) == "1k" - assert human.pretty_size(1024 + (1024 / 2.0)) == "1.5k" - assert human.pretty_size(1024 * 1024) == "1m" - assert human.pretty_size(10 * 1024 * 1024) == "10m" - - -def test_pretty_duration(): - assert human.pretty_duration(0.00001) == "0ms" - assert human.pretty_duration(0.0001) == "0ms" - assert human.pretty_duration(0.001) == "1ms" - assert human.pretty_duration(0.01) == "10ms" - assert human.pretty_duration(0.1) == "100ms" - assert human.pretty_duration(1) == "1.00s" - assert human.pretty_duration(10) == "10.0s" - assert human.pretty_duration(100) == "100s" - assert human.pretty_duration(1000) == "1000s" - assert human.pretty_duration(10000) == "10000s" - assert human.pretty_duration(1.123) == "1.12s" - assert human.pretty_duration(0.123) == "123ms" diff --git a/test/mitmproxy/test_utils_strutils.py b/test/mitmproxy/test_utils_strutils.py deleted file mode 100644 index 84281c6b..00000000 --- a/test/mitmproxy/test_utils_strutils.py +++ /dev/null @@ -1,96 +0,0 @@ -from mitmproxy.utils import strutils -from mitmproxy.test import tutils - - -def test_always_bytes(): - assert strutils.always_bytes(bytes(range(256))) == bytes(range(256)) - assert strutils.always_bytes("foo") == b"foo" - with tutils.raises(ValueError): - strutils.always_bytes(u"\u2605", "ascii") - with tutils.raises(TypeError): - strutils.always_bytes(42, "ascii") - - -def test_native(): - with tutils.raises(TypeError): - strutils.native(42) - assert strutils.native(u"foo") == u"foo" - assert strutils.native(b"foo") == u"foo" - - -def test_escape_control_characters(): - assert strutils.escape_control_characters(u"one") == u"one" - assert strutils.escape_control_characters(u"\00ne") == u".ne" - assert strutils.escape_control_characters(u"\nne") == u"\nne" - assert strutils.escape_control_characters(u"\nne", False) == u".ne" - assert strutils.escape_control_characters(u"\u2605") == u"\u2605" - assert ( - strutils.escape_control_characters(bytes(bytearray(range(128))).decode()) == - u'.........\t\n..\r.................. !"#$%&\'()*+,-./0123456789:;<' - u'=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~.' - ) - assert ( - strutils.escape_control_characters(bytes(bytearray(range(128))).decode(), False) == - u'................................ !"#$%&\'()*+,-./0123456789:;<' - u'=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~.' - ) - - with tutils.raises(ValueError): - strutils.escape_control_characters(b"foo") - - -def test_bytes_to_escaped_str(): - assert strutils.bytes_to_escaped_str(b"foo") == "foo" - assert strutils.bytes_to_escaped_str(b"\b") == r"\x08" - assert strutils.bytes_to_escaped_str(br"&!?=\)") == r"&!?=\\)" - assert strutils.bytes_to_escaped_str(b'\xc3\xbc') == r"\xc3\xbc" - assert strutils.bytes_to_escaped_str(b"'") == r"'" - assert strutils.bytes_to_escaped_str(b'"') == r'"' - - assert strutils.bytes_to_escaped_str(b"'", escape_single_quotes=True) == r"\'" - assert strutils.bytes_to_escaped_str(b'"', escape_single_quotes=True) == r'"' - - assert strutils.bytes_to_escaped_str(b"\r\n\t") == "\\r\\n\\t" - assert strutils.bytes_to_escaped_str(b"\r\n\t", True) == "\r\n\t" - - assert strutils.bytes_to_escaped_str(b"\n", True) == "\n" - assert strutils.bytes_to_escaped_str(b"\\n", True) == "\\ \\ n".replace(" ", "") - assert strutils.bytes_to_escaped_str(b"\\\n", True) == "\\ \\ \n".replace(" ", "") - assert strutils.bytes_to_escaped_str(b"\\\\n", True) == "\\ \\ \\ \\ n".replace(" ", "") - - with tutils.raises(ValueError): - strutils.bytes_to_escaped_str(u"such unicode") - - -def test_escaped_str_to_bytes(): - assert strutils.escaped_str_to_bytes("foo") == b"foo" - assert strutils.escaped_str_to_bytes("\x08") == b"\b" - assert strutils.escaped_str_to_bytes("&!?=\\\\)") == br"&!?=\)" - assert strutils.escaped_str_to_bytes(u"\\x08") == b"\b" - assert strutils.escaped_str_to_bytes(u"&!?=\\\\)") == br"&!?=\)" - assert strutils.escaped_str_to_bytes(u"\u00fc") == b'\xc3\xbc' - - with tutils.raises(ValueError): - strutils.escaped_str_to_bytes(b"very byte") - - -def test_is_mostly_bin(): - assert not strutils.is_mostly_bin(b"foo\xFF") - assert strutils.is_mostly_bin(b"foo" + b"\xFF" * 10) - assert not strutils.is_mostly_bin("") - - -def test_is_xml(): - assert not strutils.is_xml(b"foo") - assert strutils.is_xml(b"?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~.' + ) + assert ( + strutils.escape_control_characters(bytes(bytearray(range(128))).decode(), False) == + u'................................ !"#$%&\'()*+,-./0123456789:;<' + u'=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~.' + ) + + with tutils.raises(ValueError): + strutils.escape_control_characters(b"foo") + + +def test_bytes_to_escaped_str(): + assert strutils.bytes_to_escaped_str(b"foo") == "foo" + assert strutils.bytes_to_escaped_str(b"\b") == r"\x08" + assert strutils.bytes_to_escaped_str(br"&!?=\)") == r"&!?=\\)" + assert strutils.bytes_to_escaped_str(b'\xc3\xbc') == r"\xc3\xbc" + assert strutils.bytes_to_escaped_str(b"'") == r"'" + assert strutils.bytes_to_escaped_str(b'"') == r'"' + + assert strutils.bytes_to_escaped_str(b"'", escape_single_quotes=True) == r"\'" + assert strutils.bytes_to_escaped_str(b'"', escape_single_quotes=True) == r'"' + + assert strutils.bytes_to_escaped_str(b"\r\n\t") == "\\r\\n\\t" + assert strutils.bytes_to_escaped_str(b"\r\n\t", True) == "\r\n\t" + + assert strutils.bytes_to_escaped_str(b"\n", True) == "\n" + assert strutils.bytes_to_escaped_str(b"\\n", True) == "\\ \\ n".replace(" ", "") + assert strutils.bytes_to_escaped_str(b"\\\n", True) == "\\ \\ \n".replace(" ", "") + assert strutils.bytes_to_escaped_str(b"\\\\n", True) == "\\ \\ \\ \\ n".replace(" ", "") + + with tutils.raises(ValueError): + strutils.bytes_to_escaped_str(u"such unicode") + + +def test_escaped_str_to_bytes(): + assert strutils.escaped_str_to_bytes("foo") == b"foo" + assert strutils.escaped_str_to_bytes("\x08") == b"\b" + assert strutils.escaped_str_to_bytes("&!?=\\\\)") == br"&!?=\)" + assert strutils.escaped_str_to_bytes(u"\\x08") == b"\b" + assert strutils.escaped_str_to_bytes(u"&!?=\\\\)") == br"&!?=\)" + assert strutils.escaped_str_to_bytes(u"\u00fc") == b'\xc3\xbc' + + with tutils.raises(ValueError): + strutils.escaped_str_to_bytes(b"very byte") + + +def test_is_mostly_bin(): + assert not strutils.is_mostly_bin(b"foo\xFF") + assert strutils.is_mostly_bin(b"foo" + b"\xFF" * 10) + assert not strutils.is_mostly_bin("") + + +def test_is_xml(): + assert not strutils.is_xml(b"foo") + assert strutils.is_xml(b"