Skip to content

Commit

Permalink
chore(crypto): move test scripts to own files (#4367)
Browse files Browse the repository at this point in the history
  • Loading branch information
iuioiua authored Feb 22, 2024
1 parent 104d02a commit 22c0dfc
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 61 deletions.
65 changes: 4 additions & 61 deletions crypto/crypto_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
import { assert, assertEquals, assertInstanceOf, fail } from "../assert/mod.ts";
import { crypto as stdCrypto } from "./mod.ts";
import { repeat } from "../bytes/repeat.ts";
import { dirname, fromFileUrl } from "../path/mod.ts";
import { DigestAlgorithm, digestAlgorithms } from "./_wasm/mod.ts";
import { encodeHex } from "../encoding/hex.ts";

const moduleDir = dirname(fromFileUrl(import.meta.url));

const webCrypto = globalThis.crypto;

Deno.test(
Expand Down Expand Up @@ -167,35 +164,9 @@ Deno.test("digest() handles length option", async () => {
});

Deno.test("digest() keeps memory usage reasonable with large inputs", async () => {
const code = `
import { crypto as stdCrypto } from "./mod.ts";
import { instantiateWithInstance } from "./_wasm/lib/deno_std_wasm_crypto.generated.mjs";
import { encodeHex } from "../encoding/hex.ts";
const { memory } = instantiateWithInstance().instance.exports;
const heapBytesInitial = memory.buffer.byteLength;
const smallData = new Uint8Array(64);
const smallDigest = encodeHex(stdCrypto.subtle.digestSync("BLAKE3", smallData.buffer));
const heapBytesAfterSmall = memory.buffer.byteLength;
const largeData = new Uint8Array(64_000_000);
const largeDigest = encodeHex(stdCrypto.subtle.digestSync("BLAKE3", largeData.buffer));
const heapBytesAfterLarge = memory.buffer.byteLength;
console.log(JSON.stringify({
heapBytesInitial,
smallDigest,
heapBytesAfterSmall,
largeDigest,
heapBytesAfterLarge,
}));
`;

const command = new Deno.Command(Deno.execPath(), {
args: ["eval", "--no-lock", code],
cwd: moduleDir,
args: ["run", "--no-lock", "crypto/testdata/digest_large_inputs.ts"],
stderr: "inherit",
});

const { success, stdout } = await command.output();
Expand Down Expand Up @@ -249,37 +220,9 @@ Deno.test("digest() keeps memory usage reasonable with large inputs", async () =
});

Deno.test("digest() keeps memory usage reasonable with many calls", async () => {
const code = `
import { crypto as stdCrypto } from "./mod.ts";
import { instantiateWithInstance } from "./_wasm/lib/deno_std_wasm_crypto.generated.mjs";
import { encodeHex } from "../encoding/hex.ts";
const { memory } = instantiateWithInstance().instance.exports;
const heapBytesInitial = memory.buffer.byteLength;
let state = new ArrayBuffer(0);
for (let i = 0; i < 1_000_000; i++) {
state = stdCrypto.subtle.digestSync({
name: "BLAKE3"
}, state);
}
const heapBytesFinal = memory.buffer.byteLength;
const stateFinal = encodeHex(state);
console.log(JSON.stringify({
heapBytesInitial,
heapBytesFinal,
stateFinal,
}));
`;

const command = new Deno.Command(Deno.execPath(), {
args: ["eval", "--no-lock", code],
cwd: moduleDir,
args: ["run", "--no-lock", "crypto/testdata/digest_many_calls.ts"],
stderr: "inherit",
});
const { stdout, success } = await command.output();
const output = new TextDecoder().decode(stdout);
Expand Down
28 changes: 28 additions & 0 deletions crypto/testdata/digest_large_inputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { crypto as stdCrypto } from "../crypto.ts";
import { instantiateWithInstance } from "../_wasm/lib/deno_std_wasm_crypto.generated.mjs";
import { encodeHex } from "../../encoding/hex.ts";

const { memory } = instantiateWithInstance().instance.exports;

const heapBytesInitial = memory.buffer.byteLength;

const smallData = new Uint8Array(64);
const smallDigest = encodeHex(
stdCrypto.subtle.digestSync("BLAKE3", smallData.buffer),
);
const heapBytesAfterSmall = memory.buffer.byteLength;

const largeData = new Uint8Array(64_000_000);
const largeDigest = encodeHex(
stdCrypto.subtle.digestSync("BLAKE3", largeData.buffer),
);
const heapBytesAfterLarge = memory.buffer.byteLength;

console.log(JSON.stringify({
heapBytesInitial,
smallDigest,
heapBytesAfterSmall,
largeDigest,
heapBytesAfterLarge,
}));
26 changes: 26 additions & 0 deletions crypto/testdata/digest_many_calls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { crypto as stdCrypto } from "../crypto.ts";
import { instantiateWithInstance } from "../_wasm/lib/deno_std_wasm_crypto.generated.mjs";
import { encodeHex } from "../../encoding/hex.ts";

const { memory } = instantiateWithInstance().instance.exports;

const heapBytesInitial = memory.buffer.byteLength;

let state = new ArrayBuffer(0);

for (let i = 0; i < 1_000_000; i++) {
state = stdCrypto.subtle.digestSync({
name: "BLAKE3",
}, state);
}

const heapBytesFinal = memory.buffer.byteLength;

const stateFinal = encodeHex(state);

console.log(JSON.stringify({
heapBytesInitial,
heapBytesFinal,
stateFinal,
}));

0 comments on commit 22c0dfc

Please sign in to comment.