Skip to content

Commit

Permalink
ADD: add Unit-test to verify revert any transfer of ETH to Palmera Mo…
Browse files Browse the repository at this point in the history
…dule, Palmera Guard or Palmera Roles
  • Loading branch information
alfredolopez80 committed May 23, 2024
1 parent 98326f1 commit 7e1dac8
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ PalmeraGuardTest:testDisconnectSafeBeforeToRemoveSafe_Two_Level() (gas: 2932360)
PalmeraGuardTest:testDisconnectSafe_As_ROOTSAFE_TARGET_ITSELF_If_Not_Have_children() (gas: 1904860)
PalmeraGuardTest:testDisconnectSafe_As_ROOTSAFE_TARGET_ROOT_SAFE() (gas: 2394819)
PalmeraGuardTest:testDisconnectSafe_As_ROOTSAFE_TARGET_SUPERSAFE_SAME_TREE() (gas: 1706722)
PalmeraGuardTestFallbackAndReceive:testFallbackFunctionNonExistentFunction() (gas: 5738)
PalmeraGuardTestFallbackAndReceive:testFallbackFunctionSendETHWithInvalidData() (gas: 15353)
PalmeraGuardTestFallbackAndReceive:testReceiveFunctionSendETHWithoutData() (gas: 15405)
PalmeraModuleTestFallbackAndReceive:testFallbackFunctionSendETHWithInvalidData() (gas: 16469)
PalmeraModuleTestFallbackAndReceive:testReceiveFunctionSendETHWithoutData() (gas: 15381)
PalmeraRoleDeployTest:testSetUserRoles() (gas: 32281)
PalmeraRoleDeployTest:testSetupRolesCapabilities() (gas: 157726)
PalmeraRolesTest:testCan_PalmeraModule_Setup_RoleContract() (gas: 18152)
Expand All @@ -137,6 +142,9 @@ PalmeraRolesTest:testCannot_ROOT_SAFE_SetRole_ROOT_SAFE_to_EOA_DifferentTree_Saf
PalmeraRolesTest:testCannot_ROOT_SAFE_SetRole_SUPER_SAFE_to_EAO() (gas: 1680827)
PalmeraRolesTest:testCannot_ROOT_SAFE_SetRole_SUPER_SAFE_to_SAFE() (gas: 2304690)
PalmeraRolesTest:testCannot_SUPER_SAFE_SetRole_SAFE_LEAD_to_EAO() (gas: 1680276)
PalmeraRolesTestFallbackAndReceive:testFallbackFunctionNonExistentFunction() (gas: 5963)
PalmeraRolesTestFallbackAndReceive:testFallbackFunctionSendETHWithInvalidData() (gas: 15578)
PalmeraRolesTestFallbackAndReceive:testReceiveFunctionSendETHWithoutData() (gas: 15405)
TestDeploy:testDeploy() (gas: 6284229)
TestDeploySafe:testTransferFundsSafe() (gas: 109281)
TestEnableGuard:testEnablePalmeraGuard() (gas: 120836)
Expand Down
4 changes: 3 additions & 1 deletion src/PalmeraGuard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ contract PalmeraGuard is BaseGuard, Context {
}

/// @notice Fallback function: called when someone sends ETH or calls a function that does not exist
fallback() external {}
fallback() external {
revert("Fallback function called");
}

/// @notice Receive function: called when someone sends ETH to the contract without data
receive() external payable {
Expand Down
4 changes: 3 additions & 1 deletion src/PalmeraRoles.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ contract PalmeraRoles is RolesAuthority, ValidAddress {
}

/// @notice Fallback function: called when someone sends ETH or calls a function that does not exist
fallback() external {}
fallback() external {
revert("Fallback function called");
}

/// @notice Receive function: called when someone sends ETH to the contract without data
receive() external payable {
Expand Down
43 changes: 43 additions & 0 deletions test/PalmeraGuardTestFallbackAndReceive.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.15;

import "./helpers/DeployHelper.t.sol";

contract PalmeraGuardTestFallbackAndReceive is DeployHelper {
/// @notice Set up the environment for testing
function setUp() public {
deployAllContracts(60);
}
/// @notice Calling a non-existent function

function testFallbackFunctionNonExistentFunction() public {
(bool success,) = address(palmeraGuard).call(
abi.encodeWithSignature("nonExistentFunction()")
);
assertFalse(
success,
"Fallback function should revert on non-existent function call"
);
}

/// @notice Sending ETH without data
function testReceiveFunctionSendETHWithoutData() public {
vm.deal(address(this), 1 ether); // Give this contract 1 ether to work with
(bool success,) = address(palmeraGuard).call{value: 1 ether}("");
assertFalse(
success, "Receive function should revert on ETH send without data"
);
}

/// @notice Sending ETH with data that does not match any function
function testFallbackFunctionSendETHWithInvalidData() public {
vm.deal(address(this), 1 ether); // Give this contract 1 ether to work with
(bool success,) = address(palmeraGuard).call{value: 1 ether}(
abi.encodeWithSignature("checkTransaction()")
);
assertFalse(
success,
"Fallback function should revert on ETH send with invalid data"
);
}
}
32 changes: 32 additions & 0 deletions test/PalmeraModuleTestFallbackAndReceive.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.15;

import "./helpers/DeployHelper.t.sol";

contract PalmeraModuleTestFallbackAndReceive is DeployHelper {
/// @notice Set up the environment for testing
function setUp() public {
deployAllContracts(60);
}

/// @notice Sending ETH without data
function testReceiveFunctionSendETHWithoutData() public {
vm.deal(address(this), 1 ether); // Give this contract 1 ether to work with
(bool success,) = address(palmeraModule).call{value: 1 ether}("");
assertFalse(
success, "Receive function should revert on ETH send without data"
);
}

/// @notice Sending ETH with data that does not match any function
function testFallbackFunctionSendETHWithInvalidData() public {
vm.deal(address(this), 1 ether); // Give this contract 1 ether to work with
(bool success,) = address(palmeraModule).call{value: 1 ether}(
abi.encodeWithSignature("execTransactionOnBehalf()")
);
assertFalse(
success,
"Fallback function should revert on ETH send with invalid data"
);
}
}
43 changes: 43 additions & 0 deletions test/PalmeraRolesTestFallbackAndReceive.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.15;

import "./helpers/DeployHelper.t.sol";

contract PalmeraRolesTestFallbackAndReceive is DeployHelper {
/// @notice Set up the environment for testing
function setUp() public {
deployAllContracts(60);
}
/// @notice Calling a non-existent function

function testFallbackFunctionNonExistentFunction() public {
(bool success,) = address(palmeraRolesContract).call(
abi.encodeWithSignature("nonExistentFunction()")
);
assertFalse(
success,
"Fallback function should revert on non-existent function call"
);
}

/// @notice Sending ETH without data
function testReceiveFunctionSendETHWithoutData() public {
vm.deal(address(this), 1 ether); // Give this contract 1 ether to work with
(bool success,) = address(palmeraRolesContract).call{value: 1 ether}("");
assertFalse(
success, "Receive function should revert on ETH send without data"
);
}

/// @notice Sending ETH with data that does not match any function
function testFallbackFunctionSendETHWithInvalidData() public {
vm.deal(address(this), 1 ether); // Give this contract 1 ether to work with
(bool success,) = address(palmeraRolesContract).call{value: 1 ether}(
abi.encodeWithSignature("setUserRole()")
);
assertFalse(
success,
"Fallback function should revert on ETH send with invalid data"
);
}
}

0 comments on commit 7e1dac8

Please sign in to comment.