1 // Copyright 2015 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.
7 #include "base/memory/scoped_ptr.h"
8 #include "base/prefs/pref_registry_simple.h"
9 #include "base/prefs/testing_pref_service.h"
10 #include "base/test/histogram_tester.h"
11 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
12 #include "chrome/common/pref_names.h"
13 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config_test_utils.h"
14 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h"
15 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
16 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params_test_utils.h"
17 #include "testing/gtest/include/gtest/gtest.h"
20 using testing::Return
;
22 class DataReductionProxyChromeSettingsTest
: public testing::Test
{
24 void SetUp() override
{
25 drp_chrome_settings_
=
26 make_scoped_ptr(new DataReductionProxyChromeSettings());
28 data_reduction_proxy::DataReductionProxyTestContext::Builder()
30 .SkipSettingsInitialization()
32 config_
= test_context_
->mock_config();
33 drp_chrome_settings_
->ResetConfigForTest(config_
);
34 dict_
= make_scoped_ptr(new base::DictionaryValue());
36 PrefRegistrySimple
* registry
= test_context_
->pref_service()->registry();
37 registry
->RegisterDictionaryPref(prefs::kProxy
);
40 base::MessageLoopForIO message_loop_
;
41 scoped_ptr
<DataReductionProxyChromeSettings
> drp_chrome_settings_
;
42 scoped_ptr
<base::DictionaryValue
> dict_
;
43 scoped_ptr
<data_reduction_proxy::DataReductionProxyTestContext
> test_context_
;
44 data_reduction_proxy::MockDataReductionProxyConfig
* config_
;
47 TEST_F(DataReductionProxyChromeSettingsTest
, MigrateNonexistentProxyPref
) {
48 base::HistogramTester histogram_tester
;
49 EXPECT_CALL(*config_
, ContainsDataReductionProxy(_
)).Times(0);
50 drp_chrome_settings_
->MigrateDataReductionProxyOffProxyPrefs(
51 test_context_
->pref_service());
53 EXPECT_EQ(NULL
, test_context_
->pref_service()->GetUserPref(prefs::kProxy
));
54 histogram_tester
.ExpectUniqueSample(
55 "DataReductionProxy.ProxyPrefMigrationResult",
56 DataReductionProxyChromeSettings::PROXY_PREF_NOT_CLEARED
, 1);
59 TEST_F(DataReductionProxyChromeSettingsTest
, MigrateBadlyFormedProxyPref
) {
61 // NULL indicates that mode is unset.
62 const char* proxy_mode_string
;
63 // NULL indicates that server is unset.
64 const char* proxy_server_string
;
66 // The pref should not be cleared if mode is unset.
67 {nullptr, "http=compress.googlezip.net"},
68 // The pref should not be cleared for modes other than "fixed_servers" and
70 {"auto_detect", "http=compress.googlezip.net"},
71 // The pref should not be cleared when the server field is unset.
72 {"fixed_servers", nullptr},
75 for (const auto& test
: test_cases
) {
76 base::HistogramTester histogram_tester
;
77 dict_
.reset(new base::DictionaryValue());
78 if (test
.proxy_mode_string
)
79 dict_
->SetString("mode", test
.proxy_mode_string
);
80 if (test
.proxy_server_string
)
81 dict_
->SetString("server", test
.proxy_server_string
);
82 test_context_
->pref_service()->Set(prefs::kProxy
, *dict_
.get());
84 EXPECT_CALL(*config_
, ContainsDataReductionProxy(_
)).Times(0);
85 drp_chrome_settings_
->MigrateDataReductionProxyOffProxyPrefs(
86 test_context_
->pref_service());
88 const base::DictionaryValue
* final_value
;
89 test_context_
->pref_service()
90 ->GetUserPref(prefs::kProxy
)
91 ->GetAsDictionary(&final_value
);
92 EXPECT_NE(nullptr, final_value
);
93 EXPECT_TRUE(dict_
->Equals(final_value
));
95 histogram_tester
.ExpectUniqueSample(
96 "DataReductionProxy.ProxyPrefMigrationResult",
97 DataReductionProxyChromeSettings::PROXY_PREF_NOT_CLEARED
, 1);
101 TEST_F(DataReductionProxyChromeSettingsTest
, MigrateEmptyProxy
) {
102 base::HistogramTester histogram_tester
;
103 test_context_
->pref_service()->Set(prefs::kProxy
, *dict_
.get());
104 EXPECT_CALL(*config_
, ContainsDataReductionProxy(_
)).Times(0);
105 drp_chrome_settings_
->MigrateDataReductionProxyOffProxyPrefs(
106 test_context_
->pref_service());
108 EXPECT_EQ(NULL
, test_context_
->pref_service()->GetUserPref(prefs::kProxy
));
109 histogram_tester
.ExpectUniqueSample(
110 "DataReductionProxy.ProxyPrefMigrationResult",
111 DataReductionProxyChromeSettings::PROXY_PREF_CLEARED_EMPTY
, 1);
114 TEST_F(DataReductionProxyChromeSettingsTest
, MigrateSystemProxy
) {
115 base::HistogramTester histogram_tester
;
116 dict_
->SetString("mode", "system");
117 test_context_
->pref_service()->Set(prefs::kProxy
, *dict_
.get());
118 EXPECT_CALL(*config_
, ContainsDataReductionProxy(_
)).Times(0);
120 drp_chrome_settings_
->MigrateDataReductionProxyOffProxyPrefs(
121 test_context_
->pref_service());
123 EXPECT_EQ(NULL
, test_context_
->pref_service()->GetUserPref(prefs::kProxy
));
124 histogram_tester
.ExpectUniqueSample(
125 "DataReductionProxy.ProxyPrefMigrationResult",
126 DataReductionProxyChromeSettings::PROXY_PREF_CLEARED_MODE_SYSTEM
, 1);
129 TEST_F(DataReductionProxyChromeSettingsTest
, MigrateDataReductionProxy
) {
130 const std::string kTestServers
[] = {"http=http://proxy.googlezip.net",
131 "http=https://my-drp.org",
132 "https=https://tunneldrp.com"};
134 for (const std::string
& test_server
: kTestServers
) {
135 base::HistogramTester histogram_tester
;
136 dict_
.reset(new base::DictionaryValue());
137 dict_
->SetString("mode", "fixed_servers");
138 dict_
->SetString("server", test_server
);
139 test_context_
->pref_service()->Set(prefs::kProxy
, *dict_
.get());
140 EXPECT_CALL(*config_
, ContainsDataReductionProxy(_
))
142 .WillOnce(Return(true));
144 drp_chrome_settings_
->MigrateDataReductionProxyOffProxyPrefs(
145 test_context_
->pref_service());
147 EXPECT_EQ(NULL
, test_context_
->pref_service()->GetUserPref(prefs::kProxy
));
148 histogram_tester
.ExpectUniqueSample(
149 "DataReductionProxy.ProxyPrefMigrationResult",
150 DataReductionProxyChromeSettings::PROXY_PREF_CLEARED_DRP
, 1);
154 TEST_F(DataReductionProxyChromeSettingsTest
,
155 MigrateGooglezipDataReductionProxy
) {
156 const std::string kTestServers
[] = {
157 "http=http://proxy-dev.googlezip.net",
158 "http=https://arbitraryprefix.googlezip.net",
159 "https=https://tunnel.googlezip.net"};
161 for (const std::string
& test_server
: kTestServers
) {
162 base::HistogramTester histogram_tester
;
163 dict_
.reset(new base::DictionaryValue());
164 // The proxy pref is set to a Data Reduction Proxy that doesn't match the
165 // currently configured DRP, but the pref should still be cleared.
166 dict_
->SetString("mode", "fixed_servers");
167 dict_
->SetString("server", test_server
);
168 test_context_
->pref_service()->Set(prefs::kProxy
, *dict_
.get());
169 EXPECT_CALL(*config_
, ContainsDataReductionProxy(_
))
171 .WillOnce(Return(false));
173 drp_chrome_settings_
->MigrateDataReductionProxyOffProxyPrefs(
174 test_context_
->pref_service());
176 EXPECT_EQ(NULL
, test_context_
->pref_service()->GetUserPref(prefs::kProxy
));
177 histogram_tester
.ExpectUniqueSample(
178 "DataReductionProxy.ProxyPrefMigrationResult",
179 DataReductionProxyChromeSettings::PROXY_PREF_CLEARED_GOOGLEZIP
, 1);
183 TEST_F(DataReductionProxyChromeSettingsTest
,
184 MigratePacGooglezipDataReductionProxy
) {
187 bool expect_pref_cleared
;
189 // PAC with bypass rules that returns 'HTTPS proxy.googlezip.net:443;
190 // PROXY compress.googlezip.net:80; DIRECT'.
192 "x-ns-proxy-autoconfig;base64,"
193 "ZnVuY3Rpb24gRmluZFByb3h5Rm9yVVJMKHVybCwgaG9zdCkgeyAgaWYgKChzaEV4cE1hdGN"
194 "oKHVybCwgJ2h0dHA6Ly93d3cuZ29vZ2xlLmNvbS9wb2xpY2llcy9wcml2YWN5KicpKSkgey"
195 "AgICByZXR1cm4gJ0RJUkVDVCc7ICB9ICAgaWYgKHVybC5zdWJzdHJpbmcoMCwgNSkgPT0gJ"
196 "2h0dHA6JykgeyAgICByZXR1cm4gJ0hUVFBTIHByb3h5Lmdvb2dsZXppcC5uZXQ6NDQzOyBQ"
197 "Uk9YWSBjb21wcmVzcy5nb29nbGV6aXAubmV0OjgwOyBESVJFQ1QnOyAgfSAgcmV0dXJuICd"
200 // PAC with bypass rules that returns 'PROXY compress.googlezip.net:80;
203 "x-ns-proxy-autoconfig;base64,"
204 "ZnVuY3Rpb24gRmluZFByb3h5Rm9yVVJMKHVybCwgaG9zdCkgeyAgaWYgKChzaEV4cE1hdGN"
205 "oKHVybCwgJ2h0dHA6Ly93d3cuZ29vZ2xlLmNvbS9wb2xpY2llcy9wcml2YWN5KicpKSkgey"
206 "AgICByZXR1cm4gJ0RJUkVDVCc7ICB9ICAgaWYgKHVybC5zdWJzdHJpbmcoMCwgNSkgPT0gJ"
207 "2h0dHA6JykgeyAgICByZXR1cm4gJ1BST1hZIGNvbXByZXNzLmdvb2dsZXppcC5uZXQ6ODA7"
208 "IERJUkVDVCc7ICB9ICByZXR1cm4gJ0RJUkVDVCc7fQ==",
210 // PAC with bypass rules that returns 'PROXY proxy-dev.googlezip.net:80;
213 "x-ns-proxy-autoconfig;base64,"
214 "ZnVuY3Rpb24gRmluZFByb3h5Rm9yVVJMKHVybCwgaG9zdCkgeyAgaWYgKChzaEV4cE1hdGN"
215 "oKHVybCwgJ2h0dHA6Ly93d3cuZ29vZ2xlLmNvbS9wb2xpY2llcy9wcml2YWN5KicpKSkgey"
216 "AgICByZXR1cm4gJ0RJUkVDVCc7ICB9ICAgaWYgKHVybC5zdWJzdHJpbmcoMCwgNSkgPT0gJ"
217 "2h0dHA6JykgeyAgICByZXR1cm4gJ1BST1hZIHByb3h5LWRldi5nb29nbGV6aXAubmV0Ojgw"
218 "OyBESVJFQ1QnOyAgfSAgcmV0dXJuICdESVJFQ1QnO30=",
220 // Simple PAC that returns 'PROXY compress.googlezip.net:80'.
222 "x-ns-proxy-autoconfig;base64,"
223 "ZnVuY3Rpb24gRmluZFByb3h5Rm9yVVJMKHVybCwgaG9zdCkge3JldHVybiAnUFJPWFkgY29"
224 "tcHJlc3MuZ29vZ2xlemlwLm5ldDo4MCc7fQo=",
226 // Simple PAC that returns 'PROXY compress.googlezip.net'. Note that since
227 // the port is not specified, the pref will not be cleared.
229 "x-ns-proxy-autoconfig;base64,"
230 "ZnVuY3Rpb24gRmluZFByb3h5Rm9yVVJMKHVybCwgaG9zdCkge3JldHVybiAnUFJPWFkgY29"
231 "tcHJlc3MuZ29vZ2xlemlwLm5ldCc7fQ==",
233 // Simple PAC that returns 'PROXY mycustomdrp.net:80'.
235 "x-ns-proxy-autoconfig;base64,"
236 "ZnVuY3Rpb24gRmluZFByb3h5Rm9yVVJMKHVybCwgaG9zdCkge3JldHVybiAnUFJPWFkgb3J"
237 "pZ2luLm5ldDo4MCc7fQo=",
239 // Simple PAC that returns 'PROXY myprefixgooglezip.net:80'.
241 "x-ns-proxy-autoconfig;base64,"
242 "ZnVuY3Rpb24gRmluZFByb3h5Rm9yVVJMKHVybCwgaG9zdCkge3JldHVybiAnUFJPWFkgbXl"
243 "wcmVmaXhnb29nbGV6aXAubmV0OjgwJzt9Cg==",
245 // Simple PAC that returns 'PROXY compress.googlezip.net.mydomain.com:80'.
247 "x-ns-proxy-autoconfig;base64,"
248 "ZnVuY3Rpb24gRmluZFByb3h5Rm9yVVJMKHVybCwgaG9zdCkge3JldHVybiAnUFJPWFkgY29"
249 "tcHJlc3MuZ29vZ2xlemlwLm5ldC5teWRvbWFpbi5jb206ODAnO30K",
251 // PAC URL that doesn't embed a script.
252 {"http://compress.googlezip.net/pac", false},
255 for (const auto& test
: test_cases
) {
256 base::HistogramTester histogram_tester
;
257 dict_
.reset(new base::DictionaryValue());
258 dict_
->SetString("mode", "pac_script");
259 dict_
->SetString("pac_url", test
.pac_url
);
260 test_context_
->pref_service()->Set(prefs::kProxy
, *dict_
.get());
261 EXPECT_CALL(*config_
, ContainsDataReductionProxy(_
)).Times(0);
263 drp_chrome_settings_
->MigrateDataReductionProxyOffProxyPrefs(
264 test_context_
->pref_service());
266 if (test
.expect_pref_cleared
) {
268 test_context_
->pref_service()->GetUserPref(prefs::kProxy
));
269 histogram_tester
.ExpectUniqueSample(
270 "DataReductionProxy.ProxyPrefMigrationResult",
271 DataReductionProxyChromeSettings::PROXY_PREF_CLEARED_PAC_GOOGLEZIP
,
274 const base::DictionaryValue
* value
;
275 EXPECT_TRUE(test_context_
->pref_service()
276 ->GetUserPref(prefs::kProxy
)
277 ->GetAsDictionary(&value
));
279 EXPECT_TRUE(value
->GetString("mode", &mode
));
280 EXPECT_EQ("pac_script", mode
);
282 EXPECT_TRUE(value
->GetString("pac_url", &pac_url
));
283 EXPECT_EQ(test
.pac_url
, pac_url
);
285 histogram_tester
.ExpectUniqueSample(
286 "DataReductionProxy.ProxyPrefMigrationResult",
287 DataReductionProxyChromeSettings::PROXY_PREF_NOT_CLEARED
, 1);
292 TEST_F(DataReductionProxyChromeSettingsTest
, MigrateIgnoreOtherProxy
) {
293 const std::string kTestServers
[] = {
294 "http=https://youtube.com",
295 "http=http://googlezip.net",
296 "http=http://thisismyproxynotgooglezip.net",
297 "https=http://arbitraryprefixgooglezip.net"};
299 for (const std::string
& test_server
: kTestServers
) {
300 base::HistogramTester histogram_tester
;
301 dict_
.reset(new base::DictionaryValue());
302 dict_
->SetString("mode", "fixed_servers");
303 dict_
->SetString("server", test_server
);
304 test_context_
->pref_service()->Set(prefs::kProxy
, *dict_
.get());
305 EXPECT_CALL(*config_
, ContainsDataReductionProxy(_
))
307 .WillOnce(Return(false));
309 drp_chrome_settings_
->MigrateDataReductionProxyOffProxyPrefs(
310 test_context_
->pref_service());
312 base::DictionaryValue
* value
=
313 (base::DictionaryValue
*)test_context_
->pref_service()->GetUserPref(
316 EXPECT_TRUE(value
->GetString("mode", &mode
));
317 EXPECT_EQ("fixed_servers", mode
);
319 EXPECT_TRUE(value
->GetString("server", &server
));
320 EXPECT_EQ(test_server
, server
);
322 histogram_tester
.ExpectUniqueSample(
323 "DataReductionProxy.ProxyPrefMigrationResult",
324 DataReductionProxyChromeSettings::PROXY_PREF_NOT_CLEARED
, 1);