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

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 comments from mef 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 void AddProxyToCommandLine() {
111 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
112 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin);
113 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
114 switches::kSpdyProxyAuthValue, kDataReductionProxyAuth);
115 }
116
117 // testing::Test implementation:
118 virtual void SetUp() OVERRIDE {
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);
mef 2013/09/24 16:44:15 nit: EXPECT_TRUE(pac_url.empty());
bengr 2013/09/24 21:13:34 Done.
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 AddProxyToCommandLine();
191 JNIEnv* env = base::android::AttachCurrentThread();
192 DataReductionProxySettingsAndroid::Register(env);
193 // SetUp() adds the origin to the command line, which should be returned here.
194 ScopedJavaLocalRef<jstring> result =
195 settings_->GetDataReductionProxyOrigin(env, NULL);
196 ASSERT_TRUE(result.obj());
197 const base::android::JavaRef<jstring>& str_ref = result;
198 EXPECT_EQ(kDataReductionProxyOrigin, ConvertJavaStringToUTF8(str_ref));
199 }
200
201 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyAuth) {
202 AddProxyToCommandLine();
203 JNIEnv* env = base::android::AttachCurrentThread();
204 DataReductionProxySettingsAndroid::Register(env);
205 // SetUp() adds the auth string to the command line, which should be returned
206 // here.
207 ScopedJavaLocalRef<jstring> result =
208 settings_->GetDataReductionProxyAuth(env, NULL);
209 ASSERT_TRUE(result.obj());
210 const base::android::JavaRef<jstring>& str_ref = result;
211 EXPECT_EQ(kDataReductionProxyAuth, ConvertJavaStringToUTF8(str_ref));
212 }
213
214 TEST_F(DataReductionProxySettingsAndroidTest,
215 TestGetDataReductionProxyAuthWithOriginSetViaSwitch) {
216 // Test that the auth value set by preprocessor directive is not returned
mef 2013/09/24 16:44:15 nit: Put comment above test declaration. Same for
bengr 2013/09/24 21:13:34 Done.
217 // when an origin is set via a switch.
218 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
219 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin);
220 JNIEnv* env = base::android::AttachCurrentThread();
221 DataReductionProxySettingsAndroid::Register(env);
222 // SetUp() adds the auth string to the command line, which should be returned
223 // here.
224 ScopedJavaLocalRef<jstring> result =
225 settings_->GetDataReductionProxyAuth(env, NULL);
226 ASSERT_TRUE(result.obj());
227 const base::android::JavaRef<jstring>& str_ref = result;
228 EXPECT_EQ(std::string(), ConvertJavaStringToUTF8(str_ref));
229 }
230
231 TEST_F(DataReductionProxySettingsAndroidTest, TestBypassRules) {
232 // Confirm that the bypass rule functions generate the intended JavaScript
233 // code for the Proxy PAC.
234 settings_->AddURLPatternToBypass("http://foo.com/*");
235 settings_->AddHostPatternToBypass("bar.com");
236 settings_->AddHostToBypass("127.0.0.1");
237
238 std::string expected[] = {
239 "shExpMatch(url, \"http://foo.com/*\")",
240 "shExpMatch(host, \"bar.com\")",
241 "host == \"127.0.0.1\""
242 };
243
244 int i = 0;
245 for (std::vector<std::string>::iterator it = settings_->bypass_rules_.begin();
246 it != settings_->bypass_rules_.end(); ++it) {
247 EXPECT_EQ(expected[i++], *it);
248 }
249 }
250
251 TEST_F(DataReductionProxySettingsAndroidTest, TestIsProxyEnabledOrManaged) {
252 EXPECT_FALSE(settings_->IsDataReductionProxyEnabled(NULL, NULL));
253 EXPECT_FALSE(settings_->IsDataReductionProxyManaged(NULL, NULL));
254
255 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true);
256 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled(NULL, NULL));
257 EXPECT_FALSE(settings_->IsDataReductionProxyManaged(NULL, NULL));
258
259 pref_service_.SetManagedPref(prefs::kSpdyProxyAuthEnabled,
260 base::Value::CreateBooleanValue(true));
261 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled(NULL, NULL));
262 EXPECT_TRUE(settings_->IsDataReductionProxyManaged(NULL, NULL));
263 }
264
265 TEST_F(DataReductionProxySettingsAndroidTest, TestSetProxyPac) {
266 settings_->AddDefaultProxyBypassRules();
267 std::string pac;
268 base::Base64Encode(settings_->GetProxyPacScript(), &pac);
269 std::string expected_pac_url =
270 "data:application/x-ns-proxy-autoconfig;base64," + pac;
271 // Test setting the PAC, without generating histograms.
272 settings_->has_turned_on_ = true;
273 settings_->SetProxyPac(true, false);
274 CheckProxyPrefSetToDataReductionProxy(expected_pac_url);
275
276 // Test disabling the PAC, without generating histograms.
277 settings_->has_turned_off_ = true;
278 settings_->SetProxyPac(false, false);
279 CheckProxyPrefSetToSystem();
280 }
281
282 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDailyContentLengths) {
283 JNIEnv* env = base::android::AttachCurrentThread();
284 DataReductionProxySettingsAndroid::Register(env);
285 ScopedJavaLocalRef<jlongArray> result =
286 settings_->GetDailyContentLengths(env,
287 prefs::kDailyHttpOriginalContentLength);
288 ASSERT_TRUE(result.obj());
289
290 jsize java_array_len = env->GetArrayLength(result.obj());
291 ASSERT_EQ(static_cast<jsize>(spdyproxy::kNumDaysInHistory), java_array_len);
292
293 jlong value;
294 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) {
295 env->GetLongArrayRegion(result.obj(), i, 1, &value);
296 ASSERT_EQ(static_cast<long>(118 - (2 * i)), value);
mef 2013/09/24 16:44:15 What's the magic of 118?
bengr 2013/09/24 21:13:34 Done.
297 }
298 }
299
300 TEST_F(DataReductionProxySettingsAndroidTest,
301 TestResetDataReductionStatistics) {
302 int64 original_content_length;
303 int64 received_content_length;
304 int64 last_update_time;
305 settings_->ResetDataReductionStatistics();
306 settings_->GetContentLengthsInternal(spdyproxy::kNumDaysInHistory,
307 &original_content_length,
308 &received_content_length,
309 &last_update_time);
310 EXPECT_EQ(0L, original_content_length);
311 EXPECT_EQ(0L, received_content_length);
312 EXPECT_EQ(last_update_time_.ToInternalValue(), last_update_time);
313 }
314
315
316 TEST_F(DataReductionProxySettingsAndroidTest, TestContentLengthsInternal) {
317 int64 original_content_length;
318 int64 received_content_length;
319 int64 last_update_time;
320 settings_->GetContentLengthsInternal(spdyproxy::kNumDaysInHistory,
mef 2013/09/24 16:44:15 Add tests with days == 0, 1, spdyproxy::kNumDaysIn
bengr 2013/09/24 21:13:34 Done, except for kNumDaysInHistory + 1, because Ge
321 &original_content_length,
322 &received_content_length,
323 &last_update_time);
324 EXPECT_EQ(3540L, original_content_length);
325 EXPECT_EQ(1770L, received_content_length);
mef 2013/09/24 16:44:15 Where are these constants coming from?
bengr 2013/09/24 21:13:34 Done.
326 EXPECT_EQ(last_update_time_.ToInternalValue(), last_update_time);
327 }
328
329 TEST_F(DataReductionProxySettingsAndroidTest,
330 TestMaybeActivateDataReductionProxy) {
331 // TODO(bengr): Test enabling/disabling while a probe is outstanding.
332 base::MessageLoop loop(base::MessageLoop::TYPE_UI);
333 // The proxy is enabled initially.
334 // Request succeeded but with bad response, expect proxy to be disabled.
335 CheckProbe(true, kProbeURLWithBadResponse, "Bad", true, false);
336 // Request succeeded with valid response, expect proxy to be enabled.
337 CheckProbe(true, kProbeURLWithOKResponse, "OK", true, true);
338 // Request failed, expect proxy to be disabled.
339 CheckProbe(true, kProbeURLWithNoResponse, "", false, false);
340
341 // The proxy is disabled initially. Probes should not be emitted to change
342 // state.
343 EXPECT_EQ(3, settings_->fake_fetcher_request_count());
344 CheckProbe(false, kProbeURLWithOKResponse, "OK", true, false);
345 EXPECT_EQ(3, settings_->fake_fetcher_request_count());
346 }
347
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698