| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 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 // Unit tests for the SafeBrowsing storage system (specific to the | 5 // Unit tests for the SafeBrowsing storage system (specific to the |
| 6 // SafeBrowsingDatabaseImpl implementation). | 6 // SafeBrowsingDatabaseImpl implementation). |
| 7 | 7 |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 | 82 |
| 83 std::vector<SBPrefix> prefixes; | 83 std::vector<SBPrefix> prefixes; |
| 84 database->CacheHashResults(prefixes, results); | 84 database->CacheHashResults(prefixes, results); |
| 85 } | 85 } |
| 86 | 86 |
| 87 TEST(SafeBrowsingDatabaseImpl, HashCaching) { | 87 TEST(SafeBrowsingDatabaseImpl, HashCaching) { |
| 88 std::wstring filename; | 88 std::wstring filename; |
| 89 PathService::Get(base::DIR_TEMP, &filename); | 89 PathService::Get(base::DIR_TEMP, &filename); |
| 90 filename.push_back(file_util::kPathSeparator); | 90 filename.push_back(file_util::kPathSeparator); |
| 91 filename.append(L"SafeBrowsingTestDatabase"); | 91 filename.append(L"SafeBrowsingTestDatabase"); |
| 92 DeleteFile(filename.c_str()); // In case it existed from a previous run. | 92 file_util::Delete(filename, false); // In case it existed from a previous run
. |
| 93 | 93 |
| 94 SafeBrowsingDatabaseImpl database; | 94 SafeBrowsingDatabaseImpl database; |
| 95 database.SetSynchronous(); | 95 database.SetSynchronous(); |
| 96 EXPECT_TRUE(database.Init(filename, NULL)); | 96 EXPECT_TRUE(database.Init(filename, NULL)); |
| 97 | 97 |
| 98 PopulateDatabaseForCacheTest(&database); | 98 PopulateDatabaseForCacheTest(&database); |
| 99 | 99 |
| 100 // We should have both full hashes in the cache. | 100 // We should have both full hashes in the cache. |
| 101 EXPECT_EQ(database.hash_cache_.size(), 2); | 101 EXPECT_EQ(database.hash_cache_.size(), 2U); |
| 102 | 102 |
| 103 // Test the cache lookup for the first prefix. | 103 // Test the cache lookup for the first prefix. |
| 104 std::string list; | 104 std::string list; |
| 105 std::vector<SBPrefix> prefixes; | 105 std::vector<SBPrefix> prefixes; |
| 106 std::vector<SBFullHashResult> full_hashes; | 106 std::vector<SBFullHashResult> full_hashes; |
| 107 database.ContainsUrl(GURL("http://www.evil.com/phishing.html"), | 107 database.ContainsUrl(GURL("http://www.evil.com/phishing.html"), |
| 108 &list, &prefixes, &full_hashes, Time::Now()); | 108 &list, &prefixes, &full_hashes, Time::Now()); |
| 109 EXPECT_EQ(full_hashes.size(), 1); | 109 EXPECT_EQ(full_hashes.size(), 1U); |
| 110 | 110 |
| 111 SBFullHashResult full_hash; | 111 SBFullHashResult full_hash; |
| 112 base::SHA256HashString("www.evil.com/phishing.html", | 112 base::SHA256HashString("www.evil.com/phishing.html", |
| 113 &full_hash.hash, sizeof(SBFullHash)); | 113 &full_hash.hash, sizeof(SBFullHash)); |
| 114 EXPECT_EQ(memcmp(&full_hashes[0].hash, | 114 EXPECT_EQ(memcmp(&full_hashes[0].hash, |
| 115 &full_hash.hash, sizeof(SBFullHash)), 0); | 115 &full_hash.hash, sizeof(SBFullHash)), 0); |
| 116 | 116 |
| 117 prefixes.clear(); | 117 prefixes.clear(); |
| 118 full_hashes.clear(); | 118 full_hashes.clear(); |
| 119 | 119 |
| 120 // Test the cache lookup for the second prefix. | 120 // Test the cache lookup for the second prefix. |
| 121 database.ContainsUrl(GURL("http://www.evil.com/malware.html"), | 121 database.ContainsUrl(GURL("http://www.evil.com/malware.html"), |
| 122 &list, &prefixes, &full_hashes, Time::Now()); | 122 &list, &prefixes, &full_hashes, Time::Now()); |
| 123 EXPECT_EQ(full_hashes.size(), 1); | 123 EXPECT_EQ(full_hashes.size(), 1U); |
| 124 base::SHA256HashString("www.evil.com/malware.html", | 124 base::SHA256HashString("www.evil.com/malware.html", |
| 125 &full_hash.hash, sizeof(SBFullHash)); | 125 &full_hash.hash, sizeof(SBFullHash)); |
| 126 EXPECT_EQ(memcmp(&full_hashes[0].hash, | 126 EXPECT_EQ(memcmp(&full_hashes[0].hash, |
| 127 &full_hash.hash, sizeof(SBFullHash)), 0); | 127 &full_hash.hash, sizeof(SBFullHash)), 0); |
| 128 | 128 |
| 129 prefixes.clear(); | 129 prefixes.clear(); |
| 130 full_hashes.clear(); | 130 full_hashes.clear(); |
| 131 | 131 |
| 132 // Test removing a prefix via a sub chunk. | 132 // Test removing a prefix via a sub chunk. |
| 133 SBChunkHost host; | 133 SBChunkHost host; |
| 134 host.host = Sha256Prefix("www.evil.com/"); | 134 host.host = Sha256Prefix("www.evil.com/"); |
| 135 host.entry = SBEntry::Create(SBEntry::SUB_PREFIX, 2); | 135 host.entry = SBEntry::Create(SBEntry::SUB_PREFIX, 2); |
| 136 host.entry->set_chunk_id(1); | 136 host.entry->set_chunk_id(1); |
| 137 host.entry->SetChunkIdAtPrefix(0, 1); | 137 host.entry->SetChunkIdAtPrefix(0, 1); |
| 138 host.entry->SetPrefixAt(0, Sha256Prefix("www.evil.com/phishing.html")); | 138 host.entry->SetPrefixAt(0, Sha256Prefix("www.evil.com/phishing.html")); |
| 139 | 139 |
| 140 SBChunk chunk; | 140 SBChunk chunk; |
| 141 chunk.chunk_number = 2; | 141 chunk.chunk_number = 2; |
| 142 chunk.is_add = false; | 142 chunk.is_add = false; |
| 143 chunk.hosts.clear(); | 143 chunk.hosts.clear(); |
| 144 chunk.hosts.push_back(host); | 144 chunk.hosts.push_back(host); |
| 145 std::deque<SBChunk>* chunks = new std::deque<SBChunk>; | 145 std::deque<SBChunk>* chunks = new std::deque<SBChunk>; |
| 146 chunks->push_back(chunk); | 146 chunks->push_back(chunk); |
| 147 database.InsertChunks("goog-malware-shavar", chunks); | 147 database.InsertChunks("goog-malware-shavar", chunks); |
| 148 | 148 |
| 149 // This prefix should still be there. | 149 // This prefix should still be there. |
| 150 database.ContainsUrl(GURL("http://www.evil.com/malware.html"), | 150 database.ContainsUrl(GURL("http://www.evil.com/malware.html"), |
| 151 &list, &prefixes, &full_hashes, Time::Now()); | 151 &list, &prefixes, &full_hashes, Time::Now()); |
| 152 EXPECT_EQ(full_hashes.size(), 1); | 152 EXPECT_EQ(full_hashes.size(), 1U); |
| 153 base::SHA256HashString("www.evil.com/malware.html", | 153 base::SHA256HashString("www.evil.com/malware.html", |
| 154 &full_hash.hash, sizeof(SBFullHash)); | 154 &full_hash.hash, sizeof(SBFullHash)); |
| 155 EXPECT_EQ(memcmp(&full_hashes[0].hash, | 155 EXPECT_EQ(memcmp(&full_hashes[0].hash, |
| 156 &full_hash.hash, sizeof(SBFullHash)), 0); | 156 &full_hash.hash, sizeof(SBFullHash)), 0); |
| 157 | 157 |
| 158 prefixes.clear(); | 158 prefixes.clear(); |
| 159 full_hashes.clear(); | 159 full_hashes.clear(); |
| 160 | 160 |
| 161 // This prefix should be gone. | 161 // This prefix should be gone. |
| 162 database.ContainsUrl(GURL("http://www.evil.com/phishing.html"), | 162 database.ContainsUrl(GURL("http://www.evil.com/phishing.html"), |
| 163 &list, &prefixes, &full_hashes, Time::Now()); | 163 &list, &prefixes, &full_hashes, Time::Now()); |
| 164 EXPECT_EQ(full_hashes.size(), 0); | 164 EXPECT_EQ(full_hashes.size(), 0U); |
| 165 | 165 |
| 166 prefixes.clear(); | 166 prefixes.clear(); |
| 167 full_hashes.clear(); | 167 full_hashes.clear(); |
| 168 | 168 |
| 169 // Test that an AddDel for the original chunk removes the last cached entry. | 169 // Test that an AddDel for the original chunk removes the last cached entry. |
| 170 AddDelChunk(&database, "goog-malware-shavar", 1); | 170 AddDelChunk(&database, "goog-malware-shavar", 1); |
| 171 database.ContainsUrl(GURL("http://www.evil.com/malware.html"), | 171 database.ContainsUrl(GURL("http://www.evil.com/malware.html"), |
| 172 &list, &prefixes, &full_hashes, Time::Now()); | 172 &list, &prefixes, &full_hashes, Time::Now()); |
| 173 EXPECT_EQ(full_hashes.size(), 0); | 173 EXPECT_EQ(full_hashes.size(), 0U); |
| 174 EXPECT_EQ(database.hash_cache_.size(), 0); | 174 EXPECT_EQ(database.hash_cache_.size(), 0U); |
| 175 | 175 |
| 176 prefixes.clear(); | 176 prefixes.clear(); |
| 177 full_hashes.clear(); | 177 full_hashes.clear(); |
| 178 | 178 |
| 179 // Test that the cache won't return expired values. First we have to adjust | 179 // Test that the cache won't return expired values. First we have to adjust |
| 180 // the cached entries' received time to make them older, since the database | 180 // the cached entries' received time to make them older, since the database |
| 181 // cache insert uses Time::Now(). First, store some entries. | 181 // cache insert uses Time::Now(). First, store some entries. |
| 182 PopulateDatabaseForCacheTest(&database); | 182 PopulateDatabaseForCacheTest(&database); |
| 183 EXPECT_EQ(database.hash_cache_.size(), 2); | 183 EXPECT_EQ(database.hash_cache_.size(), 2U); |
| 184 | 184 |
| 185 // Now adjust one of the entries times to be in the past. | 185 // Now adjust one of the entries times to be in the past. |
| 186 Time expired = Time::Now() - TimeDelta::FromMinutes(60); | 186 Time expired = Time::Now() - TimeDelta::FromMinutes(60); |
| 187 SBPrefix key; | 187 SBPrefix key; |
| 188 memcpy(&key, &full_hash.hash, sizeof(SBPrefix)); | 188 memcpy(&key, &full_hash.hash, sizeof(SBPrefix)); |
| 189 SafeBrowsingDatabaseImpl::HashList& entries = database.hash_cache_[key]; | 189 SafeBrowsingDatabaseImpl::HashList& entries = database.hash_cache_[key]; |
| 190 SafeBrowsingDatabaseImpl::HashCacheEntry entry = entries.front(); | 190 SafeBrowsingDatabaseImpl::HashCacheEntry entry = entries.front(); |
| 191 entries.pop_front(); | 191 entries.pop_front(); |
| 192 entry.received = expired; | 192 entry.received = expired; |
| 193 entries.push_back(entry); | 193 entries.push_back(entry); |
| 194 | 194 |
| 195 database.ContainsUrl(GURL("http://www.evil.com/malware.html"), | 195 database.ContainsUrl(GURL("http://www.evil.com/malware.html"), |
| 196 &list, &prefixes, &full_hashes, expired); | 196 &list, &prefixes, &full_hashes, expired); |
| 197 EXPECT_EQ(full_hashes.size(), 0); | 197 EXPECT_EQ(full_hashes.size(), 0U); |
| 198 | 198 |
| 199 // Expired entry was dumped. | 199 // Expired entry was dumped. |
| 200 EXPECT_EQ(database.hash_cache_.size(), 1); | 200 EXPECT_EQ(database.hash_cache_.size(), 1U); |
| 201 | 201 |
| 202 // This entry should still exist. | 202 // This entry should still exist. |
| 203 database.ContainsUrl(GURL("http://www.evil.com/phishing.html"), | 203 database.ContainsUrl(GURL("http://www.evil.com/phishing.html"), |
| 204 &list, &prefixes, &full_hashes, expired); | 204 &list, &prefixes, &full_hashes, expired); |
| 205 EXPECT_EQ(full_hashes.size(), 1); | 205 EXPECT_EQ(full_hashes.size(), 1U); |
| 206 | 206 |
| 207 | 207 |
| 208 // Testing prefix miss caching. First, we clear out the existing database, | 208 // Testing prefix miss caching. First, we clear out the existing database, |
| 209 // Since PopulateDatabaseForCacheTest() doesn't handle adding duplicate | 209 // Since PopulateDatabaseForCacheTest() doesn't handle adding duplicate |
| 210 // chunks. | 210 // chunks. |
| 211 AddDelChunk(&database, "goog-malware-shavar", 1); | 211 AddDelChunk(&database, "goog-malware-shavar", 1); |
| 212 | 212 |
| 213 std::vector<SBPrefix> prefix_misses; | 213 std::vector<SBPrefix> prefix_misses; |
| 214 std::vector<SBFullHashResult> empty_full_hash; | 214 std::vector<SBFullHashResult> empty_full_hash; |
| 215 prefix_misses.push_back(Sha256Prefix("http://www.bad.com/malware.html")); | 215 prefix_misses.push_back(Sha256Prefix("http://www.bad.com/malware.html")); |
| 216 prefix_misses.push_back(Sha256Prefix("http://www.bad.com/phishing.html")); | 216 prefix_misses.push_back(Sha256Prefix("http://www.bad.com/phishing.html")); |
| 217 database.CacheHashResults(prefix_misses, empty_full_hash); | 217 database.CacheHashResults(prefix_misses, empty_full_hash); |
| 218 | 218 |
| 219 // Prefixes with no full results are misses. | 219 // Prefixes with no full results are misses. |
| 220 EXPECT_EQ(database.prefix_miss_cache_.size(), 2); | 220 EXPECT_EQ(database.prefix_miss_cache_.size(), 2U); |
| 221 | 221 |
| 222 // Update the database. | 222 // Update the database. |
| 223 PopulateDatabaseForCacheTest(&database); | 223 PopulateDatabaseForCacheTest(&database); |
| 224 | 224 |
| 225 // Prefix miss cache should be cleared. | 225 // Prefix miss cache should be cleared. |
| 226 EXPECT_EQ(database.prefix_miss_cache_.size(), 0); | 226 EXPECT_EQ(database.prefix_miss_cache_.size(), 0U); |
| 227 } | 227 } |
| OLD | NEW |