SignatureBouncer.test.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. const { assertRevert } = require('../helpers/assertRevert');
  2. const { getSignFor } = require('../helpers/sign');
  3. const { shouldBehaveLikePublicRole } = require('../access/roles/PublicRole.behavior');
  4. const SignatureBouncerMock = artifacts.require('SignatureBouncerMock');
  5. const BigNumber = web3.BigNumber;
  6. require('chai')
  7. .use(require('chai-bignumber')(BigNumber))
  8. .should();
  9. const UINT_VALUE = 23;
  10. const BYTES_VALUE = web3.toHex('test');
  11. const INVALID_SIGNATURE = '0xabcd';
  12. contract('SignatureBouncer', function ([_, signer, otherSigner, anyone, authorizedUser, ...otherAccounts]) {
  13. beforeEach(async function () {
  14. this.sigBouncer = await SignatureBouncerMock.new({ from: signer });
  15. this.signFor = getSignFor(this.sigBouncer, signer);
  16. });
  17. describe('signer role', function () {
  18. beforeEach(async function () {
  19. this.contract = this.sigBouncer;
  20. await this.contract.addSigner(otherSigner, { from: signer });
  21. });
  22. shouldBehaveLikePublicRole(signer, otherSigner, otherAccounts, 'signer');
  23. });
  24. describe('modifiers', function () {
  25. context('plain signature', function () {
  26. it('allows valid signature for sender', async function () {
  27. await this.sigBouncer.onlyWithValidSignature(this.signFor(authorizedUser), { from: authorizedUser });
  28. });
  29. it('does not allow invalid signature for sender', async function () {
  30. await assertRevert(
  31. this.sigBouncer.onlyWithValidSignature(INVALID_SIGNATURE, { from: authorizedUser })
  32. );
  33. });
  34. it('does not allow valid signature for other sender', async function () {
  35. await assertRevert(
  36. this.sigBouncer.onlyWithValidSignature(this.signFor(authorizedUser), { from: anyone })
  37. );
  38. });
  39. it('does not allow valid signature for method for sender', async function () {
  40. await assertRevert(
  41. this.sigBouncer.onlyWithValidSignature(this.signFor(authorizedUser, 'onlyWithValidSignature'),
  42. { from: authorizedUser })
  43. );
  44. });
  45. });
  46. context('method signature', function () {
  47. it('allows valid signature with correct method for sender', async function () {
  48. await this.sigBouncer.onlyWithValidSignatureAndMethod(
  49. this.signFor(authorizedUser, 'onlyWithValidSignatureAndMethod'), { from: authorizedUser }
  50. );
  51. });
  52. it('does not allow invalid signature with correct method for sender', async function () {
  53. await assertRevert(
  54. this.sigBouncer.onlyWithValidSignatureAndMethod(INVALID_SIGNATURE, { from: authorizedUser })
  55. );
  56. });
  57. it('does not allow valid signature with correct method for other sender', async function () {
  58. await assertRevert(
  59. this.sigBouncer.onlyWithValidSignatureAndMethod(
  60. this.signFor(authorizedUser, 'onlyWithValidSignatureAndMethod'), { from: anyone }
  61. )
  62. );
  63. });
  64. it('does not allow valid method signature with incorrect method for sender', async function () {
  65. await assertRevert(
  66. this.sigBouncer.onlyWithValidSignatureAndMethod(this.signFor(authorizedUser, 'theWrongMethod'),
  67. { from: authorizedUser })
  68. );
  69. });
  70. it('does not allow valid non-method signature method for sender', async function () {
  71. await assertRevert(
  72. this.sigBouncer.onlyWithValidSignatureAndMethod(this.signFor(authorizedUser), { from: authorizedUser })
  73. );
  74. });
  75. });
  76. context('method and data signature', function () {
  77. it('allows valid signature with correct method and data for sender', async function () {
  78. await this.sigBouncer.onlyWithValidSignatureAndData(UINT_VALUE,
  79. this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]), { from: authorizedUser }
  80. );
  81. });
  82. it('does not allow invalid signature with correct method and data for sender', async function () {
  83. await assertRevert(
  84. this.sigBouncer.onlyWithValidSignatureAndData(UINT_VALUE, INVALID_SIGNATURE, { from: authorizedUser })
  85. );
  86. });
  87. it('does not allow valid signature with correct method and incorrect data for sender', async function () {
  88. await assertRevert(
  89. this.sigBouncer.onlyWithValidSignatureAndData(UINT_VALUE + 10,
  90. this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]),
  91. { from: authorizedUser }
  92. )
  93. );
  94. });
  95. it('does not allow valid signature with correct method and data for other sender', async function () {
  96. await assertRevert(
  97. this.sigBouncer.onlyWithValidSignatureAndData(UINT_VALUE,
  98. this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]),
  99. { from: anyone }
  100. )
  101. );
  102. });
  103. it('does not allow valid non-method signature for sender', async function () {
  104. await assertRevert(
  105. this.sigBouncer.onlyWithValidSignatureAndData(UINT_VALUE,
  106. this.signFor(authorizedUser), { from: authorizedUser }
  107. )
  108. );
  109. });
  110. it('does not allow msg.data shorter than SIGNATURE_SIZE', async function () {
  111. await assertRevert(
  112. this.sigBouncer.tooShortMsgData({ from: authorizedUser })
  113. );
  114. });
  115. });
  116. });
  117. context('signature validation', function () {
  118. context('plain signature', function () {
  119. it('validates valid signature for valid user', async function () {
  120. (await this.sigBouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser))).should.equal(true);
  121. });
  122. it('does not validate invalid signature for valid user', async function () {
  123. (await this.sigBouncer.checkValidSignature(authorizedUser, INVALID_SIGNATURE)).should.equal(false);
  124. });
  125. it('does not validate valid signature for anyone', async function () {
  126. (await this.sigBouncer.checkValidSignature(anyone, this.signFor(authorizedUser))).should.equal(false);
  127. });
  128. it('does not validate valid signature for method for valid user', async function () {
  129. (await this.sigBouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser, 'checkValidSignature'))
  130. ).should.equal(false);
  131. });
  132. });
  133. context('method signature', function () {
  134. it('validates valid signature with correct method for valid user', async function () {
  135. (await this.sigBouncer.checkValidSignatureAndMethod(authorizedUser,
  136. this.signFor(authorizedUser, 'checkValidSignatureAndMethod'))
  137. ).should.equal(true);
  138. });
  139. it('does not validate invalid signature with correct method for valid user', async function () {
  140. (await this.sigBouncer.checkValidSignatureAndMethod(authorizedUser, INVALID_SIGNATURE)).should.equal(false);
  141. });
  142. it('does not validate valid signature with correct method for anyone', async function () {
  143. (await this.sigBouncer.checkValidSignatureAndMethod(anyone,
  144. this.signFor(authorizedUser, 'checkValidSignatureAndMethod'))
  145. ).should.equal(false);
  146. });
  147. it('does not validate valid non-method signature with correct method for valid user', async function () {
  148. (await this.sigBouncer.checkValidSignatureAndMethod(authorizedUser, this.signFor(authorizedUser))
  149. ).should.equal(false);
  150. });
  151. });
  152. context('method and data signature', function () {
  153. it('validates valid signature with correct method and data for valid user', async function () {
  154. (await this.sigBouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE,
  155. this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
  156. ).should.equal(true);
  157. });
  158. it('does not validate invalid signature with correct method and data for valid user', async function () {
  159. (await this.sigBouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE, INVALID_SIGNATURE)
  160. ).should.equal(false);
  161. });
  162. it('does not validate valid signature with correct method and incorrect data for valid user',
  163. async function () {
  164. (await this.sigBouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE + 10,
  165. this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
  166. ).should.equal(false);
  167. }
  168. );
  169. it('does not validate valid signature with correct method and data for anyone', async function () {
  170. (await this.sigBouncer.checkValidSignatureAndData(anyone, BYTES_VALUE, UINT_VALUE,
  171. this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
  172. ).should.equal(false);
  173. });
  174. it('does not validate valid non-method-data signature with correct method and data for valid user',
  175. async function () {
  176. (await this.sigBouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE,
  177. this.signFor(authorizedUser, 'checkValidSignatureAndData'))
  178. ).should.equal(false);
  179. }
  180. );
  181. });
  182. });
  183. });