aboutsummaryrefslogtreecommitdiffstats
path: root/test/mitmproxy/tools/console/test_commander.py
blob: a77be04321e14be561236be2bdf4cd96c101bc70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import pytest

from mitmproxy.test import taddons
from mitmproxy.tools.console.commander import commander


class TestListCompleter:
    def test_cycle(self):
        tests = [
            [
                "",
                ["a", "b", "c"],
                ["a", "b", "c", "a"]
            ],
            [
                "xxx",
                ["a", "b", "c"],
                ["xxx", "xxx", "xxx"]
            ],
            [
                "b",
                ["a", "b", "ba", "bb", "c"],
                ["b", "ba", "bb", "b"]
            ],
        ]
        for start, options, cycle in tests:
            c = commander.ListCompleter(start, options)
            for expected in cycle:
                assert c.cycle() == expected


class TestCommandEdit:
    def test_open_command_bar(self):
        with taddons.context() as tctx:
            history = commander.CommandHistory(tctx.master, size=3)
            edit = commander.CommandEdit(tctx.master, '', history)

            try:
                edit.update()
            except IndexError:
                pytest.faied("Unexpected IndexError")

    def test_insert(self):
        with taddons.context() as tctx:
            history = commander.CommandHistory(tctx.master, size=3)
            edit = commander.CommandEdit(tctx.master, '', history)
            edit.keypress(1, 'a')
            assert edit.get_edit_text() == 'a'

            # Don't let users type a space before starting a command
            # as a usability feature
            history = commander.CommandHistory(tctx.master, size=3)
            edit = commander.CommandEdit(tctx.master, '', history)
            edit.keypress(1, ' ')
            assert edit.get_edit_text() == ''

    def test_backspace(self):
        with taddons.context() as tctx:
            history = commander.CommandHistory(tctx.master, size=3)
            edit = commander.CommandEdit(tctx.master, '', history)
            edit.keypress(1, 'a')
            edit.keypress(1, 'b')
            assert edit.get_edit_text() == 'ab'
            edit.keypress(1, 'backspace')
            assert edit.get_edit_text() == 'a'

    def test_left(self):
        with taddons.context() as tctx:
            history = commander.CommandHistory(tctx.master, size=3)
            edit = commander.CommandEdit(tctx.master, '', history)
            edit.keypress(1, 'a')
            assert edit.cbuf.cursor == 1
            edit.keypress(1, 'left')
            assert edit.cbuf.cursor == 0

            # Do it again to make sure it won't go negative
            edit.keypress(1, 'left')
            assert edit.cbuf.cursor == 0

    def test_right(self):
        with taddons.context() as tctx:
            history = commander.CommandHistory(tctx.master, size=3)
            edit = commander.CommandEdit(tctx.master, '', history)
            edit.keypress(1, 'a')
            assert edit.cbuf.cursor == 1

            # Make sure cursor won't go past the text
            edit.keypress(1, 'right')
            assert edit.cbuf.cursor == 1

            # Make sure cursor goes left and then back right
            edit.keypress(1, 'left')
            assert edit.cbuf.cursor == 0
            edit.keypress(1, 'right')
            assert edit.cbuf.cursor == 1

    def test_up_and_down(self):
        with taddons.context() as tctx:
            history = commander.CommandHistory(tctx.master, size=3)
            edit = commander.CommandEdit(tctx.master, '', history)

            buf = commander.CommandBuffer(tctx.master, 'cmd1')
            history.add_command(buf)
            buf = commander.CommandBuffer(tctx.master, 'cmd2')
            history.add_command(buf)

            edit.keypress(1, 'up')
            assert edit.get_edit_text() == 'cmd2'
            edit.keypress(1, 'up')
            assert edit.get_edit_text() == 'cmd1'
            edit.keypress(1, 'up')
            assert edit.get_edit_text() == 'cmd1'

            history = commander.CommandHistory(tctx.master, size=5)
            edit = commander.CommandEdit(tctx.master, '', history)
            edit.keypress(1, 'a')
            edit.keypress(1, 'b')
            edit.keypress(1, 'c')
            assert edit.get_edit_text() == 'abc'
            edit.keypress(1, 'up')
            assert edit.get_edit_text() == ''
            edit.keypress(1, 'down')
            assert edit.get_edit_text() == 'abc'
            edit.keypress(1, 'down')
            assert edit.get_edit_text() == 'abc'

            history = commander.CommandHistory(tctx.master, size=5)
            edit = commander.CommandEdit(tctx.master, '', history)
            buf = commander.CommandBuffer(tctx.master, 'cmd3')
            history.add_command(buf)
            edit.keypress(1, 'z')
            edit.keypress(1, 'up')
            assert edit.get_edit_text() == 'cmd3'
            edit.keypress(1, 'down')
            assert edit.get_edit_text() == 'z'


