Skip to content

Commit

Permalink
feat: Add crypto.getRandomValues test case
Browse files Browse the repository at this point in the history
  • Loading branch information
koji-1009 committed Feb 27, 2024
1 parent eb2dd20 commit 856e6d7
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
51 changes: 51 additions & 0 deletions test/browser_chrome_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,57 @@ void main() {
});
});

group('crypto', () {
test('getRandomValues: success', () {
final data = Uint8List(16 * 1024);
isAllZero(data);
window.crypto.getRandomValues(data.toJS);
isNotAllZero(data);
});

test('getRandomValues: too long', () {
expect(
() => window.crypto.getRandomValues(Uint8List(1000000).toJS),
throwsA(
isA<JSDomException>()
.having(
(e) => e.name,
'name',
'QuotaExceededError',
)
.having(
(e) => e.message,
'message',
contains(
'''Failed to execute 'getRandomValues' on 'Crypto': The ArrayBufferView's byte length (1000000) exceeds the number of bytes of entropy available via this API (65536).''',
),
),
),
);
});

test('getRandomValues: not supported type', () {
expect(
() => window.crypto.getRandomValues(Float32List(32).toJS),
throwsA(
isA<JSDomException>()
.having(
(e) => e.name,
'name',
'TypeMismatchError',
)
.having(
(e) => e.message,
'message',
contains(
'''Failed to execute 'getRandomValues' on 'Crypto': The provided ArrayBufferView is of type 'Float32', which is not an integer array type.''',
),
),
),
);
});
});

group('crypto.subtle', () {
test('generateCryptoKey: success', () async {
final key = await window.crypto.subtle
Expand Down
51 changes: 51 additions & 0 deletions test/browser_firefox_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,57 @@ void main() {
});
});

group('crypto', () {
test('getRandomValues: success', () {
final data = Uint8List(16 * 1024);
isAllZero(data);
window.crypto.getRandomValues(data.toJS);
isNotAllZero(data);
});

test('getRandomValues: too long', () {
expect(
() => window.crypto.getRandomValues(Uint8List(1000000).toJS),
throwsA(
isA<JSDomException>()
.having(
(e) => e.name,
'name',
'QuotaExceededError',
)
.having(
(e) => e.message,
'message',
contains(
'''Failed to execute 'getRandomValues' on 'Crypto': The ArrayBufferView's byte length (1000000) exceeds the number of bytes of entropy available via this API (65536).''',
),
),
),
);
});

test('getRandomValues: not supported type', () {
expect(
() => window.crypto.getRandomValues(Float32List(32).toJS),
throwsA(
isA<JSDomException>()
.having(
(e) => e.name,
'name',
'TypeMismatchError',
)
.having(
(e) => e.message,
'message',
contains(
'''Failed to execute 'getRandomValues' on 'Crypto': The provided ArrayBufferView is of type 'Float32', which is not an integer array type.''',
),
),
),
);
});
});

group('crypto.subtle', () {
test('generateCryptoKey: success', () async {
final key = await window.crypto.subtle
Expand Down

0 comments on commit 856e6d7

Please sign in to comment.