OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings_
test_utils.h" | 5 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings_
test_utils.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/prefs/pref_registry_simple.h" | 9 #include "base/prefs/pref_registry_simple.h" |
10 #include "base/prefs/scoped_user_pref_update.h" | 10 #include "base/prefs/scoped_user_pref_update.h" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names
.h" | 12 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names
.h" |
13 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h
" | 13 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h
" |
14 | 14 |
15 using testing::_; | 15 using testing::_; |
16 using testing::AnyNumber; | 16 using testing::AnyNumber; |
17 using testing::Return; | 17 using testing::Return; |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 const char kDataReductionProxy[] = "https://foo.com:443/"; | 21 const char kDataReductionProxy[] = "https://foo.com:443/"; |
22 const char kDataReductionProxyFallback[] = "http://bar.com:80"; | 22 const char kDataReductionProxyFallback[] = "http://bar.com:80/"; |
23 const char kDataReductionProxyKey[] = "12345"; | 23 const char kDataReductionProxyKey[] = "12345"; |
24 | 24 |
25 const char kProbeURLWithOKResponse[] = "http://ok.org/"; | 25 const char kProbeURLWithOKResponse[] = "http://ok.org/"; |
26 | 26 |
27 const char kProxy[] = "proxy"; | 27 const char kProxy[] = "proxy"; |
28 | 28 |
29 } // namespace | 29 } // namespace |
30 | 30 |
31 namespace data_reduction_proxy { | 31 namespace data_reduction_proxy { |
32 | 32 |
33 // Transform "normal"-looking headers (\n-separated) to the appropriate | 33 // Transform "normal"-looking headers (\n-separated) to the appropriate |
34 // input format for ParseRawHeaders (\0-separated). | 34 // input format for ParseRawHeaders (\0-separated). |
35 void HeadersToRaw(std::string* headers) { | 35 void HeadersToRaw(std::string* headers) { |
36 std::replace(headers->begin(), headers->end(), '\n', '\0'); | 36 std::replace(headers->begin(), headers->end(), '\n', '\0'); |
37 if (!headers->empty()) | 37 if (!headers->empty()) |
38 *headers += '\0'; | 38 *headers += '\0'; |
39 } | 39 } |
40 | 40 |
41 ProbeURLFetchResult FetchResult(bool enabled, bool success) { | 41 ProbeURLFetchResult FetchResult(bool enabled, bool success) { |
42 if (enabled) { | 42 if (enabled) { |
43 if (success) | 43 if (success) |
44 return SUCCEEDED_PROXY_ALREADY_ENABLED; | 44 return SUCCEEDED_PROXY_ALREADY_ENABLED; |
45 return FAILED_PROXY_DISABLED; | 45 return FAILED_PROXY_DISABLED; |
46 } | 46 } |
47 if (success) | 47 if (success) |
48 return SUCCEEDED_PROXY_ENABLED; | 48 return SUCCEEDED_PROXY_ENABLED; |
49 return FAILED_PROXY_ALREADY_DISABLED; | 49 return FAILED_PROXY_ALREADY_DISABLED; |
50 } | 50 } |
51 | 51 |
| 52 TestDataReductionProxyConfig::TestDataReductionProxyConfig() |
| 53 : enabled_(false), |
| 54 restricted_(false), |
| 55 fallback_restricted_(false) {} |
| 56 |
52 void TestDataReductionProxyConfig::Enable( | 57 void TestDataReductionProxyConfig::Enable( |
53 bool restricted, | 58 bool restricted, |
54 bool fallback_restricted, | 59 bool fallback_restricted, |
55 const std::string& primary_origin, | 60 const std::string& primary_origin, |
56 const std::string& fallback_origin) { | 61 const std::string& fallback_origin, |
| 62 const std::string& ssl_origin) { |
57 enabled_ = true; | 63 enabled_ = true; |
58 restricted_ = restricted; | 64 restricted_ = restricted; |
59 fallback_restricted_ = fallback_restricted; | 65 fallback_restricted_ = fallback_restricted; |
| 66 origin_ = primary_origin; |
| 67 fallback_origin_ = fallback_origin; |
| 68 ssl_origin_ = ssl_origin; |
60 } | 69 } |
61 | 70 |
62 void TestDataReductionProxyConfig::Disable() { | 71 void TestDataReductionProxyConfig::Disable() { |
63 enabled_ = false; | 72 enabled_ = false; |
64 restricted_ = false; | 73 restricted_ = false; |
65 fallback_restricted_ = false; | 74 fallback_restricted_ = false; |
| 75 origin_ = ""; |
| 76 fallback_origin_ = ""; |
| 77 ssl_origin_ = ""; |
| 78 } |
| 79 |
| 80 // static |
| 81 void DataReductionProxySettingsTestBase::AddTestProxyToCommandLine() { |
| 82 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 83 switches::kDataReductionProxy, kDataReductionProxy); |
| 84 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 85 switches::kDataReductionProxyFallback, kDataReductionProxyFallback); |
| 86 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 87 switches::kDataReductionProxyKey, kDataReductionProxyKey); |
| 88 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 89 switches::kDataReductionProxyProbeURL, kProbeURLWithOKResponse); |
66 } | 90 } |
67 | 91 |
68 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase() | 92 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase() |
69 : testing::Test() { | 93 : testing::Test() { |
70 } | 94 } |
71 | 95 |
72 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {} | 96 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {} |
73 | 97 |
74 void DataReductionProxySettingsTestBase::AddProxyToCommandLine() { | 98 void DataReductionProxySettingsTestBase::AddProxyToCommandLine() { |
75 DataReductionProxySettings::SetAllowed(true); | 99 AddTestProxyToCommandLine(); |
76 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
77 switches::kDataReductionProxy, kDataReductionProxy); | |
78 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
79 switches::kDataReductionProxyFallback, kDataReductionProxyFallback); | |
80 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
81 switches::kDataReductionProxyKey, kDataReductionProxyKey); | |
82 } | 100 } |
83 | 101 |
84 // testing::Test implementation: | 102 // testing::Test implementation: |
85 void DataReductionProxySettingsTestBase::SetUp() { | 103 void DataReductionProxySettingsTestBase::SetUp() { |
86 DataReductionProxySettings::SetAllowed(true); | |
87 PrefRegistrySimple* registry = pref_service_.registry(); | 104 PrefRegistrySimple* registry = pref_service_.registry(); |
88 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength); | 105 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength); |
89 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength); | 106 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength); |
90 registry->RegisterInt64Pref(prefs::kDailyHttpContentLengthLastUpdateDate, | 107 registry->RegisterInt64Pref(prefs::kDailyHttpContentLengthLastUpdateDate, |
91 0L); | 108 0L); |
92 registry->RegisterDictionaryPref(kProxy); | 109 registry->RegisterDictionaryPref(kProxy); |
93 registry->RegisterBooleanPref(prefs::kDataReductionProxyEnabled, false); | 110 registry->RegisterBooleanPref(prefs::kDataReductionProxyEnabled, false); |
| 111 registry->RegisterBooleanPref(prefs::kDataReductionProxyAltEnabled, false); |
94 registry->RegisterBooleanPref(prefs::kDataReductionProxyWasEnabledBefore, | 112 registry->RegisterBooleanPref(prefs::kDataReductionProxyWasEnabledBefore, |
95 false); | 113 false); |
96 ResetSettings(); | 114 AddProxyToCommandLine(); |
| 115 ResetSettings(true, true, false, true); |
97 | 116 |
98 ListPrefUpdate original_update(&pref_service_, | 117 ListPrefUpdate original_update(&pref_service_, |
99 prefs::kDailyHttpOriginalContentLength); | 118 prefs::kDailyHttpOriginalContentLength); |
100 ListPrefUpdate received_update(&pref_service_, | 119 ListPrefUpdate received_update(&pref_service_, |
101 prefs::kDailyHttpReceivedContentLength); | 120 prefs::kDailyHttpReceivedContentLength); |
102 for (int64 i = 0; i < kNumDaysInHistory; i++) { | 121 for (int64 i = 0; i < kNumDaysInHistory; i++) { |
103 original_update->Insert(0, | 122 original_update->Insert(0, |
104 new base::StringValue(base::Int64ToString(2 * i))); | 123 new base::StringValue(base::Int64ToString(2 * i))); |
105 received_update->Insert(0, new base::StringValue(base::Int64ToString(i))); | 124 received_update->Insert(0, new base::StringValue(base::Int64ToString(i))); |
106 } | 125 } |
107 last_update_time_ = base::Time::Now().LocalMidnight(); | 126 last_update_time_ = base::Time::Now().LocalMidnight(); |
108 pref_service_.SetInt64( | 127 pref_service_.SetInt64( |
109 prefs::kDailyHttpContentLengthLastUpdateDate, | 128 prefs::kDailyHttpContentLengthLastUpdateDate, |
110 last_update_time_.ToInternalValue()); | 129 last_update_time_.ToInternalValue()); |
111 } | 130 } |
112 | 131 |
113 template <class C> | 132 template <class C> |
114 void DataReductionProxySettingsTestBase::ResetSettings() { | 133 void DataReductionProxySettingsTestBase::ResetSettings(bool allowed, |
| 134 bool fallback_allowed, |
| 135 bool alt_allowed, |
| 136 bool promo_allowed) { |
| 137 int flags = 0; |
| 138 if (allowed) |
| 139 flags |= DataReductionProxyParams::kAllowed; |
| 140 if (fallback_allowed) |
| 141 flags |= DataReductionProxyParams::kFallbackAllowed; |
| 142 if (alt_allowed) |
| 143 flags |= DataReductionProxyParams::kAlternativeAllowed; |
| 144 if (promo_allowed) |
| 145 flags |= DataReductionProxyParams::kPromoAllowed; |
115 MockDataReductionProxySettings<C>* settings = | 146 MockDataReductionProxySettings<C>* settings = |
116 new MockDataReductionProxySettings<C>(); | 147 new MockDataReductionProxySettings<C>(flags); |
117 EXPECT_CALL(*settings, GetOriginalProfilePrefs()) | 148 EXPECT_CALL(*settings, GetOriginalProfilePrefs()) |
118 .Times(AnyNumber()) | 149 .Times(AnyNumber()) |
119 .WillRepeatedly(Return(&pref_service_)); | 150 .WillRepeatedly(Return(&pref_service_)); |
120 EXPECT_CALL(*settings, GetLocalStatePrefs()) | 151 EXPECT_CALL(*settings, GetLocalStatePrefs()) |
121 .Times(AnyNumber()) | 152 .Times(AnyNumber()) |
122 .WillRepeatedly(Return(&pref_service_)); | 153 .WillRepeatedly(Return(&pref_service_)); |
123 EXPECT_CALL(*settings, GetURLFetcher()).Times(0); | 154 EXPECT_CALL(*settings, GetURLFetcher()).Times(0); |
124 EXPECT_CALL(*settings, LogProxyState(_, _, _)).Times(0); | 155 EXPECT_CALL(*settings, LogProxyState(_, _, _)).Times(0); |
125 settings_.reset(settings); | 156 settings_.reset(settings); |
126 settings_->config_.reset(new TestDataReductionProxyConfig()); | 157 settings_->configurator_.reset(new TestDataReductionProxyConfig()); |
127 } | 158 } |
128 | 159 |
129 // Explicitly generate required instantiations. | 160 // Explicitly generate required instantiations. |
130 template void | 161 template void |
131 DataReductionProxySettingsTestBase::ResetSettings<DataReductionProxySettings>(); | 162 DataReductionProxySettingsTestBase::ResetSettings<DataReductionProxySettings>( |
| 163 bool allowed, bool fallback_allowed, bool alt_allowed, bool promo_allowed); |
132 | 164 |
133 template <class C> | 165 template <class C> |
134 void DataReductionProxySettingsTestBase::SetProbeResult( | 166 void DataReductionProxySettingsTestBase::SetProbeResult( |
135 const std::string& test_url, | 167 const std::string& test_url, |
136 const std::string& response, | 168 const std::string& response, |
137 ProbeURLFetchResult result, | 169 ProbeURLFetchResult result, |
138 bool success, | 170 bool success, |
139 int expected_calls) { | 171 int expected_calls) { |
140 MockDataReductionProxySettings<C>* settings = | 172 MockDataReductionProxySettings<C>* settings = |
141 static_cast<MockDataReductionProxySettings<C>*>(settings_.get()); | 173 static_cast<MockDataReductionProxySettings<C>*>(settings_.get()); |
(...skipping 21 matching lines...) Expand all Loading... |
163 const std::string& response, | 195 const std::string& response, |
164 ProbeURLFetchResult result, | 196 ProbeURLFetchResult result, |
165 bool success, | 197 bool success, |
166 int expected_calls); | 198 int expected_calls); |
167 | 199 |
168 void DataReductionProxySettingsTestBase::CheckProxyConfigs( | 200 void DataReductionProxySettingsTestBase::CheckProxyConfigs( |
169 bool expected_enabled, | 201 bool expected_enabled, |
170 bool expected_restricted, | 202 bool expected_restricted, |
171 bool expected_fallback_restricted) { | 203 bool expected_fallback_restricted) { |
172 TestDataReductionProxyConfig* config = | 204 TestDataReductionProxyConfig* config = |
173 static_cast<TestDataReductionProxyConfig*>(settings_->config_.get()); | 205 static_cast<TestDataReductionProxyConfig*>( |
| 206 settings_->configurator_.get()); |
174 ASSERT_EQ(expected_restricted, config->restricted_); | 207 ASSERT_EQ(expected_restricted, config->restricted_); |
175 ASSERT_EQ(expected_fallback_restricted, config->fallback_restricted_); | 208 ASSERT_EQ(expected_fallback_restricted, config->fallback_restricted_); |
176 ASSERT_EQ(expected_enabled, config->enabled_); | 209 ASSERT_EQ(expected_enabled, config->enabled_); |
177 } | 210 } |
178 | 211 |
179 void DataReductionProxySettingsTestBase::CheckProbe( | 212 void DataReductionProxySettingsTestBase::CheckProbe( |
180 bool initially_enabled, | 213 bool initially_enabled, |
181 const std::string& probe_url, | 214 const std::string& probe_url, |
182 const std::string& response, | 215 const std::string& response, |
183 bool request_succeeded, | 216 bool request_succeeded, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 } else { | 268 } else { |
236 pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, enabled); | 269 pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, enabled); |
237 } | 270 } |
238 base::MessageLoop::current()->RunUntilIdle(); | 271 base::MessageLoop::current()->RunUntilIdle(); |
239 // Never expect the proxy to be restricted for pref change tests. | 272 // Never expect the proxy to be restricted for pref change tests. |
240 CheckProxyConfigs(expected_enabled, false, false); | 273 CheckProxyConfigs(expected_enabled, false, false); |
241 } | 274 } |
242 | 275 |
243 void DataReductionProxySettingsTestBase::CheckInitDataReductionProxy( | 276 void DataReductionProxySettingsTestBase::CheckInitDataReductionProxy( |
244 bool enabled_at_startup) { | 277 bool enabled_at_startup) { |
245 AddProxyToCommandLine(); | |
246 base::MessageLoopForUI loop; | 278 base::MessageLoopForUI loop; |
247 SetProbeResult(kProbeURLWithOKResponse, | 279 SetProbeResult(kProbeURLWithOKResponse, |
248 "OK", | 280 "OK", |
249 FetchResult(enabled_at_startup, true), | 281 FetchResult(enabled_at_startup, true), |
250 true, | 282 true, |
251 enabled_at_startup ? 1 : 0); | 283 enabled_at_startup ? 1 : 0); |
252 scoped_ptr<DataReductionProxyConfigurator> configurator( | 284 scoped_ptr<DataReductionProxyConfigurator> configurator( |
253 new TestDataReductionProxyConfig()); | 285 new TestDataReductionProxyConfig()); |
254 settings_->SetProxyConfigurator(configurator.Pass()); | 286 settings_->SetProxyConfigurator(configurator.Pass()); |
255 scoped_refptr<net::TestURLRequestContextGetter> request_context = | 287 scoped_refptr<net::TestURLRequestContextGetter> request_context = |
256 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current()); | 288 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current()); |
257 settings_->InitDataReductionProxySettings(&pref_service_, | 289 settings_->InitDataReductionProxySettings(&pref_service_, |
258 &pref_service_, | 290 &pref_service_, |
259 request_context.get()); | 291 request_context.get()); |
260 | 292 |
261 base::MessageLoop::current()->RunUntilIdle(); | 293 base::MessageLoop::current()->RunUntilIdle(); |
262 CheckProxyConfigs(enabled_at_startup, false, false); | 294 CheckProxyConfigs(enabled_at_startup, false, false); |
263 } | 295 } |
264 | 296 |
265 } // namespace data_reduction_proxy | 297 } // namespace data_reduction_proxy |
OLD | NEW |