diff --git a/spec/unit/crypto/verification/request.spec.js b/spec/unit/crypto/verification/request.spec.js index 81d29077a..3df49c0ee 100644 --- a/spec/unit/crypto/verification/request.spec.js +++ b/spec/unit/crypto/verification/request.spec.js @@ -18,7 +18,7 @@ import "../../../olm-loader"; import {verificationMethods} from "../../../../src/crypto"; import {logger} from "../../../../src/logger"; import {SAS} from "../../../../src/crypto/verification/SAS"; -import {makeTestClients} from './util'; +import {makeTestClients, setupWebcrypto, teardownWebcrypto} from './util'; const Olm = global.Olm; @@ -31,9 +31,14 @@ describe("verification request integration tests with crypto layer", function() } beforeAll(function() { + setupWebcrypto(); return Olm.init(); }); + afterAll(() => { + teardownWebcrypto(); + }); + it("should request and accept a verification", async function() { const [alice, bob] = await makeTestClients( [ diff --git a/spec/unit/crypto/verification/sas.spec.js b/spec/unit/crypto/verification/sas.spec.js index 7d0ced92b..0a65083c3 100644 --- a/spec/unit/crypto/verification/sas.spec.js +++ b/spec/unit/crypto/verification/sas.spec.js @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. */ import "../../../olm-loader"; -import {makeTestClients} from './util'; +import {makeTestClients, setupWebcrypto, teardownWebcrypto} from './util'; import {MatrixEvent} from "../../../../src/models/event"; import {SAS} from "../../../../src/crypto/verification/SAS"; import {DeviceInfo} from "../../../../src/crypto/deviceinfo"; @@ -35,9 +35,14 @@ describe("SAS verification", function() { } beforeAll(function() { + setupWebcrypto(); return Olm.init(); }); + afterAll(() => { + teardownWebcrypto(); + }); + it("should error on an unexpected event", async function() { const sas = new SAS({}, "@alice:example.com", "ABCDEFG"); sas.handleEvent(new MatrixEvent({ diff --git a/spec/unit/crypto/verification/util.js b/spec/unit/crypto/verification/util.js index 01749077e..b47508f9f 100644 --- a/spec/unit/crypto/verification/util.js +++ b/spec/unit/crypto/verification/util.js @@ -17,6 +17,7 @@ limitations under the License. import {TestClient} from '../../../TestClient'; import {MatrixEvent} from "../../../../src/models/event"; +import nodeCrypto from "crypto"; export async function makeTestClients(userInfos, options) { const clients = []; @@ -102,3 +103,15 @@ export async function makeTestClients(userInfos, options) { return clients; } + +export function setupWebcrypto() { + global.crypto = { + getRandomValues: (buf) => { + return nodeCrypto.randomFillSync(buf); + }, + }; +} + +export function teardownWebcrypto() { + global.crypto = undefined; +} diff --git a/spec/unit/crypto/verification/verification_request.spec.js b/spec/unit/crypto/verification/verification_request.spec.js index 34b15d444..32f2c614d 100644 --- a/spec/unit/crypto/verification/verification_request.spec.js +++ b/spec/unit/crypto/verification/verification_request.spec.js @@ -19,6 +19,7 @@ import {InRoomChannel} from "../../../../src/crypto/verification/request/InRoomC import {ToDeviceChannel} from "../../../../src/crypto/verification/request/ToDeviceChannel"; import {MatrixEvent} from "../../../../src/models/event"; +import {setupWebcrypto, teardownWebcrypto} from "./util"; function makeMockClient(userId, deviceId) { let counter = 1; @@ -115,6 +116,15 @@ async function distributeEvent(ownRequest, theirRequest, event) { } describe("verification request unit tests", function() { + + beforeAll(function() { + setupWebcrypto(); + }); + + afterAll(() => { + teardownWebcrypto(); + }); + it("transition from UNSENT to DONE through happy path", async function() { const alice = makeMockClient("@alice:matrix.tld", "device1"); const bob = makeMockClient("@bob:matrix.tld", "device1"); diff --git a/src/crypto/verification/request/VerificationRequest.js b/src/crypto/verification/request/VerificationRequest.js index d4419e310..452bfebca 100644 --- a/src/crypto/verification/request/VerificationRequest.js +++ b/src/crypto/verification/request/VerificationRequest.js @@ -335,7 +335,7 @@ export class VerificationRequest extends EventEmitter { _generateSharedSecret() { const secretBytes = new Uint8Array(32); // 256bits - window.crypto.getRandomValues(secretBytes); + global.crypto.getRandomValues(secretBytes); this._sharedSecret = olmlib.encodeBase64(secretBytes); }