BreakInvariantBounty.test.js 5.0 KB

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