ReentrancyGuard.test.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. const { contract } = require('@openzeppelin/test-environment');
  2. const { expectRevert } = require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. const ReentrancyMock = contract.fromArtifact('ReentrancyMock');
  5. const ReentrancyAttack = contract.fromArtifact('ReentrancyAttack');
  6. describe('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('should not allow remote callback', async function () {
  12. const attacker = await ReentrancyAttack.new();
  13. await expectRevert(
  14. this.reentrancyMock.countAndCall(attacker.address), 'ReentrancyAttack: failed call');
  15. });
  16. // The following are more side-effects than intended behavior:
  17. // I put them here as documentation, and to monitor any changes
  18. // in the side-effects.
  19. it('should not allow local recursion', async function () {
  20. await expectRevert(
  21. this.reentrancyMock.countLocalRecursive(10), 'ReentrancyGuard: reentrant call'
  22. );
  23. });
  24. it('should not allow indirect local recursion', async function () {
  25. await expectRevert(
  26. this.reentrancyMock.countThisRecursive(10), 'ReentrancyMock: failed call'
  27. );
  28. });
  29. });