123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
- const { ZERO_ADDRESS } = require('@openzeppelin/test-helpers/src/constants');
- const { expect } = require('chai');
- const { time } = require('@nomicfoundation/hardhat-network-helpers');
- const { shouldSupportInterfaces } = require('../utils/introspection/SupportsInterface.behavior');
- const DEFAULT_ADMIN_ROLE = '0x0000000000000000000000000000000000000000000000000000000000000000';
- const ROLE = web3.utils.soliditySha3('ROLE');
- const OTHER_ROLE = web3.utils.soliditySha3('OTHER_ROLE');
- function shouldBehaveLikeAccessControl(errorPrefix, admin, authorized, other, otherAdmin) {
- shouldSupportInterfaces(['AccessControl']);
- describe('default admin', function () {
- it('deployer has default admin role', async function () {
- expect(await this.accessControl.hasRole(DEFAULT_ADMIN_ROLE, admin)).to.equal(true);
- });
- it("other roles's admin is the default admin role", async function () {
- expect(await this.accessControl.getRoleAdmin(ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
- });
- it("default admin role's admin is itself", async function () {
- expect(await this.accessControl.getRoleAdmin(DEFAULT_ADMIN_ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
- });
- });
- describe('granting', function () {
- beforeEach(async function () {
- await this.accessControl.grantRole(ROLE, authorized, { from: admin });
- });
- it('non-admin cannot grant role to other accounts', async function () {
- await expectRevert(
- this.accessControl.grantRole(ROLE, authorized, { from: other }),
- `${errorPrefix}: account ${other.toLowerCase()} is missing role ${DEFAULT_ADMIN_ROLE}`,
- );
- });
- it('accounts can be granted a role multiple times', async function () {
- await this.accessControl.grantRole(ROLE, authorized, { from: admin });
- const receipt = await this.accessControl.grantRole(ROLE, authorized, { from: admin });
- expectEvent.notEmitted(receipt, 'RoleGranted');
- });
- });
- describe('revoking', function () {
- it('roles that are not had can be revoked', async function () {
- expect(await this.accessControl.hasRole(ROLE, authorized)).to.equal(false);
- const receipt = await this.accessControl.revokeRole(ROLE, authorized, { from: admin });
- expectEvent.notEmitted(receipt, 'RoleRevoked');
- });
- context('with granted role', function () {
- beforeEach(async function () {
- await this.accessControl.grantRole(ROLE, authorized, { from: admin });
- });
- it('admin can revoke role', async function () {
- const receipt = await this.accessControl.revokeRole(ROLE, authorized, { from: admin });
- expectEvent(receipt, 'RoleRevoked', { account: authorized, role: ROLE, sender: admin });
- expect(await this.accessControl.hasRole(ROLE, authorized)).to.equal(false);
- });
- it('non-admin cannot revoke role', async function () {
- await expectRevert(
- this.accessControl.revokeRole(ROLE, authorized, { from: other }),
- `${errorPrefix}: account ${other.toLowerCase()} is missing role ${DEFAULT_ADMIN_ROLE}`,
- );
- });
- it('a role can be revoked multiple times', async function () {
- await this.accessControl.revokeRole(ROLE, authorized, { from: admin });
- const receipt = await this.accessControl.revokeRole(ROLE, authorized, { from: admin });
- expectEvent.notEmitted(receipt, 'RoleRevoked');
- });
- });
- });
- describe('renouncing', function () {
- it('roles that are not had can be renounced', async function () {
- const receipt = await this.accessControl.renounceRole(ROLE, authorized, { from: authorized });
- expectEvent.notEmitted(receipt, 'RoleRevoked');
- });
- context('with granted role', function () {
- beforeEach(async function () {
- await this.accessControl.grantRole(ROLE, authorized, { from: admin });
- });
- it('bearer can renounce role', async function () {
- const receipt = await this.accessControl.renounceRole(ROLE, authorized, { from: authorized });
- expectEvent(receipt, 'RoleRevoked', { account: authorized, role: ROLE, sender: authorized });
- expect(await this.accessControl.hasRole(ROLE, authorized)).to.equal(false);
- });
- it('only the sender can renounce their roles', async function () {
- await expectRevert(
- this.accessControl.renounceRole(ROLE, authorized, { from: admin }),
- `${errorPrefix}: can only renounce roles for self`,
- );
- });
- it('a role can be renounced multiple times', async function () {
- await this.accessControl.renounceRole(ROLE, authorized, { from: authorized });
- const receipt = await this.accessControl.renounceRole(ROLE, authorized, { from: authorized });
- expectEvent.notEmitted(receipt, 'RoleRevoked');
- });
- });
- });
- describe('setting role admin', function () {
- beforeEach(async function () {
- const receipt = await this.accessControl.$_setRoleAdmin(ROLE, OTHER_ROLE);
- expectEvent(receipt, 'RoleAdminChanged', {
- role: ROLE,
- previousAdminRole: DEFAULT_ADMIN_ROLE,
- newAdminRole: OTHER_ROLE,
- });
- await this.accessControl.grantRole(OTHER_ROLE, otherAdmin, { from: admin });
- });
- it("a role's admin role can be changed", async function () {
- expect(await this.accessControl.getRoleAdmin(ROLE)).to.equal(OTHER_ROLE);
- });
- it('the new admin can grant roles', async function () {
- const receipt = await this.accessControl.grantRole(ROLE, authorized, { from: otherAdmin });
- expectEvent(receipt, 'RoleGranted', { account: authorized, role: ROLE, sender: otherAdmin });
- });
- it('the new admin can revoke roles', async function () {
- await this.accessControl.grantRole(ROLE, authorized, { from: otherAdmin });
- const receipt = await this.accessControl.revokeRole(ROLE, authorized, { from: otherAdmin });
- expectEvent(receipt, 'RoleRevoked', { account: authorized, role: ROLE, sender: otherAdmin });
- });
- it("a role's previous admins no longer grant roles", async function () {
- await expectRevert(
- this.accessControl.grantRole(ROLE, authorized, { from: admin }),
- `${errorPrefix}: account ${admin.toLowerCase()} is missing role ${OTHER_ROLE}`,
- );
- });
- it("a role's previous admins no longer revoke roles", async function () {
- await expectRevert(
- this.accessControl.revokeRole(ROLE, authorized, { from: admin }),
- `${errorPrefix}: account ${admin.toLowerCase()} is missing role ${OTHER_ROLE}`,
- );
- });
- });
- describe('onlyRole modifier', function () {
- beforeEach(async function () {
- await this.accessControl.grantRole(ROLE, authorized, { from: admin });
- });
- it('do not revert if sender has role', async function () {
- await this.accessControl.methods['$_checkRole(bytes32)'](ROLE, { from: authorized });
- });
- it("revert if sender doesn't have role #1", async function () {
- await expectRevert(
- this.accessControl.methods['$_checkRole(bytes32)'](ROLE, { from: other }),
- `${errorPrefix}: account ${other.toLowerCase()} is missing role ${ROLE}`,
- );
- });
- it("revert if sender doesn't have role #2", async function () {
- await expectRevert(
- this.accessControl.methods['$_checkRole(bytes32)'](OTHER_ROLE, { from: authorized }),
- `${errorPrefix}: account ${authorized.toLowerCase()} is missing role ${OTHER_ROLE}`,
- );
- });
- });
- }
- function shouldBehaveLikeAccessControlEnumerable(errorPrefix, admin, authorized, other, otherAdmin, otherAuthorized) {
- shouldSupportInterfaces(['AccessControlEnumerable']);
- describe('enumerating', function () {
- it('role bearers can be enumerated', async function () {
- await this.accessControl.grantRole(ROLE, authorized, { from: admin });
- await this.accessControl.grantRole(ROLE, other, { from: admin });
- await this.accessControl.grantRole(ROLE, otherAuthorized, { from: admin });
- await this.accessControl.revokeRole(ROLE, other, { from: admin });
- const memberCount = await this.accessControl.getRoleMemberCount(ROLE);
- expect(memberCount).to.bignumber.equal('2');
- const bearers = [];
- for (let i = 0; i < memberCount; ++i) {
- bearers.push(await this.accessControl.getRoleMember(ROLE, i));
- }
- expect(bearers).to.have.members([authorized, otherAuthorized]);
- });
- it('role enumeration should be in sync after renounceRole call', async function () {
- expect(await this.accessControl.getRoleMemberCount(ROLE)).to.bignumber.equal('0');
- await this.accessControl.grantRole(ROLE, admin, { from: admin });
- expect(await this.accessControl.getRoleMemberCount(ROLE)).to.bignumber.equal('1');
- await this.accessControl.renounceRole(ROLE, admin, { from: admin });
- expect(await this.accessControl.getRoleMemberCount(ROLE)).to.bignumber.equal('0');
- });
- });
- }
- function shouldBehaveLikeAccessControlDefaultAdminRules(errorPrefix, delay, defaultAdmin, newDefaultAdmin, other) {
- shouldSupportInterfaces(['AccessControlDefaultAdminRules']);
- it('has a default disabled delayed until', async function () {
- expect(await this.accessControl.defaultAdminTransferDelayedUntil()).to.be.bignumber.equal(web3.utils.toBN(0));
- });
- it('has a default pending default admin', async function () {
- expect(await this.accessControl.pendingDefaultAdmin()).to.equal(ZERO_ADDRESS);
- });
- it('has a default current owner set to the initial default admin', async function () {
- const owner = await this.accessControl.owner();
- expect(owner).to.equal(defaultAdmin);
- expect(await this.accessControl.hasRole(DEFAULT_ADMIN_ROLE, owner)).to.be.true;
- });
- it('should revert if granting default admin role', async function () {
- await expectRevert(
- this.accessControl.grantRole(DEFAULT_ADMIN_ROLE, defaultAdmin, { from: defaultAdmin }),
- `${errorPrefix}: can't directly grant default admin role`,
- );
- });
- it('should revert if revoking default admin role', async function () {
- await expectRevert(
- this.accessControl.revokeRole(DEFAULT_ADMIN_ROLE, defaultAdmin, { from: defaultAdmin }),
- `${errorPrefix}: can't directly revoke default admin role`,
- );
- });
- it("should revert if defaultAdmin's admin is changed", async function () {
- await expectRevert(
- this.accessControl.$_setRoleAdmin(DEFAULT_ADMIN_ROLE, defaultAdmin),
- `${errorPrefix}: can't violate default admin rules`,
- );
- });
- it('should not grant the default admin role twice', async function () {
- await expectRevert(
- this.accessControl.$_grantRole(DEFAULT_ADMIN_ROLE, defaultAdmin),
- `${errorPrefix}: default admin already granted`,
- );
- });
- describe('begins transfer of default admin', function () {
- let receipt;
- let defaultAdminTransferDelayedUntil;
- beforeEach('begins admin transfer', async function () {
- receipt = await this.accessControl.beginDefaultAdminTransfer(newDefaultAdmin, { from: defaultAdmin });
- defaultAdminTransferDelayedUntil = web3.utils.toBN(await time.latest()).add(delay);
- });
- it('should set pending default admin and delayed until', async function () {
- expect(await this.accessControl.pendingDefaultAdmin()).to.equal(newDefaultAdmin);
- expect(await this.accessControl.defaultAdminTransferDelayedUntil()).to.be.bignumber.equal(
- defaultAdminTransferDelayedUntil,
- );
- expectEvent(receipt, 'DefaultAdminRoleChangeStarted', {
- newDefaultAdmin,
- defaultAdminTransferDelayedUntil,
- });
- });
- it('should be able to begin a transfer again before delay pass', async function () {
- // Time passes just before delay
- await time.setNextBlockTimestamp(defaultAdminTransferDelayedUntil.subn(1));
- // defaultAdmin changes its mind and begin again to another address
- await this.accessControl.beginDefaultAdminTransfer(other, { from: defaultAdmin });
- const newDelayedUntil = web3.utils.toBN(await time.latest()).add(delay);
- expect(await this.accessControl.pendingDefaultAdmin()).to.equal(other);
- expect(await this.accessControl.defaultAdminTransferDelayedUntil()).to.be.bignumber.equal(newDelayedUntil);
- });
- it('should be able to begin a transfer again after delay pass if not accepted', async function () {
- // Time passes after delay without acceptance
- await time.setNextBlockTimestamp(defaultAdminTransferDelayedUntil.addn(1));
- // defaultAdmin changes its mind and begin again to another address
- await this.accessControl.beginDefaultAdminTransfer(other, { from: defaultAdmin });
- const newDelayedUntil = web3.utils.toBN(await time.latest()).add(delay);
- expect(await this.accessControl.pendingDefaultAdmin()).to.equal(other);
- expect(await this.accessControl.defaultAdminTransferDelayedUntil()).to.be.bignumber.equal(newDelayedUntil);
- });
- it('should revert if it is called by non-admin accounts', async function () {
- await expectRevert(
- this.accessControl.beginDefaultAdminTransfer(newDefaultAdmin, { from: other }),
- `${errorPrefix}: account ${other.toLowerCase()} is missing role ${DEFAULT_ADMIN_ROLE}`,
- );
- });
- });
- describe('accepts transfer admin', function () {
- let delayPassed;
- beforeEach(async function () {
- await this.accessControl.beginDefaultAdminTransfer(newDefaultAdmin, { from: defaultAdmin });
- delayPassed = web3.utils
- .toBN(await time.latest())
- .add(delay)
- .addn(1);
- });
- describe('caller is pending default admin and delay has passed', function () {
- let from;
- beforeEach(async function () {
- await time.setNextBlockTimestamp(delayPassed);
- from = newDefaultAdmin;
- });
- it('accepts a transfer and changes default admin', async function () {
- const receipt = await this.accessControl.acceptDefaultAdminTransfer({ from });
- // Storage changes
- expect(await this.accessControl.hasRole(DEFAULT_ADMIN_ROLE, defaultAdmin)).to.be.false;
- expect(await this.accessControl.hasRole(DEFAULT_ADMIN_ROLE, newDefaultAdmin)).to.be.true;
- expect(await this.accessControl.owner()).to.equal(newDefaultAdmin);
- // Emit events
- expectEvent(receipt, 'RoleRevoked', {
- role: DEFAULT_ADMIN_ROLE,
- account: defaultAdmin,
- });
- expectEvent(receipt, 'RoleGranted', {
- role: DEFAULT_ADMIN_ROLE,
- account: newDefaultAdmin,
- });
- // Resets pending default admin and delayed until
- expect(await this.accessControl.defaultAdminTransferDelayedUntil()).to.be.bignumber.equal(web3.utils.toBN(0));
- expect(await this.accessControl.pendingDefaultAdmin()).to.equal(ZERO_ADDRESS);
- });
- });
- it('should revert if caller is not pending default admin', async function () {
- await time.setNextBlockTimestamp(delayPassed);
- await expectRevert(
- this.accessControl.acceptDefaultAdminTransfer({ from: other }),
- `${errorPrefix}: pending admin must accept`,
- );
- });
- describe('delayedUntil not passed', function () {
- let delayNotPassed;
- beforeEach(function () {
- delayNotPassed = delayPassed.subn(1);
- });
- it('should revert if block.timestamp is equal to delayed until', async function () {
- await time.setNextBlockTimestamp(delayNotPassed);
- await expectRevert(
- this.accessControl.acceptDefaultAdminTransfer({ from: newDefaultAdmin }),
- `${errorPrefix}: transfer delay not passed`,
- );
- });
- it('should revert if block.timestamp is less than delayed until', async function () {
- await expectRevert(
- this.accessControl.acceptDefaultAdminTransfer({ from: newDefaultAdmin }),
- `${errorPrefix}: transfer delay not passed`,
- );
- });
- });
- });
- describe('cancel transfer default admin', function () {
- let delayPassed;
- beforeEach(async function () {
- await this.accessControl.beginDefaultAdminTransfer(newDefaultAdmin, { from: defaultAdmin });
- delayPassed = web3.utils
- .toBN(await time.latest())
- .add(delay)
- .addn(1);
- });
- it('resets pending default admin and delayed until', async function () {
- await this.accessControl.cancelDefaultAdminTransfer({ from: defaultAdmin });
- expect(await this.accessControl.defaultAdminTransferDelayedUntil()).to.be.bignumber.equal(web3.utils.toBN(0));
- expect(await this.accessControl.pendingDefaultAdmin()).to.equal(ZERO_ADDRESS);
- // Advance until passed delay
- await time.setNextBlockTimestamp(delayPassed);
- // Previous pending default admin should not be able to accept after cancellation.
- await expectRevert(
- this.accessControl.acceptDefaultAdminTransfer({ from: newDefaultAdmin }),
- `${errorPrefix}: pending admin must accept`,
- );
- });
- it('cancels even after delay has passed', async function () {
- await this.accessControl.cancelDefaultAdminTransfer({ from: defaultAdmin });
- await time.setNextBlockTimestamp(delayPassed);
- expect(await this.accessControl.defaultAdminTransferDelayedUntil()).to.be.bignumber.equal(web3.utils.toBN(0));
- expect(await this.accessControl.pendingDefaultAdmin()).to.equal(ZERO_ADDRESS);
- });
- it('reverts if called by non default admin accounts', async function () {
- await expectRevert(
- this.accessControl.cancelDefaultAdminTransfer({ from: other }),
- `${errorPrefix}: account ${other.toLowerCase()} is missing role ${DEFAULT_ADMIN_ROLE}`,
- );
- });
- });
- describe('renouncing admin', function () {
- let delayPassed;
- let from = defaultAdmin;
- beforeEach(async function () {
- await this.accessControl.beginDefaultAdminTransfer(ZERO_ADDRESS, { from });
- delayPassed = web3.utils
- .toBN(await time.latest())
- .add(delay)
- .addn(1);
- });
- it('it renounces role', async function () {
- await time.setNextBlockTimestamp(delayPassed);
- const receipt = await this.accessControl.renounceRole(DEFAULT_ADMIN_ROLE, from, { from });
- expect(await this.accessControl.hasRole(DEFAULT_ADMIN_ROLE, defaultAdmin)).to.be.false;
- expect(await this.accessControl.hasRole(ZERO_ADDRESS, defaultAdmin)).to.be.false;
- expectEvent(receipt, 'RoleRevoked', {
- role: DEFAULT_ADMIN_ROLE,
- account: from,
- });
- expect(await this.accessControl.owner()).to.equal(ZERO_ADDRESS);
- });
- it('allows to recover access using the internal _grantRole', async function () {
- await time.setNextBlockTimestamp(delayPassed);
- await this.accessControl.renounceRole(DEFAULT_ADMIN_ROLE, from, { from });
- const grantRoleReceipt = await this.accessControl.$_grantRole(DEFAULT_ADMIN_ROLE, other);
- expectEvent(grantRoleReceipt, 'RoleGranted', {
- role: DEFAULT_ADMIN_ROLE,
- account: other,
- });
- });
- it('reverts if caller is not default admin', async function () {
- await time.setNextBlockTimestamp(delayPassed);
- await expectRevert(
- this.accessControl.renounceRole(DEFAULT_ADMIN_ROLE, other, { from }),
- `${errorPrefix}: can only renounce roles for self`,
- );
- });
- describe('delayed until not passed', function () {
- let delayNotPassed;
- beforeEach(function () {
- delayNotPassed = delayPassed.subn(1);
- });
- it('reverts if block.timestamp is equal to delayed until', async function () {
- await time.setNextBlockTimestamp(delayNotPassed);
- await expectRevert(
- this.accessControl.renounceRole(DEFAULT_ADMIN_ROLE, defaultAdmin, { from }),
- `${errorPrefix}: only can renounce in two delayed steps`,
- );
- });
- it('reverts if block.timestamp is less than delayed until', async function () {
- await time.setNextBlockTimestamp(delayNotPassed.subn(1));
- await expectRevert(
- this.accessControl.renounceRole(DEFAULT_ADMIN_ROLE, defaultAdmin, { from }),
- `${errorPrefix}: only can renounce in two delayed steps`,
- );
- });
- });
- });
- }
- module.exports = {
- DEFAULT_ADMIN_ROLE,
- shouldBehaveLikeAccessControl,
- shouldBehaveLikeAccessControlEnumerable,
- shouldBehaveLikeAccessControlDefaultAdminRules,
- };
|