AccessControl.behavior.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { ZERO_ADDRESS } = require('@openzeppelin/test-helpers/src/constants');
  3. const { expect } = require('chai');
  4. const { time } = require('@nomicfoundation/hardhat-network-helpers');
  5. const { shouldSupportInterfaces } = require('../utils/introspection/SupportsInterface.behavior');
  6. const DEFAULT_ADMIN_ROLE = '0x0000000000000000000000000000000000000000000000000000000000000000';
  7. const ROLE = web3.utils.soliditySha3('ROLE');
  8. const OTHER_ROLE = web3.utils.soliditySha3('OTHER_ROLE');
  9. function shouldBehaveLikeAccessControl(errorPrefix, admin, authorized, other, otherAdmin) {
  10. shouldSupportInterfaces(['AccessControl']);
  11. describe('default admin', function () {
  12. it('deployer has default admin role', async function () {
  13. expect(await this.accessControl.hasRole(DEFAULT_ADMIN_ROLE, admin)).to.equal(true);
  14. });
  15. it("other roles's admin is the default admin role", async function () {
  16. expect(await this.accessControl.getRoleAdmin(ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
  17. });
  18. it("default admin role's admin is itself", async function () {
  19. expect(await this.accessControl.getRoleAdmin(DEFAULT_ADMIN_ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
  20. });
  21. });
  22. describe('granting', function () {
  23. beforeEach(async function () {
  24. await this.accessControl.grantRole(ROLE, authorized, { from: admin });
  25. });
  26. it('non-admin cannot grant role to other accounts', async function () {
  27. await expectRevert(
  28. this.accessControl.grantRole(ROLE, authorized, { from: other }),
  29. `${errorPrefix}: account ${other.toLowerCase()} is missing role ${DEFAULT_ADMIN_ROLE}`,
  30. );
  31. });
  32. it('accounts can be granted a role multiple times', async function () {
  33. await this.accessControl.grantRole(ROLE, authorized, { from: admin });
  34. const receipt = await this.accessControl.grantRole(ROLE, authorized, { from: admin });
  35. expectEvent.notEmitted(receipt, 'RoleGranted');
  36. });
  37. });
  38. describe('revoking', function () {
  39. it('roles that are not had can be revoked', async function () {
  40. expect(await this.accessControl.hasRole(ROLE, authorized)).to.equal(false);
  41. const receipt = await this.accessControl.revokeRole(ROLE, authorized, { from: admin });
  42. expectEvent.notEmitted(receipt, 'RoleRevoked');
  43. });
  44. context('with granted role', function () {
  45. beforeEach(async function () {
  46. await this.accessControl.grantRole(ROLE, authorized, { from: admin });
  47. });
  48. it('admin can revoke role', async function () {
  49. const receipt = await this.accessControl.revokeRole(ROLE, authorized, { from: admin });
  50. expectEvent(receipt, 'RoleRevoked', { account: authorized, role: ROLE, sender: admin });
  51. expect(await this.accessControl.hasRole(ROLE, authorized)).to.equal(false);
  52. });
  53. it('non-admin cannot revoke role', async function () {
  54. await expectRevert(
  55. this.accessControl.revokeRole(ROLE, authorized, { from: other }),
  56. `${errorPrefix}: account ${other.toLowerCase()} is missing role ${DEFAULT_ADMIN_ROLE}`,
  57. );
  58. });
  59. it('a role can be revoked multiple times', async function () {
  60. await this.accessControl.revokeRole(ROLE, authorized, { from: admin });
  61. const receipt = await this.accessControl.revokeRole(ROLE, authorized, { from: admin });
  62. expectEvent.notEmitted(receipt, 'RoleRevoked');
  63. });
  64. });
  65. });
  66. describe('renouncing', function () {
  67. it('roles that are not had can be renounced', async function () {
  68. const receipt = await this.accessControl.renounceRole(ROLE, authorized, { from: authorized });
  69. expectEvent.notEmitted(receipt, 'RoleRevoked');
  70. });
  71. context('with granted role', function () {
  72. beforeEach(async function () {
  73. await this.accessControl.grantRole(ROLE, authorized, { from: admin });
  74. });
  75. it('bearer can renounce role', async function () {
  76. const receipt = await this.accessControl.renounceRole(ROLE, authorized, { from: authorized });
  77. expectEvent(receipt, 'RoleRevoked', { account: authorized, role: ROLE, sender: authorized });
  78. expect(await this.accessControl.hasRole(ROLE, authorized)).to.equal(false);
  79. });
  80. it('only the sender can renounce their roles', async function () {
  81. await expectRevert(
  82. this.accessControl.renounceRole(ROLE, authorized, { from: admin }),
  83. `${errorPrefix}: can only renounce roles for self`,
  84. );
  85. });
  86. it('a role can be renounced multiple times', async function () {
  87. await this.accessControl.renounceRole(ROLE, authorized, { from: authorized });
  88. const receipt = await this.accessControl.renounceRole(ROLE, authorized, { from: authorized });
  89. expectEvent.notEmitted(receipt, 'RoleRevoked');
  90. });
  91. });
  92. });
  93. describe('setting role admin', function () {
  94. beforeEach(async function () {
  95. const receipt = await this.accessControl.$_setRoleAdmin(ROLE, OTHER_ROLE);
  96. expectEvent(receipt, 'RoleAdminChanged', {
  97. role: ROLE,
  98. previousAdminRole: DEFAULT_ADMIN_ROLE,
  99. newAdminRole: OTHER_ROLE,
  100. });
  101. await this.accessControl.grantRole(OTHER_ROLE, otherAdmin, { from: admin });
  102. });
  103. it("a role's admin role can be changed", async function () {
  104. expect(await this.accessControl.getRoleAdmin(ROLE)).to.equal(OTHER_ROLE);
  105. });
  106. it('the new admin can grant roles', async function () {
  107. const receipt = await this.accessControl.grantRole(ROLE, authorized, { from: otherAdmin });
  108. expectEvent(receipt, 'RoleGranted', { account: authorized, role: ROLE, sender: otherAdmin });
  109. });
  110. it('the new admin can revoke roles', async function () {
  111. await this.accessControl.grantRole(ROLE, authorized, { from: otherAdmin });
  112. const receipt = await this.accessControl.revokeRole(ROLE, authorized, { from: otherAdmin });
  113. expectEvent(receipt, 'RoleRevoked', { account: authorized, role: ROLE, sender: otherAdmin });
  114. });
  115. it("a role's previous admins no longer grant roles", async function () {
  116. await expectRevert(
  117. this.accessControl.grantRole(ROLE, authorized, { from: admin }),
  118. `${errorPrefix}: account ${admin.toLowerCase()} is missing role ${OTHER_ROLE}`,
  119. );
  120. });
  121. it("a role's previous admins no longer revoke roles", async function () {
  122. await expectRevert(
  123. this.accessControl.revokeRole(ROLE, authorized, { from: admin }),
  124. `${errorPrefix}: account ${admin.toLowerCase()} is missing role ${OTHER_ROLE}`,
  125. );
  126. });
  127. });
  128. describe('onlyRole modifier', function () {
  129. beforeEach(async function () {
  130. await this.accessControl.grantRole(ROLE, authorized, { from: admin });
  131. });
  132. it('do not revert if sender has role', async function () {
  133. await this.accessControl.methods['$_checkRole(bytes32)'](ROLE, { from: authorized });
  134. });
  135. it("revert if sender doesn't have role #1", async function () {
  136. await expectRevert(
  137. this.accessControl.methods['$_checkRole(bytes32)'](ROLE, { from: other }),
  138. `${errorPrefix}: account ${other.toLowerCase()} is missing role ${ROLE}`,
  139. );
  140. });
  141. it("revert if sender doesn't have role #2", async function () {
  142. await expectRevert(
  143. this.accessControl.methods['$_checkRole(bytes32)'](OTHER_ROLE, { from: authorized }),
  144. `${errorPrefix}: account ${authorized.toLowerCase()} is missing role ${OTHER_ROLE}`,
  145. );
  146. });
  147. });
  148. }
  149. function shouldBehaveLikeAccessControlEnumerable(errorPrefix, admin, authorized, other, otherAdmin, otherAuthorized) {
  150. shouldSupportInterfaces(['AccessControlEnumerable']);
  151. describe('enumerating', function () {
  152. it('role bearers can be enumerated', async function () {
  153. await this.accessControl.grantRole(ROLE, authorized, { from: admin });
  154. await this.accessControl.grantRole(ROLE, other, { from: admin });
  155. await this.accessControl.grantRole(ROLE, otherAuthorized, { from: admin });
  156. await this.accessControl.revokeRole(ROLE, other, { from: admin });
  157. const memberCount = await this.accessControl.getRoleMemberCount(ROLE);
  158. expect(memberCount).to.bignumber.equal('2');
  159. const bearers = [];
  160. for (let i = 0; i < memberCount; ++i) {
  161. bearers.push(await this.accessControl.getRoleMember(ROLE, i));
  162. }
  163. expect(bearers).to.have.members([authorized, otherAuthorized]);
  164. });
  165. it('role enumeration should be in sync after renounceRole call', async function () {
  166. expect(await this.accessControl.getRoleMemberCount(ROLE)).to.bignumber.equal('0');
  167. await this.accessControl.grantRole(ROLE, admin, { from: admin });
  168. expect(await this.accessControl.getRoleMemberCount(ROLE)).to.bignumber.equal('1');
  169. await this.accessControl.renounceRole(ROLE, admin, { from: admin });
  170. expect(await this.accessControl.getRoleMemberCount(ROLE)).to.bignumber.equal('0');
  171. });
  172. });
  173. }
  174. function shouldBehaveLikeAccessControlDefaultAdminRules(errorPrefix, delay, defaultAdmin, newDefaultAdmin, other) {
  175. shouldSupportInterfaces(['AccessControlDefaultAdminRules']);
  176. it('has a default disabled delayed until', async function () {
  177. expect(await this.accessControl.defaultAdminTransferDelayedUntil()).to.be.bignumber.equal(web3.utils.toBN(0));
  178. });
  179. it('has a default pending default admin', async function () {
  180. expect(await this.accessControl.pendingDefaultAdmin()).to.equal(ZERO_ADDRESS);
  181. });
  182. it('has a default current owner set to the initial default admin', async function () {
  183. const owner = await this.accessControl.owner();
  184. expect(owner).to.equal(defaultAdmin);
  185. expect(await this.accessControl.hasRole(DEFAULT_ADMIN_ROLE, owner)).to.be.true;
  186. });
  187. it('should revert if granting default admin role', async function () {
  188. await expectRevert(
  189. this.accessControl.grantRole(DEFAULT_ADMIN_ROLE, defaultAdmin, { from: defaultAdmin }),
  190. `${errorPrefix}: can't directly grant default admin role`,
  191. );
  192. });
  193. it('should revert if revoking default admin role', async function () {
  194. await expectRevert(
  195. this.accessControl.revokeRole(DEFAULT_ADMIN_ROLE, defaultAdmin, { from: defaultAdmin }),
  196. `${errorPrefix}: can't directly revoke default admin role`,
  197. );
  198. });
  199. it("should revert if defaultAdmin's admin is changed", async function () {
  200. await expectRevert(
  201. this.accessControl.$_setRoleAdmin(DEFAULT_ADMIN_ROLE, defaultAdmin),
  202. `${errorPrefix}: can't violate default admin rules`,
  203. );
  204. });
  205. it('should not grant the default admin role twice', async function () {
  206. await expectRevert(
  207. this.accessControl.$_grantRole(DEFAULT_ADMIN_ROLE, defaultAdmin),
  208. `${errorPrefix}: default admin already granted`,
  209. );
  210. });
  211. describe('begins transfer of default admin', function () {
  212. let receipt;
  213. let defaultAdminTransferDelayedUntil;
  214. beforeEach('begins admin transfer', async function () {
  215. receipt = await this.accessControl.beginDefaultAdminTransfer(newDefaultAdmin, { from: defaultAdmin });
  216. defaultAdminTransferDelayedUntil = web3.utils.toBN(await time.latest()).add(delay);
  217. });
  218. it('should set pending default admin and delayed until', async function () {
  219. expect(await this.accessControl.pendingDefaultAdmin()).to.equal(newDefaultAdmin);
  220. expect(await this.accessControl.defaultAdminTransferDelayedUntil()).to.be.bignumber.equal(
  221. defaultAdminTransferDelayedUntil,
  222. );
  223. expectEvent(receipt, 'DefaultAdminRoleChangeStarted', {
  224. newDefaultAdmin,
  225. defaultAdminTransferDelayedUntil,
  226. });
  227. });
  228. it('should be able to begin a transfer again before delay pass', async function () {
  229. // Time passes just before delay
  230. await time.setNextBlockTimestamp(defaultAdminTransferDelayedUntil.subn(1));
  231. // defaultAdmin changes its mind and begin again to another address
  232. await this.accessControl.beginDefaultAdminTransfer(other, { from: defaultAdmin });
  233. const newDelayedUntil = web3.utils.toBN(await time.latest()).add(delay);
  234. expect(await this.accessControl.pendingDefaultAdmin()).to.equal(other);
  235. expect(await this.accessControl.defaultAdminTransferDelayedUntil()).to.be.bignumber.equal(newDelayedUntil);
  236. });
  237. it('should be able to begin a transfer again after delay pass if not accepted', async function () {
  238. // Time passes after delay without acceptance
  239. await time.setNextBlockTimestamp(defaultAdminTransferDelayedUntil.addn(1));
  240. // defaultAdmin changes its mind and begin again to another address
  241. await this.accessControl.beginDefaultAdminTransfer(other, { from: defaultAdmin });
  242. const newDelayedUntil = web3.utils.toBN(await time.latest()).add(delay);
  243. expect(await this.accessControl.pendingDefaultAdmin()).to.equal(other);
  244. expect(await this.accessControl.defaultAdminTransferDelayedUntil()).to.be.bignumber.equal(newDelayedUntil);
  245. });
  246. it('should revert if it is called by non-admin accounts', async function () {
  247. await expectRevert(
  248. this.accessControl.beginDefaultAdminTransfer(newDefaultAdmin, { from: other }),
  249. `${errorPrefix}: account ${other.toLowerCase()} is missing role ${DEFAULT_ADMIN_ROLE}`,
  250. );
  251. });
  252. });
  253. describe('accepts transfer admin', function () {
  254. let delayPassed;
  255. beforeEach(async function () {
  256. await this.accessControl.beginDefaultAdminTransfer(newDefaultAdmin, { from: defaultAdmin });
  257. delayPassed = web3.utils
  258. .toBN(await time.latest())
  259. .add(delay)
  260. .addn(1);
  261. });
  262. describe('caller is pending default admin and delay has passed', function () {
  263. let from;
  264. beforeEach(async function () {
  265. await time.setNextBlockTimestamp(delayPassed);
  266. from = newDefaultAdmin;
  267. });
  268. it('accepts a transfer and changes default admin', async function () {
  269. const receipt = await this.accessControl.acceptDefaultAdminTransfer({ from });
  270. // Storage changes
  271. expect(await this.accessControl.hasRole(DEFAULT_ADMIN_ROLE, defaultAdmin)).to.be.false;
  272. expect(await this.accessControl.hasRole(DEFAULT_ADMIN_ROLE, newDefaultAdmin)).to.be.true;
  273. expect(await this.accessControl.owner()).to.equal(newDefaultAdmin);
  274. // Emit events
  275. expectEvent(receipt, 'RoleRevoked', {
  276. role: DEFAULT_ADMIN_ROLE,
  277. account: defaultAdmin,
  278. });
  279. expectEvent(receipt, 'RoleGranted', {
  280. role: DEFAULT_ADMIN_ROLE,
  281. account: newDefaultAdmin,
  282. });
  283. // Resets pending default admin and delayed until
  284. expect(await this.accessControl.defaultAdminTransferDelayedUntil()).to.be.bignumber.equal(web3.utils.toBN(0));
  285. expect(await this.accessControl.pendingDefaultAdmin()).to.equal(ZERO_ADDRESS);
  286. });
  287. });
  288. it('should revert if caller is not pending default admin', async function () {
  289. await time.setNextBlockTimestamp(delayPassed);
  290. await expectRevert(
  291. this.accessControl.acceptDefaultAdminTransfer({ from: other }),
  292. `${errorPrefix}: pending admin must accept`,
  293. );
  294. });
  295. describe('delayedUntil not passed', function () {
  296. let delayNotPassed;
  297. beforeEach(function () {
  298. delayNotPassed = delayPassed.subn(1);
  299. });
  300. it('should revert if block.timestamp is equal to delayed until', async function () {
  301. await time.setNextBlockTimestamp(delayNotPassed);
  302. await expectRevert(
  303. this.accessControl.acceptDefaultAdminTransfer({ from: newDefaultAdmin }),
  304. `${errorPrefix}: transfer delay not passed`,
  305. );
  306. });
  307. it('should revert if block.timestamp is less than delayed until', async function () {
  308. await expectRevert(
  309. this.accessControl.acceptDefaultAdminTransfer({ from: newDefaultAdmin }),
  310. `${errorPrefix}: transfer delay not passed`,
  311. );
  312. });
  313. });
  314. });
  315. describe('cancel transfer default admin', function () {
  316. let delayPassed;
  317. beforeEach(async function () {
  318. await this.accessControl.beginDefaultAdminTransfer(newDefaultAdmin, { from: defaultAdmin });
  319. delayPassed = web3.utils
  320. .toBN(await time.latest())
  321. .add(delay)
  322. .addn(1);
  323. });
  324. it('resets pending default admin and delayed until', async function () {
  325. await this.accessControl.cancelDefaultAdminTransfer({ from: defaultAdmin });
  326. expect(await this.accessControl.defaultAdminTransferDelayedUntil()).to.be.bignumber.equal(web3.utils.toBN(0));
  327. expect(await this.accessControl.pendingDefaultAdmin()).to.equal(ZERO_ADDRESS);
  328. // Advance until passed delay
  329. await time.setNextBlockTimestamp(delayPassed);
  330. // Previous pending default admin should not be able to accept after cancellation.
  331. await expectRevert(
  332. this.accessControl.acceptDefaultAdminTransfer({ from: newDefaultAdmin }),
  333. `${errorPrefix}: pending admin must accept`,
  334. );
  335. });
  336. it('cancels even after delay has passed', async function () {
  337. await this.accessControl.cancelDefaultAdminTransfer({ from: defaultAdmin });
  338. await time.setNextBlockTimestamp(delayPassed);
  339. expect(await this.accessControl.defaultAdminTransferDelayedUntil()).to.be.bignumber.equal(web3.utils.toBN(0));
  340. expect(await this.accessControl.pendingDefaultAdmin()).to.equal(ZERO_ADDRESS);
  341. });
  342. it('reverts if called by non default admin accounts', async function () {
  343. await expectRevert(
  344. this.accessControl.cancelDefaultAdminTransfer({ from: other }),
  345. `${errorPrefix}: account ${other.toLowerCase()} is missing role ${DEFAULT_ADMIN_ROLE}`,
  346. );
  347. });
  348. });
  349. describe('renouncing admin', function () {
  350. let delayPassed;
  351. let from = defaultAdmin;
  352. beforeEach(async function () {
  353. await this.accessControl.beginDefaultAdminTransfer(ZERO_ADDRESS, { from });
  354. delayPassed = web3.utils
  355. .toBN(await time.latest())
  356. .add(delay)
  357. .addn(1);
  358. });
  359. it('it renounces role', async function () {
  360. await time.setNextBlockTimestamp(delayPassed);
  361. const receipt = await this.accessControl.renounceRole(DEFAULT_ADMIN_ROLE, from, { from });
  362. expect(await this.accessControl.hasRole(DEFAULT_ADMIN_ROLE, defaultAdmin)).to.be.false;
  363. expect(await this.accessControl.hasRole(ZERO_ADDRESS, defaultAdmin)).to.be.false;
  364. expectEvent(receipt, 'RoleRevoked', {
  365. role: DEFAULT_ADMIN_ROLE,
  366. account: from,
  367. });
  368. expect(await this.accessControl.owner()).to.equal(ZERO_ADDRESS);
  369. });
  370. it('allows to recover access using the internal _grantRole', async function () {
  371. await time.setNextBlockTimestamp(delayPassed);
  372. await this.accessControl.renounceRole(DEFAULT_ADMIN_ROLE, from, { from });
  373. const grantRoleReceipt = await this.accessControl.$_grantRole(DEFAULT_ADMIN_ROLE, other);
  374. expectEvent(grantRoleReceipt, 'RoleGranted', {
  375. role: DEFAULT_ADMIN_ROLE,
  376. account: other,
  377. });
  378. });
  379. it('reverts if caller is not default admin', async function () {
  380. await time.setNextBlockTimestamp(delayPassed);
  381. await expectRevert(
  382. this.accessControl.renounceRole(DEFAULT_ADMIN_ROLE, other, { from }),
  383. `${errorPrefix}: can only renounce roles for self`,
  384. );
  385. });
  386. describe('delayed until not passed', function () {
  387. let delayNotPassed;
  388. beforeEach(function () {
  389. delayNotPassed = delayPassed.subn(1);
  390. });
  391. it('reverts if block.timestamp is equal to delayed until', async function () {
  392. await time.setNextBlockTimestamp(delayNotPassed);
  393. await expectRevert(
  394. this.accessControl.renounceRole(DEFAULT_ADMIN_ROLE, defaultAdmin, { from }),
  395. `${errorPrefix}: only can renounce in two delayed steps`,
  396. );
  397. });
  398. it('reverts if block.timestamp is less than delayed until', async function () {
  399. await time.setNextBlockTimestamp(delayNotPassed.subn(1));
  400. await expectRevert(
  401. this.accessControl.renounceRole(DEFAULT_ADMIN_ROLE, defaultAdmin, { from }),
  402. `${errorPrefix}: only can renounce in two delayed steps`,
  403. );
  404. });
  405. });
  406. });
  407. }
  408. module.exports = {
  409. DEFAULT_ADMIN_ROLE,
  410. shouldBehaveLikeAccessControl,
  411. shouldBehaveLikeAccessControlEnumerable,
  412. shouldBehaveLikeAccessControlDefaultAdminRules,
  413. };