Skip to content

Commit

Permalink
Add tests for scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
wshino committed Sep 22, 2024
1 parent d4d4a20 commit 10cc285
Show file tree
Hide file tree
Showing 6 changed files with 373 additions and 0 deletions.
107 changes: 107 additions & 0 deletions script/test/BaseDeployTest.sol
Original file line number Diff line number Diff line change
@@ -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));
}
}
18 changes: 18 additions & 0 deletions script/test/Compute7579RecoveryDataHash.t.sol
Original file line number Diff line number Diff line change
@@ -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();
}
}
70 changes: 70 additions & 0 deletions script/test/DeployEmailRecoveryModule.t.sol
Original file line number Diff line number Diff line change
@@ -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();
}
}
88 changes: 88 additions & 0 deletions script/test/DeploySafeNativeRecovery.t.sol
Original file line number Diff line number Diff line change
@@ -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();
}
}
38 changes: 38 additions & 0 deletions script/test/DeploySafeRecovery.t.sol
Original file line number Diff line number Diff line change
@@ -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();
}
}
52 changes: 52 additions & 0 deletions script/test/DeployUniversalEmailRecoveryModule.t.sol
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 10cc285

Please sign in to comment.