-
Notifications
You must be signed in to change notification settings - Fork 4
/
b2g.py
executable file
·318 lines (253 loc) · 11 KB
/
b2g.py
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import sys
import time
import subprocess
from Peach.Utilities import ADB, network
from Peach.agent import Monitor, MonitorDebug
try:
from marionette import Marionette
except ImportError:
sys.exit("Please install Marionette first.")
from Peach.Utilities.Gaia import gaia
class LaunchEmulator(Monitor):
"""
This monitor will start the B2G emulator and point the FirefoxApp to our publisher.
We also monitor the main B2G process and other crashes via DEBUG:I in adb logcat.
"""
def __init__(self, args):
Monitor.__init__(self, args)
if args.has_key("URL"):
self.publisherRequestPath = str(args["URL"]).replace("'''", "")
else:
raise Exception("No publisher URL provided.")
if args.has_key("EmulatorScript"):
self.emulatorStartScript = str(args["EmulatorScript"]).replace("'''", "")
else:
raise Exception("No script provided to run the emulator.")
if args.has_key("AppName"):
self.appName = str(args["AppName"]).replace("'''", "")
else:
self.appName = "browser"
if args.has_key("ScriptTimeout"):
self.scriptTimeout = int(args["ScriptTimeout"]).replace("''", "")
else:
self.scriptTimeout = 600000
if args.has_key("PortADB"):
self.forwardedPortADB = int(args["PortADB"])
else:
self.forwardedPortADB = 2828
self._name = "MarionetteEmulator"
self.monitoringProcessId = -1
self.monitoredProcessName = "/system/b2g/b2g"
self.publisherHost = "10.0.2.2"
self.publisherPort = network.runHTTPDThread()
self.publisherURL = "http://%s:%d/%s" % (self.publisherHost, self.publisherPort, self.publisherRequestPath)
self.isEmulatorInitialized = False
self.isMonitorInitialized = False
self.adb = ADB.AccessDebugBridge(isEmulator=True)
self.emulatorProcess = None
self.crashSuccess = False
self.adbLogcat = self.adb.runCmd(["logcat"])
self.debugLogData = str()
def OnTestStarting(self):
if not self._IsRunning():
self._StartProcess()
def _StartProcess(self):
if not self.isEmulatorInitialized:
print("Starting Emulator ...")
self.emulatorProcess = subprocess.Popen(
[self.emulatorStartScript], cwd=os.path.dirname(self.emulatorStartScript), shell=True)
# adb shell setprop net.dns1 10.0.2.3
self._isBootFinished()
self.monitoringProcessId = self.adb.getPID(self.monitoredProcessName)
print("Forwarding TCP port %d ..." % self.forwardedPortADB)
self.adb.command(["forward", "tcp:%d" % self.forwardedPortADB, "tcp:%d" % self.forwardedPortADB])
self.isEmulatorInitialized = True
time.sleep(20)
if self.crashSuccess:
print("Restarting %s ..." % self.monitoredProcessName)
self.adb.killProcess(self.monitoredProcessName, True)
time.sleep(40)
self.monitoringProcessId = self.adb.getPID(self.monitoredProcessName)
self.crashSuccess = False
self.debugLogData = str()
self.adb.checkCmd(["logcat", "-c"])
print("Starting Marionette session ...")
marionette = Marionette('localhost', self.forwardedPortADB)
print(marionette.status())
marionette.start_session()
marionette.set_script_timeout(self.scriptTimeout)
marionette.switch_to_frame()
lock = gaia.LockScreen(marionette)
lock.unlock()
apps = gaia.GaiaApps(marionette)
print(apps.runningApps())
print("Launching Browser application")
apps.launch(self.appName, switch_to_frame=True)
print("Navigating to %s ..." % self.publisherURL)
marionette.execute_script("return window.wrappedJSObject.Browser.navigate('%s')" % self.publisherURL)
self.isMonitorInitialized = True
def _isBootFinished(self):
stdout = self.adbLogcat.stdout.readline()
while stdout:
if stdout.find("Get idle time: time since reset") >= 0:
return 0
stdout = self.adbLogcat.stdout.readline()
return -1
def _getB2GPid(self):
pid = -1
while pid == -1:
pid = self.adb.getPID(self.monitoredProcessName)
return pid
def _detectPidChange(self):
processIds = self.adb.getPID(self.monitoredProcessName)
return processIds
def _IsRunning(self):
if not self.isMonitorInitialized or self.crashSuccess:
return False
# We get might get the pid directly but we need to wait till the process
# is finished with loading for futher actions. See _isBootFinished()
pid = self._getB2GPid()
if self.monitoringProcessId != pid:
MonitorDebug(self._name, "pid of %s changed from %d to %d" %
(self.monitoredProcessName, self.monitoringProcessId, pid))
return False
MonitorDebug(self._name, "pid of %s is %d" % (self.monitoredProcessName, pid))
debugLog = self.adb.runCmd(["logcat", "-d", "-s", "DEBUG:I", "-v", "threadtime"])
self.debugLogData = debugLog.stdout.read()
if "Build fingerprint" in self.debugLogData:
return False
else:
debugLog.kill()
return True
def OnTestFinished(self):
if not self._IsRunning():
self.crashSuccess = True
def DetectedFault(self):
if self.crashSuccess:
return True
return False
def GetMonitorData(self):
if not self.crashSuccess:
return {}
id = self.monitoredProcessName.replace("/", "-").strip("-")
bucket = {
"Bucket": id,
"process.txt": "Process %s (%d) aborted." % (id, self.monitoringProcessId),
"device.txt": self.adb.makeDeviceReport()
}
if self.debugLogData:
bucket["debugLogData.txt"] = self.debugLogData
return bucket
class LaunchDevice(LaunchEmulator):
def __init__(self, args):
Monitor.__init__(self, args)
if args.has_key("URL"):
self.publisherRequestPath = str(args["URL"]).replace("'''", "")
else:
raise Exception("No publisher URL provided.")
if args.has_key("AppName"):
self.appName = str(args["AppName"]).replace("'''", "")
else:
self.appName = "browser"
if args.has_key("ScriptTimeout"):
self.scriptTimeout = int(args["ScriptTimeout"]).replace("''", "")
else:
self.scriptTimeout = 600000
if args.has_key("PublisherHost"):
self.publisherHost = str(args["PublisherHost"]).replace("''", "")
else:
self.publisherHost = "192.168.178.20"
if args.has_key("PortADB"):
self.forwardedPortADB = int(args["PortADB"])
else:
self.forwardedPortADB = 2828
self._name = "MarionetteDevice"
self.monitoringProcessId = -1
self.monitoredProcessName = "b2g"
self.publisherPort = network.runHTTPDThread()
self.publisherURL = "http://%s:%d/%s" % (self.publisherHost, self.publisherPort, self.publisherRequestPath)
self.isDeviceInitialized = False
self.isMonitorInitialized = False
self.adb = ADB.AccessDebugBridge(isEmulator=False)
self.crashSuccess = False
self.adbLogcat = self.adb.runCmd(["logcat"])
self.debugLogData = str()
def _StartProcess(self):
if not self.isDeviceInitialized:
print("Starting ...")
self.monitoringProcessId = self.adb.getPID(self.monitoredProcessName)
print("Forwarding TCP port %d ..." % self.forwardedPortADB)
self.adb.command(["forward", "tcp:%d" % self.forwardedPortADB, "tcp:%d" % self.forwardedPortADB])
self.isDeviceInitialized = True
print("Sleeping ...")
time.sleep(20)
if self.crashSuccess:
print("Restarting %s" % self.monitoredProcessName)
self.adb.killProcess(self.monitoredProcessName, True)
time.sleep(40)
self.monitoringProcessId = self.adb.getPID(self.monitoredProcessName)
self.crashSuccess = False
self.debugLogData = str()
self.adb.checkCmd(["logcat", "-c"])
print("Starting Marionette session")
marionette = Marionette('localhost', self.forwardedPortADB)
print(marionette.status())
marionette.start_session()
marionette.set_script_timeout(self.scriptTimeout)
marionette.switch_to_frame()
lock = gaia.LockScreen(marionette)
assert(lock.unlock())
apps = gaia.GaiaApps(marionette)
print(apps.runningApps())
print("Launching Browser application")
apps.launch(self.appName, switch_to_frame=True)
print("Navigating to %s ..." % self.publisherURL)
marionette.execute_script("return window.wrappedJSObject.Browser.navigate('%s')" % self.publisherURL)
self.isMonitorInitialized = True
def _isBootFinished(self):
stdout = self.adbLogcat.stdout.readline()
while stdout:
if stdout.find("Get idle time: time since reset") >= 0:
return 0
stdout = self.adbLogcat.stdout.readline()
return -1
def _getB2GPid(self):
pid = -1
while pid == -1:
pid = self.adb.getPID(self.monitoredProcessName)
return pid
def _detectPidChange(self):
processIds = self.adb.getPID(self.monitoredProcessName)
return processIds
def _IsRunning(self):
if not self.isMonitorInitialized or self.crashSuccess:
return False
pid = self._getB2GPid()
if self.monitoringProcessId != pid:
MonitorDebug(self._name, "pid of %s changed from %d to %d" %
(self.monitoredProcessName, self.monitoringProcessId, pid))
return False
MonitorDebug(self._name, "pid of %s is %d" % (self.monitoredProcessName, pid))
debugLog = self.adb.runCmd(["logcat", "-d", "-s", "DEBUG:I", "-v", "threadtime"])
self.debugLogData = debugLog.stdout.read()
if "Build fingerprint" in self.debugLogData:
return False
else:
debugLog.kill()
return True
def GetMonitorData(self):
if not self.crashSuccess:
return {}
id = self.monitoredProcessName.replace("/", "-").strip("-")
bucket = {
"Bucket": id,
"process.txt": "Process %s (%d) aborted." % (id, self.monitoringProcessId),
"device.txt": self.adb.makeDeviceReport()
}
if self.debugLogData:
bucket["debugLogData.txt"] = self.debugLogData
return bucket