blob: b92e61877c6ca48b669a6ea205cd6f71cafd5c20 [file] [log] [blame]
Kostya Serebryany71672552015-01-30 23:26:57 +00001#include "FuzzerInternal.h"
2#include "gtest/gtest.h"
3#include <set>
4
Kostya Serebryany8ce74242015-08-01 01:42:51 +00005using namespace fuzzer;
6
Kostya Serebryany566bc5a2015-05-06 22:19:00 +00007// For now, have LLVMFuzzerTestOneInput just to make it link.
8// Later we may want to make unittests that actually call LLVMFuzzerTestOneInput.
9extern "C" void LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
Kostya Serebryany71672552015-01-30 23:26:57 +000010 abort();
11}
12
13TEST(Fuzzer, CrossOver) {
Kostya Serebryany404c69f2015-07-24 01:06:40 +000014 FuzzerRandomLibc Rand(0);
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000015 MutationDispatcher MD(Rand);
Kostya Serebryany71672552015-01-30 23:26:57 +000016 Unit A({0, 1, 2}), B({5, 6, 7});
17 Unit C;
18 Unit Expected[] = {
19 { 0 },
20 { 0, 1 },
21 { 0, 5 },
22 { 0, 1, 2 },
23 { 0, 1, 5 },
24 { 0, 5, 1 },
25 { 0, 5, 6 },
26 { 0, 1, 2, 5 },
27 { 0, 1, 5, 2 },
28 { 0, 1, 5, 6 },
29 { 0, 5, 1, 2 },
30 { 0, 5, 1, 6 },
31 { 0, 5, 6, 1 },
32 { 0, 5, 6, 7 },
33 { 0, 1, 2, 5, 6 },
34 { 0, 1, 5, 2, 6 },
35 { 0, 1, 5, 6, 2 },
36 { 0, 1, 5, 6, 7 },
37 { 0, 5, 1, 2, 6 },
38 { 0, 5, 1, 6, 2 },
39 { 0, 5, 1, 6, 7 },
40 { 0, 5, 6, 1, 2 },
41 { 0, 5, 6, 1, 7 },
42 { 0, 5, 6, 7, 1 },
43 { 0, 1, 2, 5, 6, 7 },
44 { 0, 1, 5, 2, 6, 7 },
45 { 0, 1, 5, 6, 2, 7 },
46 { 0, 1, 5, 6, 7, 2 },
47 { 0, 5, 1, 2, 6, 7 },
48 { 0, 5, 1, 6, 2, 7 },
49 { 0, 5, 1, 6, 7, 2 },
50 { 0, 5, 6, 1, 2, 7 },
51 { 0, 5, 6, 1, 7, 2 },
52 { 0, 5, 6, 7, 1, 2 }
53 };
54 for (size_t Len = 1; Len < 8; Len++) {
55 std::set<Unit> FoundUnits, ExpectedUnitsWitThisLength;
56 for (int Iter = 0; Iter < 3000; Iter++) {
Kostya Serebryanyf3424592015-05-22 22:35:31 +000057 C.resize(Len);
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000058 size_t NewSize = MD.CrossOver(A.data(), A.size(), B.data(), B.size(),
59 C.data(), C.size());
Kostya Serebryanyf3424592015-05-22 22:35:31 +000060 C.resize(NewSize);
Kostya Serebryany71672552015-01-30 23:26:57 +000061 FoundUnits.insert(C);
62 }
63 for (const Unit &U : Expected)
64 if (U.size() <= Len)
65 ExpectedUnitsWitThisLength.insert(U);
66 EXPECT_EQ(ExpectedUnitsWitThisLength, FoundUnits);
67 }
68}
Kostya Serebryany96eab652015-05-14 22:41:49 +000069
70TEST(Fuzzer, Hash) {
71 uint8_t A[] = {'a', 'b', 'c'};
72 fuzzer::Unit U(A, A + sizeof(A));
73 EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d", fuzzer::Hash(U));
74 U.push_back('d');
75 EXPECT_EQ("81fe8bfe87576c3ecb22426f8e57847382917acf", fuzzer::Hash(U));
76}
Kostya Serebryany8ce74242015-08-01 01:42:51 +000077
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000078typedef size_t (MutationDispatcher::*Mutator)(uint8_t *Data, size_t Size,
79 size_t MaxSize);
Kostya Serebryany8ce74242015-08-01 01:42:51 +000080
81void TestEraseByte(Mutator M, int NumIter) {
82 uint8_t REM0[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
83 uint8_t REM1[8] = {0x00, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
84 uint8_t REM2[8] = {0x00, 0x11, 0x33, 0x44, 0x55, 0x66, 0x77};
85 uint8_t REM3[8] = {0x00, 0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
86 uint8_t REM4[8] = {0x00, 0x11, 0x22, 0x33, 0x55, 0x66, 0x77};
87 uint8_t REM5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x66, 0x77};
88 uint8_t REM6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x77};
89 uint8_t REM7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
90 FuzzerRandomLibc Rand(0);
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000091 MutationDispatcher MD(Rand);
Kostya Serebryany8ce74242015-08-01 01:42:51 +000092 int FoundMask = 0;
93 for (int i = 0; i < NumIter; i++) {
94 uint8_t T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +000095 size_t NewSize = (MD.*M)(T, sizeof(T), sizeof(T));
Kostya Serebryany86a5fba2015-08-01 02:23:06 +000096 if (NewSize == 7 && !memcmp(REM0, T, 7)) FoundMask |= 1 << 0;
97 if (NewSize == 7 && !memcmp(REM1, T, 7)) FoundMask |= 1 << 1;
98 if (NewSize == 7 && !memcmp(REM2, T, 7)) FoundMask |= 1 << 2;
99 if (NewSize == 7 && !memcmp(REM3, T, 7)) FoundMask |= 1 << 3;
100 if (NewSize == 7 && !memcmp(REM4, T, 7)) FoundMask |= 1 << 4;
101 if (NewSize == 7 && !memcmp(REM5, T, 7)) FoundMask |= 1 << 5;
102 if (NewSize == 7 && !memcmp(REM6, T, 7)) FoundMask |= 1 << 6;
103 if (NewSize == 7 && !memcmp(REM7, T, 7)) FoundMask |= 1 << 7;
Kostya Serebryany8ce74242015-08-01 01:42:51 +0000104 }
105 EXPECT_EQ(FoundMask, 255);
106}
107
Kostya Serebryany7d211662015-09-04 00:12:11 +0000108TEST(FuzzerMutate, EraseByte1) {
109 TestEraseByte(&MutationDispatcher::Mutate_EraseByte, 100);
110}
111TEST(FuzzerMutate, EraseByte2) {
112 TestEraseByte(&MutationDispatcher::Mutate, 1000);
113}
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000114
115void TestInsertByte(Mutator M, int NumIter) {
116 FuzzerRandomLibc Rand(0);
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000117 MutationDispatcher MD(Rand);
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000118 int FoundMask = 0;
119 uint8_t INS0[8] = {0xF1, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
120 uint8_t INS1[8] = {0x00, 0xF2, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
121 uint8_t INS2[8] = {0x00, 0x11, 0xF3, 0x22, 0x33, 0x44, 0x55, 0x66};
122 uint8_t INS3[8] = {0x00, 0x11, 0x22, 0xF4, 0x33, 0x44, 0x55, 0x66};
123 uint8_t INS4[8] = {0x00, 0x11, 0x22, 0x33, 0xF5, 0x44, 0x55, 0x66};
124 uint8_t INS5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0xF6, 0x55, 0x66};
125 uint8_t INS6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0xF7, 0x66};
126 uint8_t INS7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xF8};
127 for (int i = 0; i < NumIter; i++) {
128 uint8_t T[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000129 size_t NewSize = (MD.*M)(T, 7, 8);
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000130 if (NewSize == 8 && !memcmp(INS0, T, 8)) FoundMask |= 1 << 0;
131 if (NewSize == 8 && !memcmp(INS1, T, 8)) FoundMask |= 1 << 1;
132 if (NewSize == 8 && !memcmp(INS2, T, 8)) FoundMask |= 1 << 2;
133 if (NewSize == 8 && !memcmp(INS3, T, 8)) FoundMask |= 1 << 3;
134 if (NewSize == 8 && !memcmp(INS4, T, 8)) FoundMask |= 1 << 4;
135 if (NewSize == 8 && !memcmp(INS5, T, 8)) FoundMask |= 1 << 5;
136 if (NewSize == 8 && !memcmp(INS6, T, 8)) FoundMask |= 1 << 6;
137 if (NewSize == 8 && !memcmp(INS7, T, 8)) FoundMask |= 1 << 7;
138 }
139 EXPECT_EQ(FoundMask, 255);
140}
141
Kostya Serebryany7d211662015-09-04 00:12:11 +0000142TEST(FuzzerMutate, InsertByte1) {
143 TestInsertByte(&MutationDispatcher::Mutate_InsertByte, 1 << 15);
144}
145TEST(FuzzerMutate, InsertByte2) {
146 TestInsertByte(&MutationDispatcher::Mutate, 1 << 17);
147}
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000148
149void TestChangeByte(Mutator M, int NumIter) {
150 FuzzerRandomLibc Rand(0);
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000151 MutationDispatcher MD(Rand);
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000152 int FoundMask = 0;
153 uint8_t CH0[8] = {0xF0, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
154 uint8_t CH1[8] = {0x00, 0xF1, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
155 uint8_t CH2[8] = {0x00, 0x11, 0xF2, 0x33, 0x44, 0x55, 0x66, 0x77};
156 uint8_t CH3[8] = {0x00, 0x11, 0x22, 0xF3, 0x44, 0x55, 0x66, 0x77};
157 uint8_t CH4[8] = {0x00, 0x11, 0x22, 0x33, 0xF4, 0x55, 0x66, 0x77};
158 uint8_t CH5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0xF5, 0x66, 0x77};
159 uint8_t CH6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0xF5, 0x77};
160 uint8_t CH7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xF7};
161 for (int i = 0; i < NumIter; i++) {
162 uint8_t T[9] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000163 size_t NewSize = (MD.*M)(T, 8, 9);
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000164 if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0;
165 if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1;
166 if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2;
167 if (NewSize == 8 && !memcmp(CH3, T, 8)) FoundMask |= 1 << 3;
168 if (NewSize == 8 && !memcmp(CH4, T, 8)) FoundMask |= 1 << 4;
169 if (NewSize == 8 && !memcmp(CH5, T, 8)) FoundMask |= 1 << 5;
170 if (NewSize == 8 && !memcmp(CH6, T, 8)) FoundMask |= 1 << 6;
171 if (NewSize == 8 && !memcmp(CH7, T, 8)) FoundMask |= 1 << 7;
172 }
173 EXPECT_EQ(FoundMask, 255);
174}
175
Kostya Serebryany7d211662015-09-04 00:12:11 +0000176TEST(FuzzerMutate, ChangeByte1) {
177 TestChangeByte(&MutationDispatcher::Mutate_ChangeByte, 1 << 15);
178}
179TEST(FuzzerMutate, ChangeByte2) {
180 TestChangeByte(&MutationDispatcher::Mutate, 1 << 17);
181}
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000182
183void TestChangeBit(Mutator M, int NumIter) {
184 FuzzerRandomLibc Rand(0);
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000185 MutationDispatcher MD(Rand);
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000186 int FoundMask = 0;
187 uint8_t CH0[8] = {0x01, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
188 uint8_t CH1[8] = {0x00, 0x13, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
189 uint8_t CH2[8] = {0x00, 0x11, 0x02, 0x33, 0x44, 0x55, 0x66, 0x77};
190 uint8_t CH3[8] = {0x00, 0x11, 0x22, 0x37, 0x44, 0x55, 0x66, 0x77};
191 uint8_t CH4[8] = {0x00, 0x11, 0x22, 0x33, 0x54, 0x55, 0x66, 0x77};
192 uint8_t CH5[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x54, 0x66, 0x77};
193 uint8_t CH6[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x76, 0x77};
194 uint8_t CH7[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xF7};
195 for (int i = 0; i < NumIter; i++) {
196 uint8_t T[9] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000197 size_t NewSize = (MD.*M)(T, 8, 9);
Kostya Serebryany86a5fba2015-08-01 02:23:06 +0000198 if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0;
199 if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1;
200 if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2;
201 if (NewSize == 8 && !memcmp(CH3, T, 8)) FoundMask |= 1 << 3;
202 if (NewSize == 8 && !memcmp(CH4, T, 8)) FoundMask |= 1 << 4;
203 if (NewSize == 8 && !memcmp(CH5, T, 8)) FoundMask |= 1 << 5;
204 if (NewSize == 8 && !memcmp(CH6, T, 8)) FoundMask |= 1 << 6;
205 if (NewSize == 8 && !memcmp(CH7, T, 8)) FoundMask |= 1 << 7;
206 }
207 EXPECT_EQ(FoundMask, 255);
208}
209
Kostya Serebryany7d211662015-09-04 00:12:11 +0000210TEST(FuzzerMutate, ChangeBit1) {
211 TestChangeBit(&MutationDispatcher::Mutate_ChangeBit, 1 << 16);
212}
213TEST(FuzzerMutate, ChangeBit2) {
214 TestChangeBit(&MutationDispatcher::Mutate, 1 << 18);
215}
Kostya Serebryanybf29ff22015-08-06 01:29:13 +0000216
217void TestShuffleBytes(Mutator M, int NumIter) {
218 FuzzerRandomLibc Rand(0);
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000219 MutationDispatcher MD(Rand);
Kostya Serebryanybf29ff22015-08-06 01:29:13 +0000220 int FoundMask = 0;
221 uint8_t CH0[7] = {0x00, 0x22, 0x11, 0x33, 0x44, 0x55, 0x66};
222 uint8_t CH1[7] = {0x11, 0x00, 0x33, 0x22, 0x44, 0x55, 0x66};
223 uint8_t CH2[7] = {0x00, 0x33, 0x11, 0x22, 0x44, 0x55, 0x66};
224 uint8_t CH3[7] = {0x00, 0x11, 0x22, 0x44, 0x55, 0x66, 0x33};
225 uint8_t CH4[7] = {0x00, 0x11, 0x22, 0x33, 0x55, 0x44, 0x66};
226 for (int i = 0; i < NumIter; i++) {
227 uint8_t T[7] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66};
Kostya Serebryanyec2dcb12015-09-03 21:24:19 +0000228 size_t NewSize = (MD.*M)(T, 7, 7);
Kostya Serebryanybf29ff22015-08-06 01:29:13 +0000229 if (NewSize == 7 && !memcmp(CH0, T, 7)) FoundMask |= 1 << 0;
230 if (NewSize == 7 && !memcmp(CH1, T, 7)) FoundMask |= 1 << 1;
231 if (NewSize == 7 && !memcmp(CH2, T, 7)) FoundMask |= 1 << 2;
232 if (NewSize == 7 && !memcmp(CH3, T, 7)) FoundMask |= 1 << 3;
233 if (NewSize == 7 && !memcmp(CH4, T, 7)) FoundMask |= 1 << 4;
234 }
235 EXPECT_EQ(FoundMask, 31);
236}
237
Kostya Serebryany7d211662015-09-04 00:12:11 +0000238TEST(FuzzerMutate, ShuffleBytes1) {
239 TestShuffleBytes(&MutationDispatcher::Mutate_ShuffleBytes, 1 << 15);
240}
241TEST(FuzzerMutate, ShuffleBytes2) {
242 TestShuffleBytes(&MutationDispatcher::Mutate, 1 << 16);
243}
244
245void TestAddWordFromDictionary(Mutator M, int NumIter) {
246 FuzzerRandomLibc Rand(0);
247 MutationDispatcher MD(Rand);
248 uint8_t Word1[4] = {0xAA, 0xBB, 0xCC, 0xDD};
249 uint8_t Word2[3] = {0xFF, 0xEE, 0xEF};
250 MD.AddWordToDictionary(Word1, sizeof(Word1));
251 MD.AddWordToDictionary(Word2, sizeof(Word2));
252 int FoundMask = 0;
253 uint8_t CH0[7] = {0x00, 0x11, 0x22, 0xAA, 0xBB, 0xCC, 0xDD};
254 uint8_t CH1[7] = {0x00, 0x11, 0xAA, 0xBB, 0xCC, 0xDD, 0x22};
255 uint8_t CH2[7] = {0x00, 0xAA, 0xBB, 0xCC, 0xDD, 0x11, 0x22};
256 uint8_t CH3[7] = {0xAA, 0xBB, 0xCC, 0xDD, 0x00, 0x11, 0x22};
257 uint8_t CH4[6] = {0x00, 0x11, 0x22, 0xFF, 0xEE, 0xEF};
258 uint8_t CH5[6] = {0x00, 0x11, 0xFF, 0xEE, 0xEF, 0x22};
259 uint8_t CH6[6] = {0x00, 0xFF, 0xEE, 0xEF, 0x11, 0x22};
260 uint8_t CH7[6] = {0xFF, 0xEE, 0xEF, 0x00, 0x11, 0x22};
261 for (int i = 0; i < NumIter; i++) {
262 uint8_t T[7] = {0x00, 0x11, 0x22};
263 size_t NewSize = (MD.*M)(T, 3, 7);
264 if (NewSize == 7 && !memcmp(CH0, T, 7)) FoundMask |= 1 << 0;
265 if (NewSize == 7 && !memcmp(CH1, T, 7)) FoundMask |= 1 << 1;
266 if (NewSize == 7 && !memcmp(CH2, T, 7)) FoundMask |= 1 << 2;
267 if (NewSize == 7 && !memcmp(CH3, T, 7)) FoundMask |= 1 << 3;
268 if (NewSize == 6 && !memcmp(CH4, T, 6)) FoundMask |= 1 << 4;
269 if (NewSize == 6 && !memcmp(CH5, T, 6)) FoundMask |= 1 << 5;
270 if (NewSize == 6 && !memcmp(CH6, T, 6)) FoundMask |= 1 << 6;
271 if (NewSize == 6 && !memcmp(CH7, T, 6)) FoundMask |= 1 << 7;
272 }
273 EXPECT_EQ(FoundMask, 255);
274}
275
276TEST(FuzzerMutate, AddWordFromDictionary1) {
277 TestAddWordFromDictionary(&MutationDispatcher::Mutate_AddWordFromDictionary,
278 1 << 15);
279}
280
281TEST(FuzzerMutate, AddWordFromDictionary2) {
282 TestAddWordFromDictionary(&MutationDispatcher::Mutate, 1 << 15);
283}
Kostya Serebryany9838b2b2015-09-03 20:23:46 +0000284
Kostya Serebryany25425ad2015-09-08 17:19:31 +0000285void TestChangeASCIIInteger(Mutator M, int NumIter) {
286 FuzzerRandomLibc Rand(0);
287 MutationDispatcher MD(Rand);
288
289 uint8_t CH0[8] = {'1', '2', '3', '4', '5', '6', '7', '7'};
290 uint8_t CH1[8] = {'1', '2', '3', '4', '5', '6', '7', '9'};
291 uint8_t CH2[8] = {'2', '4', '6', '9', '1', '3', '5', '6'};
292 uint8_t CH3[8] = {'0', '6', '1', '7', '2', '8', '3', '9'};
293 int FoundMask = 0;
294 for (int i = 0; i < NumIter; i++) {
295 uint8_t T[8] = {'1', '2', '3', '4', '5', '6', '7', '8'};
296 size_t NewSize = (MD.*M)(T, 8, 8);
297 /**/ if (NewSize == 8 && !memcmp(CH0, T, 8)) FoundMask |= 1 << 0;
298 else if (NewSize == 8 && !memcmp(CH1, T, 8)) FoundMask |= 1 << 1;
299 else if (NewSize == 8 && !memcmp(CH2, T, 8)) FoundMask |= 1 << 2;
300 else if (NewSize == 8 && !memcmp(CH3, T, 8)) FoundMask |= 1 << 3;
301 else if (NewSize == 8) FoundMask |= 1 << 4;
302 }
303 EXPECT_EQ(FoundMask, 31);
304}
305
306TEST(FuzzerMutate, ChangeASCIIInteger1) {
307 TestChangeASCIIInteger(&MutationDispatcher::Mutate_ChangeASCIIInteger,
308 1 << 15);
309}
310
311TEST(FuzzerMutate, ChangeASCIIInteger2) {
312 TestChangeASCIIInteger(&MutationDispatcher::Mutate, 1 << 15);
313}
314
315
Kostya Serebryany9838b2b2015-09-03 20:23:46 +0000316TEST(FuzzerDictionary, ParseOneDictionaryEntry) {
317 Unit U;
318 EXPECT_FALSE(ParseOneDictionaryEntry("", &U));
319 EXPECT_FALSE(ParseOneDictionaryEntry(" ", &U));
320 EXPECT_FALSE(ParseOneDictionaryEntry("\t ", &U));
321 EXPECT_FALSE(ParseOneDictionaryEntry(" \" ", &U));
322 EXPECT_FALSE(ParseOneDictionaryEntry(" zz\" ", &U));
323 EXPECT_FALSE(ParseOneDictionaryEntry(" \"zz ", &U));
324 EXPECT_FALSE(ParseOneDictionaryEntry(" \"\" ", &U));
325 EXPECT_TRUE(ParseOneDictionaryEntry("\"a\"", &U));
326 EXPECT_EQ(U, Unit({'a'}));
327 EXPECT_TRUE(ParseOneDictionaryEntry("\"abc\"", &U));
328 EXPECT_EQ(U, Unit({'a', 'b', 'c'}));
329 EXPECT_TRUE(ParseOneDictionaryEntry("abc=\"abc\"", &U));
330 EXPECT_EQ(U, Unit({'a', 'b', 'c'}));
331 EXPECT_FALSE(ParseOneDictionaryEntry("\"\\\"", &U));
332 EXPECT_TRUE(ParseOneDictionaryEntry("\"\\\\\"", &U));
333 EXPECT_EQ(U, Unit({'\\'}));
334 EXPECT_TRUE(ParseOneDictionaryEntry("\"\\xAB\"", &U));
335 EXPECT_EQ(U, Unit({0xAB}));
336 EXPECT_TRUE(ParseOneDictionaryEntry("\"\\xABz\\xDE\"", &U));
337 EXPECT_EQ(U, Unit({0xAB, 'z', 0xDE}));
338 EXPECT_TRUE(ParseOneDictionaryEntry("\"#\"", &U));
339 EXPECT_EQ(U, Unit({'#'}));
340 EXPECT_TRUE(ParseOneDictionaryEntry("\"\\\"\"", &U));
341 EXPECT_EQ(U, Unit({'"'}));
342}
343
344TEST(FuzzerDictionary, ParseDictionaryFile) {
345 std::vector<Unit> Units;
346 EXPECT_FALSE(ParseDictionaryFile("zzz\n", &Units));
347 EXPECT_FALSE(ParseDictionaryFile("", &Units));
348 EXPECT_TRUE(ParseDictionaryFile("\n", &Units));
349 EXPECT_EQ(Units.size(), 0U);
350 EXPECT_TRUE(ParseDictionaryFile("#zzzz a b c d\n", &Units));
351 EXPECT_EQ(Units.size(), 0U);
352 EXPECT_TRUE(ParseDictionaryFile(" #zzzz\n", &Units));
353 EXPECT_EQ(Units.size(), 0U);
354 EXPECT_TRUE(ParseDictionaryFile(" #zzzz\n", &Units));
355 EXPECT_EQ(Units.size(), 0U);
356 EXPECT_TRUE(ParseDictionaryFile(" #zzzz\naaa=\"aa\"", &Units));
357 EXPECT_EQ(Units, std::vector<Unit>({Unit({'a', 'a'})}));
358 EXPECT_TRUE(
359 ParseDictionaryFile(" #zzzz\naaa=\"aa\"\n\nabc=\"abc\"", &Units));
360 EXPECT_EQ(Units,
361 std::vector<Unit>({Unit({'a', 'a'}), Unit({'a', 'b', 'c'})}));
362}
Kostya Serebryany9e48cda2015-12-04 22:29:39 +0000363
364TEST(FuzzerUtil, Base64) {
365 EXPECT_EQ("", Base64({}));
366 EXPECT_EQ("YQ==", Base64({'a'}));
367 EXPECT_EQ("eA==", Base64({'x'}));
368 EXPECT_EQ("YWI=", Base64({'a', 'b'}));
369 EXPECT_EQ("eHk=", Base64({'x', 'y'}));
370 EXPECT_EQ("YWJj", Base64({'a', 'b', 'c'}));
371 EXPECT_EQ("eHl6", Base64({'x', 'y', 'z'}));
372 EXPECT_EQ("YWJjeA==", Base64({'a', 'b', 'c', 'x'}));
373 EXPECT_EQ("YWJjeHk=", Base64({'a', 'b', 'c', 'x', 'y'}));
374 EXPECT_EQ("YWJjeHl6", Base64({'a', 'b', 'c', 'x', 'y', 'z'}));
375}