Skip to content

Commit

Permalink
fix: getRandomValues
Browse files Browse the repository at this point in the history
  • Loading branch information
koji-1009 committed Dec 3, 2024
1 parent 88f1cc4 commit 9539b5f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
30 changes: 18 additions & 12 deletions lib/src/crypto_subtle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions lib/src/impl_js/impl_js.random.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
21 changes: 9 additions & 12 deletions test/crypto_subtle_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<JSUint8Array>(),
);
expect(
(result as JSUint8Array).toDart.any((e) => e != 0),
values.toDart.any((e) => e != 0),
isTrue,
);
});
Expand All @@ -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',
);
}
});
Expand All @@ -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',
);
}
});
Expand Down

0 comments on commit 9539b5f

Please sign in to comment.