class TestCommandHistory:
    def fill_history(self, commands):
        with taddons.context() as tctx:
            history = commander.CommandHistory(tctx.master, size=3)
            for c in commands:
                cbuf = commander.CommandBuffer(tctx.master, c)
                history.add_command(cbuf)
        return history, tctx.master

    def test_add_command(self):
        commands = ["command1", "command2"]
        history, tctx_master = self.fill_history(commands)

        saved_commands = [buf.text for buf in history.saved_commands]
        assert saved_commands == [""] + commands

        # The history size is only 3. So, we forget the first
        # one command, when adding fourth command
        cbuf = commander.CommandBuffer(tctx_master, "command3")
        history.add_command(cbuf)
        saved_commands = [buf.text for buf in history.saved_commands]
        assert saved_commands == commands + ["command3"]

        # Commands with the same text are not repeated in the history one by one
        history.add_command(cbuf)
        saved_commands = [buf.text for buf in history.saved_commands]
        assert saved_commands == commands + ["command3"]

        # adding command in execution mode sets index at the beginning of the history
        # and replace the last command buffer if it is empty or has the same text
        cbuf = commander.CommandBuffer(tctx_master, "")
        history.add_command(cbuf)
        history.index = 0
        cbuf = commander.CommandBuffer(tctx_master, "command4")
        history.add_command(cbuf, True)
        assert history.index == history.last_index
        saved_commands = [buf.text for buf in history.saved_commands]
        assert saved_commands == ["command2", "command3", "command4"]

    def test_get_next(self):
        commands = ["command1", "command2"]
        history, tctx_master = self.fill_history(commands)

        history.index = -1
        expected_items = ["", "command1", "command2"]
        for i in range(3):
            assert history.get_next().text == expected_items[i]
        # We are at the last item of the history
        assert history.get_next() is None

    def test_get_prev(self):
        commands = ["command1", "command2"]
        history, tctx_master = self.fill_history(commands)

        expected_items = ["command2", "command1", ""]
        history.index = history.last_index + 1
        for i in range(3):
            assert history.get_prev().text == expected_items[i]
        # We are at the first item of the history
        assert history.get_prev() is None


class TestCommandBuffer:

    def test_backspace(self):
        tests = [
            [("", 0), ("", 0)],
            [("1", 0), ("1", 0)],
            [("1", 1), ("", 0)],
            [("123", 3), ("12", 2)],
            [("123", 2), ("13", 1)],
            [("123", 0), ("123", 0)],
        ]
        with taddons.context() as tctx:
            for start, output in tests:
                cb = commander.CommandBuffer(tctx.master)
                cb.text, cb.cursor = start[0], start[1]
                cb.backspace()
                assert cb.text == output[0]
                assert cb.cursor == output[1]

    def test_left(self):
        cursors = [3, 2, 1, 0, 0]
        with taddons.context() as tctx:
            cb = commander.CommandBuffer(tctx.master)
            cb.text, cb.cursor = "abcd", 4
            for c in cursors:
                cb.left()
                assert cb.cursor == c

    def test_right(self):
        cursors = [1, 2, 3, 4, 4]
        with taddons.context() as tctx:
            cb = commander.CommandBuffer(tctx.master)
            cb.text, cb.cursor = "abcd", 0
            for c in cursors:
                cb.right()
                assert cb.cursor == c

    def test_insert(self):
        tests = [
            [("", 0), ("x", 1)],
            [("a", 0), ("xa", 1)],
            [("xa", 2), ("xax", 3)],
        ]
        with taddons.context() as tctx:
            for start, output in tests:
                cb = commander.CommandBuffer(tctx.master)
                cb.text, cb.cursor = start[0], start[1]
                cb.insert("x")
                assert cb.text == output[0]
                assert cb.cursor == output[1]

    def test_cycle_completion(self):
        with taddons.context() as tctx:
            cb = commander.CommandBuffer(tctx.master)
            cb.text = "foo bar"
            cb.cursor = len(cb.text)
            cb.cycle_completion()

            ch = commander.CommandHistory(tctx.master, 30)
            ce = commander.CommandEdit(tctx.master, "se", ch)
            ce.keypress(1, 'tab')
            ce.update()
            ret = ce.cbuf.render()
            assert ret == [
                ('commander_command', 'set'),
                ('text', ' '),
                ('commander_hint', 'option '),
                ('commander_hint', 'value '),
            ]

    def test_render(self):
        with taddons.context() as tctx:
            cb = commander.CommandBuffer(tctx.master)
            cb.text = "foo"
            assert cb.render()

            cb.text = "set view_filter '~bq test'"
            ret = cb.render()
            assert ret == [
                ('commander_command', 'set'),
                ('text', ' '),
                ('text', 'view_filter'),
                ('text', ' '),
                ('text', "'~bq test'"),
            ]

            cb.text = "set"
            ret = cb.render()
            assert ret == [
                ('commander_command', 'set'),
                ('text', ' '),
                ('commander_hint', 'option '),
                ('commander_hint', 'value '),
            ]