Skip to content

Commit

Permalink
add optimism test
Browse files Browse the repository at this point in the history
  • Loading branch information
hexonaut committed Jun 5, 2024
1 parent 5669f7d commit dc07c16
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions test/OptimismReceiver.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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 { OptimismReceiver } from "src/receivers/OptimismReceiver.sol";

contract OptimismMessengerMock {

address public xDomainMessageSender;

function __setSender(address _xDomainMessageSender) public {
xDomainMessageSender = _xDomainMessageSender;
}

}

contract OptimismReceiverTest is Test {

OptimismMessengerMock l2CrossDomain;
TargetContractMock target;

OptimismReceiver receiver;

address l2CrossDomainAddr = 0x4200000000000000000000000000000000000007;

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

function setUp() public {
// Set the code at the particular address
l2CrossDomain = new OptimismMessengerMock();
vm.etch(l2CrossDomainAddr, address(l2CrossDomain).code);
l2CrossDomain = OptimismMessengerMock(l2CrossDomainAddr);
l2CrossDomain.__setSender(sourceAuthority);

target = new TargetContractMock();

receiver = new OptimismReceiver(
sourceAuthority,
address(target)
);
}

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

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

function test_forward_invalidSender() public {
vm.prank(randomAddress);
vm.expectRevert("OptimismReceiver/invalid-sender");
receiver.forward(abi.encodeCall(TargetContractMock.someFunc, ()));
}

function test_forward_invalidL1Authority() public {
l2CrossDomain.__setSender(randomAddress);

vm.prank(address(l2CrossDomain));
vm.expectRevert("OptimismReceiver/invalid-l1Authority");
receiver.forward(abi.encodeCall(TargetContractMock.someFunc, ()));
}

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

function test_forward_revert() public {
vm.prank(address(l2CrossDomain));
vm.expectRevert("error");
receiver.forward(abi.encodeCall(TargetContractMock.revertFunc, ()));
}

}

0 comments on commit dc07c16

Please sign in to comment.