ReentrancyGuard.test.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 (accounts) {
  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. // The following are more side-effects than intended behavior:
  21. // I put them here as documentation, and to monitor any changes
  22. // in the side-effects.
  23. it('does not allow local recursion', async function () {
  24. await expectRevert(
  25. this.reentrancyMock.countLocalRecursive(10), 'ReentrancyGuard: reentrant call',
  26. );
  27. });
  28. it('does not allow indirect local recursion', async function () {
  29. await expectRevert(
  30. this.reentrancyMock.countThisRecursive(10), 'ReentrancyMock: failed call',
  31. );
  32. });
  33. });