ReentrancyGuard.test.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  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('does not allow remote callback', async function () {
  11. const attacker = await ReentrancyAttack.new();
  12. await expectRevert(
  13. this.reentrancyMock.countAndCall(attacker.address), 'ReentrancyAttack: failed call');
  14. });
  15. // The following are more side-effects than intended behavior:
  16. // I put them here as documentation, and to monitor any changes
  17. // in the side-effects.
  18. it('does not allow local recursion', async function () {
  19. await expectRevert(
  20. this.reentrancyMock.countLocalRecursive(10), 'ReentrancyGuard: reentrant call',
  21. );
  22. });
  23. it('does not allow indirect local recursion', async function () {
  24. await expectRevert(
  25. this.reentrancyMock.countThisRecursive(10), 'ReentrancyMock: failed call',
  26. );
  27. });
  28. });