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 "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_configurator.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/testing_pref_service.h"
12 #include "base/test/test_simple_task_runner.h"
13 #include "base/values.h"
14 #include "chrome/common/pref_names.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 class DataReductionProxyConfigTest : public testing::Test {
20 virtual void SetUp() {
21 PrefRegistrySimple* registry = pref_service_.registry();
22 registry->RegisterDictionaryPref(prefs::kProxy);
23 config_.reset(new DataReductionProxyChromeConfigurator(
25 new base::TestSimpleTaskRunner()));
28 void CheckProxyConfig(
29 const std::string& expected_mode,
30 const std::string& expected_server,
31 const std::string& expected_bypass_list) {
33 const base::DictionaryValue* dict =
34 pref_service_.GetDictionary(prefs::kProxy);
37 std::string bypass_list;
38 dict->GetString("mode", &mode);
39 ASSERT_EQ(expected_mode, mode);
40 dict->GetString("server", &server);
41 ASSERT_EQ(expected_server, server);
42 dict->GetString("bypass_list", &bypass_list);
43 ASSERT_EQ(expected_bypass_list, bypass_list);
46 scoped_ptr<DataReductionProxyChromeConfigurator> config_;
47 TestingPrefServiceSimple pref_service_;
50 TEST_F(DataReductionProxyConfigTest, TestUnrestricted) {
51 config_->Enable(false,
53 "https://www.foo.com:443/",
54 "http://www.bar.com:80/",
58 "http=https://www.foo.com:443,http://www.bar.com:80,direct://;",
62 TEST_F(DataReductionProxyConfigTest, TestUnrestrictedSSL) {
63 config_->Enable(false,
65 "https://www.foo.com:443/",
66 "http://www.bar.com:80/",
67 "http://www.ssl.com:80/");
70 "http=https://www.foo.com:443,http://www.bar.com:80,direct://;"
71 "https=http://www.ssl.com:80,direct://;",
75 TEST_F(DataReductionProxyConfigTest, TestUnrestrictedWithBypassRule) {
76 config_->AddHostPatternToBypass("<local>");
77 config_->AddHostPatternToBypass("*.goo.com");
78 config_->Enable(false,
80 "https://www.foo.com:443/",
81 "http://www.bar.com:80/",
85 "http=https://www.foo.com:443,http://www.bar.com:80,direct://;",
86 "<local>, *.goo.com");
89 TEST_F(DataReductionProxyConfigTest, TestUnrestrictedWithoutFallback) {
90 config_->Enable(false, false, "https://www.foo.com:443/", "", "");
91 CheckProxyConfig("fixed_servers",
92 "http=https://www.foo.com:443,direct://;",
96 TEST_F(DataReductionProxyConfigTest, TestRestricted) {
99 "https://www.foo.com:443/",
100 "http://www.bar.com:80/",
102 CheckProxyConfig("fixed_servers",
103 "http=http://www.bar.com:80,direct://;",
107 TEST_F(DataReductionProxyConfigTest, TestFallbackRestricted) {
108 config_->Enable(false,
110 "https://www.foo.com:443/",
111 "http://www.bar.com:80/",
113 CheckProxyConfig("fixed_servers",
114 "http=https://www.foo.com:443,direct://;",
118 TEST_F(DataReductionProxyConfigTest, TestBothRestricted) {
119 config_->Enable(true,
121 "https://www.foo.com:443/",
122 "http://www.bar.com:80/",
124 CheckProxyConfig("system", "", "");
127 TEST_F(DataReductionProxyConfigTest, TestDisable) {
129 CheckProxyConfig("system", "", "");
133 TEST_F(DataReductionProxyConfigTest, TestBypassList) {
134 config_->AddHostPatternToBypass("http://www.google.com");
135 config_->AddHostPatternToBypass("fefe:13::abc/33");
136 config_->AddURLPatternToBypass("foo.org/images/*");
137 config_->AddURLPatternToBypass("http://foo.com/*");
138 config_->AddURLPatternToBypass("http://baz.com:22/bar/*");
139 config_->AddURLPatternToBypass("http://*bat.com/bar/*");
141 std::string expected[] = {
142 "http://www.google.com",
150 ASSERT_EQ(config_->bypass_rules_.size(), 6u);
152 for (std::vector<std::string>::iterator it = config_->bypass_rules_.begin();
153 it != config_->bypass_rules_.end(); ++it) {
154 EXPECT_EQ(expected[i++], *it);