data_reduction_proxy_settings_unittest_android.cc revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
14e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 24e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 34e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// found in the LICENSE file. 44e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 58bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest.h" 68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) 74e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/android/jni_android.h" 84e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/android/jni_string.h" 94e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/android/scoped_java_ref.h" 104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/base64.h" 114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/command_line.h" 124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/metrics/field_trial.h" 134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/prefs/pref_service.h" 141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/prefs/scoped_user_pref_update.h" 154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/prefs/testing_pref_service.h" 164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/strings/string_number_conversions.h" 174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h" 184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/browser/prefs/proxy_prefs.h" 194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/common/chrome_switches.h" 204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/common/metrics/variations/variations_util.h" 214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/common/pref_names.h" 224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "components/variations/entropy_provider.h" 234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "net/url_request/test_url_fetcher_factory.h" 240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h" 254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "url/gurl.h" 274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const char kDataReductionProxyOrigin[] = "https://foo.com:443/"; 291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const char kDataReductionProxyOriginPAC[] = "HTTPS foo.com:443;"; 301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const char kDataReductionProxyFallbackPAC[] = "HTTP bar.com:80;"; 314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)class DataReductionProxySettingsAndroidTest 330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) : public ConcreteDataReductionProxySettingsTest< 340f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) DataReductionProxySettingsAndroid> { 358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) public: 368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) // DataReductionProxySettingsTest implementation: 374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) virtual void SetUp() OVERRIDE { 384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) env_ = base::android::AttachCurrentThread(); 394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) DataReductionProxySettingsAndroid::Register(env_); 408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) DataReductionProxySettingsTestBase::SetUp(); 414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) void CheckProxyPacPref(const std::string& expected_pac_url, 448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) const std::string& expected_mode) { 454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) const DictionaryValue* dict = pref_service_.GetDictionary(prefs::kProxy); 464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) std::string mode; 474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) std::string pac_url; 484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) dict->GetString("mode", &mode); 494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) ASSERT_EQ(expected_mode, mode); 504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) dict->GetString("pac_url", &pac_url); 514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) ASSERT_EQ(expected_pac_url, pac_url); 524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) DataReductionProxySettingsAndroid* Settings() { 550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) return static_cast<DataReductionProxySettingsAndroid*>(settings_.get()); 560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } 570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) JNIEnv* env_; 594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}; 604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyOrigin) { 624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) AddProxyToCommandLine(); 634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // SetUp() adds the origin to the command line, which should be returned here. 644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) ScopedJavaLocalRef<jstring> result = 650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) Settings()->GetDataReductionProxyOrigin(env_, NULL); 664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) ASSERT_TRUE(result.obj()); 674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) const base::android::JavaRef<jstring>& str_ref = result; 684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) EXPECT_EQ(kDataReductionProxyOrigin, ConvertJavaStringToUTF8(str_ref)); 694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)} 704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Confirm that the bypass rule functions generate the intended JavaScript 724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// code for the Proxy PAC. 738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)TEST_F(DataReductionProxySettingsAndroidTest, TestBypassPACRules) { 740f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) Settings()->AddURLPatternToBypass("http://foo.com/*"); 750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) Settings()->AddHostPatternToBypass("bar.com"); 764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 770f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_EQ(Settings()->pac_bypass_rules_.size(), 1u); 788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) EXPECT_EQ("shExpMatch(url, 'http://foo.com/*')", 790f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) Settings()->pac_bypass_rules_[0]); 804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_EQ(Settings()->BypassRules().size(), 1u); 820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) EXPECT_EQ("bar.com", Settings()->BypassRules()[0]); 834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)} 844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)TEST_F(DataReductionProxySettingsAndroidTest, TestSetProxyPac) { 861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) AddProxyToCommandLine(); 870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) Settings()->AddDefaultProxyBypassRules(); 880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) std::string raw_pac = Settings()->GetProxyPacScript(); 891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) EXPECT_NE(raw_pac.find(kDataReductionProxyOriginPAC), std::string::npos); 901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) EXPECT_NE(raw_pac.find(kDataReductionProxyFallbackPAC), std::string::npos);; 914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) std::string pac; 921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) base::Base64Encode(raw_pac, &pac); 934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) std::string expected_pac_url = 944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) "data:application/x-ns-proxy-autoconfig;base64," + pac; 954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // Test setting the PAC, without generating histograms. 960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) Settings()->SetHasTurnedOn(); 970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) Settings()->SetProxyConfigs(true, false); 988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) CheckProxyPacPref(expected_pac_url, 998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT)); 1004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 1014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) // Test disabling the PAC, without generating histograms. 1020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) Settings()->SetHasTurnedOff(); 1030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) Settings()->SetProxyConfigs(false, false); 1048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) CheckProxyPacPref(std::string(), ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); 1054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)} 1064e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 1074e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)TEST_F(DataReductionProxySettingsAndroidTest, TestGetDailyContentLengths) { 1080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) ScopedJavaLocalRef<jlongArray> result = Settings()->GetDailyContentLengths( 1098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) env_, prefs::kDailyHttpOriginalContentLength); 1104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) ASSERT_TRUE(result.obj()); 1114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 1124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) jsize java_array_len = env_->GetArrayLength(result.obj()); 1134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) ASSERT_EQ(static_cast<jsize>(spdyproxy::kNumDaysInHistory), java_array_len); 1144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 1154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) jlong value; 1164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) { 1174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) env_->GetLongArrayRegion(result.obj(), i, 1, &value); 1184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) ASSERT_EQ( 1194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) static_cast<long>((spdyproxy::kNumDaysInHistory - 1 - i) * 2), value); 1204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) } 1214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)} 1224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 123