Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
31 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,37 @@ | ||
import * as assert from "assert"; | ||
import * as assert from "node:assert"; | ||
import * as nodeCrypto from "node:crypto"; | ||
import { Crypto, SubtleCrypto } from "../src"; | ||
|
||
context("Crypto", () => { | ||
|
||
it("Crypto matches to globalThis.Crypto", () => { | ||
class MyCrypto extends Crypto { | ||
public subtle = new SubtleCrypto(); | ||
public getRandomValues<T extends ArrayBufferView | null>(array: T): T { | ||
throw new Error("Method not implemented."); | ||
class MyCrypto extends Crypto { | ||
public subtle = new SubtleCrypto(); | ||
public getRandomValues<T extends ArrayBufferView | null>(array: T): T { | ||
if (ArrayBuffer.isView(array)) { | ||
const buffer = Buffer.from(array.buffer, array.byteOffset, array.byteLength); | ||
nodeCrypto.randomFillSync(buffer); | ||
} | ||
|
||
return array; | ||
} | ||
} | ||
|
||
it("Crypto matches to globalThis.Crypto", () => { | ||
// tslint:disable-next-line: no-shadowed-variable | ||
let crypto: globalThis.Crypto; | ||
crypto = new MyCrypto(); | ||
assert.ok(crypto); | ||
}); | ||
|
||
it("randomUUID", () => { | ||
const crypto = new MyCrypto(); | ||
|
||
let counter = 1000; | ||
const regex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; | ||
while (counter--) { | ||
const uuid = crypto.randomUUID(); | ||
assert.ok(new RegExp(regex).test(uuid), `UUID ${uuid} is incorrect`); | ||
} | ||
}); | ||
|
||
}); |