Skip to content
Permalink
Browse files

ReentrancyGuard: add tests to ensure mutex always ends false

  • Loading branch information...
sohkai committed Apr 5, 2019
1 parent 884cbeb commit 3cb559afdba0a08fac01f885511100e6f0cb9533
Showing with 19 additions and 9 deletions.
  1. +19 −9 test/reentrancy_guard.js
@@ -5,16 +5,20 @@ const ZERO_ADDR = '0x0000000000000000000000000000000000000000'
contract('ReentrancyGuard', accounts => {
let reentrancyMock

async function assertReentrancyGuard(mutex, msg) {
assert.equal(
await web3.eth.getStorageAt(reentrancyMock.address, (await reentrancyMock.getReentrancyMutexPosition())),
mutex,
msg
)
}

beforeEach(async () => {
reentrancyMock = await artifacts.require('ReentrancyGuardMock').new()
})

it('starts with false mutex', async () => {
assert.equal(
await web3.eth.getStorageAt(reentrancyMock.address, (await reentrancyMock.getReentrancyMutexPosition())),
false,
're-entrancy guard should start false'
)
await assertReentrancyGuard(false, 're-entrancy mutex should start false')
})

it('starts with no calls', async () => {
@@ -24,25 +28,27 @@ contract('ReentrancyGuard', accounts => {
it('can call re-entrant function normally', async () => {
await reentrancyMock.reentrantCall(ZERO_ADDR)
assert.equal((await reentrancyMock.callCounter()).toString(), 1, 'should have registered one call')
await assertReentrancyGuard(false, 're-entrancy mutex should end false')
})

it('can call non-re-entrant function normally', async () => {
await reentrancyMock.nonReentrantCall(ZERO_ADDR)
assert.equal((await reentrancyMock.callCounter()).toString(), 1, 'should have registered one call')
await assertReentrancyGuard(false, 're-entrancy mutex should end false')
})

context('> Enabled re-entrancy guard', () => {
context('> Enabled re-entrancy mutex', () => {
beforeEach(async () => {
// Manually set re-entrancy guard
// Manually set re-entrancy mutex
await reentrancyMock.setReentrancyMutex(true)
})

it('can call re-entrant function if re-entrancy guard is enabled', async () => {
it('can call re-entrant function if re-entrancy mutex is enabled', async () => {
await reentrancyMock.reentrantCall(ZERO_ADDR)
assert.equal((await reentrancyMock.callCounter()).toString(), 1, 'should have called')
})

it('can not call non-re-entrant function if re-entrancy guard is enabled', async () => {
it('can not call non-re-entrant function if re-entrancy mutex is enabled', async () => {
await assertRevert(async () => {
await reentrancyMock.nonReentrantCall(ZERO_ADDR)
})
@@ -53,6 +59,10 @@ contract('ReentrancyGuard', accounts => {
context('> Re-entering through contract', async () => {
let reentrantActor

afterEach(async () => {
await assertReentrancyGuard(false, 're-entrancy mutex should end false')
})

context('> Re-enters re-entrant call', async () => {
before(async () => {
reentrantActor = await artifacts.require('ReentrantActor').new(false)

0 comments on commit 3cb559a

Please sign in to comment.
You can’t perform that action at this time.