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.
5 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/android/scoped_java_ref.h"
10 #include "base/base64.h"
11 #include "base/command_line.h"
12 #include "base/metrics/field_trial.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/prefs/scoped_user_pref_update.h"
15 #include "base/prefs/testing_pref_service.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h"
18 #include "chrome/browser/prefs/proxy_prefs.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/metrics/variations/variations_util.h"
21 #include "chrome/common/pref_names.h"
22 #include "components/variations/entropy_provider.h"
23 #include "net/url_request/test_url_fetcher_factory.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
28 const char kDataReductionProxyOrigin
[] = "https://foo.com:443/";
29 const char kDataReductionProxyDevHost
[] = "http://foo-dev.com:80";
30 const char kDataReductionProxyOriginPAC
[] = "HTTPS foo.com:443;";
31 const char kDataReductionProxyFallbackPAC
[] = "PROXY bar.com:80;";
33 class DataReductionProxySettingsAndroidTest
34 : public ConcreteDataReductionProxySettingsTest
<
35 DataReductionProxySettingsAndroid
> {
37 // DataReductionProxySettingsTest implementation:
38 virtual void SetUp() OVERRIDE
{
39 env_
= base::android::AttachCurrentThread();
40 DataReductionProxySettingsAndroid::Register(env_
);
41 DataReductionProxySettingsTestBase::SetUp();
44 void CheckProxyPacPref(const std::string
& expected_pac_url
,
45 const std::string
& expected_mode
) {
46 const base::DictionaryValue
* dict
=
47 pref_service_
.GetDictionary(prefs::kProxy
);
50 dict
->GetString("mode", &mode
);
51 ASSERT_EQ(expected_mode
, mode
);
52 dict
->GetString("pac_url", &pac_url
);
53 ASSERT_EQ(expected_pac_url
, pac_url
);
56 DataReductionProxySettingsAndroid
* Settings() {
57 return static_cast<DataReductionProxySettingsAndroid
*>(settings_
.get());
63 TEST_F(DataReductionProxySettingsAndroidTest
, TestGetDataReductionProxyOrigin
) {
64 AddProxyToCommandLine();
65 // SetUp() adds the origin to the command line, which should be returned here.
66 ScopedJavaLocalRef
<jstring
> result
=
67 Settings()->GetDataReductionProxyOrigin(env_
, NULL
);
68 ASSERT_TRUE(result
.obj());
69 const base::android::JavaRef
<jstring
>& str_ref
= result
;
70 EXPECT_EQ(kDataReductionProxyOrigin
, ConvertJavaStringToUTF8(str_ref
));
73 TEST_F(DataReductionProxySettingsAndroidTest
,
74 TestGetDataReductionProxyDevOrigin
) {
75 AddProxyToCommandLine();
76 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
77 switches::kSpdyProxyDevAuthOrigin
, kDataReductionProxyDevHost
);
78 ScopedJavaLocalRef
<jstring
> result
=
79 Settings()->GetDataReductionProxyOrigin(env_
, NULL
);
80 ASSERT_TRUE(result
.obj());
81 const base::android::JavaRef
<jstring
>& str_ref
= result
;
82 EXPECT_EQ(kDataReductionProxyDevHost
, ConvertJavaStringToUTF8(str_ref
));
85 // Confirm that the bypass rule functions generate the intended JavaScript
86 // code for the Proxy PAC.
87 TEST_F(DataReductionProxySettingsAndroidTest
, TestBypassPACRules
) {
88 Settings()->AddURLPatternToBypass("http://foo.com/*");
89 Settings()->AddHostPatternToBypass("bar.com");
91 EXPECT_EQ(Settings()->pac_bypass_rules_
.size(), 1u);
92 EXPECT_EQ("shExpMatch(url, 'http://foo.com/*')",
93 Settings()->pac_bypass_rules_
[0]);
95 EXPECT_EQ(Settings()->BypassRules().size(), 1u);
96 EXPECT_EQ("bar.com", Settings()->BypassRules()[0]);
99 TEST_F(DataReductionProxySettingsAndroidTest
, TestSetProxyPac
) {
100 AddProxyToCommandLine();
101 Settings()->AddDefaultProxyBypassRules();
103 // First check without restriction.
104 std::string raw_pac
= Settings()->GetProxyPacScript(false);
105 EXPECT_NE(raw_pac
.find(kDataReductionProxyOriginPAC
), std::string::npos
);
106 EXPECT_NE(raw_pac
.find(kDataReductionProxyFallbackPAC
), std::string::npos
);
108 base::Base64Encode(raw_pac
, &pac
);
109 std::string expected_pac_url
=
110 "data:application/x-ns-proxy-autoconfig;base64," + pac
;
111 Settings()->SetProxyConfigs(true, false, false);
112 CheckProxyPacPref(expected_pac_url
,
113 ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT
));
115 // Now check with restriction.
116 raw_pac
= Settings()->GetProxyPacScript(true);
117 // Primary proxy origin should not appear.
118 EXPECT_EQ(raw_pac
.find(kDataReductionProxyOriginPAC
), std::string::npos
);
119 EXPECT_NE(raw_pac
.find(kDataReductionProxyFallbackPAC
), std::string::npos
);
120 base::Base64Encode(raw_pac
, &pac
);
121 expected_pac_url
= "data:application/x-ns-proxy-autoconfig;base64," + pac
;
122 Settings()->SetProxyConfigs(true, true, false);
123 CheckProxyPacPref(expected_pac_url
,
124 ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT
));
126 Settings()->SetProxyConfigs(false, false, false);
127 CheckProxyPacPref(std::string(), ProxyModeToString(ProxyPrefs::MODE_SYSTEM
));
129 // Restriction is irrelevant when the proxy is disabled.
130 Settings()->SetProxyConfigs(false, false, false);
131 CheckProxyPacPref(std::string(), ProxyModeToString(ProxyPrefs::MODE_SYSTEM
));
134 TEST_F(DataReductionProxySettingsAndroidTest
, TestGetDailyContentLengths
) {
135 ScopedJavaLocalRef
<jlongArray
> result
= Settings()->GetDailyContentLengths(
136 env_
, prefs::kDailyHttpOriginalContentLength
);
137 ASSERT_TRUE(result
.obj());
139 jsize java_array_len
= env_
->GetArrayLength(result
.obj());
140 ASSERT_EQ(static_cast<jsize
>(spdyproxy::kNumDaysInHistory
), java_array_len
);
143 for (size_t i
= 0; i
< spdyproxy::kNumDaysInHistory
; ++i
) {
144 env_
->GetLongArrayRegion(result
.obj(), i
, 1, &value
);
146 static_cast<long>((spdyproxy::kNumDaysInHistory
- 1 - i
) * 2), value
);