SignatureBouncer.test.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. });
  111. });
  112. context('signature validation', function () {
  113. context('plain signature', function () {
  114. it('validates valid signature for valid user', async function () {
  115. (await this.sigBouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser))).should.equal(true);
  116. });
  117. it('does not validate invalid signature for valid user', async function () {
  118. (await this.sigBouncer.checkValidSignature(authorizedUser, INVALID_SIGNATURE)).should.equal(false);
  119. });
  120. it('does not validate valid signature for anyone', async function () {
  121. (await this.sigBouncer.checkValidSignature(anyone, this.signFor(authorizedUser))).should.equal(false);
  122. });
  123. it('does not validate valid signature for method for valid user', async function () {
  124. (await this.sigBouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser, 'checkValidSignature'))
  125. ).should.equal(false);
  126. });
  127. });
  128. context('method signature', function () {
  129. it('validates valid signature with correct method for valid user', async function () {
  130. (await this.sigBouncer.checkValidSignatureAndMethod(authorizedUser,
  131. this.signFor(authorizedUser, 'checkValidSignatureAndMethod'))
  132. ).should.equal(true);
  133. });
  134. it('does not validate invalid signature with correct method for valid user', async function () {
  135. (await this.sigBouncer.checkValidSignatureAndMethod(authorizedUser, INVALID_SIGNATURE)).should.equal(false);
  136. });
  137. it('does not validate valid signature with correct method for anyone', async function () {
  138. (await this.sigBouncer.checkValidSignatureAndMethod(anyone,
  139. this.signFor(authorizedUser, 'checkValidSignatureAndMethod'))
  140. ).should.equal(false);
  141. });
  142. it('does not validate valid non-method signature with correct method for valid user', async function () {
  143. (await this.sigBouncer.checkValidSignatureAndMethod(authorizedUser, this.signFor(authorizedUser))
  144. ).should.equal(false);
  145. });
  146. });
  147. context('method and data signature', function () {
  148. it('validates valid signature with correct method and data for valid user', async function () {
  149. (await this.sigBouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE,
  150. this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
  151. ).should.equal(true);
  152. });
  153. it('does not validate invalid signature with correct method and data for valid user', async function () {
  154. (await this.sigBouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE, INVALID_SIGNATURE)
  155. ).should.equal(false);
  156. });
  157. it('does not validate valid signature with correct method and incorrect data for valid user',
  158. async function () {
  159. (await this.sigBouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE + 10,
  160. this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
  161. ).should.equal(false);
  162. }
  163. );
  164. it('does not validate valid signature with correct method and data for anyone', async function () {
  165. (await this.sigBouncer.checkValidSignatureAndData(anyone, BYTES_VALUE, UINT_VALUE,
  166. this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
  167. ).should.equal(false);
  168. });
  169. it('does not validate valid non-method-data signature with correct method and data for valid user',
  170. async function () {
  171. (await this.sigBouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE,
  172. this.signFor(authorizedUser, 'checkValidSignatureAndData'))
  173. ).should.equal(false);
  174. }
  175. );
  176. });
  177. });
  178. });