aboutsummaryrefslogtreecommitdiff
path: root/test/js
diff options
context:
space:
mode:
authorGravatar Silver <14016168+silversquirl@users.noreply.github.com> 2023-04-27 15:45:32 +0100
committerGravatar GitHub <noreply@github.com> 2023-04-27 07:45:32 -0700
commit0060e499235a88693d2b884f5067afa9d4a5793f (patch)
tree97d0977e6355c796a430d152d1a73746963cbebb /test/js
parent87921520e45a2c8e44ab7dc844da2641a83bd164 (diff)
downloadbun-0060e499235a88693d2b884f5067afa9d4a5793f.tar.gz
bun-0060e499235a88693d2b884f5067afa9d4a5793f.tar.zst
bun-0060e499235a88693d2b884f5067afa9d4a5793f.zip
fix: make node:crypto Hash.copy work correctly (#2761)
This commit will also: - add a CryptoHasher.copy function - make CryptoHasher.digest reset the hasher so it can be reused Resolves #2651
Diffstat (limited to 'test/js')
-rw-r--r--test/js/node/crypto/crypto.test.ts63
-rw-r--r--test/js/node/crypto/node-crypto.test.js35
2 files changed, 98 insertions, 0 deletions
diff --git a/test/js/node/crypto/crypto.test.ts b/test/js/node/crypto/crypto.test.ts
index b5b8e9286..3efd4c317 100644
--- a/test/js/node/crypto/crypto.test.ts
+++ b/test/js/node/crypto/crypto.test.ts
@@ -46,6 +46,69 @@ describe("CryptoHasher", () => {
);
expect(hasher.algorithm).toBe("sha512");
});
+
+ it("CryptoHasher sha256", () => {
+ var hasher = new CryptoHasher("sha256");
+ hasher.update("hello world");
+ expect(hasher.digest("hex")).toBe(
+ "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
+ );
+ expect(hasher.algorithm).toBe("sha256");
+ });
+
+ it("CryptoHasher sha256 multi-part", () => {
+ var hasher = new CryptoHasher("sha256");
+ hasher.update("hello ");
+ hasher.update("world");
+ expect(hasher.digest("hex")).toBe(
+ "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
+ );
+ expect(hasher.algorithm).toBe("sha256");
+ });
+
+ it("CryptoHasher resets when digest is called", () => {
+ var hasher = new CryptoHasher("sha256");
+ hasher.update("hello");
+ expect(hasher.digest("hex")).toBe(
+ "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824",
+ );
+ hasher.update("world");
+ expect(hasher.digest("hex")).toBe(
+ "486ea46224d1bb4fb680f34f7c9ad96a8f24ec88be73ea8e5a6c65260e9cb8a7",
+ );
+ });
+
+ for (let alg of CryptoHasher.algorithms) {
+ it(`CryptoHasher ${alg} copy is the same`, () => {
+ const orig = new CryptoHasher(alg);
+ orig.update("hello");
+ const copy = orig.copy();
+
+ expect(copy.digest("hex")).toBe(orig.digest("hex"));
+ expect(copy.algorithm).toBe(orig.algorithm);
+ });
+
+
+ it(`CryptoHasher ${alg} copy is not linked`, () => {
+ const orig = new CryptoHasher(alg);
+ orig.update("hello");
+ const copy = orig.copy();
+
+ orig.update("world");
+ expect(copy.digest("hex")).not.toBe(orig.digest("hex"));
+ });
+
+
+ it(`CryptoHasher ${alg} copy updates the same`, () => {
+ const orig = new CryptoHasher(alg);
+ orig.update("hello");
+ const copy = orig.copy();
+
+ orig.update("world");
+ copy.update("world");
+ expect(copy.digest("hex")).toBe(orig.digest("hex"));
+ });
+ }
});
describe("crypto", () => {
diff --git a/test/js/node/crypto/node-crypto.test.js b/test/js/node/crypto/node-crypto.test.js
index bf5414196..9bed5a47b 100644
--- a/test/js/node/crypto/node-crypto.test.js
+++ b/test/js/node/crypto/node-crypto.test.js
@@ -68,6 +68,7 @@ describe("createHash", () => {
s.end();
});
+
it("repeated calls doesnt segfault", () => {
function fn() {
crypto.createHash("sha1").update(Math.random(), "ascii").digest("base64");
@@ -75,6 +76,40 @@ describe("createHash", () => {
for (let i = 0; i < 10; i++) fn();
});
+
+ it("multiple calls to digest throws exception", () => {
+ const hash = crypto.createHash("sha256");
+ hash.update("hello world");
+ expect(hash.digest("hex")).toBe("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9");
+ expect(() => hash.digest("hex")).toThrow();
+ });
+
+ it("copy is the same", () => {
+ const hash = crypto.createHash("sha256");
+ hash.update("hello");
+ const copy = hash.copy();
+
+ expect(copy.digest("hex")).toBe(hash.digest("hex"));
+ });
+
+ it("copy is not linked", () => {
+ const hash = crypto.createHash("sha256");
+ hash.update("hello");
+ const copy = hash.copy();
+
+ hash.update("world");
+ expect(copy.digest("hex")).not.toBe(hash.digest("hex"));
+ });
+
+ it("copy updates the same", () => {
+ const hash = crypto.createHash("sha256");
+ hash.update("hello");
+ const copy = hash.copy();
+
+ hash.update("world");
+ copy.update("world");
+ expect(copy.digest("hex")).toBe(hash.digest("hex"));
+ });
});
it("crypto.createHmac", () => {