BreakInvariantBounty.test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const { ethGetBalance, ethSendTransaction } = require('./helpers/web3');
  2. const { sendEther } = require('./helpers/sendTransaction');
  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 = new web3.BigNumber(web3.toWei(1, 'ether'));
  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 sendEther(owner, this.bounty.address, reward);
  18. (await ethGetBalance(this.bounty.address)).should.be.bignumber.equal(reward);
  19. });
  20. context('with reward', function () {
  21. beforeEach(async function () {
  22. await sendEther(owner, this.bounty.address, reward);
  23. });
  24. describe('destroy', function () {
  25. it('returns all balance to the owner', async function () {
  26. const ownerPreBalance = await ethGetBalance(owner);
  27. await this.bounty.destroy({ from: owner, gasPrice: 0 });
  28. const ownerPostBalance = await ethGetBalance(owner);
  29. (await ethGetBalance(this.bounty.address)).should.be.bignumber.equal(0);
  30. ownerPostBalance.sub(ownerPreBalance).should.be.bignumber.equal(reward);
  31. });
  32. it('reverts when called by anyone', async function () {
  33. await assertRevert(this.bounty.destroy({ from: anyone }));
  34. });
  35. });
  36. describe('claim', function () {
  37. it('is initially unclaimed', async function () {
  38. (await this.bounty.claimed()).should.equal(false);
  39. });
  40. it('can create claimable target', async function () {
  41. const { logs } = await this.bounty.createTarget({ from: researcher });
  42. expectEvent.inLogs(logs, 'TargetCreated');
  43. });
  44. context('with target', async function () {
  45. beforeEach(async function () {
  46. const { logs } = await this.bounty.createTarget({ from: researcher });
  47. const event = expectEvent.inLogs(logs, 'TargetCreated');
  48. this.target = TargetMock.at(event.args.createdAddress);
  49. });
  50. context('before exploiting vulnerability', async function () {
  51. it('reverts when claiming reward', async function () {
  52. await assertRevert(this.bounty.claim(this.target.address, { from: researcher }));
  53. });
  54. });
  55. context('after exploiting vulnerability', async function () {
  56. beforeEach(async function () {
  57. await this.target.exploitVulnerability({ from: researcher });
  58. });
  59. it('sends the reward to the researcher', async function () {
  60. await this.bounty.claim(this.target.address, { from: anyone });
  61. (await balanceDifference(researcher, () => this.bounty.withdrawPayments(researcher)))
  62. .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. });