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/bind.h" 8 #include "base/command_line.h" 9 #include "base/message_loop/message_loop.h" 10 #include "base/prefs/pref_registry_simple.h" 11 #include "base/prefs/scoped_user_pref_update.h" 12 #include "base/strings/string_number_conversions.h" 13 #include "base/test/test_simple_task_runner.h" 14 #include "base/time/time.h" 15 #include "components/data_reduction_proxy/browser/data_reduction_proxy_prefs.h" 16 #include "components/data_reduction_proxy/browser/data_reduction_proxy_statistics_prefs.h" 17 #include "components/data_reduction_proxy/common/data_reduction_proxy_headers_test_utils.h" 18 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names.h" 19 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h" 20 21 using testing::_; 22 using testing::AnyNumber; 23 using testing::Return; 24 25 namespace { 26 27 const char kProbeURLWithOKResponse[] = "http://ok.org/"; 28 29 const char kProxy[] = "proxy"; 30 31 } // namespace 32 33 namespace data_reduction_proxy { 34 35 ProbeURLFetchResult FetchResult(bool enabled, bool success) { 36 if (enabled) { 37 if (success) 38 return SUCCEEDED_PROXY_ALREADY_ENABLED; 39 return FAILED_PROXY_DISABLED; 40 } 41 if (success) 42 return SUCCEEDED_PROXY_ENABLED; 43 return FAILED_PROXY_ALREADY_DISABLED; 44 } 45 46 TestDataReductionProxyConfig::TestDataReductionProxyConfig() 47 : enabled_(false), 48 restricted_(false), 49 fallback_restricted_(false) {} 50 51 void TestDataReductionProxyConfig::Enable( 52 bool restricted, 53 bool fallback_restricted, 54 const std::string& primary_origin, 55 const std::string& fallback_origin, 56 const std::string& ssl_origin) { 57 enabled_ = true; 58 restricted_ = restricted; 59 fallback_restricted_ = fallback_restricted; 60 origin_ = primary_origin; 61 fallback_origin_ = fallback_origin; 62 ssl_origin_ = ssl_origin; 63 } 64 65 void TestDataReductionProxyConfig::Disable() { 66 enabled_ = false; 67 restricted_ = false; 68 fallback_restricted_ = false; 69 origin_ = ""; 70 fallback_origin_ = ""; 71 ssl_origin_ = ""; 72 } 73 74 DataReductionProxySettingsTestBase::DataReductionProxySettingsTestBase() 75 : testing::Test() {} 76 77 DataReductionProxySettingsTestBase::~DataReductionProxySettingsTestBase() {} 78 79 // testing::Test implementation: 80 void DataReductionProxySettingsTestBase::SetUp() { 81 PrefRegistrySimple* registry = pref_service_.registry(); 82 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength); 83 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength); 84 registry->RegisterInt64Pref(prefs::kDailyHttpContentLengthLastUpdateDate, 85 0L); 86 registry->RegisterDictionaryPref(kProxy); 87 registry->RegisterBooleanPref(prefs::kDataReductionProxyEnabled, false); 88 registry->RegisterBooleanPref(prefs::kDataReductionProxyAltEnabled, false); 89 registry->RegisterBooleanPref(prefs::kDataReductionProxyWasEnabledBefore, 90 false); 91 92 statistics_prefs_.reset(new DataReductionProxyStatisticsPrefs( 93 &pref_service_, 94 scoped_refptr<base::TestSimpleTaskRunner>( 95 new base::TestSimpleTaskRunner()), 96 base::TimeDelta())); 97 98 //AddProxyToCommandLine(); 99 ResetSettings(true, true, false, true, false); 100 101 ListPrefUpdate original_update(&pref_service_, 102 prefs::kDailyHttpOriginalContentLength); 103 ListPrefUpdate received_update(&pref_service_, 104 prefs::kDailyHttpReceivedContentLength); 105 for (int64 i = 0; i < kNumDaysInHistory; i++) { 106 original_update->Insert(0, 107 new base::StringValue(base::Int64ToString(2 * i))); 108 received_update->Insert(0, new base::StringValue(base::Int64ToString(i))); 109 } 110 last_update_time_ = base::Time::Now().LocalMidnight(); 111 statistics_prefs_->SetInt64( 112 prefs::kDailyHttpContentLengthLastUpdateDate, 113 last_update_time_.ToInternalValue()); 114 expected_params_.reset(new TestDataReductionProxyParams( 115 DataReductionProxyParams::kAllowed | 116 DataReductionProxyParams::kFallbackAllowed | 117 DataReductionProxyParams::kPromoAllowed, 118 TestDataReductionProxyParams::HAS_EVERYTHING & 119 ~TestDataReductionProxyParams::HAS_DEV_ORIGIN & 120 ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN)); 121 } 122 123 template <class C> 124 void DataReductionProxySettingsTestBase::ResetSettings(bool allowed, 125 bool fallback_allowed, 126 bool alt_allowed, 127 bool promo_allowed, 128 bool holdback) { 129 int flags = 0; 130 if (allowed) 131 flags |= DataReductionProxyParams::kAllowed; 132 if (fallback_allowed) 133 flags |= DataReductionProxyParams::kFallbackAllowed; 134 if (alt_allowed) 135 flags |= DataReductionProxyParams::kAlternativeAllowed; 136 if (promo_allowed) 137 flags |= DataReductionProxyParams::kPromoAllowed; 138 if (holdback) 139 flags |= DataReductionProxyParams::kHoldback; 140 MockDataReductionProxySettings<C>* settings = 141 new MockDataReductionProxySettings<C>(flags); 142 EXPECT_CALL(*settings, GetOriginalProfilePrefs()) 143 .Times(AnyNumber()) 144 .WillRepeatedly(Return(&pref_service_)); 145 EXPECT_CALL(*settings, GetLocalStatePrefs()) 146 .Times(AnyNumber()) 147 .WillRepeatedly(Return(&pref_service_)); 148 EXPECT_CALL(*settings, GetURLFetcherForAvailabilityCheck()).Times(0); 149 EXPECT_CALL(*settings, LogProxyState(_, _, _)).Times(0); 150 settings_.reset(settings); 151 configurator_.reset(new TestDataReductionProxyConfig()); 152 settings_->configurator_ = configurator_.get(); 153 settings_->SetDataReductionProxyStatisticsPrefs(statistics_prefs_.get()); 154 } 155 156 // Explicitly generate required instantiations. 157 template void 158 DataReductionProxySettingsTestBase::ResetSettings<DataReductionProxySettings>( 159 bool allowed, 160 bool fallback_allowed, 161 bool alt_allowed, 162 bool promo_allowed, 163 bool holdback); 164 165 template <class C> 166 void 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, GetURLFetcherForAvailabilityCheck()).Times(0); 176 EXPECT_CALL(*settings, RecordProbeURLFetchResult(_)).Times(0); 177 } else { 178 EXPECT_CALL(*settings, RecordProbeURLFetchResult(result)).Times(1); 179 EXPECT_CALL(*settings, GetURLFetcherForAvailabilityCheck()) 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. 192 template void 193 DataReductionProxySettingsTestBase::SetProbeResult<DataReductionProxySettings>( 194 const std::string& test_url, 195 const std::string& response, 196 ProbeURLFetchResult result, 197 bool success, 198 int expected_calls); 199 200 void DataReductionProxySettingsTestBase::CheckProxyConfigs( 201 bool expected_enabled, 202 bool expected_restricted, 203 bool expected_fallback_restricted) { 204 TestDataReductionProxyConfig* config = 205 static_cast<TestDataReductionProxyConfig*>(settings_->configurator_); 206 ASSERT_EQ(expected_restricted, config->restricted_); 207 ASSERT_EQ(expected_fallback_restricted, config->fallback_restricted_); 208 ASSERT_EQ(expected_enabled, config->enabled_); 209 } 210 211 void DataReductionProxySettingsTestBase::CheckProbe( 212 bool initially_enabled, 213 const std::string& probe_url, 214 const std::string& response, 215 bool request_succeeded, 216 bool expected_enabled, 217 bool expected_restricted, 218 bool expected_fallback_restricted) { 219 pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, 220 initially_enabled); 221 if (initially_enabled) 222 settings_->enabled_by_user_ = true; 223 settings_->restricted_by_carrier_ = false; 224 SetProbeResult(probe_url, 225 response, 226 FetchResult(initially_enabled, 227 request_succeeded && (response == "OK")), 228 request_succeeded, 229 initially_enabled ? 1 : 0); 230 settings_->MaybeActivateDataReductionProxy(false); 231 base::MessageLoop::current()->RunUntilIdle(); 232 CheckProxyConfigs(expected_enabled, 233 expected_restricted, 234 expected_fallback_restricted); 235 } 236 237 void DataReductionProxySettingsTestBase::CheckProbeOnIPChange( 238 const std::string& probe_url, 239 const std::string& response, 240 bool request_succeeded, 241 bool expected_restricted, 242 bool expected_fallback_restricted) { 243 SetProbeResult(probe_url, 244 response, 245 FetchResult(!settings_->restricted_by_carrier_, 246 request_succeeded && (response == "OK")), 247 request_succeeded, 248 1); 249 settings_->OnIPAddressChanged(); 250 base::MessageLoop::current()->RunUntilIdle(); 251 CheckProxyConfigs(true, expected_restricted, expected_fallback_restricted); 252 } 253 254 void DataReductionProxySettingsTestBase::CheckOnPrefChange( 255 bool enabled, 256 bool expected_enabled, 257 bool managed) { 258 // Always have a sucessful probe for pref change tests. 259 SetProbeResult(kProbeURLWithOKResponse, 260 "OK", 261 FetchResult(enabled, true), 262 true, 263 expected_enabled ? 1 : 0); 264 if (managed) { 265 pref_service_.SetManagedPref(prefs::kDataReductionProxyEnabled, 266 new base::FundamentalValue(enabled)); 267 } else { 268 pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, enabled); 269 } 270 base::MessageLoop::current()->RunUntilIdle(); 271 // Never expect the proxy to be restricted for pref change tests. 272 CheckProxyConfigs(expected_enabled, false, false); 273 } 274 275 void DataReductionProxySettingsTestBase::CheckInitDataReductionProxy( 276 bool enabled_at_startup) { 277 base::MessageLoopForUI loop; 278 SetProbeResult(kProbeURLWithOKResponse, 279 "OK", 280 FetchResult(enabled_at_startup, true), 281 true, 282 enabled_at_startup ? 1 : 0); 283 scoped_ptr<DataReductionProxyConfigurator> configurator( 284 new TestDataReductionProxyConfig()); 285 settings_->SetProxyConfigurator(configurator.get()); 286 scoped_refptr<net::TestURLRequestContextGetter> request_context = 287 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current()); 288 289 settings_->InitDataReductionProxySettings( 290 &pref_service_, 291 request_context.get()); 292 settings_->SetOnDataReductionEnabledCallback( 293 base::Bind(&DataReductionProxySettingsTestBase:: 294 RegisterSyntheticFieldTrialCallback, 295 base::Unretained(this))); 296 297 base::MessageLoop::current()->RunUntilIdle(); 298 CheckProxyConfigs(enabled_at_startup, false, false); 299 EXPECT_EQ(enabled_at_startup, proxy_enabled_); 300 } 301 302 } // namespace data_reduction_proxy 303