2015-05-30 00:03:28 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import mock
|
|
|
|
import gc
|
2014-02-05 22:17:26 +00:00
|
|
|
from os.path import normpath
|
|
|
|
import mock_urwid
|
2012-02-19 22:04:07 +00:00
|
|
|
from libmproxy import console
|
2012-02-07 03:39:37 +00:00
|
|
|
from libmproxy.console import common
|
2014-02-05 22:17:26 +00:00
|
|
|
|
2011-03-05 02:58:48 +00:00
|
|
|
import tutils
|
2010-02-16 04:09:07 +00:00
|
|
|
|
2015-05-30 00:03:28 +00:00
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
class TestConsoleState:
|
2010-02-16 04:09:07 +00:00
|
|
|
def test_flow(self):
|
|
|
|
"""
|
|
|
|
normal flow:
|
|
|
|
|
|
|
|
connect -> request -> response
|
|
|
|
"""
|
2011-01-25 02:02:48 +00:00
|
|
|
c = console.ConsoleState()
|
2011-02-19 04:00:24 +00:00
|
|
|
f = self._add_request(c)
|
2014-11-26 03:18:21 +00:00
|
|
|
assert f in c.flows
|
2010-02-16 04:09:07 +00:00
|
|
|
assert c.get_focus() == (f, 0)
|
|
|
|
|
|
|
|
def test_focus(self):
|
|
|
|
"""
|
|
|
|
normal flow:
|
|
|
|
|
|
|
|
connect -> request -> response
|
|
|
|
"""
|
2011-01-25 02:02:48 +00:00
|
|
|
c = console.ConsoleState()
|
2011-02-19 04:00:24 +00:00
|
|
|
f = self._add_request(c)
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
assert c.get_focus() == (f, 0)
|
|
|
|
assert c.get_from_pos(0) == (f, 0)
|
|
|
|
assert c.get_from_pos(1) == (None, None)
|
|
|
|
assert c.get_next(0) == (None, None)
|
|
|
|
|
2011-02-19 04:00:24 +00:00
|
|
|
f2 = self._add_request(c)
|
2011-03-19 20:31:39 +00:00
|
|
|
assert c.get_focus() == (f, 0)
|
|
|
|
assert c.get_next(0) == (f2, 1)
|
|
|
|
assert c.get_prev(1) == (f, 0)
|
2010-02-16 04:09:07 +00:00
|
|
|
assert c.get_next(1) == (None, None)
|
|
|
|
|
|
|
|
c.set_focus(0)
|
2011-03-19 20:31:39 +00:00
|
|
|
assert c.get_focus() == (f, 0)
|
2010-02-16 04:09:07 +00:00
|
|
|
c.set_focus(-1)
|
2011-03-19 20:31:39 +00:00
|
|
|
assert c.get_focus() == (f, 0)
|
2011-03-20 05:52:16 +00:00
|
|
|
c.set_focus(2)
|
|
|
|
assert c.get_focus() == (f2, 1)
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
c.delete_flow(f2)
|
|
|
|
assert c.get_focus() == (f, 0)
|
|
|
|
c.delete_flow(f)
|
|
|
|
assert c.get_focus() == (None, None)
|
|
|
|
|
|
|
|
def _add_request(self, state):
|
2014-09-03 14:57:56 +00:00
|
|
|
f = tutils.tflow()
|
2014-11-26 03:18:21 +00:00
|
|
|
return state.add_flow(f)
|
2010-02-16 04:09:07 +00:00
|
|
|
|
|
|
|
def _add_response(self, state):
|
|
|
|
f = self._add_request(state)
|
2014-09-03 14:57:56 +00:00
|
|
|
f.response = tutils.tresp()
|
2014-11-26 03:18:21 +00:00
|
|
|
state.update_flow(f)
|
2010-02-16 04:09:07 +00:00
|
|
|
|
2011-02-02 23:16:03 +00:00
|
|
|
def test_add_response(self):
|
2011-01-25 02:02:48 +00:00
|
|
|
c = console.ConsoleState()
|
2011-02-02 23:16:03 +00:00
|
|
|
f = self._add_request(c)
|
2014-09-03 14:57:56 +00:00
|
|
|
f.response = tutils.tresp()
|
2011-02-02 23:16:03 +00:00
|
|
|
c.focus = None
|
2014-11-26 03:18:21 +00:00
|
|
|
c.update_flow(f)
|
2010-02-16 04:09:07 +00:00
|
|
|
|
2011-02-02 23:16:03 +00:00
|
|
|
def test_focus_view(self):
|
2011-01-26 03:50:17 +00:00
|
|
|
c = console.ConsoleState()
|
|
|
|
self._add_request(c)
|
|
|
|
self._add_response(c)
|
|
|
|
self._add_request(c)
|
|
|
|
self._add_response(c)
|
|
|
|
self._add_request(c)
|
|
|
|
self._add_response(c)
|
2012-02-10 02:31:45 +00:00
|
|
|
assert not c.set_limit("~s")
|
2011-02-02 23:16:03 +00:00
|
|
|
assert len(c.view) == 3
|
2011-03-19 20:31:39 +00:00
|
|
|
assert c.focus == 0
|
2011-01-26 03:50:17 +00:00
|
|
|
|
2012-04-01 21:30:38 +00:00
|
|
|
def test_settings(self):
|
|
|
|
c = console.ConsoleState()
|
|
|
|
f = self._add_request(c)
|
|
|
|
c.add_flow_setting(f, "foo", "bar")
|
|
|
|
assert c.get_flow_setting(f, "foo") == "bar"
|
|
|
|
assert c.get_flow_setting(f, "oink") == None
|
|
|
|
assert c.get_flow_setting(f, "oink", "foo") == "foo"
|
|
|
|
assert len(c.flowsettings) == 1
|
|
|
|
c.delete_flow(f)
|
|
|
|
del f
|
2014-02-05 22:17:26 +00:00
|
|
|
gc.collect()
|
2012-04-01 21:30:38 +00:00
|
|
|
assert len(c.flowsettings) == 0
|
|
|
|
|
2010-02-16 04:09:07 +00:00
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
def test_format_keyvals():
|
|
|
|
assert common.format_keyvals(
|
|
|
|
[
|
|
|
|
("aa", "bb"),
|
|
|
|
None,
|
|
|
|
("cc", "dd"),
|
|
|
|
(None, "dd"),
|
|
|
|
(None, "dd"),
|
|
|
|
]
|
|
|
|
)
|
2010-02-16 04:09:07 +00:00
|
|
|
|
2011-02-02 23:16:03 +00:00
|
|
|
|
2012-06-09 01:42:43 +00:00
|
|
|
def test_options():
|
|
|
|
assert console.Options(kill=True)
|