This is a short and simple exercise to start sharpening your smart contract fuzzing skills with Foundry.
The scenario is simple. There's a registry contract that allows callers to register by paying a fixed fee in ETH. If the caller sends too little ETH, execution should revert. If the caller sends too much ETH, the contract should give back the change.
Things look good according to the unit test we coded in the Registry.t.sol
contract.
Your goal is to code at least one fuzz test for the Registry
contract. By following the brief specification above, the test must be able to detect a bug in the register
function.
Yes, it gives following error output instead:
`Ran 1 test for test/registry.t.sol:RegistryTest
[FAIL. Reason: Unexpected registry balance: 1000000000000000001 != 1000000000000000000; counterexample: calldata=0xeddb66010000000000000000000000000000000000000000000000000de0b6b3a7640001 args=[1000000000000000001 [1e18]]] testRegisterFuzz(uint256) (runs: 1, μ: 45509, ~: 45509)
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 23.53ms (22.14ms CPU time)
Ran 1 test suite in 24.79ms (23.53ms CPU time): 0 tests passed, 1 failed, 0 skipped (1 total tests)
Failing tests:
Encountered 1 failing test in test/registry.t.sol:RegistryTest
[FAIL. Reason: Unexpected registry balance: 1000000000000000001 != 1000000000000000000; counterexample: calldata=0xeddb66010000000000000000000000000000000000000000000000000de0b6b3a7640001 args=[1000000000000000001 [1e18]]] testRegisterFuzz(uint256) (runs: 1, μ: 45509, ~: 45509)
Encountered a total of 1 failing tests, 0 tests succeeded`
Thanks @tinchoabbate