blob: 1796bd30df834203ae4e90b52fffe5eff01011e2 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// Copyright (c) 2006-2008 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.
initial.commit09911bf2008-07-26 23:55:294//
5// Program to test the SafeBrowsing protocol parsing v2.1.
6
initial.commit09911bf2008-07-26 23:55:297#include "base/string_util.h"
initial.commit09911bf2008-07-26 23:55:298#include "chrome/browser/safe_browsing/protocol_parser.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11
12// Test parsing one add chunk.
13TEST(SafeBrowsingProtocolParsingTest, TestAddChunk) {
14 std::string add_chunk("a:1:4:35\naaaax1111\0032222333344447777\00288889999");
15 add_chunk[13] = '\0';
16
17 // Run the parse.
18 SafeBrowsingProtocolParser parser;
19 bool re_key = false;
20 std::deque<SBChunk> chunks;
21 bool result = parser.ParseChunk(add_chunk.data(),
22 static_cast<int>(add_chunk.length()),
23 "", "", &re_key, &chunks);
24 EXPECT_TRUE(result);
25 EXPECT_FALSE(re_key);
[email protected]954bc8a52008-09-17 18:16:0126 EXPECT_EQ(chunks.size(), static_cast<size_t>(1));
initial.commit09911bf2008-07-26 23:55:2927 EXPECT_EQ(chunks[0].chunk_number, 1);
[email protected]954bc8a52008-09-17 18:16:0128 EXPECT_EQ(chunks[0].hosts.size(), static_cast<size_t>(3));
initial.commit09911bf2008-07-26 23:55:2929
30 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161);
31 SBEntry* entry = chunks[0].hosts[0].entry;
[email protected]eef5337c2010-02-18 23:53:1132 EXPECT_TRUE(entry->IsAdd());
33 EXPECT_TRUE(entry->IsPrefix());
initial.commit09911bf2008-07-26 23:55:2934 EXPECT_EQ(entry->prefix_count(), 0);
35
36 EXPECT_EQ(chunks[0].hosts[1].host, 0x31313131);
37 entry = chunks[0].hosts[1].entry;
[email protected]eef5337c2010-02-18 23:53:1138 EXPECT_TRUE(entry->IsAdd());
39 EXPECT_TRUE(entry->IsPrefix());
initial.commit09911bf2008-07-26 23:55:2940 EXPECT_EQ(entry->prefix_count(), 3);
41 EXPECT_EQ(entry->PrefixAt(0), 0x32323232);
42 EXPECT_EQ(entry->PrefixAt(1), 0x33333333);
43 EXPECT_EQ(entry->PrefixAt(2), 0x34343434);
44
45 EXPECT_EQ(chunks[0].hosts[2].host, 0x37373737);
46 entry = chunks[0].hosts[2].entry;
[email protected]eef5337c2010-02-18 23:53:1147 EXPECT_TRUE(entry->IsAdd());
48 EXPECT_TRUE(entry->IsPrefix());
initial.commit09911bf2008-07-26 23:55:2949 EXPECT_EQ(entry->prefix_count(), 2);
50 EXPECT_EQ(entry->PrefixAt(0), 0x38383838);
51 EXPECT_EQ(entry->PrefixAt(1), 0x39393939);
52
53 safe_browsing_util::FreeChunks(&chunks);
54}
55
56// Test parsing one add chunk with full hashes.
57TEST(SafeBrowsingProtocolParsingTest, TestAddFullChunk) {
58 std::string add_chunk("a:1:32:69\naaaa");
59 add_chunk.push_back(2);
60
61 SBFullHash full_hash1, full_hash2;
62 for (int i = 0; i < 32; ++i) {
63 full_hash1.full_hash[i] = i % 2 ? 1 : 2;
64 full_hash2.full_hash[i] = i % 2 ? 3 : 4;
65 }
66
67 add_chunk.append(full_hash1.full_hash, 32);
68 add_chunk.append(full_hash2.full_hash, 32);
69
70 // Run the parse.
71 SafeBrowsingProtocolParser parser;
72 bool re_key = false;
73 std::deque<SBChunk> chunks;
74 bool result = parser.ParseChunk(add_chunk.data(),
75 static_cast<int>(add_chunk.length()),
76 "", "", &re_key, &chunks);
77 EXPECT_TRUE(result);
78 EXPECT_FALSE(re_key);
[email protected]954bc8a52008-09-17 18:16:0179 EXPECT_EQ(chunks.size(), static_cast<size_t>(1));
initial.commit09911bf2008-07-26 23:55:2980 EXPECT_EQ(chunks[0].chunk_number, 1);
[email protected]954bc8a52008-09-17 18:16:0181 EXPECT_EQ(chunks[0].hosts.size(), static_cast<size_t>(1));
initial.commit09911bf2008-07-26 23:55:2982
83 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161);
84 SBEntry* entry = chunks[0].hosts[0].entry;
[email protected]eef5337c2010-02-18 23:53:1185 EXPECT_TRUE(entry->IsAdd());
86 EXPECT_FALSE(entry->IsPrefix());
initial.commit09911bf2008-07-26 23:55:2987 EXPECT_EQ(entry->prefix_count(), 2);
88 EXPECT_TRUE(entry->FullHashAt(0) == full_hash1);
89 EXPECT_TRUE(entry->FullHashAt(1) == full_hash2);
90
91 safe_browsing_util::FreeChunks(&chunks);
92}
93
94// Test parsing multiple add chunks. We'll use the same chunk as above, and add
95// one more after it.
96TEST(SafeBrowsingProtocolParsingTest, TestAddChunks) {
97 std::string add_chunk("a:1:4:35\naaaax1111\0032222333344447777\00288889999"
98 "a:2:4:13\n5555\002ppppgggg");
99 add_chunk[13] = '\0';
100
101 // Run the parse.
102 SafeBrowsingProtocolParser parser;
103 bool re_key = false;
104 std::deque<SBChunk> chunks;
105 bool result = parser.ParseChunk(add_chunk.data(),
106 static_cast<int>(add_chunk.length()),
107 "", "", &re_key, &chunks);
108 EXPECT_TRUE(result);
109 EXPECT_FALSE(re_key);
[email protected]954bc8a52008-09-17 18:16:01110 EXPECT_EQ(chunks.size(), static_cast<size_t>(2));
initial.commit09911bf2008-07-26 23:55:29111 EXPECT_EQ(chunks[0].chunk_number, 1);
[email protected]954bc8a52008-09-17 18:16:01112 EXPECT_EQ(chunks[0].hosts.size(), static_cast<size_t>(3));
initial.commit09911bf2008-07-26 23:55:29113
114 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161);
115 SBEntry* entry = chunks[0].hosts[0].entry;
[email protected]eef5337c2010-02-18 23:53:11116 EXPECT_TRUE(entry->IsAdd());
117 EXPECT_TRUE(entry->IsPrefix());
initial.commit09911bf2008-07-26 23:55:29118 EXPECT_EQ(entry->prefix_count(), 0);
119
120 EXPECT_EQ(chunks[0].hosts[1].host, 0x31313131);
121 entry = chunks[0].hosts[1].entry;
[email protected]eef5337c2010-02-18 23:53:11122 EXPECT_TRUE(entry->IsAdd());
123 EXPECT_TRUE(entry->IsPrefix());
initial.commit09911bf2008-07-26 23:55:29124 EXPECT_EQ(entry->prefix_count(), 3);
125 EXPECT_EQ(entry->PrefixAt(0), 0x32323232);
126 EXPECT_EQ(entry->PrefixAt(1), 0x33333333);
127 EXPECT_EQ(entry->PrefixAt(2), 0x34343434);
128
129 EXPECT_EQ(chunks[0].hosts[2].host, 0x37373737);
130 entry = chunks[0].hosts[2].entry;
[email protected]eef5337c2010-02-18 23:53:11131 EXPECT_TRUE(entry->IsAdd());
132 EXPECT_TRUE(entry->IsPrefix());
initial.commit09911bf2008-07-26 23:55:29133 EXPECT_EQ(entry->prefix_count(), 2);
134 EXPECT_EQ(entry->PrefixAt(0), 0x38383838);
135 EXPECT_EQ(entry->PrefixAt(1), 0x39393939);
136
137
138 EXPECT_EQ(chunks[1].chunk_number, 2);
[email protected]954bc8a52008-09-17 18:16:01139 EXPECT_EQ(chunks[1].hosts.size(), static_cast<size_t>(1));
initial.commit09911bf2008-07-26 23:55:29140
141 EXPECT_EQ(chunks[1].hosts[0].host, 0x35353535);
142 entry = chunks[1].hosts[0].entry;
[email protected]eef5337c2010-02-18 23:53:11143 EXPECT_TRUE(entry->IsAdd());
144 EXPECT_TRUE(entry->IsPrefix());
initial.commit09911bf2008-07-26 23:55:29145 EXPECT_EQ(entry->prefix_count(), 2);
146 EXPECT_EQ(entry->PrefixAt(0), 0x70707070);
147 EXPECT_EQ(entry->PrefixAt(1), 0x67676767);
148
149 safe_browsing_util::FreeChunks(&chunks);
150}
151
152// Test parsing one add chunk where a hostkey spans several entries.
153TEST(SafeBrowsingProtocolParsingTest, TestAddBigChunk) {
154 std::string add_chunk("a:1:4:1050\naaaaX");
155 add_chunk[add_chunk.size() - 1] |= 0xFF;
156 for (int i = 0; i < 255; ++i)
157 add_chunk.append(StringPrintf("%04d", i));
158
159 add_chunk.append("aaaa");
160 add_chunk.push_back(5);
161 for (int i = 0; i < 5; ++i)
162 add_chunk.append(StringPrintf("001%d", i));
163
164 SafeBrowsingProtocolParser parser;
165 bool re_key = false;
166 std::deque<SBChunk> chunks;
167 bool result = parser.ParseChunk(add_chunk.data(),
168 static_cast<int>(add_chunk.length()),
169 "", "", &re_key, &chunks);
170 EXPECT_TRUE(result);
171 EXPECT_FALSE(re_key);
[email protected]954bc8a52008-09-17 18:16:01172 EXPECT_EQ(chunks.size(), static_cast<size_t>(1));
initial.commit09911bf2008-07-26 23:55:29173 EXPECT_EQ(chunks[0].chunk_number, 1);
174
[email protected]954bc8a52008-09-17 18:16:01175 EXPECT_EQ(chunks[0].hosts.size(), static_cast<size_t>(1));
initial.commit09911bf2008-07-26 23:55:29176
177 const SBChunkHost& host = chunks[0].hosts[0];
178 EXPECT_EQ(host.host, 0x61616161);
179 EXPECT_EQ(host.entry->prefix_count(), 260);
180
181 safe_browsing_util::FreeChunks(&chunks);
182}
183
184// Test parsing one sub chunk.
185TEST(SafeBrowsingProtocolParsingTest, TestSubChunk) {
186 std::string sub_chunk("s:9:4:59\naaaaxkkkk1111\003"
187 "zzzz2222zzzz3333zzzz4444"
188 "7777\002yyyy8888yyyy9999");
189 sub_chunk[13] = '\0';
190
191 // Run the parse.
192 SafeBrowsingProtocolParser parser;
193 bool re_key = false;
194 std::deque<SBChunk> chunks;
195 bool result = parser.ParseChunk(sub_chunk.data(),
196 static_cast<int>(sub_chunk.length()),
197 "", "", &re_key, &chunks);
198 EXPECT_TRUE(result);
199 EXPECT_FALSE(re_key);
[email protected]954bc8a52008-09-17 18:16:01200 EXPECT_EQ(chunks.size(), static_cast<size_t>(1));
initial.commit09911bf2008-07-26 23:55:29201 EXPECT_EQ(chunks[0].chunk_number, 9);
[email protected]954bc8a52008-09-17 18:16:01202 EXPECT_EQ(chunks[0].hosts.size(), static_cast<size_t>(3));
initial.commit09911bf2008-07-26 23:55:29203
204 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161);
205 SBEntry* entry = chunks[0].hosts[0].entry;
[email protected]eef5337c2010-02-18 23:53:11206 EXPECT_TRUE(entry->IsSub());
207 EXPECT_TRUE(entry->IsPrefix());
initial.commit09911bf2008-07-26 23:55:29208 EXPECT_EQ(entry->chunk_id(), 0x6b6b6b6b);
209 EXPECT_EQ(entry->prefix_count(), 0);
210
211 EXPECT_EQ(chunks[0].hosts[1].host, 0x31313131);
212 entry = chunks[0].hosts[1].entry;
[email protected]eef5337c2010-02-18 23:53:11213 EXPECT_TRUE(entry->IsSub());
214 EXPECT_TRUE(entry->IsPrefix());
initial.commit09911bf2008-07-26 23:55:29215 EXPECT_EQ(entry->prefix_count(), 3);
216 EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x7a7a7a7a);
217 EXPECT_EQ(entry->PrefixAt(0), 0x32323232);
218 EXPECT_EQ(entry->ChunkIdAtPrefix(1), 0x7a7a7a7a);
219 EXPECT_EQ(entry->PrefixAt(1), 0x33333333);
220 EXPECT_EQ(entry->ChunkIdAtPrefix(2), 0x7a7a7a7a);
221 EXPECT_EQ(entry->PrefixAt(2), 0x34343434);
222
223 EXPECT_EQ(chunks[0].hosts[2].host, 0x37373737);
224 entry = chunks[0].hosts[2].entry;
[email protected]eef5337c2010-02-18 23:53:11225 EXPECT_TRUE(entry->IsSub());
226 EXPECT_TRUE(entry->IsPrefix());
initial.commit09911bf2008-07-26 23:55:29227 EXPECT_EQ(entry->prefix_count(), 2);
228 EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x79797979);
229 EXPECT_EQ(entry->PrefixAt(0), 0x38383838);
230 EXPECT_EQ(entry->ChunkIdAtPrefix(1), 0x79797979);
231 EXPECT_EQ(entry->PrefixAt(1), 0x39393939);
232
233 safe_browsing_util::FreeChunks(&chunks);
234}
235
236// Test parsing one sub chunk with full hashes.
237TEST(SafeBrowsingProtocolParsingTest, TestSubFullChunk) {
238 std::string sub_chunk("s:1:32:77\naaaa");
239 sub_chunk.push_back(2);
240
241 SBFullHash full_hash1, full_hash2;
242 for (int i = 0; i < 32; ++i) {
243 full_hash1.full_hash[i] = i % 2 ? 1 : 2;
244 full_hash2.full_hash[i] = i % 2 ? 3 : 4;
245 }
246
247 sub_chunk.append("yyyy");
248 sub_chunk.append(full_hash1.full_hash, 32);
249 sub_chunk.append("zzzz");
250 sub_chunk.append(full_hash2.full_hash, 32);
251
252 // Run the parse.
253 SafeBrowsingProtocolParser parser;
254 bool re_key = false;
255 std::deque<SBChunk> chunks;
256 bool result = parser.ParseChunk(sub_chunk.data(),
257 static_cast<int>(sub_chunk.length()),
258 "", "", &re_key, &chunks);
259 EXPECT_TRUE(result);
260 EXPECT_FALSE(re_key);
[email protected]954bc8a52008-09-17 18:16:01261 EXPECT_EQ(chunks.size(), static_cast<size_t>(1));
initial.commit09911bf2008-07-26 23:55:29262 EXPECT_EQ(chunks[0].chunk_number, 1);
[email protected]954bc8a52008-09-17 18:16:01263 EXPECT_EQ(chunks[0].hosts.size(), static_cast<size_t>(1));
initial.commit09911bf2008-07-26 23:55:29264
265 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161);
266 SBEntry* entry = chunks[0].hosts[0].entry;
[email protected]eef5337c2010-02-18 23:53:11267 EXPECT_TRUE(entry->IsSub());
268 EXPECT_FALSE(entry->IsPrefix());
initial.commit09911bf2008-07-26 23:55:29269 EXPECT_EQ(entry->prefix_count(), 2);
270 EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x79797979);
271 EXPECT_TRUE(entry->FullHashAt(0) == full_hash1);
272 EXPECT_EQ(entry->ChunkIdAtPrefix(1), 0x7a7a7a7a);
273 EXPECT_TRUE(entry->FullHashAt(1) == full_hash2);
274
275 safe_browsing_util::FreeChunks(&chunks);
276}
277
278// Test parsing the SafeBrowsing update response.
279TEST(SafeBrowsingProtocolParsingTest, TestChunkDelete) {
280 std::string add_del("n:1700\ni:phishy\nad:1-7,43-597,44444,99999\n"
281 "i:malware\nsd:21-27,42,171717\n");
282
283 SafeBrowsingProtocolParser parser;
284 int next_query_sec = 0;
285 bool re_key = false;
286 bool reset = false;
287 std::vector<SBChunkDelete> deletes;
288 std::vector<ChunkUrl> urls;
289 EXPECT_TRUE(parser.ParseUpdate(add_del.data(),
290 static_cast<int>(add_del.length()), "",
291 &next_query_sec, &re_key,
292 &reset, &deletes, &urls));
293
294 EXPECT_TRUE(urls.empty());
295 EXPECT_FALSE(re_key);
296 EXPECT_FALSE(reset);
297 EXPECT_EQ(next_query_sec, 1700);
[email protected]954bc8a52008-09-17 18:16:01298 EXPECT_EQ(deletes.size(), static_cast<size_t>(2));
initial.commit09911bf2008-07-26 23:55:29299
[email protected]954bc8a52008-09-17 18:16:01300 EXPECT_EQ(deletes[0].chunk_del.size(), static_cast<size_t>(4));
initial.commit09911bf2008-07-26 23:55:29301 EXPECT_TRUE(deletes[0].chunk_del[0] == ChunkRange(1, 7));
302 EXPECT_TRUE(deletes[0].chunk_del[1] == ChunkRange(43, 597));
303 EXPECT_TRUE(deletes[0].chunk_del[2] == ChunkRange(44444));
304 EXPECT_TRUE(deletes[0].chunk_del[3] == ChunkRange(99999));
305
[email protected]954bc8a52008-09-17 18:16:01306 EXPECT_EQ(deletes[1].chunk_del.size(), static_cast<size_t>(3));
initial.commit09911bf2008-07-26 23:55:29307 EXPECT_TRUE(deletes[1].chunk_del[0] == ChunkRange(21, 27));
308 EXPECT_TRUE(deletes[1].chunk_del[1] == ChunkRange(42));
309 EXPECT_TRUE(deletes[1].chunk_del[2] == ChunkRange(171717));
310
311 // An update response with missing list name.
312
313 next_query_sec = 0;
314 deletes.clear();
315 urls.clear();
316 add_del = "n:1700\nad:1-7,43-597,44444,99999\ni:malware\nsd:4,21-27171717\n";
317 EXPECT_FALSE(parser.ParseUpdate(add_del.data(),
318 static_cast<int>(add_del.length()), "",
319 &next_query_sec, &re_key,
320 &reset, &deletes, &urls));
321}
322
323// Test parsing the SafeBrowsing update response.
324TEST(SafeBrowsingProtocolParsingTest, TestRedirects) {
325 std::string redirects("i:goog-malware-shavar\n"
326 "u:cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_1\n"
327 "u:cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_2\n"
328 "u:cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_3\n"
329 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8641-8800:8641-8689,"
330 "8691-8731,8733-8786\n");
331
332 SafeBrowsingProtocolParser parser;
333 int next_query_sec = 0;
334 bool re_key = false;
335 bool reset = false;
336 std::vector<SBChunkDelete> deletes;
337 std::vector<ChunkUrl> urls;
338 EXPECT_TRUE(parser.ParseUpdate(redirects.data(),
339 static_cast<int>(redirects.length()), "",
340 &next_query_sec, &re_key,
341 &reset, &deletes, &urls));
342
343 EXPECT_FALSE(re_key);
344 EXPECT_FALSE(reset);
[email protected]954bc8a52008-09-17 18:16:01345 EXPECT_EQ(urls.size(), static_cast<size_t>(4));
initial.commit09911bf2008-07-26 23:55:29346 EXPECT_EQ(urls[0].url,
347 "cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_1");
348 EXPECT_EQ(urls[1].url,
349 "cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_2");
350 EXPECT_EQ(urls[2].url,
351 "cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_3");
352 EXPECT_EQ(urls[3].url,
353 "s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8641-8800:8641-8689,"
354 "8691-8731,8733-8786");
355 EXPECT_EQ(next_query_sec, 0);
356 EXPECT_TRUE(deletes.empty());
357}
358
359TEST(SafeBrowsingProtocolParsingTest, TestRedirectsWithMac) {
360 std::string redirects("i:goog-phish-shavar\n"
361 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6501-6505:6501-6505,"
362 "pcY6iVeT9-CBQ3fdAF0rpnKjR1Y=\n"
363 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8001-8160:8001-8024,"
364 "8026-8045,8048-8049,8051-8134,8136-8152,8155-8160,"
365 "j6XXAEWnjYk9tVVLBSdQvIEq2Wg=\n");
366
367 SafeBrowsingProtocolParser parser;
368 int next_query_sec = 0;
369 bool re_key = false;
370 bool reset = false;
371 const std::string key("58Lqn5WIP961x3zuLGo5Uw==");
372 std::vector<SBChunkDelete> deletes;
373 std::vector<ChunkUrl> urls;
374 EXPECT_TRUE(parser.ParseUpdate(redirects.data(),
375 static_cast<int>(redirects.length()), key,
376 &next_query_sec, &re_key,
377 &reset, &deletes, &urls));
378
379 EXPECT_FALSE(re_key);
380 EXPECT_FALSE(reset);
[email protected]954bc8a52008-09-17 18:16:01381 EXPECT_EQ(urls.size(), static_cast<size_t>(2));
initial.commit09911bf2008-07-26 23:55:29382 EXPECT_EQ(urls[0].url,
383 "s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6501-6505:6501-6505");
384 EXPECT_EQ(urls[0].mac, "pcY6iVeT9-CBQ3fdAF0rpnKjR1Y=");
385 EXPECT_EQ(urls[1].url,
386 "s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8001-8160:8001-8024,"
387 "8026-8045,8048-8049,8051-8134,8136-8152,8155-8160");
388 EXPECT_EQ(urls[1].mac, "j6XXAEWnjYk9tVVLBSdQvIEq2Wg=");
389}
390
391// Test parsing various SafeBrowsing protocol headers.
392TEST(SafeBrowsingProtocolParsingTest, TestNextQueryTime) {
393 std::string headers("n:1800\ni:goog-white-shavar\n");
394 SafeBrowsingProtocolParser parser;
395 int next_query_sec = 0;
396 bool re_key = false;
397 bool reset = false;
398 std::vector<SBChunkDelete> deletes;
399 std::vector<ChunkUrl> urls;
400 EXPECT_TRUE(parser.ParseUpdate(headers.data(),
401 static_cast<int>(headers.length()), "",
402 &next_query_sec, &re_key,
403 &reset, &deletes, &urls));
404
405 EXPECT_EQ(next_query_sec, 1800);
406 EXPECT_FALSE(re_key);
407 EXPECT_FALSE(reset);
408 EXPECT_TRUE(deletes.empty());
409 EXPECT_TRUE(urls.empty());
410}
411
412// Test parsing data from a GetHashRequest
413TEST(SafeBrowsingProtocolParsingTest, TestGetHash) {
414 std::string get_hash("goog-phish-shavar:19:96\n"
415 "00112233445566778899aabbccddeeff"
416 "00001111222233334444555566667777"
417 "ffffeeeeddddccccbbbbaaaa99998888");
418 std::vector<SBFullHashResult> full_hashes;
419 bool re_key = false;
420 SafeBrowsingProtocolParser parser;
[email protected]071a3f8c2009-10-14 19:31:58421 EXPECT_TRUE(parser.ParseGetHash(get_hash.data(),
422 static_cast<int>(get_hash.length()), "",
423 &re_key,
424 &full_hashes));
initial.commit09911bf2008-07-26 23:55:29425
426 EXPECT_FALSE(re_key);
[email protected]954bc8a52008-09-17 18:16:01427 EXPECT_EQ(full_hashes.size(), static_cast<size_t>(3));
initial.commit09911bf2008-07-26 23:55:29428 EXPECT_EQ(memcmp(&full_hashes[0].hash,
429 "00112233445566778899aabbccddeeff",
430 sizeof(SBFullHash)), 0);
431 EXPECT_EQ(full_hashes[0].list_name, "goog-phish-shavar");
432 EXPECT_EQ(memcmp(&full_hashes[1].hash,
433 "00001111222233334444555566667777",
434 sizeof(SBFullHash)), 0);
435 EXPECT_EQ(full_hashes[1].list_name, "goog-phish-shavar");
436 EXPECT_EQ(memcmp(&full_hashes[2].hash,
437 "ffffeeeeddddccccbbbbaaaa99998888",
438 sizeof(SBFullHash)), 0);
439 EXPECT_EQ(full_hashes[2].list_name, "goog-phish-shavar");
440
441 // Test multiple lists in the GetHash results.
442 std::string get_hash2("goog-phish-shavar:19:32\n"
443 "00112233445566778899aabbccddeeff"
444 "goog-malware-shavar:19:64\n"
445 "cafebeefcafebeefdeaddeaddeaddead"
446 "zzzzyyyyxxxxwwwwvvvvuuuuttttssss");
[email protected]071a3f8c2009-10-14 19:31:58447 EXPECT_TRUE(parser.ParseGetHash(get_hash2.data(),
448 static_cast<int>(get_hash2.length()), "",
449 &re_key,
450 &full_hashes));
initial.commit09911bf2008-07-26 23:55:29451
452 EXPECT_FALSE(re_key);
[email protected]954bc8a52008-09-17 18:16:01453 EXPECT_EQ(full_hashes.size(), static_cast<size_t>(3));
initial.commit09911bf2008-07-26 23:55:29454 EXPECT_EQ(memcmp(&full_hashes[0].hash,
455 "00112233445566778899aabbccddeeff",
456 sizeof(SBFullHash)), 0);
457 EXPECT_EQ(full_hashes[0].list_name, "goog-phish-shavar");
458 EXPECT_EQ(memcmp(&full_hashes[1].hash,
459 "cafebeefcafebeefdeaddeaddeaddead",
460 sizeof(SBFullHash)), 0);
461 EXPECT_EQ(full_hashes[1].list_name, "goog-malware-shavar");
462 EXPECT_EQ(memcmp(&full_hashes[2].hash,
463 "zzzzyyyyxxxxwwwwvvvvuuuuttttssss",
464 sizeof(SBFullHash)), 0);
465 EXPECT_EQ(full_hashes[2].list_name, "goog-malware-shavar");
466}
467
468TEST(SafeBrowsingProtocolParsingTest, TestGetHashWithMac) {
initial.commit09911bf2008-07-26 23:55:29469 const unsigned char get_hash[] = {
470 0x32, 0x56, 0x74, 0x6f, 0x6b, 0x36, 0x64, 0x41,
471 0x51, 0x72, 0x65, 0x51, 0x62, 0x38, 0x51, 0x68,
472 0x59, 0x45, 0x57, 0x51, 0x57, 0x4d, 0x52, 0x65,
473 0x42, 0x63, 0x41, 0x3d, 0x0a, 0x67, 0x6f, 0x6f,
474 0x67, 0x2d, 0x70, 0x68, 0x69, 0x73, 0x68, 0x2d,
475 0x73, 0x68, 0x61, 0x76, 0x61, 0x72, 0x3a, 0x36,
476 0x31, 0x36, 0x39, 0x3a, 0x33, 0x32, 0x0a, 0x17,
477 0x7f, 0x03, 0x42, 0x28, 0x1c, 0x31, 0xb9, 0x0b,
478 0x1c, 0x7b, 0x9d, 0xaf, 0x7b, 0x43, 0x99, 0x10,
479 0xc1, 0xab, 0xe3, 0x1b, 0x35, 0x80, 0x38, 0x96,
480 0xf9, 0x44, 0x4f, 0x28, 0xb4, 0xeb, 0x45
481 };
482
[email protected]4a3dab22009-11-11 17:36:50483 const unsigned char hash_result[] = {
initial.commit09911bf2008-07-26 23:55:29484 0x17, 0x7f, 0x03, 0x42, 0x28, 0x1c, 0x31, 0xb9,
485 0x0b, 0x1c, 0x7b, 0x9d, 0xaf, 0x7b, 0x43, 0x99,
486 0x10, 0xc1, 0xab, 0xe3, 0x1b, 0x35, 0x80, 0x38,
487 0x96, 0xf9, 0x44, 0x4f, 0x28, 0xb4, 0xeb, 0x45
488 };
489
490 const std::string key = "58Lqn5WIP961x3zuLGo5Uw==";
491 std::vector<SBFullHashResult> full_hashes;
492 bool re_key = false;
493 SafeBrowsingProtocolParser parser;
494 EXPECT_TRUE(parser.ParseGetHash(reinterpret_cast<const char*>(get_hash),
495 sizeof(get_hash),
496 key,
497 &re_key,
498 &full_hashes));
499 EXPECT_FALSE(re_key);
[email protected]954bc8a52008-09-17 18:16:01500 EXPECT_EQ(full_hashes.size(), static_cast<size_t>(1));
initial.commit09911bf2008-07-26 23:55:29501 EXPECT_EQ(memcmp(hash_result, &full_hashes[0].hash, sizeof(SBFullHash)), 0);
502}
initial.commit09911bf2008-07-26 23:55:29503
[email protected]3b0f5f62009-01-08 01:22:30504TEST(SafeBrowsingProtocolParsingTest, TestGetHashWithUnknownList) {
505 std::string hash_response = "goog-phish-shavar:1:32\n"
506 "12345678901234567890123456789012"
507 "googpub-phish-shavar:19:32\n"
508 "09876543210987654321098765432109";
509 bool re_key = false;
510 std::string key = "";
511 std::vector<SBFullHashResult> full_hashes;
512 SafeBrowsingProtocolParser parser;
513 EXPECT_TRUE(parser.ParseGetHash(hash_response.data(),
514 hash_response.size(),
515 key,
516 &re_key,
517 &full_hashes));
518
[email protected]3b39a3102009-01-08 01:32:15519 EXPECT_EQ(full_hashes.size(), static_cast<size_t>(1));
[email protected]f0a51fb52009-03-05 12:46:38520 EXPECT_EQ(memcmp("12345678901234567890123456789012",
[email protected]3b0f5f62009-01-08 01:22:30521 &full_hashes[0].hash, sizeof(SBFullHash)), 0);
522 EXPECT_EQ(full_hashes[0].list_name, "goog-phish-shavar");
523 EXPECT_EQ(full_hashes[0].add_chunk_id, 1);
524
525 hash_response += "goog-malware-shavar:7:32\n"
526 "abcdefghijklmnopqrstuvwxyz123457";
527 full_hashes.clear();
528 EXPECT_TRUE(parser.ParseGetHash(hash_response.data(),
529 hash_response.size(),
530 key,
531 &re_key,
532 &full_hashes));
533
[email protected]3b39a3102009-01-08 01:32:15534 EXPECT_EQ(full_hashes.size(), static_cast<size_t>(2));
[email protected]f0a51fb52009-03-05 12:46:38535 EXPECT_EQ(memcmp("12345678901234567890123456789012",
[email protected]3b0f5f62009-01-08 01:22:30536 &full_hashes[0].hash, sizeof(SBFullHash)), 0);
537 EXPECT_EQ(full_hashes[0].list_name, "goog-phish-shavar");
538 EXPECT_EQ(full_hashes[0].add_chunk_id, 1);
[email protected]f0a51fb52009-03-05 12:46:38539 EXPECT_EQ(memcmp("abcdefghijklmnopqrstuvwxyz123457",
[email protected]3b0f5f62009-01-08 01:22:30540 &full_hashes[1].hash, sizeof(SBFullHash)), 0);
541 EXPECT_EQ(full_hashes[1].list_name, "goog-malware-shavar");
542 EXPECT_EQ(full_hashes[1].add_chunk_id, 7);
543}
544
initial.commit09911bf2008-07-26 23:55:29545TEST(SafeBrowsingProtocolParsingTest, TestFormatHash) {
546 SafeBrowsingProtocolParser parser;
547 std::vector<SBPrefix> prefixes;
548 std::string get_hash;
549
550 prefixes.push_back(0x34333231);
551 prefixes.push_back(0x64636261);
552 prefixes.push_back(0x73727170);
553
554 parser.FormatGetHash(prefixes, &get_hash);
555 EXPECT_EQ(get_hash, "4:12\n1234abcdpqrs");
556}
557
558TEST(SafeBrowsingProtocolParsingTest, TestGetKey) {
559 SafeBrowsingProtocolParser parser;
560 std::string key_response("clientkey:10:0123456789\n"
561 "wrappedkey:20:abcdefghijklmnopqrst\n");
562
563 std::string client_key, wrapped_key;
564 EXPECT_TRUE(parser.ParseNewKey(key_response.data(),
565 static_cast<int>(key_response.length()),
566 &client_key,
567 &wrapped_key));
568
569 EXPECT_EQ(client_key, "0123456789");
570 EXPECT_EQ(wrapped_key, "abcdefghijklmnopqrst");
571}
572
573TEST(SafeBrowsingProtocolParsingTest, TestReKey) {
574 SafeBrowsingProtocolParser parser;
575 std::string update("n:1800\ni:phishy\ne:pleaserekey\n");
576
577 bool re_key = false;
578 bool reset = false;
579 int next_update = -1;
580 std::vector<SBChunkDelete> deletes;
581 std::vector<ChunkUrl> urls;
582 EXPECT_TRUE(parser.ParseUpdate(update.data(),
583 static_cast<int>(update.size()), "",
584 &next_update, &re_key,
585 &reset, &deletes, &urls));
586 EXPECT_TRUE(re_key);
587}
588
589TEST(SafeBrowsingProtocolParsingTest, TestReset) {
590 SafeBrowsingProtocolParser parser;
591 std::string update("n:1800\ni:phishy\nr:pleasereset\n");
592
593 bool re_key = false;
594 bool reset = false;
595 int next_update = -1;
596 std::vector<SBChunkDelete> deletes;
597 std::vector<ChunkUrl> urls;
598 EXPECT_TRUE(parser.ParseUpdate(update.data(),
599 static_cast<int>(update.size()), "",
600 &next_update, &re_key,
601 &reset, &deletes, &urls));
602 EXPECT_TRUE(reset);
603}
604
[email protected]445d4cc2008-10-09 21:31:57605// The SafeBrowsing service will occasionally send zero length chunks so that
606// client requests will have longer contiguous chunk number ranges, and thus
607// reduce the request size.
608TEST(SafeBrowsingProtocolParsingTest, TestZeroSizeAddChunk) {
609 std::string add_chunk("a:1:4:0\n");
610 SafeBrowsingProtocolParser parser;
611 bool re_key = false;
612 std::deque<SBChunk> chunks;
613
614 bool result = parser.ParseChunk(add_chunk.data(),
615 static_cast<int>(add_chunk.length()),
616 "", "", &re_key, &chunks);
617 EXPECT_TRUE(result);
618 EXPECT_EQ(chunks.size(), static_cast<size_t>(1));
619 EXPECT_EQ(chunks[0].chunk_number, 1);
620 EXPECT_EQ(chunks[0].hosts.size(), static_cast<size_t>(0));
621
622 safe_browsing_util::FreeChunks(&chunks);
623
624 // Now test a zero size chunk in between normal chunks.
625 chunks.clear();
626 std::string add_chunks("a:1:4:18\n1234\001abcd5678\001wxyz"
627 "a:2:4:0\n"
628 "a:3:4:9\ncafe\001beef");
629 result = parser.ParseChunk(add_chunks.data(),
630 static_cast<int>(add_chunks.length()),
631 "", "", &re_key, &chunks);
632 EXPECT_TRUE(result);
633 EXPECT_EQ(chunks.size(), static_cast<size_t>(3));
634
635 // See that each chunk has the right content.
636 EXPECT_EQ(chunks[0].chunk_number, 1);
637 EXPECT_EQ(chunks[0].hosts.size(), static_cast<size_t>(2));
638 EXPECT_EQ(chunks[0].hosts[0].host, 0x34333231);
639 EXPECT_EQ(chunks[0].hosts[0].entry->PrefixAt(0), 0x64636261);
640 EXPECT_EQ(chunks[0].hosts[1].host, 0x38373635);
641 EXPECT_EQ(chunks[0].hosts[1].entry->PrefixAt(0), 0x7a797877);
642
643 EXPECT_EQ(chunks[1].chunk_number, 2);
644 EXPECT_EQ(chunks[1].hosts.size(), static_cast<size_t>(0));
[email protected]f0a51fb52009-03-05 12:46:38645
[email protected]445d4cc2008-10-09 21:31:57646 EXPECT_EQ(chunks[2].chunk_number, 3);
647 EXPECT_EQ(chunks[2].hosts.size(), static_cast<size_t>(1));
648 EXPECT_EQ(chunks[2].hosts[0].host, 0x65666163);
649 EXPECT_EQ(chunks[2].hosts[0].entry->PrefixAt(0), 0x66656562);
650
651 safe_browsing_util::FreeChunks(&chunks);
652}
653
654// Test parsing a zero sized sub chunk.
655TEST(SafeBrowsingProtocolParsingTest, TestZeroSizeSubChunk) {
656 std::string sub_chunk("s:9:4:0\n");
657 SafeBrowsingProtocolParser parser;
658 bool re_key = false;
659 std::deque<SBChunk> chunks;
660
661 bool result = parser.ParseChunk(sub_chunk.data(),
662 static_cast<int>(sub_chunk.length()),
663 "", "", &re_key, &chunks);
664 EXPECT_TRUE(result);
665 EXPECT_EQ(chunks.size(), static_cast<size_t>(1));
666 EXPECT_EQ(chunks[0].chunk_number, 9);
667 EXPECT_EQ(chunks[0].hosts.size(), static_cast<size_t>(0));
668
669 safe_browsing_util::FreeChunks(&chunks);
670 chunks.clear();
671
672 // Test parsing a zero sized sub chunk mixed in with content carrying chunks.
673 std::string sub_chunks("s:1:4:9\nabcdxwxyz"
674 "s:2:4:0\n"
675 "s:3:4:26\nefgh\0011234pqrscafe\0015678lmno");
676 sub_chunks[12] = '\0';
677
678 result = parser.ParseChunk(sub_chunks.data(),
679 static_cast<int>(sub_chunks.length()),
680 "", "", &re_key, &chunks);
681 EXPECT_TRUE(result);
682
683 EXPECT_EQ(chunks[0].chunk_number, 1);
684 EXPECT_EQ(chunks[0].hosts.size(), static_cast<size_t>(1));
685 EXPECT_EQ(chunks[0].hosts[0].host, 0x64636261);
686 EXPECT_EQ(chunks[0].hosts[0].entry->prefix_count(), 0);
687
688 EXPECT_EQ(chunks[1].chunk_number, 2);
689 EXPECT_EQ(chunks[1].hosts.size(), static_cast<size_t>(0));
690
691 EXPECT_EQ(chunks[2].chunk_number, 3);
692 EXPECT_EQ(chunks[2].hosts.size(), static_cast<size_t>(2));
693 EXPECT_EQ(chunks[2].hosts[0].host, 0x68676665);
694 EXPECT_EQ(chunks[2].hosts[0].entry->prefix_count(), 1);
695 EXPECT_EQ(chunks[2].hosts[0].entry->PrefixAt(0), 0x73727170);
696 EXPECT_EQ(chunks[2].hosts[0].entry->ChunkIdAtPrefix(0), 0x31323334);
697 EXPECT_EQ(chunks[2].hosts[1].host, 0x65666163);
698 EXPECT_EQ(chunks[2].hosts[1].entry->prefix_count(), 1);
699 EXPECT_EQ(chunks[2].hosts[1].entry->PrefixAt(0), 0x6f6e6d6c);
700 EXPECT_EQ(chunks[2].hosts[1].entry->ChunkIdAtPrefix(0), 0x35363738);
701
702 safe_browsing_util::FreeChunks(&chunks);
703}
704
initial.commit09911bf2008-07-26 23:55:29705TEST(SafeBrowsingProtocolParsingTest, TestVerifyUpdateMac) {
initial.commit09911bf2008-07-26 23:55:29706 SafeBrowsingProtocolParser parser;
707
708 const std::string update =
709 "m:XIU0LiQhAPJq6dynXwHbygjS5tw=\n"
710 "n:1895\n"
711 "i:goog-phish-shavar\n"
[email protected]d3216442009-03-05 21:07:27712 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6501-6505:6501-6505,"
713 "pcY6iVeT9-CBQ3fdAF0rpnKjR1Y=\n"
714 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6506-6510:6506-6510,"
715 "SDBrYC3rX3KEPe72LOypnP6QYac=\n"
716 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6511-6520:6511-6520,"
717 "9UQo-e7OkcsXT2wFWTAhOuWOsUs=\n"
718 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6521-6560:6521-6560,"
719 "qVNw6JIpR1q6PIXST7J4LJ9n3Zg=\n"
720 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6561-6720:6561-6720,"
721 "7OiJvCbiwvpzPITW-hQohY5NHuc=\n"
722 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6721-6880:6721-6880,"
723 "oBS3svhoi9deIa0sWZ_gnD0ujj8=\n"
724 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6881-7040:6881-7040,"
725 "a0r8Xit4VvH39xgyQHZTPczKBIE=\n"
726 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_7041-7200:7041-7163,"
727 "q538LChutGknBw55s6kcE2wTcvU=\n"
728 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8001-8160:8001-8024,"
729 "8026-8045,8048-8049,8051-8134,8136-8152,8155-8160,"
730 "j6XXAEWnjYk9tVVLBSdQvIEq2Wg=\n"
731 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8161-8320:8161-8215,"
732 "8217-8222,8224-8320,YaNfiqdQOt-uLCLWVLj46AZpAjQ=\n"
733 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8321-8480:8321-8391,"
734 "8393-8399,8402,8404-8419,8421-8425,8427,8431-8433,8435-8439,8441-8443,"
735 "8445-8446,8448-8480,ALj31GQMwGiIeU3bM2ZYKITfU-U=\n"
736 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8481-8640:8481-8500,"
737 "8502-8508,8510-8511,8513-8517,8519-8525,8527-8531,8533,8536-8539,"
738 "8541-8576,8578-8638,8640,TlQYRmS_kZ5PBAUIUyNQDq0Jprs=\n"
739 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8641-8800:8641-8689,"
740 "8691-8731,8733-8786,x1Qf7hdNrO8b6yym03ZzNydDS1o=\n";
initial.commit09911bf2008-07-26 23:55:29741
742 bool re_key = false;
743 bool reset = false;
744 int next_update = -1;
745 std::vector<SBChunkDelete> deletes;
746 std::vector<ChunkUrl> urls;
747 const std::string key("58Lqn5WIP961x3zuLGo5Uw==");
748 EXPECT_TRUE(parser.ParseUpdate(update.data(),
749 static_cast<int>(update.size()), key,
750 &next_update, &re_key,
751 &reset, &deletes, &urls));
752 EXPECT_FALSE(re_key);
753 EXPECT_EQ(next_update, 1895);
754}
755
756TEST(SafeBrowsingProtocolParsingTest, TestVerifyChunkMac) {
initial.commit09911bf2008-07-26 23:55:29757 SafeBrowsingProtocolParser parser;
758
759 const unsigned char chunk[] = {
760 0x73, 0x3a, 0x32, 0x30, 0x30, 0x32, 0x3a, 0x34,
761 0x3a, 0x32, 0x32, 0x0a, 0x2f, 0x4f, 0x89, 0x7a,
762 0x01, 0x00, 0x00, 0x0a, 0x59, 0xc8, 0x71, 0xdf,
763 0x9d, 0x29, 0x0c, 0xba, 0xd7, 0x00, 0x00, 0x00,
764 0x0a, 0x59
765 };
766
767 bool re_key = false;
768 std::deque<SBChunk> chunks;
769 const std::string key("v_aDSz6jI92WeHCOoZ07QA==");
770 const std::string mac("W9Xp2fUcQ9V66If6Cvsrstpa4Kk=");
771
772 EXPECT_TRUE(parser.ParseChunk(reinterpret_cast<const char*>(chunk),
773 sizeof(chunk), key, mac,
774 &re_key, &chunks));
775 EXPECT_FALSE(re_key);
776
777 safe_browsing_util::FreeChunks(&chunks);
license.botbf09a502008-08-24 00:55:55778}