Missing Revert Reason Tests in Test Functions
Overview
- Severity: Medium
- Confidence: High
- Affected Versions: All
What are Missing Revert Reason Tests?
Missing revert reason tests occur when test functions for smart contracts do not properly verify the specific reasons for function reverts. This can lead to incomplete testing of contract behavior and potentially miss critical validation issues.
Why are Missing Revert Reason Tests Problematic?
Missing revert reason tests in test functions can lead to several issues:
- Failure to detect when functions revert for the wrong reasons
- Inability to verify the correctness of error messages or custom errors
- Incomplete testing of contract's error handling and input validation
- Reduced clarity in understanding contract behavior under invalid conditions
- Potential for overlooking subtle bugs in contract logic
Technical Example of Missing Revert Reason Tests
pragma solidity ^0.8.0;
import "./test.sol";
contract TokenTest is Test {
Token token;
function setUp() public {
token = new Token();
}
function testInvalidTransfer(address to, uint256 amount) public {
vm.expectRevert();
token.transfer(to, amount);
// Missing check for specific revert reason
}
}
In this example, the test function only checks that the transfer reverts, but doesn't verify the specific reason for the revert.
Technical Example with Proper Revert Reason Tests
pragma solidity ^0.8.0;
import "./test.sol";
contract TokenTest is Test {
Token token;
function setUp() public {
token = new Token();
}
function testInvalidTransfer(address to, uint256 amount) public {
vm.expectRevert("Insufficient balance");
token.transfer(to, amount);
}
function testZeroAddressTransfer(uint256 amount) public {
vm.expectRevert("Invalid recipient");
token.transfer(address(0), amount);
}
}
In this improved version, the test functions use `vm.expectRevert` with specific error messages to verify that the contract reverts for the correct reasons under different invalid conditions.