mitmproxy/test/test_utils.py

238 lines
6.4 KiB
Python
Raw Normal View History

import textwrap, cStringIO, os, time, re, json
2010-02-16 04:09:07 +00:00
import libpry
from libmproxy import utils
utils.CERT_SLEEP_TIME = 0
2010-02-16 04:09:07 +00:00
class uformat_timestamp(libpry.AutoTree):
def test_simple(self):
assert utils.format_timestamp(utils.timestamp())
2010-02-16 04:09:07 +00:00
class uisBin(libpry.AutoTree):
def test_simple(self):
assert not utils.isBin("testing\n\r")
assert utils.isBin("testing\x01")
assert utils.isBin("testing\x0e")
assert utils.isBin("testing\x7f")
class uisXML(libpry.AutoTree):
def test_simple(self):
assert not utils.isXML("foo")
assert utils.isXML("<foo")
assert utils.isXML(" \n<foo")
2010-02-16 04:09:07 +00:00
class uhexdump(libpry.AutoTree):
def test_simple(self):
assert utils.hexdump("one\0"*10)
2011-08-02 04:52:47 +00:00
class udel_all(libpry.AutoTree):
def test_simple(self):
d = dict(a=1, b=2, c=3)
utils.del_all(d, ["a", "x", "b"])
assert d.keys() == ["c"]
2010-02-16 04:09:07 +00:00
class upretty_size(libpry.AutoTree):
def test_simple(self):
assert utils.pretty_size(100) == "100B"
assert utils.pretty_size(1024) == "1kB"
2011-08-03 22:34:34 +00:00
assert utils.pretty_size(1024 + (1024/2.0)) == "1.5kB"
2010-02-16 04:09:07 +00:00
assert utils.pretty_size(1024*1024) == "1M"
class uData(libpry.AutoTree):
def test_nonexistent(self):
libpry.raises("does not exist", utils.pkg_data.path, "nonexistent")
2010-02-16 04:09:07 +00:00
class upretty_xmlish(libpry.AutoTree):
def test_tagre(self):
def f(s):
return re.search(utils.TAG, s, re.VERBOSE|re.MULTILINE)
assert f(r"<body>")
assert f(r"<body/>")
assert f(r"< body/>")
assert f(r"< body/ >")
assert f(r"< body / >")
assert f(r"<foo a=b>")
assert f(r"<foo a='b'>")
assert f(r"<foo a='b\"'>")
assert f(r'<a b=(a.b) href="foo">')
assert f('<td width=25%>')
2011-02-06 03:56:13 +00:00
assert f('<form name="search" action="/search.php" method="get" accept-charset="utf-8" class="search">')
assert f('<img src="gif" width="125" height="16" alt=&quot;&quot; />')
def test_all(self):
def isbalanced(ret):
# The last tag should have no indent
assert ret[-1].strip() == ret[-1]
s = "<html><br><br></br><p>one</p></html>"
ret = utils.pretty_xmlish(s)
isbalanced(ret)
s = r"""
<body bgcolor=#ffffff text=#000000 link=#0000cc vlink=#551a8b alink=#ff0000 onload="document.f.q.focus();if(document.images)new Image().src='/images/srpr/nav_logo27.png'" ><textarea id=csi style=display:none></textarea></body>
"""
isbalanced(utils.pretty_xmlish(textwrap.dedent(s)))
s = r"""
<a href="http://foo.com" target="">
<img src="http://foo.gif" alt="bar" height="25" width="132">
</a>
"""
isbalanced(utils.pretty_xmlish(textwrap.dedent(s)))
s = r"""
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html></html>
"""
ret = utils.pretty_xmlish(textwrap.dedent(s))
isbalanced(ret)
s = "<html><br/><p>one</p></html>"
ret = utils.pretty_xmlish(s)
assert len(ret) == 6
isbalanced(ret)
s = "gobbledygook"
2011-02-06 03:56:13 +00:00
assert utils.pretty_xmlish(s) == ["gobbledygook"]
class upretty_json(libpry.AutoTree):
def test_one(self):
s = json.dumps({"foo": 1})
assert utils.pretty_json(s)
assert not utils.pretty_json("moo")
class u_urldecode(libpry.AutoTree):
def test_one(self):
s = "one=two&three=four"
assert len(utils.urldecode(s)) == 2
class udummy_ca(libpry.AutoTree):
def test_all(self):
d = self.tmpdir()
path = os.path.join(d, "foo/cert.cnf")
assert utils.dummy_ca(path)
assert os.path.exists(path)
path = os.path.join(d, "foo/cert2.pem")
assert utils.dummy_ca(path)
assert os.path.exists(path)
assert os.path.exists(os.path.join(d, "foo/cert2-cert.pem"))
assert os.path.exists(os.path.join(d, "foo/cert2-cert.p12"))
class udummy_cert(libpry.AutoTree):
def test_with_ca(self):
d = self.tmpdir()
cacert = os.path.join(d, "foo/cert.cnf")
assert utils.dummy_ca(cacert)
assert utils.dummy_cert(
os.path.join(d, "foo"),
cacert,
"foo.com"
)
assert os.path.exists(os.path.join(d, "foo", "foo.com.pem"))
# Short-circuit
assert utils.dummy_cert(
os.path.join(d, "foo"),
cacert,
"foo.com"
)
def test_no_ca(self):
d = self.tmpdir()
assert utils.dummy_cert(
d,
None,
"foo.com"
)
assert os.path.exists(os.path.join(d, "foo.com.pem"))
class uLRUCache(libpry.AutoTree):
def test_one(self):
class Foo:
ran = False
@utils.LRUCache(2)
def one(self, x):
self.ran = True
return x
f = Foo()
assert f.one(1) == 1
assert f.ran
f.ran = False
assert f.one(1) == 1
assert not f.ran
f.ran = False
assert f.one(1) == 1
assert not f.ran
assert f.one(2) == 2
assert f.one(3) == 3
assert f.ran
f.ran = False
assert f.one(1) == 1
assert f.ran
assert len(f._cached_one) == 2
assert len(f._cachelist_one) == 2
class u_parse_url(libpry.AutoTree):
def test_simple(self):
assert not utils.parse_url("")
u = "http://foo.com:8888/test"
s, h, po, pa = utils.parse_url(u)
assert s == "http"
assert h == "foo.com"
assert po == 8888
assert pa == "/test"
s, h, po, pa = utils.parse_url("http://foo/bar")
assert s == "http"
assert h == "foo"
assert po == 80
assert pa == "/bar"
s, h, po, pa = utils.parse_url("http://foo")
assert pa == "/"
s, h, po, pa = utils.parse_url("https://foo")
assert po == 443
assert not utils.parse_url("https://foo:bar")
assert not utils.parse_url("https://foo:")
2010-02-16 04:09:07 +00:00
tests = [
uformat_timestamp(),
2010-02-16 04:09:07 +00:00
uisBin(),
uisXML(),
2010-02-16 04:09:07 +00:00
uhexdump(),
upretty_size(),
uData(),
upretty_xmlish(),
upretty_json(),
u_urldecode(),
2011-08-02 04:52:47 +00:00
udel_all(),
udummy_ca(),
udummy_cert(),
uLRUCache(),
u_parse_url()
2010-02-16 04:09:07 +00:00
]