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.com:443/"; |
29 const char kDataReductionProxyAuth[] = "12345"; | 29 const char kDataReductionProxyOriginPAC[] = "HTTPS foo.com:443;"; |
| 30 const char kDataReductionProxyFallbackPAC[] = "HTTP bar.com:80;"; |
30 | 31 |
31 class TestDataReductionProxySettingsAndroid | 32 class TestDataReductionProxySettingsAndroid |
32 : public DataReductionProxySettingsAndroid { | 33 : public DataReductionProxySettingsAndroid { |
33 public: | 34 public: |
34 TestDataReductionProxySettingsAndroid(JNIEnv* env, | 35 TestDataReductionProxySettingsAndroid(JNIEnv* env, |
35 jobject obj, | 36 jobject obj, |
36 PrefService* profile_prefs, | 37 PrefService* profile_prefs, |
37 PrefService* local_state_prefs) | 38 PrefService* local_state_prefs) |
38 : DataReductionProxySettingsAndroid(env, obj), | 39 : DataReductionProxySettingsAndroid(env, obj), |
39 success_(false), | 40 success_(false), |
40 fake_fetcher_request_count_(0), | 41 fake_fetcher_request_count_(0), |
41 profile_prefs_(profile_prefs), | 42 profile_prefs_(profile_prefs), |
42 local_state_prefs_(local_state_prefs) { | 43 local_state_prefs_(local_state_prefs) { |
43 } | 44 } |
44 | 45 |
45 // DataReductionProxySettings implementation: | 46 // TODO(marq): Replace virtual methods with MOCKs. |
| 47 // DataReductionProxySettingsAndroid implementation: |
46 virtual net::URLFetcher* GetURLFetcher() OVERRIDE { | 48 virtual net::URLFetcher* GetURLFetcher() OVERRIDE { |
47 if (test_url_.empty()) | 49 if (test_url_.empty()) |
48 return NULL; | 50 return NULL; |
49 net::URLFetcher* fetcher = | 51 net::URLFetcher* fetcher = |
50 new net::FakeURLFetcher(GURL(test_url_), this, response_, success_); | 52 new net::FakeURLFetcher(GURL(test_url_), this, response_, success_); |
51 fake_fetcher_request_count_++; | 53 fake_fetcher_request_count_++; |
52 return fetcher; | 54 return fetcher; |
53 } | 55 } |
54 | 56 |
55 virtual PrefService* GetOriginalProfilePrefs() OVERRIDE { | 57 virtual PrefService* GetOriginalProfilePrefs() OVERRIDE { |
56 return profile_prefs_; | 58 return profile_prefs_; |
57 } | 59 } |
| 60 |
58 virtual PrefService* GetLocalStatePrefs() OVERRIDE { | 61 virtual PrefService* GetLocalStatePrefs() OVERRIDE { |
59 return local_state_prefs_; | 62 return local_state_prefs_; |
60 } | 63 } |
61 | 64 |
62 void set_probe_result(const std::string& test_url, | 65 void set_probe_result(const std::string& test_url, |
63 const std::string& response, | 66 const std::string& response, |
64 bool success) { | 67 bool success) { |
65 test_url_ = test_url; | 68 test_url_ = test_url; |
66 response_ = response; | 69 response_ = response; |
67 success_ = success; | 70 success_ = success; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyOrigin) { | 127 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyOrigin) { |
125 AddProxyToCommandLine(); | 128 AddProxyToCommandLine(); |
126 // SetUp() adds the origin to the command line, which should be returned here. | 129 // SetUp() adds the origin to the command line, which should be returned here. |
127 ScopedJavaLocalRef<jstring> result = | 130 ScopedJavaLocalRef<jstring> result = |
128 settings_->GetDataReductionProxyOrigin(env_, NULL); | 131 settings_->GetDataReductionProxyOrigin(env_, NULL); |
129 ASSERT_TRUE(result.obj()); | 132 ASSERT_TRUE(result.obj()); |
130 const base::android::JavaRef<jstring>& str_ref = result; | 133 const base::android::JavaRef<jstring>& str_ref = result; |
131 EXPECT_EQ(kDataReductionProxyOrigin, ConvertJavaStringToUTF8(str_ref)); | 134 EXPECT_EQ(kDataReductionProxyOrigin, ConvertJavaStringToUTF8(str_ref)); |
132 } | 135 } |
133 | 136 |
134 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyAuth) { | |
135 AddProxyToCommandLine(); | |
136 // SetUp() adds the auth string to the command line, which should be returned | |
137 // here. | |
138 ScopedJavaLocalRef<jstring> result = | |
139 settings_->GetDataReductionProxyAuth(env_, NULL); | |
140 ASSERT_TRUE(result.obj()); | |
141 const base::android::JavaRef<jstring>& str_ref = result; | |
142 EXPECT_EQ(kDataReductionProxyAuth, ConvertJavaStringToUTF8(str_ref)); | |
143 } | |
144 | |
145 // Test that the auth value set by preprocessor directive is not returned | |
146 // when an origin is set via a switch. This test only does anything useful in | |
147 // Chrome builds. | |
148 TEST_F(DataReductionProxySettingsAndroidTest, | |
149 TestGetDataReductionProxyAuthWithOriginSetViaSwitch) { | |
150 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
151 switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin); | |
152 // SetUp() adds the auth string to the command line, which should be returned | |
153 // here. | |
154 ScopedJavaLocalRef<jstring> result = | |
155 settings_->GetDataReductionProxyAuth(env_, NULL); | |
156 ASSERT_TRUE(result.obj()); | |
157 const base::android::JavaRef<jstring>& str_ref = result; | |
158 EXPECT_EQ(std::string(), ConvertJavaStringToUTF8(str_ref)); | |
159 } | |
160 | |
161 // Confirm that the bypass rule functions generate the intended JavaScript | 137 // Confirm that the bypass rule functions generate the intended JavaScript |
162 // code for the Proxy PAC. | 138 // code for the Proxy PAC. |
163 TEST_F(DataReductionProxySettingsAndroidTest, TestBypassPACRules) { | 139 TEST_F(DataReductionProxySettingsAndroidTest, TestBypassPACRules) { |
164 settings_->AddURLPatternToBypass("http://foo.com/*"); | 140 settings_->AddURLPatternToBypass("http://foo.com/*"); |
165 settings_->AddHostPatternToBypass("bar.com"); | 141 settings_->AddHostPatternToBypass("bar.com"); |
166 | 142 |
167 EXPECT_EQ(settings_->pac_bypass_rules_.size(), 1u); | 143 EXPECT_EQ(settings_->pac_bypass_rules_.size(), 1u); |
168 EXPECT_EQ("shExpMatch(url, 'http://foo.com/*')", | 144 EXPECT_EQ("shExpMatch(url, 'http://foo.com/*')", |
169 settings_->pac_bypass_rules_[0]); | 145 settings_->pac_bypass_rules_[0]); |
170 | 146 |
171 EXPECT_EQ(settings_->BypassRules().size(), 1u); | 147 EXPECT_EQ(settings_->BypassRules().size(), 1u); |
172 EXPECT_EQ("bar.com", settings_->BypassRules()[0]); | 148 EXPECT_EQ("bar.com", settings_->BypassRules()[0]); |
173 } | 149 } |
174 | 150 |
175 TEST_F(DataReductionProxySettingsAndroidTest, TestSetProxyPac) { | 151 TEST_F(DataReductionProxySettingsAndroidTest, TestSetProxyPac) { |
| 152 AddProxyToCommandLine(); |
176 settings_->AddDefaultProxyBypassRules(); | 153 settings_->AddDefaultProxyBypassRules(); |
| 154 std::string raw_pac = settings_->GetProxyPacScript(); |
| 155 EXPECT_NE(raw_pac.find(kDataReductionProxyOriginPAC), std::string::npos); |
| 156 EXPECT_NE(raw_pac.find(kDataReductionProxyFallbackPAC), std::string::npos);; |
177 std::string pac; | 157 std::string pac; |
178 base::Base64Encode(settings_->GetProxyPacScript(), &pac); | 158 base::Base64Encode(raw_pac, &pac); |
179 std::string expected_pac_url = | 159 std::string expected_pac_url = |
180 "data:application/x-ns-proxy-autoconfig;base64," + pac; | 160 "data:application/x-ns-proxy-autoconfig;base64," + pac; |
181 // Test setting the PAC, without generating histograms. | 161 // Test setting the PAC, without generating histograms. |
182 settings_->SetHasTurnedOn(); | 162 settings_->SetHasTurnedOn(); |
183 settings_->SetProxyConfigs(true, false); | 163 settings_->SetProxyConfigs(true, false); |
184 CheckProxyPacPref(expected_pac_url, | 164 CheckProxyPacPref(expected_pac_url, |
185 ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT)); | 165 ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT)); |
186 | 166 |
187 // Test disabling the PAC, without generating histograms. | 167 // Test disabling the PAC, without generating histograms. |
188 settings_->SetHasTurnedOff(); | 168 settings_->SetHasTurnedOff(); |
(...skipping 10 matching lines...) Expand all Loading... |
199 ASSERT_EQ(static_cast<jsize>(spdyproxy::kNumDaysInHistory), java_array_len); | 179 ASSERT_EQ(static_cast<jsize>(spdyproxy::kNumDaysInHistory), java_array_len); |
200 | 180 |
201 jlong value; | 181 jlong value; |
202 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) { | 182 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) { |
203 env_->GetLongArrayRegion(result.obj(), i, 1, &value); | 183 env_->GetLongArrayRegion(result.obj(), i, 1, &value); |
204 ASSERT_EQ( | 184 ASSERT_EQ( |
205 static_cast<long>((spdyproxy::kNumDaysInHistory - 1 - i) * 2), value); | 185 static_cast<long>((spdyproxy::kNumDaysInHistory - 1 - i) * 2), value); |
206 } | 186 } |
207 } | 187 } |
208 | 188 |
OLD | NEW |