Skip to content

Commit

Permalink
Merge pull request #18 from marsfoundation/SC-437-fix-coverage
Browse files Browse the repository at this point in the history
[SC-437] Fixing coverage by adding unit tests
  • Loading branch information
hexonaut authored Jun 12, 2024
2 parents f222fe2 + fd8a5f8 commit 1b6d35f
Show file tree
Hide file tree
Showing 11 changed files with 409 additions and 23 deletions.
10 changes: 5 additions & 5 deletions src/testing/bridges/AMBBridgeTesting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ pragma solidity >=0.8.0;

import { Vm } from "forge-std/Vm.sol";

import { Bridge } from "src/testing/Bridge.sol";
import { Domain, DomainHelpers } from "src/testing/Domain.sol";
import { RecordedLogs } from "src/testing/utils/RecordedLogs.sol";
import { Bridge } from "../Bridge.sol";
import { Domain, DomainHelpers } from "../Domain.sol";
import { RecordedLogs } from "../utils/RecordedLogs.sol";

interface IAMB {
function validatorContract() external view returns (address);
Expand Down Expand Up @@ -71,7 +71,7 @@ library AMBBridgeTesting {
return bridge;
}

function relayMessagesToDestination(Bridge memory bridge, bool switchToDestinationFork) internal {
function relayMessagesToDestination(Bridge storage bridge, bool switchToDestinationFork) internal {
bridge.destination.selectFork();

Vm.Log[] memory logs = bridge.ingestAndFilterLogs(true, USER_REQUEST_FOR_AFFIRMATION_TOPIC, USER_REQUEST_FOR_SIGNATURE_TOPIC, bridge.sourceCrossChainMessenger);
Expand All @@ -82,7 +82,7 @@ library AMBBridgeTesting {
}
}

function relayMessagesToSource(Bridge memory bridge, bool switchToSourceFork) internal {
function relayMessagesToSource(Bridge storage bridge, bool switchToSourceFork) internal {
bridge.source.selectFork();

Vm.Log[] memory logs = bridge.ingestAndFilterLogs(false, USER_REQUEST_FOR_AFFIRMATION_TOPIC, USER_REQUEST_FOR_SIGNATURE_TOPIC, bridge.destinationCrossChainMessenger);
Expand Down
10 changes: 5 additions & 5 deletions src/testing/bridges/ArbitrumBridgeTesting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ pragma solidity >=0.8.0;

import { Vm } from "forge-std/Vm.sol";

import { Bridge } from "src/testing/Bridge.sol";
import { Domain, DomainHelpers } from "src/testing/Domain.sol";
import { RecordedLogs } from "src/testing/utils/RecordedLogs.sol";
import { Bridge } from "../Bridge.sol";
import { Domain, DomainHelpers } from "../Domain.sol";
import { RecordedLogs } from "../utils/RecordedLogs.sol";

interface InboxLike {
function createRetryableTicket(
Expand Down Expand Up @@ -122,7 +122,7 @@ library ArbitrumBridgeTesting {
return bridge;
}

function relayMessagesToDestination(Bridge memory bridge, bool switchToDestinationFork) internal {
function relayMessagesToDestination(Bridge storage bridge, bool switchToDestinationFork) internal {
bridge.destination.selectFork();

Vm.Log[] memory logs = RecordedLogs.getLogs();
Expand All @@ -149,7 +149,7 @@ library ArbitrumBridgeTesting {
}
}

function relayMessagesToSource(Bridge memory bridge, bool switchToSourceFork) internal {
function relayMessagesToSource(Bridge storage bridge, bool switchToSourceFork) internal {
bridge.source.selectFork();

Vm.Log[] memory logs = bridge.ingestAndFilterLogs(false, SEND_TO_L1_TOPIC, bridge.destinationCrossChainMessenger);
Expand Down
10 changes: 5 additions & 5 deletions src/testing/bridges/CCTPBridgeTesting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ pragma solidity >=0.8.0;

import { Vm } from "forge-std/Vm.sol";

import { Bridge } from "src/testing/Bridge.sol";
import { Domain, DomainHelpers } from "src/testing/Domain.sol";
import { RecordedLogs } from "src/testing/utils/RecordedLogs.sol";
import { Bridge } from "../Bridge.sol";
import { Domain, DomainHelpers } from "../Domain.sol";
import { RecordedLogs } from "../utils/RecordedLogs.sol";

interface IMessenger {
function receiveMessage(bytes calldata message, bytes calldata attestation) external returns (bool success);
Expand Down Expand Up @@ -71,7 +71,7 @@ library CCTPBridgeTesting {
return bridge;
}

function relayMessagesToDestination(Bridge memory bridge, bool switchToDestinationFork) internal {
function relayMessagesToDestination(Bridge storage bridge, bool switchToDestinationFork) internal {
bridge.destination.selectFork();

Vm.Log[] memory logs = bridge.ingestAndFilterLogs(true, SENT_MESSAGE_TOPIC, bridge.sourceCrossChainMessenger);
Expand All @@ -84,7 +84,7 @@ library CCTPBridgeTesting {
}
}

function relayMessagesToSource(Bridge memory bridge, bool switchToSourceFork) internal {
function relayMessagesToSource(Bridge storage bridge, bool switchToSourceFork) internal {
bridge.source.selectFork();

Vm.Log[] memory logs = bridge.ingestAndFilterLogs(false, SENT_MESSAGE_TOPIC, bridge.destinationCrossChainMessenger);
Expand Down
10 changes: 5 additions & 5 deletions src/testing/bridges/OptimismBridgeTesting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ pragma solidity >=0.8.0;

import { Vm } from "forge-std/Vm.sol";

import { Bridge } from "src/testing/Bridge.sol";
import { Domain, DomainHelpers } from "src/testing/Domain.sol";
import { RecordedLogs } from "src/testing/utils/RecordedLogs.sol";
import { Bridge } from "../Bridge.sol";
import { Domain, DomainHelpers } from "../Domain.sol";
import { RecordedLogs } from "../utils/RecordedLogs.sol";

interface IMessenger {
function sendMessage(
Expand Down Expand Up @@ -78,7 +78,7 @@ library OptimismBridgeTesting {
return bridge;
}

function relayMessagesToDestination(Bridge memory bridge, bool switchToDestinationFork) internal {
function relayMessagesToDestination(Bridge storage bridge, bool switchToDestinationFork) internal {
bridge.destination.selectFork();

address malias;
Expand All @@ -100,7 +100,7 @@ library OptimismBridgeTesting {
}
}

function relayMessagesToSource(Bridge memory bridge, bool switchToSourceFork) internal {
function relayMessagesToSource(Bridge storage bridge, bool switchToSourceFork) internal {
bridge.source.selectFork();

Vm.Log[] memory logs = bridge.ingestAndFilterLogs(false, SENT_MESSAGE_TOPIC, bridge.destinationCrossChainMessenger);
Expand Down
6 changes: 3 additions & 3 deletions src/testing/utils/RecordedLogs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity >=0.8.0;

import { Vm } from "forge-std/Vm.sol";

import { Bridge } from "src/testing/Bridge.sol";
import { Bridge } from "../Bridge.sol";

library RecordedLogs {

Expand Down Expand Up @@ -33,7 +33,7 @@ library RecordedLogs {
return logs;
}

function ingestAndFilterLogs(Bridge memory bridge, bool sourceToDestination, bytes32 topic0, bytes32 topic1, address emitter) internal returns (Vm.Log[] memory filteredLogs) {
function ingestAndFilterLogs(Bridge storage bridge, bool sourceToDestination, bytes32 topic0, bytes32 topic1, address emitter) internal returns (Vm.Log[] memory filteredLogs) {
Vm.Log[] memory logs = RecordedLogs.getLogs();
uint256 lastIndex = sourceToDestination ? bridge.lastSourceLogIndex : bridge.lastDestinationLogIndex;
uint256 pushedIndex = 0;
Expand All @@ -53,7 +53,7 @@ library RecordedLogs {
assembly { mstore(filteredLogs, pushedIndex) }
}

function ingestAndFilterLogs(Bridge memory bridge, bool sourceToDestination, bytes32 topic, address emitter) internal returns (Vm.Log[] memory filteredLogs) {
function ingestAndFilterLogs(Bridge storage bridge, bool sourceToDestination, bytes32 topic, address emitter) internal returns (Vm.Log[] memory filteredLogs) {
return ingestAndFilterLogs(bridge, sourceToDestination, topic, bytes32(0), emitter);
}

Expand Down
102 changes: 102 additions & 0 deletions test/AMBReceiver.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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 { AMBReceiver } from "src/receivers/AMBReceiver.sol";

contract AMBMock {

bytes32 public messageSourceChainId;
address public messageSender;

constructor(bytes32 _messageSourceChainId, address _messageSender) {
messageSourceChainId = _messageSourceChainId;
messageSender = _messageSender;
}

function __setSourceChainId(bytes32 _messageSourceChainId) public {
messageSourceChainId = _messageSourceChainId;
}

function __setSender(address _messageSender) public {
messageSender = _messageSender;
}

}

contract AMBReceiverTest is Test {

AMBMock amb;
TargetContractMock target;

AMBReceiver receiver;

bytes32 sourceChainId = bytes32(uint256(1));
address sourceAuthority = makeAddr("sourceAuthority");
address randomAddress = makeAddr("randomAddress");

function setUp() public {
amb = new AMBMock(sourceChainId, sourceAuthority);
target = new TargetContractMock();

receiver = new AMBReceiver(
address(amb),
sourceChainId,
sourceAuthority,
address(target)
);
}

function test_constructor() public {
receiver = new AMBReceiver(
address(amb),
sourceChainId,
sourceAuthority,
address(target)
);

assertEq(receiver.amb(), address(amb));
assertEq(receiver.sourceChainId(), sourceChainId);
assertEq(receiver.sourceAuthority(), sourceAuthority);
assertEq(receiver.target(), address(target));
}

function test_forward_invalidSender() public {
vm.prank(randomAddress);
vm.expectRevert("AMBReceiver/invalid-sender");
TargetContractMock(address(receiver)).increment();
}

function test_forward_invalidSourceChainId() public {
amb.__setSourceChainId(bytes32(uint256(2)));

vm.prank(address(amb));
vm.expectRevert("AMBReceiver/invalid-sourceChainId");
TargetContractMock(address(receiver)).increment();
}

function test_forward_invalidSourceAuthority() public {
amb.__setSender(randomAddress);

vm.prank(address(amb));
vm.expectRevert("AMBReceiver/invalid-sourceAuthority");
TargetContractMock(address(receiver)).increment();
}

function test_forward_success() public {
assertEq(target.count(), 0);
vm.prank(address(amb));
TargetContractMock(address(receiver)).increment();
assertEq(target.count(), 1);
}

function test_forward_revert() public {
vm.prank(address(amb));
vm.expectRevert("TargetContract/error");
TargetContractMock(address(receiver)).revertFunc();
}

}
67 changes: 67 additions & 0 deletions test/ArbitrumReceiver.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 { ArbitrumReceiver } from "src/receivers/ArbitrumReceiver.sol";

contract ArbitrumReceiverTest is Test {

TargetContractMock target;

ArbitrumReceiver receiver;

address sourceAuthority = makeAddr("sourceAuthority");
address sourceAuthorityWithOffset;
address randomAddress = makeAddr("randomAddress");

function setUp() public {
target = new TargetContractMock();

receiver = new ArbitrumReceiver(
sourceAuthority,
address(target)
);
unchecked {
sourceAuthorityWithOffset = address(uint160(sourceAuthority) + uint160(0x1111000000000000000000000000000000001111));
}
}

function test_constructor() public {
receiver = new ArbitrumReceiver(
sourceAuthority,
address(target)
);

assertEq(receiver.l1Authority(), sourceAuthority);
assertEq(receiver.target(), address(target));
}

function test_forward_invalidL1Authority() public {
vm.prank(randomAddress);
vm.expectRevert("ArbitrumReceiver/invalid-l1Authority");
TargetContractMock(address(receiver)).increment();
}

function test_forward_invalidL1AuthoritySourceAuthorityNoOffset() public {
vm.prank(sourceAuthority);
vm.expectRevert("ArbitrumReceiver/invalid-l1Authority");
TargetContractMock(address(receiver)).increment();
}

function test_forward_success() public {
assertEq(target.count(), 0);
vm.prank(sourceAuthorityWithOffset);
TargetContractMock(address(receiver)).increment();
assertEq(target.count(), 1);
}

function test_forward_revert() public {
vm.prank(sourceAuthorityWithOffset);
vm.expectRevert("TargetContract/error");
TargetContractMock(address(receiver)).revertFunc();
}

}
Loading

0 comments on commit 1b6d35f

Please sign in to comment.