From 07485d83c947828b5148212444ec1779100da957 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Fri, 10 Jan 2025 23:32:59 +0100 Subject: [PATCH] Add basic test for encryption --- .github/workflows/main.yml | 3 ++- sqlite3/test/wasm/encryption_test.dart | 30 ++++++++++++++++++++++++++ sqlite3/test/wasm/sqlite3_test.dart | 5 +---- sqlite3/test/wasm/utils.dart | 6 +++--- 4 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 sqlite3/test/wasm/encryption_test.dart diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0155e866..dff343ea 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -187,7 +187,8 @@ jobs: - name: Web tests run: | - curl https://storage.googleapis.com/simon-public-euw3/assets/sqlite3/wasm/2.4.6/sqlite3.wasm -o example/web/sqlite3.wasm + curl https://simon-public.fsn1.your-objectstorage.com/assets/sqlite3/2.6.0/sqlite3.wasm -o example/web/sqlite3.wasm + curl https://simon-public.fsn1.your-objectstorage.com/assets/sqlite3/2.6.0/sqlite3mc.wasm -o example/web/sqlite3.wasm dart test -P web -r expanded # If browsers behave differently on different platforms, surely that's not our fault... # So, only run browser tests on Linux to be faster. diff --git a/sqlite3/test/wasm/encryption_test.dart b/sqlite3/test/wasm/encryption_test.dart new file mode 100644 index 00000000..94227672 --- /dev/null +++ b/sqlite3/test/wasm/encryption_test.dart @@ -0,0 +1,30 @@ +@Tags(['wasm']) +library; + +import 'package:sqlite3/wasm.dart'; +import 'package:test/test.dart'; + +import 'utils.dart'; + +void main() { + test('can open databases with sqlite3mc', () async { + final sqlite3 = await loadSqlite3WithoutVfs(encryption: true); + sqlite3.registerVirtualFileSystem(InMemoryFileSystem(), makeDefault: true); + + sqlite3.open('/test') + ..execute('pragma key = "key"') + ..execute('CREATE TABLE foo (bar TEXT) STRICT;') + ..execute('INSERT INTO foo VALUES (?)', ['test']) + ..dispose(); + + final database = sqlite3.open('/test'); + expect( + () => database.select('SELECT * FROM foo'), + throwsA(isA() + .having((e) => e.message, 'message', contains('not a database'))), + ); + + database.execute('pragma key = "key"'); + expect(database.select('SELECT * FROM foo'), isNotEmpty); + }); +} diff --git a/sqlite3/test/wasm/sqlite3_test.dart b/sqlite3/test/wasm/sqlite3_test.dart index 6b1d8f96..6f091db3 100644 --- a/sqlite3/test/wasm/sqlite3_test.dart +++ b/sqlite3/test/wasm/sqlite3_test.dart @@ -3,7 +3,6 @@ library; import 'dart:js_interop'; import 'dart:js_interop_unsafe'; -import 'dart:math'; import 'package:http/http.dart' as http; import 'package:sqlite3/wasm.dart'; @@ -35,9 +34,7 @@ void main() { sqlite3 = await WasmSqlite3.load(response.bodyBytes); sqlite3.registerVirtualFileSystem( - // Not using the default Random.secure() because it's not supported - // by dart2wasm - InMemoryFileSystem(random: Random()), + InMemoryFileSystem(), makeDefault: true, ); } diff --git a/sqlite3/test/wasm/utils.dart b/sqlite3/test/wasm/utils.dart index dabb8ae4..39dccdb0 100644 --- a/sqlite3/test/wasm/utils.dart +++ b/sqlite3/test/wasm/utils.dart @@ -1,12 +1,12 @@ import 'package:sqlite3/wasm.dart'; import 'package:test/scaffolding.dart'; -Future loadSqlite3WithoutVfs() async { +Future loadSqlite3WithoutVfs({bool encryption = false}) async { final channel = spawnHybridUri('/test/wasm/asset_server.dart'); final port = (await channel.stream.first as double).toInt(); - final sqliteWasm = - Uri.parse('http://localhost:$port/example/web/sqlite3.wasm'); + final filename = encryption ? 'sqlite3mc.wasm' : 'sqlite3.wasm'; + final sqliteWasm = Uri.parse('http://localhost:$port/example/web/$filename'); return await WasmSqlite3.loadFromUrl(sqliteWasm); }