[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / safe_browsing / protocol_parser_unittest.cc
blob6c29c06a9fecf77d915c4388b2cfb9426ede6898
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // Program to test the SafeBrowsing protocol parsing v2.1.
7 #include <map>
8 #include <string>
10 #include "base/strings/stringprintf.h"
11 #include "chrome/browser/safe_browsing/protocol_parser.h"
12 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 // Test parsing one add chunk.
16 TEST(SafeBrowsingProtocolParsingTest, TestAddChunk) {
17 std::string add_chunk("a:1:4:35\naaaax1111\0032222333344447777\00288889999");
18 add_chunk[13] = '\0';
20 // Run the parse.
21 SafeBrowsingProtocolParser parser;
22 SBChunkList chunks;
23 bool result = parser.ParseChunk(
24 safe_browsing_util::kMalwareList,
25 add_chunk.data(),
26 static_cast<int>(add_chunk.length()),
27 &chunks);
28 EXPECT_TRUE(result);
29 EXPECT_EQ(chunks.size(), 1U);
30 EXPECT_EQ(chunks[0].chunk_number, 1);
31 EXPECT_EQ(chunks[0].hosts.size(), 3U);
33 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161U);
34 SBEntry* entry = chunks[0].hosts[0].entry;
35 EXPECT_TRUE(entry->IsAdd());
36 EXPECT_TRUE(entry->IsPrefix());
37 EXPECT_EQ(entry->prefix_count(), 0);
39 EXPECT_EQ(chunks[0].hosts[1].host, 0x31313131U);
40 entry = chunks[0].hosts[1].entry;
41 EXPECT_TRUE(entry->IsAdd());
42 EXPECT_TRUE(entry->IsPrefix());
43 EXPECT_EQ(entry->prefix_count(), 3);
44 EXPECT_EQ(entry->PrefixAt(0), 0x32323232U);
45 EXPECT_EQ(entry->PrefixAt(1), 0x33333333U);
46 EXPECT_EQ(entry->PrefixAt(2), 0x34343434U);
48 EXPECT_EQ(chunks[0].hosts[2].host, 0x37373737U);
49 entry = chunks[0].hosts[2].entry;
50 EXPECT_TRUE(entry->IsAdd());
51 EXPECT_TRUE(entry->IsPrefix());
52 EXPECT_EQ(entry->prefix_count(), 2);
53 EXPECT_EQ(entry->PrefixAt(0), 0x38383838U);
54 EXPECT_EQ(entry->PrefixAt(1), 0x39393939U);
57 // Test parsing one add chunk with full hashes.
58 TEST(SafeBrowsingProtocolParsingTest, TestAddFullChunk) {
59 std::string add_chunk("a:1:32:69\naaaa");
60 add_chunk.push_back(2);
62 SBFullHash full_hash1, full_hash2;
63 for (int i = 0; i < 32; ++i) {
64 full_hash1.full_hash[i] = i % 2 ? 1 : 2;
65 full_hash2.full_hash[i] = i % 2 ? 3 : 4;
68 add_chunk.append(full_hash1.full_hash, 32);
69 add_chunk.append(full_hash2.full_hash, 32);
71 // Run the parse.
72 SafeBrowsingProtocolParser parser;
73 SBChunkList chunks;
74 bool result = parser.ParseChunk(
75 safe_browsing_util::kMalwareList,
76 add_chunk.data(),
77 static_cast<int>(add_chunk.length()),
78 &chunks);
79 EXPECT_TRUE(result);
80 EXPECT_EQ(chunks.size(), 1U);
81 EXPECT_EQ(chunks[0].chunk_number, 1);
82 EXPECT_EQ(chunks[0].hosts.size(), 1U);
84 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161U);
85 SBEntry* entry = chunks[0].hosts[0].entry;
86 EXPECT_TRUE(entry->IsAdd());
87 EXPECT_FALSE(entry->IsPrefix());
88 EXPECT_EQ(entry->prefix_count(), 2);
89 EXPECT_TRUE(SBFullHashEqual(entry->FullHashAt(0), full_hash1));
90 EXPECT_TRUE(SBFullHashEqual(entry->FullHashAt(1), full_hash2));
93 // Test parsing multiple add chunks. We'll use the same chunk as above, and add
94 // one more after it.
95 TEST(SafeBrowsingProtocolParsingTest, TestAddChunks) {
96 std::string add_chunk("a:1:4:35\naaaax1111\0032222333344447777\00288889999"
97 "a:2:4:13\n5555\002ppppgggg");
98 add_chunk[13] = '\0';
100 // Run the parse.
101 SafeBrowsingProtocolParser parser;
102 SBChunkList chunks;
103 bool result = parser.ParseChunk(
104 safe_browsing_util::kMalwareList,
105 add_chunk.data(),
106 static_cast<int>(add_chunk.length()),
107 &chunks);
108 EXPECT_TRUE(result);
109 EXPECT_EQ(chunks.size(), 2U);
110 EXPECT_EQ(chunks[0].chunk_number, 1);
111 EXPECT_EQ(chunks[0].hosts.size(), 3U);
113 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161U);
114 SBEntry* entry = chunks[0].hosts[0].entry;
115 EXPECT_TRUE(entry->IsAdd());
116 EXPECT_TRUE(entry->IsPrefix());
117 EXPECT_EQ(entry->prefix_count(), 0);
119 EXPECT_EQ(chunks[0].hosts[1].host, 0x31313131U);
120 entry = chunks[0].hosts[1].entry;
121 EXPECT_TRUE(entry->IsAdd());
122 EXPECT_TRUE(entry->IsPrefix());
123 EXPECT_EQ(entry->prefix_count(), 3);
124 EXPECT_EQ(entry->PrefixAt(0), 0x32323232U);
125 EXPECT_EQ(entry->PrefixAt(1), 0x33333333U);
126 EXPECT_EQ(entry->PrefixAt(2), 0x34343434U);
128 EXPECT_EQ(chunks[0].hosts[2].host, 0x37373737U);
129 entry = chunks[0].hosts[2].entry;
130 EXPECT_TRUE(entry->IsAdd());
131 EXPECT_TRUE(entry->IsPrefix());
132 EXPECT_EQ(entry->prefix_count(), 2);
133 EXPECT_EQ(entry->PrefixAt(0), 0x38383838U);
134 EXPECT_EQ(entry->PrefixAt(1), 0x39393939U);
137 EXPECT_EQ(chunks[1].chunk_number, 2);
138 EXPECT_EQ(chunks[1].hosts.size(), 1U);
140 EXPECT_EQ(chunks[1].hosts[0].host, 0x35353535U);
141 entry = chunks[1].hosts[0].entry;
142 EXPECT_TRUE(entry->IsAdd());
143 EXPECT_TRUE(entry->IsPrefix());
144 EXPECT_EQ(entry->prefix_count(), 2);
145 EXPECT_EQ(entry->PrefixAt(0), 0x70707070U);
146 EXPECT_EQ(entry->PrefixAt(1), 0x67676767U);
149 // Test parsing one add chunk where a hostkey spans several entries.
150 TEST(SafeBrowsingProtocolParsingTest, TestAddBigChunk) {
151 std::string add_chunk("a:1:4:1050\naaaaX");
152 add_chunk[add_chunk.size() - 1] |= 0xFF;
153 for (int i = 0; i < 255; ++i)
154 add_chunk.append(base::StringPrintf("%04d", i));
156 add_chunk.append("aaaa");
157 add_chunk.push_back(5);
158 for (int i = 0; i < 5; ++i)
159 add_chunk.append(base::StringPrintf("001%d", i));
161 SafeBrowsingProtocolParser parser;
162 SBChunkList chunks;
163 bool result = parser.ParseChunk(
164 safe_browsing_util::kMalwareList,
165 add_chunk.data(),
166 static_cast<int>(add_chunk.length()),
167 &chunks);
168 EXPECT_TRUE(result);
169 EXPECT_EQ(chunks.size(), 1U);
170 EXPECT_EQ(chunks[0].chunk_number, 1);
172 EXPECT_EQ(chunks[0].hosts.size(), 2U);
174 const SBChunkHost& host0 = chunks[0].hosts[0];
175 EXPECT_EQ(host0.host, 0x61616161U);
176 EXPECT_EQ(host0.entry->prefix_count(), 255);
178 const SBChunkHost& host1 = chunks[0].hosts[1];
179 EXPECT_EQ(host1.host, 0x61616161U);
180 EXPECT_EQ(host1.entry->prefix_count(), 5);
183 // Test to make sure we could deal with truncated goog-*-digestvar chunk.
184 TEST(SafeBrowsingProtocolParsingTest, TestTruncatedHashChunk) {
185 // This chunk delares there are 4 prefixes but actually only contains 2.
186 const char add_chunk[] = "a:1:4:16\n11112222";
187 SafeBrowsingProtocolParser parser;
188 SBChunkList chunks;
189 bool result = parser.ParseChunk(safe_browsing_util::kExtensionBlacklist,
190 add_chunk,
191 static_cast<int>(sizeof(add_chunk)),
192 &chunks);
193 EXPECT_FALSE(result);
194 EXPECT_EQ(chunks.size(), 0U);
197 // Test to make sure we could deal with truncated malwarelist chunk.
198 TEST(SafeBrowsingProtocolParsingTest, TestTruncatedUrlHashChunk) {
199 // This chunk delares there are 4 prefixes but actually only contains 2.
200 const char add_chunk[] = "a:1:4:21\naaaa\00411112222";
201 SafeBrowsingProtocolParser parser;
202 SBChunkList chunks;
204 // For safe_browsing_util::kMalwareList.
205 bool result = parser.ParseChunk(safe_browsing_util::kMalwareList,
206 add_chunk,
207 static_cast<int>(sizeof(add_chunk)),
208 &chunks);
209 EXPECT_FALSE(result);
210 EXPECT_EQ(chunks.size(), 0U);
212 // For safe_browsing_util::kPhishingList.
213 result = parser.ParseChunk(safe_browsing_util::kPhishingList,
214 add_chunk,
215 static_cast<int>(sizeof(add_chunk)),
216 &chunks);
217 EXPECT_FALSE(result);
218 EXPECT_EQ(chunks.size(), 0U);
220 // For safe_browsing_util::kBinUrlList.
221 result = parser.ParseChunk(safe_browsing_util::kBinUrlList,
222 add_chunk,
223 static_cast<int>(sizeof(add_chunk)),
224 &chunks);
225 EXPECT_FALSE(result);
226 EXPECT_EQ(chunks.size(), 0U);
229 // Test to verify handling of a truncated chunk header.
230 TEST(SafeBrowsingProtocolParsingTest, TestTruncatedHeader) {
231 std::string truncated_chunks("a:1:4:0\na:");
233 // Run the parser.
234 SafeBrowsingProtocolParser parser;
235 SBChunkList chunks;
236 bool result = parser.ParseChunk(
237 safe_browsing_util::kMalwareList,
238 truncated_chunks.data(),
239 static_cast<int>(truncated_chunks.length()),
240 &chunks);
241 EXPECT_FALSE(result);
244 // Test to verify handling of a negative chunk length.
245 TEST(SafeBrowsingProtocolParsingTest, TestNegativeChunkLength) {
246 std::string negative_chunk_length("a:1:4:-100000\naaaabbbbcc");
248 // Run the parser.
249 SafeBrowsingProtocolParser parser;
250 SBChunkList chunks;
251 bool result = parser.ParseChunk(
252 safe_browsing_util::kDownloadWhiteList,
253 negative_chunk_length.data(),
254 static_cast<int>(negative_chunk_length.length()),
255 &chunks);
256 EXPECT_FALSE(result);
259 // Test parsing one sub chunk.
260 TEST(SafeBrowsingProtocolParsingTest, TestSubChunk) {
261 std::string sub_chunk("s:9:4:59\naaaaxkkkk1111\003"
262 "zzzz2222zzzz3333zzzz4444"
263 "7777\002yyyy8888yyyy9999");
264 sub_chunk[13] = '\0';
266 // Run the parse.
267 SafeBrowsingProtocolParser parser;
268 SBChunkList chunks;
269 bool result = parser.ParseChunk(
270 safe_browsing_util::kMalwareList,
271 sub_chunk.data(),
272 static_cast<int>(sub_chunk.length()),
273 &chunks);
274 EXPECT_TRUE(result);
275 EXPECT_EQ(chunks.size(), 1U);
276 EXPECT_EQ(chunks[0].chunk_number, 9);
277 EXPECT_EQ(chunks[0].hosts.size(), 3U);
279 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161U);
280 SBEntry* entry = chunks[0].hosts[0].entry;
281 EXPECT_TRUE(entry->IsSub());
282 EXPECT_TRUE(entry->IsPrefix());
283 EXPECT_EQ(entry->chunk_id(), 0x6b6b6b6b);
284 EXPECT_EQ(entry->prefix_count(), 0);
286 EXPECT_EQ(chunks[0].hosts[1].host, 0x31313131U);
287 entry = chunks[0].hosts[1].entry;
288 EXPECT_TRUE(entry->IsSub());
289 EXPECT_TRUE(entry->IsPrefix());
290 EXPECT_EQ(entry->prefix_count(), 3);
291 EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x7a7a7a7a);
292 EXPECT_EQ(entry->PrefixAt(0), 0x32323232U);
293 EXPECT_EQ(entry->ChunkIdAtPrefix(1), 0x7a7a7a7a);
294 EXPECT_EQ(entry->PrefixAt(1), 0x33333333U);
295 EXPECT_EQ(entry->ChunkIdAtPrefix(2), 0x7a7a7a7a);
296 EXPECT_EQ(entry->PrefixAt(2), 0x34343434U);
298 EXPECT_EQ(chunks[0].hosts[2].host, 0x37373737U);
299 entry = chunks[0].hosts[2].entry;
300 EXPECT_TRUE(entry->IsSub());
301 EXPECT_TRUE(entry->IsPrefix());
302 EXPECT_EQ(entry->prefix_count(), 2);
303 EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x79797979);
304 EXPECT_EQ(entry->PrefixAt(0), 0x38383838U);
305 EXPECT_EQ(entry->ChunkIdAtPrefix(1), 0x79797979);
306 EXPECT_EQ(entry->PrefixAt(1), 0x39393939U);
309 // Test parsing one sub chunk with full hashes.
310 TEST(SafeBrowsingProtocolParsingTest, TestSubFullChunk) {
311 std::string sub_chunk("s:1:32:77\naaaa");
312 sub_chunk.push_back(2);
314 SBFullHash full_hash1, full_hash2;
315 for (int i = 0; i < 32; ++i) {
316 full_hash1.full_hash[i] = i % 2 ? 1 : 2;
317 full_hash2.full_hash[i] = i % 2 ? 3 : 4;
320 sub_chunk.append("yyyy");
321 sub_chunk.append(full_hash1.full_hash, 32);
322 sub_chunk.append("zzzz");
323 sub_chunk.append(full_hash2.full_hash, 32);
325 // Run the parse.
326 SafeBrowsingProtocolParser parser;
327 SBChunkList chunks;
328 bool result = parser.ParseChunk(
329 safe_browsing_util::kMalwareList,
330 sub_chunk.data(),
331 static_cast<int>(sub_chunk.length()),
332 &chunks);
333 EXPECT_TRUE(result);
334 EXPECT_EQ(chunks.size(), 1U);
335 EXPECT_EQ(chunks[0].chunk_number, 1);
336 EXPECT_EQ(chunks[0].hosts.size(), 1U);
338 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161U);
339 SBEntry* entry = chunks[0].hosts[0].entry;
340 EXPECT_TRUE(entry->IsSub());
341 EXPECT_FALSE(entry->IsPrefix());
342 EXPECT_EQ(entry->prefix_count(), 2);
343 EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x79797979);
344 EXPECT_TRUE(SBFullHashEqual(entry->FullHashAt(0), full_hash1));
345 EXPECT_EQ(entry->ChunkIdAtPrefix(1), 0x7a7a7a7a);
346 EXPECT_TRUE(SBFullHashEqual(entry->FullHashAt(1), full_hash2));
349 // Test parsing the SafeBrowsing update response.
350 TEST(SafeBrowsingProtocolParsingTest, TestChunkDelete) {
351 std::string add_del("n:1700\ni:phishy\nad:1-7,43-597,44444,99999\n"
352 "i:malware\nsd:21-27,42,171717\n");
354 SafeBrowsingProtocolParser parser;
355 int next_query_sec = 0;
356 bool reset = false;
357 std::vector<SBChunkDelete> deletes;
358 std::vector<ChunkUrl> urls;
359 EXPECT_TRUE(parser.ParseUpdate(add_del.data(),
360 static_cast<int>(add_del.length()),
361 &next_query_sec, &reset, &deletes, &urls));
363 EXPECT_TRUE(urls.empty());
364 EXPECT_FALSE(reset);
365 EXPECT_EQ(next_query_sec, 1700);
366 EXPECT_EQ(deletes.size(), 2U);
368 EXPECT_EQ(deletes[0].chunk_del.size(), 4U);
369 EXPECT_TRUE(deletes[0].chunk_del[0] == ChunkRange(1, 7));
370 EXPECT_TRUE(deletes[0].chunk_del[1] == ChunkRange(43, 597));
371 EXPECT_TRUE(deletes[0].chunk_del[2] == ChunkRange(44444));
372 EXPECT_TRUE(deletes[0].chunk_del[3] == ChunkRange(99999));
374 EXPECT_EQ(deletes[1].chunk_del.size(), 3U);
375 EXPECT_TRUE(deletes[1].chunk_del[0] == ChunkRange(21, 27));
376 EXPECT_TRUE(deletes[1].chunk_del[1] == ChunkRange(42));
377 EXPECT_TRUE(deletes[1].chunk_del[2] == ChunkRange(171717));
379 // An update response with missing list name.
381 next_query_sec = 0;
382 deletes.clear();
383 urls.clear();
384 add_del = "n:1700\nad:1-7,43-597,44444,99999\ni:malware\nsd:4,21-27171717\n";
385 EXPECT_FALSE(parser.ParseUpdate(add_del.data(),
386 static_cast<int>(add_del.length()),
387 &next_query_sec, &reset, &deletes, &urls));
390 // Test parsing the SafeBrowsing update response.
391 TEST(SafeBrowsingProtocolParsingTest, TestRedirects) {
392 std::string redirects("i:goog-malware-shavar\n"
393 "u:cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_1\n"
394 "u:cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_2\n"
395 "u:cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_3\n"
396 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8641-8800:8641-8689,"
397 "8691-8731,8733-8786\n");
399 SafeBrowsingProtocolParser parser;
400 int next_query_sec = 0;
401 bool reset = false;
402 std::vector<SBChunkDelete> deletes;
403 std::vector<ChunkUrl> urls;
404 EXPECT_TRUE(parser.ParseUpdate(redirects.data(),
405 static_cast<int>(redirects.length()),
406 &next_query_sec, &reset, &deletes, &urls));
408 EXPECT_FALSE(reset);
409 EXPECT_EQ(urls.size(), 4U);
410 EXPECT_EQ(urls[0].url,
411 "cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_1");
412 EXPECT_EQ(urls[1].url,
413 "cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_2");
414 EXPECT_EQ(urls[2].url,
415 "cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_3");
416 EXPECT_EQ(urls[3].url,
417 "s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8641-8800:8641-8689,"
418 "8691-8731,8733-8786");
419 EXPECT_EQ(next_query_sec, 0);
420 EXPECT_TRUE(deletes.empty());
423 // Test parsing various SafeBrowsing protocol headers.
424 TEST(SafeBrowsingProtocolParsingTest, TestNextQueryTime) {
425 std::string headers("n:1800\ni:goog-white-shavar\n");
426 SafeBrowsingProtocolParser parser;
427 int next_query_sec = 0;
428 bool reset = false;
429 std::vector<SBChunkDelete> deletes;
430 std::vector<ChunkUrl> urls;
431 EXPECT_TRUE(parser.ParseUpdate(headers.data(),
432 static_cast<int>(headers.length()),
433 &next_query_sec, &reset, &deletes, &urls));
435 EXPECT_EQ(next_query_sec, 1800);
436 EXPECT_FALSE(reset);
437 EXPECT_TRUE(deletes.empty());
438 EXPECT_TRUE(urls.empty());
441 // Test parsing data from a GetHashRequest
442 TEST(SafeBrowsingProtocolParsingTest, TestGetHash) {
443 std::string get_hash("goog-phish-shavar:19:96\n"
444 "00112233445566778899aabbccddeeff"
445 "00001111222233334444555566667777"
446 "ffffeeeeddddccccbbbbaaaa99998888");
447 std::vector<SBFullHashResult> full_hashes;
448 SafeBrowsingProtocolParser parser;
449 EXPECT_TRUE(parser.ParseGetHash(get_hash.data(),
450 static_cast<int>(get_hash.length()),
451 &full_hashes));
453 EXPECT_EQ(full_hashes.size(), 3U);
454 EXPECT_EQ(memcmp(&full_hashes[0].hash,
455 "00112233445566778899aabbccddeeff",
456 sizeof(SBFullHash)), 0);
457 EXPECT_EQ(full_hashes[0].list_id, safe_browsing_util::PHISH);
458 EXPECT_EQ(memcmp(&full_hashes[1].hash,
459 "00001111222233334444555566667777",
460 sizeof(SBFullHash)), 0);
461 EXPECT_EQ(full_hashes[1].list_id, safe_browsing_util::PHISH);
462 EXPECT_EQ(memcmp(&full_hashes[2].hash,
463 "ffffeeeeddddccccbbbbaaaa99998888",
464 sizeof(SBFullHash)), 0);
465 EXPECT_EQ(full_hashes[2].list_id, safe_browsing_util::PHISH);
467 // Test multiple lists in the GetHash results.
468 std::string get_hash2("goog-phish-shavar:19:32\n"
469 "00112233445566778899aabbccddeeff"
470 "goog-malware-shavar:19:64\n"
471 "cafebeefcafebeefdeaddeaddeaddead"
472 "zzzzyyyyxxxxwwwwvvvvuuuuttttssss");
473 EXPECT_TRUE(parser.ParseGetHash(get_hash2.data(),
474 static_cast<int>(get_hash2.length()),
475 &full_hashes));
477 EXPECT_EQ(full_hashes.size(), 3U);
478 EXPECT_EQ(memcmp(&full_hashes[0].hash,
479 "00112233445566778899aabbccddeeff",
480 sizeof(SBFullHash)), 0);
481 EXPECT_EQ(full_hashes[0].list_id, safe_browsing_util::PHISH);
482 EXPECT_EQ(memcmp(&full_hashes[1].hash,
483 "cafebeefcafebeefdeaddeaddeaddead",
484 sizeof(SBFullHash)), 0);
485 EXPECT_EQ(full_hashes[1].list_id, safe_browsing_util::MALWARE);
486 EXPECT_EQ(memcmp(&full_hashes[2].hash,
487 "zzzzyyyyxxxxwwwwvvvvuuuuttttssss",
488 sizeof(SBFullHash)), 0);
489 EXPECT_EQ(full_hashes[2].list_id, safe_browsing_util::MALWARE);
492 TEST(SafeBrowsingProtocolParsingTest, TestGetHashWithUnknownList) {
493 std::string hash_response = "goog-phish-shavar:1:32\n"
494 "12345678901234567890123456789012"
495 "googpub-phish-shavar:19:32\n"
496 "09876543210987654321098765432109";
497 std::vector<SBFullHashResult> full_hashes;
498 SafeBrowsingProtocolParser parser;
499 EXPECT_TRUE(parser.ParseGetHash(hash_response.data(),
500 hash_response.size(),
501 &full_hashes));
503 EXPECT_EQ(full_hashes.size(), 1U);
504 EXPECT_EQ(memcmp("12345678901234567890123456789012",
505 &full_hashes[0].hash, sizeof(SBFullHash)), 0);
506 EXPECT_EQ(full_hashes[0].list_id, safe_browsing_util::PHISH);
508 hash_response += "goog-malware-shavar:7:32\n"
509 "abcdefghijklmnopqrstuvwxyz123457";
510 full_hashes.clear();
511 EXPECT_TRUE(parser.ParseGetHash(hash_response.data(),
512 hash_response.size(),
513 &full_hashes));
515 EXPECT_EQ(full_hashes.size(), 2U);
516 EXPECT_EQ(memcmp("12345678901234567890123456789012",
517 &full_hashes[0].hash, sizeof(SBFullHash)), 0);
518 EXPECT_EQ(full_hashes[0].list_id, safe_browsing_util::PHISH);
519 EXPECT_EQ(memcmp("abcdefghijklmnopqrstuvwxyz123457",
520 &full_hashes[1].hash, sizeof(SBFullHash)), 0);
521 EXPECT_EQ(full_hashes[1].list_id, safe_browsing_util::MALWARE);
524 TEST(SafeBrowsingProtocolParsingTest, TestFormatHash) {
525 SafeBrowsingProtocolParser parser;
526 std::vector<SBPrefix> prefixes;
527 std::string get_hash;
529 prefixes.push_back(0x34333231);
530 prefixes.push_back(0x64636261);
531 prefixes.push_back(0x73727170);
533 parser.FormatGetHash(prefixes, &get_hash);
534 EXPECT_EQ(get_hash, "4:12\n1234abcdpqrs");
537 TEST(SafeBrowsingProtocolParsingTest, TestReset) {
538 SafeBrowsingProtocolParser parser;
539 std::string update("n:1800\ni:phishy\nr:pleasereset\n");
541 bool reset = false;
542 int next_update = -1;
543 std::vector<SBChunkDelete> deletes;
544 std::vector<ChunkUrl> urls;
545 EXPECT_TRUE(parser.ParseUpdate(update.data(),
546 static_cast<int>(update.size()),
547 &next_update, &reset, &deletes, &urls));
548 EXPECT_TRUE(reset);
551 // The SafeBrowsing service will occasionally send zero length chunks so that
552 // client requests will have longer contiguous chunk number ranges, and thus
553 // reduce the request size.
554 TEST(SafeBrowsingProtocolParsingTest, TestZeroSizeAddChunk) {
555 std::string add_chunk("a:1:4:0\n");
556 SafeBrowsingProtocolParser parser;
557 SBChunkList chunks;
559 bool result = parser.ParseChunk(
560 safe_browsing_util::kMalwareList,
561 add_chunk.data(),
562 static_cast<int>(add_chunk.length()),
563 &chunks);
564 EXPECT_TRUE(result);
565 EXPECT_EQ(chunks.size(), 1U);
566 EXPECT_EQ(chunks[0].chunk_number, 1);
567 EXPECT_EQ(chunks[0].hosts.size(), 0U);
569 // Now test a zero size chunk in between normal chunks.
570 chunks.clear();
571 std::string add_chunks("a:1:4:18\n1234\001abcd5678\001wxyz"
572 "a:2:4:0\n"
573 "a:3:4:9\ncafe\001beef");
574 result = parser.ParseChunk(
575 safe_browsing_util::kMalwareList,
576 add_chunks.data(),
577 static_cast<int>(add_chunks.length()),
578 &chunks);
579 EXPECT_TRUE(result);
580 EXPECT_EQ(chunks.size(), 3U);
582 // See that each chunk has the right content.
583 EXPECT_EQ(chunks[0].chunk_number, 1);
584 EXPECT_EQ(chunks[0].hosts.size(), 2U);
585 EXPECT_EQ(chunks[0].hosts[0].host, 0x34333231U);
586 EXPECT_EQ(chunks[0].hosts[0].entry->PrefixAt(0), 0x64636261U);
587 EXPECT_EQ(chunks[0].hosts[1].host, 0x38373635U);
588 EXPECT_EQ(chunks[0].hosts[1].entry->PrefixAt(0), 0x7a797877U);
590 EXPECT_EQ(chunks[1].chunk_number, 2);
591 EXPECT_EQ(chunks[1].hosts.size(), 0U);
593 EXPECT_EQ(chunks[2].chunk_number, 3);
594 EXPECT_EQ(chunks[2].hosts.size(), 1U);
595 EXPECT_EQ(chunks[2].hosts[0].host, 0x65666163U);
596 EXPECT_EQ(chunks[2].hosts[0].entry->PrefixAt(0), 0x66656562U);
599 // Test parsing a zero sized sub chunk.
600 TEST(SafeBrowsingProtocolParsingTest, TestZeroSizeSubChunk) {
601 std::string sub_chunk("s:9:4:0\n");
602 SafeBrowsingProtocolParser parser;
603 SBChunkList chunks;
605 bool result = parser.ParseChunk(
606 safe_browsing_util::kMalwareList,
607 sub_chunk.data(),
608 static_cast<int>(sub_chunk.length()),
609 &chunks);
610 EXPECT_TRUE(result);
611 EXPECT_EQ(chunks.size(), 1U);
612 EXPECT_EQ(chunks[0].chunk_number, 9);
613 EXPECT_EQ(chunks[0].hosts.size(), 0U);
614 chunks.clear();
616 // Test parsing a zero sized sub chunk mixed in with content carrying chunks.
617 std::string sub_chunks("s:1:4:9\nabcdxwxyz"
618 "s:2:4:0\n"
619 "s:3:4:26\nefgh\0011234pqrscafe\0015678lmno");
620 sub_chunks[12] = '\0';
622 result = parser.ParseChunk(
623 safe_browsing_util::kMalwareList,
624 sub_chunks.data(),
625 static_cast<int>(sub_chunks.length()),
626 &chunks);
627 EXPECT_TRUE(result);
629 EXPECT_EQ(chunks[0].chunk_number, 1);
630 EXPECT_EQ(chunks[0].hosts.size(), 1U);
631 EXPECT_EQ(chunks[0].hosts[0].host, 0x64636261U);
632 EXPECT_EQ(chunks[0].hosts[0].entry->prefix_count(), 0);
634 EXPECT_EQ(chunks[1].chunk_number, 2);
635 EXPECT_EQ(chunks[1].hosts.size(), 0U);
637 EXPECT_EQ(chunks[2].chunk_number, 3);
638 EXPECT_EQ(chunks[2].hosts.size(), 2U);
639 EXPECT_EQ(chunks[2].hosts[0].host, 0x68676665U);
640 EXPECT_EQ(chunks[2].hosts[0].entry->prefix_count(), 1);
641 EXPECT_EQ(chunks[2].hosts[0].entry->PrefixAt(0), 0x73727170U);
642 EXPECT_EQ(chunks[2].hosts[0].entry->ChunkIdAtPrefix(0), 0x31323334);
643 EXPECT_EQ(chunks[2].hosts[1].host, 0x65666163U);
644 EXPECT_EQ(chunks[2].hosts[1].entry->prefix_count(), 1);
645 EXPECT_EQ(chunks[2].hosts[1].entry->PrefixAt(0), 0x6f6e6d6cU);
646 EXPECT_EQ(chunks[2].hosts[1].entry->ChunkIdAtPrefix(0), 0x35363738);
649 TEST(SafeBrowsingProtocolParsingTest, TestAddBinHashChunks) {
650 std::string add_chunk("a:1:4:16\naaaabbbbccccdddd"
651 "a:2:4:8\n11112222");
652 // Run the parse.
653 SafeBrowsingProtocolParser parser;
654 SBChunkList chunks;
655 bool result = parser.ParseChunk(
656 safe_browsing_util::kExtensionBlacklist,
657 add_chunk.data(),
658 static_cast<int>(add_chunk.length()),
659 &chunks);
660 EXPECT_TRUE(result);
661 EXPECT_EQ(chunks.size(), 2U);
662 EXPECT_EQ(chunks[0].chunk_number, 1);
663 EXPECT_EQ(chunks[0].hosts.size(), 1U);
665 EXPECT_EQ(chunks[0].hosts[0].host, 0U);
666 SBEntry* entry = chunks[0].hosts[0].entry;
667 EXPECT_TRUE(entry->IsAdd());
668 EXPECT_TRUE(entry->IsPrefix());
669 EXPECT_EQ(entry->prefix_count(), 4);
671 EXPECT_EQ(chunks[1].chunk_number, 2);
672 EXPECT_EQ(chunks[1].hosts.size(), 1U);
674 EXPECT_EQ(chunks[1].hosts[0].host, 0U);
675 entry = chunks[1].hosts[0].entry;
676 EXPECT_TRUE(entry->IsAdd());
677 EXPECT_TRUE(entry->IsPrefix());
678 EXPECT_EQ(entry->prefix_count(), 2);
679 EXPECT_EQ(entry->PrefixAt(0), 0x31313131U);
680 EXPECT_EQ(entry->PrefixAt(1), 0x32323232U);
683 // Test parsing one add chunk where a hostkey spans several entries.
684 TEST(SafeBrowsingProtocolParsingTest, TestAddBigBinHashChunk) {
685 std::string add_chunk("a:1:4:1028\n");
686 for (int i = 0; i < 257; ++i)
687 add_chunk.append(base::StringPrintf("%04d", i));
689 SafeBrowsingProtocolParser parser;
690 SBChunkList chunks;
691 bool result = parser.ParseChunk(
692 safe_browsing_util::kExtensionBlacklist,
693 add_chunk.data(),
694 static_cast<int>(add_chunk.length()),
695 &chunks);
696 EXPECT_TRUE(result);
697 EXPECT_EQ(chunks.size(), 1U);
698 EXPECT_EQ(chunks[0].chunk_number, 1);
700 EXPECT_EQ(chunks[0].hosts.size(), 1U);
702 const SBChunkHost& host0 = chunks[0].hosts[0];
703 EXPECT_EQ(host0.host, 0U);
704 EXPECT_EQ(host0.entry->prefix_count(), 257);
707 // Test parsing one sub chunk.
708 TEST(SafeBrowsingProtocolParsingTest, TestSubBinHashChunk) {
709 std::string sub_chunk("s:9:4:16\n1111mmmm2222nnnn");
711 // Run the parser.
712 SafeBrowsingProtocolParser parser;
713 SBChunkList chunks;
714 bool result = parser.ParseChunk(
715 safe_browsing_util::kExtensionBlacklist,
716 sub_chunk.data(),
717 static_cast<int>(sub_chunk.length()),
718 &chunks);
719 EXPECT_TRUE(result);
720 EXPECT_EQ(chunks.size(), 1U);
721 EXPECT_EQ(chunks[0].chunk_number, 9);
722 EXPECT_EQ(chunks[0].hosts.size(), 1U);
724 EXPECT_EQ(chunks[0].hosts[0].host, 0U);
725 SBEntry* entry = chunks[0].hosts[0].entry;
726 EXPECT_TRUE(entry->IsSub());
727 EXPECT_TRUE(entry->IsPrefix());
728 EXPECT_EQ(entry->prefix_count(), 2);
729 EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x31313131);
730 EXPECT_EQ(entry->PrefixAt(0), 0x6d6d6d6dU);
731 EXPECT_EQ(entry->ChunkIdAtPrefix(1), 0x32323232);
732 EXPECT_EQ(entry->PrefixAt(1), 0x6e6e6e6eU);
735 TEST(SafeBrowsingProtocolParsingTest, TestAddDownloadWhitelistChunk) {
736 std::string add_chunk("a:1:32:32\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
737 "a:2:32:64\nyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
738 "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");
739 // Run the parse.
740 SafeBrowsingProtocolParser parser;
741 SBChunkList chunks;
742 bool result = parser.ParseChunk(
743 safe_browsing_util::kDownloadWhiteList,
744 add_chunk.data(),
745 static_cast<int>(add_chunk.length()),
746 &chunks);
747 EXPECT_TRUE(result);
748 EXPECT_EQ(chunks.size(), 2U);
749 EXPECT_EQ(chunks[0].chunk_number, 1);
750 EXPECT_EQ(chunks[0].hosts.size(), 1U);
751 EXPECT_EQ(chunks[0].hosts[0].host, 0U);
752 SBEntry* entry = chunks[0].hosts[0].entry;
753 EXPECT_TRUE(entry->IsAdd());
754 EXPECT_FALSE(entry->IsPrefix());
755 EXPECT_EQ(entry->prefix_count(), 1);
756 SBFullHash full;
757 memcpy(full.full_hash, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 32);
758 EXPECT_TRUE(SBFullHashEqual(entry->FullHashAt(0), full));
760 EXPECT_EQ(chunks[1].chunk_number, 2);
761 EXPECT_EQ(chunks[1].hosts.size(), 1U);
762 EXPECT_EQ(chunks[1].hosts[0].host, 0U);
763 entry = chunks[1].hosts[0].entry;
764 EXPECT_TRUE(entry->IsAdd());
765 EXPECT_FALSE(entry->IsPrefix());
766 EXPECT_EQ(entry->prefix_count(), 2);
767 memcpy(full.full_hash, "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy", 32);
768 EXPECT_TRUE(SBFullHashEqual(entry->FullHashAt(0), full));
769 memcpy(full.full_hash, "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", 32);
770 EXPECT_TRUE(SBFullHashEqual(entry->FullHashAt(1), full));
773 // Test parsing one sub chunk.
774 TEST(SafeBrowsingProtocolParsingTest, TestSubDownloadWhitelistChunk) {
775 std::string sub_chunk("s:1:32:36\n1111xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
777 // Run the parser.
778 SafeBrowsingProtocolParser parser;
779 SBChunkList chunks;
780 bool result = parser.ParseChunk(
781 safe_browsing_util::kDownloadWhiteList,
782 sub_chunk.data(),
783 static_cast<int>(sub_chunk.length()),
784 &chunks);
785 ASSERT_TRUE(result);
786 ASSERT_EQ(chunks.size(), 1U);
787 EXPECT_EQ(chunks[0].chunk_number, 1);
788 EXPECT_EQ(chunks[0].hosts.size(), 1U);
790 EXPECT_EQ(chunks[0].hosts[0].host, 0U);
791 SBEntry* entry = chunks[0].hosts[0].entry;
792 EXPECT_TRUE(entry->IsSub());
793 ASSERT_FALSE(entry->IsPrefix());
794 ASSERT_EQ(entry->prefix_count(), 1);
795 EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x31313131);
796 SBFullHash full;
797 memcpy(full.full_hash, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 32);
798 EXPECT_TRUE(SBFullHashEqual(entry->FullHashAt(0), full));
801 // There should be one test case for every list added here. Each list
802 // chunk data should contain exactly one add chunk and one sub chunk.
803 TEST(SafeBrowsingProtocolParsingTest, TestAllLists) {
804 std::map<std::string, std::string> add_testdata;
805 // goog-*-shavar lists have a host-key followed by one or more prefixes.
806 add_testdata[safe_browsing_util::kMalwareList] = std::string(
807 "a:129057:4:34\n\xd9\xb8\xb2\x14\x00\xe4""B7P\x00\xaf\x91"
808 "{\xd7\x01\xad\xa6\xb5""X\xbb\xcf""F\xcf\x00\xf7""A\xbd"
809 "p\x00\xab\xd7\x89\xd3\x00", 48);
810 add_testdata[safe_browsing_util::kPhishingList] = std::string(
811 "a:301377:4:9\n\xdb\xe0\xaa\x8e\x01\x85\xba\xb2\x9e", 22);
812 add_testdata[safe_browsing_util::kBinUrlList] = std::string(
813 "a:19420:4:18\n_\x92\x9e\xcd\x01\x03""}I\xa2\\3\xe6""h\x01\xee"
814 "H\xf6\xe4", 31);
815 add_testdata[safe_browsing_util::kSideEffectFreeWhitelist] = std::string(
816 "a:1818:4:9\n\x85\xd0\xfe""i\x01""}\x98\xb1\xe5", 20);
817 // goog-*-digestvar lists have no host-key data.
818 add_testdata[safe_browsing_util::kExtensionBlacklist] = std::string(
819 "a:81:4:8\nhleedfcc", 17);
820 // goog-*-sha256 lists have host-keys but they only contains
821 // full-length entires.
822 add_testdata[safe_browsing_util::kCsdWhiteList] = std::string(
823 "a:35:32:37\n\x06\xf9\xb1\xaf\x01\x06\xf9\xb1\xaf""5\xc""9!\x17\x1e"
824 "*-\xc9"",*>YSl6\xf9""B\xb8\x96""O\x98""r\xf2\xd5\x8d\xe3""T\x99", 48);
825 // goog-*-digest256 has no host-keys and contains only full-length
826 // hashes.
827 add_testdata[safe_browsing_util::kDownloadWhiteList] = std::string(
828 "a:42:32:32\n\xc8\xec\x9f\x9c\x9b\x9a"",\x82""G:F(\xe9\xad\x9c""b$\x8a"
829 "\xba""%\x19\xae""c\x03\x87""~\xd1\xd3""bvC\xfd", 43);
830 add_testdata[safe_browsing_util::kIPBlacklist] = std::string(
831 "a:12:32:32\n8\x99\x17\xda\xec""+i`\x1a\xb3""8pVh\n$\x01\xd1\x12"
832 ":\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 43);
834 std::map<std::string, std::string> sub_testdata;
835 // goog-*-shavar lists have a host-key, add number followed by zero or more
836 // prefixes.
837 sub_testdata[safe_browsing_util::kMalwareList] = std::string(
838 "s:122930:4:9\n\x85""!_\x13\x00\x00\x01\xf1\xa2", 22);
839 sub_testdata[safe_browsing_util::kPhishingList] = std::string(
840 "s:150181:4:9\nr\x90""zt\x00\x00\x04\x9f""@", 22);
841 sub_testdata[safe_browsing_util::kBinUrlList] = std::string(
842 "s:3:4:13\nHOST\x01""####BBBB", 22);
843 sub_testdata[safe_browsing_util::kSideEffectFreeWhitelist] = std::string(
844 "s:4:4:9\nHOST\x00""####", 17);
845 // goog-*-digestvar lists have no host-key data.
846 sub_testdata[safe_browsing_util::kExtensionBlacklist] = std::string(
847 "s:3:4:8\n\x00\x00\x00""%pgkc", 16);
848 // goog-*-sha256 lists have host-keys but they only contains
849 // full-length entires.
850 sub_testdata[safe_browsing_util::kCsdWhiteList] = std::string(
851 "s:1:32:41\n\x1a""\"[\n\x01\x00\x00\x00\x15\x1""a\"[\n\xe9\x81""P\x11"
852 "LR\xcb""3\x00""B\x90\xb3\x15""K\xf5\xdc\xd0""V\xc2""aI\x1e""-\xc8"
853 "\xce"":\t\x01", 51);
854 // goog-*-digest256 has no host-keys and contains only full-length
855 // hashes.
856 sub_testdata[safe_browsing_util::kDownloadWhiteList] = std::string(
857 "s:15:32:36\n\x00\x00\x00""-\x12""!\xa2\x8d""z\x80""a\xfb\x14\xff"
858 "f\x13\x18\xcc\xdb\xbd\xc0\xb1""~\xd6\x82""[\xf6\xdc\xa1\x81"
859 "TI%\xef""C\xab", 47);
860 sub_testdata[safe_browsing_util::kIPBlacklist] = std::string(
861 "s:13:32:108\n\x00\x00\x00\rL2\x1e\xc6""P3\x94\x1f\xf1""Cv\x18\x9d\xdf"
862 "ih4M.c\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\r_"
863 "\xfd\xbe""v\x87\xba""'\x1d\x12\r\xc0\xde""B\xc8""{a\xe7\x07""u\x11\x80"
864 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\tz\xfe\xdb"
865 "h\t\x08\xc5""dd\x9""ag\xee\xf5\x83\x91""@\xd9\xa4""i*\x80\x00\x00\x00"
866 "\x00\x00\x00\x00\x00\x00\x00\x00", 120);
868 SafeBrowsingProtocolParser parser;
869 for (size_t i = 0; i < arraysize(safe_browsing_util::kAllLists); ++i) {
870 std::string listname = safe_browsing_util::kAllLists[i];
873 ASSERT_EQ(add_testdata.count(listname), 1U)
874 << "Missing add chunk test case for Safe Browsing list: " << listname;
875 const std::string& chunk_data = add_testdata[listname];
876 SBChunkList chunks;
877 EXPECT_TRUE(parser.ParseChunk(listname,
878 chunk_data.data(),
879 static_cast<int>(chunk_data.length()),
880 &chunks))
881 << "Unable to parse add chunk data for listname: "
882 << listname;
883 ASSERT_EQ(chunks.size(), 1U);
884 EXPECT_TRUE(chunks[0].is_add);
887 ASSERT_EQ(sub_testdata.count(listname), 1U)
888 << "Missing sub chunk test case for Safe Browsing list: " << listname;
889 const std::string& chunk_data = sub_testdata[listname];
890 SBChunkList chunks;
891 EXPECT_TRUE(parser.ParseChunk(listname,
892 chunk_data.data(),
893 static_cast<int>(chunk_data.length()),
894 &chunks))
895 << "Unable to parse sub chunk data for listname: "
896 << listname;
897 ASSERT_EQ(chunks.size(), 1U);
898 EXPECT_FALSE(chunks[0].is_add);