-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/script tests #49
Changes from all commits
8a0a6c1
da559ec
b01a231
d4d4a20
10cc285
fee3ba7
f2d35c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)); | ||
} | ||
} |
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(); | ||
} | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing cases for no |
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
vm.setEnv("DKIM_REGISTRY", vm.toString(address(0))); | ||
vm.setEnv("SIGNER", vm.toString(address(0))); | ||
DeployEmailRecoveryModuleScript target = new DeployEmailRecoveryModuleScript(); | ||
target.run(); | ||
} | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing test cases for no |
||
// 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(); | ||
} | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing test cases for no |
||
BaseDeployTest.setUp(); | ||
vm.setEnv("DKIM_REGISTRY", vm.toString(address(0))); | ||
DeploySafeRecovery_Script target = new DeploySafeRecovery_Script(); | ||
target.run(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add
--threads 1
to the test command because the tests for scripts might fail with multiple threads due to conflicts among env settings.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SoraSuegami pnpm test:script command already has this option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SoraSuegami I've added thread 1 option to foundry.toml