Revert of Re-enabling webrtc Telemetry tests. (patchset #1 id:1 of https://codereview...
[chromium-blink-merge.git] / net / base / sdch_manager_unittest.cc
blob71362f77925137d7bd609d38885e5adbb1d2a95a
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.
5 #include <limits.h>
7 #include <string>
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/sdch_manager.h"
14 #include "net/base/sdch_observer.h"
15 #include "net/log/net_log.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "url/gurl.h"
19 namespace net {
21 //------------------------------------------------------------------------------
22 // Provide sample data and compression results with a sample VCDIFF dictionary.
23 // Note an SDCH dictionary has extra meta-data before the VCDIFF dictionary.
24 static const char kTestVcdiffDictionary[] = "DictionaryFor"
25 "SdchCompression1SdchCompression2SdchCompression3SdchCompression\n";
27 //------------------------------------------------------------------------------
29 class MockSdchObserver : public SdchObserver {
30 public:
31 MockSdchObserver()
32 : dictionary_added_notifications_(0),
33 dictionary_removed_notifications_(0),
34 dictionary_used_notifications_(0),
35 get_dictionary_notifications_(0),
36 clear_dictionaries_notifications_(0) {}
38 int dictionary_added_notifications() const {
39 return dictionary_added_notifications_;
41 int dictionary_removed_notifications() const {
42 return dictionary_removed_notifications_;
44 std::string last_server_hash() const { return last_server_hash_; }
45 int dictionary_used_notifications() const {
46 return dictionary_used_notifications_;
48 const GURL& last_dictionary_request_url() const {
49 return last_dictionary_request_url_;
51 const GURL& last_dictionary_url() const { return last_dictionary_url_; }
52 int get_dictionary_notifications() const {
53 return get_dictionary_notifications_;
56 int clear_dictionary_notifications() const {
57 return clear_dictionaries_notifications_;
60 // SdchObserver implementation
61 void OnDictionaryAdded(const GURL& dictionary_url,
62 const std::string& server_hash) override {
63 last_server_hash_ = server_hash;
64 last_dictionary_url_ = dictionary_url;
65 ++dictionary_added_notifications_;
67 void OnDictionaryRemoved(const std::string& server_hash) override {
68 last_server_hash_ = server_hash;
69 ++dictionary_removed_notifications_;
71 void OnDictionaryUsed(const std::string& server_hash) override {
72 last_server_hash_ = server_hash;
73 ++dictionary_used_notifications_;
76 void OnGetDictionary(const GURL& request_url,
77 const GURL& dictionary_url) override {
78 ++get_dictionary_notifications_;
79 last_dictionary_request_url_ = request_url;
80 last_dictionary_url_ = dictionary_url;
82 void OnClearDictionaries() override {
83 ++clear_dictionaries_notifications_;
86 private:
87 int dictionary_added_notifications_;
88 int dictionary_removed_notifications_;
89 int dictionary_used_notifications_;
90 int get_dictionary_notifications_;
91 int clear_dictionaries_notifications_;
93 std::string last_server_hash_;
94 GURL last_dictionary_request_url_;
95 GURL last_dictionary_url_;
97 DISALLOW_COPY_AND_ASSIGN(MockSdchObserver);
100 class SdchManagerTest : public testing::Test {
101 protected:
102 SdchManagerTest()
103 : sdch_manager_(new SdchManager),
104 default_support_(sdch_manager_->sdch_enabled()) { }
106 ~SdchManagerTest() override {}
108 SdchManager* sdch_manager() { return sdch_manager_.get(); }
110 // Reset globals back to default state.
111 void TearDown() override {
112 SdchManager::EnableSdchSupport(default_support_);
115 // Attempt to add a dictionary to the manager and probe for success or
116 // failure.
117 bool AddSdchDictionary(const std::string& dictionary_text,
118 const GURL& gurl) {
119 return sdch_manager_->AddSdchDictionary(dictionary_text, gurl, nullptr) ==
120 SDCH_OK;
123 private:
124 scoped_ptr<SdchManager> sdch_manager_;
125 bool default_support_;
128 static std::string NewSdchDictionary(const std::string& domain) {
129 std::string dictionary;
130 if (!domain.empty()) {
131 dictionary.append("Domain: ");
132 dictionary.append(domain);
133 dictionary.append("\n");
135 dictionary.append("\n");
136 dictionary.append(kTestVcdiffDictionary, sizeof(kTestVcdiffDictionary) - 1);
137 return dictionary;
140 TEST_F(SdchManagerTest, DomainSupported) {
141 GURL google_url("http://www.google.com");
143 SdchManager::EnableSdchSupport(false);
144 EXPECT_EQ(SDCH_DISABLED, sdch_manager()->IsInSupportedDomain(google_url));
145 SdchManager::EnableSdchSupport(true);
146 EXPECT_EQ(SDCH_OK, sdch_manager()->IsInSupportedDomain(google_url));
149 TEST_F(SdchManagerTest, DomainBlacklisting) {
150 GURL test_url("http://www.test.com");
151 GURL google_url("http://www.google.com");
153 sdch_manager()->BlacklistDomain(test_url, SDCH_OK);
154 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET,
155 sdch_manager()->IsInSupportedDomain(test_url));
156 EXPECT_EQ(SDCH_OK, sdch_manager()->IsInSupportedDomain(google_url));
158 sdch_manager()->BlacklistDomain(google_url, SDCH_OK);
159 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET,
160 sdch_manager()->IsInSupportedDomain(google_url));
163 TEST_F(SdchManagerTest, DomainBlacklistingCaseSensitivity) {
164 GURL test_url("http://www.TesT.com");
165 GURL test2_url("http://www.tEst.com");
167 EXPECT_EQ(SDCH_OK, sdch_manager()->IsInSupportedDomain(test_url));
168 EXPECT_EQ(SDCH_OK, sdch_manager()->IsInSupportedDomain(test2_url));
169 sdch_manager()->BlacklistDomain(test_url, SDCH_OK);
170 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET,
171 sdch_manager()->IsInSupportedDomain(test2_url));
174 TEST_F(SdchManagerTest, BlacklistingReset) {
175 GURL gurl("http://mytest.DoMain.com");
176 std::string domain(gurl.host());
178 sdch_manager()->ClearBlacklistings();
179 EXPECT_EQ(sdch_manager()->BlackListDomainCount(domain), 0);
180 EXPECT_EQ(sdch_manager()->BlacklistDomainExponential(domain), 0);
181 EXPECT_EQ(SDCH_OK, sdch_manager()->IsInSupportedDomain(gurl));
184 TEST_F(SdchManagerTest, BlacklistingSingleBlacklist) {
185 GURL gurl("http://mytest.DoMain.com");
186 std::string domain(gurl.host());
187 sdch_manager()->ClearBlacklistings();
189 sdch_manager()->BlacklistDomain(gurl, SDCH_OK);
190 EXPECT_EQ(sdch_manager()->BlackListDomainCount(domain), 1);
191 EXPECT_EQ(sdch_manager()->BlacklistDomainExponential(domain), 1);
193 // Check that any domain lookup reduces the blacklist counter.
194 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET,
195 sdch_manager()->IsInSupportedDomain(gurl));
196 EXPECT_EQ(sdch_manager()->BlackListDomainCount(domain), 0);
197 EXPECT_EQ(SDCH_OK, sdch_manager()->IsInSupportedDomain(gurl));
200 TEST_F(SdchManagerTest, BlacklistingExponential) {
201 GURL gurl("http://mytest.DoMain.com");
202 std::string domain(gurl.host());
203 sdch_manager()->ClearBlacklistings();
205 int exponential = 1;
206 for (int i = 1; i < 100; ++i) {
207 sdch_manager()->BlacklistDomain(gurl, SDCH_OK);
208 EXPECT_EQ(sdch_manager()->BlacklistDomainExponential(domain), exponential);
210 EXPECT_EQ(sdch_manager()->BlackListDomainCount(domain), exponential);
211 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET,
212 sdch_manager()->IsInSupportedDomain(gurl));
213 EXPECT_EQ(sdch_manager()->BlackListDomainCount(domain), exponential - 1);
215 // Simulate a large number of domain checks (which eventually remove the
216 // blacklisting).
217 sdch_manager()->ClearDomainBlacklisting(domain);
218 EXPECT_EQ(sdch_manager()->BlackListDomainCount(domain), 0);
219 EXPECT_EQ(SDCH_OK, sdch_manager()->IsInSupportedDomain(gurl));
221 // Predict what exponential backoff will be.
222 exponential = 1 + 2 * exponential;
223 if (exponential < 0)
224 exponential = INT_MAX; // We don't wrap.
228 TEST_F(SdchManagerTest, CanSetExactMatchDictionary) {
229 std::string dictionary_domain("x.y.z.google.com");
230 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
232 // Perfect match should work.
233 EXPECT_TRUE(AddSdchDictionary(dictionary_text,
234 GURL("http://" + dictionary_domain)));
237 TEST_F(SdchManagerTest, CanAdvertiseDictionaryOverHTTP) {
238 std::string dictionary_domain("x.y.z.google.com");
239 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
241 EXPECT_TRUE(AddSdchDictionary(dictionary_text,
242 GURL("http://" + dictionary_domain)));
244 // HTTP target URL can advertise dictionary.
245 EXPECT_TRUE(sdch_manager()->GetDictionarySet(
246 GURL("http://" + dictionary_domain + "/test")));
249 TEST_F(SdchManagerTest, CanNotAdvertiseDictionaryOverHTTPS) {
250 std::string dictionary_domain("x.y.z.google.com");
251 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
253 EXPECT_TRUE(AddSdchDictionary(dictionary_text,
254 GURL("http://" + dictionary_domain)));
256 // HTTPS target URL should NOT advertise dictionary.
257 EXPECT_FALSE(sdch_manager()->GetDictionarySet(
258 GURL("https://" + dictionary_domain + "/test")));
261 TEST_F(SdchManagerTest, CanUseHTTPSDictionaryOverHTTPSIfEnabled) {
262 std::string dictionary_domain("x.y.z.google.com");
263 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
265 EXPECT_TRUE(AddSdchDictionary(dictionary_text,
266 GURL("https://" + dictionary_domain)));
268 GURL target_url("https://" + dictionary_domain + "/test");
269 // HTTPS target URL should advertise dictionary if secure scheme support is
270 // enabled.
271 EXPECT_TRUE(sdch_manager()->GetDictionarySet(target_url));
273 // Dictionary should be available.
274 std::string client_hash;
275 std::string server_hash;
276 sdch_manager()->GenerateHash(dictionary_text, &client_hash, &server_hash);
277 SdchProblemCode problem_code;
278 scoped_ptr<SdchManager::DictionarySet> dict_set(
279 sdch_manager()->GetDictionarySetByHash(
280 target_url, server_hash, &problem_code));
281 EXPECT_EQ(SDCH_OK, problem_code);
282 EXPECT_TRUE(dict_set.get());
283 EXPECT_TRUE(dict_set->GetDictionaryText(server_hash));
286 TEST_F(SdchManagerTest, CanNotUseHTTPDictionaryOverHTTPS) {
287 std::string dictionary_domain("x.y.z.google.com");
288 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
290 EXPECT_TRUE(AddSdchDictionary(dictionary_text,
291 GURL("http://" + dictionary_domain)));
293 GURL target_url("https://" + dictionary_domain + "/test");
294 // HTTPS target URL should not advertise dictionary acquired over HTTP even if
295 // secure scheme support is enabled.
296 EXPECT_FALSE(sdch_manager()->GetDictionarySet(target_url));
298 std::string client_hash;
299 std::string server_hash;
300 sdch_manager()->GenerateHash(dictionary_text, &client_hash, &server_hash);
301 SdchProblemCode problem_code;
302 scoped_ptr<SdchManager::DictionarySet> dict_set(
303 sdch_manager()->GetDictionarySetByHash(
304 target_url, server_hash, &problem_code));
305 EXPECT_FALSE(dict_set.get());
306 EXPECT_EQ(SDCH_DICTIONARY_FOUND_HAS_WRONG_SCHEME, problem_code);
309 TEST_F(SdchManagerTest, CanNotUseHTTPSDictionaryOverHTTP) {
310 std::string dictionary_domain("x.y.z.google.com");
311 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
313 EXPECT_TRUE(AddSdchDictionary(dictionary_text,
314 GURL("https://" + dictionary_domain)));
316 GURL target_url("http://" + dictionary_domain + "/test");
317 // HTTP target URL should not advertise dictionary acquired over HTTPS even if
318 // secure scheme support is enabled.
319 EXPECT_FALSE(sdch_manager()->GetDictionarySet(target_url));
321 std::string client_hash;
322 std::string server_hash;
323 sdch_manager()->GenerateHash(dictionary_text, &client_hash, &server_hash);
324 SdchProblemCode problem_code;
325 scoped_ptr<SdchManager::DictionarySet> dict_set(
326 sdch_manager()->GetDictionarySetByHash(
327 target_url, server_hash, &problem_code));
328 EXPECT_FALSE(dict_set.get());
329 EXPECT_EQ(SDCH_DICTIONARY_FOUND_HAS_WRONG_SCHEME, problem_code);
332 TEST_F(SdchManagerTest, FailToSetDomainMismatchDictionary) {
333 std::string dictionary_domain("x.y.z.google.com");
334 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
336 // Fail the "domain match" requirement.
337 EXPECT_FALSE(AddSdchDictionary(dictionary_text,
338 GURL("http://y.z.google.com")));
341 TEST_F(SdchManagerTest, FailToSetDotHostPrefixDomainDictionary) {
342 std::string dictionary_domain("x.y.z.google.com");
343 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
345 // Fail the HD with D being the domain and H having a dot requirement.
346 EXPECT_FALSE(AddSdchDictionary(dictionary_text,
347 GURL("http://w.x.y.z.google.com")));
350 TEST_F(SdchManagerTest, FailToSetDotHostPrefixDomainDictionaryTrailingDot) {
351 std::string dictionary_domain("x.y.z.google.com");
352 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
354 // Fail the HD with D being the domain and H having a dot requirement.
355 EXPECT_FALSE(AddSdchDictionary(dictionary_text,
356 GURL("http://w.x.y.z.google.com.")));
359 TEST_F(SdchManagerTest, FailToSetRepeatPrefixWithDotDictionary) {
360 // Make sure that a prefix that matches the domain postfix won't confuse
361 // the validation checks.
362 std::string dictionary_domain("www.google.com");
363 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
365 // Fail the HD with D being the domain and H having a dot requirement.
366 EXPECT_FALSE(AddSdchDictionary(dictionary_text,
367 GURL("http://www.google.com.www.google.com")));
370 TEST_F(SdchManagerTest, CanSetLeadingDotDomainDictionary) {
371 // Make sure that a prefix that matches the domain postfix won't confuse
372 // the validation checks.
373 std::string dictionary_domain(".google.com");
374 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
376 // Verify that a leading dot in the domain is acceptable, as long as the host
377 // name does not contain any dots preceding the matched domain name.
378 EXPECT_TRUE(AddSdchDictionary(dictionary_text, GURL("http://www.google.com")));
381 TEST_F(SdchManagerTest,
382 CanSetLeadingDotDomainDictionaryFromURLWithTrailingDot) {
383 // Make sure that a prefix that matches the domain postfix won't confuse
384 // the validation checks.
385 std::string dictionary_domain(".google.com");
386 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
388 // Verify that a leading dot in the domain is acceptable, as long as the host
389 // name does not contain any dots preceding the matched domain name.
390 EXPECT_TRUE(AddSdchDictionary(dictionary_text,
391 GURL("http://www.google.com.")));
394 TEST_F(SdchManagerTest, CannotSetLeadingDotDomainDictionary) {
395 // Make sure that a prefix that matches the domain postfix won't confuse
396 // the validation checks.
397 std::string dictionary_domain(".google.com");
398 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
400 // Verify that a leading dot in the domain does not affect the name containing
401 // dots failure.
402 EXPECT_FALSE(AddSdchDictionary(dictionary_text,
403 GURL("http://www.subdomain.google.com")));
406 TEST_F(SdchManagerTest, CannotSetLeadingDotDomainDictionaryTrailingDot) {
407 // Make sure that a prefix that matches the domain postfix won't confuse
408 // the validation checks.
409 std::string dictionary_domain(".google.com");
410 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
412 // Verify that a trailing period in the URL doesn't affect the check.
413 EXPECT_FALSE(AddSdchDictionary(dictionary_text,
414 GURL("http://www.subdomain.google.com.")));
417 // Make sure the order of the tests is not helping us or confusing things.
418 // See test CanSetExactMatchDictionary above for first try.
419 TEST_F(SdchManagerTest, CanStillSetExactMatchDictionary) {
420 std::string dictionary_domain("x.y.z.google.com");
421 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
423 // Perfect match should *STILL* work.
424 EXPECT_TRUE(AddSdchDictionary(dictionary_text,
425 GURL("http://" + dictionary_domain)));
428 // The following are only applicable while we have a latency test in the code,
429 // and can be removed when that functionality is stripped.
430 TEST_F(SdchManagerTest, LatencyTestControls) {
431 GURL url("http://www.google.com");
432 GURL url2("http://www.google2.com");
434 // First make sure we default to false.
435 EXPECT_FALSE(sdch_manager()->AllowLatencyExperiment(url));
436 EXPECT_FALSE(sdch_manager()->AllowLatencyExperiment(url2));
438 // That we can set each to true.
439 sdch_manager()->SetAllowLatencyExperiment(url, true);
440 EXPECT_TRUE(sdch_manager()->AllowLatencyExperiment(url));
441 EXPECT_FALSE(sdch_manager()->AllowLatencyExperiment(url2));
443 sdch_manager()->SetAllowLatencyExperiment(url2, true);
444 EXPECT_TRUE(sdch_manager()->AllowLatencyExperiment(url));
445 EXPECT_TRUE(sdch_manager()->AllowLatencyExperiment(url2));
447 // And can reset them to false.
448 sdch_manager()->SetAllowLatencyExperiment(url, false);
449 EXPECT_FALSE(sdch_manager()->AllowLatencyExperiment(url));
450 EXPECT_TRUE(sdch_manager()->AllowLatencyExperiment(url2));
452 sdch_manager()->SetAllowLatencyExperiment(url2, false);
453 EXPECT_FALSE(sdch_manager()->AllowLatencyExperiment(url));
454 EXPECT_FALSE(sdch_manager()->AllowLatencyExperiment(url2));
457 TEST_F(SdchManagerTest, CanUseMultipleManagers) {
458 SdchManager second_manager;
460 std::string dictionary_domain_1("x.y.z.google.com");
461 std::string dictionary_domain_2("x.y.z.chromium.org");
463 std::string dictionary_text_1(NewSdchDictionary(dictionary_domain_1));
464 std::string dictionary_text_2(NewSdchDictionary(dictionary_domain_2));
466 std::string tmp_hash;
467 std::string server_hash_1;
468 std::string server_hash_2;
470 SdchManager::GenerateHash(dictionary_text_1, &tmp_hash, &server_hash_1);
471 SdchManager::GenerateHash(dictionary_text_2, &tmp_hash, &server_hash_2);
473 // Confirm that if you add directories to one manager, you
474 // can't get them from the other.
475 EXPECT_TRUE(AddSdchDictionary(dictionary_text_1,
476 GURL("http://" + dictionary_domain_1)));
477 scoped_ptr<SdchManager::DictionarySet> dict_set;
479 SdchProblemCode problem_code;
480 dict_set = sdch_manager()->GetDictionarySetByHash(
481 GURL("http://" + dictionary_domain_1 + "/random_url"),
482 server_hash_1, &problem_code);
483 EXPECT_TRUE(dict_set);
484 EXPECT_TRUE(dict_set->GetDictionaryText(server_hash_1));
485 EXPECT_EQ(SDCH_OK, problem_code);
487 second_manager.AddSdchDictionary(
488 dictionary_text_2, GURL("http://" + dictionary_domain_2), nullptr);
489 dict_set = second_manager.GetDictionarySetByHash(
490 GURL("http://" + dictionary_domain_2 + "/random_url"),
491 server_hash_2, &problem_code);
492 EXPECT_TRUE(dict_set);
493 EXPECT_TRUE(dict_set->GetDictionaryText(server_hash_2));
494 EXPECT_EQ(SDCH_OK, problem_code);
496 dict_set = sdch_manager()->GetDictionarySetByHash(
497 GURL("http://" + dictionary_domain_2 + "/random_url"),
498 server_hash_2, &problem_code);
499 EXPECT_FALSE(dict_set);
500 EXPECT_EQ(SDCH_DICTIONARY_HASH_NOT_FOUND, problem_code);
502 dict_set = second_manager.GetDictionarySetByHash(
503 GURL("http://" + dictionary_domain_1 + "/random_url"),
504 server_hash_1, &problem_code);
505 EXPECT_FALSE(dict_set);
506 EXPECT_EQ(SDCH_DICTIONARY_HASH_NOT_FOUND, problem_code);
509 TEST_F(SdchManagerTest, ClearDictionaryData) {
510 std::string dictionary_domain("x.y.z.google.com");
511 GURL blacklist_url("http://bad.chromium.org");
513 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
514 std::string tmp_hash;
515 std::string server_hash;
517 SdchManager::GenerateHash(dictionary_text, &tmp_hash, &server_hash);
519 EXPECT_TRUE(AddSdchDictionary(dictionary_text,
520 GURL("http://" + dictionary_domain)));
522 scoped_ptr<SdchManager::DictionarySet> dict_set;
524 SdchProblemCode problem_code;
525 dict_set = sdch_manager()->GetDictionarySetByHash(
526 GURL("http://" + dictionary_domain + "/random_url"),
527 server_hash, &problem_code);
528 EXPECT_TRUE(dict_set);
529 EXPECT_TRUE(dict_set->GetDictionaryText(server_hash));
530 EXPECT_EQ(SDCH_OK, problem_code);
532 sdch_manager()->BlacklistDomain(GURL(blacklist_url), SDCH_OK);
533 EXPECT_EQ(SDCH_DOMAIN_BLACKLIST_INCLUDES_TARGET,
534 sdch_manager()->IsInSupportedDomain(blacklist_url));
536 sdch_manager()->ClearData();
538 dict_set = sdch_manager()->GetDictionarySetByHash(
539 GURL("http://" + dictionary_domain + "/random_url"),
540 server_hash, &problem_code);
541 EXPECT_FALSE(dict_set);
542 EXPECT_EQ(SDCH_DICTIONARY_HASH_NOT_FOUND, problem_code);
543 EXPECT_EQ(SDCH_OK, sdch_manager()->IsInSupportedDomain(blacklist_url));
546 TEST_F(SdchManagerTest, GetDictionaryNotification) {
547 GURL test_request_gurl(GURL("http://www.example.com/data"));
548 GURL test_dictionary_gurl(GURL("http://www.example.com/dict"));
549 MockSdchObserver observer;
550 sdch_manager()->AddObserver(&observer);
552 EXPECT_EQ(0, observer.get_dictionary_notifications());
553 sdch_manager()->OnGetDictionary(test_request_gurl, test_dictionary_gurl);
554 EXPECT_EQ(1, observer.get_dictionary_notifications());
555 EXPECT_EQ(test_request_gurl, observer.last_dictionary_request_url());
556 EXPECT_EQ(test_dictionary_gurl, observer.last_dictionary_url());
558 sdch_manager()->RemoveObserver(&observer);
559 sdch_manager()->OnGetDictionary(test_request_gurl, test_dictionary_gurl);
560 EXPECT_EQ(1, observer.get_dictionary_notifications());
561 EXPECT_EQ(test_request_gurl, observer.last_dictionary_request_url());
562 EXPECT_EQ(test_dictionary_gurl, observer.last_dictionary_url());
565 TEST_F(SdchManagerTest, ExpirationCheckedProperly) {
566 // Create an SDCH dictionary with an expiration time in the past.
567 std::string dictionary_domain("x.y.z.google.com");
568 std::string dictionary_text(base::StringPrintf("Domain: %s\nMax-age: -1\n\n",
569 dictionary_domain.c_str()));
570 dictionary_text.append(
571 kTestVcdiffDictionary, sizeof(kTestVcdiffDictionary) - 1);
572 std::string client_hash;
573 std::string server_hash;
574 SdchManager::GenerateHash(dictionary_text, &client_hash, &server_hash);
575 GURL target_gurl("http://" + dictionary_domain);
576 AddSdchDictionary(dictionary_text, target_gurl);
578 // It should be visible if looked up by hash whether expired or not.
579 SdchProblemCode problem_code;
580 scoped_ptr<SdchManager::DictionarySet> hash_set(
581 sdch_manager()->GetDictionarySetByHash(
582 target_gurl, server_hash, &problem_code).Pass());
583 ASSERT_TRUE(hash_set);
584 ASSERT_EQ(SDCH_OK, problem_code);
586 // Make sure it's not visible for advertisement, but is visible
587 // if looked up by hash.
588 EXPECT_FALSE(sdch_manager()->GetDictionarySet(target_gurl));
589 EXPECT_TRUE(sdch_manager()->GetDictionarySetByHash(
590 target_gurl, server_hash, &problem_code));
591 EXPECT_EQ(SDCH_OK, problem_code);
594 TEST_F(SdchManagerTest, SdchOnByDefault) {
595 GURL google_url("http://www.google.com");
596 scoped_ptr<SdchManager> sdch_manager(new SdchManager);
598 EXPECT_EQ(SDCH_OK, sdch_manager->IsInSupportedDomain(google_url));
599 SdchManager::EnableSdchSupport(false);
600 EXPECT_EQ(SDCH_DISABLED, sdch_manager->IsInSupportedDomain(google_url));
603 // Confirm dispatch of notification.
604 TEST_F(SdchManagerTest, SdchDictionaryUsed) {
605 MockSdchObserver observer;
606 sdch_manager()->AddObserver(&observer);
608 EXPECT_EQ(0, observer.dictionary_used_notifications());
609 sdch_manager()->OnDictionaryUsed("xyzzy");
610 EXPECT_EQ(1, observer.dictionary_used_notifications());
611 EXPECT_EQ("xyzzy", observer.last_server_hash());
613 std::string dictionary_domain("x.y.z.google.com");
614 GURL target_gurl("http://" + dictionary_domain);
615 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
616 std::string client_hash;
617 std::string server_hash;
618 SdchManager::GenerateHash(dictionary_text, &client_hash, &server_hash);
619 EXPECT_TRUE(AddSdchDictionary(dictionary_text, target_gurl));
620 EXPECT_EQ(1, observer.dictionary_used_notifications());
622 EXPECT_TRUE(sdch_manager()->GetDictionarySet(target_gurl));
623 EXPECT_EQ(1, observer.dictionary_used_notifications());
625 sdch_manager()->RemoveObserver(&observer);
626 EXPECT_EQ(1, observer.dictionary_used_notifications());
627 sdch_manager()->OnDictionaryUsed("plugh");
628 EXPECT_EQ(1, observer.dictionary_used_notifications());
631 TEST_F(SdchManagerTest, AddRemoveNotifications) {
632 MockSdchObserver observer;
633 sdch_manager()->AddObserver(&observer);
635 std::string dictionary_domain("x.y.z.google.com");
636 GURL target_gurl("http://" + dictionary_domain);
637 std::string dictionary_text(NewSdchDictionary(dictionary_domain));
638 std::string client_hash;
639 std::string server_hash;
640 SdchManager::GenerateHash(dictionary_text, &client_hash, &server_hash);
641 EXPECT_TRUE(AddSdchDictionary(dictionary_text, target_gurl));
642 EXPECT_EQ(1, observer.dictionary_added_notifications());
643 EXPECT_EQ(target_gurl, observer.last_dictionary_url());
644 EXPECT_EQ(server_hash, observer.last_server_hash());
646 EXPECT_EQ(SDCH_OK, sdch_manager()->RemoveSdchDictionary(server_hash));
647 EXPECT_EQ(1, observer.dictionary_removed_notifications());
648 EXPECT_EQ(server_hash, observer.last_server_hash());
650 sdch_manager()->RemoveObserver(&observer);
653 } // namespace net