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.
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"
21 // Workaround for http://crbug.com/437794; remove when fixed.
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";
30 //------------------------------------------------------------------------------
32 class MockSdchObserver
: public SdchObserver
{
34 MockSdchObserver() : get_dictionary_notifications_(0) {}
36 const GURL
& last_dictionary_request_url() {
37 return last_dictionary_request_url_
;
39 const GURL
& last_dictionary_url() { return last_dictionary_url_
; }
40 int get_dictionary_notifications() { return get_dictionary_notifications_
; }
42 // SdchObserver implementation
43 void OnGetDictionary(SdchManager
* manager
,
44 const GURL
& request_url
,
45 const GURL
& dictionary_url
) override
{
46 ++get_dictionary_notifications_
;
47 last_dictionary_request_url_
= request_url
;
48 last_dictionary_url_
= dictionary_url
;
50 void OnClearDictionaries(SdchManager
* manager
) override
{}
53 int get_dictionary_notifications_
;
54 GURL last_dictionary_request_url_
;
55 GURL last_dictionary_url_
;
58 class SdchManagerTest
: public testing::Test
{
61 : sdch_manager_(new SdchManager
),
62 default_support_(false),
63 default_https_support_(false) {
64 default_support_
= sdch_manager_
->sdch_enabled();
65 default_https_support_
= sdch_manager_
->secure_scheme_supported();
68 ~SdchManagerTest() override
{}
70 SdchManager
* sdch_manager() { return sdch_manager_
.get(); }
72 // Reset globals back to default state.
73 void TearDown() override
{
74 SdchManager::EnableSdchSupport(default_support_
);
75 SdchManager::EnableSecureSchemeSupport(default_https_support_
);
78 // Attempt to add a dictionary to the manager and probe for success or
80 bool AddSdchDictionary(const std::string
& dictionary_text
,
82 return sdch_manager_
->AddSdchDictionary(dictionary_text
, gurl
) == SDCH_OK
;
86 scoped_ptr
<SdchManager
> sdch_manager_
;
87 bool default_support_
;
88 bool default_https_support_
;
91 static std::string
NewSdchDictionary(const std::string
& domain
) {
92 std::string dictionary
;
93 if (!domain
.empty()) {
94 dictionary
.append("Domain: ");
95 dictionary
.append(domain
);
96 dictionary
.append("\n");
98 dictionary
.append("\n");
99 dictionary
.append(kTestVcdiffDictionary
, sizeof(kTestVcdiffDictionary
) - 1);
103 TEST_F(SdchManagerTest
, DomainSupported
) {
104 GURL
google_url("http://www.google.com");
106 SdchManager::EnableSdchSupport(false);
107 EXPECT_EQ(SDCH_DISABLED
, sdch_manager()->IsInSupportedDomain(google_url
));
108 SdchManager::EnableSdchSupport(true);
109 EXPECT_EQ(SDCH_OK
, sdch_manager()->IsInSupportedDomain(google_url
));
112 TEST_F(SdchManagerTest
, DomainBlacklisting
) {
113 GURL
test_url("http://www.test.com");
114 GURL
google_url("http://www.google.com");
116 sdch_manager()->BlacklistDomain(test_url
, SDCH_OK
);
117 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET
,
118 sdch_manager()->IsInSupportedDomain(test_url
));
119 EXPECT_EQ(SDCH_OK
, sdch_manager()->IsInSupportedDomain(google_url
));
121 sdch_manager()->BlacklistDomain(google_url
, SDCH_OK
);
122 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET
,
123 sdch_manager()->IsInSupportedDomain(google_url
));
126 TEST_F(SdchManagerTest
, DomainBlacklistingCaseSensitivity
) {
127 GURL
test_url("http://www.TesT.com");
128 GURL
test2_url("http://www.tEst.com");
130 EXPECT_EQ(SDCH_OK
, sdch_manager()->IsInSupportedDomain(test_url
));
131 EXPECT_EQ(SDCH_OK
, sdch_manager()->IsInSupportedDomain(test2_url
));
132 sdch_manager()->BlacklistDomain(test_url
, SDCH_OK
);
133 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET
,
134 sdch_manager()->IsInSupportedDomain(test2_url
));
137 TEST_F(SdchManagerTest
, BlacklistingReset
) {
138 GURL
gurl("http://mytest.DoMain.com");
139 std::string
domain(gurl
.host());
141 sdch_manager()->ClearBlacklistings();
142 EXPECT_EQ(sdch_manager()->BlackListDomainCount(domain
), 0);
143 EXPECT_EQ(sdch_manager()->BlacklistDomainExponential(domain
), 0);
144 EXPECT_EQ(SDCH_OK
, sdch_manager()->IsInSupportedDomain(gurl
));
147 TEST_F(SdchManagerTest
, BlacklistingSingleBlacklist
) {
148 GURL
gurl("http://mytest.DoMain.com");
149 std::string
domain(gurl
.host());
150 sdch_manager()->ClearBlacklistings();
152 sdch_manager()->BlacklistDomain(gurl
, SDCH_OK
);
153 EXPECT_EQ(sdch_manager()->BlackListDomainCount(domain
), 1);
154 EXPECT_EQ(sdch_manager()->BlacklistDomainExponential(domain
), 1);
156 // Check that any domain lookup reduces the blacklist counter.
157 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET
,
158 sdch_manager()->IsInSupportedDomain(gurl
));
159 EXPECT_EQ(sdch_manager()->BlackListDomainCount(domain
), 0);
160 EXPECT_EQ(SDCH_OK
, sdch_manager()->IsInSupportedDomain(gurl
));
163 TEST_F(SdchManagerTest
, BlacklistingExponential
) {
164 GURL
gurl("http://mytest.DoMain.com");
165 std::string
domain(gurl
.host());
166 sdch_manager()->ClearBlacklistings();
169 for (int i
= 1; i
< 100; ++i
) {
170 sdch_manager()->BlacklistDomain(gurl
, SDCH_OK
);
171 EXPECT_EQ(sdch_manager()->BlacklistDomainExponential(domain
), exponential
);
173 EXPECT_EQ(sdch_manager()->BlackListDomainCount(domain
), exponential
);
174 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET
,
175 sdch_manager()->IsInSupportedDomain(gurl
));
176 EXPECT_EQ(sdch_manager()->BlackListDomainCount(domain
), exponential
- 1);
178 // Simulate a large number of domain checks (which eventually remove the
180 sdch_manager()->ClearDomainBlacklisting(domain
);
181 EXPECT_EQ(sdch_manager()->BlackListDomainCount(domain
), 0);
182 EXPECT_EQ(SDCH_OK
, sdch_manager()->IsInSupportedDomain(gurl
));
184 // Predict what exponential backoff will be.
185 exponential
= 1 + 2 * exponential
;
187 exponential
= INT_MAX
; // We don't wrap.
191 TEST_F(SdchManagerTest
, CanSetExactMatchDictionary
) {
192 std::string
dictionary_domain("x.y.z.google.com");
193 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
195 // Perfect match should work.
196 EXPECT_TRUE(AddSdchDictionary(dictionary_text
,
197 GURL("http://" + dictionary_domain
)));
200 TEST_F(SdchManagerTest
, CanAdvertiseDictionaryOverHTTP
) {
201 std::string
dictionary_domain("x.y.z.google.com");
202 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
204 EXPECT_TRUE(AddSdchDictionary(dictionary_text
,
205 GURL("http://" + dictionary_domain
)));
207 // HTTP target URL can advertise dictionary.
208 EXPECT_TRUE(sdch_manager()->GetDictionarySet(
209 GURL("http://" + dictionary_domain
+ "/test")));
212 TEST_F(SdchManagerTest
, CanNotAdvertiseDictionaryOverHTTPS
) {
213 std::string
dictionary_domain("x.y.z.google.com");
214 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
216 EXPECT_TRUE(AddSdchDictionary(dictionary_text
,
217 GURL("http://" + dictionary_domain
)));
219 // HTTPS target URL should NOT advertise dictionary.
220 EXPECT_FALSE(sdch_manager()->GetDictionarySet(
221 GURL("https://" + dictionary_domain
+ "/test")));
224 TEST_F(SdchManagerTest
, CanUseHTTPSDictionaryOverHTTPSIfEnabled
) {
225 std::string
dictionary_domain("x.y.z.google.com");
226 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
228 SdchManager::EnableSecureSchemeSupport(false);
229 EXPECT_FALSE(AddSdchDictionary(dictionary_text
,
230 GURL("https://" + dictionary_domain
)));
231 SdchManager::EnableSecureSchemeSupport(true);
232 EXPECT_TRUE(AddSdchDictionary(dictionary_text
,
233 GURL("https://" + dictionary_domain
)));
235 GURL
target_url("https://" + dictionary_domain
+ "/test");
236 // HTTPS target URL should advertise dictionary if secure scheme support is
238 EXPECT_TRUE(sdch_manager()->GetDictionarySet(target_url
));
240 // Dictionary should be available.
241 std::string client_hash
;
242 std::string server_hash
;
243 sdch_manager()->GenerateHash(dictionary_text
, &client_hash
, &server_hash
);
244 SdchProblemCode problem_code
;
245 scoped_ptr
<SdchManager::DictionarySet
> dict_set(
246 sdch_manager()->GetDictionarySetByHash(
247 target_url
, server_hash
, &problem_code
));
248 EXPECT_EQ(SDCH_OK
, problem_code
);
249 EXPECT_TRUE(dict_set
.get());
250 EXPECT_TRUE(dict_set
->GetDictionary(server_hash
));
253 TEST_F(SdchManagerTest
, CanNotUseHTTPDictionaryOverHTTPS
) {
254 std::string
dictionary_domain("x.y.z.google.com");
255 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
257 EXPECT_TRUE(AddSdchDictionary(dictionary_text
,
258 GURL("http://" + dictionary_domain
)));
260 GURL
target_url("https://" + dictionary_domain
+ "/test");
261 // HTTPS target URL should not advertise dictionary acquired over HTTP even if
262 // secure scheme support is enabled.
263 SdchManager::EnableSecureSchemeSupport(true);
264 EXPECT_FALSE(sdch_manager()->GetDictionarySet(target_url
));
266 std::string client_hash
;
267 std::string server_hash
;
268 sdch_manager()->GenerateHash(dictionary_text
, &client_hash
, &server_hash
);
269 SdchProblemCode problem_code
;
270 scoped_ptr
<SdchManager::DictionarySet
> dict_set(
271 sdch_manager()->GetDictionarySetByHash(
272 target_url
, server_hash
, &problem_code
));
273 EXPECT_FALSE(dict_set
.get());
274 EXPECT_EQ(SDCH_DICTIONARY_FOUND_HAS_WRONG_SCHEME
, problem_code
);
277 TEST_F(SdchManagerTest
, CanNotUseHTTPSDictionaryOverHTTP
) {
278 std::string
dictionary_domain("x.y.z.google.com");
279 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
281 SdchManager::EnableSecureSchemeSupport(true);
282 EXPECT_TRUE(AddSdchDictionary(dictionary_text
,
283 GURL("https://" + dictionary_domain
)));
285 GURL
target_url("http://" + dictionary_domain
+ "/test");
286 // HTTP target URL should not advertise dictionary acquired over HTTPS even if
287 // secure scheme support is enabled.
288 EXPECT_FALSE(sdch_manager()->GetDictionarySet(target_url
));
290 std::string client_hash
;
291 std::string server_hash
;
292 sdch_manager()->GenerateHash(dictionary_text
, &client_hash
, &server_hash
);
293 SdchProblemCode problem_code
;
294 scoped_ptr
<SdchManager::DictionarySet
> dict_set(
295 sdch_manager()->GetDictionarySetByHash(
296 target_url
, server_hash
, &problem_code
));
297 EXPECT_FALSE(dict_set
.get());
298 EXPECT_EQ(SDCH_DICTIONARY_FOUND_HAS_WRONG_SCHEME
, problem_code
);
301 TEST_F(SdchManagerTest
, FailToSetDomainMismatchDictionary
) {
302 std::string
dictionary_domain("x.y.z.google.com");
303 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
305 // Fail the "domain match" requirement.
306 EXPECT_FALSE(AddSdchDictionary(dictionary_text
,
307 GURL("http://y.z.google.com")));
310 TEST_F(SdchManagerTest
, FailToSetDotHostPrefixDomainDictionary
) {
311 std::string
dictionary_domain("x.y.z.google.com");
312 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
314 // Fail the HD with D being the domain and H having a dot requirement.
315 EXPECT_FALSE(AddSdchDictionary(dictionary_text
,
316 GURL("http://w.x.y.z.google.com")));
319 TEST_F(SdchManagerTest
, FailToSetDotHostPrefixDomainDictionaryTrailingDot
) {
320 std::string
dictionary_domain("x.y.z.google.com");
321 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
323 // Fail the HD with D being the domain and H having a dot requirement.
324 EXPECT_FALSE(AddSdchDictionary(dictionary_text
,
325 GURL("http://w.x.y.z.google.com.")));
328 TEST_F(SdchManagerTest
, FailToSetRepeatPrefixWithDotDictionary
) {
329 // Make sure that a prefix that matches the domain postfix won't confuse
330 // the validation checks.
331 std::string
dictionary_domain("www.google.com");
332 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
334 // Fail the HD with D being the domain and H having a dot requirement.
335 EXPECT_FALSE(AddSdchDictionary(dictionary_text
,
336 GURL("http://www.google.com.www.google.com")));
339 TEST_F(SdchManagerTest
, CanSetLeadingDotDomainDictionary
) {
340 // Make sure that a prefix that matches the domain postfix won't confuse
341 // the validation checks.
342 std::string
dictionary_domain(".google.com");
343 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
345 // Verify that a leading dot in the domain is acceptable, as long as the host
346 // name does not contain any dots preceding the matched domain name.
347 EXPECT_TRUE(AddSdchDictionary(dictionary_text
, GURL("http://www.google.com")));
350 TEST_F(SdchManagerTest
,
351 CanSetLeadingDotDomainDictionaryFromURLWithTrailingDot
) {
352 // Make sure that a prefix that matches the domain postfix won't confuse
353 // the validation checks.
354 std::string
dictionary_domain(".google.com");
355 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
357 // Verify that a leading dot in the domain is acceptable, as long as the host
358 // name does not contain any dots preceding the matched domain name.
359 EXPECT_TRUE(AddSdchDictionary(dictionary_text
,
360 GURL("http://www.google.com.")));
363 TEST_F(SdchManagerTest
, CannotSetLeadingDotDomainDictionary
) {
364 // Make sure that a prefix that matches the domain postfix won't confuse
365 // the validation checks.
366 std::string
dictionary_domain(".google.com");
367 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
369 // Verify that a leading dot in the domain does not affect the name containing
371 EXPECT_FALSE(AddSdchDictionary(dictionary_text
,
372 GURL("http://www.subdomain.google.com")));
375 TEST_F(SdchManagerTest
, CannotSetLeadingDotDomainDictionaryTrailingDot
) {
376 // Make sure that a prefix that matches the domain postfix won't confuse
377 // the validation checks.
378 std::string
dictionary_domain(".google.com");
379 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
381 // Verify that a trailing period in the URL doesn't affect the check.
382 EXPECT_FALSE(AddSdchDictionary(dictionary_text
,
383 GURL("http://www.subdomain.google.com.")));
386 // Make sure the order of the tests is not helping us or confusing things.
387 // See test CanSetExactMatchDictionary above for first try.
388 TEST_F(SdchManagerTest
, CanStillSetExactMatchDictionary
) {
389 std::string
dictionary_domain("x.y.z.google.com");
390 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
392 // Perfect match should *STILL* work.
393 EXPECT_TRUE(AddSdchDictionary(dictionary_text
,
394 GURL("http://" + dictionary_domain
)));
397 // Make sure the DOS protection precludes the addition of too many dictionaries.
398 TEST_F(SdchManagerTest
, TooManyDictionaries
) {
399 std::string
dictionary_domain(".google.com");
400 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
402 for (size_t count
= 0; count
< SdchManager::kMaxDictionaryCount
; ++count
) {
403 EXPECT_TRUE(AddSdchDictionary(dictionary_text
,
404 GURL("http://www.google.com")));
405 dictionary_text
+= " "; // Create dictionary with different SHA signature.
408 AddSdchDictionary(dictionary_text
, GURL("http://www.google.com")));
411 TEST_F(SdchManagerTest
, DictionaryNotTooLarge
) {
412 std::string
dictionary_domain(".google.com");
413 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
415 dictionary_text
.append(
416 SdchManager::kMaxDictionarySize
- dictionary_text
.size(), ' ');
417 EXPECT_TRUE(AddSdchDictionary(dictionary_text
,
418 GURL("http://" + dictionary_domain
)));
421 TEST_F(SdchManagerTest
, DictionaryTooLarge
) {
422 std::string
dictionary_domain(".google.com");
423 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
425 dictionary_text
.append(
426 SdchManager::kMaxDictionarySize
+ 1 - dictionary_text
.size(), ' ');
427 EXPECT_FALSE(AddSdchDictionary(dictionary_text
,
428 GURL("http://" + dictionary_domain
)));
431 TEST_F(SdchManagerTest
, PathMatch
) {
432 bool (*PathMatch
)(const std::string
& path
, const std::string
& restriction
) =
433 SdchManager::Dictionary::PathMatch
;
434 // Perfect match is supported.
435 EXPECT_TRUE(PathMatch("/search", "/search"));
436 EXPECT_TRUE(PathMatch("/search/", "/search/"));
438 // Prefix only works if last character of restriction is a slash, or first
439 // character in path after a match is a slash. Validate each case separately.
441 // Rely on the slash in the path (not 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"));
447 // Rely on the slash at the end of the restriction.
448 EXPECT_TRUE(PathMatch("/search/something", "/search/"));
449 EXPECT_TRUE(PathMatch("/search/s", "/search/"));
450 EXPECT_TRUE(PathMatch("/search/other", "/search/"));
451 EXPECT_TRUE(PathMatch("/search/something", "/search/"));
453 // Make sure less that sufficient prefix match is false.
454 EXPECT_FALSE(PathMatch("/sear", "/search"));
455 EXPECT_FALSE(PathMatch("/", "/search"));
456 EXPECT_FALSE(PathMatch(std::string(), "/search"));
458 // Add examples with several levels of direcories in the restriction.
459 EXPECT_FALSE(PathMatch("/search/something", "search/s"));
460 EXPECT_FALSE(PathMatch("/search/", "/search/s"));
462 // Make sure adding characters to path will also fail.
463 EXPECT_FALSE(PathMatch("/searching", "/search/"));
464 EXPECT_FALSE(PathMatch("/searching", "/search"));
466 // Make sure we're case sensitive.
467 EXPECT_FALSE(PathMatch("/ABC", "/abc"));
468 EXPECT_FALSE(PathMatch("/abc", "/ABC"));
471 // The following are only applicable while we have a latency test in the code,
472 // and can be removed when that functionality is stripped.
473 TEST_F(SdchManagerTest
, LatencyTestControls
) {
474 GURL
url("http://www.google.com");
475 GURL
url2("http://www.google2.com");
477 // First make sure we default to false.
478 EXPECT_FALSE(sdch_manager()->AllowLatencyExperiment(url
));
479 EXPECT_FALSE(sdch_manager()->AllowLatencyExperiment(url2
));
481 // That we can set each to true.
482 sdch_manager()->SetAllowLatencyExperiment(url
, true);
483 EXPECT_TRUE(sdch_manager()->AllowLatencyExperiment(url
));
484 EXPECT_FALSE(sdch_manager()->AllowLatencyExperiment(url2
));
486 sdch_manager()->SetAllowLatencyExperiment(url2
, true);
487 EXPECT_TRUE(sdch_manager()->AllowLatencyExperiment(url
));
488 EXPECT_TRUE(sdch_manager()->AllowLatencyExperiment(url2
));
490 // And can reset them to false.
491 sdch_manager()->SetAllowLatencyExperiment(url
, false);
492 EXPECT_FALSE(sdch_manager()->AllowLatencyExperiment(url
));
493 EXPECT_TRUE(sdch_manager()->AllowLatencyExperiment(url2
));
495 sdch_manager()->SetAllowLatencyExperiment(url2
, false);
496 EXPECT_FALSE(sdch_manager()->AllowLatencyExperiment(url
));
497 EXPECT_FALSE(sdch_manager()->AllowLatencyExperiment(url2
));
500 TEST_F(SdchManagerTest
, CanUseMultipleManagers
) {
501 SdchManager second_manager
;
503 std::string
dictionary_domain_1("x.y.z.google.com");
504 std::string
dictionary_domain_2("x.y.z.chromium.org");
506 std::string
dictionary_text_1(NewSdchDictionary(dictionary_domain_1
));
507 std::string
dictionary_text_2(NewSdchDictionary(dictionary_domain_2
));
509 std::string tmp_hash
;
510 std::string server_hash_1
;
511 std::string server_hash_2
;
513 SdchManager::GenerateHash(dictionary_text_1
, &tmp_hash
, &server_hash_1
);
514 SdchManager::GenerateHash(dictionary_text_2
, &tmp_hash
, &server_hash_2
);
516 // Confirm that if you add directories to one manager, you
517 // can't get them from the other.
518 EXPECT_TRUE(AddSdchDictionary(dictionary_text_1
,
519 GURL("http://" + dictionary_domain_1
)));
520 scoped_ptr
<SdchManager::DictionarySet
> dict_set
;
522 SdchProblemCode problem_code
;
523 dict_set
= sdch_manager()->GetDictionarySetByHash(
524 GURL("http://" + dictionary_domain_1
+ "/random_url"),
525 server_hash_1
, &problem_code
);
526 EXPECT_TRUE(dict_set
);
527 EXPECT_TRUE(dict_set
->GetDictionary(server_hash_1
));
528 EXPECT_EQ(SDCH_OK
, problem_code
);
530 second_manager
.AddSdchDictionary(
531 dictionary_text_2
, GURL("http://" + dictionary_domain_2
));
532 dict_set
= second_manager
.GetDictionarySetByHash(
533 GURL("http://" + dictionary_domain_2
+ "/random_url"),
534 server_hash_2
, &problem_code
);
535 EXPECT_TRUE(dict_set
);
536 EXPECT_TRUE(dict_set
->GetDictionary(server_hash_2
));
537 EXPECT_EQ(SDCH_OK
, problem_code
);
539 dict_set
= sdch_manager()->GetDictionarySetByHash(
540 GURL("http://" + dictionary_domain_2
+ "/random_url"),
541 server_hash_2
, &problem_code
);
542 EXPECT_FALSE(dict_set
);
543 EXPECT_EQ(SDCH_DICTIONARY_HASH_NOT_FOUND
, problem_code
);
545 dict_set
= second_manager
.GetDictionarySetByHash(
546 GURL("http://" + dictionary_domain_1
+ "/random_url"),
547 server_hash_1
, &problem_code
);
548 EXPECT_FALSE(dict_set
);
549 EXPECT_EQ(SDCH_DICTIONARY_HASH_NOT_FOUND
, problem_code
);
552 TEST_F(SdchManagerTest
, HttpsCorrectlySupported
) {
553 GURL
url("http://www.google.com");
554 GURL
secure_url("https://www.google.com");
556 bool expect_https_support
= true;
558 SdchProblemCode expected_code
=
559 expect_https_support
? SDCH_OK
: SDCH_SECURE_SCHEME_NOT_SUPPORTED
;
561 EXPECT_EQ(SDCH_OK
, sdch_manager()->IsInSupportedDomain(url
));
562 EXPECT_EQ(expected_code
, sdch_manager()->IsInSupportedDomain(secure_url
));
564 SdchManager::EnableSecureSchemeSupport(!expect_https_support
);
565 EXPECT_EQ(SDCH_OK
, sdch_manager()->IsInSupportedDomain(url
));
566 EXPECT_NE(expected_code
, sdch_manager()->IsInSupportedDomain(secure_url
));
569 TEST_F(SdchManagerTest
, ClearDictionaryData
) {
570 std::string
dictionary_domain("x.y.z.google.com");
571 GURL
blacklist_url("http://bad.chromium.org");
573 std::string
dictionary_text(NewSdchDictionary(dictionary_domain
));
574 std::string tmp_hash
;
575 std::string server_hash
;
577 SdchManager::GenerateHash(dictionary_text
, &tmp_hash
, &server_hash
);
579 EXPECT_TRUE(AddSdchDictionary(dictionary_text
,
580 GURL("http://" + dictionary_domain
)));
582 scoped_ptr
<SdchManager::DictionarySet
> dict_set
;
584 SdchProblemCode problem_code
;
585 dict_set
= sdch_manager()->GetDictionarySetByHash(
586 GURL("http://" + dictionary_domain
+ "/random_url"),
587 server_hash
, &problem_code
);
588 EXPECT_TRUE(dict_set
);
589 EXPECT_TRUE(dict_set
->GetDictionary(server_hash
));
590 EXPECT_EQ(SDCH_OK
, problem_code
);
592 sdch_manager()->BlacklistDomain(GURL(blacklist_url
), SDCH_OK
);
593 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET
,
594 sdch_manager()->IsInSupportedDomain(blacklist_url
));
596 sdch_manager()->ClearData();
598 dict_set
= sdch_manager()->GetDictionarySetByHash(
599 GURL("http://" + dictionary_domain
+ "/random_url"),
600 server_hash
, &problem_code
);
601 EXPECT_FALSE(dict_set
);
602 EXPECT_EQ(SDCH_DICTIONARY_HASH_NOT_FOUND
, problem_code
);
603 EXPECT_EQ(SDCH_OK
, sdch_manager()->IsInSupportedDomain(blacklist_url
));
606 TEST_F(SdchManagerTest
, GetDictionaryNotification
) {
607 GURL
test_request_gurl(GURL("http://www.example.com/data"));
608 GURL
test_dictionary_gurl(GURL("http://www.example.com/dict"));
609 MockSdchObserver observer
;
610 sdch_manager()->AddObserver(&observer
);
612 EXPECT_EQ(0, observer
.get_dictionary_notifications());
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());
618 sdch_manager()->RemoveObserver(&observer
);
619 sdch_manager()->OnGetDictionary(test_request_gurl
, test_dictionary_gurl
);
620 EXPECT_EQ(1, observer
.get_dictionary_notifications());
621 EXPECT_EQ(test_request_gurl
, observer
.last_dictionary_request_url());
622 EXPECT_EQ(test_dictionary_gurl
, observer
.last_dictionary_url());
625 TEST_F(SdchManagerTest
, ExpirationCheckedProperly
) {
626 // Create an SDCH dictionary with an expiration time in the past.
627 std::string
dictionary_domain("x.y.z.google.com");
628 std::string
dictionary_text(base::StringPrintf(
629 "Domain: %s\nMax-age: 0\n\n", dictionary_domain
.c_str()));
630 dictionary_text
.append(
631 kTestVcdiffDictionary
, sizeof(kTestVcdiffDictionary
) - 1);
632 std::string client_hash
;
633 std::string server_hash
;
634 SdchManager::GenerateHash(dictionary_text
, &client_hash
, &server_hash
);
635 GURL
target_gurl("http://" + dictionary_domain
);
636 AddSdchDictionary(dictionary_text
, target_gurl
);
638 // It should be visible if looked up by hash whether expired or not.
639 scoped_ptr
<base::SimpleTestClock
> clock(new base::SimpleTestClock
);
640 clock
->SetNow(base::Time::Now());
641 clock
->Advance(base::TimeDelta::FromMinutes(5));
642 SdchProblemCode problem_code
;
643 scoped_ptr
<SdchManager::DictionarySet
> hash_set(
644 sdch_manager()->GetDictionarySetByHash(
645 target_gurl
, server_hash
, &problem_code
).Pass());
646 ASSERT_TRUE(hash_set
);
647 ASSERT_EQ(SDCH_OK
, problem_code
);
648 const_cast<SdchManager::Dictionary
*>(
649 hash_set
->GetDictionary(server_hash
))->SetClockForTesting(
652 // Make sure it's not visible for advertisement, but is visible
653 // if looked up by hash.
654 EXPECT_FALSE(sdch_manager()->GetDictionarySet(target_gurl
));
655 EXPECT_TRUE(sdch_manager()->GetDictionarySetByHash(
656 target_gurl
, server_hash
, &problem_code
));
657 EXPECT_EQ(SDCH_OK
, problem_code
);
660 TEST_F(SdchManagerTest
, SdchOnByDefault
) {
661 GURL
google_url("http://www.google.com");
662 scoped_ptr
<SdchManager
> sdch_manager(new SdchManager
);
664 EXPECT_EQ(SDCH_OK
, sdch_manager
->IsInSupportedDomain(google_url
));
665 SdchManager::EnableSdchSupport(false);
666 EXPECT_EQ(SDCH_DISABLED
, sdch_manager
->IsInSupportedDomain(google_url
));
671 TEST(SdchManagerTest
, SdchOffByDefault
) {
672 GURL
google_url("http://www.google.com");
673 scoped_ptr
<SdchManager
> sdch_manager(new SdchManager
);
675 EXPECT_EQ(SDCH_DISABLED
, sdch_manager
->IsInSupportedDomain(google_url
));
676 SdchManager::EnableSdchSupport(true);
677 EXPECT_EQ(SDCH_OK
, sdch_manager
->IsInSupportedDomain(google_url
));
680 #endif // !defined(OS_IOS)