From 06cde9e555941ebf9055f183ab3a2052412fefaf Mon Sep 17 00:00:00 2001 From: Krishang Date: Fri, 1 Mar 2024 18:07:14 +0530 Subject: [PATCH] Add minimal permissions test --- src/common/Permission.sol | 12 +++--- test/common/Permission.t.sol | 75 ++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 test/common/Permission.t.sol diff --git a/src/common/Permission.sol b/src/common/Permission.sol index 407eaec9..e8ef7e10 100644 --- a/src/common/Permission.sol +++ b/src/common/Permission.sol @@ -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(); } @@ -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++; } } diff --git a/test/common/Permission.t.sol b/test/common/Permission.t.sol new file mode 100644 index 00000000..bc4710c8 --- /dev/null +++ b/test/common/Permission.t.sol @@ -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); + } +} \ No newline at end of file