ReentrancyGuard.test.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const { expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const ReentrancyMock = artifacts.require('ReentrancyMock');
  4. const ReentrancyAttack = artifacts.require('ReentrancyAttack');
  5. contract('ReentrancyGuard', function () {
  6. beforeEach(async function () {
  7. this.reentrancyMock = await ReentrancyMock.new();
  8. expect(await this.reentrancyMock.counter()).to.be.bignumber.equal('0');
  9. });
  10. it('nonReentrant function can be called', async function () {
  11. expect(await this.reentrancyMock.counter()).to.be.bignumber.equal('0');
  12. await this.reentrancyMock.callback();
  13. expect(await this.reentrancyMock.counter()).to.be.bignumber.equal('1');
  14. });
  15. it('does not allow remote callback', async function () {
  16. const attacker = await ReentrancyAttack.new();
  17. await expectRevert(
  18. 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 expectRevert(
  31. this.reentrancyMock.countLocalRecursive(10), 'ReentrancyGuard: reentrant call',
  32. );
  33. });
  34. it('does not allow indirect local recursion', async function () {
  35. await expectRevert(
  36. this.reentrancyMock.countThisRecursive(10), 'ReentrancyMock: failed call',
  37. );
  38. });
  39. });