Provide fuzz test utilities for basic types.
More...
Namespaces |
namespace | bslim |
Detailed Description
- Outline
-
-
- Purpose:
- Provide fuzz test utilities for basic types.
-
- Classes:
-
- See also:
- Component bslim_fuzzdataview
-
- Description:
- This component provides a namespace,
bslim::FuzzUtil
, containing functions that create fundamental and standard library types from fuzz data provided by a fuzz harness (e.g., libFuzzer
).
- See {http://bburl/BDEFuzzTesting} for details on how to build and run with fuzz testing enabled.
-
- Usage:
- This section illustrates intended use of this component.
-
- Example 1: Consuming Integers in a Range to Pass to an Interface:
- Suppose we wish to fuzz test a function with preconditions.
- First, we define the
TradingInterfaceUnderTest
struct
: struct TradingInterfaceUnderTest {
static int numEarningsAnnouncements(int year, int month)
{
BSLS_ASSERT(1950 < year && year < 2030);
BSLS_ASSERT( 1 <= month && month <= 12);
if (2020 < year && 6 < month) {
return 11;
}
return 6;
}
};
Then, we need a block of raw bytes. This would normally come from a fuzz harness (e.g., the LLVMFuzzerTestOneInput
entry point function from libFuzzer
). Since libFuzzer
is not available here, we initialize a myFuzzData
array that we will use instead. const bsl::uint8_t myFuzzData[] = {0x43, 0x19, 0x0D, 0x44, 0x37, 0x0D,
0x38, 0x5E, 0x9B, 0xAA, 0xF3, 0xDA};
Next, we create a FuzzDataView
to wrap the raw bytes. Now, we pass this FuzzDataView
to FuzzUtil
to generate values within the permissible range of the function under test: int month = bslim::FuzzUtil::consumeNumberInRange<int>(&fdv, 1, 12);
int year = bslim::FuzzUtil::consumeNumberInRange<int>(&fdv, 1951, 2029);
assert( 1 <= month && month <= 12);
assert(1951 <= year && year <= 2029);
Finally, we can use these int
values to pass to a function that returns the number of earnings announcements scheduled in a given month. int numEarnings =
TradingInterfaceUnderTest::numEarningsAnnouncements(year, month);
(void) numEarnings;