Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

test: add comprehensive tests for file downloader and offline file storage functionality

AmanRajSinghMourya903b259ef83f
diff --git a/mobile/apps/locker/test/services/files/download/file_downloader_test.dart b/mobile/apps/locker/test/services/files/download/file_downloader_test.dart
new file mode 100644
index 0000000000..9ef466a984
--- /dev/null
+++ b/mobile/apps/locker/test/services/files/download/file_downloader_test.dart
@@ -0,0 +1,112 @@
+import 'dart:io';
+import 'dart:typed_data';
+
+import 'package:flutter_test/flutter_test.dart';
+import 'package:locker/services/files/download/file_downloader.dart'
+ as file_downloader;
+import 'package:locker/services/files/offline/offline_file_storage.dart';
+import 'package:locker/services/files/sync/models/file.dart';
+import 'package:path/path.dart' as p;
+
+import '../../../test_utils/configuration_test_util.dart';
+
+void main() {
+ EnteFile lockerFile({
+ required int uploadedFileID,
+ required String title,
+ String? localPath,
+ }) {
+ return EnteFile()
+ ..uploadedFileID = uploadedFileID
+ ..title = title
+ ..localPath = localPath;
+ }
+
+ Future<File> writeFile(String path, String contents) async {
+ final file = File(path);
+ await file.parent.create(recursive: true);
+ return file.writeAsString(contents);
+ }
+
+ group('file_downloader openFile cache identity', () {
+ late Directory root;
+
+ setUp(() async {
+ root = await setupLockerConfigurationForTest('file_downloader');
+ });
+
+ tearDown(() async {
+ clearLockerConfigurationTestHandlers();
+ if (await root.exists()) {
+ await root.delete(recursive: true);
+ }
+ });
+
+ test('returns ID-keyed cached decrypted copy and ignores localPath',
+ () async {
+ final staleLocalPath = await writeFile(
+ p.join(root.path, 'old-local-copy.pdf'),
+ 'stale local bytes',
+ );
+ final file = lockerFile(
+ uploadedFileID: 901,
+ title: 'statement.pdf',
+ localPath: staleLocalPath.path,
+ );
+ final cachedDecrypted = await writeFile(
+ getCachedDecryptedFilePath(file),
+ 'current cached bytes',
+ );
+
+ final opened = await file_downloader.openFile(file, Uint8List(32));
+
+ expect(opened, isNotNull);
+ expect(opened!.path, cachedDecrypted.path);
+ expect(await opened.readAsString(), 'current cached bytes');
+ expect(await staleLocalPath.readAsString(), 'stale local bytes');
+ });
+
+ test('isolates cached decrypted files by uploaded file ID', () async {
+ final sharedLocalPath = await writeFile(
+ p.join(root.path, 'shared-local.pdf'),
+ 'stale shared local bytes',
+ );
+ final firstFile = lockerFile(
+ uploadedFileID: 902,
+ title: 'same-name.pdf',
+ localPath: sharedLocalPath.path,
+ );
+ final secondFile = lockerFile(
+ uploadedFileID: 903,
+ title: 'same-name.pdf',
+ localPath: sharedLocalPath.path,
+ );
+ final firstCached = await writeFile(
+ getCachedDecryptedFilePath(firstFile),
+ 'first cached bytes',
+ );
+ final secondCached = await writeFile(
+ getCachedDecryptedFilePath(secondFile),
+ 'second cached bytes',
+ );
+
+ final firstOpened = await file_downloader.openFile(
+ firstFile,
+ Uint8List(32),
+ );
+ final secondOpened = await file_downloader.openFile(
+ secondFile,
+ Uint8List(32),
+ );
+
+ expect(firstOpened, isNotNull);
+ expect(secondOpened, isNotNull);
+ expect(firstOpened!.path, firstCached.path);
+ expect(secondOpened!.path, secondCached.path);
+ expect(await firstOpened.readAsString(), 'first cached bytes');
+ expect(await secondOpened.readAsString(), 'second cached bytes');
+ expect(firstOpened.path, isNot(secondOpened.path));
+ expect(await sharedLocalPath.readAsString(), 'stale shared local bytes');
+ });
+ });
+}
diff --git a/mobile/apps/locker/test/services/files/offline/offline_file_storage_test.dart b/mobile/apps/locker/test/services/files/offline/offline_file_storage_test.dart
new file mode 100644
index 0000000000..545bb8298c
--- /dev/null
+++ b/mobile/apps/locker/test/services/files/offline/offline_file_storage_test.dart
@@ -0,0 +1,326 @@
+import 'dart:io';
+
+import 'package:flutter_test/flutter_test.dart';
+import 'package:locker/services/configuration.dart';
+import 'package:locker/services/files/offline/offline_file_storage.dart';
+import 'package:locker/services/files/sync/models/file.dart';
+import 'package:path/path.dart' as p;
+
+import '../../../test_utils/configuration_test_util.dart';
+
+void main() {
+ EnteFile lockerFile(
+ int uploadedFileID, {
+ String title = 'document.pdf',
+ }) {
+ return EnteFile()
+ ..uploadedFileID = uploadedFileID
+ ..title = title;
+ }
+
+ Future<File> writeFile(String path, String contents) async {
+ final file = File(path);
+ await file.parent.create(recursive: true);
+ return file.writeAsString(contents);
+ }
+
+ Future<Directory> createHandoff(
+ int uploadedFileID,
+ String timestamp, {
+ String fileName = 'document.pdf',
+ }) async {
+ final directory = Directory(
+ p.join(
+ getOpenHandoffDirectoryPath(),
+ uploadedFileID.toString(),
+ timestamp,
+ ),
+ );
+ await directory.create(recursive: true);
+ await writeFile(p.join(directory.path, fileName), timestamp);
+ return directory;
+ }
+
+ Future<void> makeOld(File file) async {
+ await file.setLastModified(
+ DateTime.now().subtract(const Duration(minutes: 5)),
+ );
+ }
+
+ group('OfflineFileStorage', () {
+ late Directory root;
+
+ setUp(() async {
+ root = await setupLockerConfigurationForTest('offline_storage');
+ });
+
+ tearDown(() async {
+ clearLockerConfigurationTestHandlers();
+ if (await root.exists()) {
+ await root.delete(recursive: true);
+ }
+ });
+
+ test('removes only the selected file ID from owned storage', () async {
+ final firstFile = lockerFile(123, title: 'first.pdf');
+ final secondFile = lockerFile(456, title: 'second.png');
+ final firstOffline = await writeFile(
+ await getOfflineEncryptedFilePath(firstFile),
+ 'offline 123',
+ );
+ final secondOffline = await writeFile(
+ await getOfflineEncryptedFilePath(secondFile),
+ 'offline 456',
+ );
+ final malformedOffline = await writeFile(
+ p.join(firstOffline.parent.path, 'not-a-file-id.encrypted'),
+ 'malformed',
+ );
+ final firstCachedEncrypted = await writeFile(
+ getCachedEncryptedFilePath(firstFile),
+ 'cached encrypted 123',
+ );
+ final firstCachedDecrypted = await writeFile(
+ getCachedDecryptedFilePath(firstFile),
+ 'cached decrypted 123',
+ );
+ final malformedCachedDecrypted = await writeFile(
+ p.join(Configuration.instance.getCacheDirectory(), '123.decrypted_bak'),
+ 'malformed decrypted',
+ );
+ final secondCachedEncrypted = await writeFile(
+ getCachedEncryptedFilePath(secondFile),
+ 'cached encrypted 456',
+ );
+ final secondCachedDecrypted = await writeFile(
+ getCachedDecryptedFilePath(secondFile),
+ 'cached decrypted 456',
+ );
+ final firstHandoff = await createHandoff(123, '100');
+ final secondHandoff = await createHandoff(456, '100');
+ final nonIdHandoff = Directory(
+ p.join(getOpenHandoffDirectoryPath(), 'not-an-id', '100'),
+ );
+ await nonIdHandoff.create(recursive: true);
+ final unrelatedCacheFile = await writeFile(
+ p.join(Configuration.instance.getCacheDirectory(), 'keep.me'),
+ 'unrelated',
+ );
+
+ await removeOfflineFileCopiesFromDisk([123]);
+
+ expect(await firstOffline.exists(), isFalse);
+ expect(await firstCachedEncrypted.exists(), isFalse);
+ expect(await firstCachedDecrypted.exists(), isFalse);
+ expect(await firstHandoff.exists(), isFalse);
+ expect(await malformedCachedDecrypted.exists(), isTrue);
+ expect(await secondOffline.exists(), isTrue);
+ expect(await secondCachedEncrypted.exists(), isTrue);
+ expect(await secondCachedDecrypted.exists(), isTrue);
+ expect(await secondHandoff.exists(), isTrue);
+ expect(await malformedOffline.exists(), isTrue);
+ expect(await nonIdHandoff.exists(), isTrue);
+ expect(await unrelatedCacheFile.exists(), isTrue);
+ });
+
+ test('can remove offline encrypted blobs while preserving working copies',
+ () async {
+ final file = lockerFile(123, title: 'document.pdf');
+ final offline = await writeFile(
+ await getOfflineEncryptedFilePath(file),
+ 'offline 123',
+ );
+ final cachedEncrypted = await writeFile(
+ getCachedEncryptedFilePath(file),
+ 'cached encrypted 123',
+ );
+ final cachedDecrypted = await writeFile(
+ getCachedDecryptedFilePath(file),
+ 'cached decrypted 123',
+ );
+ final handoff = await createHandoff(123, '100');
+
+ await removeOfflineFileCopiesFromDisk(
+ [123],
+ removeWorkingCopies: false,
+ );
+
+ expect(await offline.exists(), isFalse);
+ expect(await cachedEncrypted.exists(), isTrue);
+ expect(await cachedDecrypted.exists(), isTrue);
+ expect(await handoff.exists(), isTrue);
+ });
+
+ test('clears owned offline cache while preserving unrelated cache files',
+ () async {
+ final firstFile = lockerFile(123, title: 'first.pdf');
+ final secondFile = lockerFile(456, title: 'second.png');
+ final firstOffline = await writeFile(
+ await getOfflineEncryptedFilePath(firstFile),
+ 'offline 123',
+ );
+ final secondOffline = await writeFile(
+ await getOfflineEncryptedFilePath(secondFile),
+ 'offline 456',
+ );
+ final malformedOffline = await writeFile(
+ p.join(firstOffline.parent.path, 'not-a-file-id.encrypted'),
+ 'malformed offline',
+ );
+ final firstCachedEncrypted = await writeFile(
+ getCachedEncryptedFilePath(firstFile),
+ 'cached encrypted 123',
+ );
+ final firstCachedDecrypted = await writeFile(
+ getCachedDecryptedFilePath(firstFile),
+ 'cached decrypted 123',
+ );
+ final secondCachedEncrypted = await writeFile(
+ getCachedEncryptedFilePath(secondFile),
+ 'cached encrypted 456',
+ );
+ final secondCachedDecrypted = await writeFile(
+ getCachedDecryptedFilePath(secondFile),
+ 'cached decrypted 456',
+ );
+ final firstHandoff = await createHandoff(123, '100');
+ final secondHandoff = await createHandoff(456, '100');
+ final nonIdHandoff = Directory(
+ p.join(getOpenHandoffDirectoryPath(), 'not-an-id', '100'),
+ );
+ await nonIdHandoff.create(recursive: true);
+ await writeFile(p.join(nonIdHandoff.path, 'document.pdf'), 'non id');
+ final rootHandoffFile = await writeFile(
+ p.join(getOpenHandoffDirectoryPath(), 'root-handoff.tmp'),
+ 'root handoff',
+ );
+ final unrelatedCacheFile = await writeFile(
+ p.join(Configuration.instance.getCacheDirectory(), 'keep.me'),
+ 'unrelated',
+ );
+
+ await clearAllOfflineFileCopies();
+
+ expect(await firstOffline.exists(), isFalse);
+ expect(await secondOffline.exists(), isFalse);
+ expect(await malformedOffline.exists(), isFalse);
+ expect(await firstCachedEncrypted.exists(), isFalse);
+ expect(await firstCachedDecrypted.exists(), isFalse);
+ expect(await secondCachedEncrypted.exists(), isFalse);
+ expect(await secondCachedDecrypted.exists(), isFalse);
+ expect(await firstHandoff.exists(), isFalse);
+ expect(await secondHandoff.exists(), isFalse);
+ expect(await nonIdHandoff.exists(), isFalse);
+ expect(await rootHandoffFile.exists(), isFalse);
+ expect(await unrelatedCacheFile.exists(), isTrue);
+ });
+
+ test('stale cache cleanup is age gated and ID pattern scoped', () async {
+ final oldFile = lockerFile(123, title: 'old.pdf');
+ final freshFile = lockerFile(456, title: 'fresh.pdf');
+ final oldOfflineEncrypted = await writeFile(
+ await getOfflineEncryptedFilePath(oldFile),
+ 'offline source',
+ );
+ final oldCachedEncrypted = await writeFile(
+ getCachedEncryptedFilePath(oldFile),
+ 'old encrypted',
+ );
+ final oldCachedDecrypted = await writeFile(
+ getCachedDecryptedFilePath(oldFile),
+ 'old decrypted',
+ );
+ final freshCachedEncrypted = await writeFile(
+ getCachedEncryptedFilePath(freshFile),
+ 'fresh encrypted',
+ );
+ final freshCachedDecrypted = await writeFile(
+ getCachedDecryptedFilePath(freshFile),
+ 'fresh decrypted',
+ );
+ final staleUnrelatedCacheFile = await writeFile(
+ p.join(Configuration.instance.getCacheDirectory(), 'keep.me'),
+ 'unrelated',
+ );
+ final staleMalformedCacheFile = await writeFile(
+ p.join(Configuration.instance.getCacheDirectory(), '123.encrypted.bak'),
+ 'malformed',
+ );
+ final staleMalformedDecryptedCacheFile = await writeFile(
+ p.join(Configuration.instance.getCacheDirectory(), '123.decrypted_bak'),
+ 'malformed decrypted',
+ );
+ await makeOld(oldOfflineEncrypted);
+ await makeOld(oldCachedEncrypted);
+ await makeOld(oldCachedDecrypted);
+ await makeOld(staleUnrelatedCacheFile);
+ await makeOld(staleMalformedCacheFile);
+ await makeOld(staleMalformedDecryptedCacheFile);
+
+ await cleanupStaleOfflineFileCopies(
+ olderThan: const Duration(minutes: 1),
+ );
+
+ expect(await oldOfflineEncrypted.exists(), isTrue);
+ expect(await oldCachedEncrypted.exists(), isFalse);
+ expect(await oldCachedDecrypted.exists(), isFalse);
+ expect(await freshCachedEncrypted.exists(), isTrue);
+ expect(await freshCachedDecrypted.exists(), isTrue);
+ expect(await staleUnrelatedCacheFile.exists(), isTrue);
+ expect(await staleMalformedCacheFile.exists(), isTrue);
+ expect(await staleMalformedDecryptedCacheFile.exists(), isTrue);
+ });
+
+ test('ages timestamp children instead of file ID parent', () async {
+ final cacheDirectory = Configuration.instance.getCacheDirectory();
+ final oldHandoff = Directory(
+ p.join(cacheDirectory, 'open_handoff', '123', '100'),
+ );
+ final freshHandoff = Directory(
+ p.join(cacheDirectory, 'open_handoff', '123', '200'),
+ );
+ final staleOnlyHandoff = Directory(
+ p.join(cacheDirectory, 'open_handoff', '456', '100'),
+ );
+ final unrelatedCacheFile = File(p.join(cacheDirectory, 'keep.me'));
+ await oldHandoff.create(recursive: true);
+ await staleOnlyHandoff.create(recursive: true);
+ await File(p.join(oldHandoff.path, 'document.pdf')).writeAsString('old');
+ await File(p.join(staleOnlyHandoff.path, 'document.pdf'))
+ .writeAsString('stale');
+ final staleRootHandoffFile = await writeFile(
+ p.join(cacheDirectory, 'open_handoff', 'stale-root.tmp'),
+ 'stale root',
+ );
+ await unrelatedCacheFile.writeAsString('unrelated');
+
+ await Future<void>.delayed(const Duration(seconds: 2));
+ await freshHandoff.create(recursive: true);
+ await File(p.join(freshHandoff.path, 'document.pdf'))
+ .writeAsString('fresh');
+ final freshRootHandoffFile = await writeFile(
+ p.join(cacheDirectory, 'open_handoff', 'fresh-root.tmp'),
+ 'fresh root',
+ );
+
+ await cleanupStaleOfflineFileCopies(
+ olderThan: const Duration(seconds: 1),
+ );
+
+ expect(await oldHandoff.exists(), isFalse);
+ expect(await freshHandoff.exists(), isTrue);
+ expect(await staleRootHandoffFile.exists(), isFalse);
+ expect(await freshRootHandoffFile.exists(), isTrue);
+ expect(
+ await Directory(p.join(cacheDirectory, 'open_handoff', '123')).exists(),
+ isTrue,
+ );
+ expect(await staleOnlyHandoff.exists(), isFalse);
+ expect(
+ await Directory(p.join(cacheDirectory, 'open_handoff', '456')).exists(),
+ isFalse,
+ );
+ expect(await unrelatedCacheFile.exists(), isTrue);
+ });
+ });
+}
diff --git a/mobile/apps/locker/test/test_utils/configuration_test_util.dart b/mobile/apps/locker/test/test_utils/configuration_test_util.dart
new file mode 100644
index 0000000000..e21e719f47
--- /dev/null
+++ b/mobile/apps/locker/test/test_utils/configuration_test_util.dart
@@ -0,0 +1,74 @@
+import 'dart:io';
+
+import 'package:flutter/services.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:locker/services/configuration.dart';
+import 'package:shared_preferences/shared_preferences.dart';
+
+const _pathProviderChannel = MethodChannel('plugins.flutter.io/path_provider');
+const _secureStorageChannel =
+ MethodChannel('plugins.it_nomads.com/flutter_secure_storage');
+
+Future<Directory> setupLockerConfigurationForTest(String name) async {
+ TestWidgetsFlutterBinding.ensureInitialized();
+
+ final root = await Directory.systemTemp.createTemp('locker_${name}_');
+ final tempDirectory = Directory('${root.path}/temp_root');
+ final supportDirectory = Directory('${root.path}/support');
+ await tempDirectory.create(recursive: true);
+ await supportDirectory.create(recursive: true);
+
+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
+ .setMockMethodCallHandler(_pathProviderChannel, (call) async {
+ switch (call.method) {
+ case 'getTemporaryDirectory':
+ return tempDirectory.path;
+ case 'getApplicationSupportDirectory':
+ return supportDirectory.path;
+ default:
+ return root.path;
+ }
+ });
+
+ final secureStorage = <String, String>{};
+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
+ .setMockMethodCallHandler(_secureStorageChannel, (call) async {
+ final arguments = (call.arguments as Map?) ?? {};
+ final key = arguments['key'] as String?;
+ switch (call.method) {
+ case 'read':
+ return secureStorage[key];
+ case 'write':
+ if (key != null) {
+ secureStorage[key] = arguments['value'] as String;
+ }
+ return null;
+ case 'delete':
+ if (key != null) {
+ secureStorage.remove(key);
+ }
+ return null;
+ case 'deleteAll':
+ secureStorage.clear();
+ return null;
+ case 'containsKey':
+ return key != null && secureStorage.containsKey(key);
+ case 'readAll':
+ return secureStorage;
+ default:
+ return null;
+ }
+ });
+
+ SharedPreferences.setMockInitialValues({});
+ await Configuration.instance.init([]);
+
+ return root;
+}
+
+void clearLockerConfigurationTestHandlers() {
+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
+ .setMockMethodCallHandler(_pathProviderChannel, null);
+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
+ .setMockMethodCallHandler(_secureStorageChannel, null);
+}
diff --git a/mobile/apps/locker/test/utils/file_util_test.dart b/mobile/apps/locker/test/utils/file_util_test.dart
new file mode 100644
index 0000000000..cb81ee6181
--- /dev/null
+++ b/mobile/apps/locker/test/utils/file_util_test.dart
@@ -0,0 +1,299 @@
+import 'dart:io';
+
+import 'package:flutter_test/flutter_test.dart';
+import 'package:locker/services/configuration.dart';
+import 'package:locker/services/files/offline/offline_file_storage.dart';
+import 'package:locker/services/files/sync/models/file.dart';
+import 'package:locker/utils/file_util.dart';
+import 'package:path/path.dart' as p;
+
+import '../test_utils/configuration_test_util.dart';
+
+void main() {
+ EnteFile lockerFile({
+ required int uploadedFileID,
+ required String title,
+ String? localPath,
+ }) {
+ return EnteFile()
+ ..uploadedFileID = uploadedFileID
+ ..title = title
+ ..localPath = localPath;
+ }
+
+ String cacheRelativePath(File file) {
+ return p.relative(
+ file.path,
+ from: Configuration.instance.getCacheDirectory(),
+ );
+ }
+
+ group('FileUtil handoff preparation', () {
+ late Directory root;
+
+ setUp(() async {
+ root = await setupLockerConfigurationForTest('file_util');
+ });
+
+ tearDown(() async {
+ clearLockerConfigurationTestHandlers();
+ if (await root.exists()) {
+ await root.delete(recursive: true);
+ }
+ });
+
+ test('creates a fresh open_handoff copy using uploaded file ID', () async {
+ final source = File(p.join(root.path, 'source.bin'));
+ await source.writeAsString('current file bytes');
+ final file = lockerFile(uploadedFileID: 123, title: 'original.pdf');
+
+ final handoffFile = await FileUtil.prepareOpenFileForTest(
+ source,
+ displayName: null,
+ lockerFile: file,
+ );
+
+ expect(handoffFile.path, isNot(source.path));
+ expect(await handoffFile.readAsString(), 'current file bytes');
+ expect(await source.exists(), isTrue);
+ expect(
+ cacheRelativePath(handoffFile),
+ startsWith(p.join('open_handoff', '123')),
+ );
+ expect(p.basename(handoffFile.path), 'file-123.pdf');
+ });
+
+ test('uses UI display name with metadata-derived extension', () async {
+ final source = File(p.join(root.path, 'source.bin'));
+ await source.writeAsString('renamed file bytes');
+ final file = lockerFile(uploadedFileID: 456, title: 'passport.png');
+
+ final handoffFile = await FileUtil.prepareOpenFileForTest(
+ source,
+ displayName: 'Front side',
+ lockerFile: file,
+ );
+
+ expect(p.basename(handoffFile.path), 'Front side.png');
+ expect(await handoffFile.readAsString(), 'renamed file bytes');
+ expect(
+ cacheRelativePath(handoffFile),
+ startsWith(p.join('open_handoff', '456')),
+ );
+ });
+
+ test('creates distinct handoff paths for repeated same-file opens',
+ () async {
+ final source = File(p.join(root.path, 'source.bin'));
+ await source.writeAsString('first version');
+ final file = lockerFile(uploadedFileID: 234, title: 'invoice.pdf');
+
+ final firstHandoff = await FileUtil.prepareOpenFileForTest(
+ source,
+ displayName: 'Invoice',
+ lockerFile: file,
+ );
+
+ await source.writeAsString('second version');
+ await Future<void>.delayed(const Duration(milliseconds: 1));
+
+ final secondHandoff = await FileUtil.prepareOpenFileForTest(
+ source,
+ displayName: 'Invoice',
+ lockerFile: file,
+ );
+
+ expect(secondHandoff.path, isNot(firstHandoff.path));
+ expect(
+ p.dirname(secondHandoff.path),
+ isNot(p.dirname(firstHandoff.path)),
+ );
+ expect(p.basename(firstHandoff.path), 'Invoice.pdf');
+ expect(p.basename(secondHandoff.path), 'Invoice.pdf');
+ expect(await firstHandoff.readAsString(), 'first version');
+ expect(await secondHandoff.readAsString(), 'second version');
+ expect(await source.readAsString(), 'second version');
+ });
+
+ test('ignores stale localPath when preparing the open handoff', () async {
+ final source = File(p.join(root.path, 'current-cache.bin'));
+ final staleLocalPath = File(p.join(root.path, 'old-local-copy.docx'));
+ await source.writeAsString('current cache bytes');
+ await staleLocalPath.writeAsString('stale local bytes');
+ final file = lockerFile(
+ uploadedFileID: 321,
+ title: 'locker-copy.pdf',
+ localPath: staleLocalPath.path,
+ );
+
+ final handoffFile = await FileUtil.prepareOpenFileForTest(
+ source,
+ displayName: null,
+ lockerFile: file,
+ );
+
+ expect(await handoffFile.readAsString(), 'current cache bytes');
+ expect(await staleLocalPath.readAsString(), 'stale local bytes');
+ expect(cacheRelativePath(handoffFile), startsWith('open_handoff'));
+ expect(
+ cacheRelativePath(handoffFile),
+ startsWith(p.join('open_handoff', '321')),
+ );
+ expect(p.basename(handoffFile.path), 'file-321.pdf');
+ expect(handoffFile.path, isNot(staleLocalPath.path));
+ });
+
+ test('partitions handoff copies by uploaded file ID, not source path',
+ () async {
+ final source = File(p.join(root.path, 'shared-source.bin'));
+ final staleLocalPath = File(p.join(root.path, 'same-stale-local.txt'));
+ await source.writeAsString('shared source bytes');
+ await staleLocalPath.writeAsString('shared stale bytes');
+ final firstFile = lockerFile(
+ uploadedFileID: 111,
+ title: 'first.pdf',
+ localPath: staleLocalPath.path,
+ );
+ final secondFile = lockerFile(
+ uploadedFileID: 222,
+ title: 'second.pdf',
+ localPath: staleLocalPath.path,
+ );
+
+ final firstHandoff = await FileUtil.prepareOpenFileForTest(
+ source,
+ displayName: 'Document',
+ lockerFile: firstFile,
+ );
+ final secondHandoff = await FileUtil.prepareOpenFileForTest(
+ source,
+ displayName: 'Document',
+ lockerFile: secondFile,
+ );
+
+ expect(
+ cacheRelativePath(firstHandoff),
+ startsWith(p.join('open_handoff', '111')),
+ );
+ expect(
+ cacheRelativePath(secondHandoff),
+ startsWith(p.join('open_handoff', '222')),
+ );
+ expect(await firstHandoff.readAsString(), 'shared source bytes');
+ expect(await secondHandoff.readAsString(), 'shared source bytes');
+ expect(await staleLocalPath.readAsString(), 'shared stale bytes');
+ });
+
+ test('replaces stale display extension with Locker metadata extension',
+ () async {
+ final source = File(p.join(root.path, 'front.tmp'));
+ await source.writeAsString('front bytes');
+ final file = lockerFile(uploadedFileID: 654, title: 'passport.png');
+
+ final handoffFile = await FileUtil.prepareOpenFileForTest(
+ source,
+ displayName: 'Front side.jpg',
+ lockerFile: file,
+ );
+
+ expect(p.basename(handoffFile.path), 'Front side.png');
+ expect(await handoffFile.readAsString(), 'front bytes');
+ });
+
+ test('keeps display extension when it already matches metadata extension',
+ () async {
+ final source = File(p.join(root.path, 'invoice.tmp'));
+ await source.writeAsString('invoice bytes');
+ final file = lockerFile(uploadedFileID: 655, title: 'invoice.pdf');
+
+ final handoffFile = await FileUtil.prepareOpenFileForTest(
+ source,
+ displayName: 'Invoice.PDF',
+ lockerFile: file,
+ );
+
+ expect(p.basename(handoffFile.path), 'Invoice.PDF');
+ expect(await handoffFile.readAsString(), 'invoice bytes');
+ });
+
+ test('falls back to source extension when metadata has no extension',
+ () async {
+ final source = File(p.join(root.path, 'current-cache.bin'));
+ await source.writeAsString('source extension bytes');
+ final file = lockerFile(uploadedFileID: 656, title: 'untitled');
+
+ final handoffFile = await FileUtil.prepareOpenFileForTest(
+ source,
+ displayName: 'Visible name',
+ lockerFile: file,
+ );
+
+ expect(p.basename(handoffFile.path), 'Visible name.bin');
+ expect(await handoffFile.readAsString(), 'source extension bytes');
+ });
+
+ test('falls back to display extension when metadata and source lack one',
+ () async {
+ final source = File(p.join(root.path, 'current-cache'));
+ await source.writeAsString('display extension bytes');
+ final file = lockerFile(uploadedFileID: 657, title: 'untitled');
+
+ final handoffFile = await FileUtil.prepareOpenFileForTest(
+ source,
+ displayName: 'Visible name.pdf',
+ lockerFile: file,
+ );
+
+ expect(p.basename(handoffFile.path), 'Visible name.pdf');
+ expect(await handoffFile.readAsString(), 'display extension bytes');
+ });
+
+ test('does not use internal decrypted cache suffix as handoff extension',
+ () async {
+ final file = lockerFile(uploadedFileID: 658, title: 'untitled');
+ final source = File(getCachedDecryptedFilePath(file));
+ await source.writeAsString('cached decrypted bytes');
+
+ final handoffFile = await FileUtil.prepareOpenFileForTest(
+ source,
+ displayName: 'Visible name.pdf',
+ lockerFile: file,
+ );
+
+ expect(p.basename(source.path), '658.decrypted');
+ expect(p.basename(handoffFile.path), 'Visible name.pdf');
+ expect(await handoffFile.readAsString(), 'cached decrypted bytes');
+ });
+
+ test('sanitizes display names before creating the handoff copy', () async {
+ final source = File(p.join(root.path, 'source.bin'));
+ await source.writeAsString('safe bytes');
+ final file = lockerFile(uploadedFileID: 987, title: 'document.pdf');
+
+ final handoffFile = await FileUtil.prepareOpenFileForTest(
+ source,
+ displayName: 'bad:*?"<>|name',
+ lockerFile: file,
+ );
+
+ final launchName = p.basename(handoffFile.path);
+ expect(launchName, endsWith('.pdf'));
+ expect(launchName, isNot(contains(RegExp(r'[\\/:*?"<>|]'))));
+ expect(await handoffFile.readAsString(), 'safe bytes');
+ });
+
+ test('fails closed when handoff copy cannot be created', () async {
+ final missingSource = File(p.join(root.path, 'missing.pdf'));
+ final file = lockerFile(uploadedFileID: 789, title: 'missing.pdf');
+
+ await expectLater(
+ FileUtil.prepareOpenFileForTest(
+ missingSource,
+ displayName: 'Missing',
+ lockerFile: file,
+ ),
+ throwsA(isA<Exception>()),
+ );
+ });
+ });
+}