ReentrancyGuard.test.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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(this.reentrancyMock.countAndCall(attacker.address), 'ReentrancyAttack: failed call');
  18. });
  19. it('_reentrancyGuardEntered should be true when guarded', async function () {
  20. await this.reentrancyMock.guardedCheckEntered();
  21. });
  22. it('_reentrancyGuardEntered should be false when unguarded', async function () {
  23. await this.reentrancyMock.unguardedCheckNotEntered();
  24. });
  25. // The following are more side-effects than intended behavior:
  26. // I put them here as documentation, and to monitor any changes
  27. // in the side-effects.
  28. it('does not allow local recursion', async function () {
  29. await expectRevert(this.reentrancyMock.countLocalRecursive(10), 'ReentrancyGuard: reentrant call');
  30. });
  31. it('does not allow indirect local recursion', async function () {
  32. await expectRevert(this.reentrancyMock.countThisRecursive(10), 'ReentrancyMock: failed call');
  33. });
  34. });