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 "components/data_reduction_proxy/core/browser/data_reduction_proxy_config_test_utils.h"
13 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h"
14 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
15 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params_test_utils.h"
16 #include "components/proxy_config/proxy_config_pref_names.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(proxy_config::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(
54 proxy_config::prefs::kProxy
));
55 histogram_tester
.ExpectUniqueSample(
56 "DataReductionProxy.ProxyPrefMigrationResult",
57 DataReductionProxyChromeSettings::PROXY_PREF_NOT_CLEARED
, 1);
60 TEST_F(DataReductionProxyChromeSettingsTest
, MigrateBadlyFormedProxyPref
) {
62 // NULL indicates that mode is unset.
63 const char* proxy_mode_string
;
64 // NULL indicates that server is unset.
65 const char* proxy_server_string
;
67 // The pref should not be cleared if mode is unset.
68 {nullptr, "http=compress.googlezip.net"},
69 // The pref should not be cleared for modes other than "fixed_servers" and
71 {"auto_detect", "http=compress.googlezip.net"},
72 // The pref should not be cleared when the server field is unset.
73 {"fixed_servers", nullptr},
76 for (const auto& test
: test_cases
) {
77 base::HistogramTester histogram_tester
;
78 dict_
.reset(new base::DictionaryValue());
79 if (test
.proxy_mode_string
)
80 dict_
->SetString("mode", test
.proxy_mode_string
);
81 if (test
.proxy_server_string
)
82 dict_
->SetString("server", test
.proxy_server_string
);
83 test_context_
->pref_service()->Set(proxy_config::prefs::kProxy
,
86 EXPECT_CALL(*config_
, ContainsDataReductionProxy(_
)).Times(0);
87 drp_chrome_settings_
->MigrateDataReductionProxyOffProxyPrefs(
88 test_context_
->pref_service());
90 const base::DictionaryValue
* final_value
;
91 test_context_
->pref_service()
92 ->GetUserPref(proxy_config::prefs::kProxy
)
93 ->GetAsDictionary(&final_value
);
94 EXPECT_NE(nullptr, final_value
);
95 EXPECT_TRUE(dict_
->Equals(final_value
));
97 histogram_tester
.ExpectUniqueSample(
98 "DataReductionProxy.ProxyPrefMigrationResult",
99 DataReductionProxyChromeSettings::PROXY_PREF_NOT_CLEARED
, 1);
103 TEST_F(DataReductionProxyChromeSettingsTest
, MigrateEmptyProxy
) {
104 base::HistogramTester histogram_tester
;
105 test_context_
->pref_service()->Set(proxy_config::prefs::kProxy
, *dict_
.get());
106 EXPECT_CALL(*config_
, ContainsDataReductionProxy(_
)).Times(0);
107 drp_chrome_settings_
->MigrateDataReductionProxyOffProxyPrefs(
108 test_context_
->pref_service());
110 EXPECT_EQ(NULL
, test_context_
->pref_service()->GetUserPref(
111 proxy_config::prefs::kProxy
));
112 histogram_tester
.ExpectUniqueSample(
113 "DataReductionProxy.ProxyPrefMigrationResult",
114 DataReductionProxyChromeSettings::PROXY_PREF_CLEARED_EMPTY
, 1);
117 TEST_F(DataReductionProxyChromeSettingsTest
, MigrateSystemProxy
) {
118 base::HistogramTester histogram_tester
;
119 dict_
->SetString("mode", "system");
120 test_context_
->pref_service()->Set(proxy_config::prefs::kProxy
, *dict_
.get());
121 EXPECT_CALL(*config_
, ContainsDataReductionProxy(_
)).Times(0);
123 drp_chrome_settings_
->MigrateDataReductionProxyOffProxyPrefs(
124 test_context_
->pref_service());
126 EXPECT_EQ(NULL
, test_context_
->pref_service()->GetUserPref(
127 proxy_config::prefs::kProxy
));
128 histogram_tester
.ExpectUniqueSample(
129 "DataReductionProxy.ProxyPrefMigrationResult",
130 DataReductionProxyChromeSettings::PROXY_PREF_CLEARED_MODE_SYSTEM
, 1);
133 TEST_F(DataReductionProxyChromeSettingsTest
, MigrateDataReductionProxy
) {
134 const std::string kTestServers
[] = {"http=http://proxy.googlezip.net",
135 "http=https://my-drp.org",
136 "https=https://tunneldrp.com"};
138 for (const std::string
& test_server
: kTestServers
) {
139 base::HistogramTester histogram_tester
;
140 dict_
.reset(new base::DictionaryValue());
141 dict_
->SetString("mode", "fixed_servers");
142 dict_
->SetString("server", test_server
);
143 test_context_
->pref_service()->Set(proxy_config::prefs::kProxy
,
145 EXPECT_CALL(*config_
, ContainsDataReductionProxy(_
))
147 .WillOnce(Return(true));
149 drp_chrome_settings_
->MigrateDataReductionProxyOffProxyPrefs(
150 test_context_
->pref_service());
152 EXPECT_EQ(NULL
, test_context_
->pref_service()->GetUserPref(
153 proxy_config::prefs::kProxy
));
154 histogram_tester
.ExpectUniqueSample(
155 "DataReductionProxy.ProxyPrefMigrationResult",
156 DataReductionProxyChromeSettings::PROXY_PREF_CLEARED_DRP
, 1);
160 TEST_F(DataReductionProxyChromeSettingsTest
,
161 MigrateGooglezipDataReductionProxy
) {
162 const std::string kTestServers
[] = {
163 "http=http://proxy-dev.googlezip.net",
164 "http=https://arbitraryprefix.googlezip.net",
165 "https=https://tunnel.googlezip.net"};
167 for (const std::string
& test_server
: kTestServers
) {
168 base::HistogramTester histogram_tester
;
169 dict_
.reset(new base::DictionaryValue());
170 // The proxy pref is set to a Data Reduction Proxy that doesn't match the
171 // currently configured DRP, but the pref should still be cleared.
172 dict_
->SetString("mode", "fixed_servers");
173 dict_
->SetString("server", test_server
);
174 test_context_
->pref_service()->Set(proxy_config::prefs::kProxy
,
176 EXPECT_CALL(*config_
, ContainsDataReductionProxy(_
))
178 .WillOnce(Return(false));
180 drp_chrome_settings_
->MigrateDataReductionProxyOffProxyPrefs(
181 test_context_
->pref_service());
183 EXPECT_EQ(NULL
, test_context_
->pref_service()->GetUserPref(
184 proxy_config::prefs::kProxy
));
185 histogram_tester
.ExpectUniqueSample(
186 "DataReductionProxy.ProxyPrefMigrationResult",
187 DataReductionProxyChromeSettings::PROXY_PREF_CLEARED_GOOGLEZIP
, 1);
191 TEST_F(DataReductionProxyChromeSettingsTest
,
192 MigratePacGooglezipDataReductionProxy
) {
195 bool expect_pref_cleared
;
197 // PAC with bypass rules that returns 'HTTPS proxy.googlezip.net:443;
198 // PROXY compress.googlezip.net:80; DIRECT'.
200 "x-ns-proxy-autoconfig;base64,"
201 "ZnVuY3Rpb24gRmluZFByb3h5Rm9yVVJMKHVybCwgaG9zdCkgeyAgaWYgKChzaEV4cE1hdGN"
202 "oKHVybCwgJ2h0dHA6Ly93d3cuZ29vZ2xlLmNvbS9wb2xpY2llcy9wcml2YWN5KicpKSkgey"
203 "AgICByZXR1cm4gJ0RJUkVDVCc7ICB9ICAgaWYgKHVybC5zdWJzdHJpbmcoMCwgNSkgPT0gJ"
204 "2h0dHA6JykgeyAgICByZXR1cm4gJ0hUVFBTIHByb3h5Lmdvb2dsZXppcC5uZXQ6NDQzOyBQ"
205 "Uk9YWSBjb21wcmVzcy5nb29nbGV6aXAubmV0OjgwOyBESVJFQ1QnOyAgfSAgcmV0dXJuICd"
208 // PAC with bypass rules that returns 'PROXY compress.googlezip.net:80;
211 "x-ns-proxy-autoconfig;base64,"
212 "ZnVuY3Rpb24gRmluZFByb3h5Rm9yVVJMKHVybCwgaG9zdCkgeyAgaWYgKChzaEV4cE1hdGN"
213 "oKHVybCwgJ2h0dHA6Ly93d3cuZ29vZ2xlLmNvbS9wb2xpY2llcy9wcml2YWN5KicpKSkgey"
214 "AgICByZXR1cm4gJ0RJUkVDVCc7ICB9ICAgaWYgKHVybC5zdWJzdHJpbmcoMCwgNSkgPT0gJ"
215 "2h0dHA6JykgeyAgICByZXR1cm4gJ1BST1hZIGNvbXByZXNzLmdvb2dsZXppcC5uZXQ6ODA7"
216 "IERJUkVDVCc7ICB9ICByZXR1cm4gJ0RJUkVDVCc7fQ==",
218 // PAC with bypass rules that returns 'PROXY proxy-dev.googlezip.net:80;
221 "x-ns-proxy-autoconfig;base64,"
222 "ZnVuY3Rpb24gRmluZFByb3h5Rm9yVVJMKHVybCwgaG9zdCkgeyAgaWYgKChzaEV4cE1hdGN"
223 "oKHVybCwgJ2h0dHA6Ly93d3cuZ29vZ2xlLmNvbS9wb2xpY2llcy9wcml2YWN5KicpKSkgey"
224 "AgICByZXR1cm4gJ0RJUkVDVCc7ICB9ICAgaWYgKHVybC5zdWJzdHJpbmcoMCwgNSkgPT0gJ"
225 "2h0dHA6JykgeyAgICByZXR1cm4gJ1BST1hZIHByb3h5LWRldi5nb29nbGV6aXAubmV0Ojgw"
226 "OyBESVJFQ1QnOyAgfSAgcmV0dXJuICdESVJFQ1QnO30=",
228 // Simple PAC that returns 'PROXY compress.googlezip.net:80'.
230 "x-ns-proxy-autoconfig;base64,"
231 "ZnVuY3Rpb24gRmluZFByb3h5Rm9yVVJMKHVybCwgaG9zdCkge3JldHVybiAnUFJPWFkgY29"
232 "tcHJlc3MuZ29vZ2xlemlwLm5ldDo4MCc7fQo=",
234 // Simple PAC that returns 'PROXY compress.googlezip.net'. Note that since
235 // the port is not specified, the pref will not be cleared.
237 "x-ns-proxy-autoconfig;base64,"
238 "ZnVuY3Rpb24gRmluZFByb3h5Rm9yVVJMKHVybCwgaG9zdCkge3JldHVybiAnUFJPWFkgY29"
239 "tcHJlc3MuZ29vZ2xlemlwLm5ldCc7fQ==",
241 // Simple PAC that returns 'PROXY mycustomdrp.net:80'.
243 "x-ns-proxy-autoconfig;base64,"
244 "ZnVuY3Rpb24gRmluZFByb3h5Rm9yVVJMKHVybCwgaG9zdCkge3JldHVybiAnUFJPWFkgb3J"
245 "pZ2luLm5ldDo4MCc7fQo=",
247 // Simple PAC that returns 'PROXY myprefixgooglezip.net:80'.
249 "x-ns-proxy-autoconfig;base64,"
250 "ZnVuY3Rpb24gRmluZFByb3h5Rm9yVVJMKHVybCwgaG9zdCkge3JldHVybiAnUFJPWFkgbXl"
251 "wcmVmaXhnb29nbGV6aXAubmV0OjgwJzt9Cg==",
253 // Simple PAC that returns 'PROXY compress.googlezip.net.mydomain.com:80'.
255 "x-ns-proxy-autoconfig;base64,"
256 "ZnVuY3Rpb24gRmluZFByb3h5Rm9yVVJMKHVybCwgaG9zdCkge3JldHVybiAnUFJPWFkgY29"
257 "tcHJlc3MuZ29vZ2xlemlwLm5ldC5teWRvbWFpbi5jb206ODAnO30K",
259 // PAC URL that doesn't embed a script.
260 {"http://compress.googlezip.net/pac", false},
263 for (const auto& test
: test_cases
) {
264 base::HistogramTester histogram_tester
;
265 dict_
.reset(new base::DictionaryValue());
266 dict_
->SetString("mode", "pac_script");
267 dict_
->SetString("pac_url", test
.pac_url
);
268 test_context_
->pref_service()->Set(proxy_config::prefs::kProxy
,
270 EXPECT_CALL(*config_
, ContainsDataReductionProxy(_
)).Times(0);
272 drp_chrome_settings_
->MigrateDataReductionProxyOffProxyPrefs(
273 test_context_
->pref_service());
275 if (test
.expect_pref_cleared
) {
276 EXPECT_EQ(NULL
, test_context_
->pref_service()->GetUserPref(
277 proxy_config::prefs::kProxy
));
278 histogram_tester
.ExpectUniqueSample(
279 "DataReductionProxy.ProxyPrefMigrationResult",
280 DataReductionProxyChromeSettings::PROXY_PREF_CLEARED_PAC_GOOGLEZIP
,
283 const base::DictionaryValue
* value
;
284 EXPECT_TRUE(test_context_
->pref_service()
285 ->GetUserPref(proxy_config::prefs::kProxy
)
286 ->GetAsDictionary(&value
));
288 EXPECT_TRUE(value
->GetString("mode", &mode
));
289 EXPECT_EQ("pac_script", mode
);
291 EXPECT_TRUE(value
->GetString("pac_url", &pac_url
));
292 EXPECT_EQ(test
.pac_url
, pac_url
);
294 histogram_tester
.ExpectUniqueSample(
295 "DataReductionProxy.ProxyPrefMigrationResult",
296 DataReductionProxyChromeSettings::PROXY_PREF_NOT_CLEARED
, 1);
301 TEST_F(DataReductionProxyChromeSettingsTest
, MigrateIgnoreOtherProxy
) {
302 const std::string kTestServers
[] = {
303 "http=https://youtube.com",
304 "http=http://googlezip.net",
305 "http=http://thisismyproxynotgooglezip.net",
306 "https=http://arbitraryprefixgooglezip.net"};
308 for (const std::string
& test_server
: kTestServers
) {
309 base::HistogramTester histogram_tester
;
310 dict_
.reset(new base::DictionaryValue());
311 dict_
->SetString("mode", "fixed_servers");
312 dict_
->SetString("server", test_server
);
313 test_context_
->pref_service()->Set(proxy_config::prefs::kProxy
,
315 EXPECT_CALL(*config_
, ContainsDataReductionProxy(_
))
317 .WillOnce(Return(false));
319 drp_chrome_settings_
->MigrateDataReductionProxyOffProxyPrefs(
320 test_context_
->pref_service());
322 base::DictionaryValue
* value
=
323 (base::DictionaryValue
*)test_context_
->pref_service()->GetUserPref(
324 proxy_config::prefs::kProxy
);
326 EXPECT_TRUE(value
->GetString("mode", &mode
));
327 EXPECT_EQ("fixed_servers", mode
);
329 EXPECT_TRUE(value
->GetString("server", &server
));
330 EXPECT_EQ(test_server
, server
);
332 histogram_tester
.ExpectUniqueSample(
333 "DataReductionProxy.ProxyPrefMigrationResult",
334 DataReductionProxyChromeSettings::PROXY_PREF_NOT_CLEARED
, 1);