Skip to content
Closed
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
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"chai-bignumber": "^2.0.0",
"coveralls": "^2.13.1",
"dotenv": "^4.0.0",
"es6-promisify": "^6.0.0",
"eslint": "^4.11.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.8.0",
Expand Down
140 changes: 58 additions & 82 deletions test/Bounty.test.js
Original file line number Diff line number Diff line change
@@ -1,111 +1,87 @@
import { getBalance } from './helpers/balance';
import expectEvent from './helpers/expectEvent';

let sendReward = function (sender, receiver, value) {
web3.eth.sendTransaction({
from: sender,
to: receiver,
value: value,
});
};
var SecureTargetBounty = artifacts.require('SecureTargetBounty');
var InsecureTargetBounty = artifacts.require('InsecureTargetBounty');
require('chai')
.use(require('chai-bignumber')(web3.BigNumber))
.should();

function awaitEvent (event, handler) {
return new Promise((resolve, reject) => {
function wrappedHandler (...args) {
Promise.resolve(handler(...args)).then(resolve).catch(reject);
}
const SecureTargetBounty = artifacts.require('SecureTargetBounty');
const InsecureTargetBounty = artifacts.require('InsecureTargetBounty');

event.watch(wrappedHandler);
});
}
const reward = new web3.BigNumber(web3.toWei(1, 'ether'));

contract('Bounty', function (accounts) {
contract('Bounty', function ([_, owner, researcher]) {
it('sets reward', async function () {
let owner = accounts[0];
let reward = web3.toWei(1, 'ether');
let bounty = await SecureTargetBounty.new();
sendReward(owner, bounty.address, reward);
const bounty = await SecureTargetBounty.new();
await bounty.sendTransaction({ from: owner, value: reward });

assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber());
const balance = await getBalance(bounty.address);
balance.should.be.bignumber.eq(reward);
});

it('empties itself when destroyed', async function () {
let owner = accounts[0];
let reward = web3.toWei(1, 'ether');
let bounty = await SecureTargetBounty.new();
sendReward(owner, bounty.address, reward);
const bounty = await SecureTargetBounty.new();
await bounty.sendTransaction({ from: owner, value: reward });

assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber());
const balance = await getBalance(bounty.address);
balance.should.be.bignumber.eq(reward);

await bounty.destroy();
assert.equal(0, web3.eth.getBalance(bounty.address).toNumber());
const newBalance = await getBalance(bounty.address);
newBalance.should.be.bignumber.eq(0);
});

describe('Against secure contract', function () {
it('cannot claim reward', async function () {
let owner = accounts[0];
let researcher = accounts[1];
let reward = web3.toWei(1, 'ether');
let bounty = await SecureTargetBounty.new();
let event = bounty.TargetCreated({});

let watcher = async function (err, result) {
event.stopWatching();
if (err) { throw err; }

var targetAddress = result.args.createdAddress;
sendReward(owner, bounty.address, reward);

assert.equal(reward,
web3.eth.getBalance(bounty.address).toNumber());

try {
await bounty.claim(targetAddress, { from: researcher });
assert.isTrue(false); // should never reach here
} catch (error) {
let reClaimedBounty = await bounty.claimed.call();
assert.isFalse(reClaimedBounty);
}
try {
await bounty.withdrawPayments({ from: researcher });
assert.isTrue(false); // should never reach here
} catch (err) {
assert.equal(reward,
web3.eth.getBalance(bounty.address).toNumber());
}
};
await bounty.createTarget({ from: researcher });
await awaitEvent(event, watcher);
const bounty = await SecureTargetBounty.new();

const { logs } = await bounty.createTarget({ from: researcher });
await expectEvent.inLogs(logs, 'TargetCreated');

const targetAddress = logs[0].args.createdAddress;
await bounty.sendTransaction({ from: owner, value: reward });

let balance = await getBalance(bounty.address);
balance.should.be.bignumber.eq(reward);

try {
await bounty.claim(targetAddress, { from: researcher });

assert.isTrue(false); // should never reach here
} catch (error) {
const reClaimedBounty = await bounty.claimed.call();
reClaimedBounty.should.eq(false);
}
try {
await bounty.withdrawPayments({ from: researcher });
assert.isTrue(false); // should never reach here
} catch (err) {
balance = await getBalance(bounty.address);
balance.should.be.bignumber.eq(reward);
}
});
});

describe('Against broken contract', function () {
it('claims reward', async function () {
let owner = accounts[0];
let researcher = accounts[1];
let reward = web3.toWei(1, 'ether');
let bounty = await InsecureTargetBounty.new();
let event = bounty.TargetCreated({});
const bounty = await InsecureTargetBounty.new();
const { logs } = await bounty.createTarget({ from: researcher });
await expectEvent.inLogs(logs, 'TargetCreated');
let targetAddress = logs[0].args.createdAddress;
await bounty.sendTransaction({ from: owner, value: reward });

let watcher = async function (err, result) {
event.stopWatching();
if (err) { throw err; }
let targetAddress = result.args.createdAddress;
sendReward(owner, bounty.address, reward);
let balance = await getBalance(bounty.address);
balance.should.be.bignumber.eq(reward);

assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber());
await bounty.claim(targetAddress, { from: researcher });
const claim = await bounty.claimed.call();

await bounty.claim(targetAddress, { from: researcher });
let claim = await bounty.claimed.call();

assert.isTrue(claim);
claim.should.eq(true);

await bounty.withdrawPayments({ from: researcher });
await bounty.withdrawPayments({ from: researcher });

assert.equal(0, web3.eth.getBalance(bounty.address).toNumber());
};
await bounty.createTarget({ from: researcher });
await awaitEvent(event, watcher);
balance = await getBalance(bounty.address);
balance.should.be.bignumber.eq(0);
});
});
});
3 changes: 3 additions & 0 deletions test/helpers/balance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { promisify } from 'es6-promisify';

export const getBalance = promisify(web3.eth.getBalance.bind(web3.eth));