Skip to content
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

Merged
merged 7 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,25 @@ pnpm install
### Build

```shell
forge build
pnpm build
# or
# forge build
```

### Test

```shell
forge test
pnpm test
# or
# forge test --match-path "test/**/*.sol" or
```

### Test for scripts

```shell
pnpm test:script
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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

# or
# forge test --match-path "script/test/**/*.sol" --threads 1
```

# ZK Email Recovery
Expand Down
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ignored_warnings_from = [
"node_modules/@rhinestone/modulekit/src/**/*.sol",
"lib",
]

threads = 1
[rpc_endpoints]
sepolia = "${BASE_SEPOLIA_RPC_URL}"

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
"gas:snapshot:optimized": "pnpm run build:optimized && FOUNDRY_PROFILE=test-optimized forge snapshot --mp \"./test/integration/**/*.sol\"",
"lint": "forge fmt --check && pnpm solhint \"{script,src,test}/**/*.sol\"",
"prepack": "pnpm install",
"test": "forge test",
"prettier:check": "prettier --check \"**/*.{json,md,svg,yml}\"",
"prettier:write": "prettier --write \"**/*.{json,md,svg,yml}\"",
"test": "forge test --match-path \"test/**/*.sol\"",
"test:script": "forge test --match-path script/test/**/*.sol --threads 1",
"test:lite": "FOUNDRY_PROFILE=lite forge test",
"test:optimized": "pnpm run build:optimized && FOUNDRY_PROFILE=test-optimized forge test"
},
Expand Down
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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing cases for no emailAuthImpl, validatorAddr and _factory?

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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testFail... may be deprecated in the future foundry-rs/foundry#4437. Don't think it is worth changing here now but FYI

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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test cases for no emailAuthImpl and commandHandler

// 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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test cases for no dkimRegistrySigner and emailAuthImpl?

BaseDeployTest.setUp();
vm.setEnv("DKIM_REGISTRY", vm.toString(address(0)));
DeploySafeRecovery_Script target = new DeploySafeRecovery_Script();
target.run();
}
}
Loading