forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(ext/node): Port crypto tests from std/node (denoland#18382)
- Loading branch information
1 parent
b4c61c1
commit 701099b
Showing
5 changed files
with
746 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
import { | ||
assert, | ||
assertEquals, | ||
assertRejects, | ||
assertThrows, | ||
} from "../../../../test_util/std/testing/asserts.ts"; | ||
import { assertCallbackErrorUncaught } from "../_test_utils.ts"; | ||
import { randomBytes } from "node:crypto"; | ||
|
||
const MAX_RANDOM_VALUES = 65536; | ||
const MAX_SIZE = 4294967295; | ||
|
||
Deno.test("randomBytes sync works correctly", function () { | ||
assertEquals(randomBytes(0).length, 0, "len: " + 0); | ||
assertEquals(randomBytes(3).length, 3, "len: " + 3); | ||
assertEquals(randomBytes(30).length, 30, "len: " + 30); | ||
assertEquals(randomBytes(300).length, 300, "len: " + 300); | ||
assertEquals( | ||
randomBytes(17 + MAX_RANDOM_VALUES).length, | ||
17 + MAX_RANDOM_VALUES, | ||
"len: " + 17 + MAX_RANDOM_VALUES, | ||
); | ||
assertEquals( | ||
randomBytes(MAX_RANDOM_VALUES * 100).length, | ||
MAX_RANDOM_VALUES * 100, | ||
"len: " + MAX_RANDOM_VALUES * 100, | ||
); | ||
assertThrows(() => randomBytes(MAX_SIZE + 1)); | ||
assertThrows(() => randomBytes(-1)); | ||
}); | ||
|
||
Deno.test("randomBytes async works correctly", async function () { | ||
randomBytes(0, function (err, resp) { | ||
assert(!err); | ||
assertEquals(resp?.length, 0, "len: " + 0); | ||
}); | ||
randomBytes(3, function (err, resp) { | ||
assert(!err); | ||
assertEquals(resp?.length, 3, "len: " + 3); | ||
}); | ||
randomBytes(30, function (err, resp) { | ||
assert(!err); | ||
assertEquals(resp?.length, 30, "len: " + 30); | ||
}); | ||
randomBytes(300, function (err, resp) { | ||
assert(!err); | ||
assertEquals(resp?.length, 300, "len: " + 300); | ||
}); | ||
randomBytes(17 + MAX_RANDOM_VALUES, function (err, resp) { | ||
assert(!err); | ||
assertEquals( | ||
resp?.length, | ||
17 + MAX_RANDOM_VALUES, | ||
"len: " + 17 + MAX_RANDOM_VALUES, | ||
); | ||
}); | ||
randomBytes(MAX_RANDOM_VALUES * 100, function (err, resp) { | ||
assert(!err); | ||
assertEquals( | ||
resp?.length, | ||
MAX_RANDOM_VALUES * 100, | ||
"len: " + MAX_RANDOM_VALUES * 100, | ||
); | ||
}); | ||
assertThrows(() => | ||
randomBytes(MAX_SIZE + 1, function (err) { | ||
//Shouldn't throw async | ||
assert(!err); | ||
}) | ||
); | ||
await assertRejects(() => | ||
new Promise((resolve, reject) => { | ||
randomBytes(-1, function (err, res) { | ||
//Shouldn't throw async | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(res); | ||
} | ||
}); | ||
}) | ||
); | ||
}); | ||
|
||
Deno.test("[std/node/crypto] randomBytes callback isn't called twice if error is thrown", async () => { | ||
const importUrl = new URL("node:crypto", import.meta.url); | ||
await assertCallbackErrorUncaught({ | ||
prelude: `import { randomBytes } from ${JSON.stringify(importUrl)}`, | ||
invocation: "randomBytes(0, ", | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
import { Buffer } from "node:buffer"; | ||
import { randomFill, randomFillSync } from "node:crypto"; | ||
import { | ||
assertEquals, | ||
assertNotEquals, | ||
assertThrows, | ||
} from "../../../../test_util/std/testing/asserts.ts"; | ||
|
||
const validateNonZero = (buf: Buffer) => { | ||
if (!buf.some((ch) => ch > 0)) throw new Error("Error"); | ||
}; | ||
|
||
const validateZero = (buf: Buffer) => { | ||
buf.forEach((val) => assertEquals(val, 0)); | ||
}; | ||
|
||
Deno.test("[node/crypto.randomFill]", () => { | ||
const buf = Buffer.alloc(10); | ||
const before = buf.toString("hex"); | ||
|
||
randomFill(buf, 5, 5, (_err, bufTwo) => { | ||
const after = bufTwo?.toString("hex"); | ||
assertEquals(before.slice(0, 10), after?.slice(0, 10)); | ||
}); | ||
}); | ||
|
||
Deno.test("[node/crypto.randomFillSync]", () => { | ||
const buf = Buffer.alloc(10); | ||
const before = buf.toString("hex"); | ||
|
||
const after = randomFillSync(buf, 5, 5); | ||
|
||
assertNotEquals(before, after.toString("hex")); | ||
}); | ||
|
||
Deno.test("[node/crypto.randomFillSync] Complete fill, explicit size", () => { | ||
const buf = Buffer.alloc(10); | ||
randomFillSync(buf, undefined, 10); | ||
validateNonZero(buf); | ||
}); | ||
|
||
Deno.test("[randomFillSync] Complete fill", () => { | ||
const buf = Buffer.alloc(10); | ||
randomFillSync(buf); | ||
validateNonZero(buf); | ||
}); | ||
|
||
Deno.test("[node/crypto.randomFillSync] Fill beginning, explicit offset+size", () => { | ||
const buf = Buffer.alloc(10); | ||
randomFillSync(buf, 0, 5); | ||
validateNonZero(buf); | ||
|
||
const untouched = buf.slice(5); | ||
assertEquals(untouched.length, 5); | ||
validateZero(untouched); | ||
}); | ||
|
||
Deno.test("[node/crypto.randomFillSync] Invalid offst/size", () => { | ||
assertThrows(() => randomFillSync(Buffer.alloc(10), 1, 10)); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
import { randomInt } from "node:crypto"; | ||
import { | ||
assert, | ||
assertThrows, | ||
} from "../../../../test_util/std/testing/asserts.ts"; | ||
|
||
const between = (x: number, min: number, max: number) => x >= min && x < max; | ||
|
||
Deno.test("[node/crypto.randomInt] One Param: Max", () => { | ||
assert(between(randomInt(55), 0, 55)); | ||
}); | ||
|
||
Deno.test("[node/crypto.randomInt] Two Params: Max and Min", () => { | ||
assert(between(randomInt(40, 120), 40, 120)); | ||
}); | ||
|
||
Deno.test("[node/crypto.randomInt] Max and Callback", () => { | ||
let called = false; | ||
randomInt(3, (_err, val) => { | ||
called = true; | ||
assert(between(val as number, 0, 3)); | ||
}); | ||
assert(called); | ||
}); | ||
|
||
Deno.test("[node/crypto.randomInt] Min, Max and Callback", () => { | ||
randomInt(3, 5, (_err, val) => { | ||
assert(between(val as number, 3, 5)); | ||
}); | ||
}); | ||
|
||
Deno.test("[node/crypto.randomInt] Min is bigger than Max", () => { | ||
assertThrows(() => randomInt(45, 34)); | ||
}); |
Oops, something went wrong.