BreakInvariantBounty.test.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. const { ethGetBalance, ethSendTransaction } = require('../helpers/web3');
  2. const expectEvent = require('../helpers/expectEvent');
  3. const { assertRevert } = require('../helpers/assertRevert');
  4. const BreakInvariantBountyMock = artifacts.require('BreakInvariantBountyMock');
  5. const TargetMock = artifacts.require('TargetMock');
  6. require('chai')
  7. .use(require('chai-bignumber')(web3.BigNumber))
  8. .should();
  9. const reward = new web3.BigNumber(web3.toWei(1, 'ether'));
  10. contract('BreakInvariantBounty', function ([_, owner, researcher, anyone, nonTarget]) {
  11. beforeEach(async function () {
  12. this.bounty = await BreakInvariantBountyMock.new({ from: owner });
  13. });
  14. it('can set reward', async function () {
  15. await ethSendTransaction({ from: owner, to: this.bounty.address, value: reward });
  16. (await ethGetBalance(this.bounty.address)).should.be.bignumber.equal(reward);
  17. });
  18. context('with reward', function () {
  19. beforeEach(async function () {
  20. await ethSendTransaction({ from: owner, to: this.bounty.address, value: reward });
  21. });
  22. describe('destroy', function () {
  23. it('returns all balance to the owner', async function () {
  24. const ownerPreBalance = await ethGetBalance(owner);
  25. await this.bounty.destroy({ from: owner, gasPrice: 0 });
  26. const ownerPostBalance = await ethGetBalance(owner);
  27. (await ethGetBalance(this.bounty.address)).should.be.bignumber.equal(0);
  28. ownerPostBalance.sub(ownerPreBalance).should.be.bignumber.equal(reward);
  29. });
  30. it('reverts when called by anyone', async function () {
  31. await assertRevert(this.bounty.destroy({ from: anyone }));
  32. });
  33. });
  34. describe('claim', function () {
  35. it('is initially unclaimed', async function () {
  36. (await this.bounty.claimed()).should.equal(false);
  37. });
  38. it('can create claimable target', async function () {
  39. const { logs } = await this.bounty.createTarget({ from: researcher });
  40. expectEvent.inLogs(logs, 'TargetCreated');
  41. });
  42. context('with target', async function () {
  43. beforeEach(async function () {
  44. const { logs } = await this.bounty.createTarget({ from: researcher });
  45. const event = expectEvent.inLogs(logs, 'TargetCreated');
  46. this.target = TargetMock.at(event.args.createdAddress);
  47. });
  48. context('before exploiting vulnerability', async function () {
  49. it('reverts when claiming reward', async function () {
  50. await assertRevert(this.bounty.claim(this.target.address, { from: researcher }));
  51. });
  52. });
  53. context('after exploiting vulnerability', async function () {
  54. beforeEach(async function () {
  55. await this.target.exploitVulnerability({ from: researcher });
  56. });
  57. it('sends the reward to the researcher', async function () {
  58. await this.bounty.claim(this.target.address, { from: anyone });
  59. const researcherPreBalance = await ethGetBalance(researcher);
  60. await this.bounty.withdrawPayments(researcher);
  61. const researcherPostBalance = await ethGetBalance(researcher);
  62. researcherPostBalance.sub(researcherPreBalance).should.be.bignumber.equal(reward);
  63. (await ethGetBalance(this.bounty.address)).should.be.bignumber.equal(0);
  64. });
  65. context('after claiming', async function () {
  66. beforeEach(async function () {
  67. await this.bounty.claim(this.target.address, { from: researcher });
  68. });
  69. it('is claimed', async function () {
  70. (await this.bounty.claimed()).should.equal(true);
  71. });
  72. it('no longer accepts rewards', async function () {
  73. await assertRevert(ethSendTransaction({ from: owner, to: this.bounty.address, value: reward }));
  74. });
  75. it('reverts when reclaimed', async function () {
  76. await assertRevert(this.bounty.claim(this.target.address, { from: researcher }));
  77. });
  78. });
  79. });
  80. });
  81. context('with non-target', function () {
  82. it('reverts when claiming reward', async function () {
  83. await assertRevert(this.bounty.claim(nonTarget, { from: researcher }));
  84. });
  85. });
  86. });
  87. });
  88. });