From 05ef725ff6963390072cc4bcb137cbca4e24cfcf Mon Sep 17 00:00:00 2001 From: rodolfopietro97 Date: Thu, 6 Jul 2023 09:12:37 +0200 Subject: [PATCH 1/2] fieat: Cover some cases in abi tests --- tests/abi.test.ts | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/abi.test.ts b/tests/abi.test.ts index 165c9c7..e3de78c 100644 --- a/tests/abi.test.ts +++ b/tests/abi.test.ts @@ -213,6 +213,16 @@ describe('abi', () => { }) + it('codec errors', () => { + // Exception on encoding + expect(() => abi.encodeParameter('bytes32', '0xdf3234')).to.throw() + // @ts-ignore + expect(() => abi.encodeParameter('WRONG', 10)).to.throw() + + // Exception on decoding + expect(abi.decodeParameter('uint256', 'WRONG_UINT').to.throw()) + }) + it('function', () => { expect(f1.signature).equal('0x27fcbb2f') expect(f1.encode(1, 'foo')).equal('0x27fcbb2f000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000003666f6f0000000000000000000000000000000000000000000000000000000000') @@ -222,6 +232,30 @@ describe('abi', () => { r1: '0xabc0000000000000000000000000000000000001', r2: '0x666f6f' }) + + // Wrong definition of function (wrong formatSignature) + expect(() => { + // @ts-ignore + new abi.Function({ + "constant": false, + // @ts-ignore + "inputs": "WRONG_FORMAT", + "name": "f1", + "outputs": [ + { + "name": "r1", + "type": "address" + }, + { + "name": "r2", + "type": "bytes" + } + ], + "payable": false, + "stateMutability": "nonpayable", + "type": "function" + }) + }).to.throw() }) it('event', () => { @@ -295,6 +329,9 @@ describe('abi', () => { const hexSlice = '0x' + Buffer.from('hello').toString('hex') expect(e5.encode({ a1: hexSlice })).deep.equal([e5.signature, hash]) + + // Wrong encoding of event hex format + expect(e5.encode({ a1: "WRONG_HEX_FORMAT" })).to.throw() }) it('v2: Function', () => { From 7ead32a5ff6785fb43ca0a176a852f0dafd9938e Mon Sep 17 00:00:00 2001 From: rodolfopietro97 Date: Thu, 6 Jul 2023 14:05:21 +0200 Subject: [PATCH 2/2] fix: chai fix --- tests/abi.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/abi.test.ts b/tests/abi.test.ts index e3de78c..8ac29e1 100644 --- a/tests/abi.test.ts +++ b/tests/abi.test.ts @@ -220,7 +220,7 @@ describe('abi', () => { expect(() => abi.encodeParameter('WRONG', 10)).to.throw() // Exception on decoding - expect(abi.decodeParameter('uint256', 'WRONG_UINT').to.throw()) + expect(() => abi.decodeParameter('uint256', 'WRONG_UINT')).to.throw(Error, "invalid hexidecimal string") }) it('function', () => { @@ -331,7 +331,7 @@ describe('abi', () => { expect(e5.encode({ a1: hexSlice })).deep.equal([e5.signature, hash]) // Wrong encoding of event hex format - expect(e5.encode({ a1: "WRONG_HEX_FORMAT" })).to.throw() + expect(() => e5.encode({ a1: "WRONG_HEX_FORMAT" })).to.throw(Error, "event.encode: invalid bytes value") }) it('v2: Function', () => {