2012-03-01 15:20:34 +00:00
|
|
|
import textwrap, re, json
|
2010-02-16 04:09:07 +00:00
|
|
|
import libpry
|
|
|
|
from libmproxy import utils
|
|
|
|
|
2011-06-11 03:16:16 +00:00
|
|
|
utils.CERT_SLEEP_TIME = 0
|
|
|
|
|
2010-02-16 04:09:07 +00:00
|
|
|
|
2011-02-03 00:30:47 +00:00
|
|
|
class uformat_timestamp(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
2011-03-07 00:46:02 +00:00
|
|
|
assert utils.format_timestamp(utils.timestamp())
|
2011-02-03 00:30:47 +00:00
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
2011-06-27 03:59:17 +00:00
|
|
|
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)
|
|
|
|
|
2012-02-10 02:04:20 +00:00
|
|
|
|
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
|
|
|
|
2012-02-10 02:04:20 +00:00
|
|
|
class uclean_hanging_newline(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
|
|
|
s = "foo\n"
|
|
|
|
assert utils.clean_hanging_newline(s) == "foo"
|
|
|
|
assert utils.clean_hanging_newline("foo") == "foo"
|
|
|
|
|
|
|
|
|
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):
|
2012-03-11 22:25:50 +00:00
|
|
|
assert utils.pkg_data.path("console")
|
2011-08-02 04:14:33 +00:00
|
|
|
libpry.raises("does not exist", utils.pkg_data.path, "nonexistent")
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
|
2011-06-30 01:27:27 +00:00
|
|
|
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")
|
|
|
|
|
|
|
|
|
2011-07-15 04:16:43 +00:00
|
|
|
class u_urldecode(libpry.AutoTree):
|
|
|
|
def test_one(self):
|
|
|
|
s = "one=two&three=four"
|
|
|
|
assert len(utils.urldecode(s)) == 2
|
|
|
|
|
|
|
|
|
2011-03-15 00:05:33 +00:00
|
|
|
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
|
|
|
|
|
2011-01-27 23:07:27 +00:00
|
|
|
|
2012-02-18 01:45:22 +00:00
|
|
|
class u_parse_proxy_spec(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
|
|
|
assert not utils.parse_proxy_spec("")
|
|
|
|
assert utils.parse_proxy_spec("http://foo.com:88") == ("http", "foo.com", 88)
|
|
|
|
assert utils.parse_proxy_spec("http://foo.com") == ("http", "foo.com", 80)
|
2012-02-19 22:04:07 +00:00
|
|
|
assert not utils.parse_proxy_spec("foo.com")
|
|
|
|
assert not utils.parse_proxy_spec("http://")
|
2012-02-18 01:45:22 +00:00
|
|
|
|
2011-08-03 10:38:23 +00:00
|
|
|
|
2012-02-18 03:27:09 +00:00
|
|
|
class u_unparse_url(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
|
|
|
assert utils.unparse_url("http", "foo.com", 99, "") == "http://foo.com:99"
|
|
|
|
assert utils.unparse_url("http", "foo.com", 80, "") == "http://foo.com"
|
|
|
|
assert utils.unparse_url("https", "foo.com", 80, "") == "https://foo.com:80"
|
|
|
|
assert utils.unparse_url("https", "foo.com", 443, "") == "https://foo.com"
|
|
|
|
|
|
|
|
|
2011-08-03 10:38:23 +00:00
|
|
|
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
|
|
|
|
|
2011-09-04 19:47:47 +00:00
|
|
|
assert not utils.parse_url("https://foo:bar")
|
|
|
|
assert not utils.parse_url("https://foo:")
|
|
|
|
|
2011-09-09 03:27:31 +00:00
|
|
|
|
|
|
|
class u_parse_size(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
2012-02-10 02:04:20 +00:00
|
|
|
assert not utils.parse_size("")
|
2011-09-09 03:27:31 +00:00
|
|
|
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
|
|
|
|
libpry.raises(ValueError, utils.parse_size, "1f")
|
|
|
|
libpry.raises(ValueError, utils.parse_size, "ak")
|
|
|
|
|
|
|
|
|
2012-03-19 21:31:07 +00:00
|
|
|
class u_parse_content_type(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
|
|
|
p = utils.parse_content_type
|
|
|
|
assert p("text/html") == ("text", "html", {})
|
|
|
|
assert p("text") == None
|
|
|
|
|
|
|
|
v = p("text/html; charset=UTF-8")
|
|
|
|
assert v == ('text', 'html', {'charset': 'UTF-8'})
|
|
|
|
|
|
|
|
|
2012-03-26 22:25:50 +00:00
|
|
|
class u_cleanBin(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
|
|
|
assert utils.cleanBin("one") == "one"
|
|
|
|
assert utils.cleanBin("\00ne") == ".ne"
|
|
|
|
assert utils.cleanBin("\nne") == "\nne"
|
|
|
|
assert utils.cleanBin("\nne", True) == ".ne"
|
|
|
|
|
|
|
|
|
2012-05-26 01:10:31 +00:00
|
|
|
class u_safe_subn(libpry.AutoTree):
|
|
|
|
def test_simple(self):
|
|
|
|
assert utils.safe_subn("foo", u"bar", "\xc2foo")
|
|
|
|
|
2012-03-26 22:25:50 +00:00
|
|
|
|
2010-02-16 04:09:07 +00:00
|
|
|
tests = [
|
2012-05-26 01:10:31 +00:00
|
|
|
u_safe_subn(),
|
2012-03-26 22:25:50 +00:00
|
|
|
u_cleanBin(),
|
2012-03-19 21:31:07 +00:00
|
|
|
u_parse_content_type(),
|
2011-02-03 00:30:47 +00:00
|
|
|
uformat_timestamp(),
|
2010-02-16 04:09:07 +00:00
|
|
|
uisBin(),
|
2011-06-27 03:59:17 +00:00
|
|
|
uisXML(),
|
2010-02-16 04:09:07 +00:00
|
|
|
uhexdump(),
|
|
|
|
upretty_size(),
|
|
|
|
uData(),
|
2011-06-30 01:27:27 +00:00
|
|
|
upretty_json(),
|
2011-07-15 04:16:43 +00:00
|
|
|
u_urldecode(),
|
2011-08-02 04:52:47 +00:00
|
|
|
udel_all(),
|
2011-03-15 00:05:33 +00:00
|
|
|
uLRUCache(),
|
2011-09-09 03:27:31 +00:00
|
|
|
u_parse_url(),
|
2012-02-18 01:45:22 +00:00
|
|
|
u_parse_proxy_spec(),
|
2012-02-18 03:27:09 +00:00
|
|
|
u_unparse_url(),
|
2012-02-10 02:04:20 +00:00
|
|
|
u_parse_size(),
|
|
|
|
uclean_hanging_newline()
|
2010-02-16 04:09:07 +00:00
|
|
|
]
|