123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- const { assertRevert } = require('../helpers/assertRevert');
- const { getBouncerSigner } = require('../helpers/sign');
- const Bouncer = artifacts.require('SignatureBouncerMock');
- const BigNumber = web3.BigNumber;
- require('chai')
- .use(require('chai-bignumber')(BigNumber))
- .should();
- const UINT_VALUE = 23;
- const BYTES_VALUE = web3.toHex('test');
- const INVALID_SIGNATURE = '0xabcd';
- contract('Bouncer', ([_, owner, anyone, bouncerAddress, authorizedUser]) => {
- beforeEach(async function () {
- this.bouncer = await Bouncer.new({ from: owner });
- this.roleBouncer = await this.bouncer.ROLE_BOUNCER();
- });
- context('management', () => {
- it('has a default owner of self', async function () {
- (await this.bouncer.owner()).should.eq(owner);
- });
- it('allows the owner to add a bouncer', async function () {
- await this.bouncer.addBouncer(bouncerAddress, { from: owner });
- (await this.bouncer.hasRole(bouncerAddress, this.roleBouncer)).should.eq(true);
- });
- it('does not allow adding an invalid address', async function () {
- await assertRevert(
- this.bouncer.addBouncer('0x0', { from: owner })
- );
- });
- it('allows the owner to remove a bouncer', async function () {
- await this.bouncer.addBouncer(bouncerAddress, { from: owner });
- await this.bouncer.removeBouncer(bouncerAddress, { from: owner });
- (await this.bouncer.hasRole(bouncerAddress, this.roleBouncer)).should.eq(false);
- });
- it('does not allow anyone to add a bouncer', async function () {
- await assertRevert(
- this.bouncer.addBouncer(bouncerAddress, { from: anyone })
- );
- });
- it('does not allow anyone to remove a bouncer', async function () {
- await this.bouncer.addBouncer(bouncerAddress, { from: owner });
- await assertRevert(
- this.bouncer.removeBouncer(bouncerAddress, { from: anyone })
- );
- });
- });
- context('with bouncer address', () => {
- beforeEach(async function () {
- await this.bouncer.addBouncer(bouncerAddress, { from: owner });
- this.signFor = getBouncerSigner(this.bouncer, bouncerAddress);
- });
- describe('modifiers', () => {
- context('plain signature', () => {
- it('allows valid signature for sender', async function () {
- await this.bouncer.onlyWithValidSignature(this.signFor(authorizedUser), { from: authorizedUser });
- });
- it('does not allow invalid signature for sender', async function () {
- await assertRevert(
- this.bouncer.onlyWithValidSignature(INVALID_SIGNATURE, { from: authorizedUser })
- );
- });
- it('does not allow valid signature for other sender', async function () {
- await assertRevert(
- this.bouncer.onlyWithValidSignature(this.signFor(authorizedUser), { from: anyone })
- );
- });
- it('does not allow valid signature for method for sender', async function () {
- await assertRevert(
- this.bouncer.onlyWithValidSignature(this.signFor(authorizedUser, 'onlyWithValidSignature'),
- { from: authorizedUser })
- );
- });
- });
- context('method signature', () => {
- it('allows valid signature with correct method for sender', async function () {
- await this.bouncer.onlyWithValidSignatureAndMethod(
- this.signFor(authorizedUser, 'onlyWithValidSignatureAndMethod'), { from: authorizedUser }
- );
- });
- it('does not allow invalid signature with correct method for sender', async function () {
- await assertRevert(
- this.bouncer.onlyWithValidSignatureAndMethod(INVALID_SIGNATURE, { from: authorizedUser })
- );
- });
- it('does not allow valid signature with correct method for other sender', async function () {
- await assertRevert(
- this.bouncer.onlyWithValidSignatureAndMethod(
- this.signFor(authorizedUser, 'onlyWithValidSignatureAndMethod'), { from: anyone }
- )
- );
- });
- it('does not allow valid method signature with incorrect method for sender', async function () {
- await assertRevert(
- this.bouncer.onlyWithValidSignatureAndMethod(this.signFor(authorizedUser, 'theWrongMethod'),
- { from: authorizedUser })
- );
- });
- it('does not allow valid non-method signature method for sender', async function () {
- await assertRevert(
- this.bouncer.onlyWithValidSignatureAndMethod(this.signFor(authorizedUser), { from: authorizedUser })
- );
- });
- });
- context('method and data signature', () => {
- it('allows valid signature with correct method and data for sender', async function () {
- await this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE,
- this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]), { from: authorizedUser }
- );
- });
- it('does not allow invalid signature with correct method and data for sender', async function () {
- await assertRevert(
- this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE, INVALID_SIGNATURE, { from: authorizedUser })
- );
- });
- it('does not allow valid signature with correct method and incorrect data for sender', async function () {
- await assertRevert(
- this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE + 10,
- this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]),
- { from: authorizedUser }
- )
- );
- });
- it('does not allow valid signature with correct method and data for other sender', async function () {
- await assertRevert(
- this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE,
- this.signFor(authorizedUser, 'onlyWithValidSignatureAndData', [UINT_VALUE]),
- { from: anyone }
- )
- );
- });
- it('does not allow valid non-method signature for sender', async function () {
- await assertRevert(
- this.bouncer.onlyWithValidSignatureAndData(UINT_VALUE,
- this.signFor(authorizedUser), { from: authorizedUser }
- )
- );
- });
- });
- });
- context('signature validation', () => {
- context('plain signature', () => {
- it('validates valid signature for valid user', async function () {
- (await this.bouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser))).should.eq(true);
- });
- it('does not validate invalid signature for valid user', async function () {
- (await this.bouncer.checkValidSignature(authorizedUser, INVALID_SIGNATURE)).should.eq(false);
- });
- it('does not validate valid signature for anyone', async function () {
- (await this.bouncer.checkValidSignature(anyone, this.signFor(authorizedUser))).should.eq(false);
- });
- it('does not validate valid signature for method for valid user', async function () {
- (await this.bouncer.checkValidSignature(authorizedUser, this.signFor(authorizedUser, 'checkValidSignature'))
- ).should.eq(false);
- });
- });
- context('method signature', () => {
- it('validates valid signature with correct method for valid user', async function () {
- (await this.bouncer.checkValidSignatureAndMethod(authorizedUser,
- this.signFor(authorizedUser, 'checkValidSignatureAndMethod'))
- ).should.eq(true);
- });
- it('does not validate invalid signature with correct method for valid user', async function () {
- (await this.bouncer.checkValidSignatureAndMethod(authorizedUser, INVALID_SIGNATURE)).should.eq(false);
- });
- it('does not validate valid signature with correct method for anyone', async function () {
- (await this.bouncer.checkValidSignatureAndMethod(anyone,
- this.signFor(authorizedUser, 'checkValidSignatureAndMethod'))
- ).should.eq(false);
- });
- it('does not validate valid non-method signature with correct method for valid user', async function () {
- (await this.bouncer.checkValidSignatureAndMethod(authorizedUser, this.signFor(authorizedUser))
- ).should.eq(false);
- });
- });
- context('method and data signature', () => {
- it('validates valid signature with correct method and data for valid user', async function () {
- (await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE,
- this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
- ).should.eq(true);
- });
- it('does not validate invalid signature with correct method and data for valid user', async function () {
- (await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE, INVALID_SIGNATURE)
- ).should.eq(false);
- });
- it('does not validate valid signature with correct method and incorrect data for valid user',
- async function () {
- (await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE + 10,
- this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
- ).should.eq(false);
- }
- );
- it('does not validate valid signature with correct method and data for anyone', async function () {
- (await this.bouncer.checkValidSignatureAndData(anyone, BYTES_VALUE, UINT_VALUE,
- this.signFor(authorizedUser, 'checkValidSignatureAndData', [authorizedUser, BYTES_VALUE, UINT_VALUE]))
- ).should.eq(false);
- });
- it('does not validate valid non-method-data signature with correct method and data for valid user',
- async function () {
- (await this.bouncer.checkValidSignatureAndData(authorizedUser, BYTES_VALUE, UINT_VALUE,
- this.signFor(authorizedUser, 'checkValidSignatureAndData'))
- ).should.eq(false);
- }
- );
- });
- });
- });
- });
|