OLD | NEW |
---|---|
(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() { | |
mmenke
2013/09/26 18:09:44
nit: const
bengr
2013/09/27 00:28:13
Done.
| |
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_; | |
mmenke
2013/09/26 18:09:44
nit: Now that these are only in test code, the "f
bengr
2013/09/27 00:28:13
Done.
| |
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(); | |
mmenke
2013/09/26 18:09:44
Since we enable at the command line, I don't any o
bengr
2013/09/27 00:28:13
Done.
| |
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 } | |
mmenke
2013/09/26 18:09:44
Again, doesn't seem to do anything.
bengr
2013/09/27 00:28:13
Done.
| |
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); | |
mmenke
2013/09/26 18:09:44
Should probably assert on the result in these 4 Ge
bengr
2013/09/27 00:28:13
Done.
| |
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 } | |
mmenke
2013/09/26 18:09:44
nit: Line break
bengr
2013/09/27 00:28:13
Done.
| |
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 EXPECT_TRUE(pac_url.empty()); | |
163 } | |
164 | |
165 void CheckProbe(bool initially_enabled, const std::string& probe_url, | |
166 const std::string& response, bool request_success, | |
167 bool expected_enabled) { | |
168 settings_->AddDefaultProxyBypassRules(); | |
mmenke
2013/09/26 18:09:44
I don't think we should be making this call, since
bengr
2013/09/27 00:28:13
Done.
| |
169 std::string pac; | |
170 base::Base64Encode(settings_->GetProxyPacScript(), &pac); | |
171 std::string expected_pac_url = | |
172 "data:application/x-ns-proxy-autoconfig;base64," + pac; | |
173 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, initially_enabled); | |
174 settings_->set_probe_result(probe_url, response, request_success); | |
175 settings_->MaybeActivateDataReductionProxy(false); | |
176 base::MessageLoop::current()->RunUntilIdle(); | |
177 if (expected_enabled) | |
178 CheckProxyPrefSetToDataReductionProxy(expected_pac_url); | |
179 else | |
180 CheckProxyPrefSetToSystem(); | |
181 } | |
182 | |
183 TestingPrefServiceSimple pref_service_; | |
184 scoped_ptr<TestDataReductionProxySettingsAndroid> settings_; | |
185 base::Time last_update_time_; | |
186 // This is a singleton that will clear all set field trials on destruction. | |
187 scoped_ptr<base::FieldTrialList> field_trial_list_; | |
188 }; | |
189 | |
190 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyOrigin) { | |
191 AddProxyToCommandLine(); | |
192 JNIEnv* env = base::android::AttachCurrentThread(); | |
193 DataReductionProxySettingsAndroid::Register(env); | |
194 // SetUp() adds the origin to the command line, which should be returned here. | |
195 ScopedJavaLocalRef<jstring> result = | |
196 settings_->GetDataReductionProxyOrigin(env, NULL); | |
197 ASSERT_TRUE(result.obj()); | |
198 const base::android::JavaRef<jstring>& str_ref = result; | |
199 EXPECT_EQ(kDataReductionProxyOrigin, ConvertJavaStringToUTF8(str_ref)); | |
200 } | |
201 | |
202 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyAuth) { | |
203 AddProxyToCommandLine(); | |
204 JNIEnv* env = base::android::AttachCurrentThread(); | |
205 DataReductionProxySettingsAndroid::Register(env); | |
206 // SetUp() adds the auth string to the command line, which should be returned | |
207 // here. | |
208 ScopedJavaLocalRef<jstring> result = | |
209 settings_->GetDataReductionProxyAuth(env, NULL); | |
210 ASSERT_TRUE(result.obj()); | |
211 const base::android::JavaRef<jstring>& str_ref = result; | |
212 EXPECT_EQ(kDataReductionProxyAuth, ConvertJavaStringToUTF8(str_ref)); | |
213 } | |
214 | |
215 // Test that the auth value set by preprocessor directive is not returned | |
216 // when an origin is set via a switch. | |
mmenke
2013/09/26 18:09:44
There is no preprocessor directive anyways in Chro
bengr
2013/09/27 00:28:13
Done.
| |
217 TEST_F(DataReductionProxySettingsAndroidTest, | |
218 TestGetDataReductionProxyAuthWithOriginSetViaSwitch) { | |
219 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
220 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); | |
221 JNIEnv* env = base::android::AttachCurrentThread(); | |
222 DataReductionProxySettingsAndroid::Register(env); | |
mmenke
2013/09/26 18:09:44
optional: May want to just move these two lines i
bengr
2013/09/27 00:28:13
Done.
| |
223 // SetUp() adds the auth string to the command line, which should be returned | |
224 // here. | |
225 ScopedJavaLocalRef<jstring> result = | |
226 settings_->GetDataReductionProxyAuth(env, NULL); | |
227 ASSERT_TRUE(result.obj()); | |
228 const base::android::JavaRef<jstring>& str_ref = result; | |
229 EXPECT_EQ(std::string(), ConvertJavaStringToUTF8(str_ref)); | |
230 } | |
231 | |
232 // Confirm that the bypass rule functions generate the intended JavaScript | |
233 // code for the Proxy PAC. | |
234 TEST_F(DataReductionProxySettingsAndroidTest, TestBypassRules) { | |
235 settings_->AddURLPatternToBypass("http://foo.com/*"); | |
236 settings_->AddHostPatternToBypass("bar.com"); | |
237 settings_->AddHostToBypass("127.0.0.1"); | |
238 | |
239 std::string expected[] = { | |
240 "shExpMatch(url, \"http://foo.com/*\")", | |
241 "shExpMatch(host, \"bar.com\")", | |
242 "host == \"127.0.0.1\"" | |
243 }; | |
244 | |
245 int i = 0; | |
246 for (std::vector<std::string>::iterator it = settings_->bypass_rules_.begin(); | |
247 it != settings_->bypass_rules_.end(); ++it) { | |
248 EXPECT_EQ(expected[i++], *it); | |
249 } | |
250 } | |
251 | |
252 TEST_F(DataReductionProxySettingsAndroidTest, TestIsProxyEnabledOrManaged) { | |
253 EXPECT_FALSE(settings_->IsDataReductionProxyEnabled(NULL, NULL)); | |
254 EXPECT_FALSE(settings_->IsDataReductionProxyManaged(NULL, NULL)); | |
255 | |
256 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true); | |
257 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled(NULL, NULL)); | |
258 EXPECT_FALSE(settings_->IsDataReductionProxyManaged(NULL, NULL)); | |
259 | |
260 pref_service_.SetManagedPref(prefs::kSpdyProxyAuthEnabled, | |
261 base::Value::CreateBooleanValue(true)); | |
262 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled(NULL, NULL)); | |
263 EXPECT_TRUE(settings_->IsDataReductionProxyManaged(NULL, NULL)); | |
264 } | |
265 | |
266 TEST_F(DataReductionProxySettingsAndroidTest, TestSetProxyPac) { | |
267 settings_->AddDefaultProxyBypassRules(); | |
268 std::string pac; | |
269 base::Base64Encode(settings_->GetProxyPacScript(), &pac); | |
270 std::string expected_pac_url = | |
271 "data:application/x-ns-proxy-autoconfig;base64," + pac; | |
272 // Test setting the PAC, without generating histograms. | |
273 settings_->has_turned_on_ = true; | |
274 settings_->SetProxyPac(true, false); | |
275 CheckProxyPrefSetToDataReductionProxy(expected_pac_url); | |
276 | |
277 // Test disabling the PAC, without generating histograms. | |
278 settings_->has_turned_off_ = true; | |
279 settings_->SetProxyPac(false, false); | |
280 CheckProxyPrefSetToSystem(); | |
281 } | |
282 | |
283 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDailyContentLengths) { | |
284 JNIEnv* env = base::android::AttachCurrentThread(); | |
285 DataReductionProxySettingsAndroid::Register(env); | |
286 ScopedJavaLocalRef<jlongArray> result = | |
287 settings_->GetDailyContentLengths(env, | |
288 prefs::kDailyHttpOriginalContentLength); | |
289 ASSERT_TRUE(result.obj()); | |
290 | |
291 jsize java_array_len = env->GetArrayLength(result.obj()); | |
292 ASSERT_EQ(static_cast<jsize>(spdyproxy::kNumDaysInHistory), java_array_len); | |
293 | |
294 jlong value; | |
295 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) { | |
296 env->GetLongArrayRegion(result.obj(), i, 1, &value); | |
297 ASSERT_EQ( | |
298 static_cast<long>((spdyproxy::kNumDaysInHistory - 1 - i) * 2), value); | |
299 } | |
300 } | |
301 | |
302 TEST_F(DataReductionProxySettingsAndroidTest, | |
303 TestResetDataReductionStatistics) { | |
304 int64 original_content_length; | |
305 int64 received_content_length; | |
306 int64 last_update_time; | |
307 settings_->ResetDataReductionStatistics(); | |
308 settings_->GetContentLengthsInternal(spdyproxy::kNumDaysInHistory, | |
309 &original_content_length, | |
310 &received_content_length, | |
311 &last_update_time); | |
312 EXPECT_EQ(0L, original_content_length); | |
313 EXPECT_EQ(0L, received_content_length); | |
314 EXPECT_EQ(last_update_time_.ToInternalValue(), last_update_time); | |
315 } | |
316 | |
317 TEST_F(DataReductionProxySettingsAndroidTest, TestContentLengthsInternal) { | |
318 int64 original_content_length; | |
319 int64 received_content_length; | |
320 int64 last_update_time; | |
321 | |
322 // Request |kNumDaysInHistory| days. | |
323 settings_->GetContentLengthsInternal(spdyproxy::kNumDaysInHistory, | |
324 &original_content_length, | |
325 &received_content_length, | |
326 &last_update_time); | |
327 const unsigned int days = spdyproxy::kNumDaysInHistory; | |
328 // Received content length history values are 0 to |kNumDaysInHistory - 1|. | |
329 int64 expected_total_received_content_length = (days - 1L) * days / 2; | |
330 // Original content length history values are 0 to | |
331 // |2 * (kNumDaysInHistory - 1)|. | |
332 long expected_total_original_content_length = (days - 1L) * days; | |
333 EXPECT_EQ(expected_total_original_content_length, original_content_length); | |
334 EXPECT_EQ(expected_total_received_content_length, received_content_length); | |
335 EXPECT_EQ(last_update_time_.ToInternalValue(), last_update_time); | |
336 | |
337 // Request |kNumDaysInHistory - 1| days. | |
338 settings_->GetContentLengthsInternal(spdyproxy::kNumDaysInHistory - 1, | |
339 &original_content_length, | |
340 &received_content_length, | |
341 &last_update_time); | |
342 expected_total_received_content_length -= (days - 1); | |
343 expected_total_original_content_length -= 2 * (days - 1); | |
344 EXPECT_EQ(expected_total_original_content_length, original_content_length); | |
345 EXPECT_EQ(expected_total_received_content_length, received_content_length); | |
346 | |
347 // Request 0 days. | |
348 settings_->GetContentLengthsInternal(0, | |
349 &original_content_length, | |
350 &received_content_length, | |
351 &last_update_time); | |
352 expected_total_received_content_length = 0; | |
353 expected_total_original_content_length = 0; | |
354 EXPECT_EQ(expected_total_original_content_length, original_content_length); | |
355 EXPECT_EQ(expected_total_received_content_length, received_content_length); | |
356 | |
357 // Request 1 day. First day had 0 bytes so should be same as 0 days. | |
358 settings_->GetContentLengthsInternal(1, | |
359 &original_content_length, | |
360 &received_content_length, | |
361 &last_update_time); | |
362 EXPECT_EQ(expected_total_original_content_length, original_content_length); | |
363 EXPECT_EQ(expected_total_received_content_length, received_content_length); | |
364 } | |
365 | |
366 TEST_F(DataReductionProxySettingsAndroidTest, | |
367 TestMaybeActivateDataReductionProxy) { | |
368 // TODO(bengr): Test enabling/disabling while a probe is outstanding. | |
369 base::MessageLoop loop(base::MessageLoop::TYPE_UI); | |
370 // The proxy is enabled initially. | |
371 // Request succeeded but with bad response, expect proxy to be disabled. | |
372 CheckProbe(true, kProbeURLWithBadResponse, "Bad", true, false); | |
373 // Request succeeded with valid response, expect proxy to be enabled. | |
374 CheckProbe(true, kProbeURLWithOKResponse, "OK", true, true); | |
375 // Request failed, expect proxy to be disabled. | |
376 CheckProbe(true, kProbeURLWithNoResponse, "", false, false); | |
377 | |
378 // The proxy is disabled initially. Probes should not be emitted to change | |
379 // state. | |
380 EXPECT_EQ(3, settings_->fake_fetcher_request_count()); | |
381 CheckProbe(false, kProbeURLWithOKResponse, "OK", true, false); | |
382 EXPECT_EQ(3, settings_->fake_fetcher_request_count()); | |
383 } | |
mmenke
2013/09/26 18:09:44
We never test OnIPAddressChanged, which follows a
mmenke
2013/09/26 18:09:44
Also, none of these tests call InitDataReductionPr
mmenke
2013/09/26 18:09:44
Also, none of the tests check that when prefs::kSp
bengr
2013/09/27 00:28:13
Done.
bengr
2013/09/27 00:28:13
Done.
bengr
2013/09/27 00:28:13
Done.
| |
384 | |
OLD | NEW |