Unverified Commit 67b67b79 by Nicolás Venturo Committed by GitHub

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
parent ce0c3274
...@@ -34,8 +34,8 @@ contract SignatureBouncer is Ownable, RBAC { ...@@ -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 { ...@@ -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 { ...@@ -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
{
}
} }
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) { ...@@ -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') ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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'); ...@@ -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'); ...@@ -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'); ...@@ -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'); ...@@ -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'); ...@@ -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) { ...@@ -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') ...@@ -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'); ...@@ -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) { ...@@ -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) { ...@@ -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') ...@@ -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);
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment