OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest.h" | 5 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest.h" |
6 | 6 |
7 | 7 |
8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
10 #include "base/android/scoped_java_ref.h" | 10 #include "base/android/scoped_java_ref.h" |
11 #include "base/base64.h" | 11 #include "base/base64.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/metrics/field_trial.h" | 13 #include "base/metrics/field_trial.h" |
14 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
15 #include "base/prefs/testing_pref_service.h" | 15 #include "base/prefs/testing_pref_service.h" |
16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
17 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h" | 17 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h" |
18 #include "chrome/browser/prefs/proxy_prefs.h" | 18 #include "chrome/browser/prefs/proxy_prefs.h" |
19 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 19 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
20 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
21 #include "chrome/common/metrics/variations/variations_util.h" | 21 #include "chrome/common/metrics/variations/variations_util.h" |
22 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
23 #include "components/variations/entropy_provider.h" | 23 #include "components/variations/entropy_provider.h" |
24 #include "net/url_request/test_url_fetcher_factory.h" | 24 #include "net/url_request/test_url_fetcher_factory.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
26 #include "url/gurl.h" | 26 #include "url/gurl.h" |
27 | 27 |
28 const char kDataReductionProxyOrigin[] = "https://foo:443/"; | 28 const char kDataReductionProxyOrigin[] = "https://foo:443/"; |
29 const char kDataReductionProxyOriginHostPort[] = "foo:443"; | 29 const char kDataReductionProxyOriginPAC[] = "HTTPS foo:443;"; |
30 const char kDataReductionProxyFallback[] = "http://bar:80/"; | |
31 const char kDataReductionProxyFallbackPAC[] = "HTTP bar:80;"; | |
30 const char kDataReductionProxyAuth[] = "12345"; | 32 const char kDataReductionProxyAuth[] = "12345"; |
31 | 33 |
32 class TestDataReductionProxySettingsAndroid | 34 class TestDataReductionProxySettingsAndroid |
33 : public DataReductionProxySettingsAndroid { | 35 : public DataReductionProxySettingsAndroid { |
34 public: | 36 public: |
35 TestDataReductionProxySettingsAndroid(JNIEnv* env, | 37 TestDataReductionProxySettingsAndroid(JNIEnv* env, |
36 jobject obj, | 38 jobject obj, |
37 PrefService* profile_prefs, | 39 PrefService* profile_prefs, |
38 PrefService* local_state_prefs) | 40 PrefService* local_state_prefs) |
39 : DataReductionProxySettingsAndroid(env, obj), | 41 : DataReductionProxySettingsAndroid(env, obj), |
40 success_(false), | 42 success_(false), |
41 fake_fetcher_request_count_(0), | 43 fake_fetcher_request_count_(0), |
42 profile_prefs_(profile_prefs), | 44 profile_prefs_(profile_prefs), |
43 local_state_prefs_(local_state_prefs) { | 45 local_state_prefs_(local_state_prefs) { |
44 } | 46 } |
45 | 47 |
46 // DataReductionProxySettings implementation: | 48 // DataReductionProxySettings implementation: |
47 virtual net::URLFetcher* GetURLFetcher() OVERRIDE { | 49 virtual net::URLFetcher* GetURLFetcher() OVERRIDE { |
48 if (test_url_.empty()) | 50 if (test_url_.empty()) |
49 return NULL; | 51 return NULL; |
50 net::URLFetcher* fetcher = | 52 net::URLFetcher* fetcher = |
51 new net::FakeURLFetcher(GURL(test_url_), this, response_, success_); | 53 new net::FakeURLFetcher(GURL(test_url_), this, response_, success_); |
52 fake_fetcher_request_count_++; | 54 fake_fetcher_request_count_++; |
53 return fetcher; | 55 return fetcher; |
54 } | 56 } |
55 | 57 |
56 virtual PrefService* GetOriginalProfilePrefs() OVERRIDE { | 58 virtual PrefService* GetOriginalProfilePrefs() OVERRIDE { |
bengr
2013/10/22 17:49:30
Use MOCKs? Also this seems awfully similar to the
marq (ping after 24h)
2013/10/22 21:18:01
MOCKS: See previous comment.
I tried. The obvious
bengr
2013/10/22 21:44:21
Hmm. Add a TODO to revisit.
| |
57 return profile_prefs_; | 59 return profile_prefs_; |
58 } | 60 } |
61 | |
59 virtual PrefService* GetLocalStatePrefs() OVERRIDE { | 62 virtual PrefService* GetLocalStatePrefs() OVERRIDE { |
60 return local_state_prefs_; | 63 return local_state_prefs_; |
61 } | 64 } |
62 | 65 |
66 virtual std::string GetDefaultProxyHost() OVERRIDE { return std::string(); } | |
bengr
2013/10/22 17:49:30
move return to new line.
marq (ping after 24h)
2013/10/22 21:18:01
Done.
| |
67 virtual std::string GetDefaultFallbackProxyHost() OVERRIDE { | |
68 return std::string(); | |
69 } | |
70 | |
63 void set_probe_result(const std::string& test_url, | 71 void set_probe_result(const std::string& test_url, |
64 const std::string& response, | 72 const std::string& response, |
65 bool success) { | 73 bool success) { |
66 test_url_ = test_url; | 74 test_url_ = test_url; |
67 response_ = response; | 75 response_ = response; |
68 success_ = success; | 76 success_ = success; |
69 } | 77 } |
70 | 78 |
71 const int fake_fetcher_request_count() { | 79 const int fake_fetcher_request_count() { |
72 return fake_fetcher_request_count_; | 80 return fake_fetcher_request_count_; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyOrigin) { | 133 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyOrigin) { |
126 AddProxyToCommandLine(); | 134 AddProxyToCommandLine(); |
127 // SetUp() adds the origin to the command line, which should be returned here. | 135 // SetUp() adds the origin to the command line, which should be returned here. |
128 ScopedJavaLocalRef<jstring> result = | 136 ScopedJavaLocalRef<jstring> result = |
129 settings_->GetDataReductionProxyOrigin(env_, NULL); | 137 settings_->GetDataReductionProxyOrigin(env_, NULL); |
130 ASSERT_TRUE(result.obj()); | 138 ASSERT_TRUE(result.obj()); |
131 const base::android::JavaRef<jstring>& str_ref = result; | 139 const base::android::JavaRef<jstring>& str_ref = result; |
132 EXPECT_EQ(kDataReductionProxyOrigin, ConvertJavaStringToUTF8(str_ref)); | 140 EXPECT_EQ(kDataReductionProxyOrigin, ConvertJavaStringToUTF8(str_ref)); |
133 } | 141 } |
134 | 142 |
135 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyAuth) { | |
136 AddProxyToCommandLine(); | |
137 // SetUp() adds the auth string to the command line, which should be returned | |
138 // here. | |
139 ScopedJavaLocalRef<jstring> result = | |
140 settings_->GetDataReductionProxyAuth(env_, NULL); | |
141 ASSERT_TRUE(result.obj()); | |
142 const base::android::JavaRef<jstring>& str_ref = result; | |
143 EXPECT_EQ(kDataReductionProxyAuth, ConvertJavaStringToUTF8(str_ref)); | |
144 } | |
145 | |
146 // Test that the auth value set by preprocessor directive is not returned | |
147 // when an origin is set via a switch. This test only does anything useful in | |
148 // Chrome builds. | |
149 TEST_F(DataReductionProxySettingsAndroidTest, | |
150 TestGetDataReductionProxyAuthWithOriginSetViaSwitch) { | |
151 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
152 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); | |
153 // SetUp() adds the auth string to the command line, which should be returned | |
154 // here. | |
155 ScopedJavaLocalRef<jstring> result = | |
156 settings_->GetDataReductionProxyAuth(env_, NULL); | |
157 ASSERT_TRUE(result.obj()); | |
158 const base::android::JavaRef<jstring>& str_ref = result; | |
159 EXPECT_EQ(std::string(), ConvertJavaStringToUTF8(str_ref)); | |
160 } | |
161 | |
162 // Confirm that the bypass rule functions generate the intended JavaScript | 143 // Confirm that the bypass rule functions generate the intended JavaScript |
163 // code for the Proxy PAC. | 144 // code for the Proxy PAC. |
164 TEST_F(DataReductionProxySettingsAndroidTest, TestBypassPACRules) { | 145 TEST_F(DataReductionProxySettingsAndroidTest, TestBypassPACRules) { |
165 settings_->AddURLPatternToBypass("http://foo.com/*"); | 146 settings_->AddURLPatternToBypass("http://foo.com/*"); |
166 settings_->AddHostPatternToBypass("bar.com"); | 147 settings_->AddHostPatternToBypass("bar.com"); |
167 | 148 |
168 EXPECT_EQ(settings_->pac_bypass_rules_.size(), 1u); | 149 EXPECT_EQ(settings_->pac_bypass_rules_.size(), 1u); |
169 EXPECT_EQ("shExpMatch(url, 'http://foo.com/*')", | 150 EXPECT_EQ("shExpMatch(url, 'http://foo.com/*')", |
170 settings_->pac_bypass_rules_[0]); | 151 settings_->pac_bypass_rules_[0]); |
171 | 152 |
172 EXPECT_EQ(settings_->BypassRules().size(), 1u); | 153 EXPECT_EQ(settings_->BypassRules().size(), 1u); |
173 EXPECT_EQ("bar.com", settings_->BypassRules()[0]); | 154 EXPECT_EQ("bar.com", settings_->BypassRules()[0]); |
174 } | 155 } |
175 | 156 |
176 TEST_F(DataReductionProxySettingsAndroidTest, TestSetProxyPac) { | 157 TEST_F(DataReductionProxySettingsAndroidTest, TestSetProxyPac) { |
158 AddProxyToCommandLine(); | |
177 settings_->AddDefaultProxyBypassRules(); | 159 settings_->AddDefaultProxyBypassRules(); |
160 std::string raw_pac = settings_->GetProxyPacScript(); | |
161 EXPECT_NE(raw_pac.find(kDataReductionProxyOriginPAC), std::string::npos); | |
162 EXPECT_NE(raw_pac.find(kDataReductionProxyFallbackPAC), std::string::npos);; | |
178 std::string pac; | 163 std::string pac; |
179 base::Base64Encode(settings_->GetProxyPacScript(), &pac); | 164 base::Base64Encode(raw_pac, &pac); |
180 std::string expected_pac_url = | 165 std::string expected_pac_url = |
181 "data:application/x-ns-proxy-autoconfig;base64," + pac; | 166 "data:application/x-ns-proxy-autoconfig;base64," + pac; |
182 // Test setting the PAC, without generating histograms. | 167 // Test setting the PAC, without generating histograms. |
183 settings_->SetHasTurnedOn(); | 168 settings_->SetHasTurnedOn(); |
184 settings_->SetProxyConfigs(true, false); | 169 settings_->SetProxyConfigs(true, false); |
185 CheckProxyPacPref(expected_pac_url, | 170 CheckProxyPacPref(expected_pac_url, |
186 ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT)); | 171 ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT)); |
187 | 172 |
188 // Test disabling the PAC, without generating histograms. | 173 // Test disabling the PAC, without generating histograms. |
189 settings_->SetHasTurnedOff(); | 174 settings_->SetHasTurnedOff(); |
(...skipping 10 matching lines...) Expand all Loading... | |
200 ASSERT_EQ(static_cast<jsize>(spdyproxy::kNumDaysInHistory), java_array_len); | 185 ASSERT_EQ(static_cast<jsize>(spdyproxy::kNumDaysInHistory), java_array_len); |
201 | 186 |
202 jlong value; | 187 jlong value; |
203 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) { | 188 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) { |
204 env_->GetLongArrayRegion(result.obj(), i, 1, &value); | 189 env_->GetLongArrayRegion(result.obj(), i, 1, &value); |
205 ASSERT_EQ( | 190 ASSERT_EQ( |
206 static_cast<long>((spdyproxy::kNumDaysInHistory - 1 - i) * 2), value); | 191 static_cast<long>((spdyproxy::kNumDaysInHistory - 1 - i) * 2), value); |
207 } | 192 } |
208 } | 193 } |
209 | 194 |
OLD | NEW |