aboutsummaryrefslogtreecommitdiffstats
path: root/test/mitmproxy/test_utils.py
diff options
context:
space:
mode:
authorMaximilian Hils <git@maximilianhils.com>2016-02-15 23:00:11 +0100
committerMaximilian Hils <git@maximilianhils.com>2016-02-15 23:00:11 +0100
commit87d9afcf2e257eee7c5aa08c3f0dc64da79b0647 (patch)
tree71b10729d160f0269d02548d1ef9e183be1397d9 /test/mitmproxy/test_utils.py
parent36f34f701991b5d474c005ec45e3b66e20f326a8 (diff)
parent3d9a5157e77b5a3237dc62994f4e3d4c75c2066e (diff)
downloadmitmproxy-87d9afcf2e257eee7c5aa08c3f0dc64da79b0647.tar.gz
mitmproxy-87d9afcf2e257eee7c5aa08c3f0dc64da79b0647.tar.bz2
mitmproxy-87d9afcf2e257eee7c5aa08c3f0dc64da79b0647.zip
Merge pull request #937 from mhils/single-repo
Combine mitmproxy, pathod and netlib in a single repo.
Diffstat (limited to 'test/mitmproxy/test_utils.py')
-rw-r--r--test/mitmproxy/test_utils.py105
1 files changed, 105 insertions, 0 deletions
diff --git a/test/mitmproxy/test_utils.py b/test/mitmproxy/test_utils.py
new file mode 100644
index 00000000..17bf3dbf
--- /dev/null
+++ b/test/mitmproxy/test_utils.py
@@ -0,0 +1,105 @@
+import json
+from libmproxy import utils
+from . import tutils
+
+utils.CERT_SLEEP_TIME = 0
+
+
+def test_format_timestamp():
+ assert utils.format_timestamp(utils.timestamp())
+
+
+def test_format_timestamp_with_milli():
+ assert utils.format_timestamp_with_milli(utils.timestamp())
+
+
+def test_isBin():
+ assert not utils.isBin("testing\n\r")
+ assert utils.isBin("testing\x01")
+ assert utils.isBin("testing\x0e")
+ assert utils.isBin("testing\x7f")
+
+
+def test_isXml():
+ assert not utils.isXML("foo")
+ assert utils.isXML("<foo")
+ assert utils.isXML(" \n<foo")
+
+
+def test_clean_hanging_newline():
+ s = "foo\n"
+ assert utils.clean_hanging_newline(s) == "foo"
+ assert utils.clean_hanging_newline("foo") == "foo"
+
+
+def test_pkg_data():
+ assert utils.pkg_data.path("console")
+ tutils.raises("does not exist", utils.pkg_data.path, "nonexistent")
+
+
+def test_pretty_json():
+ s = json.dumps({"foo": 1})
+ assert utils.pretty_json(s)
+ assert not utils.pretty_json("moo")
+
+
+def test_pretty_duration():
+ assert utils.pretty_duration(0.00001) == "0ms"
+ assert utils.pretty_duration(0.0001) == "0ms"
+ assert utils.pretty_duration(0.001) == "1ms"
+ assert utils.pretty_duration(0.01) == "10ms"
+ assert utils.pretty_duration(0.1) == "100ms"
+ assert utils.pretty_duration(1) == "1.00s"
+ assert utils.pretty_duration(10) == "10.0s"
+ assert utils.pretty_duration(100) == "100s"
+ assert utils.pretty_duration(1000) == "1000s"
+ assert utils.pretty_duration(10000) == "10000s"
+ assert utils.pretty_duration(1.123) == "1.12s"
+ assert utils.pretty_duration(0.123) == "123ms"
+
+
+def test_LRUCache():
+ cache = utils.LRUCache(2)
+
+ class Foo:
+ ran = False
+
+ def gen(self, x):
+ self.ran = True
+ return x
+ f = Foo()
+
+ assert not f.ran
+ assert cache.get(f.gen, 1) == 1
+ assert f.ran
+ f.ran = False
+ assert cache.get(f.gen, 1) == 1
+ assert not f.ran
+
+ f.ran = False
+ assert cache.get(f.gen, 1) == 1
+ assert not f.ran
+ assert cache.get(f.gen, 2) == 2
+ assert cache.get(f.gen, 3) == 3
+ assert f.ran
+
+ f.ran = False
+ assert cache.get(f.gen, 1) == 1
+ assert f.ran
+
+ assert len(cache.cacheList) == 2
+ assert len(cache.cache) == 2
+
+
+def test_parse_size():
+ assert not utils.parse_size("")
+ assert utils.parse_size("1") == 1
+ assert utils.parse_size("1k") == 1024
+ assert utils.parse_size("1m") == 1024**2
+ assert utils.parse_size("1g") == 1024**3
+ tutils.raises(ValueError, utils.parse_size, "1f")
+ tutils.raises(ValueError, utils.parse_size, "ak")
+
+
+def test_safe_subn():
+ assert utils.safe_subn("foo", u"bar", "\xc2foo")