Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(564)

Side by Side Diff: chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest_android.cc

Issue 23458016: Added probe to determine if data reduction proxy can be used (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Another round of comments addressed Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "base/android/jni_android.h"
6 #include "base/android/jni_string.h"
7 #include "base/android/scoped_java_ref.h"
8 #include "base/base64.h"
9 #include "base/command_line.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/prefs/pref_registry_simple.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/prefs/testing_pref_service.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h"
16 #include "chrome/browser/prefs/proxy_prefs.h"
17 #include "chrome/browser/prefs/scoped_user_pref_update.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/metrics/variations/variations_util.h"
20 #include "chrome/common/pref_names.h"
21 #include "components/variations/entropy_provider.h"
22 #include "net/url_request/test_url_fetcher_factory.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "url/gurl.h"
25
26 const char kDataReductionProxyOrigin[] = "https://foo:443/";
27 const char kDataReductionProxyOriginHostPort[] = "foo:443";
28 const char kDataReductionProxyAuth[] = "12345";
29
30 const char kProbeURLWithOKResponse[] = "http://ok.org/";
31 const char kProbeURLWithBadResponse[] = "http://bad.org/";
32 const char kProbeURLWithNoResponse[] = "http://no.org/";
33
34 class TestDataReductionProxySettingsAndroid
35 : public DataReductionProxySettingsAndroid {
36 public:
37 TestDataReductionProxySettingsAndroid(JNIEnv* env, jobject obj,
38 PrefService* profile_prefs,
39 PrefService* local_state_prefs)
40 : DataReductionProxySettingsAndroid(env, obj),
41 success_(false),
42 fake_fetcher_request_count_(0),
43 profile_prefs_for_testing_(profile_prefs),
44 local_state_prefs_for_testing_(local_state_prefs) {
45 InitPrefMembers();
46 }
47
48 // DataReductionProxySettingsAndroid implementation:
49 virtual net::URLFetcher* GetURLFetcher() OVERRIDE {
50 if (test_url_.empty())
51 return NULL;
52 net::URLFetcher* fetcher = new net::FakeURLFetcher(GURL(test_url_), this,
53 response_, success_);
54 fake_fetcher_request_count_++;
55 return fetcher;
56 }
57
58 virtual PrefService* GetOriginalProfilePrefs() OVERRIDE {
59 return profile_prefs_for_testing_;
60 }
61 virtual PrefService* GetLocalStatePrefs() OVERRIDE {
62 return local_state_prefs_for_testing_;
63 }
64
65 void set_probe_result(const std::string& test_url,
66 const std::string& response,
67 bool success) {
68 test_url_ = test_url;
69 response_ = response;
70 success_ = success;
71 }
72
73 int fake_fetcher_request_count() {
74 return fake_fetcher_request_count_;
75 }
76
77 private:
78 std::string test_url_;
79 std::string response_;
80 bool success_;
81 int fake_fetcher_request_count_;
82 PrefService* profile_prefs_for_testing_;
83 PrefService* local_state_prefs_for_testing_;
84 };
85
86 class DataReductionProxySettingsAndroidTest : public testing::Test {
87 protected:
88 DataReductionProxySettingsAndroidTest() {
89 ResetFieldTrialList();
90 }
91
92 void ResetFieldTrialList() {
93 // Destroy the existing FieldTrialList before creating a new one to avoid
94 // a DCHECK.
95 field_trial_list_.reset();
96 field_trial_list_.reset(new base::FieldTrialList(
97 new metrics::SHA1EntropyProvider("foo")));
98 chrome_variations::testing::ClearAllVariationParams();
99 }
100
101 // Creates and activates a field trial.
102 static base::FieldTrial* CreateTestTrial(const std::string& name,
103 const std::string& group_name) {
104 base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial(
105 name, group_name);
106 trial->group();
107 return trial;
108 }
109
110 // testing::Test implementation:
111 virtual void SetUp() OVERRIDE {
112 CreateTestTrial("DataCompressionProxyRollout", "Enabled");
113 CreateTestTrial("DataCompressionProxyPromoVisibility", "Enabled");
114 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
115 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin);
116 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
117 switches::kSpdyProxyAuthValue, kDataReductionProxyAuth);
118
119 PrefRegistrySimple* registry = pref_service_.registry();
120 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength);
121 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength);
122 registry->RegisterInt64Pref(
123 prefs::kDailyHttpContentLengthLastUpdateDate, 0L);
124 registry->RegisterDictionaryPref(prefs::kProxy);
125 registry->RegisterBooleanPref(prefs::kSpdyProxyAuthEnabled, false);
126 registry->RegisterBooleanPref(prefs::kSpdyProxyAuthWasEnabledBefore, false);
127 settings_.reset(new TestDataReductionProxySettingsAndroid(NULL, NULL,
128 &pref_service_,
129 &pref_service_));
130 ListPrefUpdate original_update(&pref_service_,
131 prefs::kDailyHttpOriginalContentLength);
132 ListPrefUpdate received_update(&pref_service_,
133 prefs::kDailyHttpReceivedContentLength);
134 for (int64 i = 0; i < spdyproxy::kNumDaysInHistory; i++) {
135 original_update->Insert(0, new StringValue(base::Int64ToString(2 * i)));
136 received_update->Insert(0, new StringValue(base::Int64ToString(i)));
137 }
138 last_update_time_ = base::Time::Now().LocalMidnight();
139 pref_service_.SetInt64(
140 prefs::kDailyHttpContentLengthLastUpdateDate,
141 last_update_time_.ToInternalValue());
142 }
143
144 void CheckProxyPrefSetToDataReductionProxy(
145 const std::string& expected_pac_url) {
146 const DictionaryValue* dict = pref_service_.GetDictionary(prefs::kProxy);
147 std::string mode;
148 std::string pac_url;
149 dict->GetString("mode", &mode);
150 EXPECT_EQ(ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT), mode);
151 dict->GetString("pac_url", &pac_url);
152 EXPECT_EQ(expected_pac_url, pac_url);
153 }
154 void CheckProxyPrefSetToSystem() {
155 const DictionaryValue* dict = pref_service_.GetDictionary(prefs::kProxy);
156 std::string mode;
157 std::string pac_url;
158 dict->GetString("mode", &mode);
159 EXPECT_EQ(ProxyModeToString(ProxyPrefs::MODE_SYSTEM), mode);
160 dict->GetString("pac_url", &pac_url);
161 EXPECT_EQ(std::string(), pac_url);
162 }
163
164 void CheckProbe(bool initially_enabled, const std::string& probe_url,
165 const std::string& response, bool request_success,
166 bool expected_enabled) {
167 settings_->AddDefaultProxyBypassRules();
168 std::string pac;
169 base::Base64Encode(settings_->GetProxyPacScript(), &pac);
170 std::string expected_pac_url =
171 "data:application/x-ns-proxy-autoconfig;base64," + pac;
172 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, initially_enabled);
173 settings_->set_probe_result(probe_url, response, request_success);
174 settings_->MaybeActivateDataReductionProxy(false);
175 base::MessageLoop::current()->RunUntilIdle();
176 if (expected_enabled)
177 CheckProxyPrefSetToDataReductionProxy(expected_pac_url);
178 else
179 CheckProxyPrefSetToSystem();
180 }
181
182 TestingPrefServiceSimple pref_service_;
183 scoped_ptr<TestDataReductionProxySettingsAndroid> settings_;
184 base::Time last_update_time_;
185 // This is a singleton that will clear all set field trials on destruction.
186 scoped_ptr<base::FieldTrialList> field_trial_list_;
187 };
188
189 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyOrigin) {
190 JNIEnv* env = base::android::AttachCurrentThread();
191 DataReductionProxySettingsAndroid::Register(env);
192 // SetUp() adds the origin to the command line, which should be returned here.
193 ScopedJavaLocalRef<jstring> result =
194 settings_->GetDataReductionProxyOrigin(env, NULL);
195 ASSERT_TRUE(result.obj());
196 const base::android::JavaRef<jstring>& str_ref = result;
197 EXPECT_EQ(kDataReductionProxyOrigin, ConvertJavaStringToUTF8(str_ref));
198 }
199
200 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyAuth) {
201 JNIEnv* env = base::android::AttachCurrentThread();
202 DataReductionProxySettingsAndroid::Register(env);
203 // SetUp() adds the auth string to the command line, which should be returned
204 // here.
205 ScopedJavaLocalRef<jstring> result =
206 settings_->GetDataReductionProxyAuth(env, NULL);
207 ASSERT_TRUE(result.obj());
208 const base::android::JavaRef<jstring>& str_ref = result;
209 EXPECT_EQ(kDataReductionProxyAuth, ConvertJavaStringToUTF8(str_ref));
210 }
211
212 TEST_F(DataReductionProxySettingsAndroidTest, TestBypassRules) {
213 // Confirm that the bypass rule functions generate the intended JavaScript
214 // code for the Proxy PAC.
215 settings_->AddURLPatternToBypass("http://foo.com/*");
216 settings_->AddHostPatternToBypass("bar.com");
217 settings_->AddHostToBypass("127.0.0.1");
218
219 std::string expected[] = {
220 "shExpMatch(url, \"http://foo.com/*\")",
221 "shExpMatch(host, \"bar.com\")",
222 "host == \"127.0.0.1\""
223 };
224
225 int i = 0;
226 for (std::vector<std::string>::iterator it = settings_->bypass_rules_.begin();
227 it != settings_->bypass_rules_.end(); ++it) {
228 EXPECT_EQ(expected[i++], *it);
229 }
230 }
231
232 TEST_F(DataReductionProxySettingsAndroidTest, TestIsProxyEnabledOrManaged) {
233 EXPECT_FALSE(settings_->IsDataReductionProxyEnabled(NULL, NULL));
234 EXPECT_FALSE(settings_->IsDataReductionProxyManaged(NULL, NULL));
235
236 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true);
237 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled(NULL, NULL));
238 EXPECT_FALSE(settings_->IsDataReductionProxyManaged(NULL, NULL));
239
240 pref_service_.SetManagedPref(prefs::kSpdyProxyAuthEnabled,
241 base::Value::CreateBooleanValue(true));
242 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled(NULL, NULL));
243 EXPECT_TRUE(settings_->IsDataReductionProxyManaged(NULL, NULL));
244 }
245
246 TEST_F(DataReductionProxySettingsAndroidTest, TestSetProxyPac) {
247 settings_->AddDefaultProxyBypassRules();
248 std::string pac;
249 base::Base64Encode(settings_->GetProxyPacScript(), &pac);
250 std::string expected_pac_url =
251 "data:application/x-ns-proxy-autoconfig;base64," + pac;
252 // Test setting the PAC, without generating histograms.
253 settings_->has_turned_on_ = true;
254 settings_->SetProxyPac(true, false);
255 CheckProxyPrefSetToDataReductionProxy(expected_pac_url);
256
257 // Test disabling the PAC, without generating histograms.
258 settings_->has_turned_off_ = true;
259 settings_->SetProxyPac(false, false);
260 CheckProxyPrefSetToSystem();
261 }
262
263 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDailyContentLengths) {
264 JNIEnv* env = base::android::AttachCurrentThread();
265 DataReductionProxySettingsAndroid::Register(env);
266 ScopedJavaLocalRef<jlongArray> result =
267 settings_->GetDailyContentLengths(env,
268 prefs::kDailyHttpOriginalContentLength);
269 ASSERT_TRUE(result.obj());
270
271 jsize java_array_len = env->GetArrayLength(result.obj());
272 ASSERT_EQ(static_cast<jsize>(spdyproxy::kNumDaysInHistory), java_array_len);
273
274 jlong value;
275 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) {
276 env->GetLongArrayRegion(result.obj(), i, 1, &value);
277 ASSERT_EQ(static_cast<long>(118 - (2 * i)), value);
278 }
279 }
280
281 TEST_F(DataReductionProxySettingsAndroidTest,
282 TestResetDataReductionStatistics) {
283 int64 original_content_length;
284 int64 received_content_length;
285 int64 last_update_time;
286 settings_->ResetDataReductionStatistics();
287 settings_->GetContentLengthsInternal(spdyproxy::kNumDaysInHistory,
288 &original_content_length,
289 &received_content_length,
290 &last_update_time);
291 EXPECT_EQ(0L, original_content_length);
292 EXPECT_EQ(0L, received_content_length);
293 EXPECT_EQ(last_update_time_.ToInternalValue(), last_update_time);
294 }
295
296
297 TEST_F(DataReductionProxySettingsAndroidTest, TestContentLengthsInternal) {
298 int64 original_content_length;
299 int64 received_content_length;
300 int64 last_update_time;
301 settings_->GetContentLengthsInternal(spdyproxy::kNumDaysInHistory,
302 &original_content_length,
303 &received_content_length,
304 &last_update_time);
305 EXPECT_EQ(3540L, original_content_length);
306 EXPECT_EQ(1770L, received_content_length);
307 EXPECT_EQ(last_update_time_.ToInternalValue(), last_update_time);
308 }
309
310 TEST_F(DataReductionProxySettingsAndroidTest,
311 TestMaybeActivateDataReductionProxy) {
312 // TODO(bengr): Test enabling/disabling while a probe is outstanding.
313 base::MessageLoop loop(base::MessageLoop::TYPE_UI);
314 // The proxy is enabled initially.
315 // Request succeeded but with bad response, expect proxy to be disabled.
316 CheckProbe(true, kProbeURLWithBadResponse, "Bad", true, false);
317 // Request succeeded with valid response, expect proxy to be enabled.
318 CheckProbe(true, kProbeURLWithOKResponse, "OK", true, true);
319 // Request failed, expect proxy to be disabled.
320 CheckProbe(true, kProbeURLWithNoResponse, "", false, false);
321
322 // The proxy is disabled initially. Probes should not be emitted to change
323 // state.
324 EXPECT_EQ(3, settings_->fake_fetcher_request_count());
325 CheckProbe(false, kProbeURLWithOKResponse, "OK", true, false);
326 EXPECT_EQ(3, settings_->fake_fetcher_request_count());
327 }
328
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698