Skip to content

Commit

Permalink
Add basic test for encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Jan 10, 2025
1 parent c398662 commit 07485d8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 30 additions & 0 deletions sqlite3/test/wasm/encryption_test.dart
Original file line number Diff line number Diff line change
@@ -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<SqliteException>()
.having((e) => e.message, 'message', contains('not a database'))),
);

database.execute('pragma key = "key"');
expect(database.select('SELECT * FROM foo'), isNotEmpty);
});
}
5 changes: 1 addition & 4 deletions sqlite3/test/wasm/sqlite3_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
);
}
Expand Down
6 changes: 3 additions & 3 deletions sqlite3/test/wasm/utils.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import 'package:sqlite3/wasm.dart';
import 'package:test/scaffolding.dart';

Future<WasmSqlite3> loadSqlite3WithoutVfs() async {
Future<WasmSqlite3> 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);
}
Expand Down

0 comments on commit 07485d8

Please sign in to comment.