βœ…Fuzz

Fuzz

Setup

Target contract:

Test file:

What is fuzzing?

Fuzzing basically means generating a bunch of random inputs and feeding them into a function. If the input violates the assertion, then we just found a bug.

A usual test case in a Foundry test does not have input parameter. If there is input parameter, then Foundry will consider it as a fuzzing scenario. For example:

bound and assume

Back to the Bit contract. If we want to limit the value of x in a certain range, we can use vm.assume(). This cheatcode is going to set a constrait on x generated by the fuzzer: if the generated input does not satisfy the condition, fuzzer is going to drop it and start another round of fuzzing. For example:

If x <= 0 then it will be dropped.

Another way of setting a range for x is using bound(). It sets lower bound and upper bound for x:

In the end we would feed x into the function and test its result:

Last updated

Was this helpful?