Bounty.test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. const { ethGetBalance, ethSendTransaction } = require('./helpers/web3');
  2. var SecureTargetBounty = artifacts.require('SecureTargetBounty');
  3. var InsecureTargetBounty = artifacts.require('InsecureTargetBounty');
  4. const sendReward = async (from, to, value) => ethSendTransaction({
  5. from, to, value,
  6. });
  7. function awaitEvent (event, handler) {
  8. return new Promise((resolve, reject) => {
  9. function wrappedHandler (...args) {
  10. Promise.resolve(handler(...args)).then(resolve).catch(reject);
  11. }
  12. event.watch(wrappedHandler);
  13. });
  14. }
  15. contract('Bounty', function (accounts) {
  16. it('sets reward', async function () {
  17. let owner = accounts[0];
  18. let reward = web3.toWei(1, 'ether');
  19. let bounty = await SecureTargetBounty.new();
  20. await sendReward(owner, bounty.address, reward);
  21. const balance = await ethGetBalance(bounty.address);
  22. assert.equal(reward, balance.toNumber());
  23. });
  24. it('empties itself when destroyed', async function () {
  25. let owner = accounts[0];
  26. let reward = web3.toWei(1, 'ether');
  27. let bounty = await SecureTargetBounty.new();
  28. await sendReward(owner, bounty.address, reward);
  29. const balance = await ethGetBalance(bounty.address);
  30. assert.equal(reward, balance.toNumber());
  31. await bounty.destroy();
  32. const updatedBalance = await ethGetBalance(bounty.address);
  33. assert.equal(0, updatedBalance.toNumber());
  34. });
  35. describe('Against secure contract', function () {
  36. it('cannot claim reward', async function () {
  37. let owner = accounts[0];
  38. let researcher = accounts[1];
  39. let reward = web3.toWei(1, 'ether');
  40. let bounty = await SecureTargetBounty.new();
  41. let event = bounty.TargetCreated({});
  42. let watcher = async function (err, result) {
  43. event.stopWatching();
  44. if (err) { throw err; }
  45. var targetAddress = result.args.createdAddress;
  46. await sendReward(owner, bounty.address, reward);
  47. const balance = await ethGetBalance(bounty.address);
  48. assert.equal(reward, balance.toNumber());
  49. try {
  50. await bounty.claim(targetAddress, { from: researcher });
  51. assert.isTrue(false); // should never reach here
  52. } catch (error) {
  53. let reClaimedBounty = await bounty.claimed.call();
  54. assert.isFalse(reClaimedBounty);
  55. }
  56. try {
  57. await bounty.withdrawPayments({ from: researcher });
  58. assert.isTrue(false); // should never reach here
  59. } catch (err) {
  60. const updatedBalance = await ethGetBalance(bounty.address);
  61. assert.equal(reward, updatedBalance.toNumber());
  62. }
  63. };
  64. await bounty.createTarget({ from: researcher });
  65. await awaitEvent(event, watcher);
  66. });
  67. });
  68. describe('Against broken contract', function () {
  69. it('claims reward', async function () {
  70. let owner = accounts[0];
  71. let researcher = accounts[1];
  72. let reward = web3.toWei(1, 'ether');
  73. let bounty = await InsecureTargetBounty.new();
  74. let event = bounty.TargetCreated({});
  75. let watcher = async function (err, result) {
  76. event.stopWatching();
  77. if (err) { throw err; }
  78. let targetAddress = result.args.createdAddress;
  79. await sendReward(owner, bounty.address, reward);
  80. const balance = await ethGetBalance(bounty.address);
  81. assert.equal(reward, balance.toNumber());
  82. await bounty.claim(targetAddress, { from: researcher });
  83. let claim = await bounty.claimed.call();
  84. assert.isTrue(claim);
  85. await bounty.withdrawPayments({ from: researcher });
  86. const updatedBalance = await ethGetBalance(bounty.address);
  87. assert.equal(0, updatedBalance.toNumber());
  88. };
  89. await bounty.createTarget({ from: researcher });
  90. await awaitEvent(event, watcher);
  91. });
  92. });
  93. });