Home | History | Annotate | Download | only in spdyproxy
      1 // Copyright 2013 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 "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest.h"
      6 
      7 #include "base/android/jni_android.h"
      8 #include "base/android/jni_string.h"
      9 #include "base/android/scoped_java_ref.h"
     10 #include "base/base64.h"
     11 #include "base/command_line.h"
     12 #include "base/metrics/field_trial.h"
     13 #include "base/prefs/pref_service.h"
     14 #include "base/prefs/scoped_user_pref_update.h"
     15 #include "base/prefs/testing_pref_service.h"
     16 #include "base/strings/string_number_conversions.h"
     17 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h"
     18 #include "chrome/browser/prefs/proxy_prefs.h"
     19 #include "chrome/common/chrome_switches.h"
     20 #include "chrome/common/metrics/variations/variations_util.h"
     21 #include "chrome/common/pref_names.h"
     22 #include "components/variations/entropy_provider.h"
     23 #include "net/url_request/test_url_fetcher_factory.h"
     24 #include "testing/gmock/include/gmock/gmock.h"
     25 #include "testing/gtest/include/gtest/gtest.h"
     26 #include "url/gurl.h"
     27 
     28 const char kDataReductionProxyOrigin[] = "https://foo.com:443/";
     29 const char kDataReductionProxyOriginPAC[] = "HTTPS foo.com:443;";
     30 const char kDataReductionProxyFallbackPAC[] = "PROXY bar.com:80;";
     31 
     32 class DataReductionProxySettingsAndroidTest
     33     : public ConcreteDataReductionProxySettingsTest<
     34         DataReductionProxySettingsAndroid> {
     35  public:
     36   // DataReductionProxySettingsTest implementation:
     37   virtual void SetUp() OVERRIDE {
     38     env_ = base::android::AttachCurrentThread();
     39     DataReductionProxySettingsAndroid::Register(env_);
     40     DataReductionProxySettingsTestBase::SetUp();
     41   }
     42 
     43   void CheckProxyPacPref(const std::string& expected_pac_url,
     44                          const std::string& expected_mode) {
     45     const DictionaryValue* dict = pref_service_.GetDictionary(prefs::kProxy);
     46     std::string mode;
     47     std::string pac_url;
     48     dict->GetString("mode", &mode);
     49     ASSERT_EQ(expected_mode, mode);
     50     dict->GetString("pac_url", &pac_url);
     51     ASSERT_EQ(expected_pac_url, pac_url);
     52   }
     53 
     54   DataReductionProxySettingsAndroid* Settings() {
     55     return static_cast<DataReductionProxySettingsAndroid*>(settings_.get());
     56   }
     57 
     58   JNIEnv* env_;
     59 };
     60 
     61 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyOrigin) {
     62   AddProxyToCommandLine();
     63   // SetUp() adds the origin to the command line, which should be returned here.
     64   ScopedJavaLocalRef<jstring> result =
     65       Settings()->GetDataReductionProxyOrigin(env_, NULL);
     66   ASSERT_TRUE(result.obj());
     67   const base::android::JavaRef<jstring>& str_ref = result;
     68   EXPECT_EQ(kDataReductionProxyOrigin, ConvertJavaStringToUTF8(str_ref));
     69 }
     70 
     71 // Confirm that the bypass rule functions generate the intended JavaScript
     72 // code for the Proxy PAC.
     73 TEST_F(DataReductionProxySettingsAndroidTest, TestBypassPACRules) {
     74   Settings()->AddURLPatternToBypass("http://foo.com/*");
     75   Settings()->AddHostPatternToBypass("bar.com");
     76 
     77   EXPECT_EQ(Settings()->pac_bypass_rules_.size(), 1u);
     78   EXPECT_EQ("shExpMatch(url, 'http://foo.com/*')",
     79             Settings()->pac_bypass_rules_[0]);
     80 
     81   EXPECT_EQ(Settings()->BypassRules().size(), 1u);
     82   EXPECT_EQ("bar.com", Settings()->BypassRules()[0]);
     83 }
     84 
     85 TEST_F(DataReductionProxySettingsAndroidTest, TestSetProxyPac) {
     86   AddProxyToCommandLine();
     87   Settings()->AddDefaultProxyBypassRules();
     88 
     89   // First check without restriction.
     90   std::string raw_pac = Settings()->GetProxyPacScript(false);
     91   EXPECT_NE(raw_pac.find(kDataReductionProxyOriginPAC), std::string::npos);
     92   EXPECT_NE(raw_pac.find(kDataReductionProxyFallbackPAC), std::string::npos);
     93   std::string pac;
     94   base::Base64Encode(raw_pac, &pac);
     95   std::string expected_pac_url =
     96       "data:application/x-ns-proxy-autoconfig;base64," + pac;
     97   Settings()->SetProxyConfigs(true, false, false);
     98   CheckProxyPacPref(expected_pac_url,
     99                     ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT));
    100 
    101   // Now check with restriction.
    102   raw_pac = Settings()->GetProxyPacScript(true);
    103   // Primary proxy origin should not appear.
    104   EXPECT_EQ(raw_pac.find(kDataReductionProxyOriginPAC), std::string::npos);
    105   EXPECT_NE(raw_pac.find(kDataReductionProxyFallbackPAC), std::string::npos);
    106   base::Base64Encode(raw_pac, &pac);
    107   expected_pac_url = "data:application/x-ns-proxy-autoconfig;base64," + pac;
    108   Settings()->SetProxyConfigs(true, true, false);
    109   CheckProxyPacPref(expected_pac_url,
    110                     ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT));
    111 
    112   Settings()->SetProxyConfigs(false, false, false);
    113   CheckProxyPacPref(std::string(), ProxyModeToString(ProxyPrefs::MODE_SYSTEM));
    114 
    115   // Restriction is irrelevant when the proxy is disabled.
    116   Settings()->SetProxyConfigs(false, false, false);
    117   CheckProxyPacPref(std::string(), ProxyModeToString(ProxyPrefs::MODE_SYSTEM));
    118 }
    119 
    120 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDailyContentLengths) {
    121   ScopedJavaLocalRef<jlongArray> result = Settings()->GetDailyContentLengths(
    122         env_, prefs::kDailyHttpOriginalContentLength);
    123   ASSERT_TRUE(result.obj());
    124 
    125   jsize java_array_len = env_->GetArrayLength(result.obj());
    126   ASSERT_EQ(static_cast<jsize>(spdyproxy::kNumDaysInHistory), java_array_len);
    127 
    128   jlong value;
    129   for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) {
    130     env_->GetLongArrayRegion(result.obj(), i, 1, &value);
    131     ASSERT_EQ(
    132         static_cast<long>((spdyproxy::kNumDaysInHistory - 1 - i) * 2), value);
    133   }
    134 }
    135 
    136