Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix: update bounty test to 2018
  • Loading branch information
shrugs committed Jun 16, 2018
commit 0e1fc86d87fe63fcaef45e782e0d189ae28559c9
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
161 changes: 58 additions & 103 deletions test/Bounty.test.js
Original file line number Diff line number Diff line change
@@ -1,132 +1,87 @@
import { getBalance } from './helpers/balance';
import expectEvent from './helpers/expectEvent';

let sendReward = function (sender, receiver, value) {
return new Promise(function (accept, reject) {
web3.eth.sendTransaction({
from: sender,
to: receiver,
value: value,
}, function (err, result) {
if (err) {
reject(err);
} else {
accept(result)
}
});
});
};

let getBalance = function (address) {
return new Promise(function (accept, reject) {
web3.eth.getBalance(address, function (err, result) {
if (err) {
reject(err);
} else {
accept(result);
}
})
});
}
require('chai')
.use(require('chai-bignumber')(web3.BigNumber))
.should();

var SecureTargetBounty = artifacts.require('SecureTargetBounty');
var InsecureTargetBounty = artifacts.require('InsecureTargetBounty');
const SecureTargetBounty = artifacts.require('SecureTargetBounty');
const InsecureTargetBounty = artifacts.require('InsecureTargetBounty');

function awaitEvent (event, handler) {
return new Promise((resolve, reject) => {
function wrappedHandler (...args) {
Promise.resolve(handler(...args)).then(resolve).catch(reject);
}
const reward = new web3.BigNumber(web3.toWei(1, 'ether'));

event.watch(wrappedHandler);
});
}

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();
await sendReward(owner, bounty.address, reward);
const bounty = await SecureTargetBounty.new();
await bounty.sendTransaction({ from: owner, value: reward });

assert.equal(reward, (await 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();
await sendReward(owner, bounty.address, reward);
const bounty = await SecureTargetBounty.new();
await bounty.sendTransaction({ from: owner, value: reward });

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

await bounty.destroy();
assert.equal(0, (await 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;
await sendReward(owner, bounty.address, reward);

assert.equal(reward,
(await 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,
(await 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({});

let watcher = async function (err, result) {
event.stopWatching();
if (err) { throw err; }
let targetAddress = result.args.createdAddress;
await sendReward(owner, bounty.address, reward);
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 });

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

await bounty.claim(targetAddress, { from: researcher });
let claim = await bounty.claimed.call();
await bounty.claim(targetAddress, { from: researcher });
const 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, (await 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));