Skip to content

Commit

Permalink
fix(crypto): correct invalid test logic that was supressing some fail…
Browse files Browse the repository at this point in the history
…ures
  • Loading branch information
jeremyBanks committed Nov 13, 2023
1 parent 4a9d438 commit e571f76
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions crypto/crypto_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, assertInstanceOf } from "../assert/mod.ts";
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";
Expand Down Expand Up @@ -1363,7 +1363,11 @@ for (const algorithm of digestAlgorithms) {
const bytePieces = pieces.map((piece) =>
typeof piece === "string" ? new TextEncoder().encode(piece) : piece
) as Array<BufferSource>;
try {

// Expected value will either be a hex string, if the case is expected
// to return successfully, or an error class/constructor function, if
// the case is expected to throw.
if (typeof expected === "string") {
const actual = toHexString(
await stdCrypto.subtle.digest({
...options,
Expand All @@ -1379,15 +1383,32 @@ for (const algorithm of digestAlgorithms) {
JSON.stringify(options)
}) returned unexpected value\n actual: ${actual}\nexpected: ${expected}`,
);
} catch (error) {
if (expected instanceof Function) {
assert(
error instanceof expected,
`got a different error than expected: ${error}`,
} else if (typeof expected === "function") {
let error;
try {
await stdCrypto.subtle.digest({
...options,
name: algorithm,
}, bytePieces);
} catch (caughtError) {
error = caughtError;
}
if (error !== undefined) {
assertInstanceOf(
error,
expected,
);
} else {
throw error;
fail(
`${algorithm} of ${caption}${
i > 0 ? ` (but not until variation [${i}]!)` : ""
} with options ${
JSON.stringify(options)
}) expected an exception of type ${expected.name}, but none was thrown.`,
);
}
} else {
throw new TypeError("expected value has an unexpected type");
}
}
}
Expand Down

0 comments on commit e571f76

Please sign in to comment.