1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "chrome/browser/safe_browsing/protocol_parser.h"
#include "chrome/browser/safe_browsing/safe_browsing_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
#if defined(OS_ANDROID)
const char kDefaultPhishList[] = "goog-mobilephish-shavar";
const char kDefaultMalwareList[] = "goog-mobilemalware-shavar";
#else
const char kDefaultPhishList[] = "goog-phish-shavar";
const char kDefaultMalwareList[] = "goog-malware-shavar";
#endif
// Test parsing one add chunk.
TEST(SafeBrowsingProtocolParsingTest, TestAddChunk) {
const char kRawAddChunk[] = {
'\0', '\0', '\0', '\x1C', // 32-bit payload length in network byte order.
'\x08', // field 1, wire format varint
'\x01', // chunk_number varint 1
'\x22', // field 4, wire format length-delimited
'\x18', // varint length 24
'1', '1', '1', '1', // 4-byte prefixes
'2', '2', '2', '2',
'3', '3', '3', '3',
'4', '4', '4', '4',
'8', '8', '8', '8',
'9', '9', '9', '9',
};
ScopedVector<SBChunkData> chunks;
EXPECT_TRUE(safe_browsing::ParseChunk(kRawAddChunk, sizeof(kRawAddChunk),
&chunks));
ASSERT_EQ(1U, chunks.size());
EXPECT_EQ(1, chunks[0]->ChunkNumber());
EXPECT_TRUE(chunks[0]->IsAdd());
EXPECT_FALSE(chunks[0]->IsSub());
EXPECT_TRUE(chunks[0]->IsPrefix());
EXPECT_FALSE(chunks[0]->IsFullHash());
ASSERT_EQ(6U, chunks[0]->PrefixCount());
EXPECT_EQ(0x31313131U, chunks[0]->PrefixAt(0)); // 1111
EXPECT_EQ(0x32323232U, chunks[0]->PrefixAt(1)); // 2222
EXPECT_EQ(0x33333333U, chunks[0]->PrefixAt(2)); // 3333
EXPECT_EQ(0x34343434U, chunks[0]->PrefixAt(3)); // 4444
EXPECT_EQ(0x38383838U, chunks[0]->PrefixAt(4)); // 8888
EXPECT_EQ(0x39393939U, chunks[0]->PrefixAt(5)); // 9999
}
// Test parsing one add chunk with full hashes.
TEST(SafeBrowsingProtocolParsingTest, TestAddFullChunk) {
const char kRawAddChunk[] = {
'\0', '\0', '\0', '\x46', // 32-bit payload length in network byte order.
'\x08', // field 1, wire format varint
'\x01', // chunk_number varint 1
'\x18', // field 3, wire format varint
'\x01', // enum PrefixType == FULL_32B
'\x22', // field 4, wire format length-delimited
'\x40', // varint length 64 (2 full hashes)
'0', '1', '0', '1', '0', '1', '0', '1',
'0', '1', '0', '1', '0', '1', '0', '1',
'0', '1', '0', '1', '0', '1', '0', '1',
'0', '1', '0', '1', '0', '1', '0', '1',
'2', '3', '2', '3', '2', '3', '2', '3',
'2', '3', '2', '3', '2', '3', '2', '3',
'2', '3', '2', '3', '2', '3', '2', '3',
'2', '3', '2', '3', '2', '3', '2', '3',
};
SBFullHash full_hash1, full_hash2;
for (int i = 0; i < 32; ++i) {
full_hash1.full_hash[i] = (i % 2) ? '1' : '0';
full_hash2.full_hash[i] = (i % 2) ? '3' : '2';
}
ScopedVector<SBChunkData> chunks;
EXPECT_TRUE(safe_browsing::ParseChunk(kRawAddChunk, sizeof(kRawAddChunk),
&chunks));
ASSERT_EQ(1U, chunks.size());
EXPECT_EQ(1, chunks[0]->ChunkNumber());
EXPECT_TRUE(chunks[0]->IsAdd());
EXPECT_FALSE(chunks[0]->IsSub());
EXPECT_FALSE(chunks[0]->IsPrefix());
EXPECT_TRUE(chunks[0]->IsFullHash());
ASSERT_EQ(2U, chunks[0]->FullHashCount());
EXPECT_TRUE(SBFullHashEqual(chunks[0]->FullHashAt(0), full_hash1));
EXPECT_TRUE(SBFullHashEqual(chunks[0]->FullHashAt(1), full_hash2));
}
// Test parsing multiple add chunks. We'll use the same chunk as above, and add
// one more after it.
TEST(SafeBrowsingProtocolParsingTest, TestAddChunks) {
const char kRawAddChunk[] = {
'\0', '\0', '\0', '\x1C', // 32-bit payload length in network byte order.
'\x08', // field 1, wire format varint
'\x01', // chunk_number varint 1
'\x22', // field 4, wire format length-delimited
'\x18', // varint length 24
'1', '1', '1', '1', // 4-byte prefixes
'2', '2', '2', '2',
'3', '3', '3', '3',
'4', '4', '4', '4',
'8', '8', '8', '8',
'9', '9', '9', '9',
'\0', '\0', '\0', '\x0C', // 32-bit payload length in network byte order.
'\x08', // field 1, wire format varint
'\x02', // chunk_number varint 1
'\x22', // field 4, wire format length-delimited
'\x08', // varint length 8
'p', 'p', 'p', 'p', // 4-byte prefixes
'g', 'g', 'g', 'g',
};
ScopedVector<SBChunkData> chunks;
EXPECT_TRUE(safe_browsing::ParseChunk(kRawAddChunk, sizeof(kRawAddChunk),
&chunks));
ASSERT_EQ(2U, chunks.size());
EXPECT_EQ(1, chunks[0]->ChunkNumber());
EXPECT_TRUE(chunks[0]->IsAdd());
EXPECT_FALSE(chunks[0]->IsSub());
EXPECT_TRUE(chunks[0]->IsPrefix());
EXPECT_FALSE(chunks[0]->IsFullHash());
ASSERT_EQ(6U, chunks[0]->PrefixCount());
EXPECT_EQ(0x31313131U, chunks[0]->PrefixAt(0)); // 1111
EXPECT_EQ(0x32323232U, chunks[0]->PrefixAt(1)); // 2222
EXPECT_EQ(0x33333333U, chunks[0]->PrefixAt(2)); // 3333
EXPECT_EQ(0x34343434U, chunks[0]->PrefixAt(3)); // 4444
EXPECT_EQ(0x38383838U, chunks[0]->PrefixAt(4)); // 8888
EXPECT_EQ(0x39393939U, chunks[0]->PrefixAt(5)); // 9999
EXPECT_EQ(2, chunks[1]->ChunkNumber());
EXPECT_TRUE(chunks[1]->IsAdd());
EXPECT_FALSE(chunks[1]->IsSub());
EXPECT_TRUE(chunks[1]->IsPrefix());
EXPECT_FALSE(chunks[1]->IsFullHash());
ASSERT_EQ(2U, chunks[1]->PrefixCount());
EXPECT_EQ(0x70707070U, chunks[1]->PrefixAt(0)); // pppp
EXPECT_EQ(0x67676767U, chunks[1]->PrefixAt(1)); // gggg
}
TEST(SafeBrowsingProtocolParsingTest, TestTruncatedPrefixChunk) {
// This chunk delares there are 6 prefixes but actually only contains 3.
const char kRawAddChunk[] = {
'\0', '\0', '\0', '\x1C', // 32-bit payload length in network byte order.
'\x08', // field 1, wire format varint
'\x01', // chunk_number varint 1
'\x22', // field 4, wire format length-delimited
'\x18', // varint length 24
'1', '1', '1', '1', // 4-byte prefixes
'2', '2', '2', '2',
'3', '3', '3', '3',
};
ScopedVector<SBChunkData> chunks;
EXPECT_FALSE(safe_browsing::ParseChunk(kRawAddChunk, sizeof(kRawAddChunk),
&chunks));
}
TEST(SafeBrowsingProtocolParsingTest, TestTruncatedFullHashChunk) {
// This chunk delares there are two full hashes but there is only one.
const char kRawAddChunk[] = {
'\0', '\0', '\0', '\x46', // 32-bit payload length in network byte order.
'\x08', // field 1, wire format varint
'\x01', // chunk_number varint 1
'\x18', // field 3, wire format varint
'\x01', // enum PrefixType == FULL_32B
'\x22', // field 4, wire format length-delimited
'\x40', // varint length 64 (2 full hashes)
'0', '1', '0', '1', '0', '1', '0', '1',
'0', '1', '0', '1', '0', '1', '0', '1',
'0', '1', '0', '1', '0', '1', '0', '1',
'0', '1', '0', '1', '0', '1', '0', '1',
};
ScopedVector<SBChunkData> chunks;
EXPECT_FALSE(safe_browsing::ParseChunk(kRawAddChunk, sizeof(kRawAddChunk),
&chunks));
}
TEST(SafeBrowsingProtocolParsingTest, TestHugeChunk) {
// This chunk delares there are 6 prefixes but actually only contains 3.
const char kRawAddChunk[] = {
'\x1', '\0', '\0', '\0', // 32-bit payload length in network byte order.
'\x08', // field 1, wire format varint
'\x01', // chunk_number varint 1
'\x22', // field 4, wire format length-delimited
'\x18', // varint length 24
'1', '1', '1', '1', // 4-byte prefixes
'2', '2', '2', '2',
'3', '3', '3', '3',
};
ScopedVector<SBChunkData> chunks;
EXPECT_FALSE(safe_browsing::ParseChunk(kRawAddChunk, sizeof(kRawAddChunk),
&chunks));
}
// Test parsing one sub chunk.
TEST(SafeBrowsingProtocolParsingTest, TestSubChunk) {
const char kRawSubChunk[] = {
'\0', '\0', '\0', '\x12', // 32-bit payload length in network byte order
'\x08', // field 1, wire format varint
'\x03', // chunk_number varint 3
'\x10', // field 2, wire format varint
'\x01', // enum ChunkType == SUB
'\x22', // field 4, wire format length-delimited
'\x08', // varint length 8 (2 prefixes)
'1', '1', '1', '1', // 4-byte prefixes
'2', '2', '2', '2',
'\x2a', // field 5, wire format length-delimited
'\x02', // varint length 2 (2 add-chunk numbers)
'\x07', '\x09', // varint 7, varint 9
};
ScopedVector<SBChunkData> chunks;
EXPECT_TRUE(safe_browsing::ParseChunk(kRawSubChunk, sizeof(kRawSubChunk),
&chunks));
ASSERT_EQ(1U, chunks.size());
EXPECT_EQ(3, chunks[0]->ChunkNumber());
EXPECT_FALSE(chunks[0]->IsAdd());
EXPECT_TRUE(chunks[0]->IsSub());
EXPECT_TRUE(chunks[0]->IsPrefix());
EXPECT_FALSE(chunks[0]->IsFullHash());
ASSERT_EQ(2U, chunks[0]->PrefixCount());
EXPECT_EQ(0x31313131U, chunks[0]->PrefixAt(0)); // 1111
EXPECT_EQ(7, chunks[0]->AddChunkNumberAt(0));
EXPECT_EQ(0x32323232U, chunks[0]->PrefixAt(1)); // 2222
EXPECT_EQ(9, chunks[0]->AddChunkNumberAt(1));
}
// Test parsing one sub chunk with full hashes.
TEST(SafeBrowsingProtocolParsingTest, TestSubFullChunk) {
const char kRawSubChunk[] = {
'\0', '\0', '\0', '\x4C', // 32-bit payload length in network byte order.
'\x08', // field 1, wire format varint
'\x02', // chunk_number varint 2
'\x10', // field 2, wire format varint
'\x01', // enum ChunkType == SUB
'\x18', // field 3, wire format varint
'\x01', // enum PrefixType == FULL_32B
'\x22', // field 4, wire format length-delimited
'\x40', // varint length 64 (2 full hashes)
'0', '1', '0', '1', '0', '1', '0', '1',
'0', '1', '0', '1', '0', '1', '0', '1',
'0', '1', '0', '1', '0', '1', '0', '1',
'0', '1', '0', '1', '0', '1', '0', '1',
'2', '3', '2', '3', '2', '3', '2', '3',
'2', '3', '2', '3', '2', '3', '2', '3',
'2', '3', '2', '3', '2', '3', '2', '3',
'2', '3', '2', '3', '2', '3', '2', '3',
'\x2a', // field 5, wire format length-delimited
'\x02', // varint length 2 (2 add-chunk numbers)
'\x07', '\x09', // varint 7, varint 9
};
SBFullHash full_hash1, full_hash2;
for (int i = 0; i < 32; ++i) {
full_hash1.full_hash[i] = i % 2 ? '1' : '0';
full_hash2.full_hash[i] = i % 2 ? '3' : '2';
}
ScopedVector<SBChunkData> chunks;
EXPECT_TRUE(safe_browsing::ParseChunk(kRawSubChunk, sizeof(kRawSubChunk),
&chunks));
ASSERT_EQ(1U, chunks.size());
EXPECT_EQ(2, chunks[0]->ChunkNumber());
EXPECT_FALSE(chunks[0]->IsAdd());
EXPECT_TRUE(chunks[0]->IsSub());
EXPECT_FALSE(chunks[0]->IsPrefix());
EXPECT_TRUE(chunks[0]->IsFullHash());
ASSERT_EQ(2U, chunks[0]->FullHashCount());
EXPECT_TRUE(SBFullHashEqual(chunks[0]->FullHashAt(0), full_hash1));
EXPECT_EQ(7, chunks[0]->AddChunkNumberAt(0));
EXPECT_TRUE(SBFullHashEqual(chunks[0]->FullHashAt(1), full_hash2));
EXPECT_EQ(9, chunks[0]->AddChunkNumberAt(1));
}
// Test parsing the SafeBrowsing update response.
TEST(SafeBrowsingProtocolParsingTest, TestChunkDelete) {
std::string add_del("n:1700\ni:phishy\nad:1-7,43-597,44444,99999\n"
"i:malware\nsd:21-27,42,171717\n");
size_t next_query_sec = 0;
bool reset = false;
std::vector<SBChunkDelete> deletes;
std::vector<ChunkUrl> urls;
EXPECT_TRUE(safe_browsing::ParseUpdate(add_del.data(), add_del.length(),
&next_query_sec, &reset,
&deletes, &urls));
EXPECT_TRUE(urls.empty());
EXPECT_FALSE(reset);
EXPECT_EQ(1700U, next_query_sec);
ASSERT_EQ(2U, deletes.size());
ASSERT_EQ(4U, deletes[0].chunk_del.size());
EXPECT_TRUE(deletes[0].chunk_del[0] == ChunkRange(1, 7));
EXPECT_TRUE(deletes[0].chunk_del[1] == ChunkRange(43, 597));
EXPECT_TRUE(deletes[0].chunk_del[2] == ChunkRange(44444));
EXPECT_TRUE(deletes[0].chunk_del[3] == ChunkRange(99999));
ASSERT_EQ(3U, deletes[1].chunk_del.size());
EXPECT_TRUE(deletes[1].chunk_del[0] == ChunkRange(21, 27));
EXPECT_TRUE(deletes[1].chunk_del[1] == ChunkRange(42));
EXPECT_TRUE(deletes[1].chunk_del[2] == ChunkRange(171717));
// An update response with missing list name.
next_query_sec = 0;
deletes.clear();
urls.clear();
add_del = "n:1700\nad:1-7,43-597,44444,99999\ni:malware\nsd:4,21-27171717\n";
EXPECT_FALSE(safe_browsing::ParseUpdate(add_del.data(), add_del.length(),
&next_query_sec, &reset,
&deletes, &urls));
}
// Test parsing the SafeBrowsing update response.
TEST(SafeBrowsingProtocolParsingTest, TestRedirects) {
const std::string redirects(base::StringPrintf(
"i:%s\n"
"u:cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_1\n"
"u:cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_2\n"
"u:cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_3\n"
"u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8641-8800:8641-8689,"
"8691-8731,8733-8786\n",
kDefaultMalwareList));
size_t next_query_sec = 0;
bool reset = false;
std::vector<SBChunkDelete> deletes;
std::vector<ChunkUrl> urls;
EXPECT_TRUE(safe_browsing::ParseUpdate(redirects.data(), redirects.length(),
&next_query_sec, &reset,
&deletes, &urls));
EXPECT_FALSE(reset);
EXPECT_EQ(0U, next_query_sec);
EXPECT_TRUE(deletes.empty());
ASSERT_EQ(4U, urls.size());
EXPECT_EQ("cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_1",
urls[0].url);
EXPECT_EQ("cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_2",
urls[1].url);
EXPECT_EQ("cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_3",
urls[2].url);
EXPECT_EQ("s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8641-8800:"
"8641-8689,8691-8731,8733-8786",
urls[3].url);
}
// Test parsing various SafeBrowsing protocol headers.
TEST(SafeBrowsingProtocolParsingTest, TestNextQueryTime) {
std::string headers("n:1800\ni:goog-white-shavar\n");
size_t next_query_sec = 0;
bool reset = false;
std::vector<SBChunkDelete> deletes;
std::vector<ChunkUrl> urls;
EXPECT_TRUE(safe_browsing::ParseUpdate(headers.data(), headers.length(),
&next_query_sec, &reset,
&deletes, &urls));
EXPECT_EQ(1800U, next_query_sec);
EXPECT_FALSE(reset);
EXPECT_TRUE(deletes.empty());
EXPECT_TRUE(urls.empty());
}
// Test parsing data from a GetHashRequest
TEST(SafeBrowsingProtocolParsingTest, TestGetHash) {
const std::string get_hash(base::StringPrintf(
"45\n"
"%s:32:3\n"
"00112233445566778899aabbccddeeff"
"00001111222233334444555566667777"
"ffffeeeeddddccccbbbbaaaa99998888",
kDefaultPhishList));
std::vector<SBFullHashResult> full_hashes;
base::TimeDelta cache_lifetime;
EXPECT_TRUE(safe_browsing::ParseGetHash(get_hash.data(), get_hash.length(),
&cache_lifetime, &full_hashes));
ASSERT_EQ(3U, full_hashes.size());
EXPECT_EQ(memcmp(&full_hashes[0].hash,
"00112233445566778899aabbccddeeff",
sizeof(SBFullHash)), 0);
EXPECT_EQ(safe_browsing_util::PHISH, full_hashes[0].list_id);
EXPECT_EQ(memcmp(&full_hashes[1].hash,
"00001111222233334444555566667777",
sizeof(SBFullHash)), 0);
EXPECT_EQ(safe_browsing_util::PHISH, full_hashes[1].list_id);
EXPECT_EQ(memcmp(&full_hashes[2].hash,
"ffffeeeeddddccccbbbbaaaa99998888",
sizeof(SBFullHash)), 0);
EXPECT_EQ(safe_browsing_util::PHISH, full_hashes[2].list_id);
// Test multiple lists in the GetHash results.
const std::string get_hash2(base::StringPrintf(
"45\n"
"%s:32:1\n"
"00112233445566778899aabbccddeeff"
"%s:32:2\n"
"cafebeefcafebeefdeaddeaddeaddead"
"zzzzyyyyxxxxwwwwvvvvuuuuttttssss",
kDefaultPhishList,
kDefaultMalwareList));
EXPECT_TRUE(safe_browsing::ParseGetHash(get_hash2.data(), get_hash2.length(),
&cache_lifetime, &full_hashes));
ASSERT_EQ(3U, full_hashes.size());
EXPECT_EQ(memcmp(&full_hashes[0].hash,
"00112233445566778899aabbccddeeff",
sizeof(SBFullHash)), 0);
EXPECT_EQ(safe_browsing_util::PHISH, full_hashes[0].list_id);
EXPECT_EQ(memcmp(&full_hashes[1].hash,
"cafebeefcafebeefdeaddeaddeaddead",
sizeof(SBFullHash)), 0);
EXPECT_EQ(safe_browsing_util::MALWARE, full_hashes[1].list_id);
EXPECT_EQ(memcmp(&full_hashes[2].hash,
"zzzzyyyyxxxxwwwwvvvvuuuuttttssss",
sizeof(SBFullHash)), 0);
EXPECT_EQ(safe_browsing_util::MALWARE, full_hashes[2].list_id);
// Test metadata parsing.
const std::string get_hash3(base::StringPrintf(
"45\n"
"%s:32:2:m\n"
"zzzzyyyyxxxxwwwwvvvvuuuuttttssss"
"00112233445566778899aabbccddeeff"
"2\nab2\nxy"
"%s:32:1\n"
"cafebeefcafebeefdeaddeaddeaddead",
kDefaultMalwareList,
kDefaultPhishList));
EXPECT_TRUE(safe_browsing::ParseGetHash(get_hash3.data(), get_hash3.length(),
&cache_lifetime, &full_hashes));
ASSERT_EQ(3U, full_hashes.size());
EXPECT_EQ(memcmp(&full_hashes[0].hash,
"zzzzyyyyxxxxwwwwvvvvuuuuttttssss",
sizeof(SBFullHash)), 0);
EXPECT_EQ(safe_browsing_util::MALWARE, full_hashes[0].list_id);
EXPECT_EQ(std::string("ab"), full_hashes[0].metadata);
EXPECT_EQ(memcmp(&full_hashes[1].hash,
"00112233445566778899aabbccddeeff",
sizeof(SBFullHash)), 0);
EXPECT_EQ(safe_browsing_util::MALWARE, full_hashes[1].list_id);
EXPECT_EQ(std::string("xy"), full_hashes[1].metadata);
EXPECT_EQ(memcmp(&full_hashes[2].hash,
"cafebeefcafebeefdeaddeaddeaddead",
sizeof(SBFullHash)), 0);
EXPECT_EQ(safe_browsing_util::PHISH, full_hashes[2].list_id);
EXPECT_EQ(std::string(), full_hashes[2].metadata);
}
TEST(SafeBrowsingProtocolParsingTest, TestGetHashWithUnknownList) {
std::string hash_response(base::StringPrintf(
"45\n"
"%s:32:1\n"
"12345678901234567890123456789012"
"googpub-phish-shavar:32:1\n"
"09876543210987654321098765432109",
kDefaultPhishList));
std::vector<SBFullHashResult> full_hashes;
base::TimeDelta cache_lifetime;
EXPECT_TRUE(safe_browsing::ParseGetHash(hash_response.data(),
hash_response.size(),
&cache_lifetime,
&full_hashes));
ASSERT_EQ(1U, full_hashes.size());
EXPECT_EQ(memcmp("12345678901234567890123456789012",
&full_hashes[0].hash, sizeof(SBFullHash)), 0);
EXPECT_EQ(safe_browsing_util::PHISH, full_hashes[0].list_id);
hash_response += base::StringPrintf(
"%s:32:1\n"
"abcdefghijklmnopqrstuvwxyz123457",
kDefaultMalwareList);
full_hashes.clear();
EXPECT_TRUE(safe_browsing::ParseGetHash(hash_response.data(),
hash_response.size(),
&cache_lifetime,
&full_hashes));
EXPECT_EQ(2U, full_hashes.size());
EXPECT_EQ(memcmp("12345678901234567890123456789012",
&full_hashes[0].hash, sizeof(SBFullHash)), 0);
EXPECT_EQ(safe_browsing_util::PHISH, full_hashes[0].list_id);
EXPECT_EQ(memcmp("abcdefghijklmnopqrstuvwxyz123457",
&full_hashes[1].hash, sizeof(SBFullHash)), 0);
EXPECT_EQ(safe_browsing_util::MALWARE, full_hashes[1].list_id);
}
TEST(SafeBrowsingProtocolParsingTest, TestGetHashWithUnknownListAndMetadata) {
std::vector<SBFullHashResult> full_hashes;
base::TimeDelta cache_lifetime;
// Test skipping over a hashentry with an unrecognized listname that also has
// metadata.
const std::string get_hash3(base::StringPrintf(
"600\n"
"BADLISTNAME:32:1:m\n"
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
"8\nMETADATA"
"%s:32:1\n"
"0123456789hashhashhashhashhashha",
kDefaultMalwareList));
EXPECT_TRUE(safe_browsing::ParseGetHash(get_hash3.data(), get_hash3.length(),
&cache_lifetime, &full_hashes));
ASSERT_EQ(1U, full_hashes.size());
EXPECT_EQ(memcmp(&full_hashes[0].hash,
"0123456789hashhashhashhashhashha",
sizeof(SBFullHash)), 0);
EXPECT_EQ(safe_browsing_util::MALWARE, full_hashes[0].list_id);
EXPECT_EQ(std::string(), full_hashes[0].metadata);
}
TEST(SafeBrowsingProtocolParsingTest, TestFormatHash) {
std::vector<SBPrefix> prefixes;
prefixes.push_back(0x34333231);
prefixes.push_back(0x64636261);
prefixes.push_back(0x73727170);
EXPECT_EQ("4:12\n1234abcdpqrs", safe_browsing::FormatGetHash(prefixes));
}
TEST(SafeBrowsingProtocolParsingTest, TestReset) {
std::string update("n:1800\ni:phishy\nr:pleasereset\n");
bool reset = false;
size_t next_update = 0;
std::vector<SBChunkDelete> deletes;
std::vector<ChunkUrl> urls;
EXPECT_TRUE(safe_browsing::ParseUpdate(update.data(), update.size(),
&next_update, &reset,
&deletes, &urls));
EXPECT_TRUE(reset);
}
// The SafeBrowsing service will occasionally send zero length chunks so that
// client requests will have longer contiguous chunk number ranges, and thus
// reduce the request size.
TEST(SafeBrowsingProtocolParsingTest, TestZeroSizeAddChunk) {
const char kEmptyAddChunk[] = {
'\0', '\0', '\0', '\x02', // 32-bit payload length in network byte order.
'\x08', // field 1, wire format varint
'\x02', // chunk_number varint 2
};
ScopedVector<SBChunkData> chunks;
EXPECT_TRUE(safe_browsing::ParseChunk(kEmptyAddChunk, sizeof(kEmptyAddChunk),
&chunks));
ASSERT_EQ(1U, chunks.size());
EXPECT_EQ(2, chunks[0]->ChunkNumber());
EXPECT_TRUE(chunks[0]->IsAdd());
EXPECT_FALSE(chunks[0]->IsSub());
EXPECT_TRUE(chunks[0]->IsPrefix());
EXPECT_FALSE(chunks[0]->IsFullHash());
EXPECT_EQ(0U, chunks[0]->PrefixCount());
// Now test a zero size chunk in between normal chunks.
chunks.clear();
const char kAddChunks[] = {
'\0', '\0', '\0', '\x0C', // 32-bit payload length in network byte order.
'\x08', // field 1, wire format varint
'\x01', // chunk_number varint 1
'\x22', // field 4, wire format length-delimited
'\x08', // varint length 8
'1', '1', '1', '1', // 4-byte prefixes
'2', '2', '2', '2',
'\0', '\0', '\0', '\x02', // 32-bit payload length in network byte order.
'\x08', // field 1, wire format varint
'\x02', // chunk_number varint 2
'\0', '\0', '\0', '\x08', // 32-bit payload length in network byte order.
'\x08', // field 1, wire format varint
'\x03', // chunk_number varint 3
'\x22', // field 4, wire format length-delimited
'\x04', // varint length 8
'p', 'p', 'p', 'p', // 4-byte prefixes
};
EXPECT_TRUE(safe_browsing::ParseChunk(kAddChunks, sizeof(kAddChunks),
&chunks));
ASSERT_EQ(3U, chunks.size());
EXPECT_EQ(1, chunks[0]->ChunkNumber());
EXPECT_TRUE(chunks[0]->IsAdd());
EXPECT_FALSE(chunks[0]->IsSub());
EXPECT_TRUE(chunks[0]->IsPrefix());
EXPECT_FALSE(chunks[0]->IsFullHash());
ASSERT_EQ(2U, chunks[0]->PrefixCount());
EXPECT_EQ(0x31313131U, chunks[0]->PrefixAt(0)); // 1111
EXPECT_EQ(0x32323232U, chunks[0]->PrefixAt(1)); // 2222
EXPECT_EQ(2, chunks[1]->ChunkNumber());
EXPECT_TRUE(chunks[1]->IsAdd());
EXPECT_FALSE(chunks[1]->IsSub());
EXPECT_TRUE(chunks[1]->IsPrefix());
EXPECT_FALSE(chunks[1]->IsFullHash());
EXPECT_EQ(0U, chunks[1]->PrefixCount());
EXPECT_EQ(3, chunks[2]->ChunkNumber());
EXPECT_TRUE(chunks[2]->IsAdd());
EXPECT_FALSE(chunks[2]->IsSub());
EXPECT_TRUE(chunks[2]->IsPrefix());
EXPECT_FALSE(chunks[2]->IsFullHash());
ASSERT_EQ(1U, chunks[2]->PrefixCount());
EXPECT_EQ(0x70707070U, chunks[2]->PrefixAt(0)); // pppp
}
// Test parsing a zero sized sub chunk.
TEST(SafeBrowsingProtocolParsingTest, TestZeroSizeSubChunk) {
const char kEmptySubChunk[] = {
'\0', '\0', '\0', '\x04', // 32-bit payload length in network byte order.
'\x08', // field 1, wire format varint
'\x02', // chunk_number varint 2
'\x10', // field 2, wire format varint
'\x01', // enum ChunkType == SUB
};
ScopedVector<SBChunkData> chunks;
EXPECT_TRUE(safe_browsing::ParseChunk(kEmptySubChunk, sizeof(kEmptySubChunk),
&chunks));
ASSERT_EQ(1U, chunks.size());
EXPECT_EQ(2, chunks[0]->ChunkNumber());
EXPECT_FALSE(chunks[0]->IsAdd());
EXPECT_TRUE(chunks[0]->IsSub());
EXPECT_TRUE(chunks[0]->IsPrefix());
EXPECT_FALSE(chunks[0]->IsFullHash());
EXPECT_EQ(0U, chunks[0]->PrefixCount());
// Test parsing a zero sized sub chunk mixed in with content carrying chunks.
chunks.clear();
const char kSubChunks[] = {
'\0', '\0', '\0', '\x12', // 32-bit payload length in network byte order.
'\x08', // field 1, wire format varint
'\x01', // chunk_number varint 1
'\x10', // field 2, wire format varint
'\x01', // enum ChunkType == SUB
'\x22', // field 4, wire format length-delimited
'\x08', // varint length 8
'1', '1', '1', '1', // 4-byte prefixes
'2', '2', '2', '2',
'\x2a', // field 5, wire format length-delimited
'\x02', // varint length 2 (2 add-chunk numbers)
'\x07', '\x09', // varint 7, varint 9
'\0', '\0', '\0', '\x04', // 32-bit payload length in network byte order.
'\x08', // field 1, wire format varint
'\x02', // chunk_number varint 2
'\x10', // field 2, wire format varint
'\x01', // enum ChunkType == SUB
'\0', '\0', '\0', '\x0D', // 32-bit payload length in network byte order.
'\x08', // field 1, wire format varint
'\x03', // chunk_number varint 3
'\x10', // field 2, wire format varint
'\x01', // enum ChunkType == SUB
'\x22', // field 4, wire format length-delimited
'\x04', // varint length 8
'p', 'p', 'p', 'p', // 4-byte prefix
'\x2a', // field 5, wire format length-delimited
'\x01', // varint length 1 (1 add-chunk numbers)
'\x0B', // varint 11
};
EXPECT_TRUE(safe_browsing::ParseChunk(kSubChunks, sizeof(kSubChunks),
&chunks));
ASSERT_EQ(3U, chunks.size());
EXPECT_EQ(1, chunks[0]->ChunkNumber());
EXPECT_FALSE(chunks[0]->IsAdd());
EXPECT_TRUE(chunks[0]->IsSub());
EXPECT_TRUE(chunks[0]->IsPrefix());
EXPECT_FALSE(chunks[0]->IsFullHash());
ASSERT_EQ(2U, chunks[0]->PrefixCount());
EXPECT_EQ(0x31313131U, chunks[0]->PrefixAt(0)); // 1111
EXPECT_EQ(7, chunks[0]->AddChunkNumberAt(0));
EXPECT_EQ(0x32323232U, chunks[0]->PrefixAt(1)); // 2222
EXPECT_EQ(9, chunks[0]->AddChunkNumberAt(1));
EXPECT_EQ(2, chunks[1]->ChunkNumber());
EXPECT_FALSE(chunks[0]->IsAdd());
EXPECT_TRUE(chunks[0]->IsSub());
EXPECT_TRUE(chunks[1]->IsPrefix());
EXPECT_FALSE(chunks[1]->IsFullHash());
EXPECT_EQ(0U, chunks[1]->PrefixCount());
EXPECT_EQ(3, chunks[2]->ChunkNumber());
EXPECT_FALSE(chunks[0]->IsAdd());
EXPECT_TRUE(chunks[0]->IsSub());
EXPECT_TRUE(chunks[2]->IsPrefix());
EXPECT_FALSE(chunks[2]->IsFullHash());
ASSERT_EQ(1U, chunks[2]->PrefixCount());
EXPECT_EQ(0x70707070U, chunks[2]->PrefixAt(0)); // pppp
EXPECT_EQ(11, chunks[2]->AddChunkNumberAt(0));
}
} // namespace
|