From 0ec09f4131078cd7d1143f9df8653fc36bff8e31 Mon Sep 17 00:00:00 2001 From: Arseniy Klempner Date: Mon, 5 Dec 2016 11:32:41 -0800 Subject: [PATCH 1/3] Create tests for Shareable --- contracts/test-helpers/ShareableMock.sol | 19 +++++ test/Shareable.js | 103 +++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 contracts/test-helpers/ShareableMock.sol create mode 100644 test/Shareable.js diff --git a/contracts/test-helpers/ShareableMock.sol b/contracts/test-helpers/ShareableMock.sol new file mode 100644 index 000000000..82530e888 --- /dev/null +++ b/contracts/test-helpers/ShareableMock.sol @@ -0,0 +1,19 @@ +pragma solidity ^0.4.4; +import "../Shareable.sol"; + +contract ShareableMock is Shareable { + + uint public count = 0; + + function ShareableMock(address[] _owners, uint _required) Shareable(_owners, _required) { + + } + + function increaseCount(bytes32 action) onlymanyowners(action) { + count = count + 1; + } + + function isOwnerConst(address _addr) constant returns (bool) { + return isOwner(_addr); + } +} diff --git a/test/Shareable.js b/test/Shareable.js new file mode 100644 index 000000000..883552856 --- /dev/null +++ b/test/Shareable.js @@ -0,0 +1,103 @@ +contract('Shareable', function(accounts) { + + it('should construct with correct owners and number of sigs required', async function() { + let requiredSigs = 2; + let owners = accounts.slice(1,4); + let shareable = await ShareableMock.new(owners, requiredSigs); + + let required = await shareable.required(); + assert.equal(required, requiredSigs); + let owner = await shareable.getOwner(0); + assert.equal(owner, accounts[0]); + + for(let i = 0; i < accounts.length; i++) { + let owner = await shareable.getOwner(i); + let isowner = await shareable.isOwnerConst(accounts[i]); + if(i <= owners.length) { + assert.equal(accounts[i], owner); + assert.isTrue(isowner); + } else { + assert.notEqual(accounts[i], owner); + assert.isFalse(isowner); + } + } + }); + + it('should only perform multisig function with enough sigs', async function() { + let requiredSigs = 3; + let owners = accounts.slice(1,4); + let shareable = await ShareableMock.new(owners, requiredSigs); + let hash = 1234; + + let initCount = await shareable.count(); + initCount = initCount.toString(); + + for(let i = 0; i < requiredSigs; i++) { + await shareable.increaseCount(hash, {from: accounts[i]}); + let count = await shareable.count(); + if(i == requiredSigs - 1) { + assert.equal(Number(initCount)+1, count.toString()); + } else { + assert.equal(initCount, count.toString()); + } + } + }); + + it('should require approval from different owners', async function() { + let requiredSigs = 2; + let owners = accounts.slice(1,4); + let shareable = await ShareableMock.new(owners, requiredSigs); + let hash = 1234; + + let initCount = await shareable.count(); + initCount = initCount.toString(); + + //Count shouldn't increase when the same owner calls repeatedly + for(let i = 0; i < 2; i++) { + await shareable.increaseCount(hash); + let count = await shareable.count(); + assert.equal(initCount, count.toString()); + } + }); + + it('should reset sig count after operation is approved', async function() { + let requiredSigs = 3; + let owners = accounts.slice(1,4); + let shareable = await ShareableMock.new(owners, requiredSigs); + let hash = 1234; + + let initCount = await shareable.count(); + initCount = initCount.toString(); + + for(let i = 0; i < requiredSigs * 3; i++) { + await shareable.increaseCount(hash, {from: accounts[i % 4]}); + let count = await shareable.count(); + if((i%(requiredSigs)) == requiredSigs - 1) { + initCount = Number(initCount)+1; + assert.equal(initCount, count.toString()); + } else { + assert.equal(initCount, count.toString()); + } + } + }); + + it('should not perform multisig function after an owner revokes', async function() { + let requiredSigs = 3; + let owners = accounts.slice(1,4); + let shareable = await ShareableMock.new(owners, requiredSigs); + let hash = 1234; + + let initCount = await shareable.count(); + initCount = initCount.toString(); + + for(let i = 0; i < requiredSigs; i++) { + if(i == 1) { + await shareable.revoke(hash, {from: accounts[i-1]}); + } + await shareable.increaseCount(hash, {from: accounts[i]}); + let count = await shareable.count(); + assert.equal(initCount, count.toString()); + } + }); + +}); From e8459148a8b9a017f0801821762c787487c7e34e Mon Sep 17 00:00:00 2001 From: Arseniy Klempner Date: Mon, 19 Dec 2016 21:52:37 -0800 Subject: [PATCH 2/3] Remove unnecessary toString calls --- test/Shareable.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/Shareable.js b/test/Shareable.js index 883552856..36bb7a89d 100644 --- a/test/Shareable.js +++ b/test/Shareable.js @@ -67,16 +67,16 @@ contract('Shareable', function(accounts) { let hash = 1234; let initCount = await shareable.count(); - initCount = initCount.toString(); + //initCount = initCount.toString(); for(let i = 0; i < requiredSigs * 3; i++) { await shareable.increaseCount(hash, {from: accounts[i % 4]}); let count = await shareable.count(); if((i%(requiredSigs)) == requiredSigs - 1) { initCount = Number(initCount)+1; - assert.equal(initCount, count.toString()); + assert.equal(initCount, count); } else { - assert.equal(initCount, count.toString()); + assert.equal(initCount, count); } } }); @@ -88,7 +88,6 @@ contract('Shareable', function(accounts) { let hash = 1234; let initCount = await shareable.count(); - initCount = initCount.toString(); for(let i = 0; i < requiredSigs; i++) { if(i == 1) { @@ -96,7 +95,7 @@ contract('Shareable', function(accounts) { } await shareable.increaseCount(hash, {from: accounts[i]}); let count = await shareable.count(); - assert.equal(initCount, count.toString()); + assert.equal(initCount, count); } }); From e5eaa919b119a0c114d567090bd13a5841a676cb Mon Sep 17 00:00:00 2001 From: Arseniy Klempner Date: Tue, 20 Dec 2016 13:02:24 -0800 Subject: [PATCH 3/3] Make isOwner() const. Add necessary toString() calls in tests. --- contracts/Shareable.sol | 5 ++--- contracts/test-helpers/ShareableMock.sol | 3 --- test/Shareable.js | 7 +++---- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/contracts/Shareable.sol b/contracts/Shareable.sol index b73f31eee..d416f80be 100644 --- a/contracts/Shareable.sol +++ b/contracts/Shareable.sol @@ -4,7 +4,7 @@ pragma solidity ^0.4.4; /* * Shareable * - * Based on https://github.com/ethereum/dapp-bin/blob/master/wallet/wallet.sol + * Based on https://github.com/ethereum/dapp-bin/blob/master/wallet/wallet.sol * * inheritable "property" contract that enables methods to be protected by requiring the acquiescence of either a single, or, crucially, each of a number of, designated owners. * @@ -98,7 +98,7 @@ contract Shareable { return address(owners[ownerIndex + 1]); } - function isOwner(address _addr) returns (bool) { + function isOwner(address _addr) constant returns (bool) { return ownerIndex[uint(_addr)] > 0; } @@ -162,4 +162,3 @@ contract Shareable { } } - diff --git a/contracts/test-helpers/ShareableMock.sol b/contracts/test-helpers/ShareableMock.sol index 82530e888..ebf06546e 100644 --- a/contracts/test-helpers/ShareableMock.sol +++ b/contracts/test-helpers/ShareableMock.sol @@ -13,7 +13,4 @@ contract ShareableMock is Shareable { count = count + 1; } - function isOwnerConst(address _addr) constant returns (bool) { - return isOwner(_addr); - } } diff --git a/test/Shareable.js b/test/Shareable.js index 36bb7a89d..fba268268 100644 --- a/test/Shareable.js +++ b/test/Shareable.js @@ -12,7 +12,7 @@ contract('Shareable', function(accounts) { for(let i = 0; i < accounts.length; i++) { let owner = await shareable.getOwner(i); - let isowner = await shareable.isOwnerConst(accounts[i]); + let isowner = await shareable.isOwner(accounts[i]); if(i <= owners.length) { assert.equal(accounts[i], owner); assert.isTrue(isowner); @@ -67,7 +67,6 @@ contract('Shareable', function(accounts) { let hash = 1234; let initCount = await shareable.count(); - //initCount = initCount.toString(); for(let i = 0; i < requiredSigs * 3; i++) { await shareable.increaseCount(hash, {from: accounts[i % 4]}); @@ -76,7 +75,7 @@ contract('Shareable', function(accounts) { initCount = Number(initCount)+1; assert.equal(initCount, count); } else { - assert.equal(initCount, count); + assert.equal(initCount.toString(), count); } } }); @@ -95,7 +94,7 @@ contract('Shareable', function(accounts) { } await shareable.increaseCount(hash, {from: accounts[i]}); let count = await shareable.count(); - assert.equal(initCount, count); + assert.equal(initCount.toString(), count); } });