ReentrancyGuard.test.js 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. const { shouldFail } = require('openzeppelin-test-helpers');
  2. const ReentrancyMock = artifacts.require('ReentrancyMock');
  3. const ReentrancyAttack = artifacts.require('ReentrancyAttack');
  4. contract('ReentrancyGuard', function () {
  5. beforeEach(async function () {
  6. this.reentrancyMock = await ReentrancyMock.new();
  7. (await this.reentrancyMock.counter()).should.be.bignumber.equal('0');
  8. });
  9. it('should not allow remote callback', async function () {
  10. const attacker = await ReentrancyAttack.new();
  11. await shouldFail.reverting.withMessage(
  12. this.reentrancyMock.countAndCall(attacker.address), 'ReentrancyGuard: reentrant call');
  13. });
  14. // The following are more side-effects than intended behavior:
  15. // I put them here as documentation, and to monitor any changes
  16. // in the side-effects.
  17. it('should not allow local recursion', async function () {
  18. await shouldFail.reverting.withMessage(
  19. this.reentrancyMock.countLocalRecursive(10), 'ReentrancyGuard: reentrant call'
  20. );
  21. });
  22. it('should not allow indirect local recursion', async function () {
  23. await shouldFail.reverting.withMessage(
  24. this.reentrancyMock.countThisRecursive(10), 'ReentrancyMock: failed call'
  25. );
  26. });
  27. });