2012-08-17 22:27:31 +00:00
|
|
|
import argparse
|
2011-03-12 02:14:25 +00:00
|
|
|
from libmproxy import cmdline
|
2016-02-02 12:25:31 +00:00
|
|
|
from . import tutils
|
2012-06-09 01:42:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_parse_replace_hook():
|
|
|
|
x = cmdline.parse_replace_hook("/foo/bar/voing")
|
|
|
|
assert x == ("foo", "bar", "voing")
|
|
|
|
|
|
|
|
x = cmdline.parse_replace_hook("/foo/bar/vo/ing/")
|
|
|
|
assert x == ("foo", "bar", "vo/ing/")
|
|
|
|
|
|
|
|
x = cmdline.parse_replace_hook("/bar/voing")
|
|
|
|
assert x == (".*", "bar", "voing")
|
|
|
|
|
|
|
|
tutils.raises(
|
2012-08-18 12:14:16 +00:00
|
|
|
cmdline.ParseException,
|
2012-06-09 01:42:43 +00:00
|
|
|
cmdline.parse_replace_hook,
|
|
|
|
"/foo"
|
|
|
|
)
|
|
|
|
tutils.raises(
|
|
|
|
"replacement regex",
|
|
|
|
cmdline.parse_replace_hook,
|
|
|
|
"patt/[/rep"
|
|
|
|
)
|
|
|
|
tutils.raises(
|
|
|
|
"filter pattern",
|
|
|
|
cmdline.parse_replace_hook,
|
|
|
|
"/~/foo/rep"
|
|
|
|
)
|
|
|
|
tutils.raises(
|
2012-08-18 12:14:16 +00:00
|
|
|
"empty clause",
|
2012-06-09 01:42:43 +00:00
|
|
|
cmdline.parse_replace_hook,
|
|
|
|
"//"
|
|
|
|
)
|
|
|
|
|
2012-08-18 12:14:16 +00:00
|
|
|
|
2014-03-10 04:11:51 +00:00
|
|
|
def test_parse_server_spec():
|
|
|
|
tutils.raises("Invalid server specification", cmdline.parse_server_spec, "")
|
2015-05-30 00:03:28 +00:00
|
|
|
assert cmdline.parse_server_spec(
|
2015-08-27 23:51:13 +00:00
|
|
|
"http://foo.com:88") == ("http", ("foo.com", 88))
|
2015-05-30 00:03:28 +00:00
|
|
|
assert cmdline.parse_server_spec(
|
2015-08-27 23:51:13 +00:00
|
|
|
"http://foo.com") == ("http", ("foo.com", 80))
|
2015-05-30 00:03:28 +00:00
|
|
|
assert cmdline.parse_server_spec(
|
2015-08-27 23:51:13 +00:00
|
|
|
"https://foo.com") == ("https", ("foo.com", 443))
|
2015-05-30 00:03:28 +00:00
|
|
|
tutils.raises(
|
|
|
|
"Invalid server specification",
|
|
|
|
cmdline.parse_server_spec,
|
|
|
|
"foo.com")
|
|
|
|
tutils.raises(
|
|
|
|
"Invalid server specification",
|
|
|
|
cmdline.parse_server_spec,
|
|
|
|
"http://")
|
2014-03-10 04:11:51 +00:00
|
|
|
|
|
|
|
|
2012-08-18 12:14:16 +00:00
|
|
|
def test_parse_setheaders():
|
2012-08-18 12:22:42 +00:00
|
|
|
x = cmdline.parse_setheader("/foo/bar/voing")
|
2012-08-18 12:14:16 +00:00
|
|
|
assert x == ("foo", "bar", "voing")
|
|
|
|
|
2014-11-15 04:25:05 +00:00
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
def test_common():
|
2012-08-17 22:27:31 +00:00
|
|
|
parser = argparse.ArgumentParser()
|
2012-06-09 01:42:43 +00:00
|
|
|
cmdline.common_options(parser)
|
2012-08-17 22:27:31 +00:00
|
|
|
opts = parser.parse_args(args=[])
|
2012-06-09 01:42:43 +00:00
|
|
|
|
|
|
|
assert cmdline.get_common_options(opts)
|
|
|
|
|
|
|
|
opts.stickycookie_filt = "foo"
|
|
|
|
opts.stickyauth_filt = "foo"
|
|
|
|
v = cmdline.get_common_options(opts)
|
|
|
|
assert v["stickycookie"] == "foo"
|
|
|
|
assert v["stickyauth"] == "foo"
|
|
|
|
|
2012-08-18 12:22:42 +00:00
|
|
|
opts.setheader = ["/foo/bar/voing"]
|
|
|
|
v = cmdline.get_common_options(opts)
|
|
|
|
assert v["setheaders"] == [("foo", "bar", "voing")]
|
|
|
|
|
|
|
|
opts.setheader = ["//"]
|
|
|
|
tutils.raises(
|
|
|
|
"empty clause",
|
|
|
|
cmdline.get_common_options,
|
|
|
|
opts
|
|
|
|
)
|
|
|
|
opts.setheader = []
|
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
opts.replace = ["/foo/bar/voing"]
|
|
|
|
v = cmdline.get_common_options(opts)
|
|
|
|
assert v["replacements"] == [("foo", "bar", "voing")]
|
|
|
|
|
|
|
|
opts.replace = ["//"]
|
|
|
|
tutils.raises(
|
2012-08-18 12:14:16 +00:00
|
|
|
"empty clause",
|
2012-06-09 01:42:43 +00:00
|
|
|
cmdline.get_common_options,
|
|
|
|
opts
|
|
|
|
)
|
|
|
|
|
|
|
|
opts.replace = []
|
|
|
|
opts.replace_file = [("/foo/bar/nonexistent")]
|
|
|
|
tutils.raises(
|
|
|
|
"could not read replace file",
|
|
|
|
cmdline.get_common_options,
|
|
|
|
opts
|
|
|
|
)
|
|
|
|
|
|
|
|
opts.replace_file = [("/~/bar/nonexistent")]
|
|
|
|
tutils.raises(
|
|
|
|
"filter pattern",
|
|
|
|
cmdline.get_common_options,
|
|
|
|
opts
|
|
|
|
)
|
|
|
|
|
|
|
|
p = tutils.test_data.path("data/replace")
|
2015-05-30 00:03:28 +00:00
|
|
|
opts.replace_file = [("/foo/bar/%s" % p)]
|
2012-06-09 01:42:43 +00:00
|
|
|
v = cmdline.get_common_options(opts)["replacements"]
|
|
|
|
assert len(v) == 1
|
|
|
|
assert v[0][2].strip() == "replacecontents"
|
2011-03-12 02:14:25 +00:00
|
|
|
|
2014-11-15 04:25:05 +00:00
|
|
|
|
|
|
|
def test_mitmproxy():
|
|
|
|
ap = cmdline.mitmproxy()
|
|
|
|
assert ap
|
|
|
|
|
|
|
|
|
|
|
|
def test_mitmdump():
|
|
|
|
ap = cmdline.mitmdump()
|
|
|
|
assert ap
|
|
|
|
|
|
|
|
|
|
|
|
def test_mitmweb():
|
|
|
|
ap = cmdline.mitmweb()
|
|
|
|
assert ap
|