Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / components / data_reduction_proxy / browser / data_reduction_proxy_settings_unittest.cc
index 56778a7..893cc8a 100644 (file)
@@ -8,6 +8,7 @@
 #include "base/md5.h"
 #include "base/message_loop/message_loop.h"
 #include "base/strings/utf_string_conversions.h"
+#include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h"
 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings_test_utils.h"
 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names.h"
 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h"
 
 namespace {
 
-const char kDataReductionProxy[] = "https://foo.com:443/";
-const char kDataReductionProxyDev[] = "http://foo-dev.com:80";
-const char kDataReductionProxyFallback[] = "http://bar.com:80";
-const char kDataReductionProxyKey[] = "12345";
-
 const char kProbeURLWithOKResponse[] = "http://ok.org/";
 const char kProbeURLWithBadResponse[] = "http://bad.org/";
 const char kProbeURLWithNoResponse[] = "http://no.org/";
@@ -37,113 +33,115 @@ class DataReductionProxySettingsTest
           DataReductionProxySettings> {
 };
 
-
-TEST_F(DataReductionProxySettingsTest, TestAuthenticationInit) {
-  AddProxyToCommandLine();
-  net::HttpAuthCache cache;
-  DataReductionProxySettings::InitDataReductionAuthentication(
-      &cache, kDataReductionProxyKey);
-  DataReductionProxySettings::DataReductionProxyList proxies =
-      DataReductionProxySettings::GetDataReductionProxies();
-  for (DataReductionProxySettings::DataReductionProxyList::iterator it =
-           proxies.begin();  it != proxies.end(); ++it) {
-    net::HttpAuthCache::Entry* entry = cache.LookupByPath(*it,
-                                                          std::string("/"));
-    EXPECT_TRUE(entry != NULL);
-    EXPECT_EQ(net::HttpAuth::AUTH_SCHEME_SPDYPROXY, entry->scheme());
-    EXPECT_EQ("SpdyProxy", entry->auth_challenge().substr(0,9));
-  }
-  GURL bad_server = GURL("https://bad.proxy.com/");
-  net::HttpAuthCache::Entry* entry =
-      cache.LookupByPath(bad_server, std::string());
-  EXPECT_TRUE(entry == NULL);
-}
-
 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyOrigin) {
-  AddProxyToCommandLine();
   // SetUp() adds the origin to the command line, which should be returned here.
   std::string result =
-      DataReductionProxySettings::GetDataReductionProxyOrigin();
-  EXPECT_EQ(kDataReductionProxy, result);
+      settings_->params()->origin().spec();
+  EXPECT_EQ(GURL(expected_params_->DefaultOrigin()), GURL(result));
 }
 
 TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxyDevOrigin) {
-  AddProxyToCommandLine();
   CommandLine::ForCurrentProcess()->AppendSwitchASCII(
-      switches::kDataReductionProxyDev, kDataReductionProxyDev);
+      switches::kDataReductionProxyDev, expected_params_->DefaultDevOrigin());
+  ResetSettings(true, true, false, true, false);
   std::string result =
-      DataReductionProxySettings::GetDataReductionProxyOrigin();
-  EXPECT_EQ(kDataReductionProxyDev, result);
+      settings_->params()->origin().spec();
+  EXPECT_EQ(GURL(expected_params_->DefaultDevOrigin()), GURL(result));
 }
 
-TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxies) {
-  DataReductionProxySettings::DataReductionProxyList proxies =
-      DataReductionProxySettings::GetDataReductionProxies();
-
-  unsigned int expected_proxy_size = 0u;
-#if defined(SPDY_PROXY_AUTH_ORIGIN)
-  ++expected_proxy_size;
-#endif
-#if defined(DATA_REDUCTION_FALLBACK_HOST)
-  ++expected_proxy_size;
-#endif
 
-  EXPECT_EQ(expected_proxy_size, proxies.size());
-
-  // Adding just the fallback on the command line shouldn't add a proxy unless
-  // there was already one compiled in.
-  CommandLine::ForCurrentProcess()->AppendSwitchASCII(
-      switches::kDataReductionProxyFallback, kDataReductionProxyFallback);
-  proxies = DataReductionProxySettings::GetDataReductionProxies();
-
-  // So: if there weren't any proxies before, there still won't be.
-  // If there were one or two, there will be two now.
-  expected_proxy_size = expected_proxy_size == 0u ? 0u : 2u;
+TEST_F(DataReductionProxySettingsTest, TestGetDataReductionProxies) {
+  DataReductionProxyParams::DataReductionProxyList proxies =
+      expected_params_->GetAllowedProxies();
 
+  unsigned int expected_proxy_size = 2u;
   EXPECT_EQ(expected_proxy_size, proxies.size());
 
-  CommandLine::ForCurrentProcess()->AppendSwitchASCII(
-    switches::kDataReductionProxy, kDataReductionProxy);
-  proxies = DataReductionProxySettings::GetDataReductionProxies();
-  EXPECT_EQ(2u, proxies.size());
-
-  // Command line proxies have precedence, so even if there were other values
-  // compiled in, these should be the ones in the list.
-  EXPECT_EQ("foo.com", proxies[0].host());
-  EXPECT_EQ(443 ,proxies[0].EffectiveIntPort());
-  EXPECT_EQ("bar.com", proxies[1].host());
-  EXPECT_EQ(80, proxies[1].EffectiveIntPort());
+  net::HostPortPair expected_origin =
+      net::HostPortPair::FromURL(GURL(expected_params_->DefaultOrigin()));
+  net::HostPortPair expected_fallback_origin =
+      net::HostPortPair::FromURL(
+          GURL(expected_params_->DefaultFallbackOrigin()));
+  EXPECT_EQ(expected_origin.host(), proxies[0].host());
+  EXPECT_EQ(expected_origin.port() ,proxies[0].EffectiveIntPort());
+  EXPECT_EQ(expected_fallback_origin.host(), proxies[1].host());
+  EXPECT_EQ(expected_fallback_origin.port(), proxies[1].EffectiveIntPort());
 }
 
-TEST_F(DataReductionProxySettingsTest, TestAuthHashGeneration) {
-  AddProxyToCommandLine();
-  std::string salt = "8675309";  // Jenny's number to test the hash generator.
-  std::string salted_key = salt + kDataReductionProxyKey + salt;
-  base::string16 expected_hash = base::UTF8ToUTF16(base::MD5String(salted_key));
-  EXPECT_EQ(expected_hash,
-            DataReductionProxySettings::AuthHashForSalt(
-                8675309, kDataReductionProxyKey));
+TEST_F(DataReductionProxySettingsTest, TestSetProxyConfigs) {
+  TestDataReductionProxyParams drp_params(
+      DataReductionProxyParams::kAllowed |
+      DataReductionProxyParams::kFallbackAllowed |
+      DataReductionProxyParams::kPromoAllowed,
+      TestDataReductionProxyParams::HAS_EVERYTHING &
+      ~TestDataReductionProxyParams::HAS_DEV_ORIGIN &
+      ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN);
+  CommandLine::ForCurrentProcess()->AppendSwitchASCII(
+      switches::kDataReductionProxyAlt, drp_params.DefaultAltOrigin());
+  CommandLine::ForCurrentProcess()->AppendSwitchASCII(
+      switches::kDataReductionProxyAltFallback,
+      drp_params.DefaultAltFallbackOrigin());
+  CommandLine::ForCurrentProcess()->AppendSwitchASCII(
+      switches::kDataReductionSSLProxy, drp_params.DefaultSSLOrigin());
+  ResetSettings(true, true, true, true, false);
+  TestDataReductionProxyConfig* config =
+      static_cast<TestDataReductionProxyConfig*>(
+          settings_->configurator());
+
+  settings_->SetProxyConfigs(true, true, false, false);
+  EXPECT_TRUE(config->enabled_);
+  EXPECT_TRUE(net::HostPortPair::FromString(
+      expected_params_->DefaultAltOrigin()).Equals(
+          net::HostPortPair::FromString(config->origin_)));
+  EXPECT_TRUE(net::HostPortPair::FromString(
+      expected_params_->DefaultAltFallbackOrigin()).Equals(
+          net::HostPortPair::FromString(config->fallback_origin_)));
+  EXPECT_TRUE(net::HostPortPair::FromString(
+      expected_params_->DefaultSSLOrigin()).Equals(
+          net::HostPortPair::FromString(config->ssl_origin_)));
+
+  settings_->SetProxyConfigs(true, false, false, false);
+  EXPECT_TRUE(config->enabled_);
+  EXPECT_TRUE(net::HostPortPair::FromString(drp_params.DefaultOrigin()).Equals(
+      net::HostPortPair::FromString(config->origin_)));
+  EXPECT_TRUE(net::HostPortPair::FromString(
+      drp_params.DefaultFallbackOrigin()).Equals(
+          net::HostPortPair::FromString(config->fallback_origin_)));
+  EXPECT_EQ("", config->ssl_origin_);
+
+  settings_->SetProxyConfigs(false, true, false, false);
+  EXPECT_FALSE(config->enabled_);
+  EXPECT_EQ("", config->origin_);
+  EXPECT_EQ("", config->fallback_origin_);
+  EXPECT_EQ("", config->ssl_origin_);
+
+  settings_->SetProxyConfigs(false, false, false, false);
+  EXPECT_FALSE(config->enabled_);
+  EXPECT_EQ("", config->origin_);
+  EXPECT_EQ("", config->fallback_origin_);
+  EXPECT_EQ("", config->ssl_origin_);
 }
 
-// Test that the auth key set by preprocessor directive is not used
-// when an origin is set via a switch. This test only does anything useful in
-// Chrome builds.
-TEST_F(DataReductionProxySettingsTest,
-       TestAuthHashGenerationWithOriginSetViaSwitch) {
-  CommandLine::ForCurrentProcess()->AppendSwitchASCII(
-      switches::kDataReductionProxy, kDataReductionProxy);
-  EXPECT_EQ(base::string16(),
-            DataReductionProxySettings::AuthHashForSalt(
-                8675309, kDataReductionProxyKey));
+TEST_F(DataReductionProxySettingsTest, TestSetProxyConfigsHoldback) {
+  ResetSettings(true, true, true, true, true);
+  TestDataReductionProxyConfig* config =
+      static_cast<TestDataReductionProxyConfig*>(
+          settings_->configurator());
+
+   // Holdback.
+  settings_->SetProxyConfigs(true, true, false, false);
+  EXPECT_FALSE(config->enabled_);
+  EXPECT_EQ("", config->origin_);
+  EXPECT_EQ("", config->fallback_origin_);
+  EXPECT_EQ("", config->ssl_origin_);
 }
 
 TEST_F(DataReductionProxySettingsTest, TestIsProxyEnabledOrManaged) {
-  AddProxyToCommandLine();
   settings_->InitPrefMembers();
-    base::MessageLoopForUI loop;
-    // The proxy is disabled initially.
-    settings_->enabled_by_user_ = false;
-    settings_->SetProxyConfigs(false, false, false);
+  base::MessageLoopForUI loop;
+  // The proxy is disabled initially.
+  settings_->enabled_by_user_ = false;
+  settings_->SetProxyConfigs(false, false, false, false);
 
   EXPECT_FALSE(settings_->IsDataReductionProxyEnabled());
   EXPECT_FALSE(settings_->IsDataReductionProxyManaged());
@@ -155,64 +153,8 @@ TEST_F(DataReductionProxySettingsTest, TestIsProxyEnabledOrManaged) {
   CheckOnPrefChange(true, true, true);
   EXPECT_TRUE(settings_->IsDataReductionProxyEnabled());
   EXPECT_TRUE(settings_->IsDataReductionProxyManaged());
-}
 
-TEST_F(DataReductionProxySettingsTest, TestAcceptableChallenges) {
-  AddProxyToCommandLine();
-  typedef struct {
-    std::string host;
-    std::string realm;
-    bool expected_to_succeed;
-  } challenge_test;
-
-  challenge_test tests[] = {
-    {"foo.com:443", "", false},                 // 0. No realm.
-    {"foo.com:443", "xxx", false},              // 1. Wrong realm.
-    {"foo.com:443", "spdyproxy", false},        // 2. Case matters.
-    {"foo.com:443", "SpdyProxy", true},         // 3. OK.
-    {"foo.com:443", "SpdyProxy1234567", true},  // 4. OK
-    {"bar.com:80", "SpdyProxy1234567", true},   // 5. OK.
-    {"foo.com:443", "SpdyProxyxxx", true},      // 6. OK
-    {"", "SpdyProxy1234567", false},            // 7. No challenger.
-    {"xxx.net:443", "SpdyProxy1234567", false}, // 8. Wrong host.
-    {"foo.com", "SpdyProxy1234567", false},     // 9. No port.
-    {"foo.com:80", "SpdyProxy1234567", false},  // 10.Wrong port.
-    {"bar.com:81", "SpdyProxy1234567", false},  // 11.Wrong port.
-  };
-
-  for (int i = 0; i <= 11; ++i) {
-    scoped_refptr<net::AuthChallengeInfo> auth_info(new net::AuthChallengeInfo);
-    auth_info->challenger = net::HostPortPair::FromString(tests[i].host);
-    auth_info->realm = tests[i].realm;
-    EXPECT_EQ(tests[i].expected_to_succeed,
-              DataReductionProxySettings::IsAcceptableAuthChallenge(
-                  auth_info.get()));
-  }
-}
-
-TEST_F(DataReductionProxySettingsTest, TestChallengeTokens) {
-  AddProxyToCommandLine();
-  typedef struct {
-    std::string realm;
-    bool expected_empty_token;
-  } token_test;
-
-  token_test tests[] = {
-    {"", true},                  // 0. No realm.
-    {"xxx", true},               // 1. realm too short.
-    {"spdyproxy", true},         // 2. no salt.
-    {"SpdyProxyxxx", true},      // 3. Salt not an int.
-    {"SpdyProxy1234567", false}, // 4. OK
-  };
-
-  for (int i = 0; i <= 4; ++i) {
-    scoped_refptr<net::AuthChallengeInfo> auth_info(new net::AuthChallengeInfo);
-    auth_info->challenger =
-        net::HostPortPair::FromString(kDataReductionProxy);
-    auth_info->realm = tests[i].realm;
-    base::string16 token = settings_->GetTokenForAuthChallenge(auth_info.get());
-    EXPECT_EQ(tests[i].expected_empty_token, token.empty());
-  }
+  base::MessageLoop::current()->RunUntilIdle();
 }
 
 TEST_F(DataReductionProxySettingsTest, TestResetDataReductionStatistics) {
@@ -281,58 +223,132 @@ TEST_F(DataReductionProxySettingsTest, TestContentLengths) {
 // TODO(marq): Add a test to verify that MaybeActivateDataReductionProxy
 // is called when the pref in |settings_| is enabled.
 TEST_F(DataReductionProxySettingsTest, TestMaybeActivateDataReductionProxy) {
-  AddProxyToCommandLine();
-
   // Initialize the pref member in |settings_| without the usual callback
   // so it won't trigger MaybeActivateDataReductionProxy when the pref value
   // is set.
   settings_->spdy_proxy_auth_enabled_.Init(
       prefs::kDataReductionProxyEnabled,
       settings_->GetOriginalProfilePrefs());
+  settings_->data_reduction_proxy_alternative_enabled_.Init(
+      prefs::kDataReductionProxyAltEnabled,
+      settings_->GetOriginalProfilePrefs());
 
   // TODO(bengr): Test enabling/disabling while a probe is outstanding.
   base::MessageLoopForUI loop;
   // The proxy is enabled and unrestructed initially.
   // Request succeeded but with bad response, expect proxy to be restricted.
-  CheckProbe(true, kProbeURLWithBadResponse, "Bad", true, true, true, false);
+  CheckProbe(true,
+             kProbeURLWithBadResponse,
+             "Bad",
+             true,
+             true,
+             true,
+             false);
   // Request succeeded with valid response, expect proxy to be unrestricted.
-  CheckProbe(true, kProbeURLWithOKResponse, "OK", true, true, false, false);
+  CheckProbe(true,
+             kProbeURLWithOKResponse,
+             "OK",
+             true,
+             true,
+             false,
+             false);
   // Request failed, expect proxy to be enabled but restricted.
-  CheckProbe(true, kProbeURLWithNoResponse, "", false, true, true, false);
+  CheckProbe(true,
+             kProbeURLWithNoResponse,
+             "",
+             false,
+             true,
+             true,
+             false);
   // The proxy is disabled initially. Probes should not be emitted to change
   // state.
-  CheckProbe(false, kProbeURLWithOKResponse, "OK", true, false, false, false);
+  CheckProbe(false,
+             kProbeURLWithOKResponse,
+             "OK",
+             true,
+             false,
+             false,
+             false);
 }
 
 TEST_F(DataReductionProxySettingsTest, TestOnIPAddressChanged) {
-  AddProxyToCommandLine();
   base::MessageLoopForUI loop;
   // The proxy is enabled initially.
   pref_service_.SetBoolean(prefs::kDataReductionProxyEnabled, true);
   settings_->spdy_proxy_auth_enabled_.Init(
       prefs::kDataReductionProxyEnabled,
       settings_->GetOriginalProfilePrefs());
+  settings_->data_reduction_proxy_alternative_enabled_.Init(
+      prefs::kDataReductionProxyAltEnabled,
+      settings_->GetOriginalProfilePrefs());
   settings_->enabled_by_user_ = true;
   settings_->restricted_by_carrier_ = false;
-  settings_->SetProxyConfigs(true, false, true);
+  settings_->SetProxyConfigs(true, false, false, true);
   // IP address change triggers a probe that succeeds. Proxy remains
   // unrestricted.
-  CheckProbeOnIPChange(kProbeURLWithOKResponse, "OK", true, false, false);
+  CheckProbeOnIPChange(kProbeURLWithOKResponse,
+                       "OK",
+                       true,
+                       false,
+                       false);
   // IP address change triggers a probe that fails. Proxy is restricted.
-  CheckProbeOnIPChange(kProbeURLWithBadResponse, "Bad", true, true, false);
+  CheckProbeOnIPChange(kProbeURLWithBadResponse,
+                       "Bad",
+                       true,
+                       true,
+                       false);
   // IP address change triggers a probe that fails. Proxy remains restricted.
-  CheckProbeOnIPChange(kProbeURLWithBadResponse, "Bad", true, true, false);
-  // IP address change triggers a probe that succeed. Proxy is unrestricted.
-  CheckProbeOnIPChange(kProbeURLWithBadResponse, "OK", true, false, false);
+  CheckProbeOnIPChange(kProbeURLWithBadResponse,
+                       "Bad",
+                       true,
+                       true,
+                       false);
+  // IP address change triggers a probe that succeeds. Proxy is unrestricted.
+  CheckProbeOnIPChange(kProbeURLWithOKResponse,
+                       "OK",
+                       true,
+                       false,
+                       false);
+  // Simulate a VPN connection. The proxy should be disabled.
+  MockSettings* settings = static_cast<MockSettings*>(settings_.get());
+  settings->network_interfaces_.reset(new net::NetworkInterfaceList());
+  settings->network_interfaces_->push_back(net::NetworkInterface(
+      "tun0", /* network interface name */
+      "tun0", /* network interface friendly name */
+      0,      /* interface index */
+      net::NetworkChangeNotifier::CONNECTION_WIFI,
+      net::IPAddressNumber(),        /* IP address */
+      0,                             /* network prefix */
+      net::IP_ADDRESS_ATTRIBUTE_NONE /* ip address attribute */
+      ));
+  settings_->OnIPAddressChanged();
+  base::MessageLoop::current()->RunUntilIdle();
+  CheckProxyConfigs(false, false, false);
+
+  // Check that the proxy is re-enabled if a non-VPN connection is later used.
+  settings->network_interfaces_.reset(new net::NetworkInterfaceList());
+  settings->network_interfaces_->push_back(net::NetworkInterface(
+      "eth0", /* network interface name */
+      "eth0", /* network interface friendly name */
+      0,      /* interface index */
+      net::NetworkChangeNotifier::CONNECTION_WIFI,
+      net::IPAddressNumber(),
+      0,                             /* network prefix */
+      net::IP_ADDRESS_ATTRIBUTE_NONE /* ip address attribute */
+      ));
+  CheckProbeOnIPChange(kProbeURLWithOKResponse,
+                       "OK",
+                       true,
+                       false,
+                       false);
 }
 
 TEST_F(DataReductionProxySettingsTest, TestOnProxyEnabledPrefChange) {
-  AddProxyToCommandLine();
   settings_->InitPrefMembers();
   base::MessageLoopForUI loop;
   // The proxy is enabled initially.
   settings_->enabled_by_user_ = true;
-  settings_->SetProxyConfigs(true, false, true);
+  settings_->SetProxyConfigs(true, false, false, true);
   // The pref is disabled, so correspondingly should be the proxy.
   CheckOnPrefChange(false, false, false);
   // The pref is enabled, so correspondingly should be the proxy.
@@ -357,7 +373,7 @@ TEST_F(DataReductionProxySettingsTest, TestInitDataReductionProxyOff) {
   CheckInitDataReductionProxy(false);
 }
 
-TEST_F(DataReductionProxySettingsTest, TestSetProxyFromCommandLine) {
+TEST_F(DataReductionProxySettingsTest, TestEnableProxyFromCommandLine) {
   MockSettings* settings = static_cast<MockSettings*>(settings_.get());
   EXPECT_CALL(*settings, RecordStartupState(PROXY_ENABLED));
 
@@ -384,19 +400,26 @@ TEST_F(DataReductionProxySettingsTest, CheckInitMetricsWhenNotAllowed) {
   // No call to |AddProxyToCommandLine()| was made, so the proxy feature
   // should be unavailable.
   base::MessageLoopForUI loop;
-  DataReductionProxySettings::SetAllowed(false);
-  EXPECT_FALSE(DataReductionProxySettings::IsDataReductionProxyAllowed());
+  // Clear the command line. Setting flags can force the proxy to be allowed.
+  CommandLine::ForCurrentProcess()->InitFromArgv(0, NULL);
+
+  ResetSettings(false, false, false, false, false);
   MockSettings* settings = static_cast<MockSettings*>(settings_.get());
+  EXPECT_FALSE(settings->params()->allowed());
   EXPECT_CALL(*settings, RecordStartupState(PROXY_NOT_AVAILABLE));
 
   scoped_ptr<DataReductionProxyConfigurator> configurator(
       new TestDataReductionProxyConfig());
-  settings_->SetProxyConfigurator(configurator.Pass());
+  settings_->SetProxyConfigurator(configurator.get());
   scoped_refptr<net::TestURLRequestContextGetter> request_context =
       new net::TestURLRequestContextGetter(base::MessageLoopProxy::current());
-  settings_->InitDataReductionProxySettings(&pref_service_,
-                                            &pref_service_,
-                                            request_context.get());
+  settings_->InitDataReductionProxySettings(
+      &pref_service_,
+      request_context.get());
+  settings_->SetOnDataReductionEnabledCallback(
+      base::Bind(&DataReductionProxySettingsTestBase::
+                 RegisterSyntheticFieldTrialCallback,
+                 base::Unretained(this)));
 
   base::MessageLoop::current()->RunUntilIdle();
 }