-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
117 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
pragma solidity >=0.8.0; | ||
|
||
import "forge-std/Test.sol"; | ||
|
||
import { TargetContractMock } from "test/mocks/TargetContractMock.sol"; | ||
|
||
import { CCTPReceiver } from "src/receivers/CCTPReceiver.sol"; | ||
|
||
contract CCTPReceiverTest is Test { | ||
|
||
TargetContractMock target; | ||
|
||
CCTPReceiver receiver; | ||
|
||
address destinationMessenger = makeAddr("destinationMessenger"); | ||
uint32 sourceDomainId = 1; | ||
address sourceAuthority = makeAddr("sourceAuthority"); | ||
address randomAddress = makeAddr("randomAddress"); | ||
|
||
function setUp() public { | ||
target = new TargetContractMock(); | ||
|
||
receiver = new CCTPReceiver( | ||
destinationMessenger, | ||
sourceDomainId, | ||
sourceAuthority, | ||
address(target) | ||
); | ||
} | ||
|
||
function test_constructor() public { | ||
receiver = new CCTPReceiver( | ||
destinationMessenger, | ||
sourceDomainId, | ||
sourceAuthority, | ||
address(target) | ||
); | ||
|
||
assertEq(receiver.destinationMessenger(), destinationMessenger); | ||
assertEq(receiver.sourceDomainId(), sourceDomainId); | ||
assertEq(receiver.sourceAuthority(), sourceAuthority); | ||
assertEq(receiver.target(), address(target)); | ||
} | ||
|
||
function test_handleReceiveMessage_invalidSender() public { | ||
vm.prank(randomAddress); | ||
vm.expectRevert("CCTPReceiver/invalid-sender"); | ||
receiver.handleReceiveMessage( | ||
sourceDomainId, | ||
bytes32(uint256(uint160(sourceAuthority))), | ||
abi.encodeCall(TargetContractMock.someFunc, ()) | ||
); | ||
} | ||
|
||
function test_handleReceiveMessage_invalidSourceChainId() public { | ||
vm.prank(destinationMessenger); | ||
vm.expectRevert("CCTPReceiver/invalid-sourceDomain"); | ||
receiver.handleReceiveMessage( | ||
2, | ||
bytes32(uint256(uint160(sourceAuthority))), | ||
abi.encodeCall(TargetContractMock.someFunc, ()) | ||
); | ||
} | ||
|
||
function test_handleReceiveMessage_invalidSourceAuthority() public { | ||
vm.prank(destinationMessenger); | ||
vm.expectRevert("CCTPReceiver/invalid-sourceAuthority"); | ||
receiver.handleReceiveMessage( | ||
sourceDomainId, | ||
bytes32(uint256(uint160(randomAddress))), | ||
abi.encodeCall(TargetContractMock.someFunc, ()) | ||
); | ||
} | ||
|
||
function test_handleReceiveMessage_success() public { | ||
assertEq(target.data(), 0); | ||
vm.prank(destinationMessenger); | ||
receiver.handleReceiveMessage( | ||
sourceDomainId, | ||
bytes32(uint256(uint160(sourceAuthority))), | ||
abi.encodeCall(TargetContractMock.someFunc, ()) | ||
); | ||
assertEq(target.data(), 1); | ||
} | ||
|
||
function test_handleReceiveMessage_revert() public { | ||
vm.prank(destinationMessenger); | ||
vm.expectRevert("error"); | ||
receiver.handleReceiveMessage( | ||
sourceDomainId, | ||
bytes32(uint256(uint160(sourceAuthority))), | ||
abi.encodeCall(TargetContractMock.revertFunc, ()) | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
pragma solidity >=0.8.0; | ||
|
||
contract TargetContractMock { | ||
|
||
uint256 public data; | ||
|
||
function someFunc() external { | ||
data++; | ||
} | ||
|
||
function revertFunc() external pure { | ||
revert("error"); | ||
} | ||
|
||
} |