227 lines
7.7 KiB
Dart
227 lines
7.7 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'package:crypto/crypto.dart';
|
|
import 'package:pipelines_app/features/updates/app_updates_io.dart' as io;
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:pipelines_app/core/servers/server_catalog.dart';
|
|
import 'package:pipelines_app/core/session/server_session.dart';
|
|
import 'package:pipelines_app/features/updates/app_updates.dart';
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
test('newer differently signed APK is skipped for compatible peer', () async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final signer = 'a' * 64;
|
|
final compatible = SavedServer.validated(
|
|
id: 'compatible',
|
|
name: 'Mac',
|
|
endpoints: ['wss://compatible.test/connect'],
|
|
);
|
|
final different = SavedServer.validated(
|
|
id: 'different',
|
|
name: 'Linux',
|
|
endpoints: ['wss://different.test/connect'],
|
|
);
|
|
final update = await newestServerUpdate(
|
|
[different, compatible],
|
|
read: (base, headers) async {
|
|
final parsed = AppUpdate.parse(
|
|
jsonEncode({
|
|
'versionCode': base.host == 'different.test' ? 30 : 20,
|
|
'apk': 'pipelines.apk',
|
|
'signingCertificateSha256': base.host == 'different.test'
|
|
? 'b' * 64
|
|
: signer,
|
|
}),
|
|
base,
|
|
installedSignature: signer.toUpperCase(),
|
|
);
|
|
if (parsed == null) throw const FormatException('Incompatible signer');
|
|
return parsed;
|
|
},
|
|
);
|
|
expect(update.versionCode, 20);
|
|
expect(update.sourceName, 'Mac');
|
|
expect(update.signingCertificateSha256, signer);
|
|
final base = Uri.parse('https://compatible.test/updates/');
|
|
for (final invalid in ['', 'sha256:bad', 42, 'g' * 64]) {
|
|
expect(
|
|
AppUpdate.parse(
|
|
jsonEncode({
|
|
'versionCode': 40,
|
|
'apk': 'pipelines.apk',
|
|
'signingCertificateSha256': invalid,
|
|
}),
|
|
base,
|
|
installedSignature: signer,
|
|
),
|
|
isNull,
|
|
);
|
|
}
|
|
// Legacy feeds retain OS installer validation rather than disappearing.
|
|
expect(
|
|
AppUpdate.parse(
|
|
'{"versionCode":1,"apk":"pipelines.apk"}',
|
|
base,
|
|
installedSignature: signer,
|
|
),
|
|
isNotNull,
|
|
);
|
|
});
|
|
test(
|
|
'retained update refuses credentials after alias/profile removal',
|
|
() async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final source = SavedServer.validated(
|
|
id: 'source',
|
|
name: 'Home',
|
|
endpoints: ['wss://home.test/connect'],
|
|
);
|
|
var current = [source];
|
|
final update = await newestServerUpdate(
|
|
current,
|
|
stillTrusted: (server, uri) =>
|
|
updateSourceStillTrusted(current, server, uri),
|
|
read: (base, headers) async => AppUpdate(
|
|
versionCode: 12,
|
|
versionName: '',
|
|
apk: base.resolve('app.apk'),
|
|
),
|
|
);
|
|
current = [];
|
|
expect(() => update.headersFor!(update.apk), throwsStateError);
|
|
},
|
|
);
|
|
test(
|
|
'downloads authenticate their source, verify digest and remove failed partials',
|
|
() async {
|
|
final previousOverrides = HttpOverrides.current;
|
|
HttpOverrides.global = null;
|
|
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
|
|
final directory = await Directory.systemTemp.createTemp('update-test-');
|
|
final payload = utf8.encode('archive fixture');
|
|
final cookies = <String?>[];
|
|
server.listen((request) async {
|
|
cookies.add(request.headers.value('cookie'));
|
|
request.response.add(payload);
|
|
await request.response.close();
|
|
});
|
|
final uri = Uri.parse('http://127.0.0.1:${server.port}/archive.zip');
|
|
AppUpdate candidate(String digest) => AppUpdate(
|
|
versionCode: 12,
|
|
versionName: '',
|
|
apk: uri,
|
|
platform: 'macos',
|
|
sha256: digest,
|
|
headersFor: (_) => {'Cookie': 'source=second'},
|
|
);
|
|
try {
|
|
final file = await io.downloadUpdate(
|
|
candidate(sha256.convert(payload).toString()),
|
|
directory,
|
|
);
|
|
expect(await file.readAsBytes(), payload);
|
|
await file.delete();
|
|
await expectLater(
|
|
io.downloadUpdate(candidate('0' * 64), directory),
|
|
throwsFormatException,
|
|
);
|
|
expect(await directory.list().toList(), isEmpty);
|
|
expect(cookies, ['source=second', 'source=second']);
|
|
} finally {
|
|
await server.close(force: true);
|
|
await directory.delete(recursive: true);
|
|
HttpOverrides.global = previousOverrides;
|
|
}
|
|
},
|
|
);
|
|
test(
|
|
'desktop metadata rejects wrong architecture, platform and escaped URLs',
|
|
() {
|
|
final base = Uri.parse('https://host/updates/macos/');
|
|
final value = <String, Object>{
|
|
'versionCode': 42,
|
|
'platform': 'macos',
|
|
'architectures': ['arm64'],
|
|
'file': 'Pipelines-macos-42.zip',
|
|
};
|
|
AppUpdate? parse() => AppUpdate.parse(
|
|
jsonEncode(value),
|
|
base,
|
|
platform: 'macos',
|
|
architecture: 'arm64',
|
|
);
|
|
expect(parse()!.platform, 'macos');
|
|
value['architectures'] = ['x64'];
|
|
expect(parse(), isNull);
|
|
value['architectures'] = ['arm64'];
|
|
value['platform'] = 'linux';
|
|
expect(parse(), isNull);
|
|
value['platform'] = 'macos';
|
|
value['file'] = 'https://evil/archive.zip';
|
|
expect(parse(), isNull);
|
|
value['file'] = '../archive.zip';
|
|
expect(parse(), isNull);
|
|
value['file'] = 'archive.zip';
|
|
value['versionCode'] = 42.5;
|
|
expect(parse(), isNull);
|
|
},
|
|
);
|
|
test(
|
|
'all saved machines checked with isolated auth without switching active server',
|
|
() async {
|
|
SharedPreferences.setMockInitialValues({
|
|
'pipelines.session.v2.first.cookie': 'session=first',
|
|
'pipelines.session.v2.second.cookie': 'session=second',
|
|
});
|
|
final a = Uri.parse('wss://first.test/connect');
|
|
final b = Uri.parse('wss://second.test/connect');
|
|
await ServerSession.activateServer(id: 'first', endpoints: [a]);
|
|
final calls = <String>[];
|
|
final update = await newestServerUpdate(
|
|
[
|
|
SavedServer.validated(id: 'first', name: 'Mac', endpoints: ['$a']),
|
|
SavedServer.validated(id: 'second', name: 'Linux', endpoints: ['$b']),
|
|
SavedServer.validated(
|
|
id: 'offline',
|
|
name: 'Offline',
|
|
endpoints: ['wss://offline.test/connect'],
|
|
),
|
|
],
|
|
read: (base, headers) async {
|
|
calls.add(base.host);
|
|
expect(headers(Uri.parse('https://untrusted.test/')), isEmpty);
|
|
if (base.host == 'offline.test') throw StateError('offline');
|
|
final second = base.host == 'second.test';
|
|
expect(
|
|
headers(base)['Cookie'],
|
|
second ? 'session=second' : 'session=first',
|
|
);
|
|
return AppUpdate(
|
|
versionCode: second ? 20 : 10,
|
|
versionName: '',
|
|
apk: base.resolve('pipelines.apk'),
|
|
);
|
|
},
|
|
);
|
|
expect(calls, containsAll(['first.test', 'second.test', 'offline.test']));
|
|
expect(update.versionCode, 20);
|
|
expect(update.sourceName, 'Linux');
|
|
expect(
|
|
update.headersFor!(Uri.parse('https://second.test/'))['Cookie'],
|
|
'session=second',
|
|
);
|
|
expect(update.headersFor!(Uri.parse('https://first.test/')), isEmpty);
|
|
expect(ServerSession.serverId, 'first');
|
|
expect(ServerSession.headersFor(a)['Cookie'], 'session=first');
|
|
final firstReader = await ServerSession.credentialReader(
|
|
id: 'first',
|
|
endpoints: [a],
|
|
);
|
|
await ServerSession.signOut();
|
|
expect(firstReader(a), isEmpty);
|
|
},
|
|
);
|
|
}
|