Skip to content

Commit

Permalink
Test reverting static methods of SafeERC20.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Aug 31, 2019
1 parent d520e20 commit 3a50d24
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
47 changes: 47 additions & 0 deletions contracts/test/mocks/lib/token/TokenRevertViewMethods.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Modified from https://github.com/OpenZeppelin/openzeppelin-solidity/blob/a9f910d34f0ab33a1ae5e714f69f9596a02b4d91/contracts/token/ERC20/StandardToken.sol

pragma solidity 0.4.24;

import "../../../../lib/math/SafeMath.sol";


contract TokenRevertViewMethods {
using SafeMath for uint256;
mapping (address => uint256) private balances;
mapping (address => mapping (address => uint256)) private allowed;
uint256 private totalSupply_;
bool private allowTransfer_;

// Allow us to set the inital balance for an account on construction
constructor(address initialAccount, uint256 initialBalance) public {
balances[initialAccount] = initialBalance;
totalSupply_ = initialBalance;
allowTransfer_ = true;
}

function totalSupply() public view returns (uint256) {
require(false, "MOCK_ERROR");
return totalSupply_;
}

/**
* @dev Gets the balance of the specified address.
* @param _owner The address to query the the balance of.
* @return An uint256 representing the amount owned by the passed address.
*/
function balanceOf(address _owner) public view returns (uint256) {
require(false, "MOCK_ERROR");
return balances[_owner];
}

/**
* @dev Function to check the amount of tokens that an owner allowed to a spender.
* @param _owner address The address which owns the funds.
* @param _spender address The address which will spend the funds.
* @return A uint256 specifying the amount of tokens still available for the spender.
*/
function allowance(address _owner, address _spender) public view returns (uint256) {
require(false, "MOCK_ERROR");
return allowed[_owner][_spender];
}
}
32 changes: 32 additions & 0 deletions test/contracts/common/safe_erc20.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
const { getEventArgument } = require('../../helpers/events')
const { assertRevert } = require('../../helpers/assertThrow')
const reverts = require('../../helpers/revertStrings')

// Mocks
const SafeERC20Mock = artifacts.require('SafeERC20Mock')
const TokenMock = artifacts.require('TokenMock')
const TokenReturnFalseMock = artifacts.require('TokenReturnFalseMock')
const TokenReturnMissingMock = artifacts.require('TokenReturnMissingMock')
const TokenRevertViewMethods = artifacts.require('TokenRevertViewMethods')

const assertMockResult = (receipt, result) => assert.equal(getEventArgument(receipt, 'Result', 'result'), result, `result does not match`)

Expand Down Expand Up @@ -128,4 +131,33 @@ contract('SafeERC20', ([owner, receiver]) => {
})
})
}

context('Reverting view methods', () => {
let tokenMock

beforeEach(async () => {
tokenMock = await TokenRevertViewMethods.new(owner, initialBalance)
})

it('allowance', async () => {
await assertRevert(
safeERC20Mock.allowance(tokenMock.address, owner, owner),
reverts.SAFE_ERC_20_ALLOWANCE_REVERTED
)
})

it('balanceOf', async () => {
await assertRevert(
safeERC20Mock.balanceOf(tokenMock.address, owner),
reverts.SAFE_ERC_20_BALANCE_REVERTED
)
})

it('totalSupply', async () => {
await assertRevert(
safeERC20Mock.totalSupply(tokenMock.address),
reverts.SAFE_ERC_20_ALLOWANCE_REVERTED
)
})
})
})

0 comments on commit 3a50d24

Please sign in to comment.