Skip to content

Commit

Permalink
fix test on invariants and fuzz
Browse files Browse the repository at this point in the history
  • Loading branch information
Aboudjem committed May 19, 2024
1 parent a659799 commit ab18e03
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 40 deletions.
6 changes: 1 addition & 5 deletions test/foundry/invariant/handlers/ExecutionHandlerTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ contract ExecutionHandlerTest is InvariantBaseTest {
/// @param amount The amount to be deposited.
function invariant_handleIncrement(uint256 amount) external {
Execution[] memory executions = new Execution[](1);
executions[0] = Execution({
target: address(nexusAccount),
value: amount,
callData: abi.encodeWithSignature("addDeposit()")
});
executions[0] = Execution({ target: address(nexusAccount), value: amount, callData: abi.encodeWithSignature("addDeposit()") });

// Execute operation through ENTRYPOINT and verify post-operation conditions
PackedUserOperation[] memory userOps = buildPackedUserOperation(
Expand Down
2 changes: 1 addition & 1 deletion test/foundry/invariant/test/ExecutorInvariantTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ contract ExecutorInvariantTest is NexusTest_Base {
/// @notice Sets up the test environment by initializing the Nexus account and installing the valid executor module
function setUp() public {
init(); // Initialize environment which includes deploying Nexus as BOB_ACCOUNT

// Deploy the executors
validExecutor = new MockExecutor();
invalidExecutor = new MockExecutor();
Expand Down
26 changes: 21 additions & 5 deletions test/foundry/invariant/test/ModuleManagerInvariantTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,26 @@ contract ModuleManagerInvariantTest is InvariantBaseTest {

/// @notice Ensures that a module remains installed after a test cycle
function invariant_moduleInstallation() public {
assertTrue(handler.invariant_checkModuleInstalled(MODULE_TYPE_VALIDATOR, address(VALIDATOR_MODULE)), "Invariant failed: Module should be installed.");
assertTrue(
handler.invariant_checkModuleInstalled(MODULE_TYPE_VALIDATOR, address(VALIDATOR_MODULE)),
"Invariant failed: Module should be installed."
);
}

/// @notice Ensures that a module remains uninstalled after a test cycle
function invariant_moduleUninstallation() public {
assertFalse(handler.invariant_checkModuleInstalled(MODULE_TYPE_EXECUTOR, address(mockExecutor)), "Invariant failed: Module should be uninstalled.");
assertFalse(
handler.invariant_checkModuleInstalled(MODULE_TYPE_EXECUTOR, address(mockExecutor)),
"Invariant failed: Module should be uninstalled."
);
}

/// @notice Checks the persistent installation of the Validator module
function invariantTest_ValidatorModuleInstalled() public {
assertTrue(BOB_ACCOUNT.isModuleInstalled(MODULE_TYPE_VALIDATOR, address(VALIDATOR_MODULE), ""), "Validator Module should be consistently installed.");
assertTrue(
BOB_ACCOUNT.isModuleInstalled(MODULE_TYPE_VALIDATOR, address(VALIDATOR_MODULE), ""),
"Validator Module should be consistently installed."
);
}

/// @notice Ensures that no duplicate installations occur
Expand All @@ -67,7 +76,11 @@ contract ModuleManagerInvariantTest is InvariantBaseTest {
PackedUserOperation[] memory userOps = buildPackedUserOperation(BOB, BOB_ACCOUNT, EXECTYPE_DEFAULT, executions, address(VALIDATOR_MODULE));

bytes32 userOpHash = ENTRYPOINT.getUserOpHash(userOps[0]);
bytes memory expectedRevertReason = abi.encodeWithSignature("ModuleAlreadyInstalled(uint256,address)", MODULE_TYPE_VALIDATOR, address(VALIDATOR_MODULE));
bytes memory expectedRevertReason = abi.encodeWithSignature(
"ModuleAlreadyInstalled(uint256,address)",
MODULE_TYPE_VALIDATOR,
address(VALIDATOR_MODULE)
);

// Expect the UserOperationRevertReason event
vm.expectEmit(true, true, true, true);
Expand All @@ -79,7 +92,10 @@ contract ModuleManagerInvariantTest is InvariantBaseTest {
/// @notice Ensures that non-installed modules are not reported as installed
function invariantTest_AbsenceOfNonInstalledModules() public {
// Check that non-installed modules are not mistakenly installed
assertFalse(BOB_ACCOUNT.isModuleInstalled(MODULE_TYPE_EXECUTOR, address(mockExecutor), ""), "Executor Module should not be installed initially.");
assertFalse(
BOB_ACCOUNT.isModuleInstalled(MODULE_TYPE_EXECUTOR, address(mockExecutor), ""),
"Executor Module should not be installed initially."
);
assertFalse(BOB_ACCOUNT.isModuleInstalled(MODULE_TYPE_HOOK, address(mockHook), ""), "Hook Module should not be installed initially.");
}
}
1 change: 0 additions & 1 deletion test/foundry/invariant/test/NexusInvariantTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import "../../utils/NexusTest_Base.t.sol";
/// @title NexusInvariantTest
/// @notice This contract tests invariants related to Nexus, ensuring execution consistency and proper nonce handling.
contract NexusInvariantTest is NexusTest_Base {

/// @notice Initializes the testing environment
function setUp() public {
init(); // Initialize environment which includes deploying Nexus as BOB_ACCOUNT
Expand Down
2 changes: 1 addition & 1 deletion test/foundry/shared/TestAccountExecution_Base.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ abstract contract TestAccountExecution_Base is NexusTest_Base {
token.transfer(address(ALICE_ACCOUNT), amountToEach);
token.transfer(address(CHARLIE_ACCOUNT), amountToEach);
}
}
}
8 changes: 1 addition & 7 deletions test/foundry/shared/TestModuleManagement_Base.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ abstract contract TestModuleManagement_Base is NexusTest_Base {
/// @param moduleTypeId The type ID of the module
/// @param moduleAddress The address of the module
/// @param execType The execution type for the operation
function installModule(
bytes memory callData,
uint256 moduleTypeId,
address moduleAddress,
ExecType execType
) internal {
function installModule(bytes memory callData, uint256 moduleTypeId, address moduleAddress, ExecType execType) internal {
Execution[] memory execution = new Execution[](1);
execution[0] = Execution(address(BOB_ACCOUNT), 0, callData);

Expand All @@ -62,4 +57,3 @@ abstract contract TestModuleManagement_Base is NexusTest_Base {
ENTRYPOINT.handleOps(userOps, payable(BOB.addr));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ contract TestAccountExecution_TryExecuteBatch is TestAccountExecution_Base {
assertEq(remainingAllowance, approvalAmount - transferAmount, "The remaining allowance should reflect the transferred amount");

uint256 aliceBalanceAfter = token.balanceOf(address(ALICE_ACCOUNT));
assertEq(aliceBalanceAfter, aliceBalanceBefore + transferAmount, "Alice should receive tokens via transferFrom");
assertEq(aliceBalanceAfter, aliceBalanceBefore + transferAmount, "Alice should receive tokens via transferFrom");
}

/// @notice Tests approval and transferFrom operations within a single execution.
Expand Down Expand Up @@ -217,4 +217,4 @@ contract TestAccountExecution_TryExecuteBatch is TestAccountExecution_Base {
uint256 aliceBalanceAfter = token.balanceOf(address(ALICE_ACCOUNT));
assertEq(aliceBalanceAfter, aliceBalanceBefore + transferAmount, "Alice should receive tokens via transferFrom");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,15 @@ contract TestERC1271Account_MockProtocol is NexusTest_Base {
AccountDomainStruct memory t;
(t.fields, t.name, t.version, t.chainId, t.verifyingContract, t.salt, t.extensions) = Nexus(account).eip712Domain();

return abi.encode(
t.fields,
keccak256(bytes(t.name)),
keccak256(bytes(t.version)),
t.chainId,
t.verifyingContract,
t.salt,
keccak256(abi.encodePacked(t.extensions))
);
return
abi.encode(
t.fields,
keccak256(bytes(t.name)),
keccak256(bytes(t.version)),
t.chainId,
t.verifyingContract,
t.salt,
keccak256(abi.encodePacked(t.extensions))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,15 @@ contract TestERC1271Account_MockProtocol is NexusTest_Base {
AccountDomainStruct memory t;
(t.fields, t.name, t.version, t.chainId, t.verifyingContract, t.salt, t.extensions) = Nexus(account).eip712Domain();

return abi.encode(
t.fields,
keccak256(bytes(t.name)),
keccak256(bytes(t.version)),
t.chainId,
t.verifyingContract,
t.salt,
keccak256(abi.encodePacked(t.extensions))
);
return
abi.encode(
t.fields,
keccak256(bytes(t.name)),
keccak256(bytes(t.version)),
t.chainId,
t.verifyingContract,
t.salt,
keccak256(abi.encodePacked(t.extensions))
);
}
}

0 comments on commit ab18e03

Please sign in to comment.