ReentrancyGuard.test.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const { expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { expectRevertCustomError } = require('../helpers/customError');
  4. const ReentrancyMock = artifacts.require('ReentrancyMock');
  5. const ReentrancyAttack = artifacts.require('ReentrancyAttack');
  6. contract('ReentrancyGuard', function () {
  7. beforeEach(async function () {
  8. this.reentrancyMock = await ReentrancyMock.new();
  9. expect(await this.reentrancyMock.counter()).to.be.bignumber.equal('0');
  10. });
  11. it('nonReentrant function can be called', async function () {
  12. expect(await this.reentrancyMock.counter()).to.be.bignumber.equal('0');
  13. await this.reentrancyMock.callback();
  14. expect(await this.reentrancyMock.counter()).to.be.bignumber.equal('1');
  15. });
  16. it('does not allow remote callback', async function () {
  17. const attacker = await ReentrancyAttack.new();
  18. await expectRevert(this.reentrancyMock.countAndCall(attacker.address), 'ReentrancyAttack: failed call', []);
  19. });
  20. it('_reentrancyGuardEntered should be true when guarded', async function () {
  21. await this.reentrancyMock.guardedCheckEntered();
  22. });
  23. it('_reentrancyGuardEntered should be false when unguarded', async function () {
  24. await this.reentrancyMock.unguardedCheckNotEntered();
  25. });
  26. // The following are more side-effects than intended behavior:
  27. // I put them here as documentation, and to monitor any changes
  28. // in the side-effects.
  29. it('does not allow local recursion', async function () {
  30. await expectRevertCustomError(this.reentrancyMock.countLocalRecursive(10), 'ReentrancyGuardReentrantCall', []);
  31. });
  32. it('does not allow indirect local recursion', async function () {
  33. await expectRevert(this.reentrancyMock.countThisRecursive(10), 'ReentrancyMock: failed call', []);
  34. });
  35. });