Skip to content

Commit

Permalink
Add minimal permissions test
Browse files Browse the repository at this point in the history
  • Loading branch information
nkrishang committed Mar 1, 2024
1 parent 74994b1 commit 06cde9e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/common/Permission.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ contract Permission is IPermission {
* @param _roleBits The bits representing the permissions to check.
* @param _startIndex The start index of the range. (inclusive)
* @param _endIndex The end index of the range. (non-inclusive)
* @return hodlers The holders with the given permissions, within the given range.
* @return holders The holders with the given permissions, within the given range.
*/
function getRoleMembers(uint256 _roleBits, uint256 _startIndex, uint256 _endIndex) external view returns (address[] memory hodlers) {
function getRoleMembers(uint256 _roleBits, uint256 _startIndex, uint256 _endIndex) external view returns (address[] memory holders) {
PermissionStorage.Data storage data = PermissionStorage.data();

uint256 len = data.roleMemberCount;
if(_endIndex >= _startIndex || _endIndex > len) {
if(_startIndex >= _endIndex || _endIndex > len) {
revert PermissionInvalidRange();
}

Expand All @@ -77,14 +77,14 @@ contract Permission is IPermission {
}
}

hodlers = new address[](count);
holders = new address[](count);
uint256 idx = 0;

for (uint256 j = 0; j < len; j++) {
for (uint256 j = 1; j < len + 1; j++) {
address holder = data.memberAtIndex[j];

if (hasRole(holder, _roleBits)) {
hodlers[idx] = holder;
holders[idx] = holder;
idx++;
}
}
Expand Down
75 changes: 75 additions & 0 deletions test/common/Permission.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import { Test } from "forge-std/Test.sol";
import { Permission } from "src/common/Permission.sol";

contract PermissionExample is Permission {

constructor(address _admin) {
_setupRole(_admin, ADMIN_ROLE_BITS);
}
}

contract PermissionTest is Test {

PermissionExample permission;
address public admin = address(0x123);

uint256 public permissionBitsOne = 2 ** 2;
uint256 public permissionBitsTwo = 2 ** 3;
uint256 public permissionBitsThree = 2 ** 4;

mapping(address => bool) public setRole;

function setUp() public {
permission = new PermissionExample(admin);
}

function test_roles() public {

uint256 count = 100;

for (uint256 i = 1; i <= count; i++) {
address account = address(uint160(i));

vm.startPrank(admin);

permission.grantRole(account, permissionBitsOne);
permission.grantRole(account, permissionBitsTwo);
permission.grantRole(account, permissionBitsThree);

vm.stopPrank();

setRole[account] = true;

assert(permission.hasRole(account, permissionBitsOne | permissionBitsTwo | permissionBitsThree));
}

address[] memory accounts = permission.getRoleMembers(permissionBitsOne | permissionBitsTwo | permissionBitsThree, 0, permission.getRoleMemberCount());
assertEq(accounts.length, 100);

for(uint256 i = 0; i < accounts.length; i++) {
assert(setRole[accounts[i]]);
}

for (uint256 i = 25; i <= 74; i++) {
address account = address(uint160(i));

vm.startPrank(admin);

permission.revokeRole(account, permissionBitsOne);
permission.revokeRole(account, permissionBitsTwo);
permission.revokeRole(account, permissionBitsThree);

vm.stopPrank();

setRole[account] = false;

assert(!permission.hasRole(account, permissionBitsOne | permissionBitsTwo | permissionBitsThree));
}

address[] memory accountsAfter = permission.getRoleMembers(permissionBitsOne | permissionBitsTwo | permissionBitsThree, 0, permission.getRoleMemberCount());
assertEq(accountsAfter.length, 50);
}
}

0 comments on commit 06cde9e

Please sign in to comment.