license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame^] | 1 | // 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | // |
| 5 | // Program to test the SafeBrowsing protocol parsing v2.1. |
| 6 | |
| 7 | #include <hash_map> |
| 8 | |
| 9 | #include "base/logging.h" |
| 10 | #include "base/string_util.h" |
| 11 | #include "base/win_util.h" |
| 12 | #include "chrome/browser/safe_browsing/protocol_parser.h" |
| 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
| 15 | |
| 16 | // Test parsing one add chunk. |
| 17 | TEST(SafeBrowsingProtocolParsingTest, TestAddChunk) { |
| 18 | std::string add_chunk("a:1:4:35\naaaax1111\0032222333344447777\00288889999"); |
| 19 | add_chunk[13] = '\0'; |
| 20 | |
| 21 | // Run the parse. |
| 22 | SafeBrowsingProtocolParser parser; |
| 23 | bool re_key = false; |
| 24 | std::deque<SBChunk> chunks; |
| 25 | bool result = parser.ParseChunk(add_chunk.data(), |
| 26 | static_cast<int>(add_chunk.length()), |
| 27 | "", "", &re_key, &chunks); |
| 28 | EXPECT_TRUE(result); |
| 29 | EXPECT_FALSE(re_key); |
| 30 | EXPECT_EQ(chunks.size(), 1); |
| 31 | EXPECT_EQ(chunks[0].chunk_number, 1); |
| 32 | EXPECT_EQ(chunks[0].hosts.size(), 3); |
| 33 | |
| 34 | EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161); |
| 35 | SBEntry* entry = chunks[0].hosts[0].entry; |
| 36 | EXPECT_EQ(entry->type(), SBEntry::ADD_PREFIX); |
| 37 | EXPECT_EQ(entry->prefix_count(), 0); |
| 38 | |
| 39 | EXPECT_EQ(chunks[0].hosts[1].host, 0x31313131); |
| 40 | entry = chunks[0].hosts[1].entry; |
| 41 | EXPECT_EQ(entry->type(), SBEntry::ADD_PREFIX); |
| 42 | EXPECT_EQ(entry->prefix_count(), 3); |
| 43 | EXPECT_EQ(entry->PrefixAt(0), 0x32323232); |
| 44 | EXPECT_EQ(entry->PrefixAt(1), 0x33333333); |
| 45 | EXPECT_EQ(entry->PrefixAt(2), 0x34343434); |
| 46 | |
| 47 | EXPECT_EQ(chunks[0].hosts[2].host, 0x37373737); |
| 48 | entry = chunks[0].hosts[2].entry; |
| 49 | EXPECT_EQ(entry->type(), SBEntry::ADD_PREFIX); |
| 50 | EXPECT_EQ(entry->prefix_count(), 2); |
| 51 | EXPECT_EQ(entry->PrefixAt(0), 0x38383838); |
| 52 | EXPECT_EQ(entry->PrefixAt(1), 0x39393939); |
| 53 | |
| 54 | safe_browsing_util::FreeChunks(&chunks); |
| 55 | } |
| 56 | |
| 57 | // Test parsing one add chunk with full hashes. |
| 58 | TEST(SafeBrowsingProtocolParsingTest, TestAddFullChunk) { |
| 59 | std::string add_chunk("a:1:32:69\naaaa"); |
| 60 | add_chunk.push_back(2); |
| 61 | |
| 62 | SBFullHash full_hash1, full_hash2; |
| 63 | for (int i = 0; i < 32; ++i) { |
| 64 | full_hash1.full_hash[i] = i % 2 ? 1 : 2; |
| 65 | full_hash2.full_hash[i] = i % 2 ? 3 : 4; |
| 66 | } |
| 67 | |
| 68 | add_chunk.append(full_hash1.full_hash, 32); |
| 69 | add_chunk.append(full_hash2.full_hash, 32); |
| 70 | |
| 71 | // Run the parse. |
| 72 | SafeBrowsingProtocolParser parser; |
| 73 | bool re_key = false; |
| 74 | std::deque<SBChunk> chunks; |
| 75 | bool result = parser.ParseChunk(add_chunk.data(), |
| 76 | static_cast<int>(add_chunk.length()), |
| 77 | "", "", &re_key, &chunks); |
| 78 | EXPECT_TRUE(result); |
| 79 | EXPECT_FALSE(re_key); |
| 80 | EXPECT_EQ(chunks.size(), 1); |
| 81 | EXPECT_EQ(chunks[0].chunk_number, 1); |
| 82 | EXPECT_EQ(chunks[0].hosts.size(), 1); |
| 83 | |
| 84 | EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161); |
| 85 | SBEntry* entry = chunks[0].hosts[0].entry; |
| 86 | EXPECT_EQ(entry->type(), SBEntry::ADD_FULL_HASH); |
| 87 | 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. |
| 96 | TEST(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); |
| 110 | EXPECT_EQ(chunks.size(), 2); |
| 111 | EXPECT_EQ(chunks[0].chunk_number, 1); |
| 112 | EXPECT_EQ(chunks[0].hosts.size(), 3); |
| 113 | |
| 114 | EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161); |
| 115 | SBEntry* entry = chunks[0].hosts[0].entry; |
| 116 | EXPECT_EQ(entry->type(), SBEntry::ADD_PREFIX); |
| 117 | EXPECT_EQ(entry->prefix_count(), 0); |
| 118 | |
| 119 | EXPECT_EQ(chunks[0].hosts[1].host, 0x31313131); |
| 120 | entry = chunks[0].hosts[1].entry; |
| 121 | EXPECT_EQ(entry->type(), SBEntry::ADD_PREFIX); |
| 122 | EXPECT_EQ(entry->prefix_count(), 3); |
| 123 | EXPECT_EQ(entry->PrefixAt(0), 0x32323232); |
| 124 | EXPECT_EQ(entry->PrefixAt(1), 0x33333333); |
| 125 | EXPECT_EQ(entry->PrefixAt(2), 0x34343434); |
| 126 | |
| 127 | EXPECT_EQ(chunks[0].hosts[2].host, 0x37373737); |
| 128 | entry = chunks[0].hosts[2].entry; |
| 129 | EXPECT_EQ(entry->type(), SBEntry::ADD_PREFIX); |
| 130 | EXPECT_EQ(entry->prefix_count(), 2); |
| 131 | EXPECT_EQ(entry->PrefixAt(0), 0x38383838); |
| 132 | EXPECT_EQ(entry->PrefixAt(1), 0x39393939); |
| 133 | |
| 134 | |
| 135 | EXPECT_EQ(chunks[1].chunk_number, 2); |
| 136 | EXPECT_EQ(chunks[1].hosts.size(), 1); |
| 137 | |
| 138 | EXPECT_EQ(chunks[1].hosts[0].host, 0x35353535); |
| 139 | entry = chunks[1].hosts[0].entry; |
| 140 | EXPECT_EQ(entry->type(), SBEntry::ADD_PREFIX); |
| 141 | EXPECT_EQ(entry->prefix_count(), 2); |
| 142 | EXPECT_EQ(entry->PrefixAt(0), 0x70707070); |
| 143 | EXPECT_EQ(entry->PrefixAt(1), 0x67676767); |
| 144 | |
| 145 | safe_browsing_util::FreeChunks(&chunks); |
| 146 | } |
| 147 | |
| 148 | // Test parsing one add chunk where a hostkey spans several entries. |
| 149 | TEST(SafeBrowsingProtocolParsingTest, TestAddBigChunk) { |
| 150 | std::string add_chunk("a:1:4:1050\naaaaX"); |
| 151 | add_chunk[add_chunk.size() - 1] |= 0xFF; |
| 152 | for (int i = 0; i < 255; ++i) |
| 153 | add_chunk.append(StringPrintf("%04d", i)); |
| 154 | |
| 155 | add_chunk.append("aaaa"); |
| 156 | add_chunk.push_back(5); |
| 157 | for (int i = 0; i < 5; ++i) |
| 158 | add_chunk.append(StringPrintf("001%d", i)); |
| 159 | |
| 160 | SafeBrowsingProtocolParser parser; |
| 161 | bool re_key = false; |
| 162 | std::deque<SBChunk> chunks; |
| 163 | bool result = parser.ParseChunk(add_chunk.data(), |
| 164 | static_cast<int>(add_chunk.length()), |
| 165 | "", "", &re_key, &chunks); |
| 166 | EXPECT_TRUE(result); |
| 167 | EXPECT_FALSE(re_key); |
| 168 | EXPECT_EQ(chunks.size(), 1); |
| 169 | EXPECT_EQ(chunks[0].chunk_number, 1); |
| 170 | |
| 171 | EXPECT_EQ(chunks[0].hosts.size(), 1); |
| 172 | |
| 173 | const SBChunkHost& host = chunks[0].hosts[0]; |
| 174 | EXPECT_EQ(host.host, 0x61616161); |
| 175 | EXPECT_EQ(host.entry->prefix_count(), 260); |
| 176 | |
| 177 | safe_browsing_util::FreeChunks(&chunks); |
| 178 | } |
| 179 | |
| 180 | // Test parsing one sub chunk. |
| 181 | TEST(SafeBrowsingProtocolParsingTest, TestSubChunk) { |
| 182 | std::string sub_chunk("s:9:4:59\naaaaxkkkk1111\003" |
| 183 | "zzzz2222zzzz3333zzzz4444" |
| 184 | "7777\002yyyy8888yyyy9999"); |
| 185 | sub_chunk[13] = '\0'; |
| 186 | |
| 187 | // Run the parse. |
| 188 | SafeBrowsingProtocolParser parser; |
| 189 | bool re_key = false; |
| 190 | std::deque<SBChunk> chunks; |
| 191 | bool result = parser.ParseChunk(sub_chunk.data(), |
| 192 | static_cast<int>(sub_chunk.length()), |
| 193 | "", "", &re_key, &chunks); |
| 194 | EXPECT_TRUE(result); |
| 195 | EXPECT_FALSE(re_key); |
| 196 | EXPECT_EQ(chunks.size(), 1); |
| 197 | EXPECT_EQ(chunks[0].chunk_number, 9); |
| 198 | EXPECT_EQ(chunks[0].hosts.size(), 3); |
| 199 | |
| 200 | EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161); |
| 201 | SBEntry* entry = chunks[0].hosts[0].entry; |
| 202 | EXPECT_EQ(entry->type(), SBEntry::SUB_PREFIX); |
| 203 | EXPECT_EQ(entry->chunk_id(), 0x6b6b6b6b); |
| 204 | EXPECT_EQ(entry->prefix_count(), 0); |
| 205 | |
| 206 | EXPECT_EQ(chunks[0].hosts[1].host, 0x31313131); |
| 207 | entry = chunks[0].hosts[1].entry; |
| 208 | EXPECT_EQ(entry->type(), SBEntry::SUB_PREFIX); |
| 209 | EXPECT_EQ(entry->prefix_count(), 3); |
| 210 | EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x7a7a7a7a); |
| 211 | EXPECT_EQ(entry->PrefixAt(0), 0x32323232); |
| 212 | EXPECT_EQ(entry->ChunkIdAtPrefix(1), 0x7a7a7a7a); |
| 213 | EXPECT_EQ(entry->PrefixAt(1), 0x33333333); |
| 214 | EXPECT_EQ(entry->ChunkIdAtPrefix(2), 0x7a7a7a7a); |
| 215 | EXPECT_EQ(entry->PrefixAt(2), 0x34343434); |
| 216 | |
| 217 | EXPECT_EQ(chunks[0].hosts[2].host, 0x37373737); |
| 218 | entry = chunks[0].hosts[2].entry; |
| 219 | EXPECT_EQ(entry->type(), SBEntry::SUB_PREFIX); |
| 220 | EXPECT_EQ(entry->prefix_count(), 2); |
| 221 | EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x79797979); |
| 222 | EXPECT_EQ(entry->PrefixAt(0), 0x38383838); |
| 223 | EXPECT_EQ(entry->ChunkIdAtPrefix(1), 0x79797979); |
| 224 | EXPECT_EQ(entry->PrefixAt(1), 0x39393939); |
| 225 | |
| 226 | safe_browsing_util::FreeChunks(&chunks); |
| 227 | } |
| 228 | |
| 229 | // Test parsing one sub chunk with full hashes. |
| 230 | TEST(SafeBrowsingProtocolParsingTest, TestSubFullChunk) { |
| 231 | std::string sub_chunk("s:1:32:77\naaaa"); |
| 232 | sub_chunk.push_back(2); |
| 233 | |
| 234 | SBFullHash full_hash1, full_hash2; |
| 235 | for (int i = 0; i < 32; ++i) { |
| 236 | full_hash1.full_hash[i] = i % 2 ? 1 : 2; |
| 237 | full_hash2.full_hash[i] = i % 2 ? 3 : 4; |
| 238 | } |
| 239 | |
| 240 | sub_chunk.append("yyyy"); |
| 241 | sub_chunk.append(full_hash1.full_hash, 32); |
| 242 | sub_chunk.append("zzzz"); |
| 243 | sub_chunk.append(full_hash2.full_hash, 32); |
| 244 | |
| 245 | // Run the parse. |
| 246 | SafeBrowsingProtocolParser parser; |
| 247 | bool re_key = false; |
| 248 | std::deque<SBChunk> chunks; |
| 249 | bool result = parser.ParseChunk(sub_chunk.data(), |
| 250 | static_cast<int>(sub_chunk.length()), |
| 251 | "", "", &re_key, &chunks); |
| 252 | EXPECT_TRUE(result); |
| 253 | EXPECT_FALSE(re_key); |
| 254 | EXPECT_EQ(chunks.size(), 1); |
| 255 | EXPECT_EQ(chunks[0].chunk_number, 1); |
| 256 | EXPECT_EQ(chunks[0].hosts.size(), 1); |
| 257 | |
| 258 | EXPECT_EQ(chunks[0].hosts[0].host, 0x61616161); |
| 259 | SBEntry* entry = chunks[0].hosts[0].entry; |
| 260 | EXPECT_EQ(entry->type(), SBEntry::SUB_FULL_HASH); |
| 261 | EXPECT_EQ(entry->prefix_count(), 2); |
| 262 | EXPECT_EQ(entry->ChunkIdAtPrefix(0), 0x79797979); |
| 263 | EXPECT_TRUE(entry->FullHashAt(0) == full_hash1); |
| 264 | EXPECT_EQ(entry->ChunkIdAtPrefix(1), 0x7a7a7a7a); |
| 265 | EXPECT_TRUE(entry->FullHashAt(1) == full_hash2); |
| 266 | |
| 267 | safe_browsing_util::FreeChunks(&chunks); |
| 268 | } |
| 269 | |
| 270 | // Test parsing the SafeBrowsing update response. |
| 271 | TEST(SafeBrowsingProtocolParsingTest, TestChunkDelete) { |
| 272 | std::string add_del("n:1700\ni:phishy\nad:1-7,43-597,44444,99999\n" |
| 273 | "i:malware\nsd:21-27,42,171717\n"); |
| 274 | |
| 275 | SafeBrowsingProtocolParser parser; |
| 276 | int next_query_sec = 0; |
| 277 | bool re_key = false; |
| 278 | bool reset = false; |
| 279 | std::vector<SBChunkDelete> deletes; |
| 280 | std::vector<ChunkUrl> urls; |
| 281 | EXPECT_TRUE(parser.ParseUpdate(add_del.data(), |
| 282 | static_cast<int>(add_del.length()), "", |
| 283 | &next_query_sec, &re_key, |
| 284 | &reset, &deletes, &urls)); |
| 285 | |
| 286 | EXPECT_TRUE(urls.empty()); |
| 287 | EXPECT_FALSE(re_key); |
| 288 | EXPECT_FALSE(reset); |
| 289 | EXPECT_EQ(next_query_sec, 1700); |
| 290 | EXPECT_EQ(deletes.size(), 2); |
| 291 | |
| 292 | EXPECT_EQ(deletes[0].chunk_del.size(), 4); |
| 293 | EXPECT_TRUE(deletes[0].chunk_del[0] == ChunkRange(1, 7)); |
| 294 | EXPECT_TRUE(deletes[0].chunk_del[1] == ChunkRange(43, 597)); |
| 295 | EXPECT_TRUE(deletes[0].chunk_del[2] == ChunkRange(44444)); |
| 296 | EXPECT_TRUE(deletes[0].chunk_del[3] == ChunkRange(99999)); |
| 297 | |
| 298 | EXPECT_EQ(deletes[1].chunk_del.size(), 3); |
| 299 | EXPECT_TRUE(deletes[1].chunk_del[0] == ChunkRange(21, 27)); |
| 300 | EXPECT_TRUE(deletes[1].chunk_del[1] == ChunkRange(42)); |
| 301 | EXPECT_TRUE(deletes[1].chunk_del[2] == ChunkRange(171717)); |
| 302 | |
| 303 | // An update response with missing list name. |
| 304 | |
| 305 | next_query_sec = 0; |
| 306 | deletes.clear(); |
| 307 | urls.clear(); |
| 308 | add_del = "n:1700\nad:1-7,43-597,44444,99999\ni:malware\nsd:4,21-27171717\n"; |
| 309 | EXPECT_FALSE(parser.ParseUpdate(add_del.data(), |
| 310 | static_cast<int>(add_del.length()), "", |
| 311 | &next_query_sec, &re_key, |
| 312 | &reset, &deletes, &urls)); |
| 313 | } |
| 314 | |
| 315 | // Test parsing the SafeBrowsing update response. |
| 316 | TEST(SafeBrowsingProtocolParsingTest, TestRedirects) { |
| 317 | std::string redirects("i:goog-malware-shavar\n" |
| 318 | "u:cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_1\n" |
| 319 | "u:cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_2\n" |
| 320 | "u:cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_3\n" |
| 321 | "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8641-8800:8641-8689," |
| 322 | "8691-8731,8733-8786\n"); |
| 323 | |
| 324 | SafeBrowsingProtocolParser parser; |
| 325 | int next_query_sec = 0; |
| 326 | bool re_key = false; |
| 327 | bool reset = false; |
| 328 | std::vector<SBChunkDelete> deletes; |
| 329 | std::vector<ChunkUrl> urls; |
| 330 | EXPECT_TRUE(parser.ParseUpdate(redirects.data(), |
| 331 | static_cast<int>(redirects.length()), "", |
| 332 | &next_query_sec, &re_key, |
| 333 | &reset, &deletes, &urls)); |
| 334 | |
| 335 | EXPECT_FALSE(re_key); |
| 336 | EXPECT_FALSE(reset); |
| 337 | EXPECT_EQ(urls.size(), 4); |
| 338 | EXPECT_EQ(urls[0].url, |
| 339 | "cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_1"); |
| 340 | EXPECT_EQ(urls[1].url, |
| 341 | "cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_2"); |
| 342 | EXPECT_EQ(urls[2].url, |
| 343 | "cache.googlevideo.com/safebrowsing/rd/goog-malware-shavar_s_3"); |
| 344 | EXPECT_EQ(urls[3].url, |
| 345 | "s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8641-8800:8641-8689," |
| 346 | "8691-8731,8733-8786"); |
| 347 | EXPECT_EQ(next_query_sec, 0); |
| 348 | EXPECT_TRUE(deletes.empty()); |
| 349 | } |
| 350 | |
| 351 | TEST(SafeBrowsingProtocolParsingTest, TestRedirectsWithMac) { |
| 352 | std::string redirects("i:goog-phish-shavar\n" |
| 353 | "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6501-6505:6501-6505," |
| 354 | "pcY6iVeT9-CBQ3fdAF0rpnKjR1Y=\n" |
| 355 | "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8001-8160:8001-8024," |
| 356 | "8026-8045,8048-8049,8051-8134,8136-8152,8155-8160," |
| 357 | "j6XXAEWnjYk9tVVLBSdQvIEq2Wg=\n"); |
| 358 | |
| 359 | SafeBrowsingProtocolParser parser; |
| 360 | int next_query_sec = 0; |
| 361 | bool re_key = false; |
| 362 | bool reset = false; |
| 363 | const std::string key("58Lqn5WIP961x3zuLGo5Uw=="); |
| 364 | std::vector<SBChunkDelete> deletes; |
| 365 | std::vector<ChunkUrl> urls; |
| 366 | EXPECT_TRUE(parser.ParseUpdate(redirects.data(), |
| 367 | static_cast<int>(redirects.length()), key, |
| 368 | &next_query_sec, &re_key, |
| 369 | &reset, &deletes, &urls)); |
| 370 | |
| 371 | EXPECT_FALSE(re_key); |
| 372 | EXPECT_FALSE(reset); |
| 373 | EXPECT_EQ(urls.size(), 2); |
| 374 | EXPECT_EQ(urls[0].url, |
| 375 | "s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6501-6505:6501-6505"); |
| 376 | EXPECT_EQ(urls[0].mac, "pcY6iVeT9-CBQ3fdAF0rpnKjR1Y="); |
| 377 | EXPECT_EQ(urls[1].url, |
| 378 | "s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8001-8160:8001-8024," |
| 379 | "8026-8045,8048-8049,8051-8134,8136-8152,8155-8160"); |
| 380 | EXPECT_EQ(urls[1].mac, "j6XXAEWnjYk9tVVLBSdQvIEq2Wg="); |
| 381 | } |
| 382 | |
| 383 | // Test parsing various SafeBrowsing protocol headers. |
| 384 | TEST(SafeBrowsingProtocolParsingTest, TestNextQueryTime) { |
| 385 | std::string headers("n:1800\ni:goog-white-shavar\n"); |
| 386 | SafeBrowsingProtocolParser parser; |
| 387 | int next_query_sec = 0; |
| 388 | bool re_key = false; |
| 389 | bool reset = false; |
| 390 | std::vector<SBChunkDelete> deletes; |
| 391 | std::vector<ChunkUrl> urls; |
| 392 | EXPECT_TRUE(parser.ParseUpdate(headers.data(), |
| 393 | static_cast<int>(headers.length()), "", |
| 394 | &next_query_sec, &re_key, |
| 395 | &reset, &deletes, &urls)); |
| 396 | |
| 397 | EXPECT_EQ(next_query_sec, 1800); |
| 398 | EXPECT_FALSE(re_key); |
| 399 | EXPECT_FALSE(reset); |
| 400 | EXPECT_TRUE(deletes.empty()); |
| 401 | EXPECT_TRUE(urls.empty()); |
| 402 | } |
| 403 | |
| 404 | // Test parsing data from a GetHashRequest |
| 405 | TEST(SafeBrowsingProtocolParsingTest, TestGetHash) { |
| 406 | std::string get_hash("goog-phish-shavar:19:96\n" |
| 407 | "00112233445566778899aabbccddeeff" |
| 408 | "00001111222233334444555566667777" |
| 409 | "ffffeeeeddddccccbbbbaaaa99998888"); |
| 410 | std::vector<SBFullHashResult> full_hashes; |
| 411 | bool re_key = false; |
| 412 | SafeBrowsingProtocolParser parser; |
| 413 | parser.ParseGetHash(get_hash.data(), |
| 414 | static_cast<int>(get_hash.length()), "", |
| 415 | &re_key, |
| 416 | &full_hashes); |
| 417 | |
| 418 | EXPECT_FALSE(re_key); |
| 419 | EXPECT_EQ(full_hashes.size(), 3); |
| 420 | EXPECT_EQ(memcmp(&full_hashes[0].hash, |
| 421 | "00112233445566778899aabbccddeeff", |
| 422 | sizeof(SBFullHash)), 0); |
| 423 | EXPECT_EQ(full_hashes[0].list_name, "goog-phish-shavar"); |
| 424 | EXPECT_EQ(memcmp(&full_hashes[1].hash, |
| 425 | "00001111222233334444555566667777", |
| 426 | sizeof(SBFullHash)), 0); |
| 427 | EXPECT_EQ(full_hashes[1].list_name, "goog-phish-shavar"); |
| 428 | EXPECT_EQ(memcmp(&full_hashes[2].hash, |
| 429 | "ffffeeeeddddccccbbbbaaaa99998888", |
| 430 | sizeof(SBFullHash)), 0); |
| 431 | EXPECT_EQ(full_hashes[2].list_name, "goog-phish-shavar"); |
| 432 | |
| 433 | // Test multiple lists in the GetHash results. |
| 434 | std::string get_hash2("goog-phish-shavar:19:32\n" |
| 435 | "00112233445566778899aabbccddeeff" |
| 436 | "goog-malware-shavar:19:64\n" |
| 437 | "cafebeefcafebeefdeaddeaddeaddead" |
| 438 | "zzzzyyyyxxxxwwwwvvvvuuuuttttssss"); |
| 439 | parser.ParseGetHash(get_hash2.data(), |
| 440 | static_cast<int>(get_hash2.length()), "", |
| 441 | &re_key, |
| 442 | &full_hashes); |
| 443 | |
| 444 | EXPECT_FALSE(re_key); |
| 445 | EXPECT_EQ(full_hashes.size(), 3); |
| 446 | EXPECT_EQ(memcmp(&full_hashes[0].hash, |
| 447 | "00112233445566778899aabbccddeeff", |
| 448 | sizeof(SBFullHash)), 0); |
| 449 | EXPECT_EQ(full_hashes[0].list_name, "goog-phish-shavar"); |
| 450 | EXPECT_EQ(memcmp(&full_hashes[1].hash, |
| 451 | "cafebeefcafebeefdeaddeaddeaddead", |
| 452 | sizeof(SBFullHash)), 0); |
| 453 | EXPECT_EQ(full_hashes[1].list_name, "goog-malware-shavar"); |
| 454 | EXPECT_EQ(memcmp(&full_hashes[2].hash, |
| 455 | "zzzzyyyyxxxxwwwwvvvvuuuuttttssss", |
| 456 | sizeof(SBFullHash)), 0); |
| 457 | EXPECT_EQ(full_hashes[2].list_name, "goog-malware-shavar"); |
| 458 | } |
| 459 | |
| 460 | TEST(SafeBrowsingProtocolParsingTest, TestGetHashWithMac) { |
| 461 | // TODO(paulg): Bug: http://b/1084719, skip this test on Windows 2000 until |
| 462 | // this bug is fixed. |
| 463 | if (win_util::GetWinVersion() <= win_util::WINVERSION_2000) |
| 464 | return; |
| 465 | |
| 466 | const unsigned char get_hash[] = { |
| 467 | 0x32, 0x56, 0x74, 0x6f, 0x6b, 0x36, 0x64, 0x41, |
| 468 | 0x51, 0x72, 0x65, 0x51, 0x62, 0x38, 0x51, 0x68, |
| 469 | 0x59, 0x45, 0x57, 0x51, 0x57, 0x4d, 0x52, 0x65, |
| 470 | 0x42, 0x63, 0x41, 0x3d, 0x0a, 0x67, 0x6f, 0x6f, |
| 471 | 0x67, 0x2d, 0x70, 0x68, 0x69, 0x73, 0x68, 0x2d, |
| 472 | 0x73, 0x68, 0x61, 0x76, 0x61, 0x72, 0x3a, 0x36, |
| 473 | 0x31, 0x36, 0x39, 0x3a, 0x33, 0x32, 0x0a, 0x17, |
| 474 | 0x7f, 0x03, 0x42, 0x28, 0x1c, 0x31, 0xb9, 0x0b, |
| 475 | 0x1c, 0x7b, 0x9d, 0xaf, 0x7b, 0x43, 0x99, 0x10, |
| 476 | 0xc1, 0xab, 0xe3, 0x1b, 0x35, 0x80, 0x38, 0x96, |
| 477 | 0xf9, 0x44, 0x4f, 0x28, 0xb4, 0xeb, 0x45 |
| 478 | }; |
| 479 | |
| 480 | const unsigned char hash_result [] = { |
| 481 | 0x17, 0x7f, 0x03, 0x42, 0x28, 0x1c, 0x31, 0xb9, |
| 482 | 0x0b, 0x1c, 0x7b, 0x9d, 0xaf, 0x7b, 0x43, 0x99, |
| 483 | 0x10, 0xc1, 0xab, 0xe3, 0x1b, 0x35, 0x80, 0x38, |
| 484 | 0x96, 0xf9, 0x44, 0x4f, 0x28, 0xb4, 0xeb, 0x45 |
| 485 | }; |
| 486 | |
| 487 | const std::string key = "58Lqn5WIP961x3zuLGo5Uw=="; |
| 488 | std::vector<SBFullHashResult> full_hashes; |
| 489 | bool re_key = false; |
| 490 | SafeBrowsingProtocolParser parser; |
| 491 | EXPECT_TRUE(parser.ParseGetHash(reinterpret_cast<const char*>(get_hash), |
| 492 | sizeof(get_hash), |
| 493 | key, |
| 494 | &re_key, |
| 495 | &full_hashes)); |
| 496 | EXPECT_FALSE(re_key); |
| 497 | EXPECT_EQ(full_hashes.size(), 1); |
| 498 | EXPECT_EQ(memcmp(hash_result, &full_hashes[0].hash, sizeof(SBFullHash)), 0); |
| 499 | } |
| 500 | |
| 501 | |
| 502 | TEST(SafeBrowsingProtocolParsingTest, TestFormatHash) { |
| 503 | SafeBrowsingProtocolParser parser; |
| 504 | std::vector<SBPrefix> prefixes; |
| 505 | std::string get_hash; |
| 506 | |
| 507 | prefixes.push_back(0x34333231); |
| 508 | prefixes.push_back(0x64636261); |
| 509 | prefixes.push_back(0x73727170); |
| 510 | |
| 511 | parser.FormatGetHash(prefixes, &get_hash); |
| 512 | EXPECT_EQ(get_hash, "4:12\n1234abcdpqrs"); |
| 513 | } |
| 514 | |
| 515 | TEST(SafeBrowsingProtocolParsingTest, TestGetKey) { |
| 516 | SafeBrowsingProtocolParser parser; |
| 517 | std::string key_response("clientkey:10:0123456789\n" |
| 518 | "wrappedkey:20:abcdefghijklmnopqrst\n"); |
| 519 | |
| 520 | std::string client_key, wrapped_key; |
| 521 | EXPECT_TRUE(parser.ParseNewKey(key_response.data(), |
| 522 | static_cast<int>(key_response.length()), |
| 523 | &client_key, |
| 524 | &wrapped_key)); |
| 525 | |
| 526 | EXPECT_EQ(client_key, "0123456789"); |
| 527 | EXPECT_EQ(wrapped_key, "abcdefghijklmnopqrst"); |
| 528 | } |
| 529 | |
| 530 | TEST(SafeBrowsingProtocolParsingTest, TestReKey) { |
| 531 | SafeBrowsingProtocolParser parser; |
| 532 | std::string update("n:1800\ni:phishy\ne:pleaserekey\n"); |
| 533 | |
| 534 | bool re_key = false; |
| 535 | bool reset = false; |
| 536 | int next_update = -1; |
| 537 | std::vector<SBChunkDelete> deletes; |
| 538 | std::vector<ChunkUrl> urls; |
| 539 | EXPECT_TRUE(parser.ParseUpdate(update.data(), |
| 540 | static_cast<int>(update.size()), "", |
| 541 | &next_update, &re_key, |
| 542 | &reset, &deletes, &urls)); |
| 543 | EXPECT_TRUE(re_key); |
| 544 | } |
| 545 | |
| 546 | TEST(SafeBrowsingProtocolParsingTest, TestReset) { |
| 547 | SafeBrowsingProtocolParser parser; |
| 548 | std::string update("n:1800\ni:phishy\nr:pleasereset\n"); |
| 549 | |
| 550 | bool re_key = false; |
| 551 | bool reset = false; |
| 552 | int next_update = -1; |
| 553 | std::vector<SBChunkDelete> deletes; |
| 554 | std::vector<ChunkUrl> urls; |
| 555 | EXPECT_TRUE(parser.ParseUpdate(update.data(), |
| 556 | static_cast<int>(update.size()), "", |
| 557 | &next_update, &re_key, |
| 558 | &reset, &deletes, &urls)); |
| 559 | EXPECT_TRUE(reset); |
| 560 | } |
| 561 | |
| 562 | TEST(SafeBrowsingProtocolParsingTest, TestVerifyUpdateMac) { |
| 563 | // TODO(paulg): Bug: http://b/1084719, skip this test on Windows 2000 until |
| 564 | // this bug is fixed. |
| 565 | if (win_util::GetWinVersion() <= win_util::WINVERSION_2000) |
| 566 | return; |
| 567 | |
| 568 | SafeBrowsingProtocolParser parser; |
| 569 | |
| 570 | const std::string update = |
| 571 | "m:XIU0LiQhAPJq6dynXwHbygjS5tw=\n" |
| 572 | "n:1895\n" |
| 573 | "i:goog-phish-shavar\n" |
| 574 | "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6501-6505:6501-6505,pcY6iVeT9-CBQ3fdAF0rpnKjR1Y=\n" |
| 575 | "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6506-6510:6506-6510,SDBrYC3rX3KEPe72LOypnP6QYac=\n" |
| 576 | "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6511-6520:6511-6520,9UQo-e7OkcsXT2wFWTAhOuWOsUs=\n" |
| 577 | "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6521-6560:6521-6560,qVNw6JIpR1q6PIXST7J4LJ9n3Zg=\n" |
| 578 | "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6561-6720:6561-6720,7OiJvCbiwvpzPITW-hQohY5NHuc=\n" |
| 579 | "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6721-6880:6721-6880,oBS3svhoi9deIa0sWZ_gnD0ujj8=\n" |
| 580 | "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_6881-7040:6881-7040,a0r8Xit4VvH39xgyQHZTPczKBIE=\n" |
| 581 | "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_s_7041-7200:7041-7163,q538LChutGknBw55s6kcE2wTcvU=\n" |
| 582 | "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8001-8160:8001-8024,8026-8045,8048-8049,8051-8134,8136-8152,8155-8160,j6XXAEWnjYk9tVVLBSdQvIEq2Wg=\n" |
| 583 | "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8161-8320:8161-8215,8217-8222,8224-8320,YaNfiqdQOt-uLCLWVLj46AZpAjQ=\n" |
| 584 | "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8321-8480:8321-8391,8393-8399,8402,8404-8419,8421-8425,8427,8431-8433,8435-8439,8441-8443,8445-8446,8448-8480,ALj31GQMwGiIeU3bM2ZYKITfU-U=\n" |
| 585 | "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8481-8640:8481-8500,8502-8508,8510-8511,8513-8517,8519-8525,8527-8531,8533,8536-8539,8541-8576,8578-8638,8640,TlQYRmS_kZ5PBAUIUyNQDq0Jprs=\n" |
| 586 | "u:s.ytimg.com/safebrowsing/rd/goog-phish-shavar_a_8641-8800:8641-8689,8691-8731,8733-8786,x1Qf7hdNrO8b6yym03ZzNydDS1o=\n"; |
| 587 | |
| 588 | bool re_key = false; |
| 589 | bool reset = false; |
| 590 | int next_update = -1; |
| 591 | std::vector<SBChunkDelete> deletes; |
| 592 | std::vector<ChunkUrl> urls; |
| 593 | const std::string key("58Lqn5WIP961x3zuLGo5Uw=="); |
| 594 | EXPECT_TRUE(parser.ParseUpdate(update.data(), |
| 595 | static_cast<int>(update.size()), key, |
| 596 | &next_update, &re_key, |
| 597 | &reset, &deletes, &urls)); |
| 598 | EXPECT_FALSE(re_key); |
| 599 | EXPECT_EQ(next_update, 1895); |
| 600 | } |
| 601 | |
| 602 | TEST(SafeBrowsingProtocolParsingTest, TestVerifyChunkMac) { |
| 603 | // TODO(paulg): Bug: http://b/1084719, skip this test on Windows 2000 until |
| 604 | // this bug is fixed. |
| 605 | if (win_util::GetWinVersion() <= win_util::WINVERSION_2000) |
| 606 | return; |
| 607 | |
| 608 | SafeBrowsingProtocolParser parser; |
| 609 | |
| 610 | const unsigned char chunk[] = { |
| 611 | 0x73, 0x3a, 0x32, 0x30, 0x30, 0x32, 0x3a, 0x34, |
| 612 | 0x3a, 0x32, 0x32, 0x0a, 0x2f, 0x4f, 0x89, 0x7a, |
| 613 | 0x01, 0x00, 0x00, 0x0a, 0x59, 0xc8, 0x71, 0xdf, |
| 614 | 0x9d, 0x29, 0x0c, 0xba, 0xd7, 0x00, 0x00, 0x00, |
| 615 | 0x0a, 0x59 |
| 616 | }; |
| 617 | |
| 618 | bool re_key = false; |
| 619 | std::deque<SBChunk> chunks; |
| 620 | const std::string key("v_aDSz6jI92WeHCOoZ07QA=="); |
| 621 | const std::string mac("W9Xp2fUcQ9V66If6Cvsrstpa4Kk="); |
| 622 | |
| 623 | EXPECT_TRUE(parser.ParseChunk(reinterpret_cast<const char*>(chunk), |
| 624 | sizeof(chunk), key, mac, |
| 625 | &re_key, &chunks)); |
| 626 | EXPECT_FALSE(re_key); |
| 627 | |
| 628 | safe_browsing_util::FreeChunks(&chunks); |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame^] | 629 | } |