[libFuzzer] make sure that 2-byte arguments of switch() are handled properly
[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   FuzzerRandomLibc Rand(0);
14   Unit A({0, 1, 2}), B({5, 6, 7});
15   Unit C;
16   Unit Expected[] = {
17        { 0 },
18        { 0, 1 },
19        { 0, 5 },
20        { 0, 1, 2 },
21        { 0, 1, 5 },
22        { 0, 5, 1 },
23        { 0, 5, 6 },
24        { 0, 1, 2, 5 },
25        { 0, 1, 5, 2 },
26        { 0, 1, 5, 6 },
27        { 0, 5, 1, 2 },
28        { 0, 5, 1, 6 },
29        { 0, 5, 6, 1 },
30        { 0, 5, 6, 7 },
31        { 0, 1, 2, 5, 6 },
32        { 0, 1, 5, 2, 6 },
33        { 0, 1, 5, 6, 2 },
34        { 0, 1, 5, 6, 7 },
35        { 0, 5, 1, 2, 6 },
36        { 0, 5, 1, 6, 2 },
37        { 0, 5, 1, 6, 7 },
38        { 0, 5, 6, 1, 2 },
39        { 0, 5, 6, 1, 7 },
40        { 0, 5, 6, 7, 1 },
41        { 0, 1, 2, 5, 6, 7 },
42        { 0, 1, 5, 2, 6, 7 },
43        { 0, 1, 5, 6, 2, 7 },
44        { 0, 1, 5, 6, 7, 2 },
45        { 0, 5, 1, 2, 6, 7 },
46        { 0, 5, 1, 6, 2, 7 },
47        { 0, 5, 1, 6, 7, 2 },
48        { 0, 5, 6, 1, 2, 7 },
49        { 0, 5, 6, 1, 7, 2 },
50        { 0, 5, 6, 7, 1, 2 }
51   };
52   for (size_t Len = 1; Len < 8; Len++) {
53     std::set<Unit> FoundUnits, ExpectedUnitsWitThisLength;
54     for (int Iter = 0; Iter < 3000; Iter++) {
55       C.resize(Len);
56       size_t NewSize = CrossOver(A.data(), A.size(), B.data(), B.size(),
57                                  C.data(), C.size(), Rand);
58       C.resize(NewSize);
59       FoundUnits.insert(C);
60     }
61     for (const Unit &U : Expected)
62       if (U.size() <= Len)
63         ExpectedUnitsWitThisLength.insert(U);
64     EXPECT_EQ(ExpectedUnitsWitThisLength, FoundUnits);
65   }
66 }
67
68 TEST(Fuzzer, Hash) {
69   uint8_t A[] = {'a', 'b', 'c'};
70   fuzzer::Unit U(A, A + sizeof(A));
71   EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d", fuzzer::Hash(U));
72   U.push_back('d');
73   EXPECT_EQ("81fe8bfe87576c3ecb22426f8e57847382917acf", fuzzer::Hash(U));
74 }