You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.5 KiB
92 lines
2.5 KiB
5 years ago
|
pragma solidity >=0.4.22 <0.7.0;
|
||
6 years ago
|
import "remix_tests.sol";
|
||
6 years ago
|
import "./SafeMath.sol";
|
||
|
import "./SafeMathProxy.sol";
|
||
|
|
||
|
contract SafeMathTest {
|
||
|
SafeMathProxy safemathproxy;
|
||
|
|
||
6 years ago
|
function beforeAll() public {
|
||
6 years ago
|
safemathproxy = new SafeMathProxy();
|
||
|
}
|
||
|
|
||
6 years ago
|
function unsafeMultiplicationShouldOverflow() public returns (bool) {
|
||
6 years ago
|
uint256 a = 4;
|
||
|
uint256 b = 2 ** 256 - 1;
|
||
|
return Assert.equal(
|
||
|
a * b,
|
||
|
2 ** 256 - 4,
|
||
|
"unsafe multiplication did not overflow"
|
||
|
);
|
||
|
}
|
||
|
|
||
6 years ago
|
function safeMultiplicationShouldRevert() public returns (bool) {
|
||
6 years ago
|
uint256 a = 4;
|
||
|
uint256 b = 2 ** 256 - 1;
|
||
6 years ago
|
(bool success, bytes memory data) = address(safemathproxy).call.gas(40000).value(0)(abi.encode("mulProxy, [a, b]"));
|
||
6 years ago
|
return Assert.equal(
|
||
6 years ago
|
success,
|
||
6 years ago
|
false,
|
||
|
"safe multiplication did not revert"
|
||
|
);
|
||
|
}
|
||
|
|
||
6 years ago
|
function safeDivisionByZeroShouldRevert() public returns (bool) {
|
||
6 years ago
|
uint256 a = 4;
|
||
|
uint256 b = 0;
|
||
6 years ago
|
(bool success, bytes memory data) = address(safemathproxy).call.gas(40000).value(0)(abi.encode("divProxy, [a, b]"));
|
||
6 years ago
|
return Assert.equal(
|
||
6 years ago
|
success,
|
||
6 years ago
|
false,
|
||
|
"safe division did not revert"
|
||
|
);
|
||
|
}
|
||
|
|
||
6 years ago
|
function unsafeSubtractShouldUnderflow() public returns (bool) {
|
||
6 years ago
|
uint256 a = 0;
|
||
|
uint256 b = a - 1;
|
||
|
return Assert.equal(
|
||
|
b,
|
||
|
2 ** 256 - 1,
|
||
|
"unsafe subtraction did not underflow"
|
||
|
);
|
||
|
}
|
||
|
|
||
6 years ago
|
function safeSubtractShouldRevert() public returns (bool) {
|
||
|
(bool success, bytes memory data) = address(safemathproxy).call.gas(40000).value(0)(abi.encode("subProxy, [0, 1]"));
|
||
6 years ago
|
return Assert.equal(
|
||
6 years ago
|
success,
|
||
6 years ago
|
false,
|
||
|
"safe subtract should revert"
|
||
|
);
|
||
|
}
|
||
|
|
||
6 years ago
|
function unsafeAdditionShouldOverflow() public returns (bool) {
|
||
6 years ago
|
uint256 a = 1;
|
||
|
uint256 b = 2 ** 256 - 1;
|
||
|
return Assert.equal(a + b, 0, "unsafe addition did not overflow");
|
||
|
}
|
||
|
|
||
6 years ago
|
function safeAdditionShouldRevert() public returns (bool) {
|
||
6 years ago
|
uint256 a = 1;
|
||
|
uint256 b = 2 ** 256 - 1;
|
||
6 years ago
|
(bool success, bytes memory data) = address(safemathproxy).call.gas(40000).value(0)(abi.encode("addProxy, [a, b]"));
|
||
6 years ago
|
return Assert.equal(
|
||
6 years ago
|
success,
|
||
6 years ago
|
false,
|
||
|
"safe addition should revert"
|
||
|
);
|
||
|
}
|
||
|
|
||
6 years ago
|
function safeModulusShouldRevert() public returns (bool) {
|
||
6 years ago
|
uint256 a = 1;
|
||
|
uint256 b = 0;
|
||
6 years ago
|
(bool success, bytes memory data) = address(safemathproxy).call.gas(40000).value(0)(abi.encode("modProxy, [a, b]"));
|
||
6 years ago
|
return Assert.equal(
|
||
6 years ago
|
success,
|
||
6 years ago
|
false,
|
||
|
"safe modulus did not revert"
|
||
|
);
|
||
|
}
|
||
|
}
|