2011-02-16 21:18:38 +00:00
|
|
|
import os
|
2011-02-16 09:10:24 +00:00
|
|
|
from cStringIO import StringIO
|
|
|
|
import libpry
|
2011-07-23 00:57:54 +00:00
|
|
|
from libmproxy import dump, flow, proxy
|
2011-03-05 02:58:48 +00:00
|
|
|
import tutils
|
2011-02-16 09:10:24 +00:00
|
|
|
|
2011-02-25 08:11:44 +00:00
|
|
|
class uStrFuncs(libpry.AutoTree):
|
|
|
|
def test_all(self):
|
2011-03-05 02:58:48 +00:00
|
|
|
t = tutils.tresp()
|
2011-02-25 08:11:44 +00:00
|
|
|
t.set_replay()
|
|
|
|
dump.str_response(t)
|
|
|
|
|
2011-03-05 02:58:48 +00:00
|
|
|
t = tutils.treq()
|
2011-03-06 03:02:28 +00:00
|
|
|
t.client_conn = None
|
2011-02-25 08:23:44 +00:00
|
|
|
t.stickycookie = True
|
|
|
|
assert "stickycookie" in dump.str_request(t)
|
2011-03-06 03:02:28 +00:00
|
|
|
assert "replay" in dump.str_request(t)
|
2011-02-25 08:23:44 +00:00
|
|
|
|
2011-02-16 09:10:24 +00:00
|
|
|
|
|
|
|
class uDumpMaster(libpry.AutoTree):
|
2011-02-19 20:55:42 +00:00
|
|
|
def _cycle(self, m, content):
|
2011-03-05 02:58:48 +00:00
|
|
|
req = tutils.treq()
|
2011-02-19 20:55:42 +00:00
|
|
|
req.content = content
|
2011-02-16 09:10:24 +00:00
|
|
|
cc = req.client_conn
|
2011-03-05 02:58:48 +00:00
|
|
|
resp = tutils.tresp(req)
|
2011-02-19 20:55:42 +00:00
|
|
|
resp.content = content
|
2011-02-19 04:00:24 +00:00
|
|
|
m.handle_clientconnect(cc)
|
2011-02-16 09:10:24 +00:00
|
|
|
m.handle_request(req)
|
|
|
|
m.handle_response(resp)
|
2011-07-23 00:57:54 +00:00
|
|
|
m.handle_clientdisconnect(proxy.ClientDisconnect(cc))
|
2011-02-16 09:10:24 +00:00
|
|
|
|
2011-02-19 20:55:42 +00:00
|
|
|
def _dummy_cycle(self, filt, content, **options):
|
2011-02-17 23:40:45 +00:00
|
|
|
cs = StringIO()
|
|
|
|
o = dump.Options(**options)
|
|
|
|
m = dump.DumpMaster(None, o, filt, outfile=cs)
|
2011-02-19 20:55:42 +00:00
|
|
|
self._cycle(m, content)
|
2011-02-17 23:40:45 +00:00
|
|
|
return cs.getvalue()
|
|
|
|
|
2011-02-20 20:54:39 +00:00
|
|
|
def test_replay(self):
|
|
|
|
cs = StringIO()
|
|
|
|
|
2011-03-05 00:03:26 +00:00
|
|
|
o = dump.Options(server_replay="nonexistent", kill=True)
|
2011-02-20 20:54:39 +00:00
|
|
|
libpry.raises(dump.DumpError, dump.DumpMaster, None, o, None, outfile=cs)
|
|
|
|
|
|
|
|
t = self.tmpdir()
|
|
|
|
p = os.path.join(t, "rep")
|
|
|
|
f = open(p, "w")
|
|
|
|
fw = flow.FlowWriter(f)
|
2011-03-05 02:58:48 +00:00
|
|
|
t = tutils.tflow_full()
|
|
|
|
t.response = tutils.tresp(t.request)
|
2011-02-20 20:54:39 +00:00
|
|
|
fw.add(t)
|
|
|
|
f.close()
|
|
|
|
|
2011-03-05 00:03:26 +00:00
|
|
|
o = dump.Options(server_replay=p, kill=True)
|
2011-02-20 20:54:39 +00:00
|
|
|
m = dump.DumpMaster(None, o, None, outfile=cs)
|
2011-02-20 22:08:35 +00:00
|
|
|
|
|
|
|
self._cycle(m, "content")
|
|
|
|
self._cycle(m, "content")
|
|
|
|
|
2011-03-05 00:03:26 +00:00
|
|
|
o = dump.Options(server_replay=p, kill=False)
|
2011-02-20 22:08:35 +00:00
|
|
|
m = dump.DumpMaster(None, o, None, outfile=cs)
|
|
|
|
self._cycle(m, "nonexistent")
|
2011-02-20 20:54:39 +00:00
|
|
|
|
2011-03-06 03:02:28 +00:00
|
|
|
o = dump.Options(client_replay=p, kill=False)
|
|
|
|
m = dump.DumpMaster(None, o, None, outfile=cs)
|
|
|
|
|
2011-02-16 10:03:46 +00:00
|
|
|
def test_options(self):
|
|
|
|
o = dump.Options(verbosity = 2)
|
|
|
|
assert o.verbosity == 2
|
|
|
|
libpry.raises(AttributeError, dump.Options, nonexistent = 2)
|
|
|
|
|
2011-02-16 21:44:08 +00:00
|
|
|
def test_filter(self):
|
2011-02-19 20:55:42 +00:00
|
|
|
assert not "GET" in self._dummy_cycle("~u foo", "", verbosity=1)
|
2011-02-16 21:44:08 +00:00
|
|
|
|
|
|
|
def test_basic(self):
|
2011-02-16 09:10:24 +00:00
|
|
|
for i in (1, 2, 3):
|
2011-07-23 00:57:54 +00:00
|
|
|
assert "GET" in self._dummy_cycle("~s", "", verbosity=i, eventlog=True)
|
2011-02-19 20:55:42 +00:00
|
|
|
assert "GET" in self._dummy_cycle("~s", "\x00\x00\x00", verbosity=i)
|
|
|
|
assert "GET" in self._dummy_cycle("~s", "ascii", verbosity=i)
|
2011-02-16 09:10:24 +00:00
|
|
|
|
2011-02-16 21:18:38 +00:00
|
|
|
def test_write(self):
|
|
|
|
d = self.tmpdir()
|
|
|
|
p = os.path.join(d, "a")
|
2011-02-19 20:55:42 +00:00
|
|
|
self._dummy_cycle(None, "", wfile=p, verbosity=0)
|
2011-02-16 21:18:38 +00:00
|
|
|
assert len(list(flow.FlowReader(open(p)).stream())) == 1
|
|
|
|
|
|
|
|
def test_write_err(self):
|
2011-02-17 23:40:45 +00:00
|
|
|
libpry.raises(
|
|
|
|
dump.DumpError,
|
|
|
|
self._dummy_cycle,
|
|
|
|
None,
|
2011-02-19 20:55:42 +00:00
|
|
|
"",
|
2011-02-17 23:40:45 +00:00
|
|
|
wfile = "nonexistentdir/foo"
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_request_script(self):
|
2011-02-19 20:55:42 +00:00
|
|
|
ret = self._dummy_cycle(None, "", request_script="scripts/a", verbosity=1)
|
2011-02-17 23:40:45 +00:00
|
|
|
assert "TESTOK" in ret
|
|
|
|
assert "DEBUG" in ret
|
|
|
|
libpry.raises(
|
|
|
|
dump.DumpError,
|
2011-02-19 20:55:42 +00:00
|
|
|
self._dummy_cycle, None, "", request_script="nonexistent"
|
2011-02-17 23:40:45 +00:00
|
|
|
)
|
|
|
|
libpry.raises(
|
|
|
|
dump.DumpError,
|
2011-02-19 20:55:42 +00:00
|
|
|
self._dummy_cycle, None, "", request_script="scripts/err_return"
|
2011-02-17 23:40:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_response_script(self):
|
2011-02-19 20:55:42 +00:00
|
|
|
ret = self._dummy_cycle(None, "", response_script="scripts/a", verbosity=1)
|
2011-02-17 23:40:45 +00:00
|
|
|
assert "TESTOK" in ret
|
|
|
|
assert "DEBUG" in ret
|
|
|
|
libpry.raises(
|
|
|
|
dump.DumpError,
|
2011-02-19 20:55:42 +00:00
|
|
|
self._dummy_cycle, None, "", response_script="nonexistent"
|
2011-02-17 23:40:45 +00:00
|
|
|
)
|
|
|
|
libpry.raises(
|
|
|
|
dump.DumpError,
|
2011-02-19 20:55:42 +00:00
|
|
|
self._dummy_cycle, None, "", response_script="scripts/err_return"
|
2011-02-16 21:18:38 +00:00
|
|
|
)
|
|
|
|
|
2011-02-25 08:11:44 +00:00
|
|
|
def test_stickycookie(self):
|
|
|
|
ret = self._dummy_cycle(None, "", stickycookie = ".*")
|
|
|
|
|
2011-03-20 05:52:16 +00:00
|
|
|
def test_stickyauth(self):
|
|
|
|
ret = self._dummy_cycle(None, "", stickyauth = ".*")
|
2011-02-16 21:18:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-02-16 09:10:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
tests = [
|
2011-02-25 08:11:44 +00:00
|
|
|
uStrFuncs(),
|
2011-02-16 09:10:24 +00:00
|
|
|
uDumpMaster()
|
|
|
|
]
|