Update modifiers so that they fail "loudly" by throwing errors rather then silently no-oping. Updated tests to remain compatible with these changes

pull/147/head
Fabio Berger 8 years ago
parent c10a2cf15c
commit 3d6988cf90
  1. 5
      contracts/DayLimit.sol
  2. 4
      contracts/ownership/Claimable.sol
  3. 5
      contracts/ownership/Ownable.sol
  4. 5
      contracts/ownership/Shareable.sol
  5. 22
      test/Claimable.js
  6. 15
      test/DayLimit.js
  7. 14
      test/Ownable.js

@ -58,8 +58,9 @@ contract DayLimit {
// simple modifier for daily limit. // simple modifier for daily limit.
modifier limitedDaily(uint _value) { modifier limitedDaily(uint _value) {
if (underLimit(_value)) { if (!underLimit(_value)) {
_; throw;
} }
_;
} }
} }

@ -13,7 +13,9 @@ contract Claimable is Ownable {
address public pendingOwner; address public pendingOwner;
modifier onlyPendingOwner() { modifier onlyPendingOwner() {
if (msg.sender == pendingOwner) if (msg.sender != pendingOwner) {
throw;
}
_; _;
} }

@ -15,9 +15,10 @@ contract Ownable {
} }
modifier onlyOwner() { modifier onlyOwner() {
if (msg.sender == owner) { if (msg.sender != owner) {
_; throw;
} }
_;
} }
function transferOwnership(address newOwner) onlyOwner { function transferOwnership(address newOwner) onlyOwner {

@ -39,9 +39,10 @@ contract Shareable {
// simple single-sig function modifier. // simple single-sig function modifier.
modifier onlyOwner { modifier onlyOwner {
if (isOwner(msg.sender)) { if (!isOwner(msg.sender)) {
_; throw;
} }
_;
} }
// multi-sig function modifier: the operation must have an intrinsic hash in order // multi-sig function modifier: the operation must have an intrinsic hash in order

@ -1,4 +1,5 @@
'use strict'; 'use strict';
const assertJump = require('./helpers/assertJump');
var Claimable = artifacts.require('../contracts/ownership/Claimable.sol'); var Claimable = artifacts.require('../contracts/ownership/Claimable.sol');
@ -23,17 +24,22 @@ contract('Claimable', function(accounts) {
}); });
it('should prevent to claimOwnership from no pendingOwner', async function() { it('should prevent to claimOwnership from no pendingOwner', async function() {
claimable.claimOwnership({from: accounts[2]}); try {
let owner = await claimable.owner(); await claimable.claimOwnership({from: accounts[2]});
} catch(error) {
assert.isTrue(owner !== accounts[2]); assertJump(error);
}
}); });
it('should prevent non-owners from transfering', async function() { it('should prevent non-owners from transfering', async function() {
await claimable.transferOwnership(accounts[2], {from: accounts[2]}); const other = accounts[2];
let pendingOwner = await claimable.pendingOwner(); const owner = await claimable.owner.call();
assert.isTrue(owner !== other);
assert.isFalse(pendingOwner === accounts[2]); try {
await claimable.transferOwnership(other, {from: other});
} catch(error) {
assertJump(error);
}
}); });
describe('after initiating a transfer', function () { describe('after initiating a transfer', function () {

@ -1,4 +1,5 @@
'use strict'; 'use strict';
const assertJump = require('./helpers/assertJump');
var DayLimitMock = artifacts.require('helpers/DayLimitMock.sol'); var DayLimitMock = artifacts.require('helpers/DayLimitMock.sol');
@ -32,9 +33,11 @@ contract('DayLimit', function(accounts) {
let spentToday = await dayLimit.spentToday(); let spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8); assert.equal(spentToday, 8);
try {
await dayLimit.attemptSpend(3); await dayLimit.attemptSpend(3);
spentToday = await dayLimit.spentToday(); } catch(error) {
assert.equal(spentToday, 8); assertJump(error);
}
}); });
it('should allow spending if daily limit is reached and then set higher', async function() { it('should allow spending if daily limit is reached and then set higher', async function() {
@ -45,7 +48,11 @@ contract('DayLimit', function(accounts) {
let spentToday = await dayLimit.spentToday(); let spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8); assert.equal(spentToday, 8);
try {
await dayLimit.attemptSpend(3); await dayLimit.attemptSpend(3);
} catch(error) {
assertJump(error);
}
spentToday = await dayLimit.spentToday(); spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8); assert.equal(spentToday, 8);
@ -63,7 +70,11 @@ contract('DayLimit', function(accounts) {
let spentToday = await dayLimit.spentToday(); let spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8); assert.equal(spentToday, 8);
try {
await dayLimit.attemptSpend(3); await dayLimit.attemptSpend(3);
} catch(error) {
assertJump(error);
}
spentToday = await dayLimit.spentToday(); spentToday = await dayLimit.spentToday();
assert.equal(spentToday, 8); assert.equal(spentToday, 8);

@ -1,4 +1,5 @@
'use strict'; 'use strict';
const assertJump = require('./helpers/assertJump');
var Ownable = artifacts.require('../contracts/ownership/Ownable.sol'); var Ownable = artifacts.require('../contracts/ownership/Ownable.sol');
@ -23,11 +24,14 @@ contract('Ownable', function(accounts) {
}); });
it('should prevent non-owners from transfering', async function() { it('should prevent non-owners from transfering', async function() {
let other = accounts[2]; const other = accounts[2];
await ownable.transferOwnership(other, {from: accounts[2]}); const owner = await ownable.owner.call();
let owner = await ownable.owner(); assert.isTrue(owner !== other);
try {
assert.isFalse(owner === other); await ownable.transferOwnership(other, {from: other});
} catch(error) {
assertJump(error);
}
}); });
it('should guard ownership against stuck state', async function() { it('should guard ownership against stuck state', async function() {

Loading…
Cancel
Save