From 9d54c1057c4beec460a2fef62124c4524c9606e9 Mon Sep 17 00:00:00 2001 From: danielbate <--global> Date: Mon, 24 Apr 2023 19:23:35 +0100 Subject: [PATCH 1/3] test: refactor and increase verbosity of b256 coder tests --- packages/abi-coder/src/coders/b256.test.ts | 85 ++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/packages/abi-coder/src/coders/b256.test.ts b/packages/abi-coder/src/coders/b256.test.ts index e69de29bb2d..2dbff011b72 100644 --- a/packages/abi-coder/src/coders/b256.test.ts +++ b/packages/abi-coder/src/coders/b256.test.ts @@ -0,0 +1,85 @@ +import B256Coder from './b256'; + +describe('B256Coder', () => { + const B256_DECODED = '0xd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b'; + const B256_ENCODED = new Uint8Array([ + 213, 87, 156, 70, 223, 204, 127, 24, 32, 112, 19, 230, 91, 68, 228, 203, 78, 44, 34, 152, 244, + 172, 69, 123, 168, 248, 39, 67, 243, 30, 147, 11, + ]); + const B256_ZERO_DECODED = '0x0000000000000000000000000000000000000000000000000000000000000000'; + const B256_ZERO_ENCODED = new Uint8Array(32); + + const coder = new B256Coder(); + + it('should encode zero as a 256 bit hash string', () => { + const expected = B256_ZERO_ENCODED; + const actual = coder.encode(B256_ZERO_DECODED); + + expect(actual).toStrictEqual(expected); + }); + + it('should encode a 256 bit hash string', () => { + const expected = B256_ENCODED; + const actual = coder.encode(B256_DECODED); + + expect(actual).toStrictEqual(expected); + }); + + it('should decode zero as a 256 bit hash string', () => { + const expectedValue = B256_ZERO_DECODED; + const expectedLength = B256_ZERO_ENCODED.length; + const [actualValue, actualLength] = coder.decode(B256_ZERO_ENCODED, 0); + + expect(actualValue).toStrictEqual(expectedValue); + expect(actualLength).toBe(expectedLength); + }); + + it('should decode a 256 bit hash string', () => { + const expectedValue = B256_DECODED; + const expectedLength = B256_ENCODED.length; + const [actualValue, actualLength] = coder.decode(B256_ENCODED, 0); + + expect(actualValue).toStrictEqual(expectedValue); + expect(actualLength).toBe(expectedLength); + }); + + it('should throw an error when encoding a 256 bit hash string that is too short', () => { + const invalidInput = B256_DECODED.slice(0, B256_DECODED.length - 1); + + expect(() => { + coder.encode(invalidInput); + }).toThrow('Invalid b256'); + }); + + it('should throw an error when decoding an encoded 256 bit hash string that is too short', () => { + const invalidInput = B256_ENCODED.slice(0, B256_ENCODED.length - 1); + + expect(() => { + coder.decode(invalidInput, 0); + }).toThrow(); + }); + + it('should throw an error when encoding a 256 bit hash string that is too long', () => { + const invalidInput = `${B256_DECODED}0`; + + expect(() => { + coder.encode(invalidInput); + }).toThrow('Invalid b256'); + }); + + it('should throw an error when decoding an encoded 256 bit hash string that is too long', () => { + const invalidInput = new Uint8Array(Array.from(Array(32).keys())); + + expect(() => { + coder.decode(invalidInput, 1); + }).toThrow('Invalid size for b256'); + }); + + it('should throw an error when encoding a 256 bit hash string that is not a hex string', () => { + const invalidInput = 'not a hex string'; + + expect(() => { + coder.encode(invalidInput); + }).toThrow('Invalid b256'); + }); +}); From 83f48e32418800682994a29df1f6f0d6f663ab0c Mon Sep 17 00:00:00 2001 From: danielbate <--global> Date: Fri, 28 Apr 2023 13:20:35 +0100 Subject: [PATCH 2/3] chore: changeset --- .changeset/shy-knives-whisper.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changeset/shy-knives-whisper.md diff --git a/.changeset/shy-knives-whisper.md b/.changeset/shy-knives-whisper.md new file mode 100644 index 00000000000..a49ba48448f --- /dev/null +++ b/.changeset/shy-knives-whisper.md @@ -0,0 +1,2 @@ +--- +--- \ No newline at end of file From 5010da794c4be4877b64d9c999c48eeaa3f97b5f Mon Sep 17 00:00:00 2001 From: danielbate <--global> Date: Tue, 2 May 2023 10:53:36 +0100 Subject: [PATCH 3/3] test: add test for missing throw condition --- packages/abi-coder/src/coders/b256.test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/abi-coder/src/coders/b256.test.ts b/packages/abi-coder/src/coders/b256.test.ts index 2dbff011b72..5b8ae9b66e0 100644 --- a/packages/abi-coder/src/coders/b256.test.ts +++ b/packages/abi-coder/src/coders/b256.test.ts @@ -67,6 +67,15 @@ describe('B256Coder', () => { }).toThrow('Invalid b256'); }); + it('should throw an error when encoding a 512 bit hash string', () => { + const B512 = + '0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2'; + + expect(() => { + coder.encode(B512); + }).toThrow('Invalid b256'); + }); + it('should throw an error when decoding an encoded 256 bit hash string that is too long', () => { const invalidInput = new Uint8Array(Array.from(Array(32).keys()));