Bounty.js 3.6 KB

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