Skip to content

Commit

Permalink
feat: add access control invariants
Browse files Browse the repository at this point in the history
  • Loading branch information
sujithsomraaj committed Oct 18, 2023
1 parent 1fcd91a commit b83212f
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 7 deletions.
40 changes: 40 additions & 0 deletions test/invariant-tests/AccessControl.Invariant.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.8.9;

/// library imports
import {Vm, Test} from "forge-std/Test.sol";

/// local imports
import "test/Setup.t.sol";

/// handler import
import {AccessControlSenderHandler} from "test/invariant-tests/handlers/AccessControlSender.handler.sol";

contract AccessControlHandlerInvariant is Setup {
AccessControlSenderHandler public handler;

/// @notice nonce snapshot for assertions
uint256 public localNonceState;

function setUp() public override {
/// @dev calls setup to spin up test contracts
super.setUp();

/// @dev selects fork and deploy the handlers
vm.selectFork(fork[SRC_CHAIN_ID]);
handler = new AccessControlSenderHandler(
contractAddress[SRC_CHAIN_ID]["GAC"],
contractAddress[SRC_CHAIN_ID]["MMA_SENDER"]
);

/// @dev bind the handler as target for invariant
targetContract(address(handler));
}

function invariant_test_acess_control_src() public {
if (handler.lastCaller() == MessageSenderGAC(contractAddress[SRC_CHAIN_ID]["GAC"]).authorisedCaller()) {
++localNonceState;
}
assertEq(MultiBridgeMessageSender(contractAddress[SRC_CHAIN_ID]["MMA_SENDER"]).nonce(), localNonceState);
}
}
8 changes: 2 additions & 6 deletions test/invariant-tests/AdapterList.Invariant.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {Vm, Test} from "forge-std/Test.sol";

/// local imports
import "test/Setup.t.sol";
import "test/contracts-mock/MockUniswapReceiver.sol";

/// handler import
import {AdapterListHandler} from "test/invariant-tests/handlers/AdapterList.handler.sol";
Expand Down Expand Up @@ -40,7 +39,7 @@ contract AdapterListInvariant is Setup {
/// @notice invariant-6: once a trusted executor is removed, it should not persist in the adapter list
function invariant_test_adapter_additions() public {
MultiBridgeMessageSender targetContract = MultiBridgeMessageSender(contractAddress[SRC_CHAIN_ID]["MMA_SENDER"]);

if (handler.success()) {
uint256 currAdds = handler.currAdds();

Expand All @@ -56,10 +55,7 @@ contract AdapterListInvariant is Setup {
/// @dev assertions for invariant-3, invariant-4, invariant-5, invariant-6
if (currAdds > 1) {
for (uint256 i = currAdds; i > 0; i--) {
assertTrue(
targetContract.senderAdapters(i)
> targetContract.senderAdapters(i - 1)
);
assertTrue(targetContract.senderAdapters(i) > targetContract.senderAdapters(i - 1));
}
}
}
Expand Down
58 changes: 58 additions & 0 deletions test/invariant-tests/handlers/AccessControlSender.Handler.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.8.9;

/// library imports
import "forge-std/Test.sol";

/// local imports
import {MessageSenderGAC} from "src/controllers/MessageSenderGAC.sol";
import {MultiBridgeMessageSender} from "src/MultiBridgeMessageSender.sol";

/// @notice handler for testing access control invariants
contract AccessControlSenderHandler is Test {
/// @notice local state
MultiBridgeMessageSender public multiBridgeMessageSender;
MessageSenderGAC public gac;

/// @notice logs last caller for validations
address public lastCaller;

/// @notice modifier to prank caller
modifier prank(address _prankster) {
vm.startPrank(_prankster);
_;
vm.stopPrank();
}

/// @notice initial setup contracts
constructor(address _gac, address _multiBridge) {
gac = MessageSenderGAC(_gac);
multiBridgeMessageSender = MultiBridgeMessageSender(_multiBridge);
}

/// @notice helper for remote call
function remoteCall(
address simulatedCaller,
uint256 _dstChainId,
address _target,
bytes memory _callData,
uint256 _nativeValue,
uint256 _expiration,
address _refundAddress,
uint256[] memory _fees,
uint256 _successThreshold,
address[] memory _excludedAdapters
) external prank(simulatedCaller) {
multiBridgeMessageSender.remoteCall(
_dstChainId,
_target,
_callData,
_nativeValue,
_expiration,
_refundAddress,
_fees,
_successThreshold,
_excludedAdapters
);
}
}
2 changes: 1 addition & 1 deletion test/invariant-tests/handlers/AdapterList.Handler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity >=0.8.9;
/// library imports
import "forge-std/Test.sol";

/// library imports
/// local imports
import {MessageSenderGAC} from "src/controllers/MessageSenderGAC.sol";
import {MultiBridgeMessageSender} from "src/MultiBridgeMessageSender.sol";

Expand Down

0 comments on commit b83212f

Please sign in to comment.