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/scoped_user_pref_update.h"
12 #include "base/prefs/testing_pref_service.h"
13 #include "base/test/test_simple_task_runner.h"
14 #include "base/values.h"
15 #include "chrome/common/pref_names.h"
16 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 class DataReductionProxyConfigTest : public testing::Test {
22 void SetUp() override {
23 PrefRegistrySimple* registry = pref_service_.registry();
24 registry->RegisterDictionaryPref(prefs::kProxy);
25 config_.reset(new DataReductionProxyChromeConfigurator(
27 new base::TestSimpleTaskRunner()));
30 void CheckProxyConfig(
31 const std::string& expected_mode,
32 const std::string& expected_server,
33 const std::string& expected_bypass_list) {
35 const base::DictionaryValue* dict =
36 pref_service_.GetDictionary(prefs::kProxy);
39 std::string bypass_list;
40 dict->GetString("mode", &mode);
41 ASSERT_EQ(expected_mode, mode);
42 dict->GetString("server", &server);
43 ASSERT_EQ(expected_server, server);
44 dict->GetString("bypass_list", &bypass_list);
45 ASSERT_EQ(expected_bypass_list, bypass_list);
48 scoped_ptr<DataReductionProxyChromeConfigurator> config_;
49 TestingPrefServiceSimple pref_service_;
52 TEST_F(DataReductionProxyConfigTest, TestUnrestricted) {
53 config_->Enable(false,
55 "https://www.foo.com:443/",
56 "http://www.bar.com:80/",
60 "http=https://www.foo.com:443,http://www.bar.com:80,direct://;",
64 TEST_F(DataReductionProxyConfigTest, TestUnrestrictedSSL) {
65 config_->Enable(false,
67 "https://www.foo.com:443/",
68 "http://www.bar.com:80/",
69 "http://www.ssl.com:80/");
72 "http=https://www.foo.com:443,http://www.bar.com:80,direct://;"
73 "https=http://www.ssl.com:80,direct://;",
77 TEST_F(DataReductionProxyConfigTest, TestUnrestrictedWithBypassRule) {
78 config_->AddHostPatternToBypass("<local>");
79 config_->AddHostPatternToBypass("*.goo.com");
80 config_->Enable(false,
82 "https://www.foo.com:443/",
83 "http://www.bar.com:80/",
87 "http=https://www.foo.com:443,http://www.bar.com:80,direct://;",
88 "<local>, *.goo.com");
91 TEST_F(DataReductionProxyConfigTest, TestUnrestrictedWithoutFallback) {
92 config_->Enable(false, false, "https://www.foo.com:443/", "", "");
93 CheckProxyConfig("fixed_servers",
94 "http=https://www.foo.com:443,direct://;",
98 TEST_F(DataReductionProxyConfigTest, TestRestricted) {
101 "https://www.foo.com:443/",
102 "http://www.bar.com:80/",
104 CheckProxyConfig("fixed_servers",
105 "http=http://www.bar.com:80,direct://;",
109 TEST_F(DataReductionProxyConfigTest, TestFallbackRestricted) {
110 config_->Enable(false,
112 "https://www.foo.com:443/",
113 "http://www.bar.com:80/",
115 CheckProxyConfig("fixed_servers",
116 "http=https://www.foo.com:443,direct://;",
120 TEST_F(DataReductionProxyConfigTest, TestBothRestricted) {
121 DictionaryPrefUpdate update(&pref_service_, prefs::kProxy);
122 base::DictionaryValue* dict = update.Get();
123 dict->SetString("mode", "system");
125 config_->Enable(true,
127 "https://www.foo.com:443/",
128 "http://www.bar.com:80/",
130 CheckProxyConfig("system", "", "");
133 TEST_F(DataReductionProxyConfigTest, TestDisable) {
134 data_reduction_proxy::DataReductionProxyParams params(
135 data_reduction_proxy::DataReductionProxyParams::
136 kAllowAllProxyConfigurations);
137 config_->Enable(false,
139 params.origin().spec(),
140 params.fallback_origin().spec(),
143 CheckProxyConfig("system", "", "");
146 TEST_F(DataReductionProxyConfigTest, TestDisableWithUserOverride) {
147 data_reduction_proxy::DataReductionProxyParams params(
148 data_reduction_proxy::DataReductionProxyParams::
149 kAllowAllProxyConfigurations);
150 config_->Enable(false,
152 params.origin().spec(),
153 params.fallback_origin().spec(),
156 // Override the data reduction proxy.
157 DictionaryPrefUpdate update(&pref_service_, prefs::kProxy);
158 base::DictionaryValue* dict = update.Get();
159 dict->SetString("server", "https://www.baz.com:22/");
161 // This should have no effect since proxy server was overridden.
164 CheckProxyConfig("fixed_servers", "https://www.baz.com:22/", "");
167 TEST_F(DataReductionProxyConfigTest, TestBypassList) {
168 config_->AddHostPatternToBypass("http://www.google.com");
169 config_->AddHostPatternToBypass("fefe:13::abc/33");
170 config_->AddURLPatternToBypass("foo.org/images/*");
171 config_->AddURLPatternToBypass("http://foo.com/*");
172 config_->AddURLPatternToBypass("http://baz.com:22/bar/*");
173 config_->AddURLPatternToBypass("http://*bat.com/bar/*");
175 std::string expected[] = {
176 "http://www.google.com",
184 ASSERT_EQ(config_->bypass_rules_.size(), 6u);
186 for (std::vector<std::string>::iterator it = config_->bypass_rules_.begin();
187 it != config_->bypass_rules_.end(); ++it) {
188 EXPECT_EQ(expected[i++], *it);