diff --git a/app/lib/features/updates/app_updates.dart b/app/lib/features/updates/app_updates.dart index e5f34aae..26237aea 100644 --- a/app/lib/features/updates/app_updates.dart +++ b/app/lib/features/updates/app_updates.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:convert'; import '../../core/servers/server_catalog.dart'; @@ -171,6 +172,13 @@ Future newestServerUpdate( Future Function(Uri, Map Function(Uri))? read, bool Function(SavedServer server, Uri uri)? stillTrusted, }) async { + // Why each machine offered nothing, so a failed check says what to fix. + final problems = []; + void note(SavedServer server, Object error) { + final line = '${server.name}: ${updateProblem(error)}'; + if (!problems.contains(line)) problems.add(line); + } + final results = await Future.wait( servers.map((server) async { Map Function(Uri) headers; @@ -179,7 +187,8 @@ Future newestServerUpdate( id: server.id, endpoints: server.endpoints.map(Uri.parse), ); - } catch (_) { + } catch (error) { + note(server, error); return null; } final snapshotHeaders = headers; @@ -201,7 +210,8 @@ Future newestServerUpdate( headers, ).timeout(const Duration(seconds: 15)); return update.fromServer(server.name, headers); - } catch (_) { + } catch (error) { + note(server, error); return null; } }), @@ -214,13 +224,39 @@ Future newestServerUpdate( final available = results.whereType().toList() ..sort((a, b) => b.versionCode.compareTo(a.versionCode)); if (available.isEmpty) { - throw const FormatException( - 'No compatible update could be read. Check your saved servers and sign-ins.', + throw FormatException( + problems.isEmpty + ? 'No saved server offers an update.' + : 'No compatible update could be read:\n${problems.join('\n')}', ); } return available.first; } +/// One failed update check in words: the reason the check itself gave +/// (sign in first, an untrusted feed key, another signing certificate), else +/// the failure's kind. +String updateProblem(Object error) => switch (error) { + FormatException(:final message) => message, + StateError(:final message) => message, + TimeoutException() => 'the server did not answer in time', + _ => _ioProblem(error), +}; + +/// A network failure's own words ("HttpException: Sign in first …, uri = …" +/// becomes "Sign in first …"). Portable: no dart:io types are named here. +String _ioProblem(Object error) { + var text = '$error'; + final kind = RegExp(r'^[A-Za-z]*(Exception|Error): ').firstMatch(text); + if (kind != null) text = text.substring(kind.end); + final uri = text.indexOf(', uri = '); + if (uri >= 0) text = text.substring(0, uri); + if (error.runtimeType.toString() == 'SocketException') { + return 'could not connect ($text)'; + } + return text.trim().isEmpty ? 'the check failed' : text.trim(); +} + /// Retained offers must stop trusting an alias if its profile is edited/removed. bool updateSourceStillTrusted( Iterable current, @@ -233,3 +269,30 @@ bool updateSourceStillTrusted( (endpoint) => loginUriFor(Uri.parse(endpoint)) == loginUriFor(uri), ), ); + +/// Why a readable, trusted feed offers this app nothing. An Android build +/// signed with another certificate than the installed app cannot install over +/// it, so that is said plainly; anything else is an incompatible build. +String incompatibleUpdateReason( + String body, { + required String platform, + String? installedSignature, +}) { + try { + final json = jsonDecode(body); + final signer = json is Map ? json['signingCertificateSha256'] : null; + if (platform == 'android' && + signer is String && + installedSignature != null && + signer.toLowerCase() != + installedSignature.replaceAll(':', '').trim().toLowerCase()) { + return 'the offered APK is signed with a different certificate than ' + 'this app, so Android cannot install it over this one; install ' + 'it from the server\'s /updates/pipelines.apk after uninstalling ' + 'this app'; + } + } on FormatException { + return 'the update feed could not be read'; + } + return 'no compatible build is offered for this device'; +} diff --git a/app/lib/features/updates/app_updates_io.dart b/app/lib/features/updates/app_updates_io.dart index 3ab26b5e..07d13a08 100644 --- a/app/lib/features/updates/app_updates_io.dart +++ b/app/lib/features/updates/app_updates_io.dart @@ -127,7 +127,15 @@ Future fetchLatestScoped( signed: trusted.isNotEmpty, ); if (update == null) { - throw const FormatException('No compatible build is offered.'); + throw FormatException( + incompatibleUpdateReason( + utf8.decode(chunks), + platform: platform, + installedSignature: platform == 'android' + ? (await PackageInfo.fromPlatform()).buildSignature + : null, + ), + ); } return update; } finally { diff --git a/app/test/features/updates/update_problem_test.dart b/app/test/features/updates/update_problem_test.dart new file mode 100644 index 00000000..8809bfa7 --- /dev/null +++ b/app/test/features/updates/update_problem_test.dart @@ -0,0 +1,83 @@ +import 'dart:async'; +import 'dart:convert'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/servers/server_catalog.dart'; +import 'package:pipelines_app/features/updates/app_updates.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +void main() { + test('a failed check names each machine and why it offered nothing', () async { + SharedPreferences.setMockInitialValues({}); + final tank = SavedServer.validated( + id: 'tank', + name: 'tank', + endpoints: ['wss://tank.test/connect'], + ); + final mac = SavedServer.validated( + id: 'mac', + name: 'Mac', + endpoints: ['wss://mac.test/connect'], + ); + await expectLater( + newestServerUpdate( + [tank, mac], + read: (base, _) async => base.host == 'tank.test' + ? throw const FormatException( + 'The update feed is signed by a key this app does not trust.', + ) + : throw TimeoutException('slow'), + ), + throwsA( + isA().having( + (e) => e.message, + 'message', + allOf( + contains( + 'tank: The update feed is signed by a key this app does not trust.', + ), + contains('Mac: the server did not answer in time'), + ), + ), + ), + ); + }); + + test('an HTTP failure keeps its own words without the exception noise', () { + expect( + updateProblem(_FakeHttpException('Sign in first (Settings → Sign in).')), + 'Sign in first (Settings → Sign in).', + ); + }); + + test('another Android signing certificate is said plainly', () { + final body = jsonEncode({ + 'versionCode': 2, + 'apk': 'pipelines.apk', + 'signingCertificateSha256': 'a' * 64, + }); + expect( + incompatibleUpdateReason( + body, + platform: 'android', + installedSignature: 'B' * 64, + ), + contains('different certificate'), + ); + expect( + incompatibleUpdateReason( + body, + platform: 'android', + installedSignature: 'A' * 64, + ), + 'no compatible build is offered for this device', + ); + }); +} + +class _FakeHttpException implements Exception { + _FakeHttpException(this.message); + final String message; + @override + String toString() => 'HttpException: $message, uri = https://tank.test/x'; +}