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

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: Addressed changes 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 : DataReductionProxySettingsAndroid(env, obj),
39 success_(false),
40 fake_fetcher_request_cnt_(0),
41 profile_prefs_for_testing_(NULL),
42 local_state_prefs_for_testing_(NULL) {
43 }
44
mmenke 2013/09/17 20:25:25 nit: "// DataReductionProxySettingsAndroid implem
bengr 2013/09/18 22:15:26 Done.
45 virtual net::URLFetcher* GetURLFetcher() OVERRIDE {
46 fake_fetcher_.reset(
47 new net::FakeURLFetcher(GURL(test_url_), this, response_, success_));
48 fake_fetcher_request_cnt_++;
49 return fake_fetcher_.get();
50 }
51
52 virtual PrefService* GetOriginalProfilePrefs() OVERRIDE {
53 return profile_prefs_for_testing_;
54 }
55 virtual PrefService* GetLocalStatePrefs() OVERRIDE {
56 return local_state_prefs_for_testing_;
57 }
58
59 void set_probe_for_testing(const std::string& test_url,
mmenke 2013/09/17 20:25:25 Maybe "set_probe_result" Since this is in a unit
bengr 2013/09/18 22:15:26 Done.
60 const std::string& response,
61 bool success) {
mmenke 2013/09/17 20:25:25 nit: Fix ident.
bengr 2013/09/18 22:15:26 Done.
62 test_url_ = test_url;
63 response_ = response;
64 success_ = success;
65 }
66
67 void set_local_state_prefs_for_testing(PrefService* pref_service) {
68 profile_prefs_for_testing_ = pref_service;
69 }
70
71 void set_profile_prefs_for_testing(PrefService* pref_service) {
72 local_state_prefs_for_testing_ = pref_service;
73 }
74
75 int fake_fetcher_request_cnt() {
mmenke 2013/09/17 20:25:25 nit: Spell out "count".
bengr 2013/09/18 22:15:26 Done.
76 return fake_fetcher_request_cnt_;
77 }
78
79 private:
80 friend class DataReductionProxySettingsAndroidTest;
mmenke 2013/09/17 20:25:25 I don't think this is needed, since the parent cla
mmenke 2013/09/17 20:25:25 optional: You could get rid of all the friends in
bengr 2013/09/18 22:15:26 Done.
bengr 2013/09/18 22:15:26 I'll leave things as is but will consider on a sub
81 std::string test_url_;
82 std::string response_;
83 bool success_;
84 int fake_fetcher_request_cnt_;
85 scoped_ptr<net::URLFetcher> fake_fetcher_;
86 PrefService* profile_prefs_for_testing_;
87 PrefService* local_state_prefs_for_testing_;
88 };
89
90 class DataReductionProxySettingsAndroidTest : public testing::Test {
91 protected:
92 DataReductionProxySettingsAndroidTest() {
93 ResetFieldTrialList();
94 }
95
96 void ResetFieldTrialList() {
97 // Destroy the existing FieldTrialList before creating a new one to avoid
98 // a DCHECK.
99 field_trial_list_.reset();
100 field_trial_list_.reset(new base::FieldTrialList(
101 new metrics::SHA1EntropyProvider("foo")));
102 chrome_variations::testing::ClearAllVariationParams();
103 }
104
105 // Creates and activates a field trial.
106 static base::FieldTrial* CreateTestTrial(const std::string& name,
107 const std::string& group_name) {
108 base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial(
109 name, group_name);
110 trial->group();
111 return trial;
112 }
113
114 // testing::Test implementation:
115 virtual void SetUp() OVERRIDE {
116 CreateTestTrial("DataCompressionProxyRollout", "Enabled");
117 CreateTestTrial("DataCompressionProxyPromoVisibility", "Enabled");
118 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
119 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin);
120 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
121 switches::kSpdyProxyAuthValue, kDataReductionProxyAuth);
122 settings_.reset(new TestDataReductionProxySettingsAndroid(NULL, NULL));
123 PrefRegistrySimple* registry = pref_service_.registry();
124 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength);
125 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength);
126 registry->RegisterInt64Pref(
127 prefs::kDailyHttpContentLengthLastUpdateDate, 0L);
128 registry->RegisterDictionaryPref(prefs::kProxy);
129 registry->RegisterBooleanPref(prefs::kSpdyProxyAuthEnabled, false);
130 registry->RegisterBooleanPref(prefs::kSpdyProxyAuthWasEnabledBefore, false);
131 settings_->set_local_state_prefs_for_testing(&pref_service_);
132 settings_->set_profile_prefs_for_testing(&pref_service_);
133 ListPrefUpdate original_update(&pref_service_,
134 prefs::kDailyHttpOriginalContentLength);
135 ListPrefUpdate received_update(&pref_service_,
136 prefs::kDailyHttpReceivedContentLength);
137 for (int64 i = 0; i < spdyproxy::kNumDaysInHistory; i++) {
138 original_update->Insert(0, new StringValue(base::Int64ToString(2 * i)));
139 received_update->Insert(0, new StringValue(base::Int64ToString(i)));
140 }
141 last_update_time_ =
142 base::Time::Now().LocalMidnight() -
143 base::TimeDelta::FromDays(spdyproxy::kNumDaysInHistory);
mmenke 2013/09/17 20:25:25 Any motivation for this?
bengr 2013/09/18 22:15:26 None that I recall. Done.
144 pref_service_.SetInt64(
145 prefs::kDailyHttpContentLengthLastUpdateDate,
146 last_update_time_.ToInternalValue());
147 }
148
149 void CheckProxyPrefSetToDataReductionProxy(
150 const std::string& expected_pac_url) {
151 const DictionaryValue* dict = pref_service_.GetDictionary(prefs::kProxy);
152 std::string mode;
153 std::string pac_url;
154 dict->GetString("mode", &mode);
155 EXPECT_EQ(ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT), mode);
156 dict->GetString("pac_url", &pac_url);
157 EXPECT_EQ(expected_pac_url, pac_url);
158 }
159 void CheckProxyPrefSetToSystem() {
160 const DictionaryValue* dict = pref_service_.GetDictionary(prefs::kProxy);
161 std::string mode;
162 std::string pac_url;
163 dict->GetString("mode", &mode);
164 EXPECT_EQ(ProxyModeToString(ProxyPrefs::MODE_SYSTEM), mode);
165 dict->GetString("pac_url", &pac_url);
166 EXPECT_EQ(std::string(), pac_url);
167 }
168
169 void CheckProbe(bool initially_enabled, const std::string& probe_url,
170 const std::string& response, bool request_success,
171 bool expected_enabled) {
mmenke 2013/09/17 20:25:25 Fix indent.
bengr 2013/09/18 22:15:26 Done.
172 settings_->AddDefaultProxyBypassRules();
173 std::string pac;
174 base::Base64Encode(settings_->GetProxyPacScript(), &pac);
175 std::string expected_pac_url =
176 "data:application/x-ns-proxy-autoconfig;base64," + pac;
177 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, initially_enabled);
178 settings_->set_probe_for_testing(probe_url, response, request_success);
179 settings_->MaybeActivateDataReductionProxy(false);
180 base::MessageLoop::current()->RunUntilIdle();
181 if (expected_enabled)
182 CheckProxyPrefSetToDataReductionProxy(expected_pac_url);
183 else
184 CheckProxyPrefSetToSystem();
185 }
mmenke 2013/09/17 20:25:25 Fix indent.
bengr 2013/09/18 22:15:26 Done.
186
187 TestingPrefServiceSimple pref_service_;
188 scoped_ptr<TestDataReductionProxySettingsAndroid> settings_;
189 base::Time last_update_time_;
190 // This is a singleton that will clear all set field trials on destruction.
191 scoped_ptr<base::FieldTrialList> field_trial_list_;
192 };
193
194 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyOrigin) {
mmenke 2013/09/17 20:25:25 Oh, right...then should have a test that checks th
bengr 2013/09/18 22:15:26 I don't know what you mean here.
mmenke 2013/09/18 22:27:31 Sorry, field trial. Right now, you're testing the
mmenke 2013/09/18 22:30:02 Alternatively, could just get rid of the field tri
bengr 2013/09/19 20:18:15 I removed the field trial stuff. All of these test
195 JNIEnv* env = base::android::AttachCurrentThread();
196 DataReductionProxySettingsAndroid::Register(env);
197 // SetUp() adds the origin to the command line, which should be returned here.
198 ScopedJavaLocalRef<jstring> result =
199 settings_->GetDataReductionProxyOrigin(env, NULL);
200 ASSERT_TRUE(result.obj());
201 const base::android::JavaRef<jstring>& str_ref = result;
202 EXPECT_EQ(kDataReductionProxyOrigin, ConvertJavaStringToUTF8(str_ref));
203 }
204
205 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyAuth) {
206 JNIEnv* env = base::android::AttachCurrentThread();
207 DataReductionProxySettingsAndroid::Register(env);
208 // SetUp() adds the auth string to the command line, which should be returned
209 // here.
210 ScopedJavaLocalRef<jstring> result =
211 settings_->GetDataReductionProxyAuth(env, NULL);
212 ASSERT_TRUE(result.obj());
213 const base::android::JavaRef<jstring>& str_ref = result;
214 EXPECT_EQ(kDataReductionProxyAuth, ConvertJavaStringToUTF8(str_ref));
215 }
216
217 TEST_F(DataReductionProxySettingsAndroidTest, TestBypassRules) {
218 // Confirm that the bypass rule functions generate the intended JavaScript
219 // code for the Proxy PAC.
220 settings_->AddURLPatternToBypass("http://foo.com/*");
221 settings_->AddHostPatternToBypass("bar.com");
222 settings_->AddHostToBypass("127.0.0.1");
223
224 std::string expected[] = {
225 "shExpMatch(url, \"http://foo.com/*\")",
226 "shExpMatch(host, \"bar.com\")",
227 "host == \"127.0.0.1\""
228 };
229
230 int i = 0;
231 for (std::vector<std::string>::iterator it = settings_->bypass_rules_.begin();
232 it != settings_->bypass_rules_.end(); ++it) {
mmenke 2013/09/17 20:25:25 nit: Seems much more common to align the next lin
bengr 2013/09/18 22:15:26 Done.
233 EXPECT_EQ(expected[i++], *it);
234 }
235 }
236
237 TEST_F(DataReductionProxySettingsAndroidTest,
238 TestGetDataReductionProxyOriginHostPort) {
239 // The origin is specified on the command line in SetUp().
240 EXPECT_EQ(kDataReductionProxyOriginHostPort,
241 settings_->GetDataReductionProxyOriginHostPort());
242 }
243
244 TEST_F(DataReductionProxySettingsAndroidTest, TestIsProxyEnabledOrManaged) {
245 EXPECT_FALSE(settings_->IsDataReductionProxyEnabled(NULL, NULL));
246 EXPECT_FALSE(settings_->IsDataReductionProxyManaged(NULL, NULL));
247
248 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true);
249 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled(NULL, NULL));
250 EXPECT_FALSE(settings_->IsDataReductionProxyManaged(NULL, NULL));
251
252 pref_service_.SetManagedPref(prefs::kSpdyProxyAuthEnabled,
253 base::Value::CreateBooleanValue(true));
254 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled(NULL, NULL));
255 EXPECT_TRUE(settings_->IsDataReductionProxyManaged(NULL, NULL));
256 }
257
258 TEST_F(DataReductionProxySettingsAndroidTest, TestSetProxyPac) {
259 settings_->AddDefaultProxyBypassRules();
260 std::string pac;
261 base::Base64Encode(settings_->GetProxyPacScript(), &pac);
262 std::string expected_pac_url =
263 "data:application/x-ns-proxy-autoconfig;base64," + pac;
264 // Test setting the PAC, without generating histograms.
265 settings_->has_turned_on_ = true;
266 settings_->SetProxyPac(true, false);
267 CheckProxyPrefSetToDataReductionProxy(expected_pac_url);
268
269 // Test disabling the PAC, without generating histograms.
270 settings_->has_turned_off_ = true;
271 settings_->SetProxyPac(false, false);
272 CheckProxyPrefSetToSystem();
273 }
274
275 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDailyContentLengths) {
276 JNIEnv* env = base::android::AttachCurrentThread();
277 DataReductionProxySettingsAndroid::Register(env);
278 ScopedJavaLocalRef<jlongArray> result =
279 settings_->GetDailyContentLengths(env,
280 prefs::kDailyHttpOriginalContentLength);
281 ASSERT_TRUE(result.obj());
282
283 jsize java_array_len = env->GetArrayLength(result.obj());
284 ASSERT_EQ(static_cast<jsize>(spdyproxy::kNumDaysInHistory), java_array_len);
285
286 jlong value;
287 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) {
288 env->GetLongArrayRegion(result.obj(), i, 1, &value);
289 ASSERT_EQ(static_cast<long>(118 - (2 * i)), value);
290 }
291 }
292
293 TEST_F(DataReductionProxySettingsAndroidTest,
294 TestResetDataReductionStatistics) {
295 int64 original_content_length;
296 int64 received_content_length;
297 int64 last_update_time;
298 settings_->ResetDataReductionStatistics();
299 settings_->GetContentLengthsInternal(spdyproxy::kNumDaysInHistory,
300 &original_content_length,
301 &received_content_length,
302 &last_update_time);
303 EXPECT_EQ(0L, original_content_length);
304 EXPECT_EQ(0L, received_content_length);
305 EXPECT_EQ(last_update_time_.ToInternalValue(), last_update_time);
306 }
307
308
309 TEST_F(DataReductionProxySettingsAndroidTest, TestContentLengthsInternal) {
310 int64 original_content_length;
311 int64 received_content_length;
312 int64 last_update_time;
313 settings_->GetContentLengthsInternal(spdyproxy::kNumDaysInHistory,
314 &original_content_length,
315 &received_content_length,
316 &last_update_time);
317 EXPECT_EQ(3540L, original_content_length);
318 EXPECT_EQ(1770L, received_content_length);
319 EXPECT_EQ(last_update_time_.ToInternalValue(), last_update_time);
320 }
321
322 TEST_F(DataReductionProxySettingsAndroidTest,
323 TestMaybeActivateDataReductionProxy) {
324 // TODO(bengr): Test enabling/disabling while a probe is outstanding.
325 base::MessageLoop loop(base::MessageLoop::TYPE_UI);
326 // The proxy is enabled initially.
327 // Request succeeded but with bad response, expect proxy to be disabled.
328 CheckProbe(true, kProbeURLWithBadResponse, "Bad", true, false);
329 // Request succeeded with valid response, expect proxy to be enabled.
330 CheckProbe(true, kProbeURLWithOKResponse, "OK", true, true);
331 // Request failed, expect proxy to be disabled.
332 CheckProbe(true, kProbeURLWithNoResponse, "", false, false);
333
334 // The proxy is disabled initially. Probes should not be emitted to change
335 // state.
336 EXPECT_EQ(3, settings_->fake_fetcher_request_cnt());
337 CheckProbe(false, kProbeURLWithOKResponse, "OK", true, false);
338 EXPECT_EQ(3, settings_->fake_fetcher_request_cnt());
339 }
340
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698