SignatureBouncer.test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. const { assertRevert } = require('../helpers/assertRevert');
  2. const { getBouncerSigner } = require('../helpers/sign');
  3. const Bouncer = artifacts.require('SignatureBouncerMock');
  4. const BigNumber = web3.BigNumber;
  5. require('chai')
  6. .use(require('chai-bignumber')(BigNumber))
  7. .should();
  8. const UINT_VALUE = 23;
  9. const BYTES_VALUE = web3.toHex('test');
  10. const INVALID_SIGNATURE = '0xabcd';
  11. contract('Bouncer', ([_, owner, anyone, bouncerAddress, authorizedUser]) => {
  12. beforeEach(async function () {
  13. this.bouncer = await Bouncer.new({ from: owner });
  14. this.roleBouncer = await this.bouncer.ROLE_BOUNCER();
  15. });
  16. context('management', () => {
  17. it('has a default owner of self', async function () {
  18. (await this.bouncer.owner()).should.eq(owner);
  19. });
  20. it('allows the owner to add a bouncer', async function () {
  21. await this.bouncer.addBouncer(bouncerAddress, { from: owner });
  22. (await this.bouncer.hasRole(bouncerAddress, this.roleBouncer)).should.eq(true);
  23. });
  24. it('does not allow adding an invalid address', async function () {
  25. await assertRevert(
  26. this.bouncer.addBouncer('0x0', { from: owner })
  27. );
  28. });
  29. it('allows the owner to remove a bouncer', async function () {
  30. await this.bouncer.addBouncer(bouncerAddress, { from: owner });
  31. await this.bouncer.removeBouncer(bouncerAddress, { from: owner });
  32. (await this.bouncer.hasRole(bouncerAddress, this.roleBouncer)).should.eq(false);
  33. });
  34. it('does not allow anyone to add a bouncer', async function () {
  35. await assertRevert(
  36. this.bouncer.addBouncer(bouncerAddress, { from: anyone })
  37. );
  38. });
  39. it('does not allow anyone to remove a bouncer', async function () {
  40. await this.bouncer.addBouncer(bouncerAddress, { from: owner });
  41. await assertRevert(
  42. this.bouncer.removeBouncer(bouncerAddress, { from: anyone })
  43. );
  44. });
  45. });
  46. context('with bouncer address', () => {
  47. beforeEach(async function () {
  48. await this.bouncer.addBouncer(bouncerAddress, { from: owner });
  49. this.signFor = getBouncerSigner(this.bouncer, bouncerAddress);
  50. });
  51. describe('modifiers', () => {
  52. context('plain signature', () => {
  53. it('allows valid signature for sender', async function () {
  54. await this.bouncer.onlyWithValidSignature(this.signFor(authorizedUser), { from: authorizedUser });
  55. });
  56. it('does not allow invalid signature for sender', async function () {
  57. await assertRevert(
  58. this.bouncer.onlyWithValidSignature(INVALID_SIGNATURE, { from: authorizedUser })
  59. );
  60. });
  61. it('does not allow valid signature for other sender', async function () {
  62. await assertRevert(
  63. this.bouncer.onlyWithValidSignature(this.signFor(authorizedUser), { from: anyone })
  64. );
  65. });
  66. it('does not allow valid signature for method for sender', async function () {
  67. await assertRevert(
  68. this.bouncer.onlyWithValidSignature(this.signFor(authorizedUser, 'onlyWithValidSignature'),
  69. { from: authorizedUser })
  70. );
  71. });
  72. });
  73. context('method signature', () => {
  74. it('allows valid signature with correct method for sender', async function () {
  75. await this.bouncer.onlyWithValidSignatureAndMethod(
  76. this.signFor(authorizedUser, 'onlyWithValidSignatureAndMethod'), { from: authorizedUser }
  77. );
  78. });
  79. it('does not allow invalid signature with correct method for sender', async function () {
  80. await assertRevert(
  81. this.bouncer.onlyWithValidSignatureAndMethod(INVALID_SIGNATURE, { from: authorizedUser })
  82. );
  83. });
  84. it('does not allow valid signature with correct method for other sender', async function () {
  85. await assertRevert(
  86. this.bouncer.onlyWithValidSignatureAndMethod(
  87. this.signFor(authorizedUser, 'onlyWithValidSignatureAndMethod'), { from: anyone }
  88. )
  89. );
  90. });
  91. it('does not allow valid method signature with incorrect method for sender', async function () {
  92. await assertRevert(
  93. this.bouncer.onlyWithValidSignatureAndMethod(this.signFor(authorizedUser, 'theWrongMethod'),
  94. { from: authorizedUser })
  95. );
  96. });
  97. it('does not allow valid non-method signature method for sender', async function () {
  98. await assertRevert(
  99. this.bouncer.onlyWithValidSignatureAndMethod(this.signFor(authorizedUser), { from: authorizedUser })
  100. );
  101. });
  102. });
  103. context('method and data signature', () => {
  104. it('allows valid signature with correct method and data for sender', async function () {
  105. await this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE,
  106. this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]), { from: authorizedUser }
  107. );
  108. });
  109. it('does not allow invalid signature with correct method and data for sender', async function () {
  110. await assertRevert(
  111. this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE, INVALID_SIGNATURE, { from: authorizedUser })
  112. );
  113. });
  114. it('does not allow valid signature with correct method and incorrect data for sender', async function () {
  115. await assertRevert(
  116. this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE + 10,
  117. this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]),
  118. { from: authorizedUser }
  119. )
  120. );
  121. });
  122. it('does not allow valid signature with correct method and data for other sender', async function () {
  123. await assertRevert(
  124. this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE,
  125. this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]),
  126. { from: anyone }
  127. )
  128. );
  129. });
  130. it('does not allow valid non-method signature for sender', async function () {
  131. await assertRevert(
  132. this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE,
  133. this.signFor(authorizedUser), { from: authorizedUser }
  134. )
  135. );
  136. });
  137. });
  138. });
  139. context('signature validation', () => {
  140. context('plain signature', () => {
  141. it('validates valid signature for valid user', async function () {
  142. (await this.bouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser))).should.eq(true);
  143. });
  144. it('does not validate invalid signature for valid user', async function () {
  145. (await this.bouncer.checkValidSignature(authorizedUser, INVALID_SIGNATURE)).should.eq(false);
  146. });
  147. it('does not validate valid signature for anyone', async function () {
  148. (await this.bouncer.checkValidSignature(anyone, this.signFor(authorizedUser))).should.eq(false);
  149. });
  150. it('does not validate valid signature for method for valid user', async function () {
  151. (await this.bouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser, 'checkValidSignature'))
  152. ).should.eq(false);
  153. });
  154. });
  155. context('method signature', () => {
  156. it('validates valid signature with correct method for valid user', async function () {
  157. (await this.bouncer.checkValidSignatureAndMethod(authorizedUser,
  158. this.signFor(authorizedUser, 'checkValidSignatureAndMethod'))
  159. ).should.eq(true);
  160. });
  161. it('does not validate invalid signature with correct method for valid user', async function () {
  162. (await this.bouncer.checkValidSignatureAndMethod(authorizedUser, INVALID_SIGNATURE)).should.eq(false);
  163. });
  164. it('does not validate valid signature with correct method for anyone', async function () {
  165. (await this.bouncer.checkValidSignatureAndMethod(anyone,
  166. this.signFor(authorizedUser, 'checkValidSignatureAndMethod'))
  167. ).should.eq(false);
  168. });
  169. it('does not validate valid non-method signature with correct method for valid user', async function () {
  170. (await this.bouncer.checkValidSignatureAndMethod(authorizedUser, this.signFor(authorizedUser))
  171. ).should.eq(false);
  172. });
  173. });
  174. context('method and data signature', () => {
  175. it('validates valid signature with correct method and data for valid user', async function () {
  176. (await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE,
  177. this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
  178. ).should.eq(true);
  179. });
  180. it('does not validate invalid signature with correct method and data for valid user', async function () {
  181. (await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE, INVALID_SIGNATURE)
  182. ).should.eq(false);
  183. });
  184. it('does not validate valid signature with correct method and incorrect data for valid user',
  185. async function () {
  186. (await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE + 10,
  187. this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
  188. ).should.eq(false);
  189. }
  190. );
  191. it('does not validate valid signature with correct method and data for anyone', async function () {
  192. (await this.bouncer.checkValidSignatureAndData(anyone, BYTES_VALUE, UINT_VALUE,
  193. this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
  194. ).should.eq(false);
  195. });
  196. it('does not validate valid non-method-data signature with correct method and data for valid user',
  197. async function () {
  198. (await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE,
  199. this.signFor(authorizedUser, 'checkValidSignatureAndData'))
  200. ).should.eq(false);
  201. }
  202. );
  203. });
  204. });
  205. });
  206. });