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
|
#!/usr/bin/env python
from __future__ import absolute_import, print_function
import os
import mock
import mozunit
import mozinfo
import requests
from mozproxy import get_playback
from support import tempdir
here = os.path.dirname(__file__)
class Process:
def __init__(self, *args, **kw):
pass
def run(self):
print("I am running something")
def poll(self):
return None
def wait(self):
return 0
def kill(self, sig=9):
pass
proc = object()
pid = 1234
stderr = stdout = None
returncode = 0
_RETRY = 0
class ProcessWithRetry(Process):
def __init__(self, *args, **kw):
Process.__init__(self, *args, **kw)
def wait(self):
global _RETRY
_RETRY += 1
if _RETRY >= 2:
_RETRY = 0
return 0
return -1
def kill(pid, signal):
if pid == 1234:
return
return os.kill(pid, signal)
def get_status_code(url, playback):
response = requests.get(
url=url, proxies={"http": "http://%s:%s/" % (playback.host, playback.port)}
)
return response.status_code
def test_mitm_check_proxy(*args):
# test setup
pageset_name = os.path.join(here, "files", "mitm5-linux-firefox-amazon.manifest")
config = {
"playback_tool": "mitmproxy",
"playback_files": [os.path.join(here, "files", pageset_name)],
"playback_version": "5.1.1",
"platform": mozinfo.os,
"run_local": "MOZ_AUTOMATION" not in os.environ,
"binary": "firefox",
"app": "firefox",
"host": "127.0.0.1",
}
with tempdir() as obj_path:
config["obj_path"] = obj_path
playback = get_playback(config)
assert playback is not None
try:
playback.start()
url = "https://m.media-amazon.com/images/G/01/csm/showads.v2.js"
assert get_status_code(url, playback) == 200
url = "http://mozproxy/checkProxy"
assert get_status_code(url, playback) == 404
finally:
playback.stop()
@mock.patch("mozproxy.backends.mitm.Mitmproxy.check_proxy")
@mock.patch("mozproxy.backends.mitm.mitm.ProcessHandler", new=Process)
@mock.patch("mozproxy.utils.ProcessHandler", new=Process)
@mock.patch("os.kill", new=kill)
def test_mitm(*args):
pageset_name = os.path.join(here, "files", "mitm5-linux-firefox-amazon.manifest")
config = {
"playback_tool": "mitmproxy",
"playback_files": [pageset_name],
"playback_version": "5.1.1",
"platform": mozinfo.os,
"run_local": True,
"binary": "firefox",
"app": "firefox",
"host": "example.com",
}
with tempdir() as obj_path:
config["obj_path"] = obj_path
playback = get_playback(config)
assert playback is not None
try:
playback.start()
finally:
playback.stop()
@mock.patch("mozproxy.backends.mitm.Mitmproxy.check_proxy")
@mock.patch("mozproxy.backends.mitm.mitm.ProcessHandler", new=Process)
@mock.patch("mozproxy.utils.ProcessHandler", new=Process)
@mock.patch("os.kill", new=kill)
def test_playback_setup_failed(*args):
class SetupFailed(Exception):
pass
def setup(*args, **kw):
def _s(self):
raise SetupFailed("Failed")
return _s
pageset_name = os.path.join(here, "files", "mitm5-linux-firefox-amazon.manifest")
config = {
"playback_tool": "mitmproxy",
"playback_files": [pageset_name],
"playback_version": "4.0.4",
"platform": mozinfo.os,
"run_local": True,
"binary": "firefox",
"app": "firefox",
"host": "example.com",
}
prefix = "mozproxy.backends.mitm.desktop.MitmproxyDesktop."
with tempdir() as obj_path:
config["obj_path"] = obj_path
with mock.patch(prefix + "setup", new_callable=setup):
with mock.patch(prefix + "stop_mitmproxy_playback") as p:
try:
pb = get_playback(config)
pb.start()
except SetupFailed:
assert p.call_count == 1
except Exception:
raise
@mock.patch("mozproxy.backends.mitm.Mitmproxy.check_proxy")
@mock.patch("mozproxy.backends.mitm.mitm.ProcessHandler", new=ProcessWithRetry)
@mock.patch("mozproxy.utils.ProcessHandler", new=ProcessWithRetry)
@mock.patch("os.kill", new=kill)
def test_mitm_with_retry(*args):
pageset_name = os.path.join(here, "files", "mitm5-linux-firefox-amazon.manifest")
config = {
"playback_tool": "mitmproxy",
"playback_files": [pageset_name],
"playback_version": "5.1.1",
"platform": mozinfo.os,
"run_local": True,
"binary": "firefox",
"app": "firefox",
"host": "example.com",
}
with tempdir() as obj_path:
config["obj_path"] = obj_path
playback = get_playback(config)
assert playback is not None
try:
playback.start()
finally:
playback.stop()
if __name__ == "__main__":
mozunit.main(runwith="pytest")
|