Skip to content

Instantly share code, notes, and snippets.

@0mkara
Created October 26, 2018 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0mkara/6011784571f8d5e8fb39475248f86792 to your computer and use it in GitHub Desktop.
Save 0mkara/6011784571f8d5e8fb39475248f86792 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.25+commit.59dbf8f1.js&optimize=false&gist=
pragma solidity ^0.4.24;
import "remix_tests.sol";
import 'https://github.com/OpenZeppelin/zeppelin-solidity/contracts/math/SafeMath.sol';
import "./SafeMathProxy.sol";
contract SafeMathTest {
SafeMathProxy safemathproxy;
function beforeAll() {
safemathproxy = new SafeMathProxy();
}
function unsafeMultiplicationShouldOverflow() public constant returns (bool) {
uint256 a = 4;
uint256 b = 2 ** 256 - 1;
return Assert.equal(
a * b,
2 ** 256 - 4,
"unsafe multiplication did not overflow"
);
}
function safeMultiplicationShouldRevert() public constant returns (bool) {
uint256 a = 4;
uint256 b = 2 ** 256 - 1;
return Assert.equal(
address(safemathproxy).call.gas(40000).value(0)("mulProxy",[a, b]),
false,
"safe multiplication did not revert"
);
}
function safeDivisionByZeroShouldRevert() public constant returns (bool) {
uint256 a = 4;
uint256 b = 0;
return Assert.equal(
address(safemathproxy).call.gas(40000).value(0)("divProxy", [a, b]),
false,
"safe division did not revert"
);
}
function unsafeSubtractShouldUnderflow() public constant returns (bool) {
uint256 a = 0;
uint256 b = a - 1;
return Assert.equal(
b,
2 ** 256 - 1,
"unsafe subtraction did not underflow"
);
}
function safeSubtractShouldRevert() public constant returns (bool) {
return Assert.equal(
address(safemathproxy).call.gas(40000).value(0)("subProxy", [0, 1]),
false,
"safe subtract should revert"
);
}
function unsafeAdditionShouldOverflow() public constant returns (bool) {
uint256 a = 1;
uint256 b = 2 ** 256 - 1;
return Assert.equal(a + b, 0, "unsafe addition did not overflow");
}
function safeAdditionShouldRevert() public constant returns (bool) {
uint256 a = 1;
uint256 b = 2 ** 256 - 1;
return Assert.equal(
address(safemathproxy).call.gas(40000).value(0)("addProxy", [a, b]),
false,
"safe addition should revert"
);
}
function safeModulusShouldRevert() public constant returns (bool) {
uint256 a = 1;
uint256 b = 0;
return Assert.equal(
address(safemathproxy).call.gas(40000).value(0)("modProxy", [a, b]),
false,
"safe modulus did not revert"
);
}
}
pragma solidity ^0.4.24;
import 'https://github.com/OpenZeppelin/zeppelin-solidity/contracts/math/SafeMath.sol';
/*
Using a proxy contract here allows revert-causing functions that contain
require() to return false rather than halt execution
https://truffleframework.com/tutorials/testing-for-throws-in-solidity-tests
*/
contract SafeMathProxy {
using SafeMath for uint;
function divProxy(uint256 a, uint256 b) returns (uint256) {
return a.div(b);
}
function mulProxy(uint256 a, uint256 b) returns (uint256) {
return a.mul(b);
}
function subProxy(uint256 a, uint256 b) returns (uint256) {
return a.sub(b);
}
function addProxy(uint256 a, uint256 b) returns (uint256) {
return a.add(b);
}
function modProxy(uint256 a, uint256 b) returns (uint256) {
return a.mod(b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment