forked from google/python-adb
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfastboot_test.py
182 lines (143 loc) · 5.25 KB
/
fastboot_test.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
# Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for adb.fastboot."""
import cStringIO
import logging
import os
import sys
import tempfile
import unittest
import common_mock
from adb import adb_protocol
from adb import fastboot
def _SumLengths(items):
return sum(len(item) for item in items)
class FastbootTest(unittest.TestCase):
def setUp(self):
super(FastbootTest, self).setUp()
self.usb = common_mock.MockUsb()
def tearDown(self):
try:
self.usb.Close()
finally:
super(FastbootTest, self).tearDown()
def ExpectDownload(self, writes, succeed=True):
self.usb.ExpectWrite('download:%08x' % _SumLengths(writes))
self.usb.ExpectRead('DATA%08x' % _SumLengths(writes))
for data in writes:
self.usb.ExpectWrite(data)
if succeed:
self.usb.ExpectRead('OKAYResult')
else:
self.usb.ExpectRead('FAILResult')
def ExpectFlash(self, partition, succeed=True):
self.usb.ExpectWrite('flash:%s' % partition)
self.usb.ExpectRead('INFORandom info from the bootloader')
if succeed:
self.usb.ExpectRead('OKAYDone')
else:
self.usb.ExpectRead('FAILDone')
def testDownload(self):
raw = 'aoeuidhtnsqjkxbmwpyfgcrl'
data = cStringIO.StringIO(raw)
self.ExpectDownload([raw])
commands = fastboot.FastbootCommands(self.usb)
response = commands.Download(data)
self.assertEqual('Result', response)
def testDownloadFail(self):
raw = 'aoeuidhtnsqjkxbmwpyfgcrl'
data = cStringIO.StringIO(raw)
self.ExpectDownload([raw], succeed=False)
commands = fastboot.FastbootCommands(self.usb)
with self.assertRaises(fastboot.FastbootRemoteFailure):
commands.Download(data)
self.usb.ExpectWrite('download:%08x' % _SumLengths([raw]))
self.usb.ExpectRead('DATA%08x' % (_SumLengths([raw]) - 2))
with self.assertRaises(fastboot.FastbootTransferError):
commands.Download(cStringIO.StringIO(raw))
def testFlash(self):
partition = 'yarr'
self.ExpectFlash(partition)
commands = fastboot.FastbootCommands(self.usb)
output = cStringIO.StringIO()
def InfoCb(message):
if message.header == 'INFO':
output.write(message.message)
response = commands.Flash(partition, info_cb=InfoCb)
self.assertEqual('Done', response)
self.assertEqual('Random info from the bootloader', output.getvalue())
def testFlashFail(self):
partition = 'matey'
self.ExpectFlash(partition, succeed=False)
commands = fastboot.FastbootCommands(self.usb)
with self.assertRaises(fastboot.FastbootRemoteFailure):
commands.Flash(partition)
def testFlashFromFile(self):
partition = 'somewhere'
# More than one packet, ends somewhere into the 3rd packet.
raw = 'SOMETHING' * 1086
tmp = tempfile.NamedTemporaryFile(delete=False)
tmp.write(raw)
tmp.close()
progresses = []
pieces = []
chunk_size = fastboot.FASTBOOT_WRITE_CHUNK_SIZE_KB * 1024
while raw:
pieces.append(raw[:chunk_size])
raw = raw[chunk_size:]
self.ExpectDownload(pieces)
self.ExpectFlash(partition)
cb = lambda progress, total: progresses.append((progress, total))
commands = fastboot.FastbootCommands(self.usb)
commands.FlashFromFile(
partition, tmp.name, progress_callback=cb)
self.assertEqual(len(pieces), len(progresses))
os.remove(tmp.name)
def testSimplerCommands(self):
commands = fastboot.FastbootCommands(self.usb)
self.usb.ExpectWrite('erase:vector')
self.usb.ExpectRead('OKAY')
commands.Erase('vector')
self.usb.ExpectWrite('getvar:variable')
self.usb.ExpectRead('OKAYstuff')
self.assertEqual('stuff', commands.Getvar('variable'))
self.usb.ExpectWrite('continue')
self.usb.ExpectRead('OKAY')
commands.Continue()
self.usb.ExpectWrite('reboot')
self.usb.ExpectRead('OKAY')
commands.Reboot()
self.usb.ExpectWrite('reboot-bootloader')
self.usb.ExpectRead('OKAY')
commands.RebootBootloader()
self.usb.ExpectWrite('oem a little somethin')
self.usb.ExpectRead('OKAYsomethin')
self.assertEqual('somethin', commands.Oem('a little somethin'))
def testVariousFailures(self):
commands = fastboot.FastbootCommands(self.usb)
self.usb.ExpectWrite('continue')
self.usb.ExpectRead('BLEH')
with self.assertRaises(fastboot.FastbootInvalidResponse):
commands.Continue()
self.usb.ExpectWrite('continue')
self.usb.ExpectRead('DATA000000')
with self.assertRaises(fastboot.FastbootStateMismatch):
commands.Continue()
if __name__ == '__main__':
if '-v' in sys.argv:
logging.basicConfig(level=logging.DEBUG) # pragma: no cover
fastboot._LOG.setLevel(logging.DEBUG) # pragma: no cover
else:
logging.basicConfig(level=logging.ERROR)
unittest.main()