From 24e018dc87ad83adcce2c01dec78a63b2d4bd404 Mon Sep 17 00:00:00 2001 From: WillXing Date: Sat, 23 Dec 2023 16:03:16 +1300 Subject: [PATCH 1/5] unit test executeKdf --- packages/amino/src/wallet.spec.ts | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 packages/amino/src/wallet.spec.ts diff --git a/packages/amino/src/wallet.spec.ts b/packages/amino/src/wallet.spec.ts new file mode 100644 index 0000000000..01e3034fca --- /dev/null +++ b/packages/amino/src/wallet.spec.ts @@ -0,0 +1,46 @@ +import { executeKdf, KdfConfiguration } from "./wallet" + +describe("wallet", () => { + describe("executeKdf", () => { + describe("with supported algorithm", () => { + it("can execute", async () => { + const password = "123"; + const kdfConfiguration: KdfConfiguration = { + algorithm: "argon2id", + params: { + outputLength: 32, + opsLimit: 4, + memLimitKib: 3 * 1024, + }, + }; + await expectAsync(executeKdf(password, kdfConfiguration)).not.toBeRejected() + }) + + describe("with invalid params", () => { + it("throws", async () => { + const password = "123"; + const kdfConfiguration: KdfConfiguration = { + algorithm: "argon2id", + params: { + outputLength: "invalid", + opsLimit: "invalid", + memLimitKib: "invalid", + }, + }; + await expectAsync(executeKdf(password, kdfConfiguration)).toBeRejectedWith(new Error("Invalid format of argon2id params")) + }) + }) + }) + + describe("with unsupported algorithm", () => { + it("throws", async () => { + const password = "123"; + const kdfConfiguration: KdfConfiguration = { + algorithm: "unsupported", + params: {}, + }; + await expectAsync(executeKdf(password, kdfConfiguration)).toBeRejectedWith(new Error("Unsupported KDF algorithm")) + }) + }) + }) +}) From d21cec28ad9cb99cc6003af54bfc53b1bffb8b29 Mon Sep 17 00:00:00 2001 From: WillXing Date: Sat, 23 Dec 2023 16:03:54 +1300 Subject: [PATCH 2/5] unit test encrypt --- packages/amino/src/wallet.spec.ts | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/amino/src/wallet.spec.ts b/packages/amino/src/wallet.spec.ts index 01e3034fca..44cc81c2b8 100644 --- a/packages/amino/src/wallet.spec.ts +++ b/packages/amino/src/wallet.spec.ts @@ -1,4 +1,5 @@ -import { executeKdf, KdfConfiguration } from "./wallet" +import { fromHex } from "@cosmjs/encoding"; +import { executeKdf, encrypt, KdfConfiguration } from "./wallet" describe("wallet", () => { describe("executeKdf", () => { @@ -43,4 +44,30 @@ describe("wallet", () => { }) }) }) + + describe("encrypt", () => { + describe("with supported algorithm", () => { + it("can encrypt without throw", async () => { + const plaintext = new Uint8Array([0x11, 0x22, 0x33, 0x44]); + const encryptionKey = fromHex("1324cdddc4b94e625bbabcac862c9429ba011e2184a1ccad60e7c3f6ff4916d8"); + const encryptionConfiguration = { + algorithm: "xchacha20poly1305-ietf", + params: {}, + }; + await expectAsync(encrypt(plaintext, encryptionKey, encryptionConfiguration)).not.toBeRejected() + }) + }) + + describe("with unsupported algorithm", () => { + it("throws", async () => { + const plaintext = new Uint8Array([0x11, 0x22, 0x33, 0x44]); + const encryptionKey = fromHex("1324cdddc4b94e625bbabcac862c9429ba011e2184a1ccad60e7c3f6ff4916d8"); + const encryptionConfiguration = { + algorithm: "unsupported", + params: {}, + }; + await expectAsync(encrypt(plaintext, encryptionKey, encryptionConfiguration)).toBeRejectedWith(new Error("Unsupported encryption algorithm: 'unsupported'")) + }) + }) + }) }) From 6f8df2b67adc9186683db021c99e39199d2f7c5a Mon Sep 17 00:00:00 2001 From: WillXing Date: Sat, 23 Dec 2023 16:04:58 +1300 Subject: [PATCH 3/5] positive unit test decrypt --- packages/amino/src/wallet.spec.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/amino/src/wallet.spec.ts b/packages/amino/src/wallet.spec.ts index 44cc81c2b8..b9b8d0d968 100644 --- a/packages/amino/src/wallet.spec.ts +++ b/packages/amino/src/wallet.spec.ts @@ -1,5 +1,5 @@ import { fromHex } from "@cosmjs/encoding"; -import { executeKdf, encrypt, KdfConfiguration } from "./wallet" +import { executeKdf, encrypt, decrypt, KdfConfiguration } from "./wallet" describe("wallet", () => { describe("executeKdf", () => { @@ -70,4 +70,21 @@ describe("wallet", () => { }) }) }) + + describe("decrypt", () => { + const plaintext = new Uint8Array([0x11, 0x22, 0x33, 0x44]); + const encryptionKey = fromHex("1324cdddc4b94e625bbabcac862c9429ba011e2184a1ccad60e7c3f6ff4916d8"); + const encryptionConfiguration = { + algorithm: "xchacha20poly1305-ietf", + params: {}, + }; + + describe("with supported algorithm", () => { + it("can decrypt without throw", async () => { + const ciphertext = await encrypt(plaintext, encryptionKey, encryptionConfiguration) + await decrypt(ciphertext, encryptionKey, encryptionConfiguration) + await expectAsync(decrypt(ciphertext, encryptionKey, encryptionConfiguration)).not.toBeRejected() + }) + }) + }) }) From f4322a139f34fe65fcc7c892c95f2d8ee2b45258 Mon Sep 17 00:00:00 2001 From: WillXing Date: Sat, 23 Dec 2023 16:05:13 +1300 Subject: [PATCH 4/5] negative unit test decrypt --- packages/amino/src/wallet.spec.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/amino/src/wallet.spec.ts b/packages/amino/src/wallet.spec.ts index b9b8d0d968..87c9e039dd 100644 --- a/packages/amino/src/wallet.spec.ts +++ b/packages/amino/src/wallet.spec.ts @@ -86,5 +86,15 @@ describe("wallet", () => { await expectAsync(decrypt(ciphertext, encryptionKey, encryptionConfiguration)).not.toBeRejected() }) }) + + describe("with unsupported algorithm", () => { + it("throws", async () => { + const ciphertext = await encrypt(plaintext, encryptionKey, encryptionConfiguration) + await expectAsync(decrypt(ciphertext, encryptionKey, { + algorithm: "unsupported", + params: {}, + })).toBeRejectedWith(new Error("Unsupported encryption algorithm: 'unsupported'")) + }) + }) }) }) From d8c590bb7759bc37b1fcbbdc401cb9375a77af94 Mon Sep 17 00:00:00 2001 From: WillXing Date: Sat, 23 Dec 2023 16:17:00 +1300 Subject: [PATCH 5/5] lint fix --- packages/amino/src/wallet.spec.ts | 73 +++++++++++++++++-------------- 1 file changed, 41 insertions(+), 32 deletions(-) diff --git a/packages/amino/src/wallet.spec.ts b/packages/amino/src/wallet.spec.ts index 87c9e039dd..bc6c0868f6 100644 --- a/packages/amino/src/wallet.spec.ts +++ b/packages/amino/src/wallet.spec.ts @@ -1,5 +1,6 @@ import { fromHex } from "@cosmjs/encoding"; -import { executeKdf, encrypt, decrypt, KdfConfiguration } from "./wallet" + +import { decrypt, encrypt, executeKdf, KdfConfiguration } from "./wallet"; describe("wallet", () => { describe("executeKdf", () => { @@ -14,8 +15,8 @@ describe("wallet", () => { memLimitKib: 3 * 1024, }, }; - await expectAsync(executeKdf(password, kdfConfiguration)).not.toBeRejected() - }) + await expectAsync(executeKdf(password, kdfConfiguration)).not.toBeRejected(); + }); describe("with invalid params", () => { it("throws", async () => { @@ -28,10 +29,12 @@ describe("wallet", () => { memLimitKib: "invalid", }, }; - await expectAsync(executeKdf(password, kdfConfiguration)).toBeRejectedWith(new Error("Invalid format of argon2id params")) - }) - }) - }) + await expectAsync(executeKdf(password, kdfConfiguration)).toBeRejectedWith( + new Error("Invalid format of argon2id params"), + ); + }); + }); + }); describe("with unsupported algorithm", () => { it("throws", async () => { @@ -40,10 +43,12 @@ describe("wallet", () => { algorithm: "unsupported", params: {}, }; - await expectAsync(executeKdf(password, kdfConfiguration)).toBeRejectedWith(new Error("Unsupported KDF algorithm")) - }) - }) - }) + await expectAsync(executeKdf(password, kdfConfiguration)).toBeRejectedWith( + new Error("Unsupported KDF algorithm"), + ); + }); + }); + }); describe("encrypt", () => { describe("with supported algorithm", () => { @@ -54,9 +59,9 @@ describe("wallet", () => { algorithm: "xchacha20poly1305-ietf", params: {}, }; - await expectAsync(encrypt(plaintext, encryptionKey, encryptionConfiguration)).not.toBeRejected() - }) - }) + await expectAsync(encrypt(plaintext, encryptionKey, encryptionConfiguration)).not.toBeRejected(); + }); + }); describe("with unsupported algorithm", () => { it("throws", async () => { @@ -66,10 +71,12 @@ describe("wallet", () => { algorithm: "unsupported", params: {}, }; - await expectAsync(encrypt(plaintext, encryptionKey, encryptionConfiguration)).toBeRejectedWith(new Error("Unsupported encryption algorithm: 'unsupported'")) - }) - }) - }) + await expectAsync(encrypt(plaintext, encryptionKey, encryptionConfiguration)).toBeRejectedWith( + new Error("Unsupported encryption algorithm: 'unsupported'"), + ); + }); + }); + }); describe("decrypt", () => { const plaintext = new Uint8Array([0x11, 0x22, 0x33, 0x44]); @@ -81,20 +88,22 @@ describe("wallet", () => { describe("with supported algorithm", () => { it("can decrypt without throw", async () => { - const ciphertext = await encrypt(plaintext, encryptionKey, encryptionConfiguration) - await decrypt(ciphertext, encryptionKey, encryptionConfiguration) - await expectAsync(decrypt(ciphertext, encryptionKey, encryptionConfiguration)).not.toBeRejected() - }) - }) + const ciphertext = await encrypt(plaintext, encryptionKey, encryptionConfiguration); + await decrypt(ciphertext, encryptionKey, encryptionConfiguration); + await expectAsync(decrypt(ciphertext, encryptionKey, encryptionConfiguration)).not.toBeRejected(); + }); + }); describe("with unsupported algorithm", () => { it("throws", async () => { - const ciphertext = await encrypt(plaintext, encryptionKey, encryptionConfiguration) - await expectAsync(decrypt(ciphertext, encryptionKey, { - algorithm: "unsupported", - params: {}, - })).toBeRejectedWith(new Error("Unsupported encryption algorithm: 'unsupported'")) - }) - }) - }) -}) + const ciphertext = await encrypt(plaintext, encryptionKey, encryptionConfiguration); + await expectAsync( + decrypt(ciphertext, encryptionKey, { + algorithm: "unsupported", + params: {}, + }), + ).toBeRejectedWith(new Error("Unsupported encryption algorithm: 'unsupported'")); + }); + }); + }); +});