-
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
1 changed file
with
84 additions
and
0 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
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, ())); | ||
} | ||
|
||
} |