OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // Program to test the SafeBrowsing protocol parsing v2.1. | 5 // Program to test the SafeBrowsing protocol parsing v2.1. |
6 | 6 |
7 #include "base/stringprintf.h" | 7 #include "base/stringprintf.h" |
8 #include "chrome/browser/safe_browsing/protocol_parser.h" | 8 #include "chrome/browser/safe_browsing/protocol_parser.h" |
9 #include "chrome/browser/safe_browsing/safe_browsing_util.h" | 9 #include "chrome/browser/safe_browsing/safe_browsing_util.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 // Test parsing one add chunk. | 12 // Test parsing one add chunk. |
13 TEST(SafeBrowsingProtocolParsingTest, TestAddChunk) { | 13 TEST(SafeBrowsingProtocolParsingTest, TestAddChunk) { |
14 std::string add_chunk("a:1:4:35\naaaax1111\0032222333344447777\00288889999"); | 14 std::string add_chunk("a:1:4:35\naaaax1111\0032222333344447777\00288889999"); |
15 add_chunk[13] = '\0'; | 15 add_chunk[13] = '\0'; |
16 | 16 |
17 // Run the parse. | 17 // Run the parse. |
18 SafeBrowsingProtocolParser parser; | 18 SafeBrowsingProtocolParser parser; |
19 bool re_key = false; | |
20 SBChunkList chunks; | 19 SBChunkList chunks; |
21 bool result = parser.ParseChunk( | 20 bool result = parser.ParseChunk( |
22 safe_browsing_util::kMalwareList, | 21 safe_browsing_util::kMalwareList, |
23 add_chunk.data(), | 22 add_chunk.data(), |
24 static_cast<int>(add_chunk.length()), | 23 static_cast<int>(add_chunk.length()), |
25 "", "", &re_key, &chunks); | 24 &chunks); |
26 EXPECT_TRUE(result); | 25 EXPECT_TRUE(result); |
27 EXPECT_FALSE(re_key); | |
28 EXPECT_EQ(chunks.size(), 1U); | 26 EXPECT_EQ(chunks.size(), 1U); |
29 EXPECT_EQ(chunks[0].chunk_number, 1); | 27 EXPECT_EQ(chunks[0].chunk_number, 1); |
30 EXPECT_EQ(chunks[0].hosts.size(), 3U); | 28 EXPECT_EQ(chunks[0].hosts.size(), 3U); |
31 | 29 |
32 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161); | 30 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161); |
33 SBEntry* entry = chunks[0].hosts[0].entry; | 31 SBEntry* entry = chunks[0].hosts[0].entry; |
34 EXPECT_TRUE(entry->IsAdd()); | 32 EXPECT_TRUE(entry->IsAdd()); |
35 EXPECT_TRUE(entry->IsPrefix()); | 33 EXPECT_TRUE(entry->IsPrefix()); |
36 EXPECT_EQ(entry->prefix_count(), 0); | 34 EXPECT_EQ(entry->prefix_count(), 0); |
37 | 35 |
(...skipping 24 matching lines...) Expand all Loading... |
62 for (int i = 0; i < 32; ++i) { | 60 for (int i = 0; i < 32; ++i) { |
63 full_hash1.full_hash[i] = i % 2 ? 1 : 2; | 61 full_hash1.full_hash[i] = i % 2 ? 1 : 2; |
64 full_hash2.full_hash[i] = i % 2 ? 3 : 4; | 62 full_hash2.full_hash[i] = i % 2 ? 3 : 4; |
65 } | 63 } |
66 | 64 |
67 add_chunk.append(full_hash1.full_hash, 32); | 65 add_chunk.append(full_hash1.full_hash, 32); |
68 add_chunk.append(full_hash2.full_hash, 32); | 66 add_chunk.append(full_hash2.full_hash, 32); |
69 | 67 |
70 // Run the parse. | 68 // Run the parse. |
71 SafeBrowsingProtocolParser parser; | 69 SafeBrowsingProtocolParser parser; |
72 bool re_key = false; | |
73 SBChunkList chunks; | 70 SBChunkList chunks; |
74 bool result = parser.ParseChunk( | 71 bool result = parser.ParseChunk( |
75 safe_browsing_util::kMalwareList, | 72 safe_browsing_util::kMalwareList, |
76 add_chunk.data(), | 73 add_chunk.data(), |
77 static_cast<int>(add_chunk.length()), | 74 static_cast<int>(add_chunk.length()), |
78 "", "", &re_key, &chunks); | 75 &chunks); |
79 EXPECT_TRUE(result); | 76 EXPECT_TRUE(result); |
80 EXPECT_FALSE(re_key); | |
81 EXPECT_EQ(chunks.size(), 1U); | 77 EXPECT_EQ(chunks.size(), 1U); |
82 EXPECT_EQ(chunks[0].chunk_number, 1); | 78 EXPECT_EQ(chunks[0].chunk_number, 1); |
83 EXPECT_EQ(chunks[0].hosts.size(), 1U); | 79 EXPECT_EQ(chunks[0].hosts.size(), 1U); |
84 | 80 |
85 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161); | 81 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161); |
86 SBEntry* entry = chunks[0].hosts[0].entry; | 82 SBEntry* entry = chunks[0].hosts[0].entry; |
87 EXPECT_TRUE(entry->IsAdd()); | 83 EXPECT_TRUE(entry->IsAdd()); |
88 EXPECT_FALSE(entry->IsPrefix()); | 84 EXPECT_FALSE(entry->IsPrefix()); |
89 EXPECT_EQ(entry->prefix_count(), 2); | 85 EXPECT_EQ(entry->prefix_count(), 2); |
90 EXPECT_TRUE(entry->FullHashAt(0) == full_hash1); | 86 EXPECT_TRUE(entry->FullHashAt(0) == full_hash1); |
91 EXPECT_TRUE(entry->FullHashAt(1) == full_hash2); | 87 EXPECT_TRUE(entry->FullHashAt(1) == full_hash2); |
92 } | 88 } |
93 | 89 |
94 // Test parsing multiple add chunks. We'll use the same chunk as above, and add | 90 // Test parsing multiple add chunks. We'll use the same chunk as above, and add |
95 // one more after it. | 91 // one more after it. |
96 TEST(SafeBrowsingProtocolParsingTest, TestAddChunks) { | 92 TEST(SafeBrowsingProtocolParsingTest, TestAddChunks) { |
97 std::string add_chunk("a:1:4:35\naaaax1111\0032222333344447777\00288889999" | 93 std::string add_chunk("a:1:4:35\naaaax1111\0032222333344447777\00288889999" |
98 "a:2:4:13\n5555\002ppppgggg"); | 94 "a:2:4:13\n5555\002ppppgggg"); |
99 add_chunk[13] = '\0'; | 95 add_chunk[13] = '\0'; |
100 | 96 |
101 // Run the parse. | 97 // Run the parse. |
102 SafeBrowsingProtocolParser parser; | 98 SafeBrowsingProtocolParser parser; |
103 bool re_key = false; | |
104 SBChunkList chunks; | 99 SBChunkList chunks; |
105 bool result = parser.ParseChunk( | 100 bool result = parser.ParseChunk( |
106 safe_browsing_util::kMalwareList, | 101 safe_browsing_util::kMalwareList, |
107 add_chunk.data(), | 102 add_chunk.data(), |
108 static_cast<int>(add_chunk.length()), | 103 static_cast<int>(add_chunk.length()), |
109 "", "", &re_key, &chunks); | 104 &chunks); |
110 EXPECT_TRUE(result); | 105 EXPECT_TRUE(result); |
111 EXPECT_FALSE(re_key); | |
112 EXPECT_EQ(chunks.size(), 2U); | 106 EXPECT_EQ(chunks.size(), 2U); |
113 EXPECT_EQ(chunks[0].chunk_number, 1); | 107 EXPECT_EQ(chunks[0].chunk_number, 1); |
114 EXPECT_EQ(chunks[0].hosts.size(), 3U); | 108 EXPECT_EQ(chunks[0].hosts.size(), 3U); |
115 | 109 |
116 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161); | 110 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161); |
117 SBEntry* entry = chunks[0].hosts[0].entry; | 111 SBEntry* entry = chunks[0].hosts[0].entry; |
118 EXPECT_TRUE(entry->IsAdd()); | 112 EXPECT_TRUE(entry->IsAdd()); |
119 EXPECT_TRUE(entry->IsPrefix()); | 113 EXPECT_TRUE(entry->IsPrefix()); |
120 EXPECT_EQ(entry->prefix_count(), 0); | 114 EXPECT_EQ(entry->prefix_count(), 0); |
121 | 115 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 add_chunk[add_chunk.size() - 1] |= 0xFF; | 149 add_chunk[add_chunk.size() - 1] |= 0xFF; |
156 for (int i = 0; i < 255; ++i) | 150 for (int i = 0; i < 255; ++i) |
157 add_chunk.append(base::StringPrintf("%04d", i)); | 151 add_chunk.append(base::StringPrintf("%04d", i)); |
158 | 152 |
159 add_chunk.append("aaaa"); | 153 add_chunk.append("aaaa"); |
160 add_chunk.push_back(5); | 154 add_chunk.push_back(5); |
161 for (int i = 0; i < 5; ++i) | 155 for (int i = 0; i < 5; ++i) |
162 add_chunk.append(base::StringPrintf("001%d", i)); | 156 add_chunk.append(base::StringPrintf("001%d", i)); |
163 | 157 |
164 SafeBrowsingProtocolParser parser; | 158 SafeBrowsingProtocolParser parser; |
165 bool re_key = false; | |
166 SBChunkList chunks; | 159 SBChunkList chunks; |
167 bool result = parser.ParseChunk( | 160 bool result = parser.ParseChunk( |
168 safe_browsing_util::kMalwareList, | 161 safe_browsing_util::kMalwareList, |
169 add_chunk.data(), | 162 add_chunk.data(), |
170 static_cast<int>(add_chunk.length()), | 163 static_cast<int>(add_chunk.length()), |
171 "", "", &re_key, &chunks); | 164 &chunks); |
172 EXPECT_TRUE(result); | 165 EXPECT_TRUE(result); |
173 EXPECT_FALSE(re_key); | |
174 EXPECT_EQ(chunks.size(), 1U); | 166 EXPECT_EQ(chunks.size(), 1U); |
175 EXPECT_EQ(chunks[0].chunk_number, 1); | 167 EXPECT_EQ(chunks[0].chunk_number, 1); |
176 | 168 |
177 EXPECT_EQ(chunks[0].hosts.size(), 2U); | 169 EXPECT_EQ(chunks[0].hosts.size(), 2U); |
178 | 170 |
179 const SBChunkHost& host0 = chunks[0].hosts[0]; | 171 const SBChunkHost& host0 = chunks[0].hosts[0]; |
180 EXPECT_EQ(host0.host, 0x61616161); | 172 EXPECT_EQ(host0.host, 0x61616161); |
181 EXPECT_EQ(host0.entry->prefix_count(), 255); | 173 EXPECT_EQ(host0.entry->prefix_count(), 255); |
182 | 174 |
183 const SBChunkHost& host1 = chunks[0].hosts[1]; | 175 const SBChunkHost& host1 = chunks[0].hosts[1]; |
184 EXPECT_EQ(host1.host, 0x61616161); | 176 EXPECT_EQ(host1.host, 0x61616161); |
185 EXPECT_EQ(host1.entry->prefix_count(), 5); | 177 EXPECT_EQ(host1.entry->prefix_count(), 5); |
186 } | 178 } |
187 | 179 |
188 // Test to make sure we could deal with truncated bin hash chunk. | 180 // Test to make sure we could deal with truncated bin hash chunk. |
189 TEST(SafeBrowsingProtocolParsingTest, TestTruncatedBinHashChunk) { | 181 TEST(SafeBrowsingProtocolParsingTest, TestTruncatedBinHashChunk) { |
190 // This chunk delares there are 4 prefixes but actually only contains 2. | 182 // This chunk delares there are 4 prefixes but actually only contains 2. |
191 const char add_chunk[] = "a:1:4:16\n11112222"; | 183 const char add_chunk[] = "a:1:4:16\n11112222"; |
192 SafeBrowsingProtocolParser parser; | 184 SafeBrowsingProtocolParser parser; |
193 bool re_key = false; | |
194 SBChunkList chunks; | 185 SBChunkList chunks; |
195 bool result = parser.ParseChunk(safe_browsing_util::kBinHashList, | 186 bool result = parser.ParseChunk(safe_browsing_util::kBinHashList, |
196 add_chunk, | 187 add_chunk, |
197 static_cast<int>(sizeof(add_chunk)), | 188 static_cast<int>(sizeof(add_chunk)), |
198 "", "", &re_key, &chunks); | 189 &chunks); |
199 EXPECT_FALSE(result); | 190 EXPECT_FALSE(result); |
200 EXPECT_FALSE(re_key); | |
201 EXPECT_EQ(chunks.size(), 0U); | 191 EXPECT_EQ(chunks.size(), 0U); |
202 } | 192 } |
203 | 193 |
204 // Test to make sure we could deal with truncated malwarelist chunk. | 194 // Test to make sure we could deal with truncated malwarelist chunk. |
205 TEST(SafeBrowsingProtocolParsingTest, TestTruncatedUrlHashChunk) { | 195 TEST(SafeBrowsingProtocolParsingTest, TestTruncatedUrlHashChunk) { |
206 // This chunk delares there are 4 prefixes but actually only contains 2. | 196 // This chunk delares there are 4 prefixes but actually only contains 2. |
207 const char add_chunk[] = "a:1:4:21\naaaa\00411112222"; | 197 const char add_chunk[] = "a:1:4:21\naaaa\00411112222"; |
208 SafeBrowsingProtocolParser parser; | 198 SafeBrowsingProtocolParser parser; |
209 bool re_key = false; | |
210 SBChunkList chunks; | 199 SBChunkList chunks; |
211 | 200 |
212 // For safe_browsing_util::kMalwareList. | 201 // For safe_browsing_util::kMalwareList. |
213 bool result = parser.ParseChunk(safe_browsing_util::kMalwareList, | 202 bool result = parser.ParseChunk(safe_browsing_util::kMalwareList, |
214 add_chunk, | 203 add_chunk, |
215 static_cast<int>(sizeof(add_chunk)), | 204 static_cast<int>(sizeof(add_chunk)), |
216 "", "", &re_key, &chunks); | 205 &chunks); |
217 EXPECT_FALSE(result); | 206 EXPECT_FALSE(result); |
218 EXPECT_FALSE(re_key); | |
219 EXPECT_EQ(chunks.size(), 0U); | 207 EXPECT_EQ(chunks.size(), 0U); |
220 | 208 |
221 // For safe_browsing_util::kPhishingList. | 209 // For safe_browsing_util::kPhishingList. |
222 result = parser.ParseChunk(safe_browsing_util::kPhishingList, | 210 result = parser.ParseChunk(safe_browsing_util::kPhishingList, |
223 add_chunk, | 211 add_chunk, |
224 static_cast<int>(sizeof(add_chunk)), | 212 static_cast<int>(sizeof(add_chunk)), |
225 "", "", &re_key, &chunks); | 213 &chunks); |
226 EXPECT_FALSE(result); | 214 EXPECT_FALSE(result); |
227 EXPECT_FALSE(re_key); | |
228 EXPECT_EQ(chunks.size(), 0U); | 215 EXPECT_EQ(chunks.size(), 0U); |
229 | 216 |
230 // For safe_browsing_util::kBinUrlList. | 217 // For safe_browsing_util::kBinUrlList. |
231 result = parser.ParseChunk(safe_browsing_util::kBinUrlList, | 218 result = parser.ParseChunk(safe_browsing_util::kBinUrlList, |
232 add_chunk, | 219 add_chunk, |
233 static_cast<int>(sizeof(add_chunk)), | 220 static_cast<int>(sizeof(add_chunk)), |
234 "", "", &re_key, &chunks); | 221 &chunks); |
235 EXPECT_FALSE(result); | 222 EXPECT_FALSE(result); |
236 EXPECT_FALSE(re_key); | |
237 EXPECT_EQ(chunks.size(), 0U); | 223 EXPECT_EQ(chunks.size(), 0U); |
238 } | 224 } |
239 | 225 |
240 // Test parsing one sub chunk. | 226 // Test parsing one sub chunk. |
241 TEST(SafeBrowsingProtocolParsingTest, TestSubChunk) { | 227 TEST(SafeBrowsingProtocolParsingTest, TestSubChunk) { |
242 std::string sub_chunk("s:9:4:59\naaaaxkkkk1111\003" | 228 std::string sub_chunk("s:9:4:59\naaaaxkkkk1111\003" |
243 "zzzz2222zzzz3333zzzz4444" | 229 "zzzz2222zzzz3333zzzz4444" |
244 "7777\002yyyy8888yyyy9999"); | 230 "7777\002yyyy8888yyyy9999"); |
245 sub_chunk[13] = '\0'; | 231 sub_chunk[13] = '\0'; |
246 | 232 |
247 // Run the parse. | 233 // Run the parse. |
248 SafeBrowsingProtocolParser parser; | 234 SafeBrowsingProtocolParser parser; |
249 bool re_key = false; | |
250 SBChunkList chunks; | 235 SBChunkList chunks; |
251 bool result = parser.ParseChunk( | 236 bool result = parser.ParseChunk( |
252 safe_browsing_util::kMalwareList, | 237 safe_browsing_util::kMalwareList, |
253 sub_chunk.data(), | 238 sub_chunk.data(), |
254 static_cast<int>(sub_chunk.length()), | 239 static_cast<int>(sub_chunk.length()), |
255 "", "", &re_key, &chunks); | 240 &chunks); |
256 EXPECT_TRUE(result); | 241 EXPECT_TRUE(result); |
257 EXPECT_FALSE(re_key); | |
258 EXPECT_EQ(chunks.size(), 1U); | 242 EXPECT_EQ(chunks.size(), 1U); |
259 EXPECT_EQ(chunks[0].chunk_number, 9); | 243 EXPECT_EQ(chunks[0].chunk_number, 9); |
260 EXPECT_EQ(chunks[0].hosts.size(), 3U); | 244 EXPECT_EQ(chunks[0].hosts.size(), 3U); |
261 | 245 |
262 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161); | 246 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161); |
263 SBEntry* entry = chunks[0].hosts[0].entry; | 247 SBEntry* entry = chunks[0].hosts[0].entry; |
264 EXPECT_TRUE(entry->IsSub()); | 248 EXPECT_TRUE(entry->IsSub()); |
265 EXPECT_TRUE(entry->IsPrefix()); | 249 EXPECT_TRUE(entry->IsPrefix()); |
266 EXPECT_EQ(entry->chunk_id(), 0x6b6b6b6b); | 250 EXPECT_EQ(entry->chunk_id(), 0x6b6b6b6b); |
267 EXPECT_EQ(entry->prefix_count(), 0); | 251 EXPECT_EQ(entry->prefix_count(), 0); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 full_hash2.full_hash[i] = i % 2 ? 3 : 4; | 284 full_hash2.full_hash[i] = i % 2 ? 3 : 4; |
301 } | 285 } |
302 | 286 |
303 sub_chunk.append("yyyy"); | 287 sub_chunk.append("yyyy"); |
304 sub_chunk.append(full_hash1.full_hash, 32); | 288 sub_chunk.append(full_hash1.full_hash, 32); |
305 sub_chunk.append("zzzz"); | 289 sub_chunk.append("zzzz"); |
306 sub_chunk.append(full_hash2.full_hash, 32); | 290 sub_chunk.append(full_hash2.full_hash, 32); |
307 | 291 |
308 // Run the parse. | 292 // Run the parse. |
309 SafeBrowsingProtocolParser parser; | 293 SafeBrowsingProtocolParser parser; |
310 bool re_key = false; | |
311 SBChunkList chunks; | 294 SBChunkList chunks; |
312 bool result = parser.ParseChunk( | 295 bool result = parser.ParseChunk( |
313 safe_browsing_util::kMalwareList, | 296 safe_browsing_util::kMalwareList, |
314 sub_chunk.data(), | 297 sub_chunk.data(), |
315 static_cast<int>(sub_chunk.length()), | 298 static_cast<int>(sub_chunk.length()), |
316 "", "", &re_key, &chunks); | 299 &chunks); |
317 EXPECT_TRUE(result); | 300 EXPECT_TRUE(result); |
318 EXPECT_FALSE(re_key); | |
319 EXPECT_EQ(chunks.size(), 1U); | 301 EXPECT_EQ(chunks.size(), 1U); |
320 EXPECT_EQ(chunks[0].chunk_number, 1); | 302 EXPECT_EQ(chunks[0].chunk_number, 1); |
321 EXPECT_EQ(chunks[0].hosts.size(), 1U); | 303 EXPECT_EQ(chunks[0].hosts.size(), 1U); |
322 | 304 |
323 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161); | 305 EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161); |
324 SBEntry* entry = chunks[0].hosts[0].entry; | 306 SBEntry* entry = chunks[0].hosts[0].entry; |
325 EXPECT_TRUE(entry->IsSub()); | 307 EXPECT_TRUE(entry->IsSub()); |
326 EXPECT_FALSE(entry->IsPrefix()); | 308 EXPECT_FALSE(entry->IsPrefix()); |
327 EXPECT_EQ(entry->prefix_count(), 2); | 309 EXPECT_EQ(entry->prefix_count(), 2); |
328 EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x79797979); | 310 EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x79797979); |
329 EXPECT_TRUE(entry->FullHashAt(0) == full_hash1); | 311 EXPECT_TRUE(entry->FullHashAt(0) == full_hash1); |
330 EXPECT_EQ(entry->ChunkIdAtPrefix(1), 0x7a7a7a7a); | 312 EXPECT_EQ(entry->ChunkIdAtPrefix(1), 0x7a7a7a7a); |
331 EXPECT_TRUE(entry->FullHashAt(1) == full_hash2); | 313 EXPECT_TRUE(entry->FullHashAt(1) == full_hash2); |
332 } | 314 } |
333 | 315 |
334 // Test parsing the SafeBrowsing update response. | 316 // Test parsing the SafeBrowsing update response. |
335 TEST(SafeBrowsingProtocolParsingTest, TestChunkDelete) { | 317 TEST(SafeBrowsingProtocolParsingTest, TestChunkDelete) { |
336 std::string add_del("n:1700\ni:phishy\nad:1-7,43-597,44444,99999\n" | 318 std::string add_del("n:1700\ni:phishy\nad:1-7,43-597,44444,99999\n" |
337 "i:malware\nsd:21-27,42,171717\n"); | 319 "i:malware\nsd:21-27,42,171717\n"); |
338 | 320 |
339 SafeBrowsingProtocolParser parser; | 321 SafeBrowsingProtocolParser parser; |
340 int next_query_sec = 0; | 322 int next_query_sec = 0; |
341 bool re_key = false; | |
342 bool reset = false; | 323 bool reset = false; |
343 std::vector<SBChunkDelete> deletes; | 324 std::vector<SBChunkDelete> deletes; |
344 std::vector<ChunkUrl> urls; | 325 std::vector<ChunkUrl> urls; |
345 EXPECT_TRUE(parser.ParseUpdate(add_del.data(), | 326 EXPECT_TRUE(parser.ParseUpdate(add_del.data(), |
346 static_cast<int>(add_del.length()), "", | 327 static_cast<int>(add_del.length()), |
347 &next_query_sec, &re_key, | 328 &next_query_sec, &reset, &deletes, &urls)); |
348 &reset, &deletes, &urls)); | |
349 | 329 |
350 EXPECT_TRUE(urls.empty()); | 330 EXPECT_TRUE(urls.empty()); |
351 EXPECT_FALSE(re_key); | |
352 EXPECT_FALSE(reset); | 331 EXPECT_FALSE(reset); |
353 EXPECT_EQ(next_query_sec, 1700); | 332 EXPECT_EQ(next_query_sec, 1700); |
354 EXPECT_EQ(deletes.size(), 2U); | 333 EXPECT_EQ(deletes.size(), 2U); |
355 | 334 |
356 EXPECT_EQ(deletes[0].chunk_del.size(), 4U); | 335 EXPECT_EQ(deletes[0].chunk_del.size(), 4U); |
357 EXPECT_TRUE(deletes[0].chunk_del[0] == ChunkRange(1, 7)); | 336 EXPECT_TRUE(deletes[0].chunk_del[0] == ChunkRange(1, 7)); |
358 EXPECT_TRUE(deletes[0].chunk_del[1] == ChunkRange(43, 597)); | 337 EXPECT_TRUE(deletes[0].chunk_del[1] == ChunkRange(43, 597)); |
359 EXPECT_TRUE(deletes[0].chunk_del[2] == ChunkRange(44444)); | 338 EXPECT_TRUE(deletes[0].chunk_del[2] == ChunkRange(44444)); |
360 EXPECT_TRUE(deletes[0].chunk_del[3] == ChunkRange(99999)); | 339 EXPECT_TRUE(deletes[0].chunk_del[3] == ChunkRange(99999)); |
361 | 340 |
362 EXPECT_EQ(deletes[1].chunk_del.size(), 3U); | 341 EXPECT_EQ(deletes[1].chunk_del.size(), 3U); |
363 EXPECT_TRUE(deletes[1].chunk_del[0] == ChunkRange(21, 27)); | 342 EXPECT_TRUE(deletes[1].chunk_del[0] == ChunkRange(21, 27)); |
364 EXPECT_TRUE(deletes[1].chunk_del[1] == ChunkRange(42)); | 343 EXPECT_TRUE(deletes[1].chunk_del[1] == ChunkRange(42)); |
365 EXPECT_TRUE(deletes[1].chunk_del[2] == ChunkRange(171717)); | 344 EXPECT_TRUE(deletes[1].chunk_del[2] == ChunkRange(171717)); |
366 | 345 |
367 // An update response with missing list name. | 346 // An update response with missing list name. |
368 | 347 |
369 next_query_sec = 0; | 348 next_query_sec = 0; |
370 deletes.clear(); | 349 deletes.clear(); |
371 urls.clear(); | 350 urls.clear(); |
372 add_del = "n:1700\nad:1-7,43-597,44444,99999\ni:malware\nsd:4,21-27171717\n"; | 351 add_del = "n:1700\nad:1-7,43-597,44444,99999\ni:malware\nsd:4,21-27171717\n"; |
373 EXPECT_FALSE(parser.ParseUpdate(add_del.data(), | 352 EXPECT_FALSE(parser.ParseUpdate(add_del.data(), |
374 static_cast<int>(add_del.length()), "", | 353 static_cast<int>(add_del.length()), |
375 &next_query_sec, &re_key, | 354 &next_query_sec, &reset, &deletes, &urls)); |
376 &reset, &deletes, &urls)); | |
377 } | 355 } |
378 | 356 |
379 // Test parsing the SafeBrowsing update response. | 357 // Test parsing the SafeBrowsing update response. |
380 TEST(SafeBrowsingProtocolParsingTest, TestRedirects) { | 358 TEST(SafeBrowsingProtocolParsingTest, TestRedirects) { |
381 std::string redirects("i:goog-malware-shavar\n" | 359 std::string redirects("i:goog-malware-shavar\n" |
382 "u:cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_1\n" | 360 "u:cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_1\n" |
383 "u:cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_2\n" | 361 "u:cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_2\n" |
384 "u:cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_3\n" | 362 "u:cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_3\n" |
385 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8641-8800:8641-8689," | 363 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8641-8800:8641-8689," |
386 "8691-8731,8733-8786\n"); | 364 "8691-8731,8733-8786\n"); |
387 | 365 |
388 SafeBrowsingProtocolParser parser; | 366 SafeBrowsingProtocolParser parser; |
389 int next_query_sec = 0; | 367 int next_query_sec = 0; |
390 bool re_key = false; | |
391 bool reset = false; | 368 bool reset = false; |
392 std::vector<SBChunkDelete> deletes; | 369 std::vector<SBChunkDelete> deletes; |
393 std::vector<ChunkUrl> urls; | 370 std::vector<ChunkUrl> urls; |
394 EXPECT_TRUE(parser.ParseUpdate(redirects.data(), | 371 EXPECT_TRUE(parser.ParseUpdate(redirects.data(), |
395 static_cast<int>(redirects.length()), "", | 372 static_cast<int>(redirects.length()), |
396 &next_query_sec, &re_key, | 373 &next_query_sec, &reset, &deletes, &urls)); |
397 &reset, &deletes, &urls)); | |
398 | 374 |
399 EXPECT_FALSE(re_key); | |
400 EXPECT_FALSE(reset); | 375 EXPECT_FALSE(reset); |
401 EXPECT_EQ(urls.size(), 4U); | 376 EXPECT_EQ(urls.size(), 4U); |
402 EXPECT_EQ(urls[0].url, | 377 EXPECT_EQ(urls[0].url, |
403 "cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_1"); | 378 "cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_1"); |
404 EXPECT_EQ(urls[1].url, | 379 EXPECT_EQ(urls[1].url, |
405 "cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_2"); | 380 "cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_2"); |
406 EXPECT_EQ(urls[2].url, | 381 EXPECT_EQ(urls[2].url, |
407 "cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_3"); | 382 "cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_3"); |
408 EXPECT_EQ(urls[3].url, | 383 EXPECT_EQ(urls[3].url, |
409 "s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8641-8800:8641-8689," | 384 "s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8641-8800:8641-8689," |
410 "8691-8731,8733-8786"); | 385 "8691-8731,8733-8786"); |
411 EXPECT_EQ(next_query_sec, 0); | 386 EXPECT_EQ(next_query_sec, 0); |
412 EXPECT_TRUE(deletes.empty()); | 387 EXPECT_TRUE(deletes.empty()); |
413 } | 388 } |
414 | 389 |
415 TEST(SafeBrowsingProtocolParsingTest, TestRedirectsWithMac) { | |
416 std::string redirects("m:IZhFUj0bMPBMsGw7MMA-VIzrNLg=\n" | |
417 "i:goog-phish-shavar\n" | |
418 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6501-6505:6501-6505," | |
419 "pcY6iVeT9-CBQ3fdAF0rpnKjR1Y=\n" | |
420 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8001-8160:8001-8024," | |
421 "8026-8045,8048-8049,8051-8134,8136-8152,8155-8160," | |
422 "j6XXAEWnjYk9tVVLBSdQvIEq2Wg=\n"); | |
423 | |
424 SafeBrowsingProtocolParser parser; | |
425 int next_query_sec = 0; | |
426 bool re_key = false; | |
427 bool reset = false; | |
428 const std::string key("58Lqn5WIP961x3zuLGo5Uw=="); | |
429 std::vector<SBChunkDelete> deletes; | |
430 std::vector<ChunkUrl> urls; | |
431 | |
432 ASSERT_TRUE(parser.ParseUpdate(redirects.data(), | |
433 static_cast<int>(redirects.length()), key, | |
434 &next_query_sec, &re_key, | |
435 &reset, &deletes, &urls)); | |
436 | |
437 EXPECT_FALSE(re_key); | |
438 EXPECT_FALSE(reset); | |
439 EXPECT_EQ(urls.size(), 2U); | |
440 EXPECT_EQ(urls[0].url, | |
441 "s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6501-6505:6501-6505"); | |
442 EXPECT_EQ(urls[0].mac, "pcY6iVeT9-CBQ3fdAF0rpnKjR1Y="); | |
443 EXPECT_EQ(urls[1].url, | |
444 "s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8001-8160:8001-8024," | |
445 "8026-8045,8048-8049,8051-8134,8136-8152,8155-8160"); | |
446 EXPECT_EQ(urls[1].mac, "j6XXAEWnjYk9tVVLBSdQvIEq2Wg="); | |
447 | |
448 std::string bad_redirects = redirects; | |
449 bad_redirects[0] = 'z'; | |
450 EXPECT_FALSE(parser.ParseUpdate(bad_redirects.data(), | |
451 static_cast<int>(bad_redirects.length()), key, | |
452 &next_query_sec, &re_key, | |
453 &reset, &deletes, &urls)); | |
454 } | |
455 | |
456 // Test parsing various SafeBrowsing protocol headers. | 390 // Test parsing various SafeBrowsing protocol headers. |
457 TEST(SafeBrowsingProtocolParsingTest, TestNextQueryTime) { | 391 TEST(SafeBrowsingProtocolParsingTest, TestNextQueryTime) { |
458 std::string headers("n:1800\ni:goog-white-shavar\n"); | 392 std::string headers("n:1800\ni:goog-white-shavar\n"); |
459 SafeBrowsingProtocolParser parser; | 393 SafeBrowsingProtocolParser parser; |
460 int next_query_sec = 0; | 394 int next_query_sec = 0; |
461 bool re_key = false; | |
462 bool reset = false; | 395 bool reset = false; |
463 std::vector<SBChunkDelete> deletes; | 396 std::vector<SBChunkDelete> deletes; |
464 std::vector<ChunkUrl> urls; | 397 std::vector<ChunkUrl> urls; |
465 EXPECT_TRUE(parser.ParseUpdate(headers.data(), | 398 EXPECT_TRUE(parser.ParseUpdate(headers.data(), |
466 static_cast<int>(headers.length()), "", | 399 static_cast<int>(headers.length()), |
467 &next_query_sec, &re_key, | 400 &next_query_sec, &reset, &deletes, &urls)); |
468 &reset, &deletes, &urls)); | |
469 | 401 |
470 EXPECT_EQ(next_query_sec, 1800); | 402 EXPECT_EQ(next_query_sec, 1800); |
471 EXPECT_FALSE(re_key); | |
472 EXPECT_FALSE(reset); | 403 EXPECT_FALSE(reset); |
473 EXPECT_TRUE(deletes.empty()); | 404 EXPECT_TRUE(deletes.empty()); |
474 EXPECT_TRUE(urls.empty()); | 405 EXPECT_TRUE(urls.empty()); |
475 } | 406 } |
476 | 407 |
477 // Test parsing data from a GetHashRequest | 408 // Test parsing data from a GetHashRequest |
478 TEST(SafeBrowsingProtocolParsingTest, TestGetHash) { | 409 TEST(SafeBrowsingProtocolParsingTest, TestGetHash) { |
479 std::string get_hash("goog-phish-shavar:19:96\n" | 410 std::string get_hash("goog-phish-shavar:19:96\n" |
480 "00112233445566778899aabbccddeeff" | 411 "00112233445566778899aabbccddeeff" |
481 "00001111222233334444555566667777" | 412 "00001111222233334444555566667777" |
482 "ffffeeeeddddccccbbbbaaaa99998888"); | 413 "ffffeeeeddddccccbbbbaaaa99998888"); |
483 std::vector<SBFullHashResult> full_hashes; | 414 std::vector<SBFullHashResult> full_hashes; |
484 bool re_key = false; | |
485 SafeBrowsingProtocolParser parser; | 415 SafeBrowsingProtocolParser parser; |
486 EXPECT_TRUE(parser.ParseGetHash(get_hash.data(), | 416 EXPECT_TRUE(parser.ParseGetHash(get_hash.data(), |
487 static_cast<int>(get_hash.length()), "", | 417 static_cast<int>(get_hash.length()), |
488 &re_key, | |
489 &full_hashes)); | 418 &full_hashes)); |
490 | 419 |
491 EXPECT_FALSE(re_key); | |
492 EXPECT_EQ(full_hashes.size(), 3U); | 420 EXPECT_EQ(full_hashes.size(), 3U); |
493 EXPECT_EQ(memcmp(&full_hashes[0].hash, | 421 EXPECT_EQ(memcmp(&full_hashes[0].hash, |
494 "00112233445566778899aabbccddeeff", | 422 "00112233445566778899aabbccddeeff", |
495 sizeof(SBFullHash)), 0); | 423 sizeof(SBFullHash)), 0); |
496 EXPECT_EQ(full_hashes[0].list_name, "goog-phish-shavar"); | 424 EXPECT_EQ(full_hashes[0].list_name, "goog-phish-shavar"); |
497 EXPECT_EQ(memcmp(&full_hashes[1].hash, | 425 EXPECT_EQ(memcmp(&full_hashes[1].hash, |
498 "00001111222233334444555566667777", | 426 "00001111222233334444555566667777", |
499 sizeof(SBFullHash)), 0); | 427 sizeof(SBFullHash)), 0); |
500 EXPECT_EQ(full_hashes[1].list_name, "goog-phish-shavar"); | 428 EXPECT_EQ(full_hashes[1].list_name, "goog-phish-shavar"); |
501 EXPECT_EQ(memcmp(&full_hashes[2].hash, | 429 EXPECT_EQ(memcmp(&full_hashes[2].hash, |
502 "ffffeeeeddddccccbbbbaaaa99998888", | 430 "ffffeeeeddddccccbbbbaaaa99998888", |
503 sizeof(SBFullHash)), 0); | 431 sizeof(SBFullHash)), 0); |
504 EXPECT_EQ(full_hashes[2].list_name, "goog-phish-shavar"); | 432 EXPECT_EQ(full_hashes[2].list_name, "goog-phish-shavar"); |
505 | 433 |
506 // Test multiple lists in the GetHash results. | 434 // Test multiple lists in the GetHash results. |
507 std::string get_hash2("goog-phish-shavar:19:32\n" | 435 std::string get_hash2("goog-phish-shavar:19:32\n" |
508 "00112233445566778899aabbccddeeff" | 436 "00112233445566778899aabbccddeeff" |
509 "goog-malware-shavar:19:64\n" | 437 "goog-malware-shavar:19:64\n" |
510 "cafebeefcafebeefdeaddeaddeaddead" | 438 "cafebeefcafebeefdeaddeaddeaddead" |
511 "zzzzyyyyxxxxwwwwvvvvuuuuttttssss"); | 439 "zzzzyyyyxxxxwwwwvvvvuuuuttttssss"); |
512 EXPECT_TRUE(parser.ParseGetHash(get_hash2.data(), | 440 EXPECT_TRUE(parser.ParseGetHash(get_hash2.data(), |
513 static_cast<int>(get_hash2.length()), "", | 441 static_cast<int>(get_hash2.length()), |
514 &re_key, | |
515 &full_hashes)); | 442 &full_hashes)); |
516 | 443 |
517 EXPECT_FALSE(re_key); | |
518 EXPECT_EQ(full_hashes.size(), 3U); | 444 EXPECT_EQ(full_hashes.size(), 3U); |
519 EXPECT_EQ(memcmp(&full_hashes[0].hash, | 445 EXPECT_EQ(memcmp(&full_hashes[0].hash, |
520 "00112233445566778899aabbccddeeff", | 446 "00112233445566778899aabbccddeeff", |
521 sizeof(SBFullHash)), 0); | 447 sizeof(SBFullHash)), 0); |
522 EXPECT_EQ(full_hashes[0].list_name, "goog-phish-shavar"); | 448 EXPECT_EQ(full_hashes[0].list_name, "goog-phish-shavar"); |
523 EXPECT_EQ(memcmp(&full_hashes[1].hash, | 449 EXPECT_EQ(memcmp(&full_hashes[1].hash, |
524 "cafebeefcafebeefdeaddeaddeaddead", | 450 "cafebeefcafebeefdeaddeaddeaddead", |
525 sizeof(SBFullHash)), 0); | 451 sizeof(SBFullHash)), 0); |
526 EXPECT_EQ(full_hashes[1].list_name, "goog-malware-shavar"); | 452 EXPECT_EQ(full_hashes[1].list_name, "goog-malware-shavar"); |
527 EXPECT_EQ(memcmp(&full_hashes[2].hash, | 453 EXPECT_EQ(memcmp(&full_hashes[2].hash, |
528 "zzzzyyyyxxxxwwwwvvvvuuuuttttssss", | 454 "zzzzyyyyxxxxwwwwvvvvuuuuttttssss", |
529 sizeof(SBFullHash)), 0); | 455 sizeof(SBFullHash)), 0); |
530 EXPECT_EQ(full_hashes[2].list_name, "goog-malware-shavar"); | 456 EXPECT_EQ(full_hashes[2].list_name, "goog-malware-shavar"); |
531 } | 457 } |
532 | 458 |
533 TEST(SafeBrowsingProtocolParsingTest, TestGetHashWithMac) { | |
534 const unsigned char get_hash[] = { | |
535 0x32, 0x56, 0x74, 0x6f, 0x6b, 0x36, 0x64, 0x41, | |
536 0x51, 0x72, 0x65, 0x51, 0x62, 0x38, 0x51, 0x68, | |
537 0x59, 0x45, 0x57, 0x51, 0x57, 0x4d, 0x52, 0x65, | |
538 0x42, 0x63, 0x41, 0x3d, 0x0a, 0x67, 0x6f, 0x6f, | |
539 0x67, 0x2d, 0x70, 0x68, 0x69, 0x73, 0x68, 0x2d, | |
540 0x73, 0x68, 0x61, 0x76, 0x61, 0x72, 0x3a, 0x36, | |
541 0x31, 0x36, 0x39, 0x3a, 0x33, 0x32, 0x0a, 0x17, | |
542 0x7f, 0x03, 0x42, 0x28, 0x1c, 0x31, 0xb9, 0x0b, | |
543 0x1c, 0x7b, 0x9d, 0xaf, 0x7b, 0x43, 0x99, 0x10, | |
544 0xc1, 0xab, 0xe3, 0x1b, 0x35, 0x80, 0x38, 0x96, | |
545 0xf9, 0x44, 0x4f, 0x28, 0xb4, 0xeb, 0x45 | |
546 }; | |
547 | |
548 const unsigned char hash_result[] = { | |
549 0x17, 0x7f, 0x03, 0x42, 0x28, 0x1c, 0x31, 0xb9, | |
550 0x0b, 0x1c, 0x7b, 0x9d, 0xaf, 0x7b, 0x43, 0x99, | |
551 0x10, 0xc1, 0xab, 0xe3, 0x1b, 0x35, 0x80, 0x38, | |
552 0x96, 0xf9, 0x44, 0x4f, 0x28, 0xb4, 0xeb, 0x45 | |
553 }; | |
554 | |
555 const std::string key = "58Lqn5WIP961x3zuLGo5Uw=="; | |
556 std::vector<SBFullHashResult> full_hashes; | |
557 bool re_key = false; | |
558 SafeBrowsingProtocolParser parser; | |
559 EXPECT_TRUE(parser.ParseGetHash(reinterpret_cast<const char*>(get_hash), | |
560 sizeof(get_hash), | |
561 key, | |
562 &re_key, | |
563 &full_hashes)); | |
564 EXPECT_FALSE(re_key); | |
565 EXPECT_EQ(full_hashes.size(), 1U); | |
566 EXPECT_EQ(memcmp(hash_result, &full_hashes[0].hash, sizeof(SBFullHash)), 0); | |
567 } | |
568 | |
569 TEST(SafeBrowsingProtocolParsingTest, TestGetHashWithUnknownList) { | 459 TEST(SafeBrowsingProtocolParsingTest, TestGetHashWithUnknownList) { |
570 std::string hash_response = "goog-phish-shavar:1:32\n" | 460 std::string hash_response = "goog-phish-shavar:1:32\n" |
571 "12345678901234567890123456789012" | 461 "12345678901234567890123456789012" |
572 "googpub-phish-shavar:19:32\n" | 462 "googpub-phish-shavar:19:32\n" |
573 "09876543210987654321098765432109"; | 463 "09876543210987654321098765432109"; |
574 bool re_key = false; | |
575 std::string key = ""; | |
576 std::vector<SBFullHashResult> full_hashes; | 464 std::vector<SBFullHashResult> full_hashes; |
577 SafeBrowsingProtocolParser parser; | 465 SafeBrowsingProtocolParser parser; |
578 EXPECT_TRUE(parser.ParseGetHash(hash_response.data(), | 466 EXPECT_TRUE(parser.ParseGetHash(hash_response.data(), |
579 hash_response.size(), | 467 hash_response.size(), |
580 key, | |
581 &re_key, | |
582 &full_hashes)); | 468 &full_hashes)); |
583 | 469 |
584 EXPECT_EQ(full_hashes.size(), 1U); | 470 EXPECT_EQ(full_hashes.size(), 1U); |
585 EXPECT_EQ(memcmp("12345678901234567890123456789012", | 471 EXPECT_EQ(memcmp("12345678901234567890123456789012", |
586 &full_hashes[0].hash, sizeof(SBFullHash)), 0); | 472 &full_hashes[0].hash, sizeof(SBFullHash)), 0); |
587 EXPECT_EQ(full_hashes[0].list_name, "goog-phish-shavar"); | 473 EXPECT_EQ(full_hashes[0].list_name, "goog-phish-shavar"); |
588 EXPECT_EQ(full_hashes[0].add_chunk_id, 1); | 474 EXPECT_EQ(full_hashes[0].add_chunk_id, 1); |
589 | 475 |
590 hash_response += "goog-malware-shavar:7:32\n" | 476 hash_response += "goog-malware-shavar:7:32\n" |
591 "abcdefghijklmnopqrstuvwxyz123457"; | 477 "abcdefghijklmnopqrstuvwxyz123457"; |
592 full_hashes.clear(); | 478 full_hashes.clear(); |
593 EXPECT_TRUE(parser.ParseGetHash(hash_response.data(), | 479 EXPECT_TRUE(parser.ParseGetHash(hash_response.data(), |
594 hash_response.size(), | 480 hash_response.size(), |
595 key, | |
596 &re_key, | |
597 &full_hashes)); | 481 &full_hashes)); |
598 | 482 |
599 EXPECT_EQ(full_hashes.size(), 2U); | 483 EXPECT_EQ(full_hashes.size(), 2U); |
600 EXPECT_EQ(memcmp("12345678901234567890123456789012", | 484 EXPECT_EQ(memcmp("12345678901234567890123456789012", |
601 &full_hashes[0].hash, sizeof(SBFullHash)), 0); | 485 &full_hashes[0].hash, sizeof(SBFullHash)), 0); |
602 EXPECT_EQ(full_hashes[0].list_name, "goog-phish-shavar"); | 486 EXPECT_EQ(full_hashes[0].list_name, "goog-phish-shavar"); |
603 EXPECT_EQ(full_hashes[0].add_chunk_id, 1); | 487 EXPECT_EQ(full_hashes[0].add_chunk_id, 1); |
604 EXPECT_EQ(memcmp("abcdefghijklmnopqrstuvwxyz123457", | 488 EXPECT_EQ(memcmp("abcdefghijklmnopqrstuvwxyz123457", |
605 &full_hashes[1].hash, sizeof(SBFullHash)), 0); | 489 &full_hashes[1].hash, sizeof(SBFullHash)), 0); |
606 EXPECT_EQ(full_hashes[1].list_name, "goog-malware-shavar"); | 490 EXPECT_EQ(full_hashes[1].list_name, "goog-malware-shavar"); |
607 EXPECT_EQ(full_hashes[1].add_chunk_id, 7); | 491 EXPECT_EQ(full_hashes[1].add_chunk_id, 7); |
608 } | 492 } |
609 | 493 |
610 TEST(SafeBrowsingProtocolParsingTest, TestFormatHash) { | 494 TEST(SafeBrowsingProtocolParsingTest, TestFormatHash) { |
611 SafeBrowsingProtocolParser parser; | 495 SafeBrowsingProtocolParser parser; |
612 std::vector<SBPrefix> prefixes; | 496 std::vector<SBPrefix> prefixes; |
613 std::string get_hash; | 497 std::string get_hash; |
614 | 498 |
615 prefixes.push_back(0x34333231); | 499 prefixes.push_back(0x34333231); |
616 prefixes.push_back(0x64636261); | 500 prefixes.push_back(0x64636261); |
617 prefixes.push_back(0x73727170); | 501 prefixes.push_back(0x73727170); |
618 | 502 |
619 parser.FormatGetHash(prefixes, &get_hash); | 503 parser.FormatGetHash(prefixes, &get_hash); |
620 EXPECT_EQ(get_hash, "4:12\n1234abcdpqrs"); | 504 EXPECT_EQ(get_hash, "4:12\n1234abcdpqrs"); |
621 } | 505 } |
622 | 506 |
623 TEST(SafeBrowsingProtocolParsingTest, TestGetKey) { | |
624 SafeBrowsingProtocolParser parser; | |
625 std::string key_response("clientkey:10:0123456789\n" | |
626 "wrappedkey:20:abcdefghijklmnopqrst\n"); | |
627 | |
628 std::string client_key, wrapped_key; | |
629 EXPECT_TRUE(parser.ParseNewKey(key_response.data(), | |
630 static_cast<int>(key_response.length()), | |
631 &client_key, | |
632 &wrapped_key)); | |
633 | |
634 EXPECT_EQ(client_key, "0123456789"); | |
635 EXPECT_EQ(wrapped_key, "abcdefghijklmnopqrst"); | |
636 } | |
637 | |
638 TEST(SafeBrowsingProtocolParsingTest, TestReKey) { | |
639 SafeBrowsingProtocolParser parser; | |
640 std::string update("n:1800\ni:phishy\ne:pleaserekey\n"); | |
641 | |
642 bool re_key = false; | |
643 bool reset = false; | |
644 int next_update = -1; | |
645 std::vector<SBChunkDelete> deletes; | |
646 std::vector<ChunkUrl> urls; | |
647 EXPECT_TRUE(parser.ParseUpdate(update.data(), | |
648 static_cast<int>(update.size()), "", | |
649 &next_update, &re_key, | |
650 &reset, &deletes, &urls)); | |
651 EXPECT_TRUE(re_key); | |
652 } | |
653 | |
654 TEST(SafeBrowsingProtocolParsingTest, TestReset) { | 507 TEST(SafeBrowsingProtocolParsingTest, TestReset) { |
655 SafeBrowsingProtocolParser parser; | 508 SafeBrowsingProtocolParser parser; |
656 std::string update("n:1800\ni:phishy\nr:pleasereset\n"); | 509 std::string update("n:1800\ni:phishy\nr:pleasereset\n"); |
657 | 510 |
658 bool re_key = false; | |
659 bool reset = false; | 511 bool reset = false; |
660 int next_update = -1; | 512 int next_update = -1; |
661 std::vector<SBChunkDelete> deletes; | 513 std::vector<SBChunkDelete> deletes; |
662 std::vector<ChunkUrl> urls; | 514 std::vector<ChunkUrl> urls; |
663 EXPECT_TRUE(parser.ParseUpdate(update.data(), | 515 EXPECT_TRUE(parser.ParseUpdate(update.data(), |
664 static_cast<int>(update.size()), "", | 516 static_cast<int>(update.size()), |
665 &next_update, &re_key, | 517 &next_update, &reset, &deletes, &urls)); |
666 &reset, &deletes, &urls)); | |
667 EXPECT_TRUE(reset); | 518 EXPECT_TRUE(reset); |
668 } | 519 } |
669 | 520 |
670 // The SafeBrowsing service will occasionally send zero length chunks so that | 521 // The SafeBrowsing service will occasionally send zero length chunks so that |
671 // client requests will have longer contiguous chunk number ranges, and thus | 522 // client requests will have longer contiguous chunk number ranges, and thus |
672 // reduce the request size. | 523 // reduce the request size. |
673 TEST(SafeBrowsingProtocolParsingTest, TestZeroSizeAddChunk) { | 524 TEST(SafeBrowsingProtocolParsingTest, TestZeroSizeAddChunk) { |
674 std::string add_chunk("a:1:4:0\n"); | 525 std::string add_chunk("a:1:4:0\n"); |
675 SafeBrowsingProtocolParser parser; | 526 SafeBrowsingProtocolParser parser; |
676 bool re_key = false; | |
677 SBChunkList chunks; | 527 SBChunkList chunks; |
678 | 528 |
679 bool result = parser.ParseChunk( | 529 bool result = parser.ParseChunk( |
680 safe_browsing_util::kMalwareList, | 530 safe_browsing_util::kMalwareList, |
681 add_chunk.data(), | 531 add_chunk.data(), |
682 static_cast<int>(add_chunk.length()), | 532 static_cast<int>(add_chunk.length()), |
683 "", "", &re_key, &chunks); | 533 &chunks); |
684 EXPECT_TRUE(result); | 534 EXPECT_TRUE(result); |
685 EXPECT_EQ(chunks.size(), 1U); | 535 EXPECT_EQ(chunks.size(), 1U); |
686 EXPECT_EQ(chunks[0].chunk_number, 1); | 536 EXPECT_EQ(chunks[0].chunk_number, 1); |
687 EXPECT_EQ(chunks[0].hosts.size(), 0U); | 537 EXPECT_EQ(chunks[0].hosts.size(), 0U); |
688 | 538 |
689 // Now test a zero size chunk in between normal chunks. | 539 // Now test a zero size chunk in between normal chunks. |
690 chunks.clear(); | 540 chunks.clear(); |
691 std::string add_chunks("a:1:4:18\n1234\001abcd5678\001wxyz" | 541 std::string add_chunks("a:1:4:18\n1234\001abcd5678\001wxyz" |
692 "a:2:4:0\n" | 542 "a:2:4:0\n" |
693 "a:3:4:9\ncafe\001beef"); | 543 "a:3:4:9\ncafe\001beef"); |
694 result = parser.ParseChunk( | 544 result = parser.ParseChunk( |
695 safe_browsing_util::kMalwareList, | 545 safe_browsing_util::kMalwareList, |
696 add_chunks.data(), | 546 add_chunks.data(), |
697 static_cast<int>(add_chunks.length()), | 547 static_cast<int>(add_chunks.length()), |
698 "", "", &re_key, &chunks); | 548 &chunks); |
699 EXPECT_TRUE(result); | 549 EXPECT_TRUE(result); |
700 EXPECT_EQ(chunks.size(), 3U); | 550 EXPECT_EQ(chunks.size(), 3U); |
701 | 551 |
702 // See that each chunk has the right content. | 552 // See that each chunk has the right content. |
703 EXPECT_EQ(chunks[0].chunk_number, 1); | 553 EXPECT_EQ(chunks[0].chunk_number, 1); |
704 EXPECT_EQ(chunks[0].hosts.size(), 2U); | 554 EXPECT_EQ(chunks[0].hosts.size(), 2U); |
705 EXPECT_EQ(chunks[0].hosts[0].host, 0x34333231); | 555 EXPECT_EQ(chunks[0].hosts[0].host, 0x34333231); |
706 EXPECT_EQ(chunks[0].hosts[0].entry->PrefixAt(0), 0x64636261); | 556 EXPECT_EQ(chunks[0].hosts[0].entry->PrefixAt(0), 0x64636261); |
707 EXPECT_EQ(chunks[0].hosts[1].host, 0x38373635); | 557 EXPECT_EQ(chunks[0].hosts[1].host, 0x38373635); |
708 EXPECT_EQ(chunks[0].hosts[1].entry->PrefixAt(0), 0x7a797877); | 558 EXPECT_EQ(chunks[0].hosts[1].entry->PrefixAt(0), 0x7a797877); |
709 | 559 |
710 EXPECT_EQ(chunks[1].chunk_number, 2); | 560 EXPECT_EQ(chunks[1].chunk_number, 2); |
711 EXPECT_EQ(chunks[1].hosts.size(), 0U); | 561 EXPECT_EQ(chunks[1].hosts.size(), 0U); |
712 | 562 |
713 EXPECT_EQ(chunks[2].chunk_number, 3); | 563 EXPECT_EQ(chunks[2].chunk_number, 3); |
714 EXPECT_EQ(chunks[2].hosts.size(), 1U); | 564 EXPECT_EQ(chunks[2].hosts.size(), 1U); |
715 EXPECT_EQ(chunks[2].hosts[0].host, 0x65666163); | 565 EXPECT_EQ(chunks[2].hosts[0].host, 0x65666163); |
716 EXPECT_EQ(chunks[2].hosts[0].entry->PrefixAt(0), 0x66656562); | 566 EXPECT_EQ(chunks[2].hosts[0].entry->PrefixAt(0), 0x66656562); |
717 } | 567 } |
718 | 568 |
719 // Test parsing a zero sized sub chunk. | 569 // Test parsing a zero sized sub chunk. |
720 TEST(SafeBrowsingProtocolParsingTest, TestZeroSizeSubChunk) { | 570 TEST(SafeBrowsingProtocolParsingTest, TestZeroSizeSubChunk) { |
721 std::string sub_chunk("s:9:4:0\n"); | 571 std::string sub_chunk("s:9:4:0\n"); |
722 SafeBrowsingProtocolParser parser; | 572 SafeBrowsingProtocolParser parser; |
723 bool re_key = false; | |
724 SBChunkList chunks; | 573 SBChunkList chunks; |
725 | 574 |
726 bool result = parser.ParseChunk( | 575 bool result = parser.ParseChunk( |
727 safe_browsing_util::kMalwareList, | 576 safe_browsing_util::kMalwareList, |
728 sub_chunk.data(), | 577 sub_chunk.data(), |
729 static_cast<int>(sub_chunk.length()), | 578 static_cast<int>(sub_chunk.length()), |
730 "", "", &re_key, &chunks); | 579 &chunks); |
731 EXPECT_TRUE(result); | 580 EXPECT_TRUE(result); |
732 EXPECT_EQ(chunks.size(), 1U); | 581 EXPECT_EQ(chunks.size(), 1U); |
733 EXPECT_EQ(chunks[0].chunk_number, 9); | 582 EXPECT_EQ(chunks[0].chunk_number, 9); |
734 EXPECT_EQ(chunks[0].hosts.size(), 0U); | 583 EXPECT_EQ(chunks[0].hosts.size(), 0U); |
735 chunks.clear(); | 584 chunks.clear(); |
736 | 585 |
737 // Test parsing a zero sized sub chunk mixed in with content carrying chunks. | 586 // Test parsing a zero sized sub chunk mixed in with content carrying chunks. |
738 std::string sub_chunks("s:1:4:9\nabcdxwxyz" | 587 std::string sub_chunks("s:1:4:9\nabcdxwxyz" |
739 "s:2:4:0\n" | 588 "s:2:4:0\n" |
740 "s:3:4:26\nefgh\0011234pqrscafe\0015678lmno"); | 589 "s:3:4:26\nefgh\0011234pqrscafe\0015678lmno"); |
741 sub_chunks[12] = '\0'; | 590 sub_chunks[12] = '\0'; |
742 | 591 |
743 result = parser.ParseChunk( | 592 result = parser.ParseChunk( |
744 safe_browsing_util::kMalwareList, | 593 safe_browsing_util::kMalwareList, |
745 sub_chunks.data(), | 594 sub_chunks.data(), |
746 static_cast<int>(sub_chunks.length()), | 595 static_cast<int>(sub_chunks.length()), |
747 "", "", &re_key, &chunks); | 596 &chunks); |
748 EXPECT_TRUE(result); | 597 EXPECT_TRUE(result); |
749 | 598 |
750 EXPECT_EQ(chunks[0].chunk_number, 1); | 599 EXPECT_EQ(chunks[0].chunk_number, 1); |
751 EXPECT_EQ(chunks[0].hosts.size(), 1U); | 600 EXPECT_EQ(chunks[0].hosts.size(), 1U); |
752 EXPECT_EQ(chunks[0].hosts[0].host, 0x64636261); | 601 EXPECT_EQ(chunks[0].hosts[0].host, 0x64636261); |
753 EXPECT_EQ(chunks[0].hosts[0].entry->prefix_count(), 0); | 602 EXPECT_EQ(chunks[0].hosts[0].entry->prefix_count(), 0); |
754 | 603 |
755 EXPECT_EQ(chunks[1].chunk_number, 2); | 604 EXPECT_EQ(chunks[1].chunk_number, 2); |
756 EXPECT_EQ(chunks[1].hosts.size(), 0U); | 605 EXPECT_EQ(chunks[1].hosts.size(), 0U); |
757 | 606 |
758 EXPECT_EQ(chunks[2].chunk_number, 3); | 607 EXPECT_EQ(chunks[2].chunk_number, 3); |
759 EXPECT_EQ(chunks[2].hosts.size(), 2U); | 608 EXPECT_EQ(chunks[2].hosts.size(), 2U); |
760 EXPECT_EQ(chunks[2].hosts[0].host, 0x68676665); | 609 EXPECT_EQ(chunks[2].hosts[0].host, 0x68676665); |
761 EXPECT_EQ(chunks[2].hosts[0].entry->prefix_count(), 1); | 610 EXPECT_EQ(chunks[2].hosts[0].entry->prefix_count(), 1); |
762 EXPECT_EQ(chunks[2].hosts[0].entry->PrefixAt(0), 0x73727170); | 611 EXPECT_EQ(chunks[2].hosts[0].entry->PrefixAt(0), 0x73727170); |
763 EXPECT_EQ(chunks[2].hosts[0].entry->ChunkIdAtPrefix(0), 0x31323334); | 612 EXPECT_EQ(chunks[2].hosts[0].entry->ChunkIdAtPrefix(0), 0x31323334); |
764 EXPECT_EQ(chunks[2].hosts[1].host, 0x65666163); | 613 EXPECT_EQ(chunks[2].hosts[1].host, 0x65666163); |
765 EXPECT_EQ(chunks[2].hosts[1].entry->prefix_count(), 1); | 614 EXPECT_EQ(chunks[2].hosts[1].entry->prefix_count(), 1); |
766 EXPECT_EQ(chunks[2].hosts[1].entry->PrefixAt(0), 0x6f6e6d6c); | 615 EXPECT_EQ(chunks[2].hosts[1].entry->PrefixAt(0), 0x6f6e6d6c); |
767 EXPECT_EQ(chunks[2].hosts[1].entry->ChunkIdAtPrefix(0), 0x35363738); | 616 EXPECT_EQ(chunks[2].hosts[1].entry->ChunkIdAtPrefix(0), 0x35363738); |
768 } | 617 } |
769 | 618 |
770 TEST(SafeBrowsingProtocolParsingTest, TestVerifyUpdateMac) { | |
771 SafeBrowsingProtocolParser parser; | |
772 | |
773 const std::string update_body = | |
774 "n:1895\n" | |
775 "i:goog-phish-shavar\n" | |
776 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6501-6505:6501-6505," | |
777 "pcY6iVeT9-CBQ3fdAF0rpnKjR1Y=\n" | |
778 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6506-6510:6506-6510," | |
779 "SDBrYC3rX3KEPe72LOypnP6QYac=\n" | |
780 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6511-6520:6511-6520," | |
781 "9UQo-e7OkcsXT2wFWTAhOuWOsUs=\n" | |
782 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6521-6560:6521-6560," | |
783 "qVNw6JIpR1q6PIXST7J4LJ9n3Zg=\n" | |
784 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6561-6720:6561-6720," | |
785 "7OiJvCbiwvpzPITW-hQohY5NHuc=\n" | |
786 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6721-6880:6721-6880," | |
787 "oBS3svhoi9deIa0sWZ_gnD0ujj8=\n" | |
788 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6881-7040:6881-7040," | |
789 "a0r8Xit4VvH39xgyQHZTPczKBIE=\n" | |
790 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_7041-7200:7041-7163," | |
791 "q538LChutGknBw55s6kcE2wTcvU=\n" | |
792 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8001-8160:8001-8024," | |
793 "8026-8045,8048-8049,8051-8134,8136-8152,8155-8160," | |
794 "j6XXAEWnjYk9tVVLBSdQvIEq2Wg=\n" | |
795 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8161-8320:8161-8215," | |
796 "8217-8222,8224-8320,YaNfiqdQOt-uLCLWVLj46AZpAjQ=\n" | |
797 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8321-8480:8321-8391," | |
798 "8393-8399,8402,8404-8419,8421-8425,8427,8431-8433,8435-8439,8441-8443," | |
799 "8445-8446,8448-8480,ALj31GQMwGiIeU3bM2ZYKITfU-U=\n" | |
800 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8481-8640:8481-8500," | |
801 "8502-8508,8510-8511,8513-8517,8519-8525,8527-8531,8533,8536-8539," | |
802 "8541-8576,8578-8638,8640,TlQYRmS_kZ5PBAUIUyNQDq0Jprs=\n" | |
803 "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8641-8800:8641-8689," | |
804 "8691-8731,8733-8786,x1Qf7hdNrO8b6yym03ZzNydDS1o=\n"; | |
805 | |
806 const std::string update = "m:XIU0LiQhAPJq6dynXwHbygjS5tw=\n" + update_body; | |
807 | |
808 bool re_key = false; | |
809 bool reset = false; | |
810 int next_update = -1; | |
811 std::vector<SBChunkDelete> deletes; | |
812 std::vector<ChunkUrl> urls; | |
813 const std::string key("58Lqn5WIP961x3zuLGo5Uw=="); | |
814 | |
815 // Should pass with correct mac. | |
816 ASSERT_TRUE(parser.ParseUpdate(update.data(), | |
817 static_cast<int>(update.size()), key, | |
818 &next_update, &re_key, | |
819 &reset, &deletes, &urls)); | |
820 EXPECT_FALSE(re_key); | |
821 EXPECT_EQ(next_update, 1895); | |
822 | |
823 // Should fail if mac is not present. | |
824 EXPECT_FALSE(parser.ParseUpdate(update_body.data(), | |
825 static_cast<int>(update_body.size()), key, | |
826 &next_update, &re_key, | |
827 &reset, &deletes, &urls)); | |
828 | |
829 std::string bad_update = update; | |
830 bad_update[1] = 'z'; | |
831 // Should fail if mac command isn't formatted correctly. | |
832 EXPECT_FALSE(parser.ParseUpdate(bad_update.data(), | |
833 static_cast<int>(bad_update.size()), key, | |
834 &next_update, &re_key, | |
835 &reset, &deletes, &urls)); | |
836 | |
837 bad_update = update; | |
838 bad_update[5] ^= 1; | |
839 // Should fail if mac is incorrect. | |
840 EXPECT_FALSE(parser.ParseUpdate(bad_update.data(), | |
841 static_cast<int>(bad_update.size()), key, | |
842 &next_update, &re_key, | |
843 &reset, &deletes, &urls)); | |
844 | |
845 } | |
846 | |
847 TEST(SafeBrowsingProtocolParsingTest, TestVerifyChunkMac) { | |
848 SafeBrowsingProtocolParser parser; | |
849 | |
850 const unsigned char chunk[] = { | |
851 0x73, 0x3a, 0x32, 0x30, 0x30, 0x32, 0x3a, 0x34, | |
852 0x3a, 0x32, 0x32, 0x0a, 0x2f, 0x4f, 0x89, 0x7a, | |
853 0x01, 0x00, 0x00, 0x0a, 0x59, 0xc8, 0x71, 0xdf, | |
854 0x9d, 0x29, 0x0c, 0xba, 0xd7, 0x00, 0x00, 0x00, | |
855 0x0a, 0x59 | |
856 }; | |
857 | |
858 bool re_key = false; | |
859 SBChunkList chunks; | |
860 const std::string key("v_aDSz6jI92WeHCOoZ07QA=="); | |
861 const std::string mac("W9Xp2fUcQ9V66If6Cvsrstpa4Kk="); | |
862 | |
863 EXPECT_TRUE(parser.ParseChunk( | |
864 safe_browsing_util::kMalwareList, | |
865 reinterpret_cast<const char*>(chunk), | |
866 sizeof(chunk), key, mac, | |
867 &re_key, &chunks)); | |
868 EXPECT_FALSE(re_key); | |
869 } | |
870 | |
871 TEST(SafeBrowsingProtocolParsingTest, TestAddBinHashChunks) { | 619 TEST(SafeBrowsingProtocolParsingTest, TestAddBinHashChunks) { |
872 std::string add_chunk("a:1:4:16\naaaabbbbccccdddd" | 620 std::string add_chunk("a:1:4:16\naaaabbbbccccdddd" |
873 "a:2:4:8\n11112222"); | 621 "a:2:4:8\n11112222"); |
874 // Run the parse. | 622 // Run the parse. |
875 SafeBrowsingProtocolParser parser; | 623 SafeBrowsingProtocolParser parser; |
876 bool re_key = false; | |
877 SBChunkList chunks; | 624 SBChunkList chunks; |
878 bool result = parser.ParseChunk( | 625 bool result = parser.ParseChunk( |
879 safe_browsing_util::kBinHashList, | 626 safe_browsing_util::kBinHashList, |
880 add_chunk.data(), | 627 add_chunk.data(), |
881 static_cast<int>(add_chunk.length()), | 628 static_cast<int>(add_chunk.length()), |
882 "", "", &re_key, &chunks); | 629 &chunks); |
883 EXPECT_TRUE(result); | 630 EXPECT_TRUE(result); |
884 EXPECT_FALSE(re_key); | |
885 EXPECT_EQ(chunks.size(), 2U); | 631 EXPECT_EQ(chunks.size(), 2U); |
886 EXPECT_EQ(chunks[0].chunk_number, 1); | 632 EXPECT_EQ(chunks[0].chunk_number, 1); |
887 EXPECT_EQ(chunks[0].hosts.size(), 1U); | 633 EXPECT_EQ(chunks[0].hosts.size(), 1U); |
888 | 634 |
889 EXPECT_EQ(chunks[0].hosts[0].host, 0); | 635 EXPECT_EQ(chunks[0].hosts[0].host, 0); |
890 SBEntry* entry = chunks[0].hosts[0].entry; | 636 SBEntry* entry = chunks[0].hosts[0].entry; |
891 EXPECT_TRUE(entry->IsAdd()); | 637 EXPECT_TRUE(entry->IsAdd()); |
892 EXPECT_TRUE(entry->IsPrefix()); | 638 EXPECT_TRUE(entry->IsPrefix()); |
893 EXPECT_EQ(entry->prefix_count(), 4); | 639 EXPECT_EQ(entry->prefix_count(), 4); |
894 | 640 |
895 EXPECT_EQ(chunks[1].chunk_number, 2); | 641 EXPECT_EQ(chunks[1].chunk_number, 2); |
896 EXPECT_EQ(chunks[1].hosts.size(), 1U); | 642 EXPECT_EQ(chunks[1].hosts.size(), 1U); |
897 | 643 |
898 EXPECT_EQ(chunks[1].hosts[0].host, 0); | 644 EXPECT_EQ(chunks[1].hosts[0].host, 0); |
899 entry = chunks[1].hosts[0].entry; | 645 entry = chunks[1].hosts[0].entry; |
900 EXPECT_TRUE(entry->IsAdd()); | 646 EXPECT_TRUE(entry->IsAdd()); |
901 EXPECT_TRUE(entry->IsPrefix()); | 647 EXPECT_TRUE(entry->IsPrefix()); |
902 EXPECT_EQ(entry->prefix_count(), 2); | 648 EXPECT_EQ(entry->prefix_count(), 2); |
903 EXPECT_EQ(entry->PrefixAt(0), 0x31313131); | 649 EXPECT_EQ(entry->PrefixAt(0), 0x31313131); |
904 EXPECT_EQ(entry->PrefixAt(1), 0x32323232); | 650 EXPECT_EQ(entry->PrefixAt(1), 0x32323232); |
905 } | 651 } |
906 | 652 |
907 // Test parsing one add chunk where a hostkey spans several entries. | 653 // Test parsing one add chunk where a hostkey spans several entries. |
908 TEST(SafeBrowsingProtocolParsingTest, TestAddBigBinHashChunk) { | 654 TEST(SafeBrowsingProtocolParsingTest, TestAddBigBinHashChunk) { |
909 std::string add_chunk("a:1:4:1028\n"); | 655 std::string add_chunk("a:1:4:1028\n"); |
910 for (int i = 0; i < 257; ++i) | 656 for (int i = 0; i < 257; ++i) |
911 add_chunk.append(base::StringPrintf("%04d", i)); | 657 add_chunk.append(base::StringPrintf("%04d", i)); |
912 | 658 |
913 SafeBrowsingProtocolParser parser; | 659 SafeBrowsingProtocolParser parser; |
914 bool re_key = false; | |
915 SBChunkList chunks; | 660 SBChunkList chunks; |
916 bool result = parser.ParseChunk( | 661 bool result = parser.ParseChunk( |
917 safe_browsing_util::kBinHashList, | 662 safe_browsing_util::kBinHashList, |
918 add_chunk.data(), | 663 add_chunk.data(), |
919 static_cast<int>(add_chunk.length()), | 664 static_cast<int>(add_chunk.length()), |
920 "", "", &re_key, &chunks); | 665 &chunks); |
921 EXPECT_TRUE(result); | 666 EXPECT_TRUE(result); |
922 EXPECT_FALSE(re_key); | |
923 EXPECT_EQ(chunks.size(), 1U); | 667 EXPECT_EQ(chunks.size(), 1U); |
924 EXPECT_EQ(chunks[0].chunk_number, 1); | 668 EXPECT_EQ(chunks[0].chunk_number, 1); |
925 | 669 |
926 EXPECT_EQ(chunks[0].hosts.size(), 1U); | 670 EXPECT_EQ(chunks[0].hosts.size(), 1U); |
927 | 671 |
928 const SBChunkHost& host0 = chunks[0].hosts[0]; | 672 const SBChunkHost& host0 = chunks[0].hosts[0]; |
929 EXPECT_EQ(host0.host, 0); | 673 EXPECT_EQ(host0.host, 0); |
930 EXPECT_EQ(host0.entry->prefix_count(), 257); | 674 EXPECT_EQ(host0.entry->prefix_count(), 257); |
931 } | 675 } |
932 | 676 |
933 // Test parsing one sub chunk. | 677 // Test parsing one sub chunk. |
934 TEST(SafeBrowsingProtocolParsingTest, TestSubBinHashChunk) { | 678 TEST(SafeBrowsingProtocolParsingTest, TestSubBinHashChunk) { |
935 std::string sub_chunk("s:9:4:16\n1111mmmm2222nnnn"); | 679 std::string sub_chunk("s:9:4:16\n1111mmmm2222nnnn"); |
936 | 680 |
937 // Run the parser. | 681 // Run the parser. |
938 SafeBrowsingProtocolParser parser; | 682 SafeBrowsingProtocolParser parser; |
939 bool re_key = false; | |
940 SBChunkList chunks; | 683 SBChunkList chunks; |
941 bool result = parser.ParseChunk( | 684 bool result = parser.ParseChunk( |
942 safe_browsing_util::kBinHashList, | 685 safe_browsing_util::kBinHashList, |
943 sub_chunk.data(), | 686 sub_chunk.data(), |
944 static_cast<int>(sub_chunk.length()), | 687 static_cast<int>(sub_chunk.length()), |
945 "", "", &re_key, &chunks); | 688 &chunks); |
946 EXPECT_TRUE(result); | 689 EXPECT_TRUE(result); |
947 EXPECT_FALSE(re_key); | |
948 EXPECT_EQ(chunks.size(), 1U); | 690 EXPECT_EQ(chunks.size(), 1U); |
949 EXPECT_EQ(chunks[0].chunk_number, 9); | 691 EXPECT_EQ(chunks[0].chunk_number, 9); |
950 EXPECT_EQ(chunks[0].hosts.size(), 1U); | 692 EXPECT_EQ(chunks[0].hosts.size(), 1U); |
951 | 693 |
952 EXPECT_EQ(chunks[0].hosts[0].host, 0); | 694 EXPECT_EQ(chunks[0].hosts[0].host, 0); |
953 SBEntry* entry = chunks[0].hosts[0].entry; | 695 SBEntry* entry = chunks[0].hosts[0].entry; |
954 EXPECT_TRUE(entry->IsSub()); | 696 EXPECT_TRUE(entry->IsSub()); |
955 EXPECT_TRUE(entry->IsPrefix()); | 697 EXPECT_TRUE(entry->IsPrefix()); |
956 EXPECT_EQ(entry->prefix_count(), 2); | 698 EXPECT_EQ(entry->prefix_count(), 2); |
957 EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x31313131); | 699 EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x31313131); |
958 EXPECT_EQ(entry->PrefixAt(0), 0x6d6d6d6d); | 700 EXPECT_EQ(entry->PrefixAt(0), 0x6d6d6d6d); |
959 EXPECT_EQ(entry->ChunkIdAtPrefix(1), 0x32323232); | 701 EXPECT_EQ(entry->ChunkIdAtPrefix(1), 0x32323232); |
960 EXPECT_EQ(entry->PrefixAt(1), 0x6e6e6e6e); | 702 EXPECT_EQ(entry->PrefixAt(1), 0x6e6e6e6e); |
961 } | 703 } |
962 | 704 |
963 TEST(SafeBrowsingProtocolParsingTest, TestAddDownloadWhitelistChunk) { | 705 TEST(SafeBrowsingProtocolParsingTest, TestAddDownloadWhitelistChunk) { |
964 std::string add_chunk("a:1:32:32\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | 706 std::string add_chunk("a:1:32:32\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
965 "a:2:32:64\nyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" | 707 "a:2:32:64\nyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" |
966 "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"); | 708 "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"); |
967 // Run the parse. | 709 // Run the parse. |
968 SafeBrowsingProtocolParser parser; | 710 SafeBrowsingProtocolParser parser; |
969 bool re_key = false; | |
970 SBChunkList chunks; | 711 SBChunkList chunks; |
971 bool result = parser.ParseChunk( | 712 bool result = parser.ParseChunk( |
972 safe_browsing_util::kDownloadWhiteList, | 713 safe_browsing_util::kDownloadWhiteList, |
973 add_chunk.data(), | 714 add_chunk.data(), |
974 static_cast<int>(add_chunk.length()), | 715 static_cast<int>(add_chunk.length()), |
975 "", "", &re_key, &chunks); | 716 &chunks); |
976 EXPECT_TRUE(result); | 717 EXPECT_TRUE(result); |
977 EXPECT_FALSE(re_key); | |
978 EXPECT_EQ(chunks.size(), 2U); | 718 EXPECT_EQ(chunks.size(), 2U); |
979 EXPECT_EQ(chunks[0].chunk_number, 1); | 719 EXPECT_EQ(chunks[0].chunk_number, 1); |
980 EXPECT_EQ(chunks[0].hosts.size(), 1U); | 720 EXPECT_EQ(chunks[0].hosts.size(), 1U); |
981 EXPECT_EQ(chunks[0].hosts[0].host, 0); | 721 EXPECT_EQ(chunks[0].hosts[0].host, 0); |
982 SBEntry* entry = chunks[0].hosts[0].entry; | 722 SBEntry* entry = chunks[0].hosts[0].entry; |
983 EXPECT_TRUE(entry->IsAdd()); | 723 EXPECT_TRUE(entry->IsAdd()); |
984 EXPECT_FALSE(entry->IsPrefix()); | 724 EXPECT_FALSE(entry->IsPrefix()); |
985 EXPECT_EQ(entry->prefix_count(), 1); | 725 EXPECT_EQ(entry->prefix_count(), 1); |
986 SBFullHash full; | 726 SBFullHash full; |
987 memcpy(full.full_hash, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 32); | 727 memcpy(full.full_hash, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 32); |
(...skipping 11 matching lines...) Expand all Loading... |
999 memcpy(full.full_hash, "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", 32); | 739 memcpy(full.full_hash, "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", 32); |
1000 EXPECT_TRUE(entry->FullHashAt(1) == full); | 740 EXPECT_TRUE(entry->FullHashAt(1) == full); |
1001 } | 741 } |
1002 | 742 |
1003 // Test parsing one sub chunk. | 743 // Test parsing one sub chunk. |
1004 TEST(SafeBrowsingProtocolParsingTest, TestSubDownloadWhitelistChunk) { | 744 TEST(SafeBrowsingProtocolParsingTest, TestSubDownloadWhitelistChunk) { |
1005 std::string sub_chunk("s:1:32:36\n1111xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); | 745 std::string sub_chunk("s:1:32:36\n1111xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); |
1006 | 746 |
1007 // Run the parser. | 747 // Run the parser. |
1008 SafeBrowsingProtocolParser parser; | 748 SafeBrowsingProtocolParser parser; |
1009 bool re_key = false; | |
1010 SBChunkList chunks; | 749 SBChunkList chunks; |
1011 bool result = parser.ParseChunk( | 750 bool result = parser.ParseChunk( |
1012 safe_browsing_util::kDownloadWhiteList, | 751 safe_browsing_util::kDownloadWhiteList, |
1013 sub_chunk.data(), | 752 sub_chunk.data(), |
1014 static_cast<int>(sub_chunk.length()), | 753 static_cast<int>(sub_chunk.length()), |
1015 "", "", &re_key, &chunks); | 754 &chunks); |
1016 ASSERT_TRUE(result); | 755 ASSERT_TRUE(result); |
1017 EXPECT_FALSE(re_key); | |
1018 ASSERT_EQ(chunks.size(), 1U); | 756 ASSERT_EQ(chunks.size(), 1U); |
1019 EXPECT_EQ(chunks[0].chunk_number, 1); | 757 EXPECT_EQ(chunks[0].chunk_number, 1); |
1020 EXPECT_EQ(chunks[0].hosts.size(), 1U); | 758 EXPECT_EQ(chunks[0].hosts.size(), 1U); |
1021 | 759 |
1022 EXPECT_EQ(chunks[0].hosts[0].host, 0); | 760 EXPECT_EQ(chunks[0].hosts[0].host, 0); |
1023 SBEntry* entry = chunks[0].hosts[0].entry; | 761 SBEntry* entry = chunks[0].hosts[0].entry; |
1024 EXPECT_TRUE(entry->IsSub()); | 762 EXPECT_TRUE(entry->IsSub()); |
1025 ASSERT_FALSE(entry->IsPrefix()); | 763 ASSERT_FALSE(entry->IsPrefix()); |
1026 ASSERT_EQ(entry->prefix_count(), 1); | 764 ASSERT_EQ(entry->prefix_count(), 1); |
1027 EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x31313131); | 765 EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x31313131); |
1028 SBFullHash full; | 766 SBFullHash full; |
1029 memcpy(full.full_hash, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 32); | 767 memcpy(full.full_hash, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 32); |
1030 EXPECT_TRUE(entry->FullHashAt(0) == full); | 768 EXPECT_TRUE(entry->FullHashAt(0) == full); |
1031 } | 769 } |
OLD | NEW |