data_reduction_proxy_settings_test_utils.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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 "components/data_reduction_proxy/browser/data_reduction_proxy_settings_test_utils.h" 6 7#include "base/command_line.h" 8#include "base/message_loop/message_loop.h" 9#include "base/prefs/pref_registry_simple.h" 10#include "base/prefs/scoped_user_pref_update.h" 11#include "base/strings/string_number_conversions.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" 14 15using testing::_; 16using testing::AnyNumber; 17using testing::Return; 18 19namespace { 20 21const char kDataReductionProxy[] = "https://foo.com:443/"; 22const char kDataReductionProxyFallback[] = "http://bar.com:80/"; 23const char kDataReductionProxyKey[] = "12345"; 24 25const char kProbeURLWithOKResponse[] = "http://ok.org/"; 26 27const char kProxy[] = "proxy"; 28 29} // namespace 30 31namespace data_reduction_proxy { 32 33// Transform "normal"-looking headers (\n-separated) to the appropriate 34// input format for ParseRawHeaders (\0-separated). 35void HeadersToRaw(std::string* headers) { 36 std::replace(headers->begin(), headers->end(), '\n', '\0'); 37 if (!headers->empty()) 38 *headers += '\0'; 39} 40 41ProbeURLFetchResult FetchResult(bool enabled, bool success) { 42 if (enabled) { 43 if (success) 44 return SUCCEEDED_PROXY_ALREADY_ENABLED; 45 return FAILED_PROXY_DISABLED; 46 } 47 if (success) 48 return SUCCEEDED_PROXY_ENABLED; 49 return FAILED_PROXY_ALREADY_DISABLED; 50} 51 52TestDataReductionProxyConfig::TestDataReductionProxyConfig() 53 : enabled_(false), 54 restricted_(false), 55 fallback_restricted_(false) {} 56 57void TestDataReductionProxyConfig::Enable( 58 bool restricted, 59 bool fallback_restricted, 60 const std::string& primary_origin, 61 const std::string& fallback_origin, 62 const std::string& ssl_origin) { 63 enabled_ = true; 64 restricted_ = restricted; 65 fallback_restricted_ = fallback_restricted; 66 origin_ = primary_origin; 67 fallback_origin_ = fallback_origin; 68 ssl_origin_ = ssl_origin; 69} 70 71void TestDataReductionProxyConfig::Disable() { 72 enabled_ = false; 73 restricted_ = false; 74 fallback_restricted_ = false; 75 origin_ = ""; 76 fallback_origin_ = ""; 77 ssl_origin_ = ""; 78} 79 80// static 81void 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); 90} 91 92DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase() 93 : testing::Test() { 94} 95 96DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {} 97 98void DataReductionProxySettingsTestBase::AddProxyToCommandLine() { 99 AddTestProxyToCommandLine(); 100} 101 102// testing::Test implementation: 103void DataReductionProxySettingsTestBase::SetUp() { 104 PrefRegistrySimple* registry = pref_service_.registry(); 105 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength); 106 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength); 107 registry->RegisterInt64Pref(prefs::kDailyHttpContentLengthLastUpdateDate, 108 0L); 109 registry->RegisterDictionaryPref(kProxy); 110 registry->RegisterBooleanPref(prefs::kDataReductionProxyEnabled, false); 111 registry->RegisterBooleanPref(prefs::kDataReductionProxyAltEnabled, false); 112 registry->RegisterBooleanPref(prefs::kDataReductionProxyWasEnabledBefore, 113 false); 114 AddProxyToCommandLine(); 115 ResetSettings(true, true, false, true); 116 117 ListPrefUpdate original_update(&pref_service_, 118 prefs::kDailyHttpOriginalContentLength); 119 ListPrefUpdate received_update(&pref_service_, 120 prefs::kDailyHttpReceivedContentLength); 121 for (int64 i = 0; i < kNumDaysInHistory; i++) { 122 original_update->Insert(0, 123 new base::StringValue(base::Int64ToString(2 * i))); 124 received_update->Insert(0, new base::StringValue(base::Int64ToString(i))); 125 } 126 last_update_time_ = base::Time::Now().LocalMidnight(); 127 pref_service_.SetInt64( 128 prefs::kDailyHttpContentLengthLastUpdateDate, 129 last_update_time_.ToInternalValue()); 130} 131 132template <class C> 133void 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; 146 MockDataReductionProxySettings<C>* settings = 147 new MockDataReductionProxySettings<C>(flags); 148 EXPECT_CALL(*settings, GetOriginalProfilePrefs()) 149 .Times(AnyNumber()) 150 .WillRepeatedly(Return(&pref_service_)); 151 EXPECT_CALL(*settings, GetLocalStatePrefs()) 152 .Times(AnyNumber()) 153 .WillRepeatedly(Return(&pref_service_)); 154 EXPECT_CALL(*settings, GetURLFetcher()).Times(0); 155 EXPECT_CALL(*settings, LogProxyState(_, _, _)).Times(0); 156 settings_.reset(settings); 157 settings_->configurator_.reset(new TestDataReductionProxyConfig()); 158} 159 160// Explicitly generate required instantiations. 161template void 162DataReductionProxySettingsTestBase::ResetSettings<DataReductionProxySettings>( 163 bool allowed, bool fallback_allowed, bool alt_allowed, bool promo_allowed); 164 165template <class C> 166void DataReductionProxySettingsTestBase::SetProbeResult( 167 const std::string& test_url, 168 const std::string& response, 169 ProbeURLFetchResult result, 170 bool success, 171 int expected_calls) { 172 MockDataReductionProxySettings<C>* settings = 173 static_cast<MockDataReductionProxySettings<C>*>(settings_.get()); 174 if (0 == expected_calls) { 175 EXPECT_CALL(*settings, GetURLFetcher()).Times(0); 176 EXPECT_CALL(*settings, RecordProbeURLFetchResult(_)).Times(0); 177 } else { 178 EXPECT_CALL(*settings, RecordProbeURLFetchResult(result)).Times(1); 179 EXPECT_CALL(*settings, GetURLFetcher()) 180 .Times(expected_calls) 181 .WillRepeatedly(Return(new net::FakeURLFetcher( 182 GURL(test_url), 183 settings, 184 response, 185 success ? net::HTTP_OK : net::HTTP_INTERNAL_SERVER_ERROR, 186 success ? net::URLRequestStatus::SUCCESS : 187 net::URLRequestStatus::FAILED))); 188 } 189} 190 191// Explicitly generate required instantiations. 192template void 193DataReductionProxySettingsTestBase::SetProbeResult<DataReductionProxySettings>( 194 const std::string& test_url, 195 const std::string& response, 196 ProbeURLFetchResult result, 197 bool success, 198 int expected_calls); 199 200void DataReductionProxySettingsTestBase::CheckProxyConfigs( 201 bool expected_enabled, 202 bool expected_restricted, 203 bool expected_fallback_restricted) { 204 TestDataReductionProxyConfig* config = 205 static_cast<TestDataReductionProxyConfig*>( 206 settings_->configurator_.get()); 207 ASSERT_EQ(expected_restricted, config->restricted_); 208 ASSERT_EQ(expected_fallback_restricted, config->fallback_restricted_); 209 ASSERT_EQ(expected_enabled, config->enabled_); 210} 211 212void DataReductionProxySettingsTestBase::CheckProbe( 213 bool initially_enabled, 214 const std::string& probe_url, 215 const std::string& response, 216 bool request_succeeded, 217 bool expected_enabled, 218 bool expected_restricted, 219 bool expected_fallback_restricted) { 220 pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, 221 initially_enabled); 222 if (initially_enabled) 223 settings_->enabled_by_user_ = true; 224 settings_->restricted_by_carrier_ = false; 225 SetProbeResult(probe_url, 226 response, 227 FetchResult(initially_enabled, 228 request_succeeded && (response == "OK")), 229 request_succeeded, 230 initially_enabled ? 1 : 0); 231 settings_->MaybeActivateDataReductionProxy(false); 232 base::MessageLoop::current()->RunUntilIdle(); 233 CheckProxyConfigs(expected_enabled, 234 expected_restricted, 235 expected_fallback_restricted); 236} 237 238void DataReductionProxySettingsTestBase::CheckProbeOnIPChange( 239 const std::string& probe_url, 240 const std::string& response, 241 bool request_succeeded, 242 bool expected_restricted, 243 bool expected_fallback_restricted) { 244 SetProbeResult(probe_url, 245 response, 246 FetchResult(!settings_->restricted_by_carrier_, 247 request_succeeded && (response == "OK")), 248 request_succeeded, 249 1); 250 settings_->OnIPAddressChanged(); 251 base::MessageLoop::current()->RunUntilIdle(); 252 CheckProxyConfigs(true, expected_restricted, expected_fallback_restricted); 253} 254 255void DataReductionProxySettingsTestBase::CheckOnPrefChange( 256 bool enabled, 257 bool expected_enabled, 258 bool managed) { 259 // Always have a sucessful probe for pref change tests. 260 SetProbeResult(kProbeURLWithOKResponse, 261 "OK", 262 FetchResult(enabled, true), 263 true, 264 expected_enabled ? 1 : 0); 265 if (managed) { 266 pref_service_.SetManagedPref(prefs::kDataReductionProxyEnabled, 267 base::Value::CreateBooleanValue(enabled)); 268 } else { 269 pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, enabled); 270 } 271 base::MessageLoop::current()->RunUntilIdle(); 272 // Never expect the proxy to be restricted for pref change tests. 273 CheckProxyConfigs(expected_enabled, false, false); 274} 275 276void DataReductionProxySettingsTestBase::CheckInitDataReductionProxy( 277 bool enabled_at_startup) { 278 base::MessageLoopForUI loop; 279 SetProbeResult(kProbeURLWithOKResponse, 280 "OK", 281 FetchResult(enabled_at_startup, true), 282 true, 283 enabled_at_startup ? 1 : 0); 284 scoped_ptr<DataReductionProxyConfigurator> configurator( 285 new TestDataReductionProxyConfig()); 286 settings_->SetProxyConfigurator(configurator.Pass()); 287 scoped_refptr<net::TestURLRequestContextGetter> request_context = 288 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current()); 289 settings_->InitDataReductionProxySettings(&pref_service_, 290 &pref_service_, 291 request_context.get()); 292 293 base::MessageLoop::current()->RunUntilIdle(); 294 CheckProxyConfigs(enabled_at_startup, false, false); 295} 296 297} // namespace data_reduction_proxy 298