Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # |
| 4 | # Copyright (C) 2015 The Android Open Source Project |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | from __future__ import print_function |
| 19 | |
| 20 | import hashlib |
| 21 | import os |
| 22 | import posixpath |
| 23 | import random |
| 24 | import shlex |
| 25 | import shutil |
| 26 | import subprocess |
| 27 | import tempfile |
| 28 | import unittest |
| 29 | |
| 30 | import mock |
| 31 | |
| 32 | import adb |
| 33 | |
| 34 | |
Dan Albert | e2b4a5f | 2015-07-28 14:53:03 -0700 | [diff] [blame] | 35 | def requires_root(func): |
| 36 | def wrapper(self, *args): |
| 37 | if self.device.get_prop('ro.debuggable') != '1': |
| 38 | raise unittest.SkipTest('requires rootable build') |
| 39 | |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 40 | was_root = self.device.shell(['id', '-un'])[0].strip() == 'root' |
Dan Albert | e2b4a5f | 2015-07-28 14:53:03 -0700 | [diff] [blame] | 41 | if not was_root: |
| 42 | self.device.root() |
| 43 | self.device.wait() |
| 44 | |
| 45 | try: |
| 46 | func(self, *args) |
| 47 | finally: |
| 48 | if not was_root: |
| 49 | self.device.unroot() |
| 50 | self.device.wait() |
| 51 | |
| 52 | return wrapper |
| 53 | |
| 54 | |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 55 | class GetDeviceTest(unittest.TestCase): |
| 56 | def setUp(self): |
| 57 | self.android_serial = os.getenv('ANDROID_SERIAL') |
Spencer Low | 3e7feda | 2015-07-30 01:19:52 -0700 | [diff] [blame] | 58 | if 'ANDROID_SERIAL' in os.environ: |
| 59 | del os.environ['ANDROID_SERIAL'] |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 60 | |
| 61 | def tearDown(self): |
Spencer Low | 3e7feda | 2015-07-30 01:19:52 -0700 | [diff] [blame] | 62 | if self.android_serial is not None: |
| 63 | os.environ['ANDROID_SERIAL'] = self.android_serial |
| 64 | else: |
| 65 | if 'ANDROID_SERIAL' in os.environ: |
| 66 | del os.environ['ANDROID_SERIAL'] |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 67 | |
| 68 | @mock.patch('adb.device.get_devices') |
| 69 | def test_explicit(self, mock_get_devices): |
| 70 | mock_get_devices.return_value = ['foo', 'bar'] |
| 71 | device = adb.get_device('foo') |
| 72 | self.assertEqual(device.serial, 'foo') |
| 73 | |
| 74 | @mock.patch('adb.device.get_devices') |
| 75 | def test_from_env(self, mock_get_devices): |
| 76 | mock_get_devices.return_value = ['foo', 'bar'] |
| 77 | os.environ['ANDROID_SERIAL'] = 'foo' |
| 78 | device = adb.get_device() |
| 79 | self.assertEqual(device.serial, 'foo') |
| 80 | |
| 81 | @mock.patch('adb.device.get_devices') |
| 82 | def test_arg_beats_env(self, mock_get_devices): |
| 83 | mock_get_devices.return_value = ['foo', 'bar'] |
| 84 | os.environ['ANDROID_SERIAL'] = 'bar' |
| 85 | device = adb.get_device('foo') |
| 86 | self.assertEqual(device.serial, 'foo') |
| 87 | |
| 88 | @mock.patch('adb.device.get_devices') |
| 89 | def test_no_such_device(self, mock_get_devices): |
| 90 | mock_get_devices.return_value = ['foo', 'bar'] |
| 91 | self.assertRaises(adb.DeviceNotFoundError, adb.get_device, ['baz']) |
| 92 | |
| 93 | os.environ['ANDROID_SERIAL'] = 'baz' |
| 94 | self.assertRaises(adb.DeviceNotFoundError, adb.get_device) |
| 95 | |
| 96 | @mock.patch('adb.device.get_devices') |
| 97 | def test_unique_device(self, mock_get_devices): |
| 98 | mock_get_devices.return_value = ['foo'] |
| 99 | device = adb.get_device() |
| 100 | self.assertEqual(device.serial, 'foo') |
| 101 | |
| 102 | @mock.patch('adb.device.get_devices') |
| 103 | def test_no_unique_device(self, mock_get_devices): |
| 104 | mock_get_devices.return_value = ['foo', 'bar'] |
| 105 | self.assertRaises(adb.NoUniqueDeviceError, adb.get_device) |
| 106 | |
| 107 | |
| 108 | class DeviceTest(unittest.TestCase): |
| 109 | def setUp(self): |
| 110 | self.device = adb.get_device() |
| 111 | |
| 112 | |
| 113 | class ShellTest(DeviceTest): |
| 114 | def test_cat(self): |
| 115 | """Check that we can at least cat a file.""" |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 116 | out = self.device.shell(['cat', '/proc/uptime'])[0].strip() |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 117 | elements = out.split() |
| 118 | self.assertEqual(len(elements), 2) |
| 119 | |
| 120 | uptime, idle = elements |
| 121 | self.assertGreater(float(uptime), 0.0) |
| 122 | self.assertGreater(float(idle), 0.0) |
| 123 | |
| 124 | def test_throws_on_failure(self): |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 125 | self.assertRaises(adb.ShellError, self.device.shell, ['false']) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 126 | |
| 127 | def test_output_not_stripped(self): |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 128 | out = self.device.shell(['echo', 'foo'])[0] |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 129 | self.assertEqual(out, 'foo' + self.device.linesep) |
| 130 | |
| 131 | def test_shell_nocheck_failure(self): |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 132 | rc, out, _ = self.device.shell_nocheck(['false']) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 133 | self.assertNotEqual(rc, 0) |
| 134 | self.assertEqual(out, '') |
| 135 | |
| 136 | def test_shell_nocheck_output_not_stripped(self): |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 137 | rc, out, _ = self.device.shell_nocheck(['echo', 'foo']) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 138 | self.assertEqual(rc, 0) |
| 139 | self.assertEqual(out, 'foo' + self.device.linesep) |
| 140 | |
| 141 | def test_can_distinguish_tricky_results(self): |
| 142 | # If result checking on ADB shell is naively implemented as |
| 143 | # `adb shell <cmd>; echo $?`, we would be unable to distinguish the |
| 144 | # output from the result for a cmd of `echo -n 1`. |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 145 | rc, out, _ = self.device.shell_nocheck(['echo', '-n', '1']) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 146 | self.assertEqual(rc, 0) |
| 147 | self.assertEqual(out, '1') |
| 148 | |
| 149 | def test_line_endings(self): |
| 150 | """Ensure that line ending translation is not happening in the pty. |
| 151 | |
| 152 | Bug: http://b/19735063 |
| 153 | """ |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 154 | output = self.device.shell(['uname'])[0] |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 155 | self.assertEqual(output, 'Linux' + self.device.linesep) |
| 156 | |
David Pursell | d4093f1 | 2015-08-10 12:52:16 -0700 | [diff] [blame] | 157 | def test_pty_logic(self): |
| 158 | """Verify PTY logic for shells. |
| 159 | |
| 160 | Interactive shells should use a PTY, non-interactive should not. |
| 161 | |
| 162 | Bug: http://b/21215503 |
| 163 | """ |
| 164 | proc = subprocess.Popen( |
| 165 | self.device.adb_cmd + ['shell'], stdin=subprocess.PIPE, |
| 166 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 167 | # [ -t 0 ] is used (rather than `tty`) to provide portability. This |
| 168 | # gives an exit code of 0 iff stdin is connected to a terminal. |
| 169 | # |
| 170 | # Closing host-side stdin doesn't currently trigger the interactive |
| 171 | # shell to exit so we need to explicitly add an exit command to |
| 172 | # close the session from the device side, and append \n to complete |
| 173 | # the interactive command. |
| 174 | result = proc.communicate('[ -t 0 ]; echo x$?; exit 0\n')[0] |
| 175 | partition = result.rpartition('x') |
| 176 | self.assertEqual(partition[1], 'x') |
| 177 | self.assertEqual(int(partition[2]), 0) |
| 178 | |
| 179 | exit_code = self.device.shell_nocheck(['[ -t 0 ]'])[0] |
| 180 | self.assertEqual(exit_code, 1) |
| 181 | |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 182 | def test_shell_protocol(self): |
| 183 | """Tests the shell protocol on the device. |
| 184 | |
| 185 | If the device supports shell protocol, this gives us the ability |
| 186 | to separate stdout/stderr and return the exit code directly. |
| 187 | |
| 188 | Bug: http://b/19734861 |
| 189 | """ |
| 190 | if self.device.SHELL_PROTOCOL_FEATURE not in self.device.features: |
| 191 | raise unittest.SkipTest('shell protocol unsupported on this device') |
| 192 | result = self.device.shell_nocheck( |
| 193 | shlex.split('echo foo; echo bar >&2; exit 17')) |
| 194 | |
| 195 | self.assertEqual(17, result[0]) |
| 196 | self.assertEqual('foo' + self.device.linesep, result[1]) |
| 197 | self.assertEqual('bar' + self.device.linesep, result[2]) |
| 198 | |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 199 | |
| 200 | class ArgumentEscapingTest(DeviceTest): |
| 201 | def test_shell_escaping(self): |
| 202 | """Make sure that argument escaping is somewhat sane.""" |
| 203 | |
| 204 | # http://b/19734868 |
| 205 | # Note that this actually matches ssh(1)'s behavior --- it's |
| 206 | # converted to `sh -c echo hello; echo world` which sh interprets |
| 207 | # as `sh -c echo` (with an argument to that shell of "hello"), |
| 208 | # and then `echo world` back in the first shell. |
| 209 | result = self.device.shell( |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 210 | shlex.split("sh -c 'echo hello; echo world'"))[0] |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 211 | result = result.splitlines() |
| 212 | self.assertEqual(['', 'world'], result) |
| 213 | # If you really wanted "hello" and "world", here's what you'd do: |
| 214 | result = self.device.shell( |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 215 | shlex.split(r'echo hello\;echo world'))[0].splitlines() |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 216 | self.assertEqual(['hello', 'world'], result) |
| 217 | |
| 218 | # http://b/15479704 |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 219 | result = self.device.shell(shlex.split("'true && echo t'"))[0].strip() |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 220 | self.assertEqual('t', result) |
| 221 | result = self.device.shell( |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 222 | shlex.split("sh -c 'true && echo t'"))[0].strip() |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 223 | self.assertEqual('t', result) |
| 224 | |
| 225 | # http://b/20564385 |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 226 | result = self.device.shell(shlex.split('FOO=a BAR=b echo t'))[0].strip() |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 227 | self.assertEqual('t', result) |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 228 | result = self.device.shell( |
| 229 | shlex.split(r'echo -n 123\;uname'))[0].strip() |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 230 | self.assertEqual('123Linux', result) |
| 231 | |
| 232 | def test_install_argument_escaping(self): |
| 233 | """Make sure that install argument escaping works.""" |
| 234 | # http://b/20323053 |
Spencer Low | d8cce18 | 2015-08-28 01:07:30 -0700 | [diff] [blame] | 235 | tf = tempfile.NamedTemporaryFile('wb', suffix='-text;ls;1.apk', |
| 236 | delete=False) |
| 237 | tf.close() |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 238 | self.assertIn("-text;ls;1.apk", self.device.install(tf.name)) |
Spencer Low | d8cce18 | 2015-08-28 01:07:30 -0700 | [diff] [blame] | 239 | os.remove(tf.name) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 240 | |
| 241 | # http://b/3090932 |
Spencer Low | d8cce18 | 2015-08-28 01:07:30 -0700 | [diff] [blame] | 242 | tf = tempfile.NamedTemporaryFile('wb', suffix="-Live Hold'em.apk", |
| 243 | delete=False) |
| 244 | tf.close() |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 245 | self.assertIn("-Live Hold'em.apk", self.device.install(tf.name)) |
Spencer Low | d8cce18 | 2015-08-28 01:07:30 -0700 | [diff] [blame] | 246 | os.remove(tf.name) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 247 | |
| 248 | |
| 249 | class RootUnrootTest(DeviceTest): |
| 250 | def _test_root(self): |
| 251 | message = self.device.root() |
| 252 | if 'adbd cannot run as root in production builds' in message: |
| 253 | return |
| 254 | self.device.wait() |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 255 | self.assertEqual('root', self.device.shell(['id', '-un'])[0].strip()) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 256 | |
| 257 | def _test_unroot(self): |
| 258 | self.device.unroot() |
| 259 | self.device.wait() |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 260 | self.assertEqual('shell', self.device.shell(['id', '-un'])[0].strip()) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 261 | |
| 262 | def test_root_unroot(self): |
| 263 | """Make sure that adb root and adb unroot work, using id(1).""" |
Dan Albert | e2b4a5f | 2015-07-28 14:53:03 -0700 | [diff] [blame] | 264 | if self.device.get_prop('ro.debuggable') != '1': |
| 265 | raise unittest.SkipTest('requires rootable build') |
| 266 | |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 267 | original_user = self.device.shell(['id', '-un'])[0].strip() |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 268 | try: |
| 269 | if original_user == 'root': |
| 270 | self._test_unroot() |
| 271 | self._test_root() |
| 272 | elif original_user == 'shell': |
| 273 | self._test_root() |
| 274 | self._test_unroot() |
| 275 | finally: |
| 276 | if original_user == 'root': |
| 277 | self.device.root() |
| 278 | else: |
| 279 | self.device.unroot() |
| 280 | self.device.wait() |
| 281 | |
| 282 | |
| 283 | class TcpIpTest(DeviceTest): |
| 284 | def test_tcpip_failure_raises(self): |
| 285 | """adb tcpip requires a port. |
| 286 | |
| 287 | Bug: http://b/22636927 |
| 288 | """ |
| 289 | self.assertRaises( |
| 290 | subprocess.CalledProcessError, self.device.tcpip, '') |
| 291 | self.assertRaises( |
| 292 | subprocess.CalledProcessError, self.device.tcpip, 'foo') |
| 293 | |
| 294 | |
Dan Albert | e2b4a5f | 2015-07-28 14:53:03 -0700 | [diff] [blame] | 295 | class SystemPropertiesTest(DeviceTest): |
| 296 | def test_get_prop(self): |
| 297 | self.assertEqual(self.device.get_prop('init.svc.adbd'), 'running') |
| 298 | |
| 299 | @requires_root |
| 300 | def test_set_prop(self): |
| 301 | prop_name = 'foo.bar' |
| 302 | self.device.shell(['setprop', prop_name, '""']) |
| 303 | |
| 304 | self.device.set_prop(prop_name, 'qux') |
| 305 | self.assertEqual( |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 306 | self.device.shell(['getprop', prop_name])[0].strip(), 'qux') |
Dan Albert | e2b4a5f | 2015-07-28 14:53:03 -0700 | [diff] [blame] | 307 | |
| 308 | |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 309 | def compute_md5(string): |
| 310 | hsh = hashlib.md5() |
| 311 | hsh.update(string) |
| 312 | return hsh.hexdigest() |
| 313 | |
| 314 | |
| 315 | def get_md5_prog(device): |
| 316 | """Older platforms (pre-L) had the name md5 rather than md5sum.""" |
| 317 | try: |
| 318 | device.shell(['md5sum', '/proc/uptime']) |
| 319 | return 'md5sum' |
| 320 | except subprocess.CalledProcessError: |
| 321 | return 'md5' |
| 322 | |
| 323 | |
| 324 | class HostFile(object): |
| 325 | def __init__(self, handle, checksum): |
| 326 | self.handle = handle |
| 327 | self.checksum = checksum |
| 328 | self.full_path = handle.name |
| 329 | self.base_name = os.path.basename(self.full_path) |
| 330 | |
| 331 | |
| 332 | class DeviceFile(object): |
| 333 | def __init__(self, checksum, full_path): |
| 334 | self.checksum = checksum |
| 335 | self.full_path = full_path |
| 336 | self.base_name = posixpath.basename(self.full_path) |
| 337 | |
| 338 | |
| 339 | def make_random_host_files(in_dir, num_files): |
| 340 | min_size = 1 * (1 << 10) |
| 341 | max_size = 16 * (1 << 10) |
| 342 | |
| 343 | files = [] |
| 344 | for _ in xrange(num_files): |
| 345 | file_handle = tempfile.NamedTemporaryFile(dir=in_dir, delete=False) |
| 346 | |
| 347 | size = random.randrange(min_size, max_size, 1024) |
| 348 | rand_str = os.urandom(size) |
| 349 | file_handle.write(rand_str) |
| 350 | file_handle.flush() |
| 351 | file_handle.close() |
| 352 | |
| 353 | md5 = compute_md5(rand_str) |
| 354 | files.append(HostFile(file_handle, md5)) |
| 355 | return files |
| 356 | |
| 357 | |
| 358 | def make_random_device_files(device, in_dir, num_files): |
| 359 | min_size = 1 * (1 << 10) |
| 360 | max_size = 16 * (1 << 10) |
| 361 | |
| 362 | files = [] |
| 363 | for file_num in xrange(num_files): |
| 364 | size = random.randrange(min_size, max_size, 1024) |
| 365 | |
| 366 | base_name = 'device_tmpfile' + str(file_num) |
Spencer Low | 3e7feda | 2015-07-30 01:19:52 -0700 | [diff] [blame] | 367 | full_path = posixpath.join(in_dir, base_name) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 368 | |
| 369 | device.shell(['dd', 'if=/dev/urandom', 'of={}'.format(full_path), |
| 370 | 'bs={}'.format(size), 'count=1']) |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 371 | dev_md5, _ = device.shell([get_md5_prog(device), full_path])[0].split() |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 372 | |
| 373 | files.append(DeviceFile(dev_md5, full_path)) |
| 374 | return files |
| 375 | |
| 376 | |
| 377 | class FileOperationsTest(DeviceTest): |
| 378 | SCRATCH_DIR = '/data/local/tmp' |
| 379 | DEVICE_TEMP_FILE = SCRATCH_DIR + '/adb_test_file' |
| 380 | DEVICE_TEMP_DIR = SCRATCH_DIR + '/adb_test_dir' |
| 381 | |
| 382 | def _test_push(self, local_file, checksum): |
| 383 | self.device.shell(['rm', '-rf', self.DEVICE_TEMP_FILE]) |
Elliott Hughes | 3841a9f | 2015-08-03 13:58:49 -0700 | [diff] [blame] | 384 | self.device.push(local=local_file, remote=self.DEVICE_TEMP_FILE) |
| 385 | dev_md5, _ = self.device.shell([get_md5_prog(self.device), |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 386 | self.DEVICE_TEMP_FILE])[0].split() |
Elliott Hughes | 3841a9f | 2015-08-03 13:58:49 -0700 | [diff] [blame] | 387 | self.assertEqual(checksum, dev_md5) |
| 388 | self.device.shell(['rm', '-f', self.DEVICE_TEMP_FILE]) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 389 | |
| 390 | def test_push(self): |
| 391 | """Push a randomly generated file to specified device.""" |
| 392 | kbytes = 512 |
| 393 | tmp = tempfile.NamedTemporaryFile(mode='wb', delete=False) |
Elliott Hughes | 3841a9f | 2015-08-03 13:58:49 -0700 | [diff] [blame] | 394 | rand_str = os.urandom(1024 * kbytes) |
| 395 | tmp.write(rand_str) |
| 396 | tmp.close() |
| 397 | self._test_push(tmp.name, compute_md5(rand_str)) |
| 398 | os.remove(tmp.name) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 399 | |
| 400 | # TODO: write push directory test. |
| 401 | |
| 402 | def _test_pull(self, remote_file, checksum): |
| 403 | tmp_write = tempfile.NamedTemporaryFile(mode='wb', delete=False) |
Elliott Hughes | 3841a9f | 2015-08-03 13:58:49 -0700 | [diff] [blame] | 404 | tmp_write.close() |
| 405 | self.device.pull(remote=remote_file, local=tmp_write.name) |
| 406 | with open(tmp_write.name, 'rb') as tmp_read: |
| 407 | host_contents = tmp_read.read() |
| 408 | host_md5 = compute_md5(host_contents) |
| 409 | self.assertEqual(checksum, host_md5) |
| 410 | os.remove(tmp_write.name) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 411 | |
| 412 | def test_pull(self): |
| 413 | """Pull a randomly generated file from specified device.""" |
| 414 | kbytes = 512 |
| 415 | self.device.shell(['rm', '-rf', self.DEVICE_TEMP_FILE]) |
Elliott Hughes | 3841a9f | 2015-08-03 13:58:49 -0700 | [diff] [blame] | 416 | cmd = ['dd', 'if=/dev/urandom', |
| 417 | 'of={}'.format(self.DEVICE_TEMP_FILE), 'bs=1024', |
| 418 | 'count={}'.format(kbytes)] |
| 419 | self.device.shell(cmd) |
| 420 | dev_md5, _ = self.device.shell( |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 421 | [get_md5_prog(self.device), self.DEVICE_TEMP_FILE])[0].split() |
Elliott Hughes | 3841a9f | 2015-08-03 13:58:49 -0700 | [diff] [blame] | 422 | self._test_pull(self.DEVICE_TEMP_FILE, dev_md5) |
| 423 | self.device.shell_nocheck(['rm', self.DEVICE_TEMP_FILE]) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 424 | |
| 425 | def test_pull_dir(self): |
| 426 | """Pull a randomly generated directory of files from the device.""" |
| 427 | host_dir = tempfile.mkdtemp() |
Elliott Hughes | 3841a9f | 2015-08-03 13:58:49 -0700 | [diff] [blame] | 428 | self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR]) |
| 429 | self.device.shell(['mkdir', '-p', self.DEVICE_TEMP_DIR]) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 430 | |
Elliott Hughes | 3841a9f | 2015-08-03 13:58:49 -0700 | [diff] [blame] | 431 | # Populate device directory with random files. |
| 432 | temp_files = make_random_device_files( |
| 433 | self.device, in_dir=self.DEVICE_TEMP_DIR, num_files=32) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 434 | |
Elliott Hughes | 3841a9f | 2015-08-03 13:58:49 -0700 | [diff] [blame] | 435 | self.device.pull(remote=self.DEVICE_TEMP_DIR, local=host_dir) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 436 | |
Elliott Hughes | 3841a9f | 2015-08-03 13:58:49 -0700 | [diff] [blame] | 437 | for temp_file in temp_files: |
| 438 | host_path = os.path.join(host_dir, temp_file.base_name) |
| 439 | with open(host_path, 'rb') as host_file: |
| 440 | host_md5 = compute_md5(host_file.read()) |
| 441 | self.assertEqual(host_md5, temp_file.checksum) |
| 442 | |
| 443 | self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR]) |
| 444 | if host_dir is not None: |
| 445 | shutil.rmtree(host_dir) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 446 | |
| 447 | def test_sync(self): |
| 448 | """Sync a randomly generated directory of files to specified device.""" |
| 449 | base_dir = tempfile.mkdtemp() |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 450 | |
Elliott Hughes | 3841a9f | 2015-08-03 13:58:49 -0700 | [diff] [blame] | 451 | # Create mirror device directory hierarchy within base_dir. |
| 452 | full_dir_path = base_dir + self.DEVICE_TEMP_DIR |
| 453 | os.makedirs(full_dir_path) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 454 | |
Elliott Hughes | 3841a9f | 2015-08-03 13:58:49 -0700 | [diff] [blame] | 455 | # Create 32 random files within the host mirror. |
| 456 | temp_files = make_random_host_files(in_dir=full_dir_path, num_files=32) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 457 | |
Elliott Hughes | 3841a9f | 2015-08-03 13:58:49 -0700 | [diff] [blame] | 458 | # Clean up any trash on the device. |
| 459 | device = adb.get_device(product=base_dir) |
| 460 | device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR]) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 461 | |
Elliott Hughes | 3841a9f | 2015-08-03 13:58:49 -0700 | [diff] [blame] | 462 | device.sync('data') |
| 463 | |
| 464 | # Confirm that every file on the device mirrors that on the host. |
| 465 | for temp_file in temp_files: |
| 466 | device_full_path = posixpath.join(self.DEVICE_TEMP_DIR, |
| 467 | temp_file.base_name) |
| 468 | dev_md5, _ = device.shell( |
David Pursell | 606835a | 2015-09-08 17:17:02 -0700 | [diff] [blame] | 469 | [get_md5_prog(self.device), device_full_path])[0].split() |
Elliott Hughes | 3841a9f | 2015-08-03 13:58:49 -0700 | [diff] [blame] | 470 | self.assertEqual(temp_file.checksum, dev_md5) |
| 471 | |
| 472 | self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR]) |
| 473 | shutil.rmtree(base_dir + self.DEVICE_TEMP_DIR) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 474 | |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 475 | def test_unicode_paths(self): |
| 476 | """Ensure that we can support non-ASCII paths, even on Windows.""" |
Spencer Low | de4505f | 2015-08-27 20:58:29 -0700 | [diff] [blame] | 477 | name = u'로보카 폴리' |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 478 | |
| 479 | ## push. |
Spencer Low | de4505f | 2015-08-27 20:58:29 -0700 | [diff] [blame] | 480 | tf = tempfile.NamedTemporaryFile('wb', suffix=name, delete=False) |
| 481 | tf.close() |
| 482 | self.device.push(tf.name, u'/data/local/tmp/adb-test-{}'.format(name)) |
| 483 | os.remove(tf.name) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 484 | self.device.shell(['rm', '-f', '/data/local/tmp/adb-test-*']) |
| 485 | |
| 486 | # pull. |
Spencer Low | de4505f | 2015-08-27 20:58:29 -0700 | [diff] [blame] | 487 | cmd = ['touch', u'"/data/local/tmp/adb-test-{}"'.format(name)] |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 488 | self.device.shell(cmd) |
| 489 | |
Spencer Low | de4505f | 2015-08-27 20:58:29 -0700 | [diff] [blame] | 490 | tf = tempfile.NamedTemporaryFile('wb', suffix=name, delete=False) |
| 491 | tf.close() |
| 492 | self.device.pull(u'/data/local/tmp/adb-test-{}'.format(name), tf.name) |
| 493 | os.remove(tf.name) |
| 494 | self.device.shell(['rm', '-f', '/data/local/tmp/adb-test-*']) |
Dan Albert | 8e1fdd7 | 2015-07-24 17:08:33 -0700 | [diff] [blame] | 495 | |
| 496 | |
| 497 | def main(): |
| 498 | random.seed(0) |
| 499 | if len(adb.get_devices()) > 0: |
| 500 | suite = unittest.TestLoader().loadTestsFromName(__name__) |
| 501 | unittest.TextTestRunner(verbosity=3).run(suite) |
| 502 | else: |
| 503 | print('Test suite must be run with attached devices') |
| 504 | |
| 505 | |
| 506 | if __name__ == '__main__': |
| 507 | main() |