Bounty.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. let sendReward = function(sender, receiver, value){
  2. web3.eth.sendTransaction({
  3. from:sender,
  4. to:receiver,
  5. value: value
  6. })
  7. }
  8. contract('Bounty', function(accounts) {
  9. it("sets reward", async function(){
  10. let owner = accounts[0];
  11. let reward = web3.toWei(1, "ether");
  12. let bounty = await SecureTargetBounty.new();
  13. sendReward(owner, bounty.address, reward);
  14. assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber())
  15. })
  16. it("empties itself when killed", async function(){
  17. let owner = accounts[0];
  18. let reward = web3.toWei(1, "ether");
  19. let bounty = await SecureTargetBounty.new();
  20. sendReward(owner, bounty.address, reward);
  21. assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber())
  22. await bounty.kill();
  23. assert.equal(0, web3.eth.getBalance(bounty.address).toNumber())
  24. })
  25. describe("Against secure contract", function(){
  26. it("checkInvariant returns true", async function(){
  27. let bounty = await SecureTargetBounty.new();
  28. let target = await bounty.createTarget();
  29. let check = await bounty.checkInvariant.call();
  30. assert.isTrue(check);
  31. })
  32. it("cannot claim reward", async function(done){
  33. let owner = accounts[0];
  34. let researcher = accounts[1];
  35. let reward = web3.toWei(1, "ether");
  36. let bounty = await SecureTargetBounty.new();
  37. let event = bounty.TargetCreated({});
  38. event.watch(async function(err, result) {
  39. event.stopWatching();
  40. if (err) { throw err }
  41. var targetAddress = result.args.createdAddress;
  42. sendReward(owner, bounty.address, reward);
  43. assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber())
  44. try {
  45. let tmpClain = await bounty.claim(targetAddress, {from:researcher});
  46. done("should not come here");
  47. } catch(error) {
  48. let reClaimedBounty = await bounty.claimed.call();
  49. assert.isFalse(reClaimedBounty);
  50. try {
  51. let withdraw = await bounty.withdrawPayments({from:researcher});
  52. done("should not come here")
  53. } catch (err) {
  54. assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber())
  55. }
  56. done();
  57. }//end of first try catch
  58. });
  59. bounty.createTarget({from:researcher});
  60. })
  61. })
  62. describe("Against broken contract", function(){
  63. it("checkInvariant returns false", async function(){
  64. let bounty = await InsecureTargetBounty.new();
  65. let target = await bounty.createTarget();
  66. let invarriantCall = await bounty.checkInvariant.call();
  67. assert.isFalse(invarriantCall);
  68. })
  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. event.watch(async function(err, result) {
  76. event.stopWatching();
  77. if (err) { throw err }
  78. let targetAddress = result.args.createdAddress;
  79. sendReward(owner, bounty.address, reward);
  80. assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber());
  81. let bountyClaim = await bounty.claim(targetAddress, {from:researcher});
  82. let claim = await bounty.claimed.call();
  83. assert.isTrue(claim);
  84. let payment = await bounty.withdrawPayments({from:researcher});
  85. assert.equal(0, web3.eth.getBalance(bounty.address).toNumber());
  86. })
  87. bounty.createTarget({from:researcher});
  88. })
  89. })
  90. });