OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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. |
| 4 |
| 5 #include <limits.h> |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "net/base/sdch_manager.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace net { |
| 15 |
| 16 //------------------------------------------------------------------------------ |
| 17 // Provide sample data and compression results with a sample VCDIFF dictionary. |
| 18 // Note an SDCH dictionary has extra meta-data before the VCDIFF dictionary. |
| 19 static const char kTestVcdiffDictionary[] = "DictionaryFor" |
| 20 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n"; |
| 21 |
| 22 //------------------------------------------------------------------------------ |
| 23 |
| 24 class SdchManagerTest : public testing::Test { |
| 25 protected: |
| 26 SdchManagerTest() |
| 27 : sdch_manager_(new SdchManager) { |
| 28 } |
| 29 |
| 30 scoped_ptr<SdchManager> sdch_manager_; // A singleton database. |
| 31 }; |
| 32 |
| 33 //------------------------------------------------------------------------------ |
| 34 static std::string NewSdchDictionary(const std::string& domain) { |
| 35 std::string dictionary; |
| 36 if (!domain.empty()) { |
| 37 dictionary.append("Domain: "); |
| 38 dictionary.append(domain); |
| 39 dictionary.append("\n"); |
| 40 } |
| 41 dictionary.append("\n"); |
| 42 dictionary.append(kTestVcdiffDictionary, sizeof(kTestVcdiffDictionary) - 1); |
| 43 return dictionary; |
| 44 } |
| 45 |
| 46 TEST_F(SdchManagerTest, DomainSupported) { |
| 47 GURL google_url("http://www.google.com"); |
| 48 |
| 49 net::SdchManager::EnableSdchSupport(false); |
| 50 EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(google_url)); |
| 51 net::SdchManager::EnableSdchSupport(true); |
| 52 EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(google_url)); |
| 53 } |
| 54 |
| 55 TEST_F(SdchManagerTest, DomainBlacklisting) { |
| 56 GURL test_url("http://www.test.com"); |
| 57 GURL google_url("http://www.google.com"); |
| 58 |
| 59 SdchManager::BlacklistDomain(test_url); |
| 60 EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(test_url)); |
| 61 EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(google_url)); |
| 62 |
| 63 SdchManager::BlacklistDomain(google_url); |
| 64 EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(google_url)); |
| 65 } |
| 66 |
| 67 TEST_F(SdchManagerTest, DomainBlacklistingCaseSensitivity) { |
| 68 GURL test_url("http://www.TesT.com"); |
| 69 GURL test2_url("http://www.tEst.com"); |
| 70 |
| 71 EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(test_url)); |
| 72 EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(test2_url)); |
| 73 SdchManager::BlacklistDomain(test_url); |
| 74 EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(test2_url)); |
| 75 } |
| 76 |
| 77 TEST_F(SdchManagerTest, BlacklistingReset) { |
| 78 GURL gurl("http://mytest.DoMain.com"); |
| 79 std::string domain(gurl.host()); |
| 80 |
| 81 SdchManager::ClearBlacklistings(); |
| 82 EXPECT_EQ(SdchManager::BlackListDomainCount(domain), 0); |
| 83 EXPECT_EQ(SdchManager::BlacklistDomainExponential(domain), 0); |
| 84 EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(gurl)); |
| 85 } |
| 86 |
| 87 TEST_F(SdchManagerTest, BlacklistingSingleBlacklist) { |
| 88 GURL gurl("http://mytest.DoMain.com"); |
| 89 std::string domain(gurl.host()); |
| 90 SdchManager::ClearBlacklistings(); |
| 91 |
| 92 SdchManager::Global()->BlacklistDomain(gurl); |
| 93 EXPECT_EQ(SdchManager::BlackListDomainCount(domain), 1); |
| 94 EXPECT_EQ(SdchManager::BlacklistDomainExponential(domain), 1); |
| 95 |
| 96 // Check that any domain lookup reduces the blacklist counter. |
| 97 EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(gurl)); |
| 98 EXPECT_EQ(SdchManager::BlackListDomainCount(domain), 0); |
| 99 EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(gurl)); |
| 100 } |
| 101 |
| 102 TEST_F(SdchManagerTest, BlacklistingExponential) { |
| 103 GURL gurl("http://mytest.DoMain.com"); |
| 104 std::string domain(gurl.host()); |
| 105 SdchManager::ClearBlacklistings(); |
| 106 |
| 107 int exponential = 1; |
| 108 for (int i = 1; i < 100; ++i) { |
| 109 SdchManager::Global()->BlacklistDomain(gurl); |
| 110 EXPECT_EQ(SdchManager::BlacklistDomainExponential(domain), exponential); |
| 111 |
| 112 EXPECT_EQ(SdchManager::BlackListDomainCount(domain), exponential); |
| 113 EXPECT_FALSE(SdchManager::Global()->IsInSupportedDomain(gurl)); |
| 114 EXPECT_EQ(SdchManager::BlackListDomainCount(domain), exponential - 1); |
| 115 |
| 116 // Simulate a large number of domain checks (which eventually remove the |
| 117 // blacklisting). |
| 118 SdchManager::ClearDomainBlacklisting(domain); |
| 119 EXPECT_EQ(SdchManager::BlackListDomainCount(domain), 0); |
| 120 EXPECT_TRUE(SdchManager::Global()->IsInSupportedDomain(gurl)); |
| 121 |
| 122 // Predict what exponential backoff will be. |
| 123 exponential = 1 + 2 * exponential; |
| 124 if (exponential < 0) |
| 125 exponential = INT_MAX; // We don't wrap. |
| 126 } |
| 127 } |
| 128 |
| 129 TEST_F(SdchManagerTest, CanSetExactMatchDictionary) { |
| 130 std::string dictionary_domain("x.y.z.google.com"); |
| 131 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
| 132 |
| 133 // Perfect match should work. |
| 134 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text, |
| 135 GURL("http://" + dictionary_domain))); |
| 136 } |
| 137 |
| 138 TEST_F(SdchManagerTest, CanAdvertiseDictionaryOverHTTP) { |
| 139 std::string dictionary_domain("x.y.z.google.com"); |
| 140 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
| 141 |
| 142 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text, |
| 143 GURL("http://" + dictionary_domain))); |
| 144 |
| 145 std::string dictionary_list; |
| 146 // HTTP target URL can advertise dictionary. |
| 147 sdch_manager_->GetAvailDictionaryList( |
| 148 GURL("http://" + dictionary_domain + "/test"), |
| 149 &dictionary_list); |
| 150 EXPECT_FALSE(dictionary_list.empty()); |
| 151 } |
| 152 |
| 153 TEST_F(SdchManagerTest, CanNotAdvertiseDictionaryOverHTTPS) { |
| 154 std::string dictionary_domain("x.y.z.google.com"); |
| 155 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
| 156 |
| 157 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text, |
| 158 GURL("http://" + dictionary_domain))); |
| 159 |
| 160 std::string dictionary_list; |
| 161 // HTTPS target URL should NOT advertise dictionary. |
| 162 sdch_manager_->GetAvailDictionaryList( |
| 163 GURL("https://" + dictionary_domain + "/test"), |
| 164 &dictionary_list); |
| 165 EXPECT_TRUE(dictionary_list.empty()); |
| 166 } |
| 167 |
| 168 TEST_F(SdchManagerTest, CanUseHTTPSDictionaryOverHTTPSIfEnabled) { |
| 169 std::string dictionary_domain("x.y.z.google.com"); |
| 170 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
| 171 |
| 172 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text, |
| 173 GURL("https://" + dictionary_domain))); |
| 174 |
| 175 GURL target_url("https://" + dictionary_domain + "/test"); |
| 176 std::string dictionary_list; |
| 177 // HTTPS target URL should advertise dictionary if secure scheme support is |
| 178 // enabled. |
| 179 sdch_manager_->EnableSecureSchemeSupport(true); |
| 180 sdch_manager_->GetAvailDictionaryList(target_url, &dictionary_list); |
| 181 EXPECT_FALSE(dictionary_list.empty()); |
| 182 |
| 183 // Dictionary should be available. |
| 184 SdchManager::Dictionary* dictionary = NULL; |
| 185 std::string client_hash; |
| 186 std::string server_hash; |
| 187 sdch_manager_->GenerateHash(dictionary_text, &client_hash, &server_hash); |
| 188 sdch_manager_->GetVcdiffDictionary(server_hash, target_url, &dictionary); |
| 189 EXPECT_TRUE(dictionary != NULL); |
| 190 } |
| 191 |
| 192 TEST_F(SdchManagerTest, CanNotUseHTTPDictionaryOverHTTPS) { |
| 193 std::string dictionary_domain("x.y.z.google.com"); |
| 194 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
| 195 |
| 196 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text, |
| 197 GURL("http://" + dictionary_domain))); |
| 198 |
| 199 GURL target_url("https://" + dictionary_domain + "/test"); |
| 200 std::string dictionary_list; |
| 201 // HTTPS target URL should not advertise dictionary acquired over HTTP even if |
| 202 // secure scheme support is enabled. |
| 203 sdch_manager_->EnableSecureSchemeSupport(true); |
| 204 sdch_manager_->GetAvailDictionaryList(target_url, &dictionary_list); |
| 205 EXPECT_TRUE(dictionary_list.empty()); |
| 206 |
| 207 SdchManager::Dictionary* dictionary = NULL; |
| 208 std::string client_hash; |
| 209 std::string server_hash; |
| 210 sdch_manager_->GenerateHash(dictionary_text, &client_hash, &server_hash); |
| 211 sdch_manager_->GetVcdiffDictionary(server_hash, target_url, &dictionary); |
| 212 EXPECT_TRUE(dictionary == NULL); |
| 213 } |
| 214 |
| 215 TEST_F(SdchManagerTest, FailToSetDomainMismatchDictionary) { |
| 216 std::string dictionary_domain("x.y.z.google.com"); |
| 217 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
| 218 |
| 219 // Fail the "domain match" requirement. |
| 220 EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text, |
| 221 GURL("http://y.z.google.com"))); |
| 222 } |
| 223 |
| 224 TEST_F(SdchManagerTest, FailToSetDotHostPrefixDomainDictionary) { |
| 225 std::string dictionary_domain("x.y.z.google.com"); |
| 226 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
| 227 |
| 228 // Fail the HD with D being the domain and H having a dot requirement. |
| 229 EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text, |
| 230 GURL("http://w.x.y.z.google.com"))); |
| 231 } |
| 232 |
| 233 TEST_F(SdchManagerTest, FailToSetRepeatPrefixWithDotDictionary) { |
| 234 // Make sure that a prefix that matches the domain postfix won't confuse |
| 235 // the validation checks. |
| 236 std::string dictionary_domain("www.google.com"); |
| 237 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
| 238 |
| 239 // Fail the HD with D being the domain and H having a dot requirement. |
| 240 EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text, |
| 241 GURL("http://www.google.com.www.google.com"))); |
| 242 } |
| 243 |
| 244 TEST_F(SdchManagerTest, CanSetLeadingDotDomainDictionary) { |
| 245 // Make sure that a prefix that matches the domain postfix won't confuse |
| 246 // the validation checks. |
| 247 std::string dictionary_domain(".google.com"); |
| 248 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
| 249 |
| 250 // Verify that a leading dot in the domain is acceptable, as long as the host |
| 251 // name does not contain any dots preceding the matched domain name. |
| 252 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text, |
| 253 GURL("http://www.google.com"))); |
| 254 } |
| 255 |
| 256 // Make sure the order of the tests is not helping us or confusing things. |
| 257 // See test CanSetExactMatchDictionary above for first try. |
| 258 TEST_F(SdchManagerTest, CanStillSetExactMatchDictionary) { |
| 259 std::string dictionary_domain("x.y.z.google.com"); |
| 260 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
| 261 |
| 262 // Perfect match should *STILL* work. |
| 263 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text, |
| 264 GURL("http://" + dictionary_domain))); |
| 265 } |
| 266 |
| 267 // Make sure the DOS protection precludes the addition of too many dictionaries. |
| 268 TEST_F(SdchManagerTest, TooManyDictionaries) { |
| 269 std::string dictionary_domain(".google.com"); |
| 270 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
| 271 |
| 272 size_t count = 0; |
| 273 while (count <= SdchManager::kMaxDictionaryCount + 1) { |
| 274 if (!sdch_manager_->AddSdchDictionary(dictionary_text, |
| 275 GURL("http://www.google.com"))) |
| 276 break; |
| 277 |
| 278 dictionary_text += " "; // Create dictionary with different SHA signature. |
| 279 ++count; |
| 280 } |
| 281 EXPECT_EQ(SdchManager::kMaxDictionaryCount, count); |
| 282 } |
| 283 |
| 284 TEST_F(SdchManagerTest, DictionaryNotTooLarge) { |
| 285 std::string dictionary_domain(".google.com"); |
| 286 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
| 287 |
| 288 dictionary_text.append( |
| 289 SdchManager::kMaxDictionarySize - dictionary_text.size(), ' '); |
| 290 EXPECT_TRUE(sdch_manager_->AddSdchDictionary(dictionary_text, |
| 291 GURL("http://" + dictionary_domain))); |
| 292 } |
| 293 |
| 294 TEST_F(SdchManagerTest, DictionaryTooLarge) { |
| 295 std::string dictionary_domain(".google.com"); |
| 296 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); |
| 297 |
| 298 dictionary_text.append( |
| 299 SdchManager::kMaxDictionarySize + 1 - dictionary_text.size(), ' '); |
| 300 EXPECT_FALSE(sdch_manager_->AddSdchDictionary(dictionary_text, |
| 301 GURL("http://" + dictionary_domain))); |
| 302 } |
| 303 |
| 304 TEST_F(SdchManagerTest, PathMatch) { |
| 305 bool (*PathMatch)(const std::string& path, const std::string& restriction) = |
| 306 SdchManager::Dictionary::PathMatch; |
| 307 // Perfect match is supported. |
| 308 EXPECT_TRUE(PathMatch("/search", "/search")); |
| 309 EXPECT_TRUE(PathMatch("/search/", "/search/")); |
| 310 |
| 311 // Prefix only works if last character of restriction is a slash, or first |
| 312 // character in path after a match is a slash. Validate each case separately. |
| 313 |
| 314 // Rely on the slash in the path (not at the end of the restriction). |
| 315 EXPECT_TRUE(PathMatch("/search/something", "/search")); |
| 316 EXPECT_TRUE(PathMatch("/search/s", "/search")); |
| 317 EXPECT_TRUE(PathMatch("/search/other", "/search")); |
| 318 EXPECT_TRUE(PathMatch("/search/something", "/search")); |
| 319 |
| 320 // Rely on the slash at the end of the restriction. |
| 321 EXPECT_TRUE(PathMatch("/search/something", "/search/")); |
| 322 EXPECT_TRUE(PathMatch("/search/s", "/search/")); |
| 323 EXPECT_TRUE(PathMatch("/search/other", "/search/")); |
| 324 EXPECT_TRUE(PathMatch("/search/something", "/search/")); |
| 325 |
| 326 // Make sure less that sufficient prefix match is false. |
| 327 EXPECT_FALSE(PathMatch("/sear", "/search")); |
| 328 EXPECT_FALSE(PathMatch("/", "/search")); |
| 329 EXPECT_FALSE(PathMatch(std::string(), "/search")); |
| 330 |
| 331 // Add examples with several levels of direcories in the restriction. |
| 332 EXPECT_FALSE(PathMatch("/search/something", "search/s")); |
| 333 EXPECT_FALSE(PathMatch("/search/", "/search/s")); |
| 334 |
| 335 // Make sure adding characters to path will also fail. |
| 336 EXPECT_FALSE(PathMatch("/searching", "/search/")); |
| 337 EXPECT_FALSE(PathMatch("/searching", "/search")); |
| 338 |
| 339 // Make sure we're case sensitive. |
| 340 EXPECT_FALSE(PathMatch("/ABC", "/abc")); |
| 341 EXPECT_FALSE(PathMatch("/abc", "/ABC")); |
| 342 } |
| 343 |
| 344 // The following are only applicable while we have a latency test in the code, |
| 345 // and can be removed when that functionality is stripped. |
| 346 TEST_F(SdchManagerTest, LatencyTestControls) { |
| 347 GURL url("http://www.google.com"); |
| 348 GURL url2("http://www.google2.com"); |
| 349 |
| 350 // First make sure we default to false. |
| 351 EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url)); |
| 352 EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url2)); |
| 353 |
| 354 // That we can set each to true. |
| 355 sdch_manager_->SetAllowLatencyExperiment(url, true); |
| 356 EXPECT_TRUE(sdch_manager_->AllowLatencyExperiment(url)); |
| 357 EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url2)); |
| 358 |
| 359 sdch_manager_->SetAllowLatencyExperiment(url2, true); |
| 360 EXPECT_TRUE(sdch_manager_->AllowLatencyExperiment(url)); |
| 361 EXPECT_TRUE(sdch_manager_->AllowLatencyExperiment(url2)); |
| 362 |
| 363 // And can reset them to false. |
| 364 sdch_manager_->SetAllowLatencyExperiment(url, false); |
| 365 EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url)); |
| 366 EXPECT_TRUE(sdch_manager_->AllowLatencyExperiment(url2)); |
| 367 |
| 368 sdch_manager_->SetAllowLatencyExperiment(url2, false); |
| 369 EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url)); |
| 370 EXPECT_FALSE(sdch_manager_->AllowLatencyExperiment(url2)); |
| 371 } |
| 372 |
| 373 } // namespace net |
| 374 |
OLD | NEW |