Skip to content

Commit

Permalink
test for zero addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Aug 7, 2024
1 parent c730f4c commit 99328df
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
9 changes: 9 additions & 0 deletions v2/src/evm/GatewayEVM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ contract GatewayEVM is
nonReentrant
returns (bytes memory)
{
if (destination == address(0)) revert ZeroAddress();
bytes memory result = _execute(destination, data);

emit Executed(destination, msg.value, data);
Expand All @@ -156,6 +157,7 @@ contract GatewayEVM is
nonReentrant
{
if (amount == 0) revert InsufficientERC20Amount();
if (to == address(0)) revert ZeroAddress();
// Approve the target contract to spend the tokens
if (!resetApproval(token, to)) revert ApprovalFailed();
if (!IERC20(token).approve(to, amount)) revert ApprovalFailed();
Expand Down Expand Up @@ -192,6 +194,7 @@ contract GatewayEVM is
nonReentrant
{
if (amount == 0) revert InsufficientERC20Amount();
if (to == address(0)) revert ZeroAddress();

IERC20(token).safeTransfer(address(to), amount);
Revertable(to).onRevert(data);
Expand All @@ -203,6 +206,8 @@ contract GatewayEVM is
/// @param receiver Address of the receiver.
function deposit(address receiver) external payable whenNotPaused nonReentrant {
if (msg.value == 0) revert InsufficientETHAmount();
if (receiver == address(0)) revert ZeroAddress();

(bool deposited,) = tssAddress.call{ value: msg.value }("");

if (!deposited) revert DepositFailed();
Expand All @@ -216,6 +221,7 @@ contract GatewayEVM is
/// @param asset Address of the ERC20 token.
function deposit(address receiver, uint256 amount, address asset) external whenNotPaused nonReentrant {
if (amount == 0) revert InsufficientERC20Amount();
if (receiver == address(0)) revert ZeroAddress();

transferFromToAssetHandler(msg.sender, asset, amount);

Expand All @@ -227,6 +233,8 @@ contract GatewayEVM is
/// @param payload Calldata to pass to the call.
function depositAndCall(address receiver, bytes calldata payload) external payable whenNotPaused nonReentrant {
if (msg.value == 0) revert InsufficientETHAmount();
if (receiver == address(0)) revert ZeroAddress();

(bool deposited,) = tssAddress.call{ value: msg.value }("");

if (!deposited) revert DepositFailed();
Expand All @@ -250,6 +258,7 @@ contract GatewayEVM is
nonReentrant
{
if (amount == 0) revert InsufficientERC20Amount();
if (receiver == address(0)) revert ZeroAddress();

transferFromToAssetHandler(msg.sender, asset, amount);

Expand Down
19 changes: 19 additions & 0 deletions v2/test/ERC20Custody.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ contract ERC20CustodyTest is Test, IGatewayEVMErrors, IGatewayEVMEvents, IReceiv
custody.withdrawAndCall(address(token), address(receiver), amount, data);
}

function testForwardCallToReceiveERC20ThroughCustodyFailsIfReceiverIsZeroAddress() public {
uint256 amount = 1;
bytes memory data =
abi.encodeWithSignature("receiveERC20(uint256,address,address)", amount, address(token), destination);

vm.prank(tssAddress);
vm.expectRevert(ZeroAddress.selector);
custody.withdrawAndCall(address(token), address(0), amount, data);
}

function testForwardCallToReceiveERC20PartialThroughCustody() public {
uint256 amount = 100_000;
bytes memory data =
Expand Down Expand Up @@ -365,4 +375,13 @@ contract ERC20CustodyTest is Test, IGatewayEVMErrors, IGatewayEVMEvents, IReceiv
vm.expectRevert(InsufficientERC20Amount.selector);
custody.withdrawAndRevert(address(token), address(receiver), amount, data);
}

function testWithdrawAndRevertThroughCustodyFailsIfReceiverIsZeroAddress() public {
uint256 amount = 1;
bytes memory data = abi.encodePacked("hello");

vm.prank(tssAddress);
vm.expectRevert(ZeroAddress.selector);
custody.withdrawAndRevert(address(token), address(0), amount, data);
}
}
38 changes: 38 additions & 0 deletions v2/test/GatewayEVM.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ contract GatewayEVMTest is Test, IGatewayEVMErrors, IGatewayEVMEvents, IReceiver
gateway.execute(address(receiver), data);
}

function testExecuteFailsIfDestinationIsZeroAddress() public {
bytes memory data = abi.encodeWithSignature("receiveNoParams()");

vm.prank(tssAddress);
vm.expectRevert(ZeroAddress.selector);
gateway.execute(address(0), data);
}

function testForwardCallToReceiveNoParamsTogglePause() public {
vm.prank(tssAddress);
vm.expectRevert(abi.encodeWithSelector(AccessControlUnauthorizedAccount.selector, tssAddress, PAUSER_ROLE));
Expand Down Expand Up @@ -317,6 +325,12 @@ contract GatewayEVMInboundTest is Test, IGatewayEVMErrors, IGatewayEVMEvents, IR
gateway.deposit(destination, amount, address(token));
}

function testFailDepositERC20ToCustodyIfReceiverIsZeroAddress() public {
uint256 amount = 1;
vm.expectRevert("ZeroAddress");
gateway.deposit(address(0), amount, address(token));
}

function testDepositEthToTss() public {
uint256 amount = 100_000;
uint256 tssBalanceBefore = tssAddress.balance;
Expand All @@ -336,6 +350,13 @@ contract GatewayEVMInboundTest is Test, IGatewayEVMErrors, IGatewayEVMEvents, IR
gateway.deposit{ value: amount }(destination);
}

function testFailDepositEthToTssIfReceiverIsZeroAddress() public {
uint256 amount = 1;

vm.expectRevert("ZeroAddress");
gateway.deposit{ value: amount }(address(0));
}

function testDepositERC20ToCustodyWithPayload() public {
uint256 amount = 100_000;
uint256 custodyBalanceBefore = token.balanceOf(address(custody));
Expand Down Expand Up @@ -365,6 +386,15 @@ contract GatewayEVMInboundTest is Test, IGatewayEVMErrors, IGatewayEVMEvents, IR
gateway.depositAndCall(destination, amount, address(token), payload);
}

function testFailDepositERC20ToCustodyWithPayloadIfReceiverIsZeroAddress() public {
uint256 amount = 1;

bytes memory payload = abi.encodeWithSignature("hello(address)", destination);

vm.expectRevert("ZeroAddress");
gateway.depositAndCall(address(0), amount, address(token), payload);
}

function testDepositEthToTssWithPayload() public {
uint256 amount = 100_000;
uint256 tssBalanceBefore = tssAddress.balance;
Expand All @@ -386,6 +416,14 @@ contract GatewayEVMInboundTest is Test, IGatewayEVMErrors, IGatewayEVMEvents, IR
gateway.depositAndCall{ value: amount }(destination, payload);
}

function testFailDepositEthToTssWithPayloadIfReceiverIsZeroAddress() public {
uint256 amount = 1;
bytes memory payload = abi.encodeWithSignature("hello(address)", destination);

vm.expectRevert("ZeroAddress");
gateway.depositAndCall{ value: amount }(address(0), payload);
}

function testCallWithPayload() public {
bytes memory payload = abi.encodeWithSignature("hello(address)", destination);

Expand Down

0 comments on commit 99328df

Please sign in to comment.