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 "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
11 #include "base/command_line.h"
12 #include "base/metrics/field_trial.h"
13 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params_test_utils.h"
14 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switches.h"
15 #include "components/data_reduction_proxy/proto/client_config.pb.h"
16 #include "components/variations/variations_associated_data.h"
17 #include "net/proxy/proxy_server.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
23 const char kConfigServiceFieldTrial
[] = "DataReductionProxyConfigService";
24 const char kConfigServiceURLParam
[] = "url";
28 namespace data_reduction_proxy
{
29 class DataReductionProxyParamsTest
: public testing::Test
{
31 void CheckParams(const TestDataReductionProxyParams
& params
,
32 bool expected_init_result
,
33 bool expected_allowed
,
34 bool expected_fallback_allowed
,
35 bool expected_promo_allowed
) {
36 EXPECT_EQ(expected_init_result
, params
.init_result());
37 EXPECT_EQ(expected_allowed
, params
.allowed());
38 EXPECT_EQ(expected_fallback_allowed
, params
.fallback_allowed());
39 EXPECT_EQ(expected_promo_allowed
, params
.promo_allowed());
41 void CheckValues(const TestDataReductionProxyParams
& params
,
42 const std::string
& expected_origin
,
43 const std::string
& expected_fallback_origin
,
44 const std::string
& expected_ssl_origin
,
45 const std::string
& expected_secure_proxy_check_url
) {
46 std::vector
<net::ProxyServer
> proxies_for_http
;
47 std::vector
<net::ProxyServer
> proxies_for_https
;
48 if (!expected_origin
.empty()) {
49 proxies_for_http
.push_back(net::ProxyServer::FromURI(
50 expected_origin
, net::ProxyServer::SCHEME_HTTP
));
53 if (!expected_fallback_origin
.empty()) {
54 proxies_for_http
.push_back(net::ProxyServer::FromURI(
55 expected_fallback_origin
, net::ProxyServer::SCHEME_HTTP
));
58 if (!expected_ssl_origin
.empty()) {
59 proxies_for_https
.push_back(net::ProxyServer::FromURI(
60 expected_ssl_origin
, net::ProxyServer::SCHEME_HTTP
));
63 EXPECT_THAT(proxies_for_http
,
64 testing::ContainerEq(params
.proxies_for_http()));
65 EXPECT_THAT(proxies_for_https
,
66 testing::ContainerEq(params
.proxies_for_https()));
67 EXPECT_EQ(GURL(expected_secure_proxy_check_url
),
68 params
.secure_proxy_check_url());
72 TEST_F(DataReductionProxyParamsTest
, EverythingDefined
) {
73 TestDataReductionProxyParams
params(
74 DataReductionProxyParams::kAllowed
|
75 DataReductionProxyParams::kFallbackAllowed
|
76 DataReductionProxyParams::kPromoAllowed
,
77 TestDataReductionProxyParams::HAS_EVERYTHING
);
78 CheckParams(params
, true, true, true, true);
79 CheckValues(params
, TestDataReductionProxyParams::DefaultDevOrigin(),
80 TestDataReductionProxyParams::DefaultDevFallbackOrigin(),
81 TestDataReductionProxyParams::DefaultSSLOrigin(),
82 TestDataReductionProxyParams::DefaultSecureProxyCheckURL());
85 TEST_F(DataReductionProxyParamsTest
, NoDevOrigin
) {
86 TestDataReductionProxyParams
params(
87 DataReductionProxyParams::kAllowed
|
88 DataReductionProxyParams::kFallbackAllowed
|
89 DataReductionProxyParams::kPromoAllowed
,
90 TestDataReductionProxyParams::HAS_EVERYTHING
&
91 ~TestDataReductionProxyParams::HAS_DEV_ORIGIN
&
92 ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN
);
93 CheckParams(params
, true, true, true, true);
94 CheckValues(params
, TestDataReductionProxyParams::DefaultOrigin(),
95 TestDataReductionProxyParams::DefaultFallbackOrigin(),
96 TestDataReductionProxyParams::DefaultSSLOrigin(),
97 TestDataReductionProxyParams::DefaultSecureProxyCheckURL());
100 TEST_F(DataReductionProxyParamsTest
, Flags
) {
101 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
102 switches::kDataReductionProxy
,
103 TestDataReductionProxyParams::FlagOrigin());
104 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
105 switches::kDataReductionProxyFallback
,
106 TestDataReductionProxyParams::FlagFallbackOrigin());
107 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
108 switches::kDataReductionSSLProxy
,
109 TestDataReductionProxyParams::FlagSSLOrigin());
110 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
111 switches::kDataReductionProxySecureProxyCheckURL
,
112 TestDataReductionProxyParams::FlagSecureProxyCheckURL());
113 TestDataReductionProxyParams
params(
114 DataReductionProxyParams::kAllowed
|
115 DataReductionProxyParams::kFallbackAllowed
|
116 DataReductionProxyParams::kPromoAllowed
,
117 TestDataReductionProxyParams::HAS_EVERYTHING
);
118 CheckParams(params
, true, true, true, true);
119 CheckValues(params
, TestDataReductionProxyParams::FlagOrigin(),
120 TestDataReductionProxyParams::FlagFallbackOrigin(),
121 TestDataReductionProxyParams::FlagSSLOrigin(),
122 TestDataReductionProxyParams::FlagSecureProxyCheckURL());
125 TEST_F(DataReductionProxyParamsTest
, CarrierTestFlag
) {
126 static const char kCarrierTestOrigin
[] =
127 "http://o-o.preferred.nttdocomodcp-hnd1.proxy-dev.googlezip.net:80";
128 base::CommandLine::ForCurrentProcess()->InitFromArgv(0, nullptr);
129 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
130 switches::kEnableDataReductionProxyCarrierTest
, kCarrierTestOrigin
);
131 DataReductionProxyParams
params(DataReductionProxyParams::kAllowed
);
132 std::vector
<net::ProxyServer
> proxies_for_http
;
133 proxies_for_http
.push_back(net::ProxyServer::FromURI(
134 kCarrierTestOrigin
, net::ProxyServer::SCHEME_HTTP
));
135 EXPECT_THAT(params
.proxies_for_http(),
136 testing::ContainerEq(proxies_for_http
));
139 TEST_F(DataReductionProxyParamsTest
, InvalidConfigurations
) {
142 bool fallback_allowed
;
144 unsigned int missing_definitions
;
145 bool expected_result
;
147 {true, true, true, TestDataReductionProxyParams::HAS_NOTHING
, true},
148 {true, false, true, TestDataReductionProxyParams::HAS_NOTHING
, true},
149 {false, true, true, TestDataReductionProxyParams::HAS_NOTHING
, false},
150 {true, true, true, TestDataReductionProxyParams::HAS_ORIGIN
, true},
154 TestDataReductionProxyParams::HAS_ORIGIN
|
155 TestDataReductionProxyParams::HAS_DEV_ORIGIN
|
156 TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN
,
161 TestDataReductionProxyParams::HAS_ORIGIN
|
162 TestDataReductionProxyParams::HAS_DEV_ORIGIN
|
163 TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN
,
168 TestDataReductionProxyParams::HAS_ORIGIN
|
169 TestDataReductionProxyParams::HAS_DEV_ORIGIN
|
170 TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN
,
175 TestDataReductionProxyParams::HAS_DEV_ORIGIN
|
176 TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN
,
181 TestDataReductionProxyParams::HAS_FALLBACK_ORIGIN
,
186 TestDataReductionProxyParams::HAS_FALLBACK_ORIGIN
,
191 TestDataReductionProxyParams::HAS_FALLBACK_ORIGIN
|
192 TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN
,
197 TestDataReductionProxyParams::HAS_SECURE_PROXY_CHECK_URL
,
202 TestDataReductionProxyParams::HAS_SECURE_PROXY_CHECK_URL
,
207 TestDataReductionProxyParams::HAS_SECURE_PROXY_CHECK_URL
,
209 {true, true, true, TestDataReductionProxyParams::HAS_SSL_ORIGIN
, true},
210 {true, false, true, TestDataReductionProxyParams::HAS_SSL_ORIGIN
, true},
211 {false, true, true, TestDataReductionProxyParams::HAS_SSL_ORIGIN
, false},
214 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
216 if (tests
[i
].allowed
)
217 flags
|= DataReductionProxyParams::kAllowed
;
218 if (tests
[i
].fallback_allowed
)
219 flags
|= DataReductionProxyParams::kFallbackAllowed
;
220 if (tests
[i
].promo_allowed
)
221 flags
|= DataReductionProxyParams::kPromoAllowed
;
222 TestDataReductionProxyParams
params(
224 TestDataReductionProxyParams::HAS_EVERYTHING
&
225 ~(tests
[i
].missing_definitions
));
226 EXPECT_EQ(tests
[i
].expected_result
, params
.init_result()) << i
;
230 TEST_F(DataReductionProxyParamsTest
, AndroidOnePromoFieldTrial
) {
231 EXPECT_TRUE(params::IsIncludedInAndroidOnePromoFieldTrial(
232 "google/sprout/sprout:4.4.4/KPW53/1379542:user/release-keys"));
233 EXPECT_FALSE(params::IsIncludedInAndroidOnePromoFieldTrial(
234 "google/hammerhead/hammerhead:5.0/LRX210/1570415:user/release-keys"));
237 TEST_F(DataReductionProxyParamsTest
, IsClientConfigEnabled
) {
239 std::string test_case
;
240 bool command_line_set
;
241 std::string trial_group_value
;
245 "Nothing set", false, "", false,
248 "Command line set", true, "", true,
251 "Enabled in experiment", false, "Enabled", true,
254 "Alternate enabled in experiment", false, "EnabledOther", true,
257 "Disabled in experiment", false, "Disabled", false,
260 "Command line set, enabled in experiment", true, "Enabled", true,
263 "Command line set, disabled in experiment", true, "Disabled", true,
267 for (const auto& test
: tests
) {
269 base::CommandLine::ForCurrentProcess()->InitFromArgv(0, NULL
);
270 if (test
.command_line_set
) {
271 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
272 switches::kEnableDataReductionProxyConfigClient
, "");
274 base::FieldTrialList
field_trial_list(nullptr);
275 if (!test
.trial_group_value
.empty()) {
276 base::FieldTrialList::CreateFieldTrial(kConfigServiceFieldTrial
,
277 test
.trial_group_value
);
279 EXPECT_EQ(test
.expected
, params::IsConfigClientEnabled()) << test
.test_case
;
283 TEST_F(DataReductionProxyParamsTest
, SecureProxyCheckDefault
) {
285 bool command_line_set
;
286 bool experiment_enabled
;
288 bool expected_use_by_default
;
291 false, false, false, true,
294 true, false, false, false,
297 true, true, false, false,
300 true, true, true, false,
303 false, true, true, false,
306 false, true, false, true,
311 for (const auto& test_case
: test_cases
) {
313 base::CommandLine::ForCurrentProcess()->InitFromArgv(0, NULL
);
315 base::FieldTrialList
trial_list(nullptr);
316 if (test_case
.command_line_set
) {
317 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
318 switches::kDataReductionProxyStartSecureDisabled
, "");
321 if (test_case
.experiment_enabled
) {
322 base::FieldTrialList::CreateFieldTrial(
323 "DataReductionProxySecureProxyAfterCheck",
324 test_case
.in_trial_group
? "Enabled" : "Disabled");
327 EXPECT_EQ(test_case
.expected_use_by_default
,
328 params::ShouldUseSecureProxyByDefault())
334 TEST_F(DataReductionProxyParamsTest
, PopulateConfigResponse
) {
335 DataReductionProxyParams
params(DataReductionProxyParams::kAllowed
|
336 DataReductionProxyParams::kFallbackAllowed
);
338 params
.PopulateConfigResponse(&config
);
339 EXPECT_TRUE(config
.has_proxy_config());
340 EXPECT_EQ(2, config
.proxy_config().http_proxy_servers_size());
341 const std::vector
<net::ProxyServer
>& proxies_for_http
=
342 params
.proxies_for_http();
343 EXPECT_EQ(proxies_for_http
[0].host_port_pair().host(),
344 config
.proxy_config().http_proxy_servers(0).host());
345 EXPECT_EQ(proxies_for_http
[0].host_port_pair().host(),
346 config
.proxy_config().http_proxy_servers(0).host());
347 EXPECT_EQ(proxies_for_http
[1].host_port_pair().host(),
348 config
.proxy_config().http_proxy_servers(1).host());
349 EXPECT_EQ(proxies_for_http
[1].host_port_pair().host(),
350 config
.proxy_config().http_proxy_servers(1).host());
353 TEST_F(DataReductionProxyParamsTest
, GetConfigServiceURL
) {
355 std::string trial_group_value
;
356 std::string trial_url_param
;
359 "Enabled", "http://enabled.config-service/",
362 "Disabled", "http://disabled.config-service/",
365 "EnabledOther", "http://other.config-service/",
369 variations::testing::ClearAllVariationParams();
370 for (const auto& variation
: variations
) {
371 std::map
<std::string
, std::string
> variation_params
;
372 variation_params
[kConfigServiceURLParam
] = variation
.trial_url_param
;
373 ASSERT_TRUE(variations::AssociateVariationParams(
374 kConfigServiceFieldTrial
, variation
.trial_group_value
,
379 std::string test_case
;
380 std::string flag_value
;
381 std::string trial_group_value
;
385 "Nothing set", "", "",
386 GURL("https://datasaver.googleapis.com/v1/clientConfigs"),
389 "Only command line set", "http://commandline.config-service/", "",
390 GURL("http://commandline.config-service/"),
393 "Enabled group", "", "Enabled",
394 GURL("http://enabled.config-service/"),
397 "Disabled group", "", "Disabled",
398 GURL("http://disabled.config-service/"),
401 "Alternate enabled group", "", "EnabledOther",
402 GURL("http://other.config-service/"),
405 "Command line precedence", "http://commandline.config-service/",
406 "Enabled", GURL("http://commandline.config-service/"),
410 for (const auto& test
: tests
) {
412 base::CommandLine::ForCurrentProcess()->InitFromArgv(0, NULL
);
413 if (!test
.flag_value
.empty()) {
414 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
415 switches::kDataReductionProxyConfigURL
, test
.flag_value
);
417 base::FieldTrialList
field_trial_list(nullptr);
418 if (!test
.trial_group_value
.empty()) {
419 base::FieldTrialList::CreateFieldTrial(kConfigServiceFieldTrial
,
420 test
.trial_group_value
);
422 EXPECT_EQ(test
.expected
, params::GetConfigServiceURL()) << test
.test_case
;
426 } // namespace data_reduction_proxy