1 // Copyright 2014 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 "components/data_reduction_proxy/browser/data_reduction_proxy_config_service.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/test/test_simple_task_runner.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
19 // Test system proxy rules.
20 static const char kSystemProxyRules[] = "http=http://system.com:80,direct://";
22 // Test data reduction proxy rules.
23 static const char kDataReductionProxyRules[] =
24 "http=https://foo.com:443,http://bar.com:80,direct://";
26 // Test data reduction proxy rules when in restricted mode.
27 static const char kDataReductionProxyRestrictedRules[] =
28 "http=http://bar.com:80,direct://";
32 namespace data_reduction_proxy {
34 class TestProxyConfigService : public net::ProxyConfigService {
36 TestProxyConfigService()
37 : availability_(net::ProxyConfigService::CONFIG_VALID) {
38 config_.proxy_rules().ParseFromString(kSystemProxyRules);
41 void SetProxyConfig(const net::ProxyConfig config,
42 ConfigAvailability availability) {
44 availability_ = availability;
45 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_,
46 OnProxyConfigChanged(config, availability));
49 virtual void AddObserver(
50 net::ProxyConfigService::Observer* observer) OVERRIDE {
51 observers_.AddObserver(observer);
54 virtual void RemoveObserver(
55 net::ProxyConfigService::Observer* observer) OVERRIDE {
56 observers_.RemoveObserver(observer);
59 virtual ConfigAvailability GetLatestProxyConfig(
60 net::ProxyConfig* config) OVERRIDE {
66 net::ProxyConfig config_;
67 ConfigAvailability availability_;
68 ObserverList<net::ProxyConfigService::Observer, true> observers_;
72 // A mock observer for capturing callbacks.
73 class MockObserver : public net::ProxyConfigService::Observer {
75 MOCK_METHOD2(OnProxyConfigChanged,
76 void(const net::ProxyConfig&,
77 net::ProxyConfigService::ConfigAvailability));
81 class DataReductionProxyConfigServiceTest : public testing::Test {
83 virtual void SetUp() {
84 observer_.reset(new MockObserver());
85 base_service_ = new TestProxyConfigService();
86 scoped_ptr<net::ProxyConfigService> base_service(base_service_);
87 config_service_.reset(
88 new DataReductionProxyConfigService(base_service.Pass()));
91 void EnableDataReductionProxy(bool data_reduction_proxy_enabled) {
92 config_service_->enabled_ = data_reduction_proxy_enabled;
93 config_service_->config_.proxy_rules().ParseFromString(
94 kDataReductionProxyRules);
97 scoped_ptr<net::ProxyConfigService::Observer> observer_;
99 // Holds a weak pointer to the base service. Ownership is passed to
100 // |config_service_|.
101 TestProxyConfigService* base_service_;
103 scoped_ptr<DataReductionProxyConfigService> config_service_;
106 // Compares proxy configurations, but allows different identifiers.
107 MATCHER_P(ProxyConfigMatches, config, "") {
108 net::ProxyConfig reference(config);
109 reference.set_id(arg.id());
110 return reference.Equals(arg);
113 TEST_F(DataReductionProxyConfigServiceTest, GetLatestProxyConfigEnabled) {
114 // Set up the |config_service_| as though Enable had been previously called
115 // and check that |GetLatestProxyConfigEnabled| return rules for the data
117 EnableDataReductionProxy(true);
118 net::ProxyConfig::ProxyRules expected_rules;
119 expected_rules.ParseFromString(kDataReductionProxyRules);
120 net::ProxyConfig latest_config;
121 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID,
122 config_service_->GetLatestProxyConfig(&latest_config));
123 ASSERT_TRUE(latest_config.proxy_rules().Equals(expected_rules));
126 TEST_F(DataReductionProxyConfigServiceTest, GetLatestProxyConfigDisabledValid) {
127 // Set up the |config_service_| with the data reduction proxy disabled and
128 // check that the underlying system config is returned.
129 EnableDataReductionProxy(false);
130 net::ProxyConfig::ProxyRules expected_rules;
131 expected_rules.ParseFromString(kSystemProxyRules);
132 net::ProxyConfig latest_config;
133 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID,
134 config_service_->GetLatestProxyConfig(&latest_config));
135 ASSERT_TRUE(latest_config.proxy_rules().Equals(expected_rules));
138 TEST_F(DataReductionProxyConfigServiceTest, GetLatestProxyConfigDisabledUnset) {
139 // Set up the |config_service_| with the data reduction proxy disabled and
140 // check that direct is returned if the the underlying system config is unset.
141 EnableDataReductionProxy(false);
142 base_service_->SetProxyConfig(net::ProxyConfig(),
143 net::ProxyConfigService::CONFIG_UNSET);
144 net::ProxyConfig latest_config;
145 EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID,
146 config_service_->GetLatestProxyConfig(&latest_config));
147 ASSERT_TRUE(latest_config.Equals(net::ProxyConfig()));
150 TEST_F(DataReductionProxyConfigServiceTest, UpdateProxyConfig) {
151 MockObserver observer;
152 base::MessageLoopForUI loop;
153 config_service_->AddObserver(&observer);
154 // Firing the observers in the delegate should trigger a notification.
155 net::ProxyConfig config2;
156 config2.set_auto_detect(true);
157 EXPECT_CALL(observer, OnProxyConfigChanged(
158 ProxyConfigMatches(config2),
159 net::ProxyConfigService::CONFIG_VALID)).Times(1);
160 base_service_->SetProxyConfig(config2, net::ProxyConfigService::CONFIG_VALID);
162 Mock::VerifyAndClearExpectations(&observer);
164 // Enable the data reduction proxy, which should trigger a notification.
165 net::ProxyConfig system_config;
166 system_config.proxy_rules().ParseFromString(kSystemProxyRules);
167 base_service_->SetProxyConfig(system_config,
168 net::ProxyConfigService::CONFIG_VALID);
169 net::ProxyConfig data_reduction_proxy_config;
170 data_reduction_proxy_config.proxy_rules().ParseFromString(
171 kDataReductionProxyRules);
173 EXPECT_CALL(observer, OnProxyConfigChanged(
174 ProxyConfigMatches(data_reduction_proxy_config),
175 net::ProxyConfigService::CONFIG_VALID)).Times(1);
176 config_service_->UpdateProxyConfig(true, data_reduction_proxy_config);
178 Mock::VerifyAndClearExpectations(&observer);
181 // Disable the data reduction proxy, which should trigger a notification.
182 base_service_->SetProxyConfig(system_config,
183 net::ProxyConfigService::CONFIG_VALID);
184 EXPECT_CALL(observer, OnProxyConfigChanged(
185 ProxyConfigMatches(system_config),
186 net::ProxyConfigService::CONFIG_VALID)).Times(1);
187 config_service_->UpdateProxyConfig(false, data_reduction_proxy_config);
189 Mock::VerifyAndClearExpectations(&observer);
191 config_service_->RemoveObserver(&observer);
194 TEST_F(DataReductionProxyConfigServiceTest, TrackerEnable) {
195 MockObserver observer;
196 //base::MessageLoopForUI loop;
197 config_service_->AddObserver(&observer);
198 scoped_refptr<base::TestSimpleTaskRunner> task_runner_(
199 new base::TestSimpleTaskRunner());
200 DataReductionProxyConfigTracker tracker(config_service_.get(),
202 net::ProxyConfig expected_config;
203 expected_config.proxy_rules().ParseFromString(kDataReductionProxyRules);
204 EXPECT_CALL(observer, OnProxyConfigChanged(
205 ProxyConfigMatches(expected_config),
206 net::ProxyConfigService::CONFIG_VALID)).Times(1);
207 tracker.Enable(false,
209 "https://foo.com:443",
210 "http://bar.com:80");
211 task_runner_->RunUntilIdle();
212 Mock::VerifyAndClearExpectations(&observer);
214 config_service_->RemoveObserver(&observer);
217 TEST_F(DataReductionProxyConfigServiceTest, TrackerEnableRestricted) {
218 MockObserver observer;
219 //base::MessageLoopForUI loop;
220 config_service_->AddObserver(&observer);
221 scoped_refptr<base::TestSimpleTaskRunner> task_runner_(
222 new base::TestSimpleTaskRunner());
223 DataReductionProxyConfigTracker tracker(config_service_.get(),
225 net::ProxyConfig expected_config;
226 expected_config.proxy_rules().ParseFromString(
227 kDataReductionProxyRestrictedRules);
228 EXPECT_CALL(observer, OnProxyConfigChanged(
229 ProxyConfigMatches(expected_config),
230 net::ProxyConfigService::CONFIG_VALID)).Times(1);
233 "https://foo.com:443",
234 "http://bar.com:80");
235 task_runner_->RunUntilIdle();
236 Mock::VerifyAndClearExpectations(&observer);
238 config_service_->RemoveObserver(&observer);
241 TEST_F(DataReductionProxyConfigServiceTest, TrackerDisable) {
242 MockObserver observer;
243 //base::MessageLoopForUI loop;
244 config_service_->AddObserver(&observer);
245 scoped_refptr<base::TestSimpleTaskRunner> task_runner_(
246 new base::TestSimpleTaskRunner());
247 DataReductionProxyConfigTracker tracker(config_service_.get(),
249 net::ProxyConfig expected_config;
250 expected_config.proxy_rules().ParseFromString(kSystemProxyRules);
251 EXPECT_CALL(observer, OnProxyConfigChanged(
252 ProxyConfigMatches(expected_config),
253 net::ProxyConfigService::CONFIG_VALID)).Times(1);
255 task_runner_->RunUntilIdle();
256 //loop.RunUntilIdle();
257 Mock::VerifyAndClearExpectations(&observer);
259 config_service_->RemoveObserver(&observer);
263 TEST_F(DataReductionProxyConfigServiceTest, TrackerBypassList) {
264 base::MessageLoopForUI loop;
265 scoped_refptr<base::TestSimpleTaskRunner> task_runner_(
266 new base::TestSimpleTaskRunner());
267 DataReductionProxyConfigTracker tracker(config_service_.get(),
269 tracker.AddHostPatternToBypass("http://www.google.com");
270 tracker.AddHostPatternToBypass("fefe:13::abc/33");
271 tracker.AddURLPatternToBypass("foo.org/images/*");
272 tracker.AddURLPatternToBypass("http://foo.com/*");
273 tracker.AddURLPatternToBypass("http://baz.com:22/bar/*");
274 tracker.AddURLPatternToBypass("http://*bat.com/bar/*");
276 std::string expected[] = {
277 "http://www.google.com",
285 ASSERT_EQ(tracker.bypass_rules_.size(), 6u);
287 for (std::vector<std::string>::iterator it = tracker.bypass_rules_.begin();
288 it != tracker.bypass_rules_.end(); ++it) {
289 EXPECT_EQ(expected[i++], *it);
293 } // namespace data_reduction_proxy