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. 4 5 #ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_TEST_UTILS_H_ 6 #define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_TEST_UTILS_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <memory> 12 #include <string> 13 #include <vector> 14 15 #include "base/macros.h" 16 #include "base/memory/ref_counted.h" 17 #include "base/single_thread_task_runner.h" 18 #include "base/strings/string_piece.h" 19 #include "base/time/clock.h" 20 #include "base/time/tick_clock.h" 21 #include "base/time/time.h" 22 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config_service_client.h" 23 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.h" 24 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h" 25 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h" 26 #include "components/data_reduction_proxy/core/browser/data_store.h" 27 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h" 28 #include "components/data_use_measurement/core/data_use_measurement.h" 29 #include "components/prefs/pref_registry_simple.h" 30 #include "components/prefs/testing_pref_service.h" 31 #include "net/base/backoff_entry.h" 32 #include "net/base/proxy_server.h" 33 #include "services/network/test/test_network_connection_tracker.h" 34 #include "testing/gmock/include/gmock/gmock.h" 35 36 class GURL; 37 class TestingPrefServiceSimple; 38 39 namespace network { 40 class SharedURLLoaderFactory; 41 class TestNetworkQualityTracker; 42 class TestURLLoaderFactory; 43 } // namespace network 44 45 namespace data_reduction_proxy { 46 47 class ClientConfig; 48 class DataReductionProxyRequestOptions; 49 class DataReductionProxySettings; 50 51 // Test version of |DataReductionProxyRequestOptions|. 52 class TestDataReductionProxyRequestOptions 53 : public DataReductionProxyRequestOptions { 54 public: 55 TestDataReductionProxyRequestOptions(Client client, 56 const std::string& version); 57 58 // Overrides of DataReductionProxyRequestOptions. 59 std::string GetDefaultKey() const override; 60 61 using DataReductionProxyRequestOptions::GetHeaderValueForTesting; 62 }; 63 64 // Mock version of |DataReductionProxyRequestOptions|. 65 class MockDataReductionProxyRequestOptions 66 : public TestDataReductionProxyRequestOptions { 67 public: 68 explicit MockDataReductionProxyRequestOptions(Client client); 69 70 ~MockDataReductionProxyRequestOptions() override; 71 72 MOCK_CONST_METHOD1(PopulateConfigResponse, void(ClientConfig* config)); 73 }; 74 75 // Test version of |DataReductionProxyConfigServiceClient|, which permits 76 // finely controlling the backoff timer. 77 class TestDataReductionProxyConfigServiceClient 78 : public DataReductionProxyConfigServiceClient { 79 public: 80 TestDataReductionProxyConfigServiceClient( 81 DataReductionProxyRequestOptions* request_options, 82 DataReductionProxyService* service, 83 network::NetworkConnectionTracker* network_connection_tracker, 84 ConfigStorer config_storer, 85 const net::BackoffEntry::Policy& backoff_policy); 86 87 ~TestDataReductionProxyConfigServiceClient() override; 88 89 using DataReductionProxyConfigServiceClient::OnConnectionChanged; 90 91 void SetNow(const base::Time& time); 92 93 void SetCustomReleaseTime(const base::TimeTicks& release_time); 94 95 base::TimeDelta GetDelay() const; 96 97 int GetBackoffErrorCount(); 98 99 base::TimeDelta GetBackoffTimeUntilRelease() const; 100 101 void SetConfigServiceURL(const GURL& service_url); 102 103 int32_t failed_attempts_before_success() const; 104 105 #if defined(OS_ANDROID) 106 bool IsApplicationStateBackground() const override; 107 set_application_state_background(bool new_state)108 void set_application_state_background(bool new_state) { 109 is_application_state_background_ = new_state; 110 } 111 foreground_fetch_pending()112 bool foreground_fetch_pending() const { return foreground_fetch_pending_; } 113 114 // Triggers the callback for Chromium status change to foreground. 115 void TriggerApplicationStatusToForeground(); 116 #endif 117 118 void SetRemoteConfigApplied(bool remote_config_applied); 119 120 bool RemoteConfigApplied() const override; 121 122 protected: 123 // Overrides of DataReductionProxyConfigServiceClient 124 base::Time Now() override; 125 net::BackoffEntry* GetBackoffEntry() override; 126 127 private: 128 // A clock which returns a fixed value in both base::Time and base::TimeTicks. 129 class TestTickClock : public base::Clock, public base::TickClock { 130 public: 131 TestTickClock(const base::Time& initial_time); 132 133 // base::TickClock implementation. 134 base::TimeTicks NowTicks() const override; 135 136 // base::Clock implementation. 137 base::Time Now() const override; 138 139 // Sets the current time. 140 void SetTime(const base::Time& time); 141 142 private: 143 base::Time time_; 144 }; 145 146 #if defined(OS_ANDROID) 147 bool is_application_state_background_; 148 #endif 149 150 TestTickClock tick_clock_; 151 net::BackoffEntry test_backoff_entry_; 152 153 base::Optional<bool> remote_config_applied_; 154 155 DISALLOW_COPY_AND_ASSIGN(TestDataReductionProxyConfigServiceClient); 156 }; 157 158 // Test version of |DataReductionProxyService|, which permits mocking of various 159 // methods. 160 class MockDataReductionProxyService : public DataReductionProxyService { 161 public: 162 MockDataReductionProxyService( 163 data_use_measurement::DataUseMeasurement* data_use_measurement, 164 DataReductionProxySettings* settings, 165 network::TestNetworkQualityTracker* test_network_quality_tracker, 166 PrefService* prefs, 167 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, 168 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); 169 ~MockDataReductionProxyService() override; 170 171 MOCK_METHOD2(SetProxyPrefs, void(bool enabled, bool at_startup)); 172 MOCK_METHOD8( 173 UpdateContentLengths, 174 void(int64_t data_used, 175 int64_t original_size, 176 bool data_reduction_proxy_enabled, 177 data_reduction_proxy::DataReductionProxyRequestType request_type, 178 const std::string& mime_type, 179 bool is_user_traffic, 180 data_use_measurement::DataUseUserData::DataUseContentType 181 content_type, 182 int32_t service_hash_code)); 183 MOCK_METHOD3(UpdateDataUseForHost, 184 void(int64_t network_bytes, 185 int64_t original_bytes, 186 const std::string& host)); 187 }; 188 189 // Test version of |DataReductionProxyService|, which bypasses initialization in 190 // the constructor in favor of explicitly passing in its owned classes. This 191 // permits the use of test/mock versions of those classes. 192 class TestDataReductionProxyService : public DataReductionProxyService { 193 public: 194 TestDataReductionProxyService( 195 data_use_measurement::DataUseMeasurement* data_use_measurement, 196 DataReductionProxySettings* settings, 197 PrefService* prefs, 198 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, 199 network::NetworkQualityTracker* network_quality_tracker, 200 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner); 201 ~TestDataReductionProxyService() override; 202 ignore_blocklist()203 bool ignore_blocklist() const { return ignore_blocklist_; } 204 205 private: 206 // Whether the long term blocklist rules should be ignored. 207 bool ignore_blocklist_ = false; 208 }; 209 210 // Test version of |DataStore|. Uses an in memory hash map to store data. 211 class TestDataStore : public data_reduction_proxy::DataStore { 212 public: 213 TestDataStore(); 214 215 ~TestDataStore() override; 216 InitializeOnDBThread()217 void InitializeOnDBThread() override {} 218 219 DataStore::Status Get(base::StringPiece key, std::string* value) override; 220 221 DataStore::Status Put(const std::map<std::string, std::string>& map) override; 222 223 DataStore::Status Delete(base::StringPiece key) override; 224 225 DataStore::Status RecreateDB() override; 226 map()227 std::map<std::string, std::string>* map() { return &map_; } 228 229 private: 230 std::map<std::string, std::string> map_; 231 }; 232 233 // Builds a test version of the Data Reduction Proxy stack for use in tests. 234 // Takes in various |TestContextOptions| which controls the behavior of the 235 // underlying objects. 236 class DataReductionProxyTestContext { 237 public: 238 // Allows for a fluent builder interface to configure what kind of objects 239 // (test vs mock vs real) are used by the |DataReductionProxyTestContext|. 240 class Builder { 241 public: 242 Builder(); 243 244 ~Builder(); 245 246 // The |Client| enum to use for |DataReductionProxyRequestOptions|. 247 Builder& WithClient(Client client); 248 249 // Specifies a |network::URLLoaderFactory| to use. 250 Builder& WithURLLoaderFactory( 251 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory); 252 253 // Specifies the use of |MockDataReductionProxyConfig| instead of 254 // |TestDataReductionProxyConfig|. 255 Builder& WithMockConfig(); 256 257 // Specifies the use of |MockDataReductionProxyService| instead of 258 // |DataReductionProxyService|. 259 Builder& WithMockDataReductionProxyService(); 260 261 // Specifies the use of |MockDataReductionProxyRequestOptions| instead of 262 // |DataReductionProxyRequestOptions|. 263 Builder& WithMockRequestOptions(); 264 265 // Specifies the use of the |DataReductionProxyConfigServiceClient|. 266 Builder& WithConfigClient(); 267 268 // Specifies the use of the a |TestDataReductionProxyConfigServiceClient| 269 // instead of a |DataReductionProxyConfigServiceClient|. 270 Builder& WithTestConfigClient(); 271 272 // Construct, but do not initialize the |DataReductionProxySettings| object. 273 Builder& SkipSettingsInitialization(); 274 275 // Specifies a settings object to use. 276 Builder& WithSettings(std::unique_ptr<DataReductionProxySettings> settings); 277 278 // Creates a |DataReductionProxyTestContext|. Owned by the caller. 279 std::unique_ptr<DataReductionProxyTestContext> Build(); 280 281 private: 282 Client client_; 283 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_; 284 285 bool use_mock_config_; 286 bool use_mock_service_; 287 bool use_mock_request_options_; 288 bool use_config_client_; 289 bool use_test_config_client_; 290 bool skip_settings_initialization_; 291 std::unique_ptr<DataReductionProxySettings> settings_; 292 std::unique_ptr<data_use_measurement::DataUseMeasurement> 293 data_use_measurement_; 294 }; 295 296 virtual ~DataReductionProxyTestContext(); 297 298 // Registers, sets, and gets the preference used to enable the Data Reduction 299 // Proxy, respectively. 300 void RegisterDataReductionProxyEnabledPref(); 301 void SetDataReductionProxyEnabled(bool enabled); 302 bool IsDataReductionProxyEnabled() const; 303 304 // Waits while executing all tasks on the current SingleThreadTaskRunner. 305 void RunUntilIdle(); 306 307 // Initializes the |DataReductionProxySettings| object. Can only be called if 308 // built with SkipSettingsInitialization. 309 void InitSettings(); 310 311 // Destroys the |DataReductionProxySettings| object and waits until objects on 312 // the DB task runner are destroyed. 313 void DestroySettings(); 314 315 // Takes ownership of the |DataReductionProxyService| object. Can only be 316 // called if built with SkipSettingsInitialization. 317 std::unique_ptr<DataReductionProxyService> TakeService(); 318 319 // Enable the Data Reduction Proxy, simulating a successful secure proxy 320 // check. This can only be called if not built with WithTestConfigurator, 321 // |settings_| has been initialized, and |this| was built with a 322 // |net::MockClientSocketFactory| specified. 323 void EnableDataReductionProxyWithSecureProxyCheckSuccess(); 324 325 326 DataReductionProxyService* data_reduction_proxy_service() const; 327 328 // Returns the underlying |TestDataReductionProxyService|. This can only be 329 // called if not built with WithMockDataReductionProxyService. 330 TestDataReductionProxyService* test_data_reduction_proxy_service() const; 331 332 // Returns the underlying |MockDataReductionProxyService|. This can only 333 // be called if built with WithMockDataReductionProxyService. 334 MockDataReductionProxyService* mock_data_reduction_proxy_service() const; 335 336 // Returns the underlying |MockDataReductionProxyRequestOptions|. This can 337 // only be called if built with WithMockRequestOptions. 338 MockDataReductionProxyRequestOptions* mock_request_options() const; 339 340 341 // Returns the underlying |TestDataReductionProxyConfigServiceClient|. This 342 // can only be called if built with WithTestConfigClient. 343 TestDataReductionProxyConfigServiceClient* test_config_client(); 344 task_runner()345 scoped_refptr<base::SingleThreadTaskRunner> task_runner() const { 346 return task_runner_; 347 } 348 pref_service()349 TestingPrefServiceSimple* pref_service() { 350 return simple_pref_service_.get(); 351 } 352 url_loader_factory()353 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory() const { 354 return test_shared_url_loader_factory_; 355 } 356 settings()357 DataReductionProxySettings* settings() const { return settings_.get(); } 358 test_network_quality_tracker()359 network::TestNetworkQualityTracker* test_network_quality_tracker() const { 360 return test_network_quality_tracker_.get(); 361 } 362 363 void InitSettingsWithoutCheck(); 364 365 366 private: 367 // Used to storage a serialized Data Reduction Proxy config. 368 class TestConfigStorer { 369 public: 370 // |prefs| must not be null and outlive |this|. 371 TestConfigStorer(PrefService* prefs); 372 373 // Stores |serialized_config| in |prefs_|. 374 void StoreSerializedConfig(const std::string& serialized_config); 375 376 private: 377 PrefService* prefs_; 378 }; 379 380 DataReductionProxyTestContext( 381 std::unique_ptr<data_use_measurement::DataUseMeasurement> 382 data_use_measurement, 383 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, 384 std::unique_ptr<TestingPrefServiceSimple> simple_pref_service, 385 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory, 386 std::unique_ptr<network::TestURLLoaderFactory> test_url_loader_factory, 387 std::unique_ptr<DataReductionProxySettings> settings, 388 std::unique_ptr<DataReductionProxyService> service, 389 std::unique_ptr<network::TestNetworkQualityTracker> 390 test_network_quality_tracker, 391 std::unique_ptr<TestConfigStorer> config_storer, 392 unsigned int test_context_flags); 393 394 std::unique_ptr<data_use_measurement::DataUseMeasurement> 395 data_use_measurement_; 396 397 unsigned int test_context_flags_; 398 399 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 400 std::unique_ptr<TestingPrefServiceSimple> simple_pref_service_; 401 scoped_refptr<network::SharedURLLoaderFactory> 402 test_shared_url_loader_factory_; 403 std::unique_ptr<network::TestURLLoaderFactory> test_url_loader_factory_; 404 405 std::unique_ptr<DataReductionProxySettings> settings_; 406 DataReductionProxyService* data_reduction_proxy_service_; 407 std::unique_ptr<network::TestNetworkQualityTracker> 408 test_network_quality_tracker_; 409 std::unique_ptr<DataReductionProxyService> service_; 410 std::unique_ptr<TestConfigStorer> config_storer_; 411 412 DISALLOW_COPY_AND_ASSIGN(DataReductionProxyTestContext); 413 }; 414 415 } // namespace data_reduction_proxy 416 417 #endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_TEST_UTILS_H_ 418