blob: ea6774e5a5cda654b99f52565cde00eb9268daee [file] [log] [blame]
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "gtest/gtest.h"
#include <cstdint>
#include <cstdlib>
#include <fuzzer/FuzzedDataProvider.h>
// The test is intentionally extensive, as behavior of |FuzzedDataProvider| must
// not be broken, given than many fuzz targets depend on it. Changing the
// behavior might invalidate existing corpora and make the fuzz targets using
// |FuzzedDataProvider| to lose code coverage accumulated over time.
/* A random 1KB buffer generated by:
$ python -c "import os; print ',\n'.join([', '.join(['0x%02X' % ord(i) for i \
in list(os.urandom(8))]) for _ in xrange(128)])"
*/
const uint8_t Data[] = {
0x8A, 0x19, 0x0D, 0x44, 0x37, 0x0D, 0x38, 0x5E, 0x9B, 0xAA, 0xF3, 0xDA,
0xAA, 0x88, 0xF2, 0x9B, 0x6C, 0xBA, 0xBE, 0xB1, 0xF2, 0xCF, 0x13, 0xB8,
0xAC, 0x1A, 0x7F, 0x1C, 0xC9, 0x90, 0xD0, 0xD9, 0x5C, 0x42, 0xB3, 0xFD,
0xE3, 0x05, 0xA4, 0x03, 0x37, 0x49, 0x50, 0x4B, 0xBC, 0x39, 0xA2, 0x09,
0x6C, 0x2F, 0xAF, 0xD1, 0xB5, 0x47, 0xBF, 0x92, 0xBD, 0x79, 0xE5, 0xC5,
0x6E, 0x51, 0xA4, 0xED, 0xE9, 0xBD, 0x40, 0x4A, 0xFC, 0x25, 0x7A, 0x27,
0xC8, 0x92, 0xF7, 0x30, 0xDE, 0x40, 0x66, 0x66, 0xE8, 0x5F, 0x65, 0x39,
0x7E, 0x9E, 0x80, 0x2B, 0x01, 0x71, 0x2A, 0xFF, 0xD3, 0x0A, 0xAC, 0x6E,
0x49, 0x32, 0x79, 0x10, 0x6A, 0x6F, 0x97, 0x96, 0x70, 0x7E, 0x50, 0x65,
0xC9, 0x1D, 0xBD, 0x4E, 0x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DataProv(Data, sizeof(Data));
EXPECT_EQ(std::vector<unsigned char>(1, 0x8A),
DataProv.ConsumeBytes<unsigned char>(1));
EXPECT_EQ(std::vector<uint8_t>(
{0x19, 0x0D, 0x44, 0x37, 0x0D, 0x38, 0x5E, 0x9B, 0xAA, 0xF3}),
DataProv.ConsumeBytes<uint8_t>(10));
std::vector<unsigned char> UChars = DataProv.ConsumeBytes<unsigned char>(24);
EXPECT_EQ(std::vector<unsigned char>({0xDA, 0xAA, 0x88, 0xF2, 0x9B, 0x6C,
0xBA, 0xBE, 0xB1, 0xF2, 0xCF, 0x13,
0xB8, 0xAC, 0x1A, 0x7F, 0x1C, 0xC9,
0x90, 0xD0, 0xD9, 0x5C, 0x42, 0xB3}),
UChars);
EXPECT_EQ(std::vector<signed char>(Data + 1 + 10 + 24, Data + sizeof(Data)),
DataProv.ConsumeBytes<signed char>(31337));
}
TEST(FuzzedDataProvider, ConsumeBytesWithTerminator) {
FuzzedDataProvider DataProv(Data, sizeof(Data));
EXPECT_EQ(std::vector<unsigned char>({0x8A, 0x00}),
DataProv.ConsumeBytesWithTerminator<unsigned char>(1));
EXPECT_EQ(std::vector<uint8_t>({0x19, 0x0D, 0x44, 0x37, 0x0D, 0x38, 0x5E,
0x9B, 0xAA, 0xF3, 111}),
DataProv.ConsumeBytesWithTerminator<uint8_t>(10, 111));
std::vector<unsigned char> UChars =
DataProv.ConsumeBytesWithTerminator<unsigned char>(24);
EXPECT_EQ(std::vector<unsigned char>(
{0xDA, 0xAA, 0x88, 0xF2, 0x9B, 0x6C, 0xBA, 0xBE, 0xB1,
0xF2, 0xCF, 0x13, 0xB8, 0xAC, 0x1A, 0x7F, 0x1C, 0xC9,
0x90, 0xD0, 0xD9, 0x5C, 0x42, 0xB3, 0x00}),
UChars);
std::vector<signed char> Expected(Data + 1 + 10 + 24, Data + sizeof(Data));
Expected.push_back(65);
EXPECT_EQ(Expected,
DataProv.ConsumeBytesWithTerminator<signed char>(31337, 65));
}
TEST(FuzzedDataProvider, ConsumeBytesAsString) {
FuzzedDataProvider DataProv(Data, sizeof(Data));
EXPECT_EQ(std::string("\x8A\x19\x0D\x44\x37\x0D\x38\x5E\x9B\xAA\xF3\xDA"),
DataProv.ConsumeBytesAsString(12));
EXPECT_EQ(std::string(Data + 12, Data + sizeof(Data)),
DataProv.ConsumeBytesAsString(31337));
}
TEST(FuzzedDataProvider, ConsumeIntegralInRange) {
FuzzedDataProvider DataProv(Data, sizeof(Data));
EXPECT_EQ(int32_t(21), DataProv.ConsumeIntegralInRange<int32_t>(10, 30));
EXPECT_EQ(int32_t(1337),
DataProv.ConsumeIntegralInRange<int32_t>(1337, 1337));
EXPECT_EQ(int8_t(-59), DataProv.ConsumeIntegralInRange<int8_t>(-100, 100));
EXPECT_EQ(uint16_t(15823),
DataProv.ConsumeIntegralInRange<uint16_t>(0, 65535));
EXPECT_EQ((signed char)(-101),
DataProv.ConsumeIntegralInRange<signed char>(-123, 123));
EXPECT_EQ(int64_t(-53253077544), DataProv.ConsumeIntegralInRange<int64_t>(
-99999999999, 99999999999));
// Exhaust the buffer.
auto String = DataProv.ConsumeBytesAsString(31337);
EXPECT_EQ(size_t(1014), String.length());
EXPECT_EQ(uint64_t(123456789),
DataProv.ConsumeIntegralInRange<uint64_t>(123456789, 987654321));
}
TEST(FuzzedDataProvider, ConsumeRandomLengthString) {
FuzzedDataProvider DataProv(Data, sizeof(Data));
EXPECT_EQ(
std::string(
"\x8A\x19\x0D\x44\x37\x0D\x38\x5E\x9B\xAA\xF3\xDA\xAA\x88\xF2\x9B\x6C"
"\xBA\xBE\xB1\xF2\xCF\x13\xB8\xAC\x1A\x7F\x1C\xC9\x90\xD0\xD9"),
DataProv.ConsumeRandomLengthString(1337));
EXPECT_EQ(std::string(
"\xB3\xFD\xE3\x05\xA4\x03\x37\x49\x50\x4B\xBC\x39\xA2\x09\x6C"
"\x2F\xAF\xD1\xB5\x47\xBF\x92\xBD\x79\xE5\xC5\x6E\x51\xA4\xED"
"\xE9\xBD\x40\x4A\xFC\x25\x7A\x27\xC8\x92\xF7\x30\xDE\x40\x66"
"\x66\xE8\x5F\x65\x39\x7E\x9E\x80\x2B\x01\x71\x2A\xFF\xD3\x0A"
"\xAC\x6E\x49\x32\x79\x10\x6A\x6F\x97\x96\x70\x7E\x50\x65\xC9"
"\x1D\xBD\x4E\x17\x04\x1E\xBA\x26\xAC\x1F\xE3\x37\x1C\x15\x43"
"\x60\x41\x2A\x7C\xCA\x70\xCE\xAB\x20\x24\xF8\xD9\x1F\x14\x7C"),
DataProv.ConsumeRandomLengthString(31337));
size_t Offset = 141;
EXPECT_EQ(std::string(Data + Offset, Data + Offset + 5),
DataProv.ConsumeRandomLengthString(5));
Offset += 5;
EXPECT_EQ(std::string(Data + Offset, Data + Offset + 2),
DataProv.ConsumeRandomLengthString(2));
Offset += 2;
// Call the overloaded method without arguments (uses max length available).
EXPECT_EQ(std::string(Data + Offset, Data + Offset + 664),
DataProv.ConsumeRandomLengthString());
Offset += 664 + 2; // +2 because of '\' character followed by any other byte.
EXPECT_EQ(std::string(Data + Offset, Data + Offset + 92),
DataProv.ConsumeRandomLengthString());
Offset += 92 + 2;
// Exhaust the buffer.
auto String = DataProv.ConsumeBytesAsString(31337);
EXPECT_EQ(size_t(116), String.length());
EXPECT_EQ(std::string(), DataProv.ConsumeRandomLengthString(1));
}
TEST(FuzzedDataProvider, ConsumeRemainingBytes) {
{
FuzzedDataProvider DataProv(Data, sizeof(Data));
EXPECT_EQ(std::vector<uint8_t>(Data, Data + sizeof(Data)),
DataProv.ConsumeRemainingBytes<uint8_t>());
EXPECT_EQ(std::vector<uint8_t>(),
DataProv.ConsumeRemainingBytes<uint8_t>());
}
{
FuzzedDataProvider DataProv(Data, sizeof(Data));
EXPECT_EQ(std::vector<uint8_t>(Data, Data + 123),
DataProv.ConsumeBytes<uint8_t>(123));
EXPECT_EQ(std::vector<char>(Data + 123, Data + sizeof(Data)),
DataProv.ConsumeRemainingBytes<char>());
}
}
TEST(FuzzedDataProvider, ConsumeRemainingBytesAsString) {
{
FuzzedDataProvider DataProv(Data, sizeof(Data));
EXPECT_EQ(std::string(Data, Data + sizeof(Data)),
DataProv.ConsumeRemainingBytesAsString());
EXPECT_EQ(std::string(""), DataProv.ConsumeRemainingBytesAsString());
}
{
FuzzedDataProvider DataProv(Data, sizeof(Data));
EXPECT_EQ(std::vector<uint8_t>(Data, Data + 123),
DataProv.ConsumeBytes<uint8_t>(123));
EXPECT_EQ(std::string(Data + 123, Data + sizeof(Data)),
DataProv.ConsumeRemainingBytesAsString());
}
}
TEST(FuzzedDataProvider, ConsumeIntegral) {
FuzzedDataProvider DataProv(Data, sizeof(Data));
EXPECT_EQ(int32_t(-903266865), DataProv.ConsumeIntegral<int32_t>());
EXPECT_EQ(uint32_t(372863811), DataProv.ConsumeIntegral<uint32_t>());
EXPECT_EQ(uint8_t(61), DataProv.ConsumeIntegral<uint8_t>());
EXPECT_EQ(int16_t(22100), DataProv.ConsumeIntegral<int16_t>());
EXPECT_EQ(uint64_t(18252263806144500217u),
DataProv.ConsumeIntegral<uint64_t>());
// Exhaust the buffer.
auto String = DataProv.ConsumeBytesAsString(31337);
EXPECT_EQ(size_t(1005), String.length());
EXPECT_EQ(std::numeric_limits<int64_t>::min(),
DataProv.ConsumeIntegral<int64_t>());
}
TEST(FuzzedDataProvider, ConsumeBool) {
FuzzedDataProvider DataProv(Data, sizeof(Data));
EXPECT_EQ(false, DataProv.ConsumeBool());
EXPECT_EQ(true, DataProv.ConsumeBool());
EXPECT_EQ(true, DataProv.ConsumeBool());
EXPECT_EQ(true, DataProv.ConsumeBool());
EXPECT_EQ(false, DataProv.ConsumeBool());
EXPECT_EQ(true, DataProv.ConsumeBool());
EXPECT_EQ(true, DataProv.ConsumeBool());
EXPECT_EQ(true, DataProv.ConsumeBool());
EXPECT_EQ(true, DataProv.ConsumeBool());
EXPECT_EQ(false, DataProv.ConsumeBool());
// Exhaust the buffer.
auto String = DataProv.ConsumeBytesAsString(31337);
EXPECT_EQ(size_t(1014), String.length());
EXPECT_EQ(false, DataProv.ConsumeBool());
}
TEST(FuzzedDataProvider, PickValueInStdArray) {
FuzzedDataProvider DataProv(Data, sizeof(Data));
const std::array<int, 5> Array = {1, 2, 3, 4, 5};
EXPECT_EQ(5, DataProv.PickValueInArray(Array));
EXPECT_EQ(2, DataProv.PickValueInArray(Array));
EXPECT_EQ(2, DataProv.PickValueInArray(Array));
EXPECT_EQ(3, DataProv.PickValueInArray(Array));
EXPECT_EQ(3, DataProv.PickValueInArray(Array));
EXPECT_EQ(3, DataProv.PickValueInArray(Array));
EXPECT_EQ(1, DataProv.PickValueInArray(Array));
EXPECT_EQ(3, DataProv.PickValueInArray(Array));
EXPECT_EQ(2, DataProv.PickValueInArray(Array));
}
TEST(FuzzedDataProvider, PickValueInArray) {
FuzzedDataProvider DataProv(Data, sizeof(Data));
const int Array[] = {1, 2, 3, 4, 5};
EXPECT_EQ(5, DataProv.PickValueInArray(Array));
EXPECT_EQ(2, DataProv.PickValueInArray(Array));
EXPECT_EQ(2, DataProv.PickValueInArray(Array));
EXPECT_EQ(3, DataProv.PickValueInArray(Array));
EXPECT_EQ(3, DataProv.PickValueInArray(Array));
EXPECT_EQ(3, DataProv.PickValueInArray(Array));
EXPECT_EQ(1, DataProv.PickValueInArray(Array));
EXPECT_EQ(3, DataProv.PickValueInArray(Array));
EXPECT_EQ(2, DataProv.PickValueInArray(Array));
EXPECT_EQ(uint8_t(0x9D), DataProv.PickValueInArray(Data));
EXPECT_EQ(uint8_t(0xBA), DataProv.PickValueInArray(Data));
EXPECT_EQ(uint8_t(0x69), DataProv.PickValueInArray(Data));
EXPECT_EQ(uint8_t(0xD6), DataProv.PickValueInArray(Data));
EXPECT_EQ(uint32_t(777), DataProv.PickValueInArray<uint32_t>({1337, 777}));
EXPECT_EQ(uint32_t(777), DataProv.PickValueInArray<uint32_t>({1337, 777}));
EXPECT_EQ(uint64_t(1337), DataProv.PickValueInArray<uint64_t>({1337, 777}));
EXPECT_EQ(size_t(777), DataProv.PickValueInArray<size_t>({1337, 777}));
EXPECT_EQ(int16_t(1337), DataProv.PickValueInArray<int16_t>({1337, 777}));
EXPECT_EQ(int32_t(777), DataProv.PickValueInArray<int32_t>({1337, 777}));
EXPECT_EQ(int64_t(777), DataProv.PickValueInArray<int64_t>({1337, 777}));
// Exhaust the buffer.
auto String = DataProv.ConsumeBytesAsString(31337);
EXPECT_EQ(size_t(1000), String.length());
EXPECT_EQ(uint8_t(0x8A), DataProv.PickValueInArray(Data));
}
TEST(FuzzedDataProvider, ConsumeEnum) {
FuzzedDataProvider DataProv(Data, sizeof(Data));
enum class Enum {
Zero,
One,
Two,
Three,
Four,
Five,
Six,
Seven,
kMaxValue = Seven
};
EXPECT_EQ(Enum::Two, DataProv.ConsumeEnum<Enum>());
EXPECT_EQ(Enum::One, DataProv.ConsumeEnum<Enum>());
EXPECT_EQ(Enum::Five, DataProv.ConsumeEnum<Enum>());
EXPECT_EQ(Enum::Seven, DataProv.ConsumeEnum<Enum>());
EXPECT_EQ(Enum::Six, DataProv.ConsumeEnum<Enum>());
EXPECT_EQ(Enum::One, DataProv.ConsumeEnum<Enum>());
EXPECT_EQ(Enum::Three, DataProv.ConsumeEnum<Enum>());
EXPECT_EQ(Enum::Three, DataProv.ConsumeEnum<Enum>());
EXPECT_EQ(Enum::Five, DataProv.ConsumeEnum<Enum>());
EXPECT_EQ(Enum::Six, DataProv.ConsumeEnum<Enum>());
// Exhaust the buffer.
auto String = DataProv.ConsumeBytesAsString(31337);
EXPECT_EQ(size_t(1014), String.length());
EXPECT_EQ(Enum::Zero, DataProv.ConsumeEnum<Enum>());
}
TEST(FuzzedDataProvider, remaining_bytes) {
FuzzedDataProvider DataProv(Data, sizeof(Data));
EXPECT_EQ(size_t(1024), DataProv.remaining_bytes());
EXPECT_EQ(false, DataProv.ConsumeBool());
EXPECT_EQ(size_t(1024 - 1), DataProv.remaining_bytes());
EXPECT_EQ(std::vector<uint8_t>(Data, Data + 8),
DataProv.ConsumeBytes<uint8_t>(8));
EXPECT_EQ(size_t(1024 - 1 - 8), DataProv.remaining_bytes());
// Exhaust the buffer.
EXPECT_EQ(std::vector<uint8_t>(Data + 8, Data + sizeof(Data) - 1),
DataProv.ConsumeRemainingBytes<uint8_t>());
EXPECT_EQ(size_t(0), DataProv.remaining_bytes());
}
TEST(FuzzedDataProvider, ConsumeProbability) {
FuzzedDataProvider DataProv(Data, sizeof(Data));
ASSERT_FLOAT_EQ(float(0.28969181), DataProv.ConsumeProbability<float>());
ASSERT_DOUBLE_EQ(double(0.086814121166605432),
DataProv.ConsumeProbability<double>());
ASSERT_FLOAT_EQ(float(0.30104411), DataProv.ConsumeProbability<float>());
ASSERT_DOUBLE_EQ(double(0.96218831486039413),
DataProv.ConsumeProbability<double>());
ASSERT_FLOAT_EQ(float(0.67005056), DataProv.ConsumeProbability<float>());
ASSERT_DOUBLE_EQ(double(0.69210584173832279),
DataProv.ConsumeProbability<double>());
// Exhaust the buffer.
EXPECT_EQ(std::vector<uint8_t>(Data, Data + sizeof(Data) - 36),
DataProv.ConsumeRemainingBytes<uint8_t>());
ASSERT_FLOAT_EQ(float(0.0), DataProv.ConsumeProbability<float>());
}
TEST(FuzzedDataProvider, ConsumeFloatingPoint) {
FuzzedDataProvider DataProv(Data, sizeof(Data));
ASSERT_FLOAT_EQ(float(-2.8546307e+38),
DataProv.ConsumeFloatingPoint<float>());
ASSERT_DOUBLE_EQ(double(8.0940194040236032e+307),
DataProv.ConsumeFloatingPoint<double>());
ASSERT_FLOAT_EQ(float(271.49084),
DataProv.ConsumeFloatingPointInRange<float>(123.0, 777.0));
ASSERT_DOUBLE_EQ(double(30.859126145478349),
DataProv.ConsumeFloatingPointInRange<double>(13.37, 31.337));
ASSERT_FLOAT_EQ(
float(-903.47729),
DataProv.ConsumeFloatingPointInRange<float>(-999.9999, -777.77));
ASSERT_DOUBLE_EQ(
double(24.561393182922771),
DataProv.ConsumeFloatingPointInRange<double>(-13.37, 31.337));
ASSERT_FLOAT_EQ(float(1.0),
DataProv.ConsumeFloatingPointInRange<float>(1.0, 1.0));
ASSERT_DOUBLE_EQ(double(-1.0),
DataProv.ConsumeFloatingPointInRange<double>(-1.0, -1.0));
// Exhaust the buffer.
EXPECT_EQ((std::vector<uint8_t>(Data, Data + sizeof(Data) - 50)).size(),
DataProv.ConsumeRemainingBytes<uint8_t>().size());
ASSERT_FLOAT_EQ(float(0.0), DataProv.ConsumeProbability<float>());
ASSERT_NEAR(std::numeric_limits<double>::lowest(),
DataProv.ConsumeFloatingPoint<double>(), 1e-10);
ASSERT_FLOAT_EQ(float(123.0),
DataProv.ConsumeFloatingPointInRange<float>(123.0, 777.0));
ASSERT_DOUBLE_EQ(double(-13.37), DataProv.ConsumeFloatingPointInRange<double>(
-13.37, 31.337));
}
TEST(FuzzedDataProvider, ConsumeData) {
FuzzedDataProvider DataProv(Data, sizeof(Data));
uint8_t Buffer[10] = {};
EXPECT_EQ(sizeof(Buffer), DataProv.ConsumeData(Buffer, sizeof(Buffer)));
std::vector<uint8_t> Expected(Data, Data + sizeof(Buffer));
EXPECT_EQ(Expected, std::vector<uint8_t>(Buffer, Buffer + sizeof(Buffer)));
EXPECT_EQ(size_t(2), DataProv.ConsumeData(Buffer, 2));
Expected[0] = Data[sizeof(Buffer)];
Expected[1] = Data[sizeof(Buffer) + 1];
EXPECT_EQ(Expected, std::vector<uint8_t>(Buffer, Buffer + sizeof(Buffer)));
// Exhaust the buffer.
EXPECT_EQ(std::vector<uint8_t>(Data + 12, Data + sizeof(Data)),
DataProv.ConsumeRemainingBytes<uint8_t>());
EXPECT_EQ(size_t(0), DataProv.ConsumeData(Buffer, sizeof(Buffer)));
EXPECT_EQ(Expected, std::vector<uint8_t>(Buffer, Buffer + sizeof(Buffer)));
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}