From 699a9f602249f7a37e48411103756cc3506c187b Mon Sep 17 00:00:00 2001 From: Christian Petrov Date: Thu, 1 Oct 2020 15:48:26 +0200 Subject: [PATCH] Fix return value of Crypto#getRandomValues() It should return the same array passed to it [1]. [1]: https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues#Return_value Change-Id: I4d7095d762bc50f4d5170d61aad8440634bbe91e --- src/tabris/Crypto.js | 1 + test/tabris/Crypto.test.js | 7 +++++++ typings/global/crypto.d.ts | 10 ++++------ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/tabris/Crypto.js b/src/tabris/Crypto.js index 270d2d8a0..727a5aad0 100644 --- a/src/tabris/Crypto.js +++ b/src/tabris/Crypto.js @@ -20,6 +20,7 @@ export default class Crypto extends NativeObject { throw new Error('Not enough random bytes available'); } new Uint8Array(typedArray.buffer).set(values); + return typedArray; } } diff --git a/test/tabris/Crypto.test.js b/test/tabris/Crypto.test.js index 1ebd23ed8..503c419c5 100644 --- a/test/tabris/Crypto.test.js +++ b/test/tabris/Crypto.test.js @@ -130,6 +130,13 @@ describe('Crypto', function() { expect(buffer[2]).to.equal(0xffffffff); }); + it('returns given array', function() { + const buffer = new Uint32Array(3); + returnValue = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 1, 255, 255, 255, 255]); + + expect(crypto.getRandomValues(buffer)).to.equal(buffer); + }); + }); }); diff --git a/typings/global/crypto.d.ts b/typings/global/crypto.d.ts index 4643cab49..03254f86d 100644 --- a/typings/global/crypto.d.ts +++ b/typings/global/crypto.d.ts @@ -1,9 +1,7 @@ -declare class Crypto { - - getRandomValues( - typedArray: Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array - ): void; +type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array; +declare class Crypto { + getRandomValues(typedArray: TypedArray): TypedArray; } -declare var crypto: Crypto; \ No newline at end of file +declare var crypto: Crypto;