From 9539b5fd6f338b736c937581b8dc94d5855f2863 Mon Sep 17 00:00:00 2001 From: Koji Wakamiya Date: Tue, 3 Dec 2024 22:20:35 +0900 Subject: [PATCH] fix: getRandomValues --- .github/workflows/test.yml | 6 ++---- lib/src/crypto_subtle.dart | 30 +++++++++++++++++------------ lib/src/impl_js/impl_js.random.dart | 5 +++++ test/crypto_subtle_test.dart | 21 +++++++++----------- 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 00168728..03ac25e1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -94,9 +94,6 @@ jobs: name: MacOS 15 desktop / Chrome / Firefox runs-on: macos-15 # Test with xcode 16 timeout-minutes: 15 - strategy: - matrix: - option: ["", "--wasm"] steps: - uses: actions/checkout@v4 - uses: subosito/flutter-action@v2 @@ -109,7 +106,8 @@ jobs: - run: flutter pub get - run: flutter pub run webcrypto:setup - run: flutter test - - run: flutter test --platform chrome ${{ matrix.option }} + - run: flutter test --platform chrome + - run: flutter test --platform chrome --wasm - run: flutter test integration_test/webcrypto_test.dart -d macos working-directory: ./example # TODO: Enable chromedriver testing on MacOS when it works reliably diff --git a/lib/src/crypto_subtle.dart b/lib/src/crypto_subtle.dart index cbd5363d..a49b161b 100644 --- a/lib/src/crypto_subtle.dart +++ b/lib/src/crypto_subtle.dart @@ -334,23 +334,29 @@ extension type JSRsaOtherPrimesInfo(JSObject _) implements JSObject { TypedData getRandomValues(TypedData array) { if (array is Uint8List) { - final values = window.crypto.getRandomValues(array.toJS); - return (values as JSUint8Array).toDart; + final values = array.toJS; + window.crypto.getRandomValues(values); + return values.toDart; } else if (array is Uint16List) { - final values = window.crypto.getRandomValues(array.toJS); - return (values as JSUint16Array).toDart; + final values = array.toJS; + window.crypto.getRandomValues(values); + return values.toDart; } else if (array is Uint32List) { - final values = window.crypto.getRandomValues(array.toJS); - return (values as JSUint32Array).toDart; + final values = array.toJS; + window.crypto.getRandomValues(values); + return values.toDart; } else if (array is Int8List) { - final values = window.crypto.getRandomValues(array.toJS); - return (values as JSInt8Array).toDart; + final values = array.toJS; + window.crypto.getRandomValues(values); + return values.toDart; } else if (array is Int16List) { - final values = window.crypto.getRandomValues(array.toJS); - return (values as JSInt16Array).toDart; + final values = array.toJS; + window.crypto.getRandomValues(values); + return values.toDart; } else if (array is Int32List) { - final values = window.crypto.getRandomValues(array.toJS); - return (values as JSInt32Array).toDart; + final values = array.toJS; + window.crypto.getRandomValues(values); + return values.toDart; } else { throw ArgumentError.value( array, diff --git a/lib/src/impl_js/impl_js.random.dart b/lib/src/impl_js/impl_js.random.dart index 4664a703..9fa963f7 100644 --- a/lib/src/impl_js/impl_js.random.dart +++ b/lib/src/impl_js/impl_js.random.dart @@ -44,6 +44,11 @@ void fillRandomBytes(TypedData destination) { } on UnsupportedError { rethrow; } on Error catch (e) { + final errorName = e.toString(); + if (errorName != 'JavaScriptError') { + rethrow; + } + throw _translateJavaScriptException(e); } } diff --git a/test/crypto_subtle_test.dart b/test/crypto_subtle_test.dart index 066f5186..f0f9b365 100644 --- a/test/crypto_subtle_test.dart +++ b/test/crypto_subtle_test.dart @@ -63,13 +63,10 @@ void main() { data.every((e) => e == 0), isTrue, ); - final result = subtle.window.crypto.getRandomValues(data.toJS); + final values = data.toJS; + subtle.window.crypto.getRandomValues(values); expect( - result, - isA(), - ); - expect( - (result as JSUint8Array).toDart.any((e) => e != 0), + values.toDart.any((e) => e != 0), isTrue, ); }); @@ -84,10 +81,10 @@ void main() { 'QuotaExceededError', ); } on Error catch (e) { - // dart2wasm throws _JavaScriptError + // dart2wasm throws JavaScriptError expect( - e.runtimeType.toString(), - '_JavaScriptError', + e.toString(), + 'JavaScriptError', ); } }); @@ -102,10 +99,10 @@ void main() { 'TypeMismatchError', ); } on Error catch (e) { - // dart2wasm throws _JavaScriptError + // dart2wasm throws JavaScriptError expect( - e.runtimeType.toString(), - '_JavaScriptError', + e.toString(), + 'JavaScriptError', ); } });