| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/core/browser/data_reduction_proxy_conf
ig_service_client.h" | 5 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_conf
ig_service_client.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 void VerifyRemoteSuccess() { | 196 void VerifyRemoteSuccess() { |
| 197 std::vector<net::ProxyServer> expected_http_proxies; | 197 std::vector<net::ProxyServer> expected_http_proxies; |
| 198 expected_http_proxies.push_back(net::ProxyServer::FromURI( | 198 expected_http_proxies.push_back(net::ProxyServer::FromURI( |
| 199 kSuccessOrigin, net::ProxyServer::SCHEME_HTTP)); | 199 kSuccessOrigin, net::ProxyServer::SCHEME_HTTP)); |
| 200 expected_http_proxies.push_back(net::ProxyServer::FromURI( | 200 expected_http_proxies.push_back(net::ProxyServer::FromURI( |
| 201 kSuccessFallback, net::ProxyServer::SCHEME_HTTP)); | 201 kSuccessFallback, net::ProxyServer::SCHEME_HTTP)); |
| 202 EXPECT_EQ(base::TimeDelta::FromSeconds(kConfigRefreshDurationSeconds), | 202 EXPECT_EQ(base::TimeDelta::FromSeconds(kConfigRefreshDurationSeconds), |
| 203 config_client()->GetDelay()); | 203 config_client()->GetDelay()); |
| 204 EXPECT_THAT(configurator()->proxies_for_http(), | 204 EXPECT_THAT(configurator()->proxies_for_http(), |
| 205 testing::ContainerEq(expected_http_proxies)); | 205 testing::ContainerEq(expected_http_proxies)); |
| 206 EXPECT_TRUE(configurator()->proxies_for_https().empty()); | |
| 207 EXPECT_EQ(kSuccessSessionKey, request_options()->GetSecureSession()); | 206 EXPECT_EQ(kSuccessSessionKey, request_options()->GetSecureSession()); |
| 208 // The config should be persisted on the pref. | 207 // The config should be persisted on the pref. |
| 209 EXPECT_EQ(encoded_config(), persisted_config()); | 208 EXPECT_EQ(encoded_config(), persisted_config()); |
| 210 } | 209 } |
| 211 | 210 |
| 212 void VerifyRemoteSuccessWithOldConfig() { | 211 void VerifyRemoteSuccessWithOldConfig() { |
| 213 std::vector<net::ProxyServer> expected_http_proxies; | 212 std::vector<net::ProxyServer> expected_http_proxies; |
| 214 expected_http_proxies.push_back(net::ProxyServer::FromURI( | 213 expected_http_proxies.push_back(net::ProxyServer::FromURI( |
| 215 kOldSuccessOrigin, net::ProxyServer::SCHEME_HTTP)); | 214 kOldSuccessOrigin, net::ProxyServer::SCHEME_HTTP)); |
| 216 expected_http_proxies.push_back(net::ProxyServer::FromURI( | 215 expected_http_proxies.push_back(net::ProxyServer::FromURI( |
| 217 kOldSuccessFallback, net::ProxyServer::SCHEME_HTTP)); | 216 kOldSuccessFallback, net::ProxyServer::SCHEME_HTTP)); |
| 218 EXPECT_EQ(base::TimeDelta::FromSeconds(kConfigRefreshDurationSeconds), | 217 EXPECT_EQ(base::TimeDelta::FromSeconds(kConfigRefreshDurationSeconds), |
| 219 config_client()->GetDelay()); | 218 config_client()->GetDelay()); |
| 220 EXPECT_THAT(configurator()->proxies_for_http(), | 219 EXPECT_THAT(configurator()->proxies_for_http(), |
| 221 testing::ContainerEq(expected_http_proxies)); | 220 testing::ContainerEq(expected_http_proxies)); |
| 222 EXPECT_TRUE(configurator()->proxies_for_https().empty()); | |
| 223 EXPECT_EQ(kOldSuccessSessionKey, request_options()->GetSecureSession()); | 221 EXPECT_EQ(kOldSuccessSessionKey, request_options()->GetSecureSession()); |
| 224 } | 222 } |
| 225 | 223 |
| 226 void VerifySuccessWithLoadedConfig() { | 224 void VerifySuccessWithLoadedConfig() { |
| 227 std::vector<net::ProxyServer> expected_http_proxies; | 225 std::vector<net::ProxyServer> expected_http_proxies; |
| 228 expected_http_proxies.push_back(net::ProxyServer::FromURI( | 226 expected_http_proxies.push_back(net::ProxyServer::FromURI( |
| 229 kPersistedOrigin, net::ProxyServer::SCHEME_HTTP)); | 227 kPersistedOrigin, net::ProxyServer::SCHEME_HTTP)); |
| 230 expected_http_proxies.push_back(net::ProxyServer::FromURI( | 228 expected_http_proxies.push_back(net::ProxyServer::FromURI( |
| 231 kPersistedFallback, net::ProxyServer::SCHEME_HTTP)); | 229 kPersistedFallback, net::ProxyServer::SCHEME_HTTP)); |
| 232 EXPECT_THAT(configurator()->proxies_for_http(), | 230 EXPECT_THAT(configurator()->proxies_for_http(), |
| 233 testing::ContainerEq(expected_http_proxies)); | 231 testing::ContainerEq(expected_http_proxies)); |
| 234 EXPECT_TRUE(configurator()->proxies_for_https().empty()); | |
| 235 EXPECT_EQ(kPersistedSessionKey, request_options()->GetSecureSession()); | 232 EXPECT_EQ(kPersistedSessionKey, request_options()->GetSecureSession()); |
| 236 } | 233 } |
| 237 | 234 |
| 238 TestDataReductionProxyConfigServiceClient* config_client() { | 235 TestDataReductionProxyConfigServiceClient* config_client() { |
| 239 return test_context_->test_config_client(); | 236 return test_context_->test_config_client(); |
| 240 } | 237 } |
| 241 | 238 |
| 242 TestDataReductionProxyConfigurator* configurator() { | 239 TestDataReductionProxyConfigurator* configurator() { |
| 243 return test_context_->test_configurator(); | 240 return test_context_->test_configurator(); |
| 244 } | 241 } |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 net::ProxyServer::FromURI( | 393 net::ProxyServer::FromURI( |
| 397 tests[i].expected_primary_proxy, | 394 tests[i].expected_primary_proxy, |
| 398 tests[i].expected_primary_proxy_scheme) | 395 tests[i].expected_primary_proxy_scheme) |
| 399 .host_port_pair()), | 396 .host_port_pair()), |
| 400 proxies_for_http[0]) | 397 proxies_for_http[0]) |
| 401 << i; | 398 << i; |
| 402 EXPECT_EQ(net::ProxyServer::FromURI(tests[i].expected_fallback_proxy, | 399 EXPECT_EQ(net::ProxyServer::FromURI(tests[i].expected_fallback_proxy, |
| 403 net::ProxyServer::SCHEME_HTTP), | 400 net::ProxyServer::SCHEME_HTTP), |
| 404 proxies_for_http[1]) | 401 proxies_for_http[1]) |
| 405 << i; | 402 << i; |
| 406 EXPECT_TRUE(configurator()->proxies_for_https().empty()) << i; | |
| 407 | 403 |
| 408 // Test that the trusted SPDY proxy is updated correctly after each config | 404 // Test that the trusted SPDY proxy is updated correctly after each config |
| 409 // retrieval. | 405 // retrieval. |
| 410 bool expect_proxy_is_trusted = | 406 bool expect_proxy_is_trusted = |
| 411 tests[i].expected_primary_proxy_scheme == | 407 tests[i].expected_primary_proxy_scheme == |
| 412 net::ProxyServer::SCHEME_HTTPS && | 408 net::ProxyServer::SCHEME_HTTPS && |
| 413 tests[i].enable_trusted_spdy_proxy_field_trial; | 409 tests[i].enable_trusted_spdy_proxy_field_trial; |
| 414 | 410 |
| 415 // Apply the specified proxy scheme. | 411 // Apply the specified proxy scheme. |
| 416 const net::ProxyServer proxy_server( | 412 const net::ProxyServer proxy_server( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 430 Init(true); | 426 Init(true); |
| 431 // Use a local/static config. | 427 // Use a local/static config. |
| 432 base::HistogramTester histogram_tester; | 428 base::HistogramTester histogram_tester; |
| 433 AddMockFailure(); | 429 AddMockFailure(); |
| 434 AddMockFailure(); | 430 AddMockFailure(); |
| 435 | 431 |
| 436 EXPECT_EQ(0, config_client()->failed_attempts_before_success()); | 432 EXPECT_EQ(0, config_client()->failed_attempts_before_success()); |
| 437 | 433 |
| 438 SetDataReductionProxyEnabled(true); | 434 SetDataReductionProxyEnabled(true); |
| 439 EXPECT_TRUE(configurator()->proxies_for_http().empty()); | 435 EXPECT_TRUE(configurator()->proxies_for_http().empty()); |
| 440 EXPECT_TRUE(configurator()->proxies_for_https().empty()); | |
| 441 | 436 |
| 442 // First attempt should be unsuccessful. | 437 // First attempt should be unsuccessful. |
| 443 config_client()->RetrieveConfig(); | 438 config_client()->RetrieveConfig(); |
| 444 RunUntilIdle(); | 439 RunUntilIdle(); |
| 445 EXPECT_TRUE(configurator()->proxies_for_http().empty()); | 440 EXPECT_TRUE(configurator()->proxies_for_http().empty()); |
| 446 EXPECT_TRUE(configurator()->proxies_for_https().empty()); | |
| 447 EXPECT_EQ(base::TimeDelta::FromSeconds(20), config_client()->GetDelay()); | 441 EXPECT_EQ(base::TimeDelta::FromSeconds(20), config_client()->GetDelay()); |
| 448 | 442 |
| 449 #if defined(OS_ANDROID) | 443 #if defined(OS_ANDROID) |
| 450 EXPECT_FALSE(config_client()->foreground_fetch_pending()); | 444 EXPECT_FALSE(config_client()->foreground_fetch_pending()); |
| 451 #endif | 445 #endif |
| 452 | 446 |
| 453 // Second attempt should be unsuccessful and backoff time should increase. | 447 // Second attempt should be unsuccessful and backoff time should increase. |
| 454 config_client()->RetrieveConfig(); | 448 config_client()->RetrieveConfig(); |
| 455 RunUntilIdle(); | 449 RunUntilIdle(); |
| 456 EXPECT_TRUE(configurator()->proxies_for_http().empty()); | 450 EXPECT_TRUE(configurator()->proxies_for_http().empty()); |
| 457 EXPECT_TRUE(configurator()->proxies_for_https().empty()); | |
| 458 EXPECT_EQ(base::TimeDelta::FromSeconds(40), config_client()->GetDelay()); | 451 EXPECT_EQ(base::TimeDelta::FromSeconds(40), config_client()->GetDelay()); |
| 459 EXPECT_TRUE(persisted_config().empty()); | 452 EXPECT_TRUE(persisted_config().empty()); |
| 460 | 453 |
| 461 #if defined(OS_ANDROID) | 454 #if defined(OS_ANDROID) |
| 462 EXPECT_FALSE(config_client()->foreground_fetch_pending()); | 455 EXPECT_FALSE(config_client()->foreground_fetch_pending()); |
| 463 #endif | 456 #endif |
| 464 | 457 |
| 465 EXPECT_EQ(2, config_client()->failed_attempts_before_success()); | 458 EXPECT_EQ(2, config_client()->failed_attempts_before_success()); |
| 466 histogram_tester.ExpectTotalCount( | 459 histogram_tester.ExpectTotalCount( |
| 467 "DataReductionProxy.ConfigService.FetchFailedAttemptsBeforeSuccess", 0); | 460 "DataReductionProxy.ConfigService.FetchFailedAttemptsBeforeSuccess", 0); |
| 468 } | 461 } |
| 469 | 462 |
| 470 // Tests that the config is read successfully on the first attempt. | 463 // Tests that the config is read successfully on the first attempt. |
| 471 TEST_F(DataReductionProxyConfigServiceClientTest, RemoteConfigSuccess) { | 464 TEST_F(DataReductionProxyConfigServiceClientTest, RemoteConfigSuccess) { |
| 472 Init(true); | 465 Init(true); |
| 473 AddMockSuccess(); | 466 AddMockSuccess(); |
| 474 SetDataReductionProxyEnabled(true); | 467 SetDataReductionProxyEnabled(true); |
| 475 EXPECT_TRUE(configurator()->proxies_for_http().empty()); | 468 EXPECT_TRUE(configurator()->proxies_for_http().empty()); |
| 476 EXPECT_TRUE(configurator()->proxies_for_https().empty()); | |
| 477 config_client()->RetrieveConfig(); | 469 config_client()->RetrieveConfig(); |
| 478 RunUntilIdle(); | 470 RunUntilIdle(); |
| 479 VerifyRemoteSuccess(); | 471 VerifyRemoteSuccess(); |
| 480 #if defined(OS_ANDROID) | 472 #if defined(OS_ANDROID) |
| 481 EXPECT_FALSE(config_client()->foreground_fetch_pending()); | 473 EXPECT_FALSE(config_client()->foreground_fetch_pending()); |
| 482 #endif | 474 #endif |
| 483 } | 475 } |
| 484 | 476 |
| 485 // Tests that the config is read successfully on the second attempt. | 477 // Tests that the config is read successfully on the second attempt. |
| 486 TEST_F(DataReductionProxyConfigServiceClientTest, | 478 TEST_F(DataReductionProxyConfigServiceClientTest, |
| 487 RemoteConfigSuccessAfterFailure) { | 479 RemoteConfigSuccessAfterFailure) { |
| 488 Init(true); | 480 Init(true); |
| 489 base::HistogramTester histogram_tester; | 481 base::HistogramTester histogram_tester; |
| 490 | 482 |
| 491 AddMockFailure(); | 483 AddMockFailure(); |
| 492 AddMockSuccess(); | 484 AddMockSuccess(); |
| 493 | 485 |
| 494 EXPECT_EQ(0, config_client()->failed_attempts_before_success()); | 486 EXPECT_EQ(0, config_client()->failed_attempts_before_success()); |
| 495 | 487 |
| 496 SetDataReductionProxyEnabled(true); | 488 SetDataReductionProxyEnabled(true); |
| 497 EXPECT_TRUE(configurator()->proxies_for_http().empty()); | 489 EXPECT_TRUE(configurator()->proxies_for_http().empty()); |
| 498 EXPECT_TRUE(configurator()->proxies_for_https().empty()); | |
| 499 | 490 |
| 500 // First attempt should be unsuccessful. | 491 // First attempt should be unsuccessful. |
| 501 config_client()->RetrieveConfig(); | 492 config_client()->RetrieveConfig(); |
| 502 RunUntilIdle(); | 493 RunUntilIdle(); |
| 503 EXPECT_EQ(1, config_client()->failed_attempts_before_success()); | 494 EXPECT_EQ(1, config_client()->failed_attempts_before_success()); |
| 504 EXPECT_EQ(base::TimeDelta::FromSeconds(20), config_client()->GetDelay()); | 495 EXPECT_EQ(base::TimeDelta::FromSeconds(20), config_client()->GetDelay()); |
| 505 EXPECT_TRUE(configurator()->proxies_for_http().empty()); | 496 EXPECT_TRUE(configurator()->proxies_for_http().empty()); |
| 506 EXPECT_TRUE(configurator()->proxies_for_https().empty()); | |
| 507 EXPECT_TRUE(request_options()->GetSecureSession().empty()); | 497 EXPECT_TRUE(request_options()->GetSecureSession().empty()); |
| 508 | 498 |
| 509 // Second attempt should be successful. | 499 // Second attempt should be successful. |
| 510 config_client()->RetrieveConfig(); | 500 config_client()->RetrieveConfig(); |
| 511 RunUntilIdle(); | 501 RunUntilIdle(); |
| 512 VerifyRemoteSuccess(); | 502 VerifyRemoteSuccess(); |
| 513 EXPECT_EQ(0, config_client()->failed_attempts_before_success()); | 503 EXPECT_EQ(0, config_client()->failed_attempts_before_success()); |
| 514 | 504 |
| 515 histogram_tester.ExpectUniqueSample( | 505 histogram_tester.ExpectUniqueSample( |
| 516 "DataReductionProxy.ConfigService.FetchFailedAttemptsBeforeSuccess", 1, | 506 "DataReductionProxy.ConfigService.FetchFailedAttemptsBeforeSuccess", 1, |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 776 } | 766 } |
| 777 | 767 |
| 778 // Tests that remote config can be applied after the serialized config has been | 768 // Tests that remote config can be applied after the serialized config has been |
| 779 // applied. | 769 // applied. |
| 780 TEST_F(DataReductionProxyConfigServiceClientTest, ApplySerializedConfig) { | 770 TEST_F(DataReductionProxyConfigServiceClientTest, ApplySerializedConfig) { |
| 781 Init(true); | 771 Init(true); |
| 782 AddMockSuccess(); | 772 AddMockSuccess(); |
| 783 | 773 |
| 784 SetDataReductionProxyEnabled(true); | 774 SetDataReductionProxyEnabled(true); |
| 785 EXPECT_TRUE(configurator()->proxies_for_http().empty()); | 775 EXPECT_TRUE(configurator()->proxies_for_http().empty()); |
| 786 EXPECT_TRUE(configurator()->proxies_for_https().empty()); | |
| 787 config_client()->ApplySerializedConfig(loaded_config()); | 776 config_client()->ApplySerializedConfig(loaded_config()); |
| 788 VerifySuccessWithLoadedConfig(); | 777 VerifySuccessWithLoadedConfig(); |
| 789 EXPECT_TRUE(persisted_config().empty()); | 778 EXPECT_TRUE(persisted_config().empty()); |
| 790 | 779 |
| 791 config_client()->RetrieveConfig(); | 780 config_client()->RetrieveConfig(); |
| 792 RunUntilIdle(); | 781 RunUntilIdle(); |
| 793 VerifyRemoteSuccess(); | 782 VerifyRemoteSuccess(); |
| 794 } | 783 } |
| 795 | 784 |
| 796 // Tests that serialized config has no effect after the config has been | 785 // Tests that serialized config has no effect after the config has been |
| 797 // retrieved successfully. | 786 // retrieved successfully. |
| 798 TEST_F(DataReductionProxyConfigServiceClientTest, | 787 TEST_F(DataReductionProxyConfigServiceClientTest, |
| 799 ApplySerializedConfigAfterReceipt) { | 788 ApplySerializedConfigAfterReceipt) { |
| 800 Init(true); | 789 Init(true); |
| 801 AddMockSuccess(); | 790 AddMockSuccess(); |
| 802 | 791 |
| 803 SetDataReductionProxyEnabled(true); | 792 SetDataReductionProxyEnabled(true); |
| 804 EXPECT_TRUE(configurator()->proxies_for_http().empty()); | 793 EXPECT_TRUE(configurator()->proxies_for_http().empty()); |
| 805 EXPECT_TRUE(configurator()->proxies_for_https().empty()); | |
| 806 EXPECT_TRUE(request_options()->GetSecureSession().empty()); | 794 EXPECT_TRUE(request_options()->GetSecureSession().empty()); |
| 807 | 795 |
| 808 // Retrieve the remote config. | 796 // Retrieve the remote config. |
| 809 config_client()->RetrieveConfig(); | 797 config_client()->RetrieveConfig(); |
| 810 RunUntilIdle(); | 798 RunUntilIdle(); |
| 811 VerifyRemoteSuccess(); | 799 VerifyRemoteSuccess(); |
| 812 | 800 |
| 813 // ApplySerializedConfig should not have any effect since the remote config is | 801 // ApplySerializedConfig should not have any effect since the remote config is |
| 814 // already applied. | 802 // already applied. |
| 815 config_client()->ApplySerializedConfig(encoded_config()); | 803 config_client()->ApplySerializedConfig(encoded_config()); |
| 816 VerifyRemoteSuccess(); | 804 VerifyRemoteSuccess(); |
| 817 } | 805 } |
| 818 | 806 |
| 819 // Tests that a local serialized config can be applied successfully if remote | 807 // Tests that a local serialized config can be applied successfully if remote |
| 820 // config has not been fetched so far. | 808 // config has not been fetched so far. |
| 821 TEST_F(DataReductionProxyConfigServiceClientTest, ApplySerializedConfigLocal) { | 809 TEST_F(DataReductionProxyConfigServiceClientTest, ApplySerializedConfigLocal) { |
| 822 Init(true); | 810 Init(true); |
| 823 SetDataReductionProxyEnabled(true); | 811 SetDataReductionProxyEnabled(true); |
| 824 EXPECT_TRUE(configurator()->proxies_for_http().empty()); | 812 EXPECT_TRUE(configurator()->proxies_for_http().empty()); |
| 825 EXPECT_TRUE(configurator()->proxies_for_https().empty()); | |
| 826 EXPECT_TRUE(request_options()->GetSecureSession().empty()); | 813 EXPECT_TRUE(request_options()->GetSecureSession().empty()); |
| 827 | 814 |
| 828 // ApplySerializedConfig should apply the encoded config. | 815 // ApplySerializedConfig should apply the encoded config. |
| 829 config_client()->ApplySerializedConfig(encoded_config()); | 816 config_client()->ApplySerializedConfig(encoded_config()); |
| 830 EXPECT_EQ(2U, configurator()->proxies_for_http().size()); | 817 EXPECT_EQ(2U, configurator()->proxies_for_http().size()); |
| 831 EXPECT_TRUE(configurator()->proxies_for_https().empty()); | |
| 832 EXPECT_TRUE(persisted_config().empty()); | 818 EXPECT_TRUE(persisted_config().empty()); |
| 833 EXPECT_FALSE(request_options()->GetSecureSession().empty()); | 819 EXPECT_FALSE(request_options()->GetSecureSession().empty()); |
| 834 } | 820 } |
| 835 | 821 |
| 836 #if defined(OS_ANDROID) | 822 #if defined(OS_ANDROID) |
| 837 // Verifies the correctness of fetching config when Chromium is in background | 823 // Verifies the correctness of fetching config when Chromium is in background |
| 838 // and foreground. | 824 // and foreground. |
| 839 TEST_F(DataReductionProxyConfigServiceClientTest, FetchConfigOnForeground) { | 825 TEST_F(DataReductionProxyConfigServiceClientTest, FetchConfigOnForeground) { |
| 840 Init(true); | 826 Init(true); |
| 841 SetDataReductionProxyEnabled(true); | 827 SetDataReductionProxyEnabled(true); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 903 histogram_tester.ExpectTotalCount( | 889 histogram_tester.ExpectTotalCount( |
| 904 "DataReductionProxy.ConfigService.FetchLatency", 1); | 890 "DataReductionProxy.ConfigService.FetchLatency", 1); |
| 905 EXPECT_EQ(base::TimeDelta::FromSeconds(kConfigRefreshDurationSeconds), | 891 EXPECT_EQ(base::TimeDelta::FromSeconds(kConfigRefreshDurationSeconds), |
| 906 config_client()->GetDelay()); | 892 config_client()->GetDelay()); |
| 907 VerifyRemoteSuccess(); | 893 VerifyRemoteSuccess(); |
| 908 } | 894 } |
| 909 } | 895 } |
| 910 #endif | 896 #endif |
| 911 | 897 |
| 912 } // namespace data_reduction_proxy | 898 } // namespace data_reduction_proxy |
| OLD | NEW |