Bounty.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. var 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", function(done){
  10. var owner = accounts[0];
  11. var reward = web3.toWei(1, "ether");
  12. SecureTargetBounty.new().
  13. then(function(bounty){
  14. sendReward(owner, bounty.address, reward);
  15. assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber())
  16. }).
  17. then(done);
  18. })
  19. it("empties itself when killed", function(done){
  20. var owner = accounts[0];
  21. var reward = web3.toWei(1, "ether");
  22. var bounty;
  23. SecureTargetBounty.new().
  24. then(function(_bounty){
  25. bounty = _bounty;
  26. sendReward(owner, bounty.address, reward);
  27. assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber())
  28. return bounty.kill()
  29. }).
  30. then(function(){
  31. assert.equal(0, web3.eth.getBalance(bounty.address).toNumber())
  32. }).
  33. then(done);
  34. })
  35. describe("Against secure contract", function(){
  36. it("checkInvariant returns true", function(done){
  37. var bounty;
  38. SecureTargetBounty.new().
  39. then(function(_bounty) {
  40. bounty = _bounty;
  41. return bounty.createTarget();
  42. }).
  43. then(function() {
  44. return bounty.checkInvariant.call()
  45. }).
  46. then(function(result) {
  47. assert.isTrue(result);
  48. }).
  49. then(done);
  50. })
  51. it("cannot claim reward", function(done){
  52. var owner = accounts[0];
  53. var researcher = accounts[1];
  54. var reward = web3.toWei(1, "ether");
  55. SecureTargetBounty.new().
  56. then(function(bounty) {
  57. var event = bounty.TargetCreated({});
  58. event.watch(function(err, result) {
  59. event.stopWatching();
  60. if (err) { throw err }
  61. var targetAddress = result.args.createdAddress;
  62. sendReward(owner, bounty.address, reward);
  63. assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber())
  64. bounty.claim(targetAddress, {from:researcher}).
  65. then(function(){ throw("should not come here")}).
  66. catch(function() {
  67. return bounty.claimed.call();
  68. }).
  69. then(function(result) {
  70. assert.isFalse(result);
  71. bounty.withdrawPayments({from:researcher}).
  72. then(function(){ throw("should not come here")}).
  73. catch(function() {
  74. assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber())
  75. done();
  76. })
  77. })
  78. })
  79. bounty.createTarget({from:researcher});
  80. })
  81. })
  82. })
  83. describe("Against broken contract", function(){
  84. it("checkInvariant returns false", function(done){
  85. var bounty;
  86. InsecureTargetBounty.new().
  87. then(function(_bounty) {
  88. bounty = _bounty;
  89. return bounty.createTarget();
  90. }).
  91. then(function() {
  92. return bounty.checkInvariant.call()
  93. }).
  94. then(function(result) {
  95. assert.isFalse(result);
  96. }).
  97. then(done);
  98. })
  99. it("claims reward", function(done){
  100. var owner = accounts[0];
  101. var researcher = accounts[1];
  102. var reward = web3.toWei(1, "ether");
  103. InsecureTargetBounty.new().
  104. then(function(bounty) {
  105. var event = bounty.TargetCreated({});
  106. event.watch(function(err, result) {
  107. event.stopWatching();
  108. if (err) { throw err }
  109. var targetAddress = result.args.createdAddress;
  110. sendReward(owner, bounty.address, reward);
  111. assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber())
  112. bounty.claim(targetAddress, {from:researcher}).
  113. then(function() {
  114. return bounty.claimed.call();
  115. }).
  116. then(function(result) {
  117. assert.isTrue(result);
  118. return bounty.withdrawPayments({from:researcher})
  119. }).
  120. then(function() {
  121. assert.equal(0, web3.eth.getBalance(bounty.address).toNumber())
  122. }).then(done);
  123. })
  124. bounty.createTarget({from:researcher});
  125. })
  126. })
  127. })
  128. });