Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create tests for Shareable #100

Merged
merged 3 commits into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions contracts/Shareable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -162,4 +162,3 @@ contract Shareable {
}

}

16 changes: 16 additions & 0 deletions contracts/test-helpers/ShareableMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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;
}

}
101 changes: 101 additions & 0 deletions test/Shareable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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.isOwner(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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a string here seems confusing. What's the reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I double checked and it doesn't make a difference. all the toString()'s in this test method can be removed.


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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above


//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();

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);
} else {
assert.equal(initCount.toString(), count);
}
}
});

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();

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.toString(), count);
}
});

});