From 22694a81d8e0aa13d2949674609555c3a1fd1964 Mon Sep 17 00:00:00 2001 From: td Date: Fri, 26 Jul 2024 22:19:02 +0530 Subject: [PATCH] fix: also delete db on logout we already cleared it out, but to change the db enc pw on the app we need to delete it completely first --- lib/src/client.dart | 1 + lib/src/database/database_api.dart | 2 +- lib/src/database/indexeddb_box.dart | 10 +++++++-- lib/src/database/matrix_sdk_database.dart | 12 ++++++---- lib/src/database/sqflite_box.dart | 6 +++++ pubspec.yaml | 1 + test/box_test.dart | 4 ++-- test/client_test.dart | 27 ++++++++++++++++++++--- test/fake_client.dart | 10 ++++++--- test/fake_database.dart | 8 ++++--- 10 files changed, 63 insertions(+), 18 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index 8e7d6d6d..84279fdd 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -1799,6 +1799,7 @@ class Client extends MatrixApi { } catch (e, s) { Logs().e('Unable to clear database', e, s); } finally { + await database?.delete(); _database = null; } diff --git a/lib/src/database/database_api.dart b/lib/src/database/database_api.dart index c07b1541..b4abfd6a 100644 --- a/lib/src/database/database_api.dart +++ b/lib/src/database/database_api.dart @@ -335,6 +335,6 @@ abstract class DatabaseApi { Future getPresence(String userId); /// Deletes the whole database. The database needs to be created again after - /// this. Used for migration only. + /// this. Future delete(); } diff --git a/lib/src/database/indexeddb_box.dart b/lib/src/database/indexeddb_box.dart index 0da0b288..9d9b2fd5 100644 --- a/lib/src/database/indexeddb_box.dart +++ b/lib/src/database/indexeddb_box.dart @@ -83,8 +83,14 @@ class BoxCollection with ZoneTransactionMixin { return _db.close(); } - static Future delete(String path, [dynamic factory]) => - (factory ?? window.indexedDB!).deleteDatabase(path); + @Deprecated('use collection.deleteDatabase now') + static Future delete(String name, [dynamic factory]) => + (factory ?? window.indexedDB!).deleteDatabase(name); + + Future deleteDatabase(String name, [dynamic factory]) async { + await close(); + await (factory ?? window.indexedDB).deleteDatabase(name); + } } class Box { diff --git a/lib/src/database/matrix_sdk_database.dart b/lib/src/database/matrix_sdk_database.dart index 89675c70..9b46732f 100644 --- a/lib/src/database/matrix_sdk_database.dart +++ b/lib/src/database/matrix_sdk_database.dart @@ -54,6 +54,7 @@ import 'package:matrix/src/database/database_file_storage_stub.dart' class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage { static const int version = 9; final String name; + late BoxCollection _collection; late Box _clientBox; late Box _accountDataBox; @@ -1623,10 +1624,13 @@ class MatrixSdkDatabase extends DatabaseApi with DatabaseFileStorage { } @override - Future delete() => BoxCollection.delete( - name, - sqfliteFactory ?? idbFactory, - ); + Future delete() async { + // database?.path is null on web + await _collection.deleteDatabase( + database?.path ?? name, + sqfliteFactory ?? idbFactory, + ); + } @override Future markUserProfileAsOutdated(userId) async { diff --git a/lib/src/database/sqflite_box.dart b/lib/src/database/sqflite_box.dart index 62eba9f4..10da426d 100644 --- a/lib/src/database/sqflite_box.dart +++ b/lib/src/database/sqflite_box.dart @@ -68,8 +68,14 @@ class BoxCollection with ZoneTransactionMixin { Future close() => _db.close(); + @Deprecated('use collection.deleteDatabase now') static Future delete(String path, [dynamic factory]) => (factory ?? databaseFactory).deleteDatabase(path); + + Future deleteDatabase(String path, [dynamic factory]) async { + await close(); + await (factory ?? databaseFactory).deleteDatabase(path); + } } class Box { diff --git a/pubspec.yaml b/pubspec.yaml index bcbafde2..375f3107 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -41,5 +41,6 @@ dev_dependencies: file: ">=6.1.1 <8.0.0" import_sorter: ^4.6.0 lints: ^4.0.0 + path: ^1.9.0 sqflite_common_ffi: 2.3.2+1 # v2.3.3 doesn't support dart v3.2.x test: ^1.15.7 diff --git a/test/box_test.dart b/test/box_test.dart index cc55a5cf..135b2526 100644 --- a/test/box_test.dart +++ b/test/box_test.dart @@ -100,9 +100,9 @@ void main() { }); test( - 'Box.delete', + 'Collection.deleteDatabase', () async { - await BoxCollection.delete( + await collection.deleteDatabase( db?.path ?? '', isWeb ? null : databaseFactoryFfi, ); diff --git a/test/client_test.dart b/test/client_test.dart index 1ac4f568..6324f1cc 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -18,11 +18,13 @@ import 'dart:async'; import 'dart:convert'; +import 'dart:io'; import 'dart:typed_data'; import 'package:canonical_json/canonical_json.dart'; import 'package:collection/collection.dart'; import 'package:olm/olm.dart' as olm; +import 'package:path/path.dart' show join; import 'package:test/test.dart'; import 'package:matrix/matrix.dart'; @@ -31,16 +33,35 @@ import 'fake_client.dart'; import 'fake_database.dart'; void main() { - late Client matrix; - // key @test:fakeServer.notExisting const pickledOlmAccount = 'N2v1MkIFGcl0mQpo2OCwSopxPQJ0wnl7oe7PKiT4141AijfdTIhRu+ceXzXKy3Kr00nLqXtRv7kid6hU4a+V0rfJWLL0Y51+3Rp/ORDVnQy+SSeo6Fn4FHcXrxifJEJ0djla5u98fBcJ8BSkhIDmtXRPi5/oJAvpiYn+8zMjFHobOeZUAxYR0VfQ9JzSYBsSovoQ7uFkNks1M4EDUvHtuyg3RxViwdNxs3718fyAqQ/VSwbXsY0Nl+qQbF+nlVGHenGqk5SuNl1P6e1PzZxcR0IfXA94Xij1Ob5gDv5YH4UCn9wRMG0abZsQP0YzpDM0FLaHSCyo9i5JD/vMlhH+nZWrgAzPPCTNGYewNV8/h3c+VyJh8ZTx/fVi6Yq46Fv+27Ga2ETRZ3Qn+Oyx6dLBjnBZ9iUvIhqpe2XqaGA1PopOz8iDnaZitw'; const identityKey = '7rvl3jORJkBiK4XX1e5TnGnqz068XfYJ0W++Ml63rgk'; const fingerprintKey = 'gjL//fyaFHADt9KBADGag8g7F8Up78B/K1zXeiEPLJo'; + group('client path', () { + late Client clientOnPath; + + final dbPath = join(Directory.current.path, 'test.sqlite'); + + setUp(() async { + expect(await File(dbPath).exists(), false); + clientOnPath = await getClient( + databasePath: dbPath, + ); + expect(await File(dbPath).exists(), true); + }); + test('logout', () async { + expect(await File(dbPath).exists(), true); + await clientOnPath.logout(); + expect(await File(dbPath).exists(), false); + }); + }); + /// All Tests related to the Login - group('Client', tags: 'olm', () { + group('client mem', tags: 'olm', () { + late Client matrix; + Logs().level = Level.error; /// Check if all Elements get created diff --git a/test/fake_client.dart b/test/fake_client.dart index 822c2a68..331f3f68 100644 --- a/test/fake_client.dart +++ b/test/fake_client.dart @@ -26,12 +26,16 @@ const ssssKey = 'EsT9 RzbW VhPW yqNp cC7j ViiW 5TZB LuY4 ryyv 9guN Ysmr WDPH'; const pickledOlmAccount = 'N2v1MkIFGcl0mQpo2OCwSopxPQJ0wnl7oe7PKiT4141AijfdTIhRu+ceXzXKy3Kr00nLqXtRv7kid6hU4a+V0rfJWLL0Y51+3Rp/ORDVnQy+SSeo6Fn4FHcXrxifJEJ0djla5u98fBcJ8BSkhIDmtXRPi5/oJAvpiYn+8zMjFHobOeZUAxYR0VfQ9JzSYBsSovoQ7uFkNks1M4EDUvHtuyg3RxViwdNxs3718fyAqQ/VSwbXsY0Nl+qQbF+nlVGHenGqk5SuNl1P6e1PzZxcR0IfXA94Xij1Ob5gDv5YH4UCn9wRMG0abZsQP0YzpDM0FLaHSCyo9i5JD/vMlhH+nZWrgAzPPCTNGYewNV8/h3c+VyJh8ZTx/fVi6Yq46Fv+27Ga2ETRZ3Qn+Oyx6dLBjnBZ9iUvIhqpe2XqaGA1PopOz8iDnaZitw'; -Future getClient( - {Duration sendTimelineEventTimeout = const Duration(minutes: 1)}) async { +/// only use `path` if you explicitly if you need a db on path instead of in mem +Future getClient({ + Duration sendTimelineEventTimeout = const Duration(minutes: 1), + String? databasePath, +}) async { final client = Client( 'testclient', httpClient: FakeMatrixApi(), - databaseBuilder: getDatabase, + databaseBuilder: (client) => + getDatabase(client, databasePath: databasePath), onSoftLogout: (client) => client.refreshAccessToken(), sendTimelineEventTimeout: sendTimelineEventTimeout, ); diff --git a/test/fake_database.dart b/test/fake_database.dart index f6763887..055e824e 100644 --- a/test/fake_database.dart +++ b/test/fake_database.dart @@ -22,7 +22,8 @@ import 'package:sqflite_common_ffi/sqflite_ffi.dart'; import 'package:matrix/matrix.dart'; -Future getDatabase(Client? c) => getMatrixSdkDatabase(c); +Future getDatabase(Client? c, {String? databasePath}) => + getMatrixSdkDatabase(c, path: databasePath); bool hiveInitialized = false; @@ -44,9 +45,10 @@ Future getHiveCollectionsDatabase(Client? c) async { } // ignore: deprecated_member_use_from_same_package -Future getMatrixSdkDatabase(Client? c) async { +Future getMatrixSdkDatabase(Client? c, + {String? path}) async { final database = await databaseFactoryFfi.openDatabase( - ':memory:', + path ?? ':memory:', options: OpenDatabaseOptions(singleInstance: false), ); final db = MatrixSdkDatabase(