1 // This file is distributed under the University of Illinois Open Source 2 // License. See LICENSE.TXT for details. 3 4 // Avoid ODR violations (LibFuzzer is built without ASan and this test is built 5 // with ASan) involving C++ standard library types when using libcxx. 6 #define _LIBCPP_HAS_NO_ASAN 7 8 // Do not attempt to use LLVM ostream from gtest. 9 #define GTEST_NO_LLVM_RAW_OSTREAM 1 10 11 #include "FuzzerCorpus.h" 12 #include "FuzzerDictionary.h" 13 #include "FuzzerInternal.h" 14 #include "FuzzerMerge.h" 15 #include "FuzzerMutate.h" 16 #include "FuzzerRandom.h" 17 #include "FuzzerTracePC.h" 18 #include "gtest/gtest.h" 19 #include <memory> 20 #include <set> 21 #include <sstream> 22 23 using namespace fuzzer; 24 25 // For now, have LLVMFuzzerTestOneInput just to make it link. 26 // Later we may want to make unittests that actually call LLVMFuzzerTestOneInput. 27 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { 28 abort(); 29 } 30 31 TEST(Fuzzer, CrossOver) { 32 std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 33 fuzzer::EF = t.get(); 34 Random Rand(0); 35 std::unique_ptr<MutationDispatcher> MD(new MutationDispatcher(Rand, {})); 36 Unit A({0, 1, 2}), B({5, 6, 7}); 37 Unit C; 38 Unit Expected[] = { 39 { 0 }, 40 { 0, 1 }, 41 { 0, 5 }, 42 { 0, 1, 2 }, 43 { 0, 1, 5 }, 44 { 0, 5, 1 }, 45 { 0, 5, 6 }, 46 { 0, 1, 2, 5 }, 47 { 0, 1, 5, 2 }, 48 { 0, 1, 5, 6 }, 49 { 0, 5, 1, 2 }, 50 { 0, 5, 1, 6 }, 51 { 0, 5, 6, 1 }, 52 { 0, 5, 6, 7 }, 53 { 0, 1, 2, 5, 6 }, 54 { 0, 1, 5, 2, 6 }, 55 { 0, 1, 5, 6, 2 }, 56 { 0, 1, 5, 6, 7 }, 57 { 0, 5, 1, 2, 6 }, 58 { 0, 5, 1, 6, 2 }, 59 { 0, 5, 1, 6, 7 }, 60 { 0, 5, 6, 1, 2 }, 61 { 0, 5, 6, 1, 7 }, 62 { 0, 5, 6, 7, 1 }, 63 { 0, 1, 2, 5, 6, 7 }, 64 { 0, 1, 5, 2, 6, 7 }, 65 { 0, 1, 5, 6, 2, 7 }, 66 { 0, 1, 5, 6, 7, 2 }, 67 { 0, 5, 1, 2, 6, 7 }, 68 { 0, 5, 1, 6, 2, 7 }, 69 { 0, 5, 1, 6, 7, 2 }, 70 { 0, 5, 6, 1, 2, 7 }, 71 { 0, 5, 6, 1, 7, 2 }, 72 { 0, 5, 6, 7, 1, 2 } 73 }; 74 for (size_t Len = 1; Len < 8; Len++) { 75 Set<Unit> FoundUnits, ExpectedUnitsWitThisLength; 76 for (int Iter = 0; Iter < 3000; Iter++) { 77 C.resize(Len); 78 size_t NewSize = MD->CrossOver(A.data(), A.size(), B.data(), B.size(), 79 C.data(), C.size()); 80 C.resize(NewSize); 81 FoundUnits.insert(C); 82 } 83 for (const Unit &U : Expected) 84 if (U.size() <= Len) 85 ExpectedUnitsWitThisLength.insert(U); 86 EXPECT_EQ(ExpectedUnitsWitThisLength, FoundUnits); 87 } 88 } 89 90 TEST(Fuzzer, Hash) { 91 uint8_t A[] = {'a', 'b', 'c'}; 92 fuzzer::Unit U(A, A + sizeof(A)); 93 EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d", fuzzer::Hash(U)); 94 U.push_back('d'); 95 EXPECT_EQ("81fe8bfe87576c3ecb22426f8e57847382917acf", fuzzer::Hash(U)); 96 } 97 98 typedef size_t (MutationDispatcher::*Mutator)(uint8_t *Data, size_t Size, 99 size_t MaxSize); 100 101 void TestEraseBytes(Mutator M, int NumIter) { 102 std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 103 fuzzer::EF = t.get(); 104 uint8_t REM0[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 105 uint8_t REM1[8] = {0x00, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 106 uint8_t REM2[8] = {0x00, 0x11, 0x33, 0x44, 0x55, 0x66, 0x77}; 107 uint8_t REM3[8] = {0x00, 0x11, 0x22, 0x44, 0x55, 0x66, 0x77}; 108 uint8_t REM4[8] = {0x00, 0x11, 0x22, 0x33, 0x55, 0x66, 0x77}; 109 uint8_t REM5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x66, 0x77}; 110 uint8_t REM6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x77}; 111 uint8_t REM7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; 112 113 uint8_t REM8[6] = {0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 114 uint8_t REM9[6] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}; 115 uint8_t REM10[6] = {0x00, 0x11, 0x22, 0x55, 0x66, 0x77}; 116 117 uint8_t REM11[5] = {0x33, 0x44, 0x55, 0x66, 0x77}; 118 uint8_t REM12[5] = {0x00, 0x11, 0x22, 0x33, 0x44}; 119 uint8_t REM13[5] = {0x00, 0x44, 0x55, 0x66, 0x77}; 120 121 122 Random Rand(0); 123 std::unique_ptr<MutationDispatcher> MD(new MutationDispatcher(Rand, {})); 124 int FoundMask = 0; 125 for (int i = 0; i < NumIter; i++) { 126 uint8_t T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 127 size_t NewSize = (*MD.*M)(T, sizeof(T), sizeof(T)); 128 if (NewSize == 7 && !memcmp(REM0, T, 7)) FoundMask |= 1 << 0; 129 if (NewSize == 7 && !memcmp(REM1, T, 7)) FoundMask |= 1 << 1; 130 if (NewSize == 7 && !memcmp(REM2, T, 7)) FoundMask |= 1 << 2; 131 if (NewSize == 7 && !memcmp(REM3, T, 7)) FoundMask |= 1 << 3; 132 if (NewSize == 7 && !memcmp(REM4, T, 7)) FoundMask |= 1 << 4; 133 if (NewSize == 7 && !memcmp(REM5, T, 7)) FoundMask |= 1 << 5; 134 if (NewSize == 7 && !memcmp(REM6, T, 7)) FoundMask |= 1 << 6; 135 if (NewSize == 7 && !memcmp(REM7, T, 7)) FoundMask |= 1 << 7; 136 137 if (NewSize == 6 && !memcmp(REM8, T, 6)) FoundMask |= 1 << 8; 138 if (NewSize == 6 && !memcmp(REM9, T, 6)) FoundMask |= 1 << 9; 139 if (NewSize == 6 && !memcmp(REM10, T, 6)) FoundMask |= 1 << 10; 140 141 if (NewSize == 5 && !memcmp(REM11, T, 5)) FoundMask |= 1 << 11; 142 if (NewSize == 5 && !memcmp(REM12, T, 5)) FoundMask |= 1 << 12; 143 if (NewSize == 5 && !memcmp(REM13, T, 5)) FoundMask |= 1 << 13; 144 } 145 EXPECT_EQ(FoundMask, (1 << 14) - 1); 146 } 147 148 TEST(FuzzerMutate, EraseBytes1) { 149 TestEraseBytes(&MutationDispatcher::Mutate_EraseBytes, 200); 150 } 151 TEST(FuzzerMutate, EraseBytes2) { 152 TestEraseBytes(&MutationDispatcher::Mutate, 2000); 153 } 154 155 void TestInsertByte(Mutator M, int NumIter) { 156 std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 157 fuzzer::EF = t.get(); 158 Random Rand(0); 159 std::unique_ptr<MutationDispatcher> MD(new MutationDispatcher(Rand, {})); 160 int FoundMask = 0; 161 uint8_t INS0[8] = {0xF1, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; 162 uint8_t INS1[8] = {0x00, 0xF2, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; 163 uint8_t INS2[8] = {0x00, 0x11, 0xF3, 0x22, 0x33, 0x44, 0x55, 0x66}; 164 uint8_t INS3[8] = {0x00, 0x11, 0x22, 0xF4, 0x33, 0x44, 0x55, 0x66}; 165 uint8_t INS4[8] = {0x00, 0x11, 0x22, 0x33, 0xF5, 0x44, 0x55, 0x66}; 166 uint8_t INS5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0xF6, 0x55, 0x66}; 167 uint8_t INS6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0xF7, 0x66}; 168 uint8_t INS7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xF8}; 169 for (int i = 0; i < NumIter; i++) { 170 uint8_t T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; 171 size_t NewSize = (*MD.*M)(T, 7, 8); 172 if (NewSize == 8 && !memcmp(INS0, T, 8)) FoundMask |= 1 << 0; 173 if (NewSize == 8 && !memcmp(INS1, T, 8)) FoundMask |= 1 << 1; 174 if (NewSize == 8 && !memcmp(INS2, T, 8)) FoundMask |= 1 << 2; 175 if (NewSize == 8 && !memcmp(INS3, T, 8)) FoundMask |= 1 << 3; 176 if (NewSize == 8 && !memcmp(INS4, T, 8)) FoundMask |= 1 << 4; 177 if (NewSize == 8 && !memcmp(INS5, T, 8)) FoundMask |= 1 << 5; 178 if (NewSize == 8 && !memcmp(INS6, T, 8)) FoundMask |= 1 << 6; 179 if (NewSize == 8 && !memcmp(INS7, T, 8)) FoundMask |= 1 << 7; 180 } 181 EXPECT_EQ(FoundMask, 255); 182 } 183 184 TEST(FuzzerMutate, InsertByte1) { 185 TestInsertByte(&MutationDispatcher::Mutate_InsertByte, 1 << 15); 186 } 187 TEST(FuzzerMutate, InsertByte2) { 188 TestInsertByte(&MutationDispatcher::Mutate, 1 << 17); 189 } 190 191 void TestInsertRepeatedBytes(Mutator M, int NumIter) { 192 std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 193 fuzzer::EF = t.get(); 194 Random Rand(0); 195 std::unique_ptr<MutationDispatcher> MD(new MutationDispatcher(Rand, {})); 196 int FoundMask = 0; 197 uint8_t INS0[7] = {0x00, 0x11, 0x22, 0x33, 'a', 'a', 'a'}; 198 uint8_t INS1[7] = {0x00, 0x11, 0x22, 'a', 'a', 'a', 0x33}; 199 uint8_t INS2[7] = {0x00, 0x11, 'a', 'a', 'a', 0x22, 0x33}; 200 uint8_t INS3[7] = {0x00, 'a', 'a', 'a', 0x11, 0x22, 0x33}; 201 uint8_t INS4[7] = {'a', 'a', 'a', 0x00, 0x11, 0x22, 0x33}; 202 203 uint8_t INS5[8] = {0x00, 0x11, 0x22, 0x33, 'b', 'b', 'b', 'b'}; 204 uint8_t INS6[8] = {0x00, 0x11, 0x22, 'b', 'b', 'b', 'b', 0x33}; 205 uint8_t INS7[8] = {0x00, 0x11, 'b', 'b', 'b', 'b', 0x22, 0x33}; 206 uint8_t INS8[8] = {0x00, 'b', 'b', 'b', 'b', 0x11, 0x22, 0x33}; 207 uint8_t INS9[8] = {'b', 'b', 'b', 'b', 0x00, 0x11, 0x22, 0x33}; 208 209 for (int i = 0; i < NumIter; i++) { 210 uint8_t T[8] = {0x00, 0x11, 0x22, 0x33}; 211 size_t NewSize = (*MD.*M)(T, 4, 8); 212 if (NewSize == 7 && !memcmp(INS0, T, 7)) FoundMask |= 1 << 0; 213 if (NewSize == 7 && !memcmp(INS1, T, 7)) FoundMask |= 1 << 1; 214 if (NewSize == 7 && !memcmp(INS2, T, 7)) FoundMask |= 1 << 2; 215 if (NewSize == 7 && !memcmp(INS3, T, 7)) FoundMask |= 1 << 3; 216 if (NewSize == 7 && !memcmp(INS4, T, 7)) FoundMask |= 1 << 4; 217 218 if (NewSize == 8 && !memcmp(INS5, T, 8)) FoundMask |= 1 << 5; 219 if (NewSize == 8 && !memcmp(INS6, T, 8)) FoundMask |= 1 << 6; 220 if (NewSize == 8 && !memcmp(INS7, T, 8)) FoundMask |= 1 << 7; 221 if (NewSize == 8 && !memcmp(INS8, T, 8)) FoundMask |= 1 << 8; 222 if (NewSize == 8 && !memcmp(INS9, T, 8)) FoundMask |= 1 << 9; 223 224 } 225 EXPECT_EQ(FoundMask, (1 << 10) - 1); 226 } 227 228 TEST(FuzzerMutate, InsertRepeatedBytes1) { 229 TestInsertRepeatedBytes(&MutationDispatcher::Mutate_InsertRepeatedBytes, 10000); 230 } 231 TEST(FuzzerMutate, InsertRepeatedBytes2) { 232 TestInsertRepeatedBytes(&MutationDispatcher::Mutate, 300000); 233 } 234 235 void TestChangeByte(Mutator M, int NumIter) { 236 std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 237 fuzzer::EF = t.get(); 238 Random Rand(0); 239 std::unique_ptr<MutationDispatcher> MD(new MutationDispatcher(Rand, {})); 240 int FoundMask = 0; 241 uint8_t CH0[8] = {0xF0, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 242 uint8_t CH1[8] = {0x00, 0xF1, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 243 uint8_t CH2[8] = {0x00, 0x11, 0xF2, 0x33, 0x44, 0x55, 0x66, 0x77}; 244 uint8_t CH3[8] = {0x00, 0x11, 0x22, 0xF3, 0x44, 0x55, 0x66, 0x77}; 245 uint8_t CH4[8] = {0x00, 0x11, 0x22, 0x33, 0xF4, 0x55, 0x66, 0x77}; 246 uint8_t CH5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0xF5, 0x66, 0x77}; 247 uint8_t CH6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0xF5, 0x77}; 248 uint8_t CH7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xF7}; 249 for (int i = 0; i < NumIter; i++) { 250 uint8_t T[9] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 251 size_t NewSize = (*MD.*M)(T, 8, 9); 252 if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0; 253 if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1; 254 if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2; 255 if (NewSize == 8 && !memcmp(CH3, T, 8)) FoundMask |= 1 << 3; 256 if (NewSize == 8 && !memcmp(CH4, T, 8)) FoundMask |= 1 << 4; 257 if (NewSize == 8 && !memcmp(CH5, T, 8)) FoundMask |= 1 << 5; 258 if (NewSize == 8 && !memcmp(CH6, T, 8)) FoundMask |= 1 << 6; 259 if (NewSize == 8 && !memcmp(CH7, T, 8)) FoundMask |= 1 << 7; 260 } 261 EXPECT_EQ(FoundMask, 255); 262 } 263 264 TEST(FuzzerMutate, ChangeByte1) { 265 TestChangeByte(&MutationDispatcher::Mutate_ChangeByte, 1 << 15); 266 } 267 TEST(FuzzerMutate, ChangeByte2) { 268 TestChangeByte(&MutationDispatcher::Mutate, 1 << 17); 269 } 270 271 void TestChangeBit(Mutator M, int NumIter) { 272 std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 273 fuzzer::EF = t.get(); 274 Random Rand(0); 275 std::unique_ptr<MutationDispatcher> MD(new MutationDispatcher(Rand, {})); 276 int FoundMask = 0; 277 uint8_t CH0[8] = {0x01, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 278 uint8_t CH1[8] = {0x00, 0x13, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 279 uint8_t CH2[8] = {0x00, 0x11, 0x02, 0x33, 0x44, 0x55, 0x66, 0x77}; 280 uint8_t CH3[8] = {0x00, 0x11, 0x22, 0x37, 0x44, 0x55, 0x66, 0x77}; 281 uint8_t CH4[8] = {0x00, 0x11, 0x22, 0x33, 0x54, 0x55, 0x66, 0x77}; 282 uint8_t CH5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x54, 0x66, 0x77}; 283 uint8_t CH6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x76, 0x77}; 284 uint8_t CH7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xF7}; 285 for (int i = 0; i < NumIter; i++) { 286 uint8_t T[9] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 287 size_t NewSize = (*MD.*M)(T, 8, 9); 288 if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0; 289 if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1; 290 if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2; 291 if (NewSize == 8 && !memcmp(CH3, T, 8)) FoundMask |= 1 << 3; 292 if (NewSize == 8 && !memcmp(CH4, T, 8)) FoundMask |= 1 << 4; 293 if (NewSize == 8 && !memcmp(CH5, T, 8)) FoundMask |= 1 << 5; 294 if (NewSize == 8 && !memcmp(CH6, T, 8)) FoundMask |= 1 << 6; 295 if (NewSize == 8 && !memcmp(CH7, T, 8)) FoundMask |= 1 << 7; 296 } 297 EXPECT_EQ(FoundMask, 255); 298 } 299 300 TEST(FuzzerMutate, ChangeBit1) { 301 TestChangeBit(&MutationDispatcher::Mutate_ChangeBit, 1 << 16); 302 } 303 TEST(FuzzerMutate, ChangeBit2) { 304 TestChangeBit(&MutationDispatcher::Mutate, 1 << 18); 305 } 306 307 void TestShuffleBytes(Mutator M, int NumIter) { 308 std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 309 fuzzer::EF = t.get(); 310 Random Rand(0); 311 std::unique_ptr<MutationDispatcher> MD(new MutationDispatcher(Rand, {})); 312 int FoundMask = 0; 313 uint8_t CH0[7] = {0x00, 0x22, 0x11, 0x33, 0x44, 0x55, 0x66}; 314 uint8_t CH1[7] = {0x11, 0x00, 0x33, 0x22, 0x44, 0x55, 0x66}; 315 uint8_t CH2[7] = {0x00, 0x33, 0x11, 0x22, 0x44, 0x55, 0x66}; 316 uint8_t CH3[7] = {0x00, 0x11, 0x22, 0x44, 0x55, 0x66, 0x33}; 317 uint8_t CH4[7] = {0x00, 0x11, 0x22, 0x33, 0x55, 0x44, 0x66}; 318 for (int i = 0; i < NumIter; i++) { 319 uint8_t T[7] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; 320 size_t NewSize = (*MD.*M)(T, 7, 7); 321 if (NewSize == 7 && !memcmp(CH0, T, 7)) FoundMask |= 1 << 0; 322 if (NewSize == 7 && !memcmp(CH1, T, 7)) FoundMask |= 1 << 1; 323 if (NewSize == 7 && !memcmp(CH2, T, 7)) FoundMask |= 1 << 2; 324 if (NewSize == 7 && !memcmp(CH3, T, 7)) FoundMask |= 1 << 3; 325 if (NewSize == 7 && !memcmp(CH4, T, 7)) FoundMask |= 1 << 4; 326 } 327 EXPECT_EQ(FoundMask, 31); 328 } 329 330 TEST(FuzzerMutate, ShuffleBytes1) { 331 TestShuffleBytes(&MutationDispatcher::Mutate_ShuffleBytes, 1 << 17); 332 } 333 TEST(FuzzerMutate, ShuffleBytes2) { 334 TestShuffleBytes(&MutationDispatcher::Mutate, 1 << 20); 335 } 336 337 void TestCopyPart(Mutator M, int NumIter) { 338 std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 339 fuzzer::EF = t.get(); 340 Random Rand(0); 341 std::unique_ptr<MutationDispatcher> MD(new MutationDispatcher(Rand, {})); 342 int FoundMask = 0; 343 uint8_t CH0[7] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x00, 0x11}; 344 uint8_t CH1[7] = {0x55, 0x66, 0x22, 0x33, 0x44, 0x55, 0x66}; 345 uint8_t CH2[7] = {0x00, 0x55, 0x66, 0x33, 0x44, 0x55, 0x66}; 346 uint8_t CH3[7] = {0x00, 0x11, 0x22, 0x00, 0x11, 0x22, 0x66}; 347 uint8_t CH4[7] = {0x00, 0x11, 0x11, 0x22, 0x33, 0x55, 0x66}; 348 349 for (int i = 0; i < NumIter; i++) { 350 uint8_t T[7] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; 351 size_t NewSize = (*MD.*M)(T, 7, 7); 352 if (NewSize == 7 && !memcmp(CH0, T, 7)) FoundMask |= 1 << 0; 353 if (NewSize == 7 && !memcmp(CH1, T, 7)) FoundMask |= 1 << 1; 354 if (NewSize == 7 && !memcmp(CH2, T, 7)) FoundMask |= 1 << 2; 355 if (NewSize == 7 && !memcmp(CH3, T, 7)) FoundMask |= 1 << 3; 356 if (NewSize == 7 && !memcmp(CH4, T, 7)) FoundMask |= 1 << 4; 357 } 358 359 uint8_t CH5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x00, 0x11, 0x22}; 360 uint8_t CH6[8] = {0x22, 0x33, 0x44, 0x00, 0x11, 0x22, 0x33, 0x44}; 361 uint8_t CH7[8] = {0x00, 0x11, 0x22, 0x00, 0x11, 0x22, 0x33, 0x44}; 362 uint8_t CH8[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x22, 0x33, 0x44}; 363 uint8_t CH9[8] = {0x00, 0x11, 0x22, 0x22, 0x33, 0x44, 0x33, 0x44}; 364 365 for (int i = 0; i < NumIter; i++) { 366 uint8_t T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 367 size_t NewSize = (*MD.*M)(T, 5, 8); 368 if (NewSize == 8 && !memcmp(CH5, T, 8)) FoundMask |= 1 << 5; 369 if (NewSize == 8 && !memcmp(CH6, T, 8)) FoundMask |= 1 << 6; 370 if (NewSize == 8 && !memcmp(CH7, T, 8)) FoundMask |= 1 << 7; 371 if (NewSize == 8 && !memcmp(CH8, T, 8)) FoundMask |= 1 << 8; 372 if (NewSize == 8 && !memcmp(CH9, T, 8)) FoundMask |= 1 << 9; 373 } 374 375 EXPECT_EQ(FoundMask, 1023); 376 } 377 378 TEST(FuzzerMutate, CopyPart1) { 379 TestCopyPart(&MutationDispatcher::Mutate_CopyPart, 1 << 10); 380 } 381 TEST(FuzzerMutate, CopyPart2) { 382 TestCopyPart(&MutationDispatcher::Mutate, 1 << 13); 383 } 384 TEST(FuzzerMutate, CopyPartNoInsertAtMaxSize) { 385 // This (non exhaustively) tests if `Mutate_CopyPart` tries to perform an 386 // insert on an input of size `MaxSize`. Performing an insert in this case 387 // will lead to the mutation failing. 388 std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 389 fuzzer::EF = t.get(); 390 Random Rand(0); 391 std::unique_ptr<MutationDispatcher> MD(new MutationDispatcher(Rand, {})); 392 uint8_t Data[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x00, 0x11, 0x22}; 393 size_t MaxSize = sizeof(Data); 394 for (int count = 0; count < (1 << 18); ++count) { 395 size_t NewSize = MD->Mutate_CopyPart(Data, MaxSize, MaxSize); 396 ASSERT_EQ(NewSize, MaxSize); 397 } 398 } 399 400 void TestAddWordFromDictionary(Mutator M, int NumIter) { 401 std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 402 fuzzer::EF = t.get(); 403 Random Rand(0); 404 std::unique_ptr<MutationDispatcher> MD(new MutationDispatcher(Rand, {})); 405 uint8_t Word1[4] = {0xAA, 0xBB, 0xCC, 0xDD}; 406 uint8_t Word2[3] = {0xFF, 0xEE, 0xEF}; 407 MD->AddWordToManualDictionary(Word(Word1, sizeof(Word1))); 408 MD->AddWordToManualDictionary(Word(Word2, sizeof(Word2))); 409 int FoundMask = 0; 410 uint8_t CH0[7] = {0x00, 0x11, 0x22, 0xAA, 0xBB, 0xCC, 0xDD}; 411 uint8_t CH1[7] = {0x00, 0x11, 0xAA, 0xBB, 0xCC, 0xDD, 0x22}; 412 uint8_t CH2[7] = {0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0x11, 0x22}; 413 uint8_t CH3[7] = {0xAA, 0xBB, 0xCC, 0xDD, 0x00, 0x11, 0x22}; 414 uint8_t CH4[6] = {0x00, 0x11, 0x22, 0xFF, 0xEE, 0xEF}; 415 uint8_t CH5[6] = {0x00, 0x11, 0xFF, 0xEE, 0xEF, 0x22}; 416 uint8_t CH6[6] = {0x00, 0xFF, 0xEE, 0xEF, 0x11, 0x22}; 417 uint8_t CH7[6] = {0xFF, 0xEE, 0xEF, 0x00, 0x11, 0x22}; 418 for (int i = 0; i < NumIter; i++) { 419 uint8_t T[7] = {0x00, 0x11, 0x22}; 420 size_t NewSize = (*MD.*M)(T, 3, 7); 421 if (NewSize == 7 && !memcmp(CH0, T, 7)) FoundMask |= 1 << 0; 422 if (NewSize == 7 && !memcmp(CH1, T, 7)) FoundMask |= 1 << 1; 423 if (NewSize == 7 && !memcmp(CH2, T, 7)) FoundMask |= 1 << 2; 424 if (NewSize == 7 && !memcmp(CH3, T, 7)) FoundMask |= 1 << 3; 425 if (NewSize == 6 && !memcmp(CH4, T, 6)) FoundMask |= 1 << 4; 426 if (NewSize == 6 && !memcmp(CH5, T, 6)) FoundMask |= 1 << 5; 427 if (NewSize == 6 && !memcmp(CH6, T, 6)) FoundMask |= 1 << 6; 428 if (NewSize == 6 && !memcmp(CH7, T, 6)) FoundMask |= 1 << 7; 429 } 430 EXPECT_EQ(FoundMask, 255); 431 } 432 433 TEST(FuzzerMutate, AddWordFromDictionary1) { 434 TestAddWordFromDictionary( 435 &MutationDispatcher::Mutate_AddWordFromManualDictionary, 1 << 15); 436 } 437 438 TEST(FuzzerMutate, AddWordFromDictionary2) { 439 TestAddWordFromDictionary(&MutationDispatcher::Mutate, 1 << 15); 440 } 441 442 void TestChangeASCIIInteger(Mutator M, int NumIter) { 443 std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 444 fuzzer::EF = t.get(); 445 Random Rand(0); 446 std::unique_ptr<MutationDispatcher> MD(new MutationDispatcher(Rand, {})); 447 448 uint8_t CH0[8] = {'1', '2', '3', '4', '5', '6', '7', '7'}; 449 uint8_t CH1[8] = {'1', '2', '3', '4', '5', '6', '7', '9'}; 450 uint8_t CH2[8] = {'2', '4', '6', '9', '1', '3', '5', '6'}; 451 uint8_t CH3[8] = {'0', '6', '1', '7', '2', '8', '3', '9'}; 452 int FoundMask = 0; 453 for (int i = 0; i < NumIter; i++) { 454 uint8_t T[8] = {'1', '2', '3', '4', '5', '6', '7', '8'}; 455 size_t NewSize = (*MD.*M)(T, 8, 8); 456 /**/ if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0; 457 else if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1; 458 else if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2; 459 else if (NewSize == 8 && !memcmp(CH3, T, 8)) FoundMask |= 1 << 3; 460 else if (NewSize == 8) FoundMask |= 1 << 4; 461 } 462 EXPECT_EQ(FoundMask, 31); 463 } 464 465 TEST(FuzzerMutate, ChangeASCIIInteger1) { 466 TestChangeASCIIInteger(&MutationDispatcher::Mutate_ChangeASCIIInteger, 467 1 << 15); 468 } 469 470 TEST(FuzzerMutate, ChangeASCIIInteger2) { 471 TestChangeASCIIInteger(&MutationDispatcher::Mutate, 1 << 15); 472 } 473 474 void TestChangeBinaryInteger(Mutator M, int NumIter) { 475 std::unique_ptr<ExternalFunctions> t(new ExternalFunctions()); 476 fuzzer::EF = t.get(); 477 Random Rand(0); 478 std::unique_ptr<MutationDispatcher> MD(new MutationDispatcher(Rand, {})); 479 480 uint8_t CH0[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x79}; 481 uint8_t CH1[8] = {0x00, 0x11, 0x22, 0x31, 0x44, 0x55, 0x66, 0x77}; 482 uint8_t CH2[8] = {0xff, 0x10, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 483 uint8_t CH3[8] = {0x00, 0x11, 0x2a, 0x33, 0x44, 0x55, 0x66, 0x77}; 484 uint8_t CH4[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x4f, 0x66, 0x77}; 485 uint8_t CH5[8] = {0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88}; 486 uint8_t CH6[8] = {0x00, 0x11, 0x22, 0x00, 0x00, 0x00, 0x08, 0x77}; // Size 487 uint8_t CH7[8] = {0x00, 0x08, 0x00, 0x33, 0x44, 0x55, 0x66, 0x77}; // Sw(Size) 488 489 int FoundMask = 0; 490 for (int i = 0; i < NumIter; i++) { 491 uint8_t T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77}; 492 size_t NewSize = (*MD.*M)(T, 8, 8); 493 /**/ if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0; 494 else if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1; 495 else if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2; 496 else if (NewSize == 8 && !memcmp(CH3, T, 8)) FoundMask |= 1 << 3; 497 else if (NewSize == 8 && !memcmp(CH4, T, 8)) FoundMask |= 1 << 4; 498 else if (NewSize == 8 && !memcmp(CH5, T, 8)) FoundMask |= 1 << 5; 499 else if (NewSize == 8 && !memcmp(CH6, T, 8)) FoundMask |= 1 << 6; 500 else if (NewSize == 8 && !memcmp(CH7, T, 8)) FoundMask |= 1 << 7; 501 } 502 EXPECT_EQ(FoundMask, 255); 503 } 504 505 TEST(FuzzerMutate, ChangeBinaryInteger1) { 506 TestChangeBinaryInteger(&MutationDispatcher::Mutate_ChangeBinaryInteger, 507 1 << 12); 508 } 509 510 TEST(FuzzerMutate, ChangeBinaryInteger2) { 511 TestChangeBinaryInteger(&MutationDispatcher::Mutate, 1 << 15); 512 } 513 514 515 TEST(FuzzerDictionary, ParseOneDictionaryEntry) { 516 Unit U; 517 EXPECT_FALSE(ParseOneDictionaryEntry("", &U)); 518 EXPECT_FALSE(ParseOneDictionaryEntry(" ", &U)); 519 EXPECT_FALSE(ParseOneDictionaryEntry("\t ", &U)); 520 EXPECT_FALSE(ParseOneDictionaryEntry(" \" ", &U)); 521 EXPECT_FALSE(ParseOneDictionaryEntry(" zz\" ", &U)); 522 EXPECT_FALSE(ParseOneDictionaryEntry(" \"zz ", &U)); 523 EXPECT_FALSE(ParseOneDictionaryEntry(" \"\" ", &U)); 524 EXPECT_TRUE(ParseOneDictionaryEntry("\"a\"", &U)); 525 EXPECT_EQ(U, Unit({'a'})); 526 EXPECT_TRUE(ParseOneDictionaryEntry("\"abc\"", &U)); 527 EXPECT_EQ(U, Unit({'a', 'b', 'c'})); 528 EXPECT_TRUE(ParseOneDictionaryEntry("abc=\"abc\"", &U)); 529 EXPECT_EQ(U, Unit({'a', 'b', 'c'})); 530 EXPECT_FALSE(ParseOneDictionaryEntry("\"\\\"", &U)); 531 EXPECT_TRUE(ParseOneDictionaryEntry("\"\\\\\"", &U)); 532 EXPECT_EQ(U, Unit({'\\'})); 533 EXPECT_TRUE(ParseOneDictionaryEntry("\"\\xAB\"", &U)); 534 EXPECT_EQ(U, Unit({0xAB})); 535 EXPECT_TRUE(ParseOneDictionaryEntry("\"\\xABz\\xDE\"", &U)); 536 EXPECT_EQ(U, Unit({0xAB, 'z', 0xDE})); 537 EXPECT_TRUE(ParseOneDictionaryEntry("\"#\"", &U)); 538 EXPECT_EQ(U, Unit({'#'})); 539 EXPECT_TRUE(ParseOneDictionaryEntry("\"\\\"\"", &U)); 540 EXPECT_EQ(U, Unit({'"'})); 541 } 542 543 TEST(FuzzerDictionary, ParseDictionaryFile) { 544 Vector<Unit> Units; 545 EXPECT_FALSE(ParseDictionaryFile("zzz\n", &Units)); 546 EXPECT_FALSE(ParseDictionaryFile("", &Units)); 547 EXPECT_TRUE(ParseDictionaryFile("\n", &Units)); 548 EXPECT_EQ(Units.size(), 0U); 549 EXPECT_TRUE(ParseDictionaryFile("#zzzz a b c d\n", &Units)); 550 EXPECT_EQ(Units.size(), 0U); 551 EXPECT_TRUE(ParseDictionaryFile(" #zzzz\n", &Units)); 552 EXPECT_EQ(Units.size(), 0U); 553 EXPECT_TRUE(ParseDictionaryFile(" #zzzz\n", &Units)); 554 EXPECT_EQ(Units.size(), 0U); 555 EXPECT_TRUE(ParseDictionaryFile(" #zzzz\naaa=\"aa\"", &Units)); 556 EXPECT_EQ(Units, Vector<Unit>({Unit({'a', 'a'})})); 557 EXPECT_TRUE( 558 ParseDictionaryFile(" #zzzz\naaa=\"aa\"\n\nabc=\"abc\"", &Units)); 559 EXPECT_EQ(Units, 560 Vector<Unit>({Unit({'a', 'a'}), Unit({'a', 'b', 'c'})})); 561 } 562 563 TEST(FuzzerUtil, Base64) { 564 EXPECT_EQ("", Base64({})); 565 EXPECT_EQ("YQ==", Base64({'a'})); 566 EXPECT_EQ("eA==", Base64({'x'})); 567 EXPECT_EQ("YWI=", Base64({'a', 'b'})); 568 EXPECT_EQ("eHk=", Base64({'x', 'y'})); 569 EXPECT_EQ("YWJj", Base64({'a', 'b', 'c'})); 570 EXPECT_EQ("eHl6", Base64({'x', 'y', 'z'})); 571 EXPECT_EQ("YWJjeA==", Base64({'a', 'b', 'c', 'x'})); 572 EXPECT_EQ("YWJjeHk=", Base64({'a', 'b', 'c', 'x', 'y'})); 573 EXPECT_EQ("YWJjeHl6", Base64({'a', 'b', 'c', 'x', 'y', 'z'})); 574 } 575 576 TEST(Corpus, Distribution) { 577 Random Rand(0); 578 std::unique_ptr<InputCorpus> C(new InputCorpus("")); 579 size_t N = 10; 580 size_t TriesPerUnit = 1<<16; 581 for (size_t i = 0; i < N; i++) 582 C->AddToCorpus(Unit{ static_cast<uint8_t>(i) }, 1, false, {}); 583 584 Vector<size_t> Hist(N); 585 for (size_t i = 0; i < N * TriesPerUnit; i++) { 586 Hist[C->ChooseUnitIdxToMutate(Rand)]++; 587 } 588 for (size_t i = 0; i < N; i++) { 589 // A weak sanity check that every unit gets invoked. 590 EXPECT_GT(Hist[i], TriesPerUnit / N / 3); 591 } 592 } 593 594 TEST(Merge, Bad) { 595 const char *kInvalidInputs[] = { 596 "", 597 "x", 598 "3\nx", 599 "2\n3", 600 "2\n2", 601 "2\n2\nA\n", 602 "2\n2\nA\nB\nC\n", 603 "0\n0\n", 604 "1\n1\nA\nDONE 0", 605 "1\n1\nA\nSTARTED 1", 606 }; 607 Merger M; 608 for (auto S : kInvalidInputs) { 609 // fprintf(stderr, "TESTING:\n%s\n", S); 610 EXPECT_FALSE(M.Parse(S, false)); 611 } 612 } 613 614 void EQ(const Vector<uint32_t> &A, const Vector<uint32_t> &B) { 615 EXPECT_EQ(A, B); 616 } 617 618 void EQ(const Vector<std::string> &A, const Vector<std::string> &B) { 619 Set<std::string> a(A.begin(), A.end()); 620 Set<std::string> b(B.begin(), B.end()); 621 EXPECT_EQ(a, b); 622 } 623 624 static void Merge(const std::string &Input, 625 const Vector<std::string> Result, 626 size_t NumNewFeatures) { 627 Merger M; 628 Vector<std::string> NewFiles; 629 EXPECT_TRUE(M.Parse(Input, true)); 630 std::stringstream SS; 631 M.PrintSummary(SS); 632 EXPECT_EQ(NumNewFeatures, M.Merge(&NewFiles)); 633 EXPECT_EQ(M.AllFeatures(), M.ParseSummary(SS)); 634 EQ(NewFiles, Result); 635 } 636 637 TEST(Merge, Good) { 638 Merger M; 639 640 EXPECT_TRUE(M.Parse("1\n0\nAA\n", false)); 641 EXPECT_EQ(M.Files.size(), 1U); 642 EXPECT_EQ(M.NumFilesInFirstCorpus, 0U); 643 EXPECT_EQ(M.Files[0].Name, "AA"); 644 EXPECT_TRUE(M.LastFailure.empty()); 645 EXPECT_EQ(M.FirstNotProcessedFile, 0U); 646 647 EXPECT_TRUE(M.Parse("2\n1\nAA\nBB\nSTARTED 0 42\n", false)); 648 EXPECT_EQ(M.Files.size(), 2U); 649 EXPECT_EQ(M.NumFilesInFirstCorpus, 1U); 650 EXPECT_EQ(M.Files[0].Name, "AA"); 651 EXPECT_EQ(M.Files[1].Name, "BB"); 652 EXPECT_EQ(M.LastFailure, "AA"); 653 EXPECT_EQ(M.FirstNotProcessedFile, 1U); 654 655 EXPECT_TRUE(M.Parse("3\n1\nAA\nBB\nC\n" 656 "STARTED 0 1000\n" 657 "DONE 0 1 2 3\n" 658 "STARTED 1 1001\n" 659 "DONE 1 4 5 6 \n" 660 "STARTED 2 1002\n" 661 "", true)); 662 EXPECT_EQ(M.Files.size(), 3U); 663 EXPECT_EQ(M.NumFilesInFirstCorpus, 1U); 664 EXPECT_EQ(M.Files[0].Name, "AA"); 665 EXPECT_EQ(M.Files[0].Size, 1000U); 666 EXPECT_EQ(M.Files[1].Name, "BB"); 667 EXPECT_EQ(M.Files[1].Size, 1001U); 668 EXPECT_EQ(M.Files[2].Name, "C"); 669 EXPECT_EQ(M.Files[2].Size, 1002U); 670 EXPECT_EQ(M.LastFailure, "C"); 671 EXPECT_EQ(M.FirstNotProcessedFile, 3U); 672 EQ(M.Files[0].Features, {1, 2, 3}); 673 EQ(M.Files[1].Features, {4, 5, 6}); 674 675 676 Vector<std::string> NewFiles; 677 678 EXPECT_TRUE(M.Parse("3\n2\nAA\nBB\nC\n" 679 "STARTED 0 1000\nDONE 0 1 2 3\n" 680 "STARTED 1 1001\nDONE 1 4 5 6 \n" 681 "STARTED 2 1002\nDONE 2 6 1 3 \n" 682 "", true)); 683 EXPECT_EQ(M.Files.size(), 3U); 684 EXPECT_EQ(M.NumFilesInFirstCorpus, 2U); 685 EXPECT_TRUE(M.LastFailure.empty()); 686 EXPECT_EQ(M.FirstNotProcessedFile, 3U); 687 EQ(M.Files[0].Features, {1, 2, 3}); 688 EQ(M.Files[1].Features, {4, 5, 6}); 689 EQ(M.Files[2].Features, {1, 3, 6}); 690 EXPECT_EQ(0U, M.Merge(&NewFiles)); 691 EQ(NewFiles, {}); 692 693 EXPECT_TRUE(M.Parse("3\n1\nA\nB\nC\n" 694 "STARTED 0 1000\nDONE 0 1 2 3\n" 695 "STARTED 1 1001\nDONE 1 4 5 6 \n" 696 "STARTED 2 1002\nDONE 2 6 1 3\n" 697 "", true)); 698 EQ(M.Files[0].Features, {1, 2, 3}); 699 EQ(M.Files[1].Features, {4, 5, 6}); 700 EQ(M.Files[2].Features, {1, 3, 6}); 701 EXPECT_EQ(3U, M.Merge(&NewFiles)); 702 EQ(NewFiles, {"B"}); 703 704 // Same as the above, but with InitialFeatures. 705 EXPECT_TRUE(M.Parse("2\n0\nB\nC\n" 706 "STARTED 0 1001\nDONE 0 4 5 6 \n" 707 "STARTED 1 1002\nDONE 1 6 1 3\n" 708 "", true)); 709 EQ(M.Files[0].Features, {4, 5, 6}); 710 EQ(M.Files[1].Features, {1, 3, 6}); 711 Set<uint32_t> InitialFeatures; 712 InitialFeatures.insert(1); 713 InitialFeatures.insert(2); 714 InitialFeatures.insert(3); 715 EXPECT_EQ(3U, M.Merge(InitialFeatures, &NewFiles)); 716 EQ(NewFiles, {"B"}); 717 } 718 719 TEST(Merge, Merge) { 720 721 Merge("3\n1\nA\nB\nC\n" 722 "STARTED 0 1000\nDONE 0 1 2 3\n" 723 "STARTED 1 1001\nDONE 1 4 5 6 \n" 724 "STARTED 2 1002\nDONE 2 6 1 3 \n", 725 {"B"}, 3); 726 727 Merge("3\n0\nA\nB\nC\n" 728 "STARTED 0 2000\nDONE 0 1 2 3\n" 729 "STARTED 1 1001\nDONE 1 4 5 6 \n" 730 "STARTED 2 1002\nDONE 2 6 1 3 \n", 731 {"A", "B", "C"}, 6); 732 733 Merge("4\n0\nA\nB\nC\nD\n" 734 "STARTED 0 2000\nDONE 0 1 2 3\n" 735 "STARTED 1 1101\nDONE 1 4 5 6 \n" 736 "STARTED 2 1102\nDONE 2 6 1 3 100 \n" 737 "STARTED 3 1000\nDONE 3 1 \n", 738 {"A", "B", "C", "D"}, 7); 739 740 Merge("4\n1\nA\nB\nC\nD\n" 741 "STARTED 0 2000\nDONE 0 4 5 6 7 8\n" 742 "STARTED 1 1100\nDONE 1 1 2 3 \n" 743 "STARTED 2 1100\nDONE 2 2 3 \n" 744 "STARTED 3 1000\nDONE 3 1 \n", 745 {"B", "D"}, 3); 746 } 747 748 TEST(Fuzzer, ForEachNonZeroByte) { 749 const size_t N = 64; 750 alignas(64) uint8_t Ar[N + 8] = { 751 0, 0, 0, 0, 0, 0, 0, 0, 752 1, 2, 0, 0, 0, 0, 0, 0, 753 0, 0, 3, 0, 4, 0, 0, 0, 754 0, 0, 0, 0, 0, 0, 0, 0, 755 0, 0, 0, 5, 0, 6, 0, 0, 756 0, 0, 0, 0, 0, 0, 7, 0, 757 0, 0, 0, 0, 0, 0, 0, 0, 758 0, 0, 0, 0, 0, 0, 0, 8, 759 9, 9, 9, 9, 9, 9, 9, 9, 760 }; 761 typedef Vector<std::pair<size_t, uint8_t> > Vec; 762 Vec Res, Expected; 763 auto CB = [&](size_t FirstFeature, size_t Idx, uint8_t V) { 764 Res.push_back({FirstFeature + Idx, V}); 765 }; 766 ForEachNonZeroByte(Ar, Ar + N, 100, CB); 767 Expected = {{108, 1}, {109, 2}, {118, 3}, {120, 4}, 768 {135, 5}, {137, 6}, {146, 7}, {163, 8}}; 769 EXPECT_EQ(Res, Expected); 770 771 Res.clear(); 772 ForEachNonZeroByte(Ar + 9, Ar + N, 109, CB); 773 Expected = { {109, 2}, {118, 3}, {120, 4}, 774 {135, 5}, {137, 6}, {146, 7}, {163, 8}}; 775 EXPECT_EQ(Res, Expected); 776 777 Res.clear(); 778 ForEachNonZeroByte(Ar + 9, Ar + N - 9, 109, CB); 779 Expected = { {109, 2}, {118, 3}, {120, 4}, 780 {135, 5}, {137, 6}, {146, 7}}; 781 EXPECT_EQ(Res, Expected); 782 } 783 784 // FuzzerCommand unit tests. The arguments in the two helper methods below must 785 // match. 786 static void makeCommandArgs(Vector<std::string> *ArgsToAdd) { 787 assert(ArgsToAdd); 788 ArgsToAdd->clear(); 789 ArgsToAdd->push_back("foo"); 790 ArgsToAdd->push_back("-bar=baz"); 791 ArgsToAdd->push_back("qux"); 792 ArgsToAdd->push_back(Command::ignoreRemainingArgs()); 793 ArgsToAdd->push_back("quux"); 794 ArgsToAdd->push_back("-grault=garply"); 795 } 796 797 static std::string makeCmdLine(const char *separator, const char *suffix) { 798 std::string CmdLine("foo -bar=baz qux "); 799 if (strlen(separator) != 0) { 800 CmdLine += separator; 801 CmdLine += " "; 802 } 803 CmdLine += Command::ignoreRemainingArgs(); 804 CmdLine += " quux -grault=garply"; 805 if (strlen(suffix) != 0) { 806 CmdLine += " "; 807 CmdLine += suffix; 808 } 809 return CmdLine; 810 } 811 812 TEST(FuzzerCommand, Create) { 813 std::string CmdLine; 814 815 // Default constructor 816 Command DefaultCmd; 817 818 CmdLine = DefaultCmd.toString(); 819 EXPECT_EQ(CmdLine, ""); 820 821 // Explicit constructor 822 Vector<std::string> ArgsToAdd; 823 makeCommandArgs(&ArgsToAdd); 824 Command InitializedCmd(ArgsToAdd); 825 826 CmdLine = InitializedCmd.toString(); 827 EXPECT_EQ(CmdLine, makeCmdLine("", "")); 828 829 // Compare each argument 830 auto InitializedArgs = InitializedCmd.getArguments(); 831 auto i = ArgsToAdd.begin(); 832 auto j = InitializedArgs.begin(); 833 while (i != ArgsToAdd.end() && j != InitializedArgs.end()) { 834 EXPECT_EQ(*i++, *j++); 835 } 836 EXPECT_EQ(i, ArgsToAdd.end()); 837 EXPECT_EQ(j, InitializedArgs.end()); 838 839 // Copy constructor 840 Command CopiedCmd(InitializedCmd); 841 842 CmdLine = CopiedCmd.toString(); 843 EXPECT_EQ(CmdLine, makeCmdLine("", "")); 844 845 // Assignment operator 846 Command AssignedCmd; 847 AssignedCmd = CopiedCmd; 848 849 CmdLine = AssignedCmd.toString(); 850 EXPECT_EQ(CmdLine, makeCmdLine("", "")); 851 } 852 853 TEST(FuzzerCommand, ModifyArguments) { 854 Vector<std::string> ArgsToAdd; 855 makeCommandArgs(&ArgsToAdd); 856 Command Cmd; 857 std::string CmdLine; 858 859 Cmd.addArguments(ArgsToAdd); 860 CmdLine = Cmd.toString(); 861 EXPECT_EQ(CmdLine, makeCmdLine("", "")); 862 863 Cmd.addArgument("waldo"); 864 EXPECT_TRUE(Cmd.hasArgument("waldo")); 865 866 CmdLine = Cmd.toString(); 867 EXPECT_EQ(CmdLine, makeCmdLine("waldo", "")); 868 869 Cmd.removeArgument("waldo"); 870 EXPECT_FALSE(Cmd.hasArgument("waldo")); 871 872 CmdLine = Cmd.toString(); 873 EXPECT_EQ(CmdLine, makeCmdLine("", "")); 874 } 875 876 TEST(FuzzerCommand, ModifyFlags) { 877 Vector<std::string> ArgsToAdd; 878 makeCommandArgs(&ArgsToAdd); 879 Command Cmd(ArgsToAdd); 880 std::string Value, CmdLine; 881 ASSERT_FALSE(Cmd.hasFlag("fred")); 882 883 Value = Cmd.getFlagValue("fred"); 884 EXPECT_EQ(Value, ""); 885 886 CmdLine = Cmd.toString(); 887 EXPECT_EQ(CmdLine, makeCmdLine("", "")); 888 889 Cmd.addFlag("fred", "plugh"); 890 EXPECT_TRUE(Cmd.hasFlag("fred")); 891 892 Value = Cmd.getFlagValue("fred"); 893 EXPECT_EQ(Value, "plugh"); 894 895 CmdLine = Cmd.toString(); 896 EXPECT_EQ(CmdLine, makeCmdLine("-fred=plugh", "")); 897 898 Cmd.removeFlag("fred"); 899 EXPECT_FALSE(Cmd.hasFlag("fred")); 900 901 Value = Cmd.getFlagValue("fred"); 902 EXPECT_EQ(Value, ""); 903 904 CmdLine = Cmd.toString(); 905 EXPECT_EQ(CmdLine, makeCmdLine("", "")); 906 } 907 908 TEST(FuzzerCommand, SetOutput) { 909 Vector<std::string> ArgsToAdd; 910 makeCommandArgs(&ArgsToAdd); 911 Command Cmd(ArgsToAdd); 912 std::string CmdLine; 913 ASSERT_FALSE(Cmd.hasOutputFile()); 914 ASSERT_FALSE(Cmd.isOutAndErrCombined()); 915 916 Cmd.combineOutAndErr(true); 917 EXPECT_TRUE(Cmd.isOutAndErrCombined()); 918 919 CmdLine = Cmd.toString(); 920 EXPECT_EQ(CmdLine, makeCmdLine("", "2>&1")); 921 922 Cmd.combineOutAndErr(false); 923 EXPECT_FALSE(Cmd.isOutAndErrCombined()); 924 925 Cmd.setOutputFile("xyzzy"); 926 EXPECT_TRUE(Cmd.hasOutputFile()); 927 928 CmdLine = Cmd.toString(); 929 EXPECT_EQ(CmdLine, makeCmdLine("", ">xyzzy")); 930 931 Cmd.setOutputFile("thud"); 932 EXPECT_TRUE(Cmd.hasOutputFile()); 933 934 CmdLine = Cmd.toString(); 935 EXPECT_EQ(CmdLine, makeCmdLine("", ">thud")); 936 937 Cmd.combineOutAndErr(); 938 EXPECT_TRUE(Cmd.isOutAndErrCombined()); 939 940 CmdLine = Cmd.toString(); 941 EXPECT_EQ(CmdLine, makeCmdLine("", ">thud 2>&1")); 942 } 943 944 int main(int argc, char **argv) { 945 testing::InitGoogleTest(&argc, argv); 946 return RUN_ALL_TESTS(); 947 } 948