From 1d8deb1bb8a1a1d1a364d3ebc420c0886ad37eac Mon Sep 17 00:00:00 2001 From: Todd Volkert Date: Thu, 15 Aug 2019 17:59:37 -0700 Subject: [PATCH] Handle case of a connected unpaired iOS device (#38629) Fixes https://github.com/flutter/flutter/issues/36789 Fixes https://github.com/flutter/flutter/issues/37000 https://github.com/flutter/flutter/issues/36524 https://github.com/flutter/flutter/issues/24600 --- .../flutter_tools/lib/src/ios/devices.dart | 4 ++ packages/flutter_tools/lib/src/ios/mac.dart | 68 ++++++++++++++++++- .../test/general.shard/ios/devices_test.dart | 2 +- .../test/general.shard/ios/mac_test.dart | 63 +++++++++++++++++ 4 files changed, 134 insertions(+), 3 deletions(-) diff --git a/packages/flutter_tools/lib/src/ios/devices.dart b/packages/flutter_tools/lib/src/ios/devices.dart index f177c9f99f..51d8f85cbc 100644 --- a/packages/flutter_tools/lib/src/ios/devices.dart +++ b/packages/flutter_tools/lib/src/ios/devices.dart @@ -20,6 +20,7 @@ import '../device.dart'; import '../globals.dart'; import '../project.dart'; import '../protocol_discovery.dart'; +import '../reporting/reporting.dart'; import 'code_signing.dart'; import 'ios_workflow.dart'; import 'mac.dart'; @@ -185,6 +186,9 @@ class IOSDevice extends Device { } on IOSDeviceNotFoundError catch (error) { // Unable to find device with given udid. Possibly a network device. printTrace('Error getting attached iOS device: $error'); + } on IOSDeviceNotTrustedError catch (error) { + printTrace('Error getting attached iOS device information: $error'); + UsageEvent('device', 'ios-trust-failure').send(); } } return devices; diff --git a/packages/flutter_tools/lib/src/ios/mac.dart b/packages/flutter_tools/lib/src/ios/mac.dart index d2e975f798..83365b17db 100644 --- a/packages/flutter_tools/lib/src/ios/mac.dart +++ b/packages/flutter_tools/lib/src/ios/mac.dart @@ -34,7 +34,7 @@ IMobileDevice get iMobileDevice => context.get(); /// Specialized exception for expected situations where the ideviceinfo /// tool responds with exit code 255 / 'No device found' message class IOSDeviceNotFoundError implements Exception { - IOSDeviceNotFoundError(this.message); + const IOSDeviceNotFoundError(this.message); final String message; @@ -42,6 +42,56 @@ class IOSDeviceNotFoundError implements Exception { String toString() => message; } +/// Exception representing an attempt to find information on an iOS device +/// that failed because the user had not paired the device with the host yet. +class IOSDeviceNotTrustedError implements Exception { + const IOSDeviceNotTrustedError(this.message, this.lockdownCode); + + /// The error message to show to the user. + final String message; + + /// The associated `lockdownd` error code. + final LockdownReturnCode lockdownCode; + + @override + String toString() => '$message (lockdownd error code ${lockdownCode.code})'; +} + +/// Class specifying possible return codes from `lockdownd`. +/// +/// This contains only a subset of the return codes that `lockdownd` can return, +/// as we only care about a limited subset. These values should be kept in sync with +/// https://github.com/libimobiledevice/libimobiledevice/blob/26373b3/include/libimobiledevice/lockdown.h#L37 +class LockdownReturnCode { + const LockdownReturnCode._(this.code); + + /// Creates a new [LockdownReturnCode] from the specified OS exit code. + /// + /// If the [code] maps to one of the known codes, a `const` instance will be + /// returned. + factory LockdownReturnCode.fromCode(int code) { + final Map knownCodes = { + pairingDialogResponsePending.code: pairingDialogResponsePending, + invalidHostId.code: invalidHostId, + }; + + return knownCodes.containsKey(code) ? knownCodes[code] : LockdownReturnCode._(code); + } + + /// The OS exit code. + final int code; + + /// Error code indicating that the pairing dialog has been shown to the user, + /// and the user has not yet responded as to whether to trust the host. + static const LockdownReturnCode pairingDialogResponsePending = LockdownReturnCode._(19); + + /// Error code indicating that the host is not trusted. + /// + /// This can happen if the user explicitly says "do not trust this computer" + /// or if they revoke all trusted computers in the device settings. + static const LockdownReturnCode invalidHostId = LockdownReturnCode._(21); +} + class IMobileDevice { IMobileDevice() : _ideviceIdPath = artifacts.getArtifactPath(Artifact.ideviceId, platform: TargetPlatform.ios) @@ -159,7 +209,21 @@ class IMobileDevice { ), ); if (result.exitCode == 255 && result.stdout != null && result.stdout.contains('No device found')) - throw IOSDeviceNotFoundError('ideviceinfo could not find device:\n${result.stdout}'); + throw IOSDeviceNotFoundError('ideviceinfo could not find device:\n${result.stdout}. Try unlocking attached devices.'); + if (result.exitCode == 255 && result.stderr != null && result.stderr.contains('Could not connect to lockdownd')) { + if (result.stderr.contains('error code -${LockdownReturnCode.pairingDialogResponsePending.code}')) { + throw const IOSDeviceNotTrustedError( + 'Device info unavailable. Is the device asking to "Trust This Computer?"', + LockdownReturnCode.pairingDialogResponsePending, + ); + } + if (result.stderr.contains('error code -${LockdownReturnCode.invalidHostId.code}')) { + throw const IOSDeviceNotTrustedError( + 'Device info unavailable. Device pairing "trust" may have been revoked.', + LockdownReturnCode.invalidHostId, + ); + } + } if (result.exitCode != 0) throw ToolExit('ideviceinfo returned an error:\n${result.stderr}'); return result.stdout.trim(); diff --git a/packages/flutter_tools/test/general.shard/ios/devices_test.dart b/packages/flutter_tools/test/general.shard/ios/devices_test.dart index b1d1b4b2fb..3e31c32c71 100644 --- a/packages/flutter_tools/test/general.shard/ios/devices_test.dart +++ b/packages/flutter_tools/test/general.shard/ios/devices_test.dart @@ -371,7 +371,7 @@ f577a7903cc54959be2e34bc4f7f80b7009efcf4 when(iMobileDevice.getInfoForDevice('98206e7a4afd4aedaff06e687594e089dede3c44', 'DeviceName')) .thenAnswer((_) => Future.value('La tele me regarde')); when(iMobileDevice.getInfoForDevice('f577a7903cc54959be2e34bc4f7f80b7009efcf4', 'DeviceName')) - .thenThrow(IOSDeviceNotFoundError('Device not found')); + .thenThrow(const IOSDeviceNotFoundError('Device not found')); final List devices = await IOSDevice.getAttachedDevices(); expect(devices, hasLength(1)); expect(devices[0].id, '98206e7a4afd4aedaff06e687594e089dede3c44'); diff --git a/packages/flutter_tools/test/general.shard/ios/mac_test.dart b/packages/flutter_tools/test/general.shard/ios/mac_test.dart index c6c0735951..e284fd5054 100644 --- a/packages/flutter_tools/test/general.shard/ios/mac_test.dart +++ b/packages/flutter_tools/test/general.shard/ios/mac_test.dart @@ -113,6 +113,69 @@ void main() { Artifacts: () => mockArtifacts, }); + testUsingContext('getInfoForDevice throws IOSDeviceNotFoundError when user has not yet trusted the host', () async { + when(mockArtifacts.getArtifactPath(Artifact.ideviceinfo, platform: anyNamed('platform'))).thenReturn(ideviceInfoPath); + when(mockProcessManager.run( + [ideviceInfoPath, '-u', 'foo', '-k', 'bar'], + environment: {'DYLD_LIBRARY_PATH': libimobiledevicePath}, + )).thenAnswer((_) { + final ProcessResult result = ProcessResult( + 1, + 255, + '', + 'ERROR: Could not connect to lockdownd, error code -${LockdownReturnCode.pairingDialogResponsePending.code}', + ); + return Future.value(result); + }); + expect(() async => await iMobileDevice.getInfoForDevice('foo', 'bar'), throwsA(isInstanceOf())); + }, overrides: { + ProcessManager: () => mockProcessManager, + Cache: () => mockCache, + Artifacts: () => mockArtifacts, + }); + + testUsingContext('getInfoForDevice throws ToolExit lockdownd fails for unknown reason', () async { + when(mockArtifacts.getArtifactPath(Artifact.ideviceinfo, platform: anyNamed('platform'))).thenReturn(ideviceInfoPath); + when(mockProcessManager.run( + [ideviceInfoPath, '-u', 'foo', '-k', 'bar'], + environment: {'DYLD_LIBRARY_PATH': libimobiledevicePath}, + )).thenAnswer((_) { + final ProcessResult result = ProcessResult( + 1, + 255, + '', + 'ERROR: Could not connect to lockdownd, error code -12345', + ); + return Future.value(result); + }); + expect(() async => await iMobileDevice.getInfoForDevice('foo', 'bar'), throwsToolExit()); + }, overrides: { + ProcessManager: () => mockProcessManager, + Cache: () => mockCache, + Artifacts: () => mockArtifacts, + }); + + testUsingContext('getInfoForDevice throws IOSDeviceNotFoundError when host trust is revoked', () async { + when(mockArtifacts.getArtifactPath(Artifact.ideviceinfo, platform: anyNamed('platform'))).thenReturn(ideviceInfoPath); + when(mockProcessManager.run( + [ideviceInfoPath, '-u', 'foo', '-k', 'bar'], + environment: {'DYLD_LIBRARY_PATH': libimobiledevicePath}, + )).thenAnswer((_) { + final ProcessResult result = ProcessResult( + 1, + 255, + '', + 'ERROR: Could not connect to lockdownd, error code -${LockdownReturnCode.invalidHostId.code}', + ); + return Future.value(result); + }); + expect(() async => await iMobileDevice.getInfoForDevice('foo', 'bar'), throwsA(isInstanceOf())); + }, overrides: { + ProcessManager: () => mockProcessManager, + Cache: () => mockCache, + Artifacts: () => mockArtifacts, + }); + group('screenshot', () { final String outputPath = fs.path.join('some', 'test', 'path', 'image.png'); MockProcessManager mockProcessManager;