Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / net / spdyproxy / data_reduction_proxy_chrome_configurator_unittest.cc
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 "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_configurator.h"
6
7 #include <string>
8
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"
19
20 class DataReductionProxyConfigTest : public testing::Test {
21  public:
22   void SetUp() override {
23     PrefRegistrySimple* registry = pref_service_.registry();
24     registry->RegisterDictionaryPref(prefs::kProxy);
25     config_.reset(new DataReductionProxyChromeConfigurator(
26         &pref_service_,
27         new base::TestSimpleTaskRunner()));
28   }
29
30   void CheckProxyConfig(
31       const std::string& expected_mode,
32       const std::string& expected_server,
33       const std::string& expected_bypass_list) {
34
35     const base::DictionaryValue* dict =
36         pref_service_.GetDictionary(prefs::kProxy);
37     std::string mode;
38     std::string server;
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);
46   }
47
48   scoped_ptr<DataReductionProxyChromeConfigurator> config_;
49   TestingPrefServiceSimple pref_service_;
50 };
51
52 TEST_F(DataReductionProxyConfigTest, TestUnrestricted) {
53   config_->Enable(false,
54                   false,
55                   "https://www.foo.com:443/",
56                   "http://www.bar.com:80/",
57                   "");
58   CheckProxyConfig(
59       "fixed_servers",
60       "http=https://www.foo.com:443,http://www.bar.com:80,direct://;",
61       "");
62 }
63
64 TEST_F(DataReductionProxyConfigTest, TestUnrestrictedSSL) {
65   config_->Enable(false,
66                   false,
67                   "https://www.foo.com:443/",
68                   "http://www.bar.com:80/",
69                   "http://www.ssl.com:80/");
70   CheckProxyConfig(
71       "fixed_servers",
72       "http=https://www.foo.com:443,http://www.bar.com:80,direct://;"
73       "https=http://www.ssl.com:80,direct://;",
74       "");
75 }
76
77 TEST_F(DataReductionProxyConfigTest, TestUnrestrictedWithBypassRule) {
78   config_->AddHostPatternToBypass("<local>");
79   config_->AddHostPatternToBypass("*.goo.com");
80   config_->Enable(false,
81                   false,
82                   "https://www.foo.com:443/",
83                   "http://www.bar.com:80/",
84                   "");
85   CheckProxyConfig(
86       "fixed_servers",
87       "http=https://www.foo.com:443,http://www.bar.com:80,direct://;",
88       "<local>, *.goo.com");
89 }
90
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://;",
95                    "");
96 }
97
98 TEST_F(DataReductionProxyConfigTest, TestRestricted) {
99   config_->Enable(true,
100                   false,
101                   "https://www.foo.com:443/",
102                   "http://www.bar.com:80/",
103                   "");
104   CheckProxyConfig("fixed_servers",
105                    "http=http://www.bar.com:80,direct://;",
106                    "");
107 }
108
109 TEST_F(DataReductionProxyConfigTest, TestFallbackRestricted) {
110   config_->Enable(false,
111                   true,
112                   "https://www.foo.com:443/",
113                   "http://www.bar.com:80/",
114                   "");
115   CheckProxyConfig("fixed_servers",
116                    "http=https://www.foo.com:443,direct://;",
117                    "");
118 }
119
120 TEST_F(DataReductionProxyConfigTest, TestBothRestricted) {
121   DictionaryPrefUpdate update(&pref_service_, prefs::kProxy);
122   base::DictionaryValue* dict = update.Get();
123   dict->SetString("mode", "system");
124
125   config_->Enable(true,
126                   true,
127                   "https://www.foo.com:443/",
128                   "http://www.bar.com:80/",
129                   "");
130   CheckProxyConfig("system", "", "");
131 }
132
133 TEST_F(DataReductionProxyConfigTest, TestDisable) {
134   data_reduction_proxy::DataReductionProxyParams params(
135       data_reduction_proxy::DataReductionProxyParams::
136           kAllowAllProxyConfigurations);
137   config_->Enable(false,
138                   false,
139                   params.origin().spec(),
140                   params.fallback_origin().spec(),
141                   "");
142   config_->Disable();
143   CheckProxyConfig("system", "", "");
144 }
145
146 TEST_F(DataReductionProxyConfigTest, TestDisableWithUserOverride) {
147   data_reduction_proxy::DataReductionProxyParams params(
148       data_reduction_proxy::DataReductionProxyParams::
149           kAllowAllProxyConfigurations);
150   config_->Enable(false,
151                   false,
152                   params.origin().spec(),
153                   params.fallback_origin().spec(),
154                   "");
155
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/");
160
161   // This should have no effect since proxy server was overridden.
162   config_->Disable();
163
164   CheckProxyConfig("fixed_servers", "https://www.baz.com:22/", "");
165 }
166
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/*");
174
175   std::string expected[] = {
176     "http://www.google.com",
177     "fefe:13::abc/33",
178     "foo.org",
179     "http://foo.com",
180     "http://baz.com:22",
181     "http://*bat.com"
182   };
183
184   ASSERT_EQ(config_->bypass_rules_.size(), 6u);
185   int i = 0;
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);
189   }
190 }
191