def _run_adb_command(self, params, timeout): """ :type params: :param list[str] params: list of ADB params (split by spaces) :param float timeout: ADB call timeout :return str: ADB command output :raises TimeoutExpired is case the ADB command was timed out :raises AdbConnectionError is case of ADB connection error """ # Make sure enough time passed since the last ASB call time_since_last_adb_call = time.time() - self.last_adb_call_time if time_since_last_adb_call < ADB_CALL_MINIMAL_INTERVAL: time.sleep(ADB_CALL_MINIMAL_INTERVAL - time_since_last_adb_call) if self.debug: print(time.time(), end=" - ") print(["adb"] + params) p = subprocess.Popen(["adb"] + params, stdout=subprocess.PIPE, stderr=subprocess.PIPE) try: if timeout is None: timeout = 60 output, error = p.communicate(timeout=timeout) finally: self.last_adb_call_time = time.time() if p.returncode != 0: raise CommandFailedError("adb returned with non-zero error code", stdout=output, stderr=error, returncode=p.returncode) return output.strip("\r\n")
def test_pull_from_non_existent(self): err = CommandFailedError() err.stderr = "adb: error: failed to stat remote object '/sdcard/tmp_file.tm': No such file or directory\n" self.conn.adb.side_effect = err self.assertRaises(RemoteFileNotFound, self.device_utils.pull, '/sdcard/tmp_file.tm', '/home/buga/tmp_dir/')
def test_push_no_permissions(self): err = CommandFailedError() err.stderr = "[100%] /tmp_file.tmp\nadb: error: failed to copy '/home/buga/tmp_dir/tmp_file.tmp' to '/tmp_file.tmp': remote couldn't create file: Read-only file system\n/home/buga/tmp_dir/tmp_file.tmp: 0 files pushed. 0.0 MB/s (44 bytes in 0.005s)\n" self.conn.adb.side_effect = err self.assertRaises(WrongPermissions, self.device_utils.push, '/home/buga/tmp_dir/tmp_file.tmp', '/')
def test_push_to_non_existent(self): err = CommandFailedError() err.stderr = "[100%] /sdcard23/\nadb: error: failed to copy '/home/buga/tmp_dir/tmp_file.tmp' to '/sdcard23/': remote couldn't create file: Is a directory\n/home/buga/tmp_dir/tmp_file.tmp: 0 files pushed. 0.0 MB/s (44 bytes in 0.003s)\n" self.conn.adb.side_effect = err self.assertRaises(RemoteFileNotFound, self.device_utils.push, '/home/buga/tmp_dir/tmp_file.tmp', '/sdcard23/')
def test_push_from_non_existent(self): err = CommandFailedError() err.stderr = "adb: error: cannot stat '/home/buga/tmp_dir/tmp_file.tm': No such file or directory\n" self.conn.adb.side_effect = err self.assertRaises(LocalFileNotFound, self.device_utils.push, '/home/buga/tmp_dir/tmp_file.tm', '/sdcard/')
def test_get_device_name_unsupported(self): err = CommandFailedError() err.stderr = 'rm: /data/local/tmp/devicename: No such file or directory\n' self.conn.adb.side_effect = err self.assertRaises(OperationUnsupported, self.device_utils.get_device_name)
def test_ls_doesnt_exist(self): err = CommandFailedError() err.stderr = 'ls: /abc: No such file or directory\n' self.conn.adb.side_effect = err self.assertRaises(RemoteFileNotFound, self.device_utils.ls, '/abc')
def side_effect(*args, **kwargs): if args[0] == command: raise CommandFailedError(command) return None
def test_remove_no_permissions(self): err = CommandFailedError() err.stderr = 'rm: /system/tmp_file: No such file or directory\n' self.conn.adb.side_effect = err self.assertRaises(WrongPermissions, self.device_utils.remove, '/system/tmp_file')
def test_touch_file_no_permissions(self): err = CommandFailedError() err.stderr = "touch: '/system/tmp_file': Read-only file system\n" self.conn.adb.side_effect = err self.assertRaises(WrongPermissions, self.device_utils.touch_file, '/system/tmp_file')
def test_touch_file_in_non_existent(self): err = CommandFailedError() err.stderr = "touch: '/sdcard/notexist/tmp_file2': No such file or directory\n" self.conn.adb.side_effect = err self.assertRaises(RemoteFileNotFound, self.device_utils.touch_file, '/sdcard/notexist/tmp_file2')
def test_mkdir_no_permissions(self): err = CommandFailedError() err.stderr = "mkdir: '/system/tmp_dir': Read-only file system\n" self.conn.adb.side_effect = err self.assertRaises(WrongPermissions, self.device_utils.mkdir, '/system/tmp_dir')
def test_mkdir_in_non_existent(self): err = CommandFailedError() err.stderr = "mkdir: '/sdcard/tmp_dir/sub/dir': No such file or directory\n" self.conn.adb.side_effect = err self.assertRaises(RemoteFileNotFound, self.device_utils.mkdir, '/sdcard/tmp_dir/sub/dir')
def test_mkdir_already_exists(self): err = CommandFailedError() err.stderr = "mkdir: '/sdcard/tmp_dir/': File exists\n" self.conn.adb.side_effect = err self.assertRaises(FileAlreadyExists, self.device_utils.mkdir, '/sdcard/tmp_dir')
def test_pull_to_non_existent(self): err = CommandFailedError() err.stderr = "adb: error: cannot create '/home/buga/tmp_dir2/': Is a directory\n" self.conn.adb.side_effect = err self.assertRaises(LocalFileNotFound, self.device_utils.pull, '/sdcard/tmp_file.tmp', '/home/buga/tmp_dir2/')
def test_pull_no_permissions(self): err = CommandFailedError() err.stderr = "adb: error: cannot create '/home/buga/tmp_dir/no_perm/tmp_file.tmp': Permission denied\n" self.conn.adb.side_effect = err self.assertRaises(WrongPermissions, self.device_utils.pull, '/sdcard/tmp_file.tmp', '/home/buga/tmp_dir/no_perm')
def test_ls_ok(self): stdout = dedent("""\ total 32216 drwxr-xr-x 2 root shell 8192 2008-12-31 20:30 . -rwxr-xr-x 1 root shell 47760 2008-12-31 20:30 ATFWD-daemon -rwxr-xr-x 1 root shell 61232 2008-12-31 20:30 BCM4345C0_003.001.025.0138.0222.HCD -rwxr-xr-x 1 root shell 52870 2008-12-31 20:30 BCM4358A3_001.004.015.0076.0130_0x66_ORC.HCD -rwxr-xr-x 1 root shell 22720 2008-12-31 20:30 WifiLogger_app lrwxr-xr-x 1 root shell 6 2008-12-31 20:30 acpi -> toybox -rwxr-xr-x 1 root shell 6264 2008-12-31 20:30 adsprpcd -rwxr-xr-x 1 root shell 210 2008-12-31 20:30 am lrwxr-xr-x 1 root shell 13 2008-12-31 20:30 app_process -> app_process64 lrwxr-xr-x 1 root shell 6 2008-12-31 20:30 xargs -> toybox lrwxr-xr-x 1 root shell 6 2008-12-31 20:30 xxd -> toybox lrwxr-xr-x 1 root shell 6 2008-12-31 20:30 yes -> toybox -rwxr-xr-x 1 root shell 67704 2008-12-31 20:30 yuvtool drwxr-xr-x 19 root root 4096 1970-01-01 02:00 .. """) stderr = dedent("""\ ls: /system/bin/PktRspTest: Permission denied ls: /system/bin/StoreKeybox: Permission denied ls: /system/bin/xtwifi-inet-agent: Permission denied """) self.conn.adb.return_value = stdout ls = self.device_utils.ls('/system/bin') self.assertEqual(len([x for x in ls if x['links_to']]), 5) self.assertEqual(len(ls), 14) err = CommandFailedError() err.stdout = stdout err.stderr = stderr self.conn.adb.side_effect = err ls = self.device_utils.ls('/system/bin') self.assertEqual(len([x for x in ls if x['links_to']]), 5) self.assertEqual(len([x for x in ls if x['permissions'] is None]), 3) self.assertEqual(len(ls), 17) self.assertIn( dict(permissions='drwxr-xr-x', n_links=2, owner='root', group='shell', size=8192, modified=datetime.datetime(2008, 12, 31, 20, 30), name='.', links_to=None), ls) self.assertIn( dict(permissions='lrwxr-xr-x', n_links=1, owner='root', group='shell', size=6, modified=datetime.datetime(2008, 12, 31, 20, 30), name='acpi', links_to='toybox'), ls) self.assertIn( dict(permissions=None, n_links=None, owner=None, group=None, size=None, modified=None, name='PktRspTest', links_to=None), ls)