From 326b5c30bcf22afcb49c53e8d5157b2005cc3963 Mon Sep 17 00:00:00 2001 From: Konstantina Blazhukova Date: Fri, 20 Oct 2023 11:26:54 +0300 Subject: [PATCH] Drafts tests for panic errors support Signed-off-by: Konstantina Blazhukova --- contracts/solidity/errors/Panic.sol | 62 +++++++++++++++++ test/constants.js | 1 + test/solidity/errors/panicErrors.js | 103 ++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 contracts/solidity/errors/Panic.sol create mode 100644 test/solidity/errors/panicErrors.js diff --git a/contracts/solidity/errors/Panic.sol b/contracts/solidity/errors/Panic.sol new file mode 100644 index 000000000..ee07ca1d7 --- /dev/null +++ b/contracts/solidity/errors/Panic.sol @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.20; + +contract Panic { + uint[] someArray; + uint[] anotherArray = [1, 2, 3]; + + enum Button { + ON, + OFF + } + constructor() { + } + + function testPanicError0x01() external pure { + assert(false); + } + + function testPanicError0x11() external pure returns(uint8) { + uint8 test = 255; + uint8 test2 = 1; + return test + test2; + } + + function testPanicError0x12() external pure returns(uint8) { + uint8 number1 = 5; + uint8 number2 = 12-12; + return number1 / number2; + } + + function testPanicError0x21() external pure { + int testValue = -1; + Button value = Button(testValue); + } + + function testPanicError0x22() external pure returns(uint8) { + return 0; + } + + function testPanicError0x31() external { + someArray.pop(); + } + + function testPanicError0x32() external view returns(uint) { + return anotherArray[5]; + } + + function testPanicError0x41() external pure returns(uint[] memory) { + uint[] memory largeArray = new uint[](2**64); + } + + function testPanicError0x51() external pure returns(uint) { + function (uint, uint) internal pure returns (uint) funcPtr; + + return funcPtr(5, 6); + } + + function getSomeArray() external view returns(uint[] memory) { + return someArray; + } + +} diff --git a/test/constants.js b/test/constants.js index 0e3a60325..0caa98c60 100644 --- a/test/constants.js +++ b/test/constants.js @@ -123,6 +123,7 @@ const Contract = { Defaults: 'Defaults', AssignmentReferenceTypes: 'AssignmentReferenceTypes', DestructuringReturns: 'DestructuringReturns', + Panic: 'Panic' } const CALL_EXCEPTION = 'CALL_EXCEPTION' diff --git a/test/solidity/errors/panicErrors.js b/test/solidity/errors/panicErrors.js new file mode 100644 index 000000000..026ebb326 --- /dev/null +++ b/test/solidity/errors/panicErrors.js @@ -0,0 +1,103 @@ +const { expect, assert } = require('chai') +const { ethers } = require('hardhat') +const Constants = require('../../constants') + +describe('Panic Errors', function () { + let contract + + before(async function () { + const factory = await ethers.getContractFactory(Constants.Contract.Panic) + contract = await factory.deploy() + }) + + it('should verify panic error 0x01', async function () { + let error; + try { + await contract.testPanicError0x01(); + } catch(e) { + error = e; + } + expect(error.errorName).to.eq('Panic'); + expect(error.errorArgs).to.deep.eq([ethers.BigNumber.from(1)]) + }) + + it('should verify panic error 0x11', async function () { + let error; + try { + await contract.testPanicError0x11(); + } catch(e) { + error = e; + } + expect(error.errorName).to.eq('Panic'); + expect(error.errorArgs).to.deep.eq([ethers.BigNumber.from(17)]) + }) + + it('should verify panic error 0x12', async function () { + let error; + try { + await contract.testPanicError0x12(); + } catch(e) { + error = e; + } + expect(error.errorName).to.eq('Panic'); + expect(error.errorArgs).to.deep.eq([ethers.BigNumber.from(18)]) + }) + + it('should verify panic error 0x21', async function () { + let error; + try { + await contract.testPanicError0x21(); + } catch(e) { + error = e; + } + expect(error.errorName).to.eq('Panic'); + expect(error.errorArgs).to.deep.eq([ethers.BigNumber.from(33)]) + }) + + it('should verify panic error 0x31', async function () { + let error; + try { + const result = await contract.getSomeArray(); + console.log(result); + await contract.testPanicError0x31(); + } catch(e) { + error = e; + } + expect(error.errorName).to.eq('Panic'); + expect(error.errorArgs).to.deep.eq([ethers.BigNumber.from(18)]) + }) + + it('should verify panic error 0x32', async function () { + let error; + try { + await contract.testPanicError0x32(); + } catch(e) { + error = e; + } + expect(error.errorName).to.eq('Panic'); + expect(error.errorArgs).to.deep.eq([ethers.BigNumber.from(50)]) + }) + + it('should verify panic error 0x41', async function () { + let error; + try { + await contract.testPanicError0x41(); + } catch(e) { + console.log(e) + error = e; + } + expect(error.errorName).to.eq('Panic'); + expect(error.errorArgs).to.deep.eq([ethers.BigNumber.from(65)]) + }) + + it('should verify panic error 0x51', async function () { + let error; + try { + await contract.testPanicError0x51(); + } catch(e) { + error = e; + } + expect(error.errorName).to.eq('Panic'); + expect(error.errorArgs).to.deep.eq([ethers.BigNumber.from(81)]) + }) +})