Bounty.js 5.3 KB

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