Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(crypto): move test scripts to own files #4367

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helps with debugability.

});

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,
}));
Loading