Navigation Menu

Skip to content

Commit

Permalink
ReentrancyGuard: add tests to ensure mutex always ends false
Browse files Browse the repository at this point in the history
  • Loading branch information
sohkai committed Apr 5, 2019
1 parent 884cbeb commit 3cb559a
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions test/reentrancy_guard.js
Expand Up @@ -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 () => {
Expand All @@ -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)
})
Expand All @@ -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)
Expand Down

0 comments on commit 3cb559a

Please sign in to comment.