From 79efe76c3f6e2aa9e579edc6c5bd3c7d428c7056 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crist=C3=B3v=C3=A3o=20Honorato?= Date: Thu, 6 Jun 2024 11:24:08 +0200 Subject: [PATCH] publishInput tests: checks for correctness and for expiration --- .../evm/contracts/test/MockInputValidator.sol | 9 +++- packages/evm/test/Enclave.spec.ts | 54 +++++++++++++++++-- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/packages/evm/contracts/test/MockInputValidator.sol b/packages/evm/contracts/test/MockInputValidator.sol index e917fd08..1763472c 100644 --- a/packages/evm/contracts/test/MockInputValidator.sol +++ b/packages/evm/contracts/test/MockInputValidator.sol @@ -8,7 +8,12 @@ contract MockInputValidator is IInputValidator { address, bytes memory params ) external pure returns (bytes memory input, bool success) { - (input) = abi.decode(params, (bytes)); - success = true; + input = abi.decode(params, (bytes)); + + if (input.length == 3) { + success = false; + } else { + success = true; + } } } diff --git a/packages/evm/test/Enclave.spec.ts b/packages/evm/test/Enclave.spec.ts index b9cff2cf..43e1668e 100644 --- a/packages/evm/test/Enclave.spec.ts +++ b/packages/evm/test/Enclave.spec.ts @@ -1,4 +1,4 @@ -import { loadFixture, time } from "@nomicfoundation/hardhat-network-helpers"; +import { loadFixture, mine, time } from "@nomicfoundation/hardhat-network-helpers"; import { expect } from "chai"; import { ZeroHash } from "ethers"; import { ethers } from "hardhat"; @@ -720,8 +720,56 @@ describe("Enclave", function () { await expect(enclave.publishInput(0, inputData)).to.not.be.reverted; }); - it("reverts if outside of input window"); - it("reverts if input is not valid"); + + it("reverts if input is not valid", async function () { + const { enclave, request } = await loadFixture(setup); + + await enclave.request( + request.pool, + request.threshold, + request.duration, + request.computationModule, + request.cMParams, + request.executionModule, + request.eMParams, + { value: 10 }, + ); + + const inputData = abiCoder.encode(["bytes"], ["0xaabbcc"]); + + await enclave.activate(0); + await expect(enclave.publishInput(0, inputData)).to.be.revertedWithCustomError( + enclave, + "InvalidInput", + ); + }); + + it("reverts if outside of input window", async function () { + const { enclave, request } = await loadFixture(setup); + + await enclave.request( + request.pool, + request.threshold, + request.duration, + request.computationModule, + request.cMParams, + request.executionModule, + request.eMParams, + { value: 10 }, + ); + + const inputData = abiCoder.encode(["bytes32"], [ZeroHash]); + + await enclave.activate(0); + await expect(enclave.publishInput(0, inputData)).to.not.be.reverted; + + await mine(2, { interval: request.duration }); + + await expect(enclave.publishInput(0, inputData)).to.be.revertedWithCustomError( + enclave, + "InputDeadlinePassed", + ); + }); it("sets ciphertextInput correctly"); it("returns true if input is published successfully"); it("emits InputPublished event");