From 10cc28539f172d35b9b6a6e5b4943230c1c174c5 Mon Sep 17 00:00:00 2001 From: wshino Date: Sun, 22 Sep 2024 12:58:51 +0900 Subject: [PATCH] Add tests for scripts --- script/test/BaseDeployTest.sol | 107 ++++++++++++++++++ script/test/Compute7579RecoveryDataHash.t.sol | 18 +++ script/test/DeployEmailRecoveryModule.t.sol | 70 ++++++++++++ script/test/DeploySafeNativeRecovery.t.sol | 88 ++++++++++++++ script/test/DeploySafeRecovery.t.sol | 38 +++++++ .../DeployUniversalEmailRecoveryModule.t.sol | 52 +++++++++ 6 files changed, 373 insertions(+) create mode 100644 script/test/BaseDeployTest.sol create mode 100644 script/test/Compute7579RecoveryDataHash.t.sol create mode 100644 script/test/DeployEmailRecoveryModule.t.sol create mode 100644 script/test/DeploySafeNativeRecovery.t.sol create mode 100644 script/test/DeploySafeRecovery.t.sol create mode 100644 script/test/DeployUniversalEmailRecoveryModule.t.sol diff --git a/script/test/BaseDeployTest.sol b/script/test/BaseDeployTest.sol new file mode 100644 index 00000000..1ac21768 --- /dev/null +++ b/script/test/BaseDeployTest.sol @@ -0,0 +1,107 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.12; + +import "forge-std/Test.sol"; +import "forge-std/console2.sol"; +import { Verifier } from "@zk-email/ether-email-auth-contracts/src/utils/Verifier.sol"; +import { EmailAuth } from "@zk-email/ether-email-auth-contracts/src/EmailAuth.sol"; +import { EmailRecoveryCommandHandler } from "src/handlers/EmailRecoveryCommandHandler.sol"; +import { EmailRecoveryUniversalFactory } from "src/factories/EmailRecoveryUniversalFactory.sol"; +import { ERC1967Proxy } from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; +import { ECDSAOwnedDKIMRegistry } from + "@zk-email/ether-email-auth-contracts/src/utils/ECDSAOwnedDKIMRegistry.sol"; +import { Groth16Verifier } from "@zk-email/ether-email-auth-contracts/src/utils/Groth16Verifier.sol"; + +abstract contract BaseDeployTest is Test { + function setUp() public virtual { + // Set environment variables + vm.setEnv( + "PRIVATE_KEY", "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" + ); + address initialOwner = vm.addr(vm.envUint("PRIVATE_KEY")); + + // Deploy Verifier and set up proxy + address verifier = deployVerifier(initialOwner); + + // Set up additional environment variables + setupEnvironmentVariables(); + + // Deploy EmailRecoveryCommandHandler + EmailRecoveryCommandHandler emailRecoveryHandler = new EmailRecoveryCommandHandler(); + + // Deploy EmailRecoveryUniversalFactory and set up module + deployEmailRecoveryModule(verifier); + } + + /** + * @dev Deploys the Verifier contract and sets up its proxy. + * @param initialOwner The address of the initial owner. + * @return The address of the deployed Verifier contract. + */ + function deployVerifier(address initialOwner) internal returns (address) { + Verifier verifierImpl = new Verifier(); + Groth16Verifier groth16Verifier = new Groth16Verifier(); + ERC1967Proxy verifierProxy = new ERC1967Proxy( + address(verifierImpl), + abi.encodeCall(verifierImpl.initialize, (initialOwner, address(groth16Verifier))) + ); + address verifier = address(Verifier(address(verifierProxy))); + vm.setEnv("VERIFIER", vm.toString(address(verifierImpl))); + return verifier; + } + + /** + * @dev Sets up additional environment variables required for the deployment. + */ + function setupEnvironmentVariables() internal { + vm.setEnv("SIGNER", vm.toString(vm.addr(5))); + address dkimRegistrySigner = vm.envOr("SIGNER", address(0)); + + // Deploy DKIM Registry and set up proxy + address dkimRegistry = deployDKIMRegistry(dkimRegistrySigner); + vm.setEnv("DKIM_REGISTRY", vm.toString(address(dkimRegistry))); + + // Set EmailAuth implementation address + address emailAuthImpl = address(new EmailAuth()); + vm.setEnv("EMAIL_AUTH_IMPL", vm.toString(emailAuthImpl)); + + // Set additional environment variables + vm.setEnv("NEW_OWNER", vm.toString(vm.addr(8))); + vm.setEnv("VALIDATOR", vm.toString(vm.addr(9))); + vm.setEnv("ACCOUNT_SALT", vm.toString(bytes32(uint256(1)))); + } + + /** + * @dev Deploys the ECDSAOwnedDKIMRegistry contract and sets up its proxy. + * @param dkimRegistrySigner The address of the DKIM registry signer. + * @return The address of the deployed DKIM Registry contract. + */ + function deployDKIMRegistry(address dkimRegistrySigner) internal returns (address) { + ECDSAOwnedDKIMRegistry dkimImpl = new ECDSAOwnedDKIMRegistry(); + console.log("ECDSAOwnedDKIMRegistry implementation deployed at: %s", address(dkimImpl)); + ERC1967Proxy dkimProxy = new ERC1967Proxy( + address(dkimImpl), + abi.encodeCall( + dkimImpl.initialize, (vm.addr(vm.envUint("PRIVATE_KEY")), dkimRegistrySigner) + ) + ); + return address(ECDSAOwnedDKIMRegistry(address(dkimProxy))); + } + + /** + * @dev Deploys the EmailRecoveryUniversalFactory and sets up the recovery module. + * @param verifier The address of the deployed Verifier contract. + */ + function deployEmailRecoveryModule(address verifier) internal { + address _factory = + address(new EmailRecoveryUniversalFactory(verifier, vm.envAddress("EMAIL_AUTH_IMPL"))); + EmailRecoveryUniversalFactory factory = EmailRecoveryUniversalFactory(_factory); + (address module,) = factory.deployUniversalEmailRecoveryModule( + bytes32(uint256(0)), + bytes32(uint256(0)), + type(EmailRecoveryCommandHandler).creationCode, + vm.envAddress("DKIM_REGISTRY") + ); + vm.setEnv("RECOVERY_MODULE", vm.toString(module)); + } +} diff --git a/script/test/Compute7579RecoveryDataHash.t.sol b/script/test/Compute7579RecoveryDataHash.t.sol new file mode 100644 index 00000000..65916eab --- /dev/null +++ b/script/test/Compute7579RecoveryDataHash.t.sol @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.12; + +import "forge-std/Test.sol"; +import "forge-std/console2.sol"; +import { Compute7579RecoveryDataHash } from "../Compute7579RecoveryDataHash.s.sol"; +import { BaseDeployTest } from "./BaseDeployTest.sol"; + +contract Compute7579RecoveryDataHashTest is BaseDeployTest { + function setUp() public override { + super.setUp(); + } + + function testRun() public { + Compute7579RecoveryDataHash target = new Compute7579RecoveryDataHash(); + target.run(); + } +} diff --git a/script/test/DeployEmailRecoveryModule.t.sol b/script/test/DeployEmailRecoveryModule.t.sol new file mode 100644 index 00000000..79d870a1 --- /dev/null +++ b/script/test/DeployEmailRecoveryModule.t.sol @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.12; + +import "forge-std/Test.sol"; +import "forge-std/console2.sol"; +import { DeployEmailRecoveryModuleScript } from "../DeployEmailRecoveryModule.s.sol"; +import { BaseDeployTest } from "./BaseDeployTest.sol"; + +/** + * @title DeployEmailRecoveryModule_Test + * @dev Test contract for deploying the Email Recovery Module + */ +contract DeployEmailRecoveryModule_Test is BaseDeployTest { + /** + * @dev Sets up the test environment. + */ + function setUp() public override { + super.setUp(); + } + + /** + * @dev Tests that the standard deployment process executes correctly. + */ + function test_run() public { + DeployEmailRecoveryModuleScript target = new DeployEmailRecoveryModuleScript(); + target.run(); + } + + /** + * @dev Tests the deployment process when the VERIFIER environment variable is not set. + */ + function test_run_no_verifier() public { + vm.setEnv("VERIFIER", vm.toString(address(0))); + DeployEmailRecoveryModuleScript target = new DeployEmailRecoveryModuleScript(); + target.run(); + } + + /** + * @dev Tests the deployment process when the DKIM_REGISTRY environment variable is not set. + */ + function test_run_no_dkim_registry() public { + vm.setEnv("DKIM_REGISTRY", vm.toString(address(0))); + DeployEmailRecoveryModuleScript target = new DeployEmailRecoveryModuleScript(); + target.run(); + } +} + +/** + * @title DeployEmailRecoveryModule_TestFail + * @dev Test contract for failure scenarios when deploying the Email Recovery Module + */ +contract DeployEmailRecoveryModule_TestFail is BaseDeployTest { + /** + * @dev Sets up the test environment. + */ + function setUp() public override { + super.setUp(); + } + + /** + * @dev Tests that deployment fails when both DKIM_REGISTRY and SIGNER environment variables are + * not set. + */ + function testFail_run_no_dkim_registry_no_signer() public { + vm.setEnv("DKIM_REGISTRY", vm.toString(address(0))); + vm.setEnv("SIGNER", vm.toString(address(0))); + DeployEmailRecoveryModuleScript target = new DeployEmailRecoveryModuleScript(); + target.run(); + } +} diff --git a/script/test/DeploySafeNativeRecovery.t.sol b/script/test/DeploySafeNativeRecovery.t.sol new file mode 100644 index 00000000..e813f69f --- /dev/null +++ b/script/test/DeploySafeNativeRecovery.t.sol @@ -0,0 +1,88 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.12; + +import "forge-std/Test.sol"; +import "forge-std/console2.sol"; +import { DeploySafeNativeRecovery_Script } from "../DeploySafeNativeRecovery.s.sol"; +import { BaseDeployTest } from "./BaseDeployTest.sol"; + +contract DeploySafeNativeRecovery_Test is BaseDeployTest { + /** + * @notice Tests the basic deployment and execution of the DeploySafeNativeRecovery script. + */ + function test_run() public { + // Set up the base test environment + BaseDeployTest.setUp(); + + // Instantiate the script and run it + DeploySafeNativeRecovery_Script target = new DeploySafeNativeRecovery_Script(); + target.run(); + } + + /** + * @notice Tests the deployment and execution of the DeploySafeNativeRecovery script + * without a verifier configured. + */ + function test_run_no_verifier() public { + // Set up the base test environment + BaseDeployTest.setUp(); + + // Disable the VERIFIER environment variable + vm.setEnv("ZK_VERIFIER", vm.toString(address(0))); + + // Instantiate the script and run it + DeploySafeNativeRecovery_Script target = new DeploySafeNativeRecovery_Script(); + target.run(); + } + + /** + * @notice Tests the deployment and execution of the DeploySafeNativeRecovery script + * without a DKIM registry configured. + */ + function test_run_no_dkim_registry() public { + // Set up the base test environment + BaseDeployTest.setUp(); + + // Disable the DKIM_REGISTRY environment variable + vm.setEnv("DKIM_REGISTRY", vm.toString(address(0))); + + // Instantiate the script and run it + DeploySafeNativeRecovery_Script target = new DeploySafeNativeRecovery_Script(); + target.run(); + } + + /** + * @notice Tests the deployment and execution of the DeploySafeNativeRecovery script + * without a SIGNER configured. + */ + function test_run_no_signer() public { + // Set up the base test environment + BaseDeployTest.setUp(); + + // Disable the SIGNER environment variable + vm.setEnv("SIGNER", vm.toString(address(0))); + + // Instantiate the script and run it + DeploySafeNativeRecovery_Script target = new DeploySafeNativeRecovery_Script(); + target.run(); + } +} + +contract DeploySafeNativeRecovery_TestFail is BaseDeployTest { + /** + * @notice Tests that the DeploySafeNativeRecovery script fails to run + * when both DKIM registry and signer are not configured. + */ + function testFail_run_no_dkim_registry_no_signer() public { + // Set up the base test environment + BaseDeployTest.setUp(); + + // Disable the DKIM_REGISTRY and SIGNER environment variables + vm.setEnv("DKIM_REGISTRY", vm.toString(address(0))); + vm.setEnv("SIGNER", vm.toString(address(0))); + + // Instantiate the script and attempt to run it, expecting failure + DeploySafeNativeRecovery_Script target = new DeploySafeNativeRecovery_Script(); + target.run(); + } +} diff --git a/script/test/DeploySafeRecovery.t.sol b/script/test/DeploySafeRecovery.t.sol new file mode 100644 index 00000000..e2b87680 --- /dev/null +++ b/script/test/DeploySafeRecovery.t.sol @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.12; + +import "forge-std/Test.sol"; +import "forge-std/console2.sol"; +import { DeploySafeRecovery_Script } from "../DeploySafeRecovery.s.sol"; +import { BaseDeployTest } from "./BaseDeployTest.sol"; + +contract DeploySafeRecovery_Test is BaseDeployTest { + /** + * @notice Tests the standard run scenario. + */ + function test_run() public { + BaseDeployTest.setUp(); + DeploySafeRecovery_Script target = new DeploySafeRecovery_Script(); + target.run(); + } + + /** + * @notice Tests the run function without a verifier set. + */ + function test_run_no_verifier() public { + BaseDeployTest.setUp(); + vm.setEnv("VERIFIER", vm.toString(address(0))); + DeploySafeRecovery_Script target = new DeploySafeRecovery_Script(); + target.run(); + } + + /** + * @notice Tests the run function without a DKIM registry. + */ + function test_run_no_dkim_registry() public { + BaseDeployTest.setUp(); + vm.setEnv("DKIM_REGISTRY", vm.toString(address(0))); + DeploySafeRecovery_Script target = new DeploySafeRecovery_Script(); + target.run(); + } +} diff --git a/script/test/DeployUniversalEmailRecoveryModule.t.sol b/script/test/DeployUniversalEmailRecoveryModule.t.sol new file mode 100644 index 00000000..ebdaf617 --- /dev/null +++ b/script/test/DeployUniversalEmailRecoveryModule.t.sol @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.12; + +import "forge-std/Test.sol"; +import "forge-std/console2.sol"; +import { DeployUniversalEmailRecoveryModuleScript } from + "../DeployUniversalEmailRecoveryModule.s.sol"; +import { BaseDeployTest } from "./BaseDeployTest.sol"; + +/// @title DeployUniversalEmailRecoveryModule_Test +/// @notice Contains tests for deploying the Universal Email Recovery Module +contract DeployUniversalEmailRecoveryModule_Test is BaseDeployTest { + /// @notice Tests the standard deployment run + function test_run() public { + BaseDeployTest.setUp(); + DeployUniversalEmailRecoveryModuleScript target = + new DeployUniversalEmailRecoveryModuleScript(); + target.run(); + } + + /// @notice Tests the deployment run without a verifier + function test_run_no_verifier() public { + BaseDeployTest.setUp(); + vm.setEnv("VERIFIER", vm.toString(address(0))); + DeployUniversalEmailRecoveryModuleScript target = + new DeployUniversalEmailRecoveryModuleScript(); + target.run(); + } + + /// @notice Tests the deployment run without a DKIM registry + function test_run_no_dkim_registry() public { + BaseDeployTest.setUp(); + vm.setEnv("DKIM_REGISTRY", vm.toString(address(0))); + DeployUniversalEmailRecoveryModuleScript target = + new DeployUniversalEmailRecoveryModuleScript(); + target.run(); + } +} + +/// @title DeployUniversalEmailRecoveryModule_TestFail +/// @notice Contains failing tests for deploying the Universal Email Recovery Module +contract DeployUniversalEmailRecoveryModule_TestFail is BaseDeployTest { + /// @notice Tests the deployment run failure without DKIM registry and signer + function testFail_run_no_dkim_registry_no_signer() public { + BaseDeployTest.setUp(); + vm.setEnv("DKIM_REGISTRY", vm.toString(address(0))); + vm.setEnv("SIGNER", vm.toString(address(0))); + DeployUniversalEmailRecoveryModuleScript target = + new DeployUniversalEmailRecoveryModuleScript(); + target.run(); + } +}