| 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 "base/strings/stringprintf.h" | |
| 12 #include "base/test/simple_test_clock.h" | |
| 13 #include "net/base/net_log.h" | |
| 14 #include "net/base/sdch_manager.h" | |
| 15 #include "net/base/sdch_observer.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 #include "url/gurl.h" | |
| 18 | |
| 19 namespace net { | |
| 20 | |
| 21 // Workaround for http://crbug.com/437794; remove when fixed. | |
| 22 #if !defined(OS_IOS) | |
| 23 | |
| 24 //------------------------------------------------------------------------------ | |
| 25 // Provide sample data and compression results with a sample VCDIFF dictionary. | |
| 26 // Note an SDCH dictionary has extra meta-data before the VCDIFF dictionary. | |
| 27 static const char kTestVcdiffDictionary[] = "DictionaryFor" | |
| 28 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n"; | |
| 29 | |
| 30 //------------------------------------------------------------------------------ | |
| 31 | |
| 32 class MockSdchObserver : public SdchObserver { | |
| 33 public: | |
| 34 MockSdchObserver() | |
| 35 : dictionary_used_notifications_(0), | |
| 36 get_dictionary_notifications_(0), | |
| 37 clear_dictionaries_notifications_(0) {} | |
| 38 | |
| 39 std::string last_server_hash() const { return last_server_hash_; } | |
| 40 int dictionary_used_notifications() const { | |
| 41 return dictionary_used_notifications_; | |
| 42 } | |
| 43 const GURL& last_dictionary_request_url() const { | |
| 44 return last_dictionary_request_url_; | |
| 45 } | |
| 46 const GURL& last_dictionary_url() const { return last_dictionary_url_; } | |
| 47 int get_dictionary_notifications() const { | |
| 48 return get_dictionary_notifications_; | |
| 49 } | |
| 50 | |
| 51 int clear_dictionary_notifications() const { | |
| 52 return clear_dictionaries_notifications_; | |
| 53 } | |
| 54 | |
| 55 // SdchObserver implementation | |
| 56 void OnDictionaryUsed(SdchManager* manager, | |
| 57 const std::string& server_hash) override { | |
| 58 last_server_hash_ = server_hash; | |
| 59 ++dictionary_used_notifications_; | |
| 60 } | |
| 61 | |
| 62 void OnGetDictionary(SdchManager* manager, | |
| 63 const GURL& request_url, | |
| 64 const GURL& dictionary_url) override { | |
| 65 ++get_dictionary_notifications_; | |
| 66 last_dictionary_request_url_ = request_url; | |
| 67 last_dictionary_url_ = dictionary_url; | |
| 68 } | |
| 69 void OnClearDictionaries(SdchManager* manager) override { | |
| 70 ++clear_dictionaries_notifications_; | |
| 71 } | |
| 72 | |
| 73 private: | |
| 74 int dictionary_used_notifications_; | |
| 75 int get_dictionary_notifications_; | |
| 76 int clear_dictionaries_notifications_; | |
| 77 | |
| 78 std::string last_server_hash_; | |
| 79 GURL last_dictionary_request_url_; | |
| 80 GURL last_dictionary_url_; | |
| 81 | |
| 82 DISALLOW_COPY_AND_ASSIGN(MockSdchObserver); | |
| 83 }; | |
| 84 | |
| 85 class SdchManagerTest : public testing::Test { | |
| 86 protected: | |
| 87 SdchManagerTest() | |
| 88 : sdch_manager_(new SdchManager), | |
| 89 default_support_(false), | |
| 90 default_https_support_(false) { | |
| 91 default_support_ = sdch_manager_->sdch_enabled(); | |
| 92 default_https_support_ = sdch_manager_->secure_scheme_supported(); | |
| 93 } | |
| 94 | |
| 95 ~SdchManagerTest() override {} | |
| 96 | |
| 97 SdchManager* sdch_manager() { return sdch_manager_.get(); } | |
| 98 | |
| 99 // Reset globals back to default state. | |
| 100 void TearDown() override { | |
| 101 SdchManager::EnableSdchSupport(default_support_); | |
| 102 SdchManager::EnableSecureSchemeSupport(default_https_support_); | |
| 103 } | |
| 104 | |
| 105 // Attempt to add a dictionary to the manager and probe for success or | |
| 106 // failure. | |
| 107 bool AddSdchDictionary(const std::string& dictionary_text, | |
| 108 const GURL& gurl) { | |
| 109 return sdch_manager_->AddSdchDictionary(dictionary_text, gurl, nullptr) == | |
| 110 SDCH_OK; | |
| 111 } | |
| 112 | |
| 113 private: | |
| 114 scoped_ptr<SdchManager> sdch_manager_; | |
| 115 bool default_support_; | |
| 116 bool default_https_support_; | |
| 117 }; | |
| 118 | |
| 119 static std::string NewSdchDictionary(const std::string& domain) { | |
| 120 std::string dictionary; | |
| 121 if (!domain.empty()) { | |
| 122 dictionary.append("Domain: "); | |
| 123 dictionary.append(domain); | |
| 124 dictionary.append("\n"); | |
| 125 } | |
| 126 dictionary.append("\n"); | |
| 127 dictionary.append(kTestVcdiffDictionary, sizeof(kTestVcdiffDictionary) - 1); | |
| 128 return dictionary; | |
| 129 } | |
| 130 | |
| 131 TEST_F(SdchManagerTest, DomainSupported) { | |
| 132 GURL google_url("http://www.google.com"); | |
| 133 | |
| 134 SdchManager::EnableSdchSupport(false); | |
| 135 EXPECT_EQ(SDCH_DISABLED, sdch_manager()->IsInSupportedDomain(google_url)); | |
| 136 SdchManager::EnableSdchSupport(true); | |
| 137 EXPECT_EQ(SDCH_OK, sdch_manager()->IsInSupportedDomain(google_url)); | |
| 138 } | |
| 139 | |
| 140 TEST_F(SdchManagerTest, DomainBlacklisting) { | |
| 141 GURL test_url("http://www.test.com"); | |
| 142 GURL google_url("http://www.google.com"); | |
| 143 | |
| 144 sdch_manager()->BlacklistDomain(test_url, SDCH_OK); | |
| 145 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET, | |
| 146 sdch_manager()->IsInSupportedDomain(test_url)); | |
| 147 EXPECT_EQ(SDCH_OK, sdch_manager()->IsInSupportedDomain(google_url)); | |
| 148 | |
| 149 sdch_manager()->BlacklistDomain(google_url, SDCH_OK); | |
| 150 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET, | |
| 151 sdch_manager()->IsInSupportedDomain(google_url)); | |
| 152 } | |
| 153 | |
| 154 TEST_F(SdchManagerTest, DomainBlacklistingCaseSensitivity) { | |
| 155 GURL test_url("http://www.TesT.com"); | |
| 156 GURL test2_url("http://www.tEst.com"); | |
| 157 | |
| 158 EXPECT_EQ(SDCH_OK, sdch_manager()->IsInSupportedDomain(test_url)); | |
| 159 EXPECT_EQ(SDCH_OK, sdch_manager()->IsInSupportedDomain(test2_url)); | |
| 160 sdch_manager()->BlacklistDomain(test_url, SDCH_OK); | |
| 161 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET, | |
| 162 sdch_manager()->IsInSupportedDomain(test2_url)); | |
| 163 } | |
| 164 | |
| 165 TEST_F(SdchManagerTest, BlacklistingReset) { | |
| 166 GURL gurl("http://mytest.DoMain.com"); | |
| 167 std::string domain(gurl.host()); | |
| 168 | |
| 169 sdch_manager()->ClearBlacklistings(); | |
| 170 EXPECT_EQ(sdch_manager()->BlackListDomainCount(domain), 0); | |
| 171 EXPECT_EQ(sdch_manager()->BlacklistDomainExponential(domain), 0); | |
| 172 EXPECT_EQ(SDCH_OK, sdch_manager()->IsInSupportedDomain(gurl)); | |
| 173 } | |
| 174 | |
| 175 TEST_F(SdchManagerTest, BlacklistingSingleBlacklist) { | |
| 176 GURL gurl("http://mytest.DoMain.com"); | |
| 177 std::string domain(gurl.host()); | |
| 178 sdch_manager()->ClearBlacklistings(); | |
| 179 | |
| 180 sdch_manager()->BlacklistDomain(gurl, SDCH_OK); | |
| 181 EXPECT_EQ(sdch_manager()->BlackListDomainCount(domain), 1); | |
| 182 EXPECT_EQ(sdch_manager()->BlacklistDomainExponential(domain), 1); | |
| 183 | |
| 184 // Check that any domain lookup reduces the blacklist counter. | |
| 185 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET, | |
| 186 sdch_manager()->IsInSupportedDomain(gurl)); | |
| 187 EXPECT_EQ(sdch_manager()->BlackListDomainCount(domain), 0); | |
| 188 EXPECT_EQ(SDCH_OK, sdch_manager()->IsInSupportedDomain(gurl)); | |
| 189 } | |
| 190 | |
| 191 TEST_F(SdchManagerTest, BlacklistingExponential) { | |
| 192 GURL gurl("http://mytest.DoMain.com"); | |
| 193 std::string domain(gurl.host()); | |
| 194 sdch_manager()->ClearBlacklistings(); | |
| 195 | |
| 196 int exponential = 1; | |
| 197 for (int i = 1; i < 100; ++i) { | |
| 198 sdch_manager()->BlacklistDomain(gurl, SDCH_OK); | |
| 199 EXPECT_EQ(sdch_manager()->BlacklistDomainExponential(domain), exponential); | |
| 200 | |
| 201 EXPECT_EQ(sdch_manager()->BlackListDomainCount(domain), exponential); | |
| 202 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET, | |
| 203 sdch_manager()->IsInSupportedDomain(gurl)); | |
| 204 EXPECT_EQ(sdch_manager()->BlackListDomainCount(domain), exponential - 1); | |
| 205 | |
| 206 // Simulate a large number of domain checks (which eventually remove the | |
| 207 // blacklisting). | |
| 208 sdch_manager()->ClearDomainBlacklisting(domain); | |
| 209 EXPECT_EQ(sdch_manager()->BlackListDomainCount(domain), 0); | |
| 210 EXPECT_EQ(SDCH_OK, sdch_manager()->IsInSupportedDomain(gurl)); | |
| 211 | |
| 212 // Predict what exponential backoff will be. | |
| 213 exponential = 1 + 2 * exponential; | |
| 214 if (exponential < 0) | |
| 215 exponential = INT_MAX; // We don't wrap. | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 TEST_F(SdchManagerTest, CanSetExactMatchDictionary) { | |
| 220 std::string dictionary_domain("x.y.z.google.com"); | |
| 221 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
| 222 | |
| 223 // Perfect match should work. | |
| 224 EXPECT_TRUE(AddSdchDictionary(dictionary_text, | |
| 225 GURL("http://" + dictionary_domain))); | |
| 226 } | |
| 227 | |
| 228 TEST_F(SdchManagerTest, CanAdvertiseDictionaryOverHTTP) { | |
| 229 std::string dictionary_domain("x.y.z.google.com"); | |
| 230 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
| 231 | |
| 232 EXPECT_TRUE(AddSdchDictionary(dictionary_text, | |
| 233 GURL("http://" + dictionary_domain))); | |
| 234 | |
| 235 // HTTP target URL can advertise dictionary. | |
| 236 EXPECT_TRUE(sdch_manager()->GetDictionarySet( | |
| 237 GURL("http://" + dictionary_domain + "/test"))); | |
| 238 } | |
| 239 | |
| 240 TEST_F(SdchManagerTest, CanNotAdvertiseDictionaryOverHTTPS) { | |
| 241 std::string dictionary_domain("x.y.z.google.com"); | |
| 242 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
| 243 | |
| 244 EXPECT_TRUE(AddSdchDictionary(dictionary_text, | |
| 245 GURL("http://" + dictionary_domain))); | |
| 246 | |
| 247 // HTTPS target URL should NOT advertise dictionary. | |
| 248 EXPECT_FALSE(sdch_manager()->GetDictionarySet( | |
| 249 GURL("https://" + dictionary_domain + "/test"))); | |
| 250 } | |
| 251 | |
| 252 TEST_F(SdchManagerTest, CanUseHTTPSDictionaryOverHTTPSIfEnabled) { | |
| 253 std::string dictionary_domain("x.y.z.google.com"); | |
| 254 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
| 255 | |
| 256 SdchManager::EnableSecureSchemeSupport(false); | |
| 257 EXPECT_FALSE(AddSdchDictionary(dictionary_text, | |
| 258 GURL("https://" + dictionary_domain))); | |
| 259 SdchManager::EnableSecureSchemeSupport(true); | |
| 260 EXPECT_TRUE(AddSdchDictionary(dictionary_text, | |
| 261 GURL("https://" + dictionary_domain))); | |
| 262 | |
| 263 GURL target_url("https://" + dictionary_domain + "/test"); | |
| 264 // HTTPS target URL should advertise dictionary if secure scheme support is | |
| 265 // enabled. | |
| 266 EXPECT_TRUE(sdch_manager()->GetDictionarySet(target_url)); | |
| 267 | |
| 268 // Dictionary should be available. | |
| 269 std::string client_hash; | |
| 270 std::string server_hash; | |
| 271 sdch_manager()->GenerateHash(dictionary_text, &client_hash, &server_hash); | |
| 272 SdchProblemCode problem_code; | |
| 273 scoped_ptr<SdchManager::DictionarySet> dict_set( | |
| 274 sdch_manager()->GetDictionarySetByHash( | |
| 275 target_url, server_hash, &problem_code)); | |
| 276 EXPECT_EQ(SDCH_OK, problem_code); | |
| 277 EXPECT_TRUE(dict_set.get()); | |
| 278 EXPECT_TRUE(dict_set->GetDictionary(server_hash)); | |
| 279 } | |
| 280 | |
| 281 TEST_F(SdchManagerTest, CanNotUseHTTPDictionaryOverHTTPS) { | |
| 282 std::string dictionary_domain("x.y.z.google.com"); | |
| 283 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
| 284 | |
| 285 EXPECT_TRUE(AddSdchDictionary(dictionary_text, | |
| 286 GURL("http://" + dictionary_domain))); | |
| 287 | |
| 288 GURL target_url("https://" + dictionary_domain + "/test"); | |
| 289 // HTTPS target URL should not advertise dictionary acquired over HTTP even if | |
| 290 // secure scheme support is enabled. | |
| 291 SdchManager::EnableSecureSchemeSupport(true); | |
| 292 EXPECT_FALSE(sdch_manager()->GetDictionarySet(target_url)); | |
| 293 | |
| 294 std::string client_hash; | |
| 295 std::string server_hash; | |
| 296 sdch_manager()->GenerateHash(dictionary_text, &client_hash, &server_hash); | |
| 297 SdchProblemCode problem_code; | |
| 298 scoped_ptr<SdchManager::DictionarySet> dict_set( | |
| 299 sdch_manager()->GetDictionarySetByHash( | |
| 300 target_url, server_hash, &problem_code)); | |
| 301 EXPECT_FALSE(dict_set.get()); | |
| 302 EXPECT_EQ(SDCH_DICTIONARY_FOUND_HAS_WRONG_SCHEME, problem_code); | |
| 303 } | |
| 304 | |
| 305 TEST_F(SdchManagerTest, CanNotUseHTTPSDictionaryOverHTTP) { | |
| 306 std::string dictionary_domain("x.y.z.google.com"); | |
| 307 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
| 308 | |
| 309 SdchManager::EnableSecureSchemeSupport(true); | |
| 310 EXPECT_TRUE(AddSdchDictionary(dictionary_text, | |
| 311 GURL("https://" + dictionary_domain))); | |
| 312 | |
| 313 GURL target_url("http://" + dictionary_domain + "/test"); | |
| 314 // HTTP target URL should not advertise dictionary acquired over HTTPS even if | |
| 315 // secure scheme support is enabled. | |
| 316 EXPECT_FALSE(sdch_manager()->GetDictionarySet(target_url)); | |
| 317 | |
| 318 std::string client_hash; | |
| 319 std::string server_hash; | |
| 320 sdch_manager()->GenerateHash(dictionary_text, &client_hash, &server_hash); | |
| 321 SdchProblemCode problem_code; | |
| 322 scoped_ptr<SdchManager::DictionarySet> dict_set( | |
| 323 sdch_manager()->GetDictionarySetByHash( | |
| 324 target_url, server_hash, &problem_code)); | |
| 325 EXPECT_FALSE(dict_set.get()); | |
| 326 EXPECT_EQ(SDCH_DICTIONARY_FOUND_HAS_WRONG_SCHEME, problem_code); | |
| 327 } | |
| 328 | |
| 329 TEST_F(SdchManagerTest, FailToSetDomainMismatchDictionary) { | |
| 330 std::string dictionary_domain("x.y.z.google.com"); | |
| 331 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
| 332 | |
| 333 // Fail the "domain match" requirement. | |
| 334 EXPECT_FALSE(AddSdchDictionary(dictionary_text, | |
| 335 GURL("http://y.z.google.com"))); | |
| 336 } | |
| 337 | |
| 338 TEST_F(SdchManagerTest, FailToSetDotHostPrefixDomainDictionary) { | |
| 339 std::string dictionary_domain("x.y.z.google.com"); | |
| 340 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
| 341 | |
| 342 // Fail the HD with D being the domain and H having a dot requirement. | |
| 343 EXPECT_FALSE(AddSdchDictionary(dictionary_text, | |
| 344 GURL("http://w.x.y.z.google.com"))); | |
| 345 } | |
| 346 | |
| 347 TEST_F(SdchManagerTest, FailToSetDotHostPrefixDomainDictionaryTrailingDot) { | |
| 348 std::string dictionary_domain("x.y.z.google.com"); | |
| 349 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
| 350 | |
| 351 // Fail the HD with D being the domain and H having a dot requirement. | |
| 352 EXPECT_FALSE(AddSdchDictionary(dictionary_text, | |
| 353 GURL("http://w.x.y.z.google.com."))); | |
| 354 } | |
| 355 | |
| 356 TEST_F(SdchManagerTest, FailToSetRepeatPrefixWithDotDictionary) { | |
| 357 // Make sure that a prefix that matches the domain postfix won't confuse | |
| 358 // the validation checks. | |
| 359 std::string dictionary_domain("www.google.com"); | |
| 360 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
| 361 | |
| 362 // Fail the HD with D being the domain and H having a dot requirement. | |
| 363 EXPECT_FALSE(AddSdchDictionary(dictionary_text, | |
| 364 GURL("http://www.google.com.www.google.com"))); | |
| 365 } | |
| 366 | |
| 367 TEST_F(SdchManagerTest, CanSetLeadingDotDomainDictionary) { | |
| 368 // Make sure that a prefix that matches the domain postfix won't confuse | |
| 369 // the validation checks. | |
| 370 std::string dictionary_domain(".google.com"); | |
| 371 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
| 372 | |
| 373 // Verify that a leading dot in the domain is acceptable, as long as the host | |
| 374 // name does not contain any dots preceding the matched domain name. | |
| 375 EXPECT_TRUE(AddSdchDictionary(dictionary_text, GURL("http://www.google.com")))
; | |
| 376 } | |
| 377 | |
| 378 TEST_F(SdchManagerTest, | |
| 379 CanSetLeadingDotDomainDictionaryFromURLWithTrailingDot) { | |
| 380 // Make sure that a prefix that matches the domain postfix won't confuse | |
| 381 // the validation checks. | |
| 382 std::string dictionary_domain(".google.com"); | |
| 383 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
| 384 | |
| 385 // Verify that a leading dot in the domain is acceptable, as long as the host | |
| 386 // name does not contain any dots preceding the matched domain name. | |
| 387 EXPECT_TRUE(AddSdchDictionary(dictionary_text, | |
| 388 GURL("http://www.google.com."))); | |
| 389 } | |
| 390 | |
| 391 TEST_F(SdchManagerTest, CannotSetLeadingDotDomainDictionary) { | |
| 392 // Make sure that a prefix that matches the domain postfix won't confuse | |
| 393 // the validation checks. | |
| 394 std::string dictionary_domain(".google.com"); | |
| 395 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
| 396 | |
| 397 // Verify that a leading dot in the domain does not affect the name containing | |
| 398 // dots failure. | |
| 399 EXPECT_FALSE(AddSdchDictionary(dictionary_text, | |
| 400 GURL("http://www.subdomain.google.com"))); | |
| 401 } | |
| 402 | |
| 403 TEST_F(SdchManagerTest, CannotSetLeadingDotDomainDictionaryTrailingDot) { | |
| 404 // Make sure that a prefix that matches the domain postfix won't confuse | |
| 405 // the validation checks. | |
| 406 std::string dictionary_domain(".google.com"); | |
| 407 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
| 408 | |
| 409 // Verify that a trailing period in the URL doesn't affect the check. | |
| 410 EXPECT_FALSE(AddSdchDictionary(dictionary_text, | |
| 411 GURL("http://www.subdomain.google.com."))); | |
| 412 } | |
| 413 | |
| 414 // Make sure the order of the tests is not helping us or confusing things. | |
| 415 // See test CanSetExactMatchDictionary above for first try. | |
| 416 TEST_F(SdchManagerTest, CanStillSetExactMatchDictionary) { | |
| 417 std::string dictionary_domain("x.y.z.google.com"); | |
| 418 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
| 419 | |
| 420 // Perfect match should *STILL* work. | |
| 421 EXPECT_TRUE(AddSdchDictionary(dictionary_text, | |
| 422 GURL("http://" + dictionary_domain))); | |
| 423 } | |
| 424 | |
| 425 TEST_F(SdchManagerTest, PathMatch) { | |
| 426 bool (*PathMatch)(const std::string& path, const std::string& restriction) = | |
| 427 SdchManager::Dictionary::PathMatch; | |
| 428 // Perfect match is supported. | |
| 429 EXPECT_TRUE(PathMatch("/search", "/search")); | |
| 430 EXPECT_TRUE(PathMatch("/search/", "/search/")); | |
| 431 | |
| 432 // Prefix only works if last character of restriction is a slash, or first | |
| 433 // character in path after a match is a slash. Validate each case separately. | |
| 434 | |
| 435 // Rely on the slash in the path (not at the end of the restriction). | |
| 436 EXPECT_TRUE(PathMatch("/search/something", "/search")); | |
| 437 EXPECT_TRUE(PathMatch("/search/s", "/search")); | |
| 438 EXPECT_TRUE(PathMatch("/search/other", "/search")); | |
| 439 EXPECT_TRUE(PathMatch("/search/something", "/search")); | |
| 440 | |
| 441 // Rely on the slash at the end of the restriction. | |
| 442 EXPECT_TRUE(PathMatch("/search/something", "/search/")); | |
| 443 EXPECT_TRUE(PathMatch("/search/s", "/search/")); | |
| 444 EXPECT_TRUE(PathMatch("/search/other", "/search/")); | |
| 445 EXPECT_TRUE(PathMatch("/search/something", "/search/")); | |
| 446 | |
| 447 // Make sure less that sufficient prefix match is false. | |
| 448 EXPECT_FALSE(PathMatch("/sear", "/search")); | |
| 449 EXPECT_FALSE(PathMatch("/", "/search")); | |
| 450 EXPECT_FALSE(PathMatch(std::string(), "/search")); | |
| 451 | |
| 452 // Add examples with several levels of direcories in the restriction. | |
| 453 EXPECT_FALSE(PathMatch("/search/something", "search/s")); | |
| 454 EXPECT_FALSE(PathMatch("/search/", "/search/s")); | |
| 455 | |
| 456 // Make sure adding characters to path will also fail. | |
| 457 EXPECT_FALSE(PathMatch("/searching", "/search/")); | |
| 458 EXPECT_FALSE(PathMatch("/searching", "/search")); | |
| 459 | |
| 460 // Make sure we're case sensitive. | |
| 461 EXPECT_FALSE(PathMatch("/ABC", "/abc")); | |
| 462 EXPECT_FALSE(PathMatch("/abc", "/ABC")); | |
| 463 } | |
| 464 | |
| 465 // The following are only applicable while we have a latency test in the code, | |
| 466 // and can be removed when that functionality is stripped. | |
| 467 TEST_F(SdchManagerTest, LatencyTestControls) { | |
| 468 GURL url("http://www.google.com"); | |
| 469 GURL url2("http://www.google2.com"); | |
| 470 | |
| 471 // First make sure we default to false. | |
| 472 EXPECT_FALSE(sdch_manager()->AllowLatencyExperiment(url)); | |
| 473 EXPECT_FALSE(sdch_manager()->AllowLatencyExperiment(url2)); | |
| 474 | |
| 475 // That we can set each to true. | |
| 476 sdch_manager()->SetAllowLatencyExperiment(url, true); | |
| 477 EXPECT_TRUE(sdch_manager()->AllowLatencyExperiment(url)); | |
| 478 EXPECT_FALSE(sdch_manager()->AllowLatencyExperiment(url2)); | |
| 479 | |
| 480 sdch_manager()->SetAllowLatencyExperiment(url2, true); | |
| 481 EXPECT_TRUE(sdch_manager()->AllowLatencyExperiment(url)); | |
| 482 EXPECT_TRUE(sdch_manager()->AllowLatencyExperiment(url2)); | |
| 483 | |
| 484 // And can reset them to false. | |
| 485 sdch_manager()->SetAllowLatencyExperiment(url, false); | |
| 486 EXPECT_FALSE(sdch_manager()->AllowLatencyExperiment(url)); | |
| 487 EXPECT_TRUE(sdch_manager()->AllowLatencyExperiment(url2)); | |
| 488 | |
| 489 sdch_manager()->SetAllowLatencyExperiment(url2, false); | |
| 490 EXPECT_FALSE(sdch_manager()->AllowLatencyExperiment(url)); | |
| 491 EXPECT_FALSE(sdch_manager()->AllowLatencyExperiment(url2)); | |
| 492 } | |
| 493 | |
| 494 TEST_F(SdchManagerTest, CanUseMultipleManagers) { | |
| 495 SdchManager second_manager; | |
| 496 | |
| 497 std::string dictionary_domain_1("x.y.z.google.com"); | |
| 498 std::string dictionary_domain_2("x.y.z.chromium.org"); | |
| 499 | |
| 500 std::string dictionary_text_1(NewSdchDictionary(dictionary_domain_1)); | |
| 501 std::string dictionary_text_2(NewSdchDictionary(dictionary_domain_2)); | |
| 502 | |
| 503 std::string tmp_hash; | |
| 504 std::string server_hash_1; | |
| 505 std::string server_hash_2; | |
| 506 | |
| 507 SdchManager::GenerateHash(dictionary_text_1, &tmp_hash, &server_hash_1); | |
| 508 SdchManager::GenerateHash(dictionary_text_2, &tmp_hash, &server_hash_2); | |
| 509 | |
| 510 // Confirm that if you add directories to one manager, you | |
| 511 // can't get them from the other. | |
| 512 EXPECT_TRUE(AddSdchDictionary(dictionary_text_1, | |
| 513 GURL("http://" + dictionary_domain_1))); | |
| 514 scoped_ptr<SdchManager::DictionarySet> dict_set; | |
| 515 | |
| 516 SdchProblemCode problem_code; | |
| 517 dict_set = sdch_manager()->GetDictionarySetByHash( | |
| 518 GURL("http://" + dictionary_domain_1 + "/random_url"), | |
| 519 server_hash_1, &problem_code); | |
| 520 EXPECT_TRUE(dict_set); | |
| 521 EXPECT_TRUE(dict_set->GetDictionary(server_hash_1)); | |
| 522 EXPECT_EQ(SDCH_OK, problem_code); | |
| 523 | |
| 524 second_manager.AddSdchDictionary( | |
| 525 dictionary_text_2, GURL("http://" + dictionary_domain_2), nullptr); | |
| 526 dict_set = second_manager.GetDictionarySetByHash( | |
| 527 GURL("http://" + dictionary_domain_2 + "/random_url"), | |
| 528 server_hash_2, &problem_code); | |
| 529 EXPECT_TRUE(dict_set); | |
| 530 EXPECT_TRUE(dict_set->GetDictionary(server_hash_2)); | |
| 531 EXPECT_EQ(SDCH_OK, problem_code); | |
| 532 | |
| 533 dict_set = sdch_manager()->GetDictionarySetByHash( | |
| 534 GURL("http://" + dictionary_domain_2 + "/random_url"), | |
| 535 server_hash_2, &problem_code); | |
| 536 EXPECT_FALSE(dict_set); | |
| 537 EXPECT_EQ(SDCH_DICTIONARY_HASH_NOT_FOUND, problem_code); | |
| 538 | |
| 539 dict_set = second_manager.GetDictionarySetByHash( | |
| 540 GURL("http://" + dictionary_domain_1 + "/random_url"), | |
| 541 server_hash_1, &problem_code); | |
| 542 EXPECT_FALSE(dict_set); | |
| 543 EXPECT_EQ(SDCH_DICTIONARY_HASH_NOT_FOUND, problem_code); | |
| 544 } | |
| 545 | |
| 546 TEST_F(SdchManagerTest, HttpsCorrectlySupported) { | |
| 547 GURL url("http://www.google.com"); | |
| 548 GURL secure_url("https://www.google.com"); | |
| 549 | |
| 550 bool expect_https_support = true; | |
| 551 | |
| 552 SdchProblemCode expected_code = | |
| 553 expect_https_support ? SDCH_OK : SDCH_SECURE_SCHEME_NOT_SUPPORTED; | |
| 554 | |
| 555 EXPECT_EQ(SDCH_OK, sdch_manager()->IsInSupportedDomain(url)); | |
| 556 EXPECT_EQ(expected_code, sdch_manager()->IsInSupportedDomain(secure_url)); | |
| 557 | |
| 558 SdchManager::EnableSecureSchemeSupport(!expect_https_support); | |
| 559 EXPECT_EQ(SDCH_OK, sdch_manager()->IsInSupportedDomain(url)); | |
| 560 EXPECT_NE(expected_code, sdch_manager()->IsInSupportedDomain(secure_url)); | |
| 561 } | |
| 562 | |
| 563 TEST_F(SdchManagerTest, ClearDictionaryData) { | |
| 564 std::string dictionary_domain("x.y.z.google.com"); | |
| 565 GURL blacklist_url("http://bad.chromium.org"); | |
| 566 | |
| 567 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
| 568 std::string tmp_hash; | |
| 569 std::string server_hash; | |
| 570 | |
| 571 SdchManager::GenerateHash(dictionary_text, &tmp_hash, &server_hash); | |
| 572 | |
| 573 EXPECT_TRUE(AddSdchDictionary(dictionary_text, | |
| 574 GURL("http://" + dictionary_domain))); | |
| 575 | |
| 576 scoped_ptr<SdchManager::DictionarySet> dict_set; | |
| 577 | |
| 578 SdchProblemCode problem_code; | |
| 579 dict_set = sdch_manager()->GetDictionarySetByHash( | |
| 580 GURL("http://" + dictionary_domain + "/random_url"), | |
| 581 server_hash, &problem_code); | |
| 582 EXPECT_TRUE(dict_set); | |
| 583 EXPECT_TRUE(dict_set->GetDictionary(server_hash)); | |
| 584 EXPECT_EQ(SDCH_OK, problem_code); | |
| 585 | |
| 586 sdch_manager()->BlacklistDomain(GURL(blacklist_url), SDCH_OK); | |
| 587 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET, | |
| 588 sdch_manager()->IsInSupportedDomain(blacklist_url)); | |
| 589 | |
| 590 sdch_manager()->ClearData(); | |
| 591 | |
| 592 dict_set = sdch_manager()->GetDictionarySetByHash( | |
| 593 GURL("http://" + dictionary_domain + "/random_url"), | |
| 594 server_hash, &problem_code); | |
| 595 EXPECT_FALSE(dict_set); | |
| 596 EXPECT_EQ(SDCH_DICTIONARY_HASH_NOT_FOUND, problem_code); | |
| 597 EXPECT_EQ(SDCH_OK, sdch_manager()->IsInSupportedDomain(blacklist_url)); | |
| 598 } | |
| 599 | |
| 600 TEST_F(SdchManagerTest, GetDictionaryNotification) { | |
| 601 GURL test_request_gurl(GURL("http://www.example.com/data")); | |
| 602 GURL test_dictionary_gurl(GURL("http://www.example.com/dict")); | |
| 603 MockSdchObserver observer; | |
| 604 sdch_manager()->AddObserver(&observer); | |
| 605 | |
| 606 EXPECT_EQ(0, observer.get_dictionary_notifications()); | |
| 607 sdch_manager()->OnGetDictionary(test_request_gurl, test_dictionary_gurl); | |
| 608 EXPECT_EQ(1, observer.get_dictionary_notifications()); | |
| 609 EXPECT_EQ(test_request_gurl, observer.last_dictionary_request_url()); | |
| 610 EXPECT_EQ(test_dictionary_gurl, observer.last_dictionary_url()); | |
| 611 | |
| 612 sdch_manager()->RemoveObserver(&observer); | |
| 613 sdch_manager()->OnGetDictionary(test_request_gurl, test_dictionary_gurl); | |
| 614 EXPECT_EQ(1, observer.get_dictionary_notifications()); | |
| 615 EXPECT_EQ(test_request_gurl, observer.last_dictionary_request_url()); | |
| 616 EXPECT_EQ(test_dictionary_gurl, observer.last_dictionary_url()); | |
| 617 } | |
| 618 | |
| 619 TEST_F(SdchManagerTest, ExpirationCheckedProperly) { | |
| 620 // Create an SDCH dictionary with an expiration time in the past. | |
| 621 std::string dictionary_domain("x.y.z.google.com"); | |
| 622 std::string dictionary_text(base::StringPrintf( | |
| 623 "Domain: %s\nMax-age: 0\n\n", dictionary_domain.c_str())); | |
| 624 dictionary_text.append( | |
| 625 kTestVcdiffDictionary, sizeof(kTestVcdiffDictionary) - 1); | |
| 626 std::string client_hash; | |
| 627 std::string server_hash; | |
| 628 SdchManager::GenerateHash(dictionary_text, &client_hash, &server_hash); | |
| 629 GURL target_gurl("http://" + dictionary_domain); | |
| 630 AddSdchDictionary(dictionary_text, target_gurl); | |
| 631 | |
| 632 // It should be visible if looked up by hash whether expired or not. | |
| 633 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); | |
| 634 clock->SetNow(base::Time::Now()); | |
| 635 clock->Advance(base::TimeDelta::FromMinutes(5)); | |
| 636 SdchProblemCode problem_code; | |
| 637 scoped_ptr<SdchManager::DictionarySet> hash_set( | |
| 638 sdch_manager()->GetDictionarySetByHash( | |
| 639 target_gurl, server_hash, &problem_code).Pass()); | |
| 640 ASSERT_TRUE(hash_set); | |
| 641 ASSERT_EQ(SDCH_OK, problem_code); | |
| 642 const_cast<SdchManager::Dictionary*>( | |
| 643 hash_set->GetDictionary(server_hash))->SetClockForTesting( | |
| 644 clock.Pass()); | |
| 645 | |
| 646 // Make sure it's not visible for advertisement, but is visible | |
| 647 // if looked up by hash. | |
| 648 EXPECT_FALSE(sdch_manager()->GetDictionarySet(target_gurl)); | |
| 649 EXPECT_TRUE(sdch_manager()->GetDictionarySetByHash( | |
| 650 target_gurl, server_hash, &problem_code)); | |
| 651 EXPECT_EQ(SDCH_OK, problem_code); | |
| 652 } | |
| 653 | |
| 654 TEST_F(SdchManagerTest, SdchOnByDefault) { | |
| 655 GURL google_url("http://www.google.com"); | |
| 656 scoped_ptr<SdchManager> sdch_manager(new SdchManager); | |
| 657 | |
| 658 EXPECT_EQ(SDCH_OK, sdch_manager->IsInSupportedDomain(google_url)); | |
| 659 SdchManager::EnableSdchSupport(false); | |
| 660 EXPECT_EQ(SDCH_DISABLED, sdch_manager->IsInSupportedDomain(google_url)); | |
| 661 } | |
| 662 | |
| 663 // Confirm dispatch of notification. | |
| 664 TEST_F(SdchManagerTest, SdchDictionaryUsed) { | |
| 665 MockSdchObserver observer; | |
| 666 sdch_manager()->AddObserver(&observer); | |
| 667 | |
| 668 EXPECT_EQ(0, observer.dictionary_used_notifications()); | |
| 669 sdch_manager()->OnDictionaryUsed("xyzzy"); | |
| 670 EXPECT_EQ(1, observer.dictionary_used_notifications()); | |
| 671 EXPECT_EQ("xyzzy", observer.last_server_hash()); | |
| 672 | |
| 673 std::string dictionary_domain("x.y.z.google.com"); | |
| 674 GURL target_gurl("http://" + dictionary_domain); | |
| 675 std::string dictionary_text(NewSdchDictionary(dictionary_domain)); | |
| 676 std::string client_hash; | |
| 677 std::string server_hash; | |
| 678 SdchManager::GenerateHash(dictionary_text, &client_hash, &server_hash); | |
| 679 EXPECT_TRUE(AddSdchDictionary(dictionary_text, target_gurl)); | |
| 680 EXPECT_EQ("xyzzy", observer.last_server_hash()); | |
| 681 EXPECT_EQ(1, observer.dictionary_used_notifications()); | |
| 682 | |
| 683 EXPECT_TRUE(sdch_manager()->GetDictionarySet(target_gurl)); | |
| 684 EXPECT_EQ("xyzzy", observer.last_server_hash()); | |
| 685 EXPECT_EQ(1, observer.dictionary_used_notifications()); | |
| 686 | |
| 687 sdch_manager()->RemoveObserver(&observer); | |
| 688 EXPECT_EQ(1, observer.dictionary_used_notifications()); | |
| 689 EXPECT_EQ("xyzzy", observer.last_server_hash()); | |
| 690 sdch_manager()->OnDictionaryUsed("plugh"); | |
| 691 EXPECT_EQ(1, observer.dictionary_used_notifications()); | |
| 692 EXPECT_EQ("xyzzy", observer.last_server_hash()); | |
| 693 } | |
| 694 | |
| 695 #else | |
| 696 | |
| 697 TEST(SdchManagerTest, SdchOffByDefault) { | |
| 698 GURL google_url("http://www.google.com"); | |
| 699 scoped_ptr<SdchManager> sdch_manager(new SdchManager); | |
| 700 | |
| 701 EXPECT_EQ(SDCH_DISABLED, sdch_manager->IsInSupportedDomain(google_url)); | |
| 702 SdchManager::EnableSdchSupport(true); | |
| 703 EXPECT_EQ(SDCH_OK, sdch_manager->IsInSupportedDomain(google_url)); | |
| 704 } | |
| 705 | |
| 706 #endif // !defined(OS_IOS) | |
| 707 | |
| 708 } // namespace net | |
| OLD | NEW |