-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathtest_adb_manager_async.py
More file actions
379 lines (295 loc) · 14.8 KB
/
test_adb_manager_async.py
File metadata and controls
379 lines (295 loc) · 14.8 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import asyncio
from contextlib import asynccontextmanager
import sys
import unittest
from unittest.mock import patch
sys.path.insert(0, "..")
from androidtv.adb_manager.adb_manager_async import _acquire, ADBPythonAsync, ADBServerAsync
from androidtv.exceptions import LockNotAcquiredException
from . import async_patchers
from .async_wrapper import awaiter
class Read(object):
"""Mock an opened file that can be read."""
async def read(self):
return ""
class ReadFail(object):
"""Mock an opened file that cannot be read."""
async def read(self):
raise FileNotFoundError
PNG_IMAGE = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\n\x00\x00\x00\n\x08\x06\x00\x00\x00\x8d2\xcf\xbd\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\x00\x00\tpHYs\x00\x00\x0fa\x00\x00\x0fa\x01\xa8?\xa7i\x00\x00\x00\x0eIDAT\x18\x95c`\x18\x05\x83\x13\x00\x00\x01\x9a\x00\x01\x16\xca\xd3i\x00\x00\x00\x00IEND\xaeB`\x82"
PNG_IMAGE_NEEDS_REPLACING = PNG_IMAGE[:5] + b"\r" + PNG_IMAGE[5:]
@asynccontextmanager
async def open_priv(infile):
"""A patch that will read the private key but not the public key."""
try:
if infile == "adbkey":
yield Read()
else:
yield ReadFail()
finally:
pass
@asynccontextmanager
async def open_priv_pub(infile):
try:
yield Read()
finally:
pass
class AsyncFakeLock:
def __init__(self):
self._acquired = True
async def acquire(self):
if self._acquired:
self._acquired = False
return True
return self._acquired
def release(self):
self._acquired = True
class AsyncLockedLock(AsyncFakeLock):
def __init__(self):
self._acquired = False
class AsyncTimedLock(AsyncFakeLock):
async def acquire(self):
await asyncio.sleep(1.0)
return await super().acquire()
class TestLock(unittest.TestCase):
"""Test the async lock code."""
@awaiter
async def test_succeed(self):
lock = AsyncFakeLock()
async with _acquire(lock):
self.assertTrue(True)
return
self.assertTrue(False)
@awaiter
async def test_fail(self):
lock = AsyncLockedLock()
with self.assertRaises(LockNotAcquiredException):
async with _acquire(lock):
pass # self.assertTrue(False)
@awaiter
async def test_fail_timeout(self):
lock = AsyncTimedLock()
with self.assertRaises(LockNotAcquiredException):
async with _acquire(lock, 0.1):
pass # self.assertTrue(False)
class TestADBPythonAsync(unittest.TestCase):
"""Test the `ADBPythonAsync` class."""
PATCH_KEY = "python"
def setUp(self):
"""Create an `ADBPythonAsync` instance."""
with async_patchers.PATCH_ADB_DEVICE_TCP, async_patchers.patch_connect(True)[self.PATCH_KEY]:
self.adb = ADBPythonAsync("HOST", 5555)
@awaiter
async def test_connect_success(self):
"""Test when the connect attempt is successful."""
with async_patchers.patch_connect(True)[self.PATCH_KEY]:
self.assertTrue(await self.adb.connect())
self.assertTrue(self.adb.available)
@awaiter
async def test_connect_fail(self):
"""Test when the connect attempt fails."""
with async_patchers.patch_connect(False)[self.PATCH_KEY]:
self.assertFalse(await self.adb.connect())
self.assertFalse(self.adb.available)
with async_patchers.patch_connect(True)[self.PATCH_KEY]:
self.assertTrue(await self.adb.connect())
self.assertTrue(self.adb.available)
with async_patchers.patch_connect(True)[self.PATCH_KEY]:
with async_patchers.PATCH_CONNECT_FAIL_CUSTOM_EXCEPTION[self.PATCH_KEY]:
self.assertFalse(await self.adb.connect())
self.assertFalse(self.adb.available)
@awaiter
async def test_connect_fail_lock(self):
"""Test when the connect attempt fails due to the lock."""
with async_patchers.patch_connect(True)[self.PATCH_KEY]:
with patch.object(self.adb, "_adb_lock", AsyncLockedLock()):
self.assertFalse(await self.adb.connect())
self.assertFalse(self.adb.available)
@awaiter
async def test_adb_shell_fail(self):
"""Test when an ADB shell command is not sent because the device is unavailable."""
self.assertFalse(self.adb.available)
with async_patchers.patch_connect(True)[self.PATCH_KEY], async_patchers.patch_shell(None)[self.PATCH_KEY]:
self.assertIsNone(await self.adb.shell("TEST"))
with async_patchers.patch_connect(True)[self.PATCH_KEY], async_patchers.patch_shell("TEST")[self.PATCH_KEY]:
self.assertTrue(await self.adb.connect())
with patch.object(self.adb, "_adb_lock", AsyncLockedLock()):
with self.assertRaises(LockNotAcquiredException):
await self.adb.shell("TEST")
with self.assertRaises(LockNotAcquiredException):
await self.adb.shell("TEST2")
@awaiter
async def test_adb_shell_success(self):
"""Test when an ADB shell command is successfully sent."""
with async_patchers.patch_connect(True)[self.PATCH_KEY], async_patchers.patch_shell("TEST")[self.PATCH_KEY]:
self.assertTrue(await self.adb.connect())
self.assertEqual(await self.adb.shell("TEST"), "TEST")
@awaiter
async def test_adb_shell_fail_lock_released(self):
"""Test that the ADB lock gets released when an exception is raised."""
with async_patchers.patch_connect(True)[self.PATCH_KEY], async_patchers.patch_shell("TEST")[self.PATCH_KEY]:
self.assertTrue(await self.adb.connect())
with (
async_patchers.patch_shell("TEST", error=True)[self.PATCH_KEY],
patch.object(self.adb, "_adb_lock", AsyncFakeLock()),
):
with patch("{}.AsyncFakeLock.release".format(__name__)) as release:
with self.assertRaises(Exception):
await self.adb.shell("TEST")
assert release.called
@awaiter
async def test_adb_shell_lock_not_acquired_not_released(self):
"""Test that the lock does not get released if it is not acquired."""
with async_patchers.patch_connect(True)[self.PATCH_KEY], async_patchers.patch_shell("TEST")[self.PATCH_KEY]:
self.assertTrue(await self.adb.connect())
self.assertEqual(await self.adb.shell("TEST"), "TEST")
with async_patchers.patch_shell("TEST")[self.PATCH_KEY], patch.object(self.adb, "_adb_lock", AsyncLockedLock()):
with patch("{}.AsyncLockedLock.release".format(__name__)) as release:
with self.assertRaises(LockNotAcquiredException):
await self.adb.shell("TEST")
release.assert_not_called()
@awaiter
async def test_adb_push_fail(self):
"""Test when an ADB push command is not executed because the device is unavailable."""
self.assertFalse(self.adb.available)
with async_patchers.patch_connect(True)[self.PATCH_KEY]:
with async_patchers.PATCH_PUSH[self.PATCH_KEY] as patch_push:
await self.adb.push("TEST_LOCAL_PATCH", "TEST_DEVICE_PATH")
patch_push.assert_not_called()
with async_patchers.patch_connect(True)[self.PATCH_KEY]:
with async_patchers.PATCH_PUSH[self.PATCH_KEY] as patch_push:
self.assertTrue(await self.adb.connect())
with patch.object(self.adb, "_adb_lock", AsyncLockedLock()):
with self.assertRaises(LockNotAcquiredException):
await self.adb.push("TEST_LOCAL_PATH", "TEST_DEVICE_PATH")
patch_push.assert_not_called()
@awaiter
async def test_adb_push_success(self):
"""Test when an ADB push command is successfully executed."""
with async_patchers.patch_connect(True)[self.PATCH_KEY]:
with async_patchers.PATCH_PUSH[self.PATCH_KEY] as patch_push:
self.assertTrue(await self.adb.connect())
await self.adb.push("TEST_LOCAL_PATH", "TEST_DEVICE_PATH")
self.assertEqual(patch_push.call_count, 1)
@awaiter
async def test_adb_pull_fail(self):
"""Test when an ADB pull command is not executed because the device is unavailable."""
self.assertFalse(self.adb.available)
with async_patchers.patch_connect(True)[self.PATCH_KEY]:
with async_patchers.PATCH_PULL[self.PATCH_KEY] as patch_pull:
await self.adb.pull("TEST_LOCAL_PATCH", "TEST_DEVICE_PATH")
patch_pull.assert_not_called()
with async_patchers.patch_connect(True)[self.PATCH_KEY]:
with async_patchers.PATCH_PULL[self.PATCH_KEY] as patch_pull:
self.assertTrue(await self.adb.connect())
with patch.object(self.adb, "_adb_lock", AsyncLockedLock()):
with self.assertRaises(LockNotAcquiredException):
await self.adb.pull("TEST_LOCAL_PATH", "TEST_DEVICE_PATH")
patch_pull.assert_not_called()
@awaiter
async def test_adb_pull_success(self):
"""Test when an ADB pull command is successfully executed."""
with async_patchers.patch_connect(True)[self.PATCH_KEY]:
with async_patchers.PATCH_PULL[self.PATCH_KEY] as patch_pull:
self.assertTrue(await self.adb.connect())
await self.adb.pull("TEST_LOCAL_PATH", "TEST_DEVICE_PATH")
self.assertEqual(patch_pull.call_count, 1)
@awaiter
async def test_adb_screencap_fail_unavailable(self):
"""Test when an ADB screencap command fails because the connection is unavailable."""
self.assertFalse(self.adb.available)
self.assertIsNone(await self.adb.screencap())
@awaiter
async def test_adb_screencap_lock_not_acquired(self):
"""Test when an ADB screencap command fails because the ADB lock could not be acquired."""
with async_patchers.patch_connect(True)[self.PATCH_KEY], async_patchers.patch_shell("TEST")[self.PATCH_KEY]:
self.assertTrue(await self.adb.connect())
self.assertEqual(await self.adb.shell("TEST"), "TEST")
with (
async_patchers.patch_shell(PNG_IMAGE)[self.PATCH_KEY],
patch.object(self.adb, "_adb_lock", AsyncLockedLock()),
):
with patch("{}.AsyncLockedLock.release".format(__name__)) as release:
with self.assertRaises(LockNotAcquiredException):
await self.adb.screencap()
release.assert_not_called()
@awaiter
async def test_adb_screencap_success(self):
"""Test the `screencap` method."""
with async_patchers.patch_connect(True)[self.PATCH_KEY], async_patchers.patch_shell(PNG_IMAGE)[self.PATCH_KEY]:
self.assertTrue(await self.adb.connect())
if isinstance(self.adb, ADBPythonAsync):
self.assertEqual(await self.adb.screencap(), PNG_IMAGE)
with async_patchers.patch_shell(PNG_IMAGE_NEEDS_REPLACING)[self.PATCH_KEY]:
self.assertEqual(await self.adb.screencap(), PNG_IMAGE)
else:
with patch.object(
self.adb._adb_device, "screencap", return_value=PNG_IMAGE, new_callable=async_patchers.AsyncMock
):
self.assertEqual(await self.adb.screencap(), PNG_IMAGE)
class TestADBPythonUsbAsync(unittest.TestCase):
"""Test the `ADBPythonAsync` class using a USB connection."""
def test_init(self):
"""Create an `ADBPythonSync` instance with a USB connection."""
with patch("androidtv.adb_manager.adb_manager_async.AdbDeviceUsbAsync") as patched:
ADBPythonAsync("", 5555)
assert patched.called
class TestADBServerAsync(TestADBPythonAsync):
"""Test the `ADBServerAsync` class."""
PATCH_KEY = "server"
def setUp(self):
"""Create an `ADBServerAsync` instance."""
self.adb = ADBServerAsync("HOST", 5555, "ADB_SERVER_IP")
@awaiter
async def test_connect_fail_server(self):
"""Test that the ``connect`` method works correctly."""
with async_patchers.patch_connect(True)[self.PATCH_KEY]:
self.assertTrue(await self.adb.connect())
with async_patchers.PATCH_ADB_SERVER_RUNTIME_ERROR:
self.assertFalse(await self.adb.connect())
self.assertFalse(self.adb.available)
class TestADBPythonAsyncWithAuthentication(unittest.TestCase):
"""Test the `ADBPythonAsync` class."""
PATCH_KEY = "python"
def setUp(self):
"""Create an `ADBPythonAsync` instance."""
with async_patchers.PATCH_ADB_DEVICE_TCP, async_patchers.patch_connect(True)[self.PATCH_KEY]:
self.adb = ADBPythonAsync("HOST", 5555, "adbkey")
@awaiter
async def test_connect_success_with_priv_key(self):
"""Test when the connect attempt is successful when using a private key."""
with (
async_patchers.patch_connect(True)[self.PATCH_KEY],
patch("androidtv.adb_manager.adb_manager_async.aiofiles.open", open_priv),
patch("androidtv.adb_manager.adb_manager_async.PythonRSASigner", return_value="TEST"),
):
self.assertTrue(await self.adb.connect())
self.assertTrue(self.adb.available)
with async_patchers.patch_connect(True)[self.PATCH_KEY]:
with patch("androidtv.adb_manager.adb_manager_async.aiofiles.open") as patch_open:
self.assertTrue(await self.adb.connect())
self.assertTrue(self.adb.available)
assert not patch_open.called
@awaiter
async def test_connect_success_with_priv_pub_key(self):
"""Test when the connect attempt is successful when using private and public keys."""
with (
async_patchers.patch_connect(True)[self.PATCH_KEY],
patch("androidtv.adb_manager.adb_manager_async.aiofiles.open", open_priv_pub),
patch("androidtv.adb_manager.adb_manager_async.PythonRSASigner", return_value=None),
):
self.assertTrue(await self.adb.connect())
self.assertTrue(self.adb.available)
class TestADBPythonAsyncClose(unittest.TestCase):
"""Test the `ADBPythonAsync.close` method."""
PATCH_KEY = "python"
@awaiter
async def test_close(self):
"""Test the `ADBPythonAsync.close` method."""
with async_patchers.PATCH_ADB_DEVICE_TCP, async_patchers.patch_connect(True)[self.PATCH_KEY]:
self.adb = ADBPythonAsync("HOST", 5555)
with async_patchers.patch_connect(True)[self.PATCH_KEY]:
self.assertTrue(await self.adb.connect())
self.assertTrue(self.adb.available)
await self.adb.close()
self.assertFalse(self.adb.available)