Skip to content

Commit

Permalink
T
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized committed Dec 9, 2024
1 parent 65e17b2 commit 6925fb4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
19 changes: 19 additions & 0 deletions test/EnumerableRoles.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,25 @@ contract EnumerableRolesTest is SoladyTest {
}
}

function testOnlyOwnerOrRole(uint256 allowedRole, uint256 holderRole) public {
address holder = _randomUniqueHashedAddress();
assertEq(mockEnumerableRoles.owner(), address(this));
if (holder == address(this)) return;
mockEnumerableRoles.setAllowedRole(allowedRole);
mockEnumerableRoles.setRoleDirect(holder, holderRole, true);
if (_randomChance(32)) {
mockEnumerableRoles.guardedByOnlyOwnerOrRole();
}
if (holderRole != allowedRole) {
vm.prank(holder);
vm.expectRevert(EnumerableRoles.EnumerableRolesUnauthorized.selector);
mockEnumerableRoles.guardedByOnlyOwnerOrRole();
} else {
vm.prank(holder);
mockEnumerableRoles.guardedByOnlyOwnerOrRole();
}
}

function testSetAndGetRoles(bytes32) public {
_TestTemps memory t;
t.holders = _sampleUniqueAddresses(_randomUniform() & 7);
Expand Down
9 changes: 9 additions & 0 deletions test/utils/mocks/MockEnumerableRoles.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ contract MockEnumerableRoles is EnumerableRoles, Brutalizer {
address owner;
bool ownerReverts;
bytes allowedRolesEncoded;
uint256 allowedRole;
}

event Yo();
Expand Down Expand Up @@ -57,10 +58,18 @@ contract MockEnumerableRoles is EnumerableRoles, Brutalizer {
$.allowedRolesEncoded = value;
}

function setAllowedRole(uint256 role) public {
$.allowedRole = role;
}

function guardedByOnlyOwnerOrRoles() public onlyOwnerOrRoles($.allowedRolesEncoded) {
emit Yo();
}

function guardedByOnlyOwnerOrRole() public onlyOwnerOrRole($.allowedRole) {
emit Yo();
}

function guardedByOnlyRoles() public onlyRoles($.allowedRolesEncoded) {
emit Yo();
}
Expand Down

0 comments on commit 6925fb4

Please sign in to comment.