BreakInvariantBounty.test.js 4.0 KB

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