Skip to content

Commit

Permalink
add bridge role tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tsnewnami committed Nov 15, 2023
1 parent 4cc81c4 commit 6905cee
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/common/BridgeRoles.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract contract BridgeRoles is AccessControlUpgradeable, PausableUpgradeable {
function grantAdaptorManagerRole(address account) external onlyRole(DEFAULT_ADMIN_ROLE) {
grantRole(ADAPTOR_MANAGER_ROLE, account);
}

// Role revoking functions
/**
* @notice Function to revoke pauser role from an address
Expand Down
2 changes: 1 addition & 1 deletion src/lib/BridgeRoles.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract contract BridgeRoles is AccessControlUpgradeable, PausableUpgradeable {
function grantAdaptorManagerRole(address account) external onlyRole(DEFAULT_ADMIN_ROLE) {
grantRole(ADAPTOR_MANAGER_ROLE, account);
}

// Role revoking functions
/**
* @notice Function to revoke pauser role from an address
Expand Down
158 changes: 158 additions & 0 deletions test/unit/common/BridgeRoles.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

import "forge-std/Test.sol";
import "@openzeppelin/contracts-upgradeable/utils/StringsUpgradeable.sol";
import {MockBridgeRoles} from "./MockBridgeRoles.sol";

contract Setup is Test {
address admin = makeAddr("admin");
address pauser = makeAddr("pauser");
address unpauser = makeAddr("unpauser");
address variableManager = makeAddr("variableManager");
address adaptorManager = makeAddr("adaptorManager");

MockBridgeRoles mockBridgeRoles;

function setUp() public {
mockBridgeRoles = new MockBridgeRoles(admin);
}
}

contract BridgeRoles is Setup {
function grantRoles() internal {
vm.startPrank(admin);
mockBridgeRoles.grantPauserRole(pauser);
mockBridgeRoles.grantUnpauserRole(unpauser);
mockBridgeRoles.grantVariableManagerRole(variableManager);
mockBridgeRoles.grantAdaptorManagerRole(adaptorManager);
vm.stopPrank();
}

function revokeRoles() internal {
vm.startPrank(admin);
mockBridgeRoles.revokePauserRole(pauser);
mockBridgeRoles.revokeUnpauserRole(unpauser);
mockBridgeRoles.revokeVariableManagerRole(variableManager);
mockBridgeRoles.revokeAdaptorManagerRole(adaptorManager);
vm.stopPrank();
}

function test_grantRoles() public {
grantRoles();
assert(mockBridgeRoles.hasRole(mockBridgeRoles.PAUSER_ROLE(), pauser));
assert(mockBridgeRoles.hasRole(mockBridgeRoles.UNPAUSER_ROLE(), unpauser));
assert(mockBridgeRoles.hasRole(mockBridgeRoles.VARIABLE_MANAGER_ROLE(), variableManager));
assert(mockBridgeRoles.hasRole(mockBridgeRoles.ADAPTOR_MANAGER_ROLE(), adaptorManager));
}

function test_revokeRoles() public {
grantRoles();
revokeRoles();
assert(!mockBridgeRoles.hasRole(mockBridgeRoles.PAUSER_ROLE(), pauser));
assert(!mockBridgeRoles.hasRole(mockBridgeRoles.UNPAUSER_ROLE(), unpauser));
assert(!mockBridgeRoles.hasRole(mockBridgeRoles.VARIABLE_MANAGER_ROLE(), variableManager));
assert(!mockBridgeRoles.hasRole(mockBridgeRoles.ADAPTOR_MANAGER_ROLE(), adaptorManager));
}

function test_pause() public {
grantRoles();
vm.prank(pauser);
mockBridgeRoles.pause();
assert(mockBridgeRoles.paused());
}

function test_unpause() public {
grantRoles();
vm.prank(pauser);
mockBridgeRoles.pause();

vm.prank(unpauser);
mockBridgeRoles.unpause();
assert(!mockBridgeRoles.paused());
}

function test_pause_reverts() public {
bytes32 role = mockBridgeRoles.PAUSER_ROLE();

// No role
vm.prank(pauser);
vm.expectRevert(
abi.encodePacked(
"AccessControl: account ",
StringsUpgradeable.toHexString(pauser),
" is missing role ",
StringsUpgradeable.toHexString(uint256(role), 32)
)
);
mockBridgeRoles.pause();

// Admin role
vm.startPrank(admin);
mockBridgeRoles.grantUnpauserRole(pauser);
vm.expectRevert(
abi.encodePacked(
"AccessControl: account ",
StringsUpgradeable.toHexString(admin),
" is missing role ",
StringsUpgradeable.toHexString(uint256(role), 32)
)
);
mockBridgeRoles.pause();
vm.stopPrank();

// Unpauser role
vm.prank(unpauser);
vm.expectRevert(
abi.encodePacked(
"AccessControl: account ",
StringsUpgradeable.toHexString(unpauser),
" is missing role ",
StringsUpgradeable.toHexString(uint256(role), 32)
)
);
mockBridgeRoles.pause();
}

function test_unpause_reverts() public {
bytes32 role = mockBridgeRoles.UNPAUSER_ROLE();

// No role
vm.prank(unpauser);
vm.expectRevert(
abi.encodePacked(
"AccessControl: account ",
StringsUpgradeable.toHexString(unpauser),
" is missing role ",
StringsUpgradeable.toHexString(uint256(role), 32)
)
);
mockBridgeRoles.unpause();

// Admin role
vm.startPrank(admin);
mockBridgeRoles.grantPauserRole(unpauser);
vm.expectRevert(
abi.encodePacked(
"AccessControl: account ",
StringsUpgradeable.toHexString(admin),
" is missing role ",
StringsUpgradeable.toHexString(uint256(role), 32)
)
);
mockBridgeRoles.unpause();
vm.stopPrank();

// Pauser role
vm.prank(unpauser);
vm.expectRevert(
abi.encodePacked(
"AccessControl: account ",
StringsUpgradeable.toHexString(unpauser),
" is missing role ",
StringsUpgradeable.toHexString(uint256(role), 32)
)
);
mockBridgeRoles.unpause();
}
}

0 comments on commit 6905cee

Please sign in to comment.