[lib/Fuzzer] extend the fuzzer interface to allow user-supplied mutators
[oota-llvm.git] / lib / Fuzzer / test / FuzzerUnittest.cpp
1 #include "FuzzerInternal.h"
2 #include "gtest/gtest.h"
3 #include <set>
4
5 // For now, have LLVMFuzzerTestOneInput just to make it link.
6 // Later we may want to make unittests that actually call LLVMFuzzerTestOneInput.
7 extern "C" void LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
8   abort();
9 }
10
11 TEST(Fuzzer, CrossOver) {
12   using namespace fuzzer;
13   Unit A({0, 1, 2}), B({5, 6, 7});
14   Unit C;
15   Unit Expected[] = {
16        { 0 },
17        { 0, 1 },
18        { 0, 5 },
19        { 0, 1, 2 },
20        { 0, 1, 5 },
21        { 0, 5, 1 },
22        { 0, 5, 6 },
23        { 0, 1, 2, 5 },
24        { 0, 1, 5, 2 },
25        { 0, 1, 5, 6 },
26        { 0, 5, 1, 2 },
27        { 0, 5, 1, 6 },
28        { 0, 5, 6, 1 },
29        { 0, 5, 6, 7 },
30        { 0, 1, 2, 5, 6 },
31        { 0, 1, 5, 2, 6 },
32        { 0, 1, 5, 6, 2 },
33        { 0, 1, 5, 6, 7 },
34        { 0, 5, 1, 2, 6 },
35        { 0, 5, 1, 6, 2 },
36        { 0, 5, 1, 6, 7 },
37        { 0, 5, 6, 1, 2 },
38        { 0, 5, 6, 1, 7 },
39        { 0, 5, 6, 7, 1 },
40        { 0, 1, 2, 5, 6, 7 },
41        { 0, 1, 5, 2, 6, 7 },
42        { 0, 1, 5, 6, 2, 7 },
43        { 0, 1, 5, 6, 7, 2 },
44        { 0, 5, 1, 2, 6, 7 },
45        { 0, 5, 1, 6, 2, 7 },
46        { 0, 5, 1, 6, 7, 2 },
47        { 0, 5, 6, 1, 2, 7 },
48        { 0, 5, 6, 1, 7, 2 },
49        { 0, 5, 6, 7, 1, 2 }
50   };
51   for (size_t Len = 1; Len < 8; Len++) {
52     std::set<Unit> FoundUnits, ExpectedUnitsWitThisLength;
53     for (int Iter = 0; Iter < 3000; Iter++) {
54       C.resize(Len);
55       size_t NewSize = CrossOver(A.data(), A.size(), B.data(), B.size(),
56                                  C.data(), C.size());
57       C.resize(NewSize);
58       FoundUnits.insert(C);
59     }
60     for (const Unit &U : Expected)
61       if (U.size() <= Len)
62         ExpectedUnitsWitThisLength.insert(U);
63     EXPECT_EQ(ExpectedUnitsWitThisLength, FoundUnits);
64   }
65 }
66
67 TEST(Fuzzer, Hash) {
68   uint8_t A[] = {'a', 'b', 'c'};
69   fuzzer::Unit U(A, A + sizeof(A));
70   EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d", fuzzer::Hash(U));
71   U.push_back('d');
72   EXPECT_EQ("81fe8bfe87576c3ecb22426f8e57847382917acf", fuzzer::Hash(U));
73 }