summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Fuzzer/test/FuzzerUnittest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Fuzzer/test/FuzzerUnittest.cpp')
-rw-r--r--llvm/lib/Fuzzer/test/FuzzerUnittest.cpp36
1 files changed, 35 insertions, 1 deletions
diff --git a/llvm/lib/Fuzzer/test/FuzzerUnittest.cpp b/llvm/lib/Fuzzer/test/FuzzerUnittest.cpp
index ed52a55a507..a159e411404 100644
--- a/llvm/lib/Fuzzer/test/FuzzerUnittest.cpp
+++ b/llvm/lib/Fuzzer/test/FuzzerUnittest.cpp
@@ -2,6 +2,8 @@
#include "gtest/gtest.h"
#include <set>
+using namespace fuzzer;
+
// For now, have LLVMFuzzerTestOneInput just to make it link.
// Later we may want to make unittests that actually call LLVMFuzzerTestOneInput.
extern "C" void LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
@@ -9,7 +11,6 @@ extern "C" void LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
}
TEST(Fuzzer, CrossOver) {
- using namespace fuzzer;
FuzzerRandomLibc Rand(0);
Unit A({0, 1, 2}), B({5, 6, 7});
Unit C;
@@ -72,3 +73,36 @@ TEST(Fuzzer, Hash) {
U.push_back('d');
EXPECT_EQ("81fe8bfe87576c3ecb22426f8e57847382917acf", fuzzer::Hash(U));
}
+
+typedef size_t (*Mutator)(uint8_t *Data, size_t Size, size_t MaxSize,
+ FuzzerRandomBase &Rand);
+
+void TestEraseByte(Mutator M, int NumIter) {
+ uint8_t REM0[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
+ uint8_t REM1[8] = {0x00, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
+ uint8_t REM2[8] = {0x00, 0x11, 0x33, 0x44, 0x55, 0x66, 0x77};
+ uint8_t REM3[8] = {0x00, 0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
+ uint8_t REM4[8] = {0x00, 0x11, 0x22, 0x33, 0x55, 0x66, 0x77};
+ uint8_t REM5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x66, 0x77};
+ uint8_t REM6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x77};
+ uint8_t REM7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
+ FuzzerRandomLibc Rand(0);
+ int FoundMask = 0;
+ for (int i = 0; i < NumIter; i++) {
+ uint8_t T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
+ size_t NewSize = Mutate_EraseByte(T, sizeof(T), sizeof(T), Rand);
+ EXPECT_EQ(7UL, NewSize);
+ if (!memcmp(REM0, T, 7)) FoundMask |= 1 << 0;
+ if (!memcmp(REM1, T, 7)) FoundMask |= 1 << 1;
+ if (!memcmp(REM2, T, 7)) FoundMask |= 1 << 2;
+ if (!memcmp(REM3, T, 7)) FoundMask |= 1 << 3;
+ if (!memcmp(REM4, T, 7)) FoundMask |= 1 << 4;
+ if (!memcmp(REM5, T, 7)) FoundMask |= 1 << 5;
+ if (!memcmp(REM6, T, 7)) FoundMask |= 1 << 6;
+ if (!memcmp(REM7, T, 7)) FoundMask |= 1 << 7;
+ }
+ EXPECT_EQ(FoundMask, 255);
+}
+
+TEST(FuzzerMutate, EraseByte1) { TestEraseByte(Mutate_EraseByte, 50); }
+TEST(FuzzerMutate, EraseByte2) { TestEraseByte(Mutate, 100); }
OpenPOWER on IntegriCloud