Changed before for beforeAll, refactored Bouncer tests. (#1094)

* Changed before for beforeAll, refactored Bouncer tests.

* Fixed linter errors.

* fix: updates for SignatureBouncer tests and voucher construction
pull/1028/merge
Nicolás Venturo 7 years ago committed by GitHub
parent ce0c3274ee
commit 67b67b791e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      contracts/access/SignatureBouncer.sol
  2. 16
      contracts/mocks/BouncerMock.sol
  3. 459
      test/access/SignatureBouncer.test.js
  4. 69
      test/helpers/sign.js
  5. 2
      test/introspection/SupportsInterfaceWithLookup.test.js
  6. 6
      test/library/ECRecovery.test.js
  7. 2
      test/library/Math.test.js
  8. 2
      test/library/MerkleProof.test.js
  9. 17
      test/lifecycle/Destructible.test.js
  10. 50
      test/lifecycle/Pausable.test.js
  11. 5
      test/lifecycle/TokenDestructible.test.js
  12. 2
      test/math/SafeMath.test.js
  13. 2
      test/ownership/HasNoEther.test.js
  14. 2
      test/ownership/Whitelist.test.js
  15. 2
      test/ownership/rbac/RBAC.test.js
  16. 2
      test/proposals/ERC1046/TokenMetadata.test.js

@ -34,8 +34,8 @@ contract SignatureBouncer is Ownable, RBAC {
string public constant ROLE_BOUNCER = "bouncer"; string public constant ROLE_BOUNCER = "bouncer";
uint constant METHOD_ID_SIZE = 4; uint constant METHOD_ID_SIZE = 4;
// (signature length size) 32 bytes + (signature size 65 bytes padded) 96 bytes // signature size is 65 bytes (tightly packed v + r + s), but gets padded to 96 bytes
uint constant SIGNATURE_SIZE = 128; uint constant SIGNATURE_SIZE = 96;
/** /**
* @dev requires that a valid signature of a bouncer was provided * @dev requires that a valid signature of a bouncer was provided

@ -36,7 +36,12 @@ contract SignatureBouncerMock is SignatureBouncer {
} }
function checkValidSignatureAndData(address _address, bytes _bytes, uint _val, bytes _sig) function checkValidSignatureAndData(
address _address,
bytes,
uint,
bytes _sig
)
public public
view view
returns (bool) returns (bool)
@ -44,11 +49,18 @@ contract SignatureBouncerMock is SignatureBouncer {
return isValidSignatureAndData(_address, _sig); return isValidSignatureAndData(_address, _sig);
} }
function onlyWithValidSignatureAndData(uint _val, bytes _sig) function onlyWithValidSignatureAndData(uint, bytes _sig)
onlyValidSignatureAndData(_sig) onlyValidSignatureAndData(_sig)
public public
view view
{ {
} }
function theWrongMethod(bytes)
public
pure
{
}
} }

@ -1,5 +1,5 @@
const { assertRevert } = require('../helpers/assertRevert'); const { assertRevert } = require('../helpers/assertRevert');
const { signHex } = require('../helpers/sign'); const { getBouncerSigner } = require('../helpers/sign');
const Bouncer = artifacts.require('SignatureBouncerMock'); const Bouncer = artifacts.require('SignatureBouncerMock');
@ -7,274 +7,239 @@ require('chai')
.use(require('chai-as-promised')) .use(require('chai-as-promised'))
.should(); .should();
const getSigner = (contract, signer, data = '') => (addr) => { const UINT_VALUE = 23;
// via: https://github.com/OpenZeppelin/zeppelin-solidity/pull/812/files const BYTES_VALUE = web3.toHex('test');
const message = contract.address.substr(2) + addr.substr(2) + data; const INVALID_SIGNATURE = '0xabcd';
// ^ substr to remove `0x` because in solidity the address is a set of byes, not a string `0xabcd`
return signHex(signer, message); contract('Bouncer', ([_, owner, anyone, bouncerAddress, authorizedUser]) => {
}; beforeEach(async function () {
const getMethodId = (methodName, ...paramTypes) => {
// methodId is a sha3 of the first 4 bytes after 0x of 'method(paramType1,...)'
return web3.sha3(`${methodName}(${paramTypes.join(',')})`).substr(2, 8);
};
const stripAndPadHexValue = (hexVal, sizeInBytes, start = true) => {
// strip 0x from the font and pad with 0's for
const strippedHexVal = hexVal.substr(2);
return start ? strippedHexVal.padStart(sizeInBytes * 2, 0) : strippedHexVal.padEnd(sizeInBytes * 2, 0);
};
contract('Bouncer', ([_, owner, authorizedUser, anyone, bouncerAddress, newBouncer]) => {
before(async function () {
this.bouncer = await Bouncer.new({ from: owner }); this.bouncer = await Bouncer.new({ from: owner });
this.roleBouncer = await this.bouncer.ROLE_BOUNCER(); this.roleBouncer = await this.bouncer.ROLE_BOUNCER();
this.genSig = getSigner(this.bouncer, bouncerAddress);
this.uintValue = 23;
this.checkValidSignatureAndMethodId = getMethodId('checkValidSignatureAndMethod', 'address', 'bytes');
this.uintValueData = stripAndPadHexValue(web3.toHex(this.uintValue), 32);
this.authorizedUserData = stripAndPadHexValue(authorizedUser, 32);
this.bytesValue = web3.toHex('bytesValue');
this.validateSignatureAndDataMsgData = [
getMethodId('checkValidSignatureAndData', 'address', 'bytes', 'uint256', 'bytes'),
stripAndPadHexValue(authorizedUser, 32),
stripAndPadHexValue(web3.toHex(32 * 4), 32), // bytesValue location
this.uintValueData,
stripAndPadHexValue(web3.toHex(32 * 6), 32), // sig location
stripAndPadHexValue(web3.toHex(this.bytesValue.substr(2).length / 2), 32), // bytesValue size
stripAndPadHexValue(this.bytesValue, 32, false), // bytesValue
];
});
it('should have a default owner of self', async function () {
const theOwner = await this.bouncer.owner();
theOwner.should.eq(owner);
});
it('should allow owner to add a bouncer', async function () {
await this.bouncer.addBouncer(bouncerAddress, { from: owner });
const hasRole = await this.bouncer.hasRole(bouncerAddress, this.roleBouncer);
hasRole.should.eq(true);
});
it('should not allow anyone to add a bouncer', async function () {
await assertRevert(
this.bouncer.addBouncer(bouncerAddress, { from: anyone })
);
}); });
context('modifiers', () => { context('management', () => {
it('should allow valid signature for sender', async function () { it('has a default owner of self', async function () {
await this.bouncer.onlyWithValidSignature( (await this.bouncer.owner()).should.eq(owner);
this.genSig(authorizedUser),
{ from: authorizedUser }
);
});
it('should not allow invalid signature for sender', async function () {
await assertRevert(
this.bouncer.onlyWithValidSignature(
'abcd',
{ from: authorizedUser }
)
);
});
it('should allow valid signature with a valid method for sender', async function () {
const sig = getSigner(
this.bouncer,
bouncerAddress,
getMethodId('onlyWithValidSignatureAndMethod', 'bytes')
)(authorizedUser);
await this.bouncer.onlyWithValidSignatureAndMethod(
sig,
{ from: authorizedUser }
);
});
it('should not allow invalid signature with method for sender', async function () {
await assertRevert(
this.bouncer.onlyWithValidSignatureAndMethod(
'abcd',
{ from: authorizedUser }
)
);
});
it('should allow valid signature with a valid data for sender', async function () {
const sig = getSigner(
this.bouncer,
bouncerAddress,
[
getMethodId('onlyWithValidSignatureAndData', 'uint256', 'bytes'),
this.uintValueData,
stripAndPadHexValue(web3.toHex(64), 32),
].join('')
)(authorizedUser);
await this.bouncer.onlyWithValidSignatureAndData(
this.uintValue,
sig,
{ from: authorizedUser }
);
});
it('should not allow invalid signature with data for sender', async function () {
await assertRevert(
this.bouncer.onlyWithValidSignatureAndData(
this.uintValue,
'abcd',
{ from: authorizedUser }
)
);
}); });
});
context('signatures', () => { it('allows the owner to add a bouncer', async function () {
it('should accept valid message for valid user', async function () { await this.bouncer.addBouncer(bouncerAddress, { from: owner });
const isValid = await this.bouncer.checkValidSignature( (await this.bouncer.hasRole(bouncerAddress, this.roleBouncer)).should.eq(true);
authorizedUser,
this.genSig(authorizedUser)
);
isValid.should.eq(true);
});
it('should not accept invalid message for valid user', async function () {
const isValid = await this.bouncer.checkValidSignature(
authorizedUser,
this.genSig(anyone)
);
isValid.should.eq(false);
}); });
it('should not accept invalid message for invalid user', async function () {
const isValid = await this.bouncer.checkValidSignature(
anyone,
'abcd'
);
isValid.should.eq(false);
});
it('should not accept valid message for invalid user', async function () {
const isValid = await this.bouncer.checkValidSignature(
anyone,
this.genSig(authorizedUser)
);
isValid.should.eq(false);
});
it('should accept valid message with valid method for valid user', async function () {
const sig = getSigner(
this.bouncer,
bouncerAddress,
getMethodId('checkValidSignatureAndMethod', 'address', 'bytes')
)(authorizedUser);
const isValid = await this.bouncer.checkValidSignatureAndMethod(
authorizedUser,
sig
);
isValid.should.eq(true);
});
it('should not accept valid message with an invalid method for valid user', async function () {
const sig = getSigner(
this.bouncer,
bouncerAddress,
getMethodId('invalidMethod', 'address', 'bytes')
)(authorizedUser);
const isValid = await this.bouncer.checkValidSignatureAndMethod(
authorizedUser,
sig
);
isValid.should.eq(false);
});
it('should not accept valid message with a valid method for an invalid user', async function () {
const sig = getSigner(
this.bouncer,
bouncerAddress,
this.checkValidSignatureAndMethodId
)(authorizedUser);
const isValid = await this.bouncer.checkValidSignatureAndMethod(
anyone,
sig
);
isValid.should.eq(false);
});
it('should accept valid method with valid params for valid user', async function () {
const sig = getSigner(
this.bouncer,
bouncerAddress,
this.validateSignatureAndDataMsgData.join('')
)(authorizedUser);
const isValid = await this.bouncer.checkValidSignatureAndData(
authorizedUser,
this.bytesValue,
this.uintValue,
sig
);
isValid.should.eq(true);
});
it('should not accept an invalid method with valid params for valid user', async function () {
this.validateSignatureAndDataMsgData[0] = getMethodId('invalidMethod', 'address', 'bytes', 'uint256', 'bytes');
const sig = getSigner(
this.bouncer,
bouncerAddress,
this.validateSignatureAndDataMsgData.join('')
)(authorizedUser);
const isValid = await this.bouncer.checkValidSignatureAndData(
authorizedUser,
this.bytesValue,
this.uintValue,
sig
);
isValid.should.eq(false);
});
it('should not accept valid method with invalid params for valid user', async function () {
const sig = getSigner(
this.bouncer,
bouncerAddress,
this.validateSignatureAndDataMsgData.join('')
)(authorizedUser);
const isValid = await this.bouncer.checkValidSignatureAndData(
authorizedUser,
this.bytesValue,
500,
sig
);
isValid.should.eq(false);
});
it('should not accept valid method with valid params for invalid user', async function () {
const sig = getSigner(
this.bouncer,
bouncerAddress,
this.validateSignatureAndDataMsgData.join('')
)(authorizedUser);
const isValid = await this.bouncer.checkValidSignatureAndData(
anyone,
this.bytesValue,
this.uintValue,
sig
);
isValid.should.eq(false);
});
});
context('management', () => { it('does not allow adding an invalid address', async function () {
it('should not allow anyone to add bouncers', async function () {
await assertRevert( await assertRevert(
this.bouncer.addBouncer(newBouncer, { from: anyone }) this.bouncer.addBouncer('0x0', { from: owner })
); );
}); });
it('should be able to add bouncers', async function () { it('allows the owner to remove a bouncer', async function () {
await this.bouncer.addBouncer(newBouncer, { from: owner }) await this.bouncer.addBouncer(bouncerAddress, { from: owner });
.should.be.fulfilled;
await this.bouncer.removeBouncer(bouncerAddress, { from: owner });
(await this.bouncer.hasRole(bouncerAddress, this.roleBouncer)).should.eq(false);
}); });
it('should not allow adding invalid address', async function () { it('does not allow anyone to add a bouncer', async function () {
await assertRevert( await assertRevert(
this.bouncer.addBouncer('0x0', { from: owner }) this.bouncer.addBouncer(bouncerAddress, { from: anyone })
); );
}); });
it('should not allow anyone to remove bouncer', async function () { it('does not allow anyone to remove a bouncer', async function () {
await this.bouncer.addBouncer(bouncerAddress, { from: owner });
await assertRevert( await assertRevert(
this.bouncer.removeBouncer(newBouncer, { from: anyone }) this.bouncer.removeBouncer(bouncerAddress, { from: anyone })
); );
}); });
});
it('should be able to remove bouncers', async function () { context('with bouncer address', () => {
await this.bouncer.removeBouncer(newBouncer, { from: owner }) beforeEach(async function () {
.should.be.fulfilled; await this.bouncer.addBouncer(bouncerAddress, { from: owner });
this.signFor = getBouncerSigner(this.bouncer, bouncerAddress);
});
describe('modifiers', () => {
context('plain signature', () => {
it('allows valid signature for sender', async function () {
await this.bouncer.onlyWithValidSignature(this.signFor(authorizedUser), { from: authorizedUser });
});
it('does not allow invalid signature for sender', async function () {
await assertRevert(
this.bouncer.onlyWithValidSignature(INVALID_SIGNATURE, { from: authorizedUser })
);
});
it('does not allow valid signature for other sender', async function () {
await assertRevert(
this.bouncer.onlyWithValidSignature(this.signFor(authorizedUser), { from: anyone })
);
});
it('does not allow valid signature for method for sender', async function () {
await assertRevert(
this.bouncer.onlyWithValidSignature(this.signFor(authorizedUser, 'onlyWithValidSignature'),
{ from: authorizedUser })
);
});
});
context('method signature', () => {
it('allows valid signature with correct method for sender', async function () {
await this.bouncer.onlyWithValidSignatureAndMethod(
this.signFor(authorizedUser, 'onlyWithValidSignatureAndMethod'), { from: authorizedUser }
);
});
it('does not allow invalid signature with correct method for sender', async function () {
await assertRevert(
this.bouncer.onlyWithValidSignatureAndMethod(INVALID_SIGNATURE, { from: authorizedUser })
);
});
it('does not allow valid signature with correct method for other sender', async function () {
await assertRevert(
this.bouncer.onlyWithValidSignatureAndMethod(
this.signFor(authorizedUser, 'onlyWithValidSignatureAndMethod'), { from: anyone }
)
);
});
it('does not allow valid method signature with incorrect method for sender', async function () {
await assertRevert(
this.bouncer.onlyWithValidSignatureAndMethod(this.signFor(authorizedUser, 'theWrongMethod'),
{ from: authorizedUser })
);
});
it('does not allow valid non-method signature method for sender', async function () {
await assertRevert(
this.bouncer.onlyWithValidSignatureAndMethod(this.signFor(authorizedUser), { from: authorizedUser })
);
});
});
context('method and data signature', () => {
it('allows valid signature with correct method and data for sender', async function () {
await this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE,
this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]), { from: authorizedUser }
);
});
it('does not allow invalid signature with correct method and data for sender', async function () {
await assertRevert(
this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE, INVALID_SIGNATURE, { from: authorizedUser })
);
});
it('does not allow valid signature with correct method and incorrect data for sender', async function () {
await assertRevert(
this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE + 10,
this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]),
{ from: authorizedUser }
)
);
});
it('does not allow valid signature with correct method and data for other sender', async function () {
await assertRevert(
this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE,
this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]),
{ from: anyone }
)
);
});
it('does not allow valid non-method signature for sender', async function () {
await assertRevert(
this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE,
this.signFor(authorizedUser), { from: authorizedUser }
)
);
});
});
});
context('signature validation', () => {
context('plain signature', () => {
it('validates valid signature for valid user', async function () {
(await this.bouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser))).should.eq(true);
});
it('does not validate invalid signature for valid user', async function () {
(await this.bouncer.checkValidSignature(authorizedUser, INVALID_SIGNATURE)).should.eq(false);
});
it('does not validate valid signature for anyone', async function () {
(await this.bouncer.checkValidSignature(anyone, this.signFor(authorizedUser))).should.eq(false);
});
it('does not validate valid signature for method for valid user', async function () {
(await this.bouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser, 'checkValidSignature'))
).should.eq(false);
});
});
context('method signature', () => {
it('validates valid signature with correct method for valid user', async function () {
(await this.bouncer.checkValidSignatureAndMethod(authorizedUser,
this.signFor(authorizedUser, 'checkValidSignatureAndMethod'))
).should.eq(true);
});
it('does not validate invalid signature with correct method for valid user', async function () {
(await this.bouncer.checkValidSignatureAndMethod(authorizedUser, INVALID_SIGNATURE)).should.eq(false);
});
it('does not validate valid signature with correct method for anyone', async function () {
(await this.bouncer.checkValidSignatureAndMethod(anyone,
this.signFor(authorizedUser, 'checkValidSignatureAndMethod'))
).should.eq(false);
});
it('does not validate valid non-method signature with correct method for valid user', async function () {
(await this.bouncer.checkValidSignatureAndMethod(authorizedUser, this.signFor(authorizedUser))
).should.eq(false);
});
});
context('method and data signature', () => {
it('validates valid signature with correct method and data for valid user', async function () {
(await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE,
this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
).should.eq(true);
});
it('does not validate invalid signature with correct method and data for valid user', async function () {
(await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE, INVALID_SIGNATURE)
).should.eq(false);
});
it('does not validate valid signature with correct method and incorrect data for valid user',
async function () {
(await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE + 10,
this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
).should.eq(false);
}
);
it('does not validate valid signature with correct method and data for anyone', async function () {
(await this.bouncer.checkValidSignatureAndData(anyone, BYTES_VALUE, UINT_VALUE,
this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
).should.eq(false);
});
it('does not validate valid non-method-data signature with correct method and data for valid user',
async function () {
(await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE,
this.signFor(authorizedUser, 'checkValidSignatureAndData'))
).should.eq(false);
}
);
});
}); });
}); });
}); });

@ -1,4 +1,10 @@
const utils = require('ethereumjs-util'); const utils = require('ethereumjs-util');
const { soliditySha3 } = require('web3-utils');
const REAL_SIGNATURE_SIZE = 2 * 65; // 65 bytes in hexadecimal string legnth
const PADDED_SIGNATURE_SIZE = 2 * 96; // 96 bytes in hexadecimal string length
const DUMMY_SIGNATURE = `0x${web3.padLeft('', REAL_SIGNATURE_SIZE)}`;
/** /**
* Hash and add same prefix to the hash that ganache use. * Hash and add same prefix to the hash that ganache use.
@ -11,18 +17,63 @@ function hashMessage (message) {
return utils.bufferToHex(utils.sha3(Buffer.concat([prefix, messageHex]))); return utils.bufferToHex(utils.sha3(Buffer.concat([prefix, messageHex])));
} }
// signs message using web3 (auto-applies prefix) // signs message in node (auto-applies prefix)
function signMessage (signer, message = '', options = {}) { // message must be in hex already! will not be autoconverted!
return web3.eth.sign(signer, web3.sha3(message, options)); const signMessage = (signer, message = '') => {
} return web3.eth.sign(signer, message);
};
// signs hex string using web3 (auto-applies prefix) // @TODO - remove this when we migrate to web3-1.0.0
function signHex (signer, message = '') { const transformToFullName = function (json) {
return signMessage(signer, message, { encoding: 'hex' }); if (json.name.indexOf('(') !== -1) {
} return json.name;
}
var typeName = json.inputs.map(function (i) { return i.type; }).join();
return json.name + '(' + typeName + ')';
};
/**
* Create a signer between a contract and a signer for a voucher of method, args, and redeemer
* Note that `method` is the web3 method, not the truffle-contract method
* Well truffle is terrible, but luckily (?) so is web3 < 1.0, so we get to make our own method id
* fetcher because the method on the contract isn't actually the SolidityFunction object _ಠ
* @param contract TruffleContract
* @param signer address
* @param redeemer address
* @param methodName string
* @param methodArgs any[]
*/
const getBouncerSigner = (contract, signer) => (redeemer, methodName, methodArgs = []) => {
const parts = [
contract.address,
redeemer,
];
// if we have a method, add it to the parts that we're signing
if (methodName) {
if (methodArgs.length > 0) {
parts.push(
contract.contract[methodName].getData(...methodArgs.concat([DUMMY_SIGNATURE])).slice(
0,
-1 * PADDED_SIGNATURE_SIZE
)
);
} else {
const abi = contract.abi.find(abi => abi.name === methodName);
const name = transformToFullName(abi);
const signature = web3.sha3(name).slice(0, 10);
parts.push(signature);
}
}
// ^ substr to remove `0x` because in solidity the address is a set of byes, not a string `0xabcd`
const hashOfMessage = soliditySha3(...parts);
return signMessage(signer, hashOfMessage);
};
module.exports = { module.exports = {
hashMessage, hashMessage,
signMessage, signMessage,
signHex, getBouncerSigner,
}; };

@ -8,7 +8,7 @@ require('chai')
.should(); .should();
contract('SupportsInterfaceWithLookup', function (accounts) { contract('SupportsInterfaceWithLookup', function (accounts) {
before(async function () { beforeEach(async function () {
this.mock = await SupportsInterfaceWithLookup.new(); this.mock = await SupportsInterfaceWithLookup.new();
}); });

@ -11,7 +11,7 @@ contract('ECRecovery', function (accounts) {
let ecrecovery; let ecrecovery;
const TEST_MESSAGE = 'OpenZeppelin'; const TEST_MESSAGE = 'OpenZeppelin';
before(async function () { beforeEach(async function () {
ecrecovery = await ECRecoveryMock.new(); ecrecovery = await ECRecoveryMock.new();
}); });
@ -37,7 +37,7 @@ contract('ECRecovery', function (accounts) {
it('recover using web3.eth.sign()', async function () { it('recover using web3.eth.sign()', async function () {
// Create the signature using account[0] // Create the signature using account[0]
const signature = signMessage(accounts[0], TEST_MESSAGE); const signature = signMessage(accounts[0], web3.sha3(TEST_MESSAGE));
// Recover the signer address from the generated message and signature. // Recover the signer address from the generated message and signature.
const addrRecovered = await ecrecovery.recover( const addrRecovered = await ecrecovery.recover(
@ -49,7 +49,7 @@ contract('ECRecovery', function (accounts) {
it('recover using web3.eth.sign() should return wrong signer', async function () { it('recover using web3.eth.sign() should return wrong signer', async function () {
// Create the signature using account[0] // Create the signature using account[0]
const signature = signMessage(accounts[0], TEST_MESSAGE); const signature = signMessage(accounts[0], web3.sha3(TEST_MESSAGE));
// Recover the signer address from the generated message and wrong signature. // Recover the signer address from the generated message and wrong signature.
const addrRecovered = await ecrecovery.recover(hashMessage('Nope'), signature); const addrRecovered = await ecrecovery.recover(hashMessage('Nope'), signature);

@ -3,7 +3,7 @@ var MathMock = artifacts.require('MathMock');
contract('Math', function (accounts) { contract('Math', function (accounts) {
let math; let math;
before(async function () { beforeEach(async function () {
math = await MathMock.new(); math = await MathMock.new();
}); });

@ -6,7 +6,7 @@ var MerkleProofWrapper = artifacts.require('MerkleProofWrapper');
contract('MerkleProof', function (accounts) { contract('MerkleProof', function (accounts) {
let merkleProof; let merkleProof;
before(async function () { beforeEach(async function () {
merkleProof = await MerkleProofWrapper.new(); merkleProof = await MerkleProofWrapper.new();
}); });

@ -2,20 +2,21 @@ var Destructible = artifacts.require('Destructible');
const { ethGetBalance } = require('../helpers/web3'); const { ethGetBalance } = require('../helpers/web3');
contract('Destructible', function (accounts) { contract('Destructible', function (accounts) {
beforeEach(async function () {
this.destructible = await Destructible.new({ from: accounts[0], value: web3.toWei('10', 'ether') });
this.owner = await this.destructible.owner();
});
it('should send balance to owner after destruction', async function () { it('should send balance to owner after destruction', async function () {
let destructible = await Destructible.new({ from: accounts[0], value: web3.toWei('10', 'ether') }); let initBalance = await ethGetBalance(this.owner);
let owner = await destructible.owner(); await this.destructible.destroy({ from: this.owner });
let initBalance = await ethGetBalance(owner); let newBalance = await ethGetBalance(this.owner);
await destructible.destroy({ from: owner });
let newBalance = await ethGetBalance(owner);
assert.isTrue(newBalance > initBalance); assert.isTrue(newBalance > initBalance);
}); });
it('should send balance to recepient after destruction', async function () { it('should send balance to recepient after destruction', async function () {
let destructible = await Destructible.new({ from: accounts[0], value: web3.toWei('10', 'ether') });
let owner = await destructible.owner();
let initBalance = await ethGetBalance(accounts[1]); let initBalance = await ethGetBalance(accounts[1]);
await destructible.destroyAndSend(accounts[1], { from: owner }); await this.destructible.destroyAndSend(accounts[1], { from: this.owner });
let newBalance = await ethGetBalance(accounts[1]); let newBalance = await ethGetBalance(accounts[1]);
assert.isTrue(newBalance.greaterThan(initBalance)); assert.isTrue(newBalance.greaterThan(initBalance));
}); });

@ -2,61 +2,59 @@ const { assertRevert } = require('../helpers/assertRevert');
const PausableMock = artifacts.require('PausableMock'); const PausableMock = artifacts.require('PausableMock');
contract('Pausable', function (accounts) { contract('Pausable', function (accounts) {
beforeEach(async function () {
this.Pausable = await PausableMock.new();
});
it('can perform normal process in non-pause', async function () { it('can perform normal process in non-pause', async function () {
let Pausable = await PausableMock.new(); let count0 = await this.Pausable.count();
let count0 = await Pausable.count();
assert.equal(count0, 0); assert.equal(count0, 0);
await Pausable.normalProcess(); await this.Pausable.normalProcess();
let count1 = await Pausable.count(); let count1 = await this.Pausable.count();
assert.equal(count1, 1); assert.equal(count1, 1);
}); });
it('can not perform normal process in pause', async function () { it('can not perform normal process in pause', async function () {
let Pausable = await PausableMock.new(); await this.Pausable.pause();
await Pausable.pause(); let count0 = await this.Pausable.count();
let count0 = await Pausable.count();
assert.equal(count0, 0); assert.equal(count0, 0);
await assertRevert(Pausable.normalProcess()); await assertRevert(this.Pausable.normalProcess());
let count1 = await Pausable.count(); let count1 = await this.Pausable.count();
assert.equal(count1, 0); assert.equal(count1, 0);
}); });
it('can not take drastic measure in non-pause', async function () { it('can not take drastic measure in non-pause', async function () {
let Pausable = await PausableMock.new(); await assertRevert(this.Pausable.drasticMeasure());
await assertRevert(Pausable.drasticMeasure()); const drasticMeasureTaken = await this.Pausable.drasticMeasureTaken();
const drasticMeasureTaken = await Pausable.drasticMeasureTaken();
assert.isFalse(drasticMeasureTaken); assert.isFalse(drasticMeasureTaken);
}); });
it('can take a drastic measure in a pause', async function () { it('can take a drastic measure in a pause', async function () {
let Pausable = await PausableMock.new(); await this.Pausable.pause();
await Pausable.pause(); await this.Pausable.drasticMeasure();
await Pausable.drasticMeasure(); let drasticMeasureTaken = await this.Pausable.drasticMeasureTaken();
let drasticMeasureTaken = await Pausable.drasticMeasureTaken();
assert.isTrue(drasticMeasureTaken); assert.isTrue(drasticMeasureTaken);
}); });
it('should resume allowing normal process after pause is over', async function () { it('should resume allowing normal process after pause is over', async function () {
let Pausable = await PausableMock.new(); await this.Pausable.pause();
await Pausable.pause(); await this.Pausable.unpause();
await Pausable.unpause(); await this.Pausable.normalProcess();
await Pausable.normalProcess(); let count0 = await this.Pausable.count();
let count0 = await Pausable.count();
assert.equal(count0, 1); assert.equal(count0, 1);
}); });
it('should prevent drastic measure after pause is over', async function () { it('should prevent drastic measure after pause is over', async function () {
let Pausable = await PausableMock.new(); await this.Pausable.pause();
await Pausable.pause(); await this.Pausable.unpause();
await Pausable.unpause();
await assertRevert(Pausable.drasticMeasure()); await assertRevert(this.Pausable.drasticMeasure());
const drasticMeasureTaken = await Pausable.drasticMeasureTaken(); const drasticMeasureTaken = await this.Pausable.drasticMeasureTaken();
assert.isFalse(drasticMeasureTaken); assert.isFalse(drasticMeasureTaken);
}); });
}); });

@ -5,16 +5,18 @@ var StandardTokenMock = artifacts.require('StandardTokenMock');
contract('TokenDestructible', function (accounts) { contract('TokenDestructible', function (accounts) {
let destructible; let destructible;
let owner;
beforeEach(async function () { beforeEach(async function () {
destructible = await TokenDestructible.new({ destructible = await TokenDestructible.new({
from: accounts[0], from: accounts[0],
value: web3.toWei('10', 'ether'), value: web3.toWei('10', 'ether'),
}); });
owner = await destructible.owner();
}); });
it('should send balance to owner after destruction', async function () { it('should send balance to owner after destruction', async function () {
let owner = await destructible.owner();
let initBalance = await ethGetBalance(owner); let initBalance = await ethGetBalance(owner);
await destructible.destroy([], { from: owner }); await destructible.destroy([], { from: owner });
let newBalance = await ethGetBalance(owner); let newBalance = await ethGetBalance(owner);
@ -22,7 +24,6 @@ contract('TokenDestructible', function (accounts) {
}); });
it('should send tokens to owner after destruction', async function () { it('should send tokens to owner after destruction', async function () {
let owner = await destructible.owner();
let token = await StandardTokenMock.new(destructible.address, 100); let token = await StandardTokenMock.new(destructible.address, 100);
let initContractBalance = await token.balanceOf(destructible.address); let initContractBalance = await token.balanceOf(destructible.address);
let initOwnerBalance = await token.balanceOf(owner); let initOwnerBalance = await token.balanceOf(owner);

@ -9,7 +9,7 @@ require('chai')
contract('SafeMath', () => { contract('SafeMath', () => {
const MAX_UINT = new BigNumber('115792089237316195423570985008687907853269984665640564039457584007913129639935'); const MAX_UINT = new BigNumber('115792089237316195423570985008687907853269984665640564039457584007913129639935');
before(async function () { beforeEach(async function () {
this.safeMath = await SafeMathMock.new(); this.safeMath = await SafeMathMock.new();
}); });

@ -7,7 +7,7 @@ const ForceEther = artifacts.require('ForceEther');
contract('HasNoEther', function (accounts) { contract('HasNoEther', function (accounts) {
const amount = web3.toWei('1', 'ether'); const amount = web3.toWei('1', 'ether');
it('should be constructorable', async function () { it('should be constructible', async function () {
await HasNoEtherTest.new(); await HasNoEtherTest.new();
}); });

@ -17,7 +17,7 @@ contract('Whitelist', function (accounts) {
const whitelistedAddresses = [whitelistedAddress1, whitelistedAddress2]; const whitelistedAddresses = [whitelistedAddress1, whitelistedAddress2];
before(async function () { beforeEach(async function () {
this.mock = await WhitelistMock.new(); this.mock = await WhitelistMock.new();
this.role = await this.mock.ROLE_WHITELISTED(); this.role = await this.mock.ROLE_WHITELISTED();
}); });

@ -19,7 +19,7 @@ contract('RBAC', function (accounts) {
...advisors ...advisors
] = accounts; ] = accounts;
before(async () => { beforeEach(async () => {
mock = await RBACMock.new(advisors, { from: admin }); mock = await RBACMock.new(advisors, { from: admin });
}); });

@ -7,7 +7,7 @@ require('chai')
const metadataURI = 'https://example.com'; const metadataURI = 'https://example.com';
describe('ERC20WithMetadata', function () { describe('ERC20WithMetadata', function () {
before(async function () { beforeEach(async function () {
this.token = await ERC20WithMetadata.new(metadataURI); this.token = await ERC20WithMetadata.new(metadataURI);
}); });

Loading…
Cancel
Save