Skip to content

Commit

Permalink
add cctp unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
hexonaut committed Jun 5, 2024
1 parent 9d081f1 commit 747c428
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 15 deletions.
19 changes: 4 additions & 15 deletions test/AMBReceiver.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pragma solidity >=0.8.0;

import "forge-std/Test.sol";

import { TargetContractMock } from "test/mocks/TargetContractMock.sol";

import { AMBReceiver } from "src/receivers/AMBReceiver.sol";

contract AMBMock {
Expand All @@ -25,20 +27,6 @@ contract AMBMock {

}

contract TargetContractMock {

uint256 public s;

function someFunc() external {
s++;
}

function revertFunc() external pure {
revert("error");
}

}

contract AMBReceiverTest is Test {

AMBMock amb;
Expand Down Expand Up @@ -99,9 +87,10 @@ contract AMBReceiverTest is Test {
}

function test_forward_success() public {
assertEq(target.data(), 0);
vm.prank(address(amb));
receiver.forward(abi.encodeCall(TargetContractMock.someFunc, ()));
assertEq(target.s(), 1);
assertEq(target.data(), 1);
}

function test_forward_revert() public {
Expand Down
97 changes: 97 additions & 0 deletions test/CCTPReceiver.t.sol
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, ())
);
}

}
16 changes: 16 additions & 0 deletions test/mocks/TargetContractMock.sol
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");
}

}

0 comments on commit 747c428

Please sign in to comment.