From d91689685501328d2506e215ad06fb8a1b14838f Mon Sep 17 00:00:00 2001 From: Benjamin Cosman Date: Tue, 17 Apr 2018 10:25:04 -0700 Subject: [PATCH] test --- contracts/CanDelegate.sol | 10 +++--- contracts/mocks/OtherMocks.sol | 59 ++++++++++++++++++++++++++++++++++ test/CanDelegate.js | 56 ++++++++++++++++++++++++++++++++ test/CanDelegate.test.js | 21 ------------ 4 files changed, 120 insertions(+), 26 deletions(-) create mode 100644 contracts/mocks/OtherMocks.sol diff --git a/contracts/CanDelegate.sol b/contracts/CanDelegate.sol index b72a4294e..ef954ef41 100644 --- a/contracts/CanDelegate.sol +++ b/contracts/CanDelegate.sol @@ -22,7 +22,7 @@ contract CanDelegate is ModularMintableToken, ModularBurnableToken { if (delegate == address(0)) { super.transferAllArgs(_from, _to, _value); } else { - assert(delegate.delegateTransfer(_to, _value, _from)); + require(delegate.delegateTransfer(_to, _value, _from)); } } @@ -30,7 +30,7 @@ contract CanDelegate is ModularMintableToken, ModularBurnableToken { if (delegate == address(0)) { super.transferFromAllArgs(_from, _to, _value, _spender); } else { - assert(delegate.delegateTransferFrom(_from, _to, _value, _spender)); + require(delegate.delegateTransferFrom(_from, _to, _value, _spender)); } } @@ -46,7 +46,7 @@ contract CanDelegate is ModularMintableToken, ModularBurnableToken { if (delegate == address(0)) { super.approveAllArgs(_spender, _value, _tokenHolder); } else { - assert(delegate.delegateApprove(_spender, _value, _tokenHolder)); + require(delegate.delegateApprove(_spender, _value, _tokenHolder)); } } @@ -70,7 +70,7 @@ contract CanDelegate is ModularMintableToken, ModularBurnableToken { if (delegate == address(0)) { super.increaseApprovalAllArgs(_spender, _addedValue, _tokenHolder); } else { - assert(delegate.delegateIncreaseApproval(_spender, _addedValue, _tokenHolder)); + require(delegate.delegateIncreaseApproval(_spender, _addedValue, _tokenHolder)); } } @@ -78,7 +78,7 @@ contract CanDelegate is ModularMintableToken, ModularBurnableToken { if (delegate == address(0)) { super.decreaseApprovalAllArgs(_spender, _subtractedValue, _tokenHolder); } else { - assert(delegate.delegateDecreaseApproval(_spender, _subtractedValue, _tokenHolder)); + require(delegate.delegateDecreaseApproval(_spender, _subtractedValue, _tokenHolder)); } } diff --git a/contracts/mocks/OtherMocks.sol b/contracts/mocks/OtherMocks.sol new file mode 100644 index 000000000..23e1f5e41 --- /dev/null +++ b/contracts/mocks/OtherMocks.sol @@ -0,0 +1,59 @@ +pragma solidity ^0.4.18; + +import "../DelegateBurnable.sol"; + +contract FailingDelegate is DelegateBurnable { + function delegateTotalSupply() public view returns (uint256) { + return 0; + } + function delegateBalanceOf(address) public view returns (uint256) { + return 0; + } + function delegateTransfer(address, uint256, address) public returns (bool) { + return false; + } + function delegateAllowance(address, address) public view returns (uint256) { + return 0; + } + function delegateTransferFrom(address, address, uint256, address) public returns (bool) { + return false; + } + function delegateApprove(address, uint256, address) public returns (bool) { + return false; + } + function delegateIncreaseApproval(address, uint, address) public returns (bool) { + return false; + } + function delegateDecreaseApproval(address, uint, address) public returns (bool) { + return false; + } + function delegateBurn(address, uint256) public {} +} + +contract SucceedingDelegate is DelegateBurnable { + function delegateTotalSupply() public view returns (uint256) { + return 0; + } + function delegateBalanceOf(address) public view returns (uint256) { + return 0; + } + function delegateTransfer(address, uint256, address) public returns (bool) { + return true; + } + function delegateAllowance(address, address) public view returns (uint256) { + return 0; + } + function delegateTransferFrom(address, address, uint256, address) public returns (bool) { + return true; + } + function delegateApprove(address, uint256, address) public returns (bool) { + return true; + } + function delegateIncreaseApproval(address, uint, address) public returns (bool) { + return true; + } + function delegateDecreaseApproval(address, uint, address) public returns (bool) { + return true; + } + function delegateBurn(address, uint256) public {} +} diff --git a/test/CanDelegate.js b/test/CanDelegate.js index ee38be977..4b48b19f1 100644 --- a/test/CanDelegate.js +++ b/test/CanDelegate.js @@ -3,6 +3,8 @@ import mintableTokenTests from './token/MintableToken'; import burnableTokenTests from './token/BurnableToken'; import standardTokenTests from './token/StandardToken'; import basicTokenTests from './token/BasicToken'; +const FailingDelegate = artifacts.require('FailingDelegate') +const SucceedingDelegate = artifacts.require('SucceedingDelegate') function canDelegateTests([owner, oneHundred, anotherAccount]) { describe('--CanDelegate Tests--', function () { @@ -33,6 +35,60 @@ function canDelegateTests([owner, oneHundred, anotherAccount]) { await assertRevert(this.token.delegateToNewContract("0x1", { from: anotherAccount })) }) }) + + describe('delegating to token that always fails', function () { + beforeEach(async function () { + this.failToken = await FailingDelegate.new({ from: owner }) + await this.token.delegateToNewContract(this.failToken.address, { from: owner }) + }) + + it('transfer', async function () { + await assertRevert(this.token.transfer(oneHundred, 50, { from: anotherAccount })) + }) + + it('transferFrom', async function () { + await assertRevert(this.token.transferFrom(oneHundred, oneHundred, 50, { from: anotherAccount })) + }) + + it('approve', async function () { + await assertRevert(this.token.approve(oneHundred, 50, { from: anotherAccount })) + }) + + it('increaseApproval', async function () { + await assertRevert(this.token.increaseApproval(oneHundred, 50, { from: anotherAccount })) + }) + + it('decreaseApproval', async function () { + await assertRevert(this.token.decreaseApproval(oneHundred, 50, { from: anotherAccount })) + }) + }) + + describe('delegating to token that always succeeds', function () { + beforeEach(async function () { + this.passToken = await SucceedingDelegate.new({ from: owner }) + await this.token.delegateToNewContract(this.passToken.address, { from: owner }) + }) + + it('transfer', async function () { + await this.token.transfer(oneHundred, 50, { from: anotherAccount }) + }) + + it('transferFrom', async function () { + await this.token.transferFrom(oneHundred, oneHundred, 50, { from: anotherAccount }) + }) + + it('approve', async function () { + await this.token.approve(oneHundred, 50, { from: anotherAccount }) + }) + + it('increaseApproval', async function () { + await this.token.increaseApproval(oneHundred, 50, { from: anotherAccount }) + }) + + it('decreaseApproval', async function () { + await this.token.decreaseApproval(oneHundred, 50, { from: anotherAccount }) + }) + }) }) } diff --git a/test/CanDelegate.test.js b/test/CanDelegate.test.js index a17c7d1cd..a0b00fb04 100644 --- a/test/CanDelegate.test.js +++ b/test/CanDelegate.test.js @@ -1,6 +1,5 @@ import canDelegateTests from './CanDelegate' const CanDelegateMock = artifacts.require('CanDelegateMock') -const StandardDelegateMock = artifacts.require('StandardDelegateMock') contract('CanDelegate', function (accounts) { const _ = accounts[0] @@ -10,27 +9,7 @@ contract('CanDelegate', function (accounts) { beforeEach(async function () { this.token = await CanDelegateMock.new(oneHundreds[0], 100, { from: owners[0] }) - // this.token2 = await StandardDelegateMock.new(oneHundreds[1], 100, { from: owners[1] }) }) canDelegateTests([owners[0], oneHundreds[0], anotherAccounts[0]]) - - // describe('chaining three contracts', function () { - // beforeEach(async function () { - // this.token.delegateToNewContract(this.token2.address, { from: owners[0] }) - // this.token2.setDelegatedFrom(this.token.address, { from: owners[1] }) - // }) - - // describe('first contract behaves', function () { - // it('temp', async function () { - // const balance = await this.token.balanceOf(oneHundreds[1]) - // assert.equal(balance, 100) - // await this.token.burn(100, { from: oneHundreds[1] }) - // // await this.token.transfer(anotherAccounts[2], 100, { from: oneHundreds[2] }) - // }) - - // // basicTokenTests([owners[2], oneHundreds[2], anotherAccounts[2]]) - // }) - // }) - }) \ No newline at end of file