1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/net/pref_proxy_config_service.h"
#include "base/command_line.h"
#include "base/file_path.h"
#include "chrome/browser/net/chrome_url_request_context.h"
#include "chrome/browser/prefs/pref_service_mock_builder.h"
#include "chrome/browser/prefs/proxy_prefs.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/testing_pref_service.h"
#include "net/proxy/proxy_config_service_common_unittest.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::Mock;
namespace {
const char kFixedPacUrl[] = "http://chromium.org/fixed_pac_url";
// Testing proxy config service that allows us to fire notifications at will.
class TestProxyConfigService : public net::ProxyConfigService {
public:
explicit TestProxyConfigService(const net::ProxyConfig& config)
: config_(config) {
}
void SetProxyConfig(const net::ProxyConfig config) {
config_ = config;
FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_,
OnProxyConfigChanged(config_));
}
private:
virtual void AddObserver(net::ProxyConfigService::Observer* observer) {
observers_.AddObserver(observer);
}
virtual void RemoveObserver(net::ProxyConfigService::Observer* observer) {
observers_.RemoveObserver(observer);
}
virtual bool GetLatestProxyConfig(net::ProxyConfig* config) {
*config = config_;
return true;
}
net::ProxyConfig config_;
ObserverList<net::ProxyConfigService::Observer, true> observers_;
};
// A mock observer for capturing callbacks.
class MockObserver : public net::ProxyConfigService::Observer {
public:
MOCK_METHOD1(OnProxyConfigChanged, void(const net::ProxyConfig&));
};
template<typename TESTBASE>
class PrefProxyConfigServiceTestBase : public TESTBASE {
protected:
PrefProxyConfigServiceTestBase()
: ui_thread_(BrowserThread::UI, &loop_),
io_thread_(BrowserThread::IO, &loop_) {}
virtual void Init(PrefService* pref_service) {
ASSERT_TRUE(pref_service);
PrefProxyConfigService::RegisterUserPrefs(pref_service);
fixed_config_.set_pac_url(GURL(kFixedPacUrl));
delegate_service_ = new TestProxyConfigService(fixed_config_);
proxy_config_tracker_ = new PrefProxyConfigTracker(pref_service);
proxy_config_service_.reset(
new PrefProxyConfigService(proxy_config_tracker_.get(),
delegate_service_));
}
virtual void TearDown() {
proxy_config_tracker_->DetachFromPrefService();
loop_.RunAllPending();
proxy_config_service_.reset();
}
MessageLoop loop_;
TestProxyConfigService* delegate_service_; // weak
scoped_ptr<PrefProxyConfigService> proxy_config_service_;
net::ProxyConfig fixed_config_;
private:
scoped_refptr<PrefProxyConfigTracker> proxy_config_tracker_;
BrowserThread ui_thread_;
BrowserThread io_thread_;
};
class PrefProxyConfigServiceTest
: public PrefProxyConfigServiceTestBase<testing::Test> {
protected:
virtual void SetUp() {
pref_service_.reset(new TestingPrefService());
Init(pref_service_.get());
}
scoped_ptr<TestingPrefService> pref_service_;
};
TEST_F(PrefProxyConfigServiceTest, BaseConfiguration) {
net::ProxyConfig actual_config;
proxy_config_service_->GetLatestProxyConfig(&actual_config);
EXPECT_EQ(GURL(kFixedPacUrl), actual_config.pac_url());
}
TEST_F(PrefProxyConfigServiceTest, DynamicPrefOverrides) {
pref_service_->SetManagedPref(
prefs::kProxyServer, Value::CreateStringValue("http://example.com:3128"));
pref_service_->SetManagedPref(
prefs::kProxyMode,
Value::CreateIntegerValue(ProxyPrefs::MODE_FIXED_SERVERS));
loop_.RunAllPending();
net::ProxyConfig actual_config;
proxy_config_service_->GetLatestProxyConfig(&actual_config);
EXPECT_FALSE(actual_config.auto_detect());
EXPECT_EQ(net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY,
actual_config.proxy_rules().type);
EXPECT_EQ(actual_config.proxy_rules().single_proxy,
net::ProxyServer::FromURI("http://example.com:3128",
net::ProxyServer::SCHEME_HTTP));
pref_service_->SetManagedPref(
prefs::kProxyMode,
Value::CreateIntegerValue(ProxyPrefs::MODE_AUTO_DETECT));
loop_.RunAllPending();
proxy_config_service_->GetLatestProxyConfig(&actual_config);
EXPECT_TRUE(actual_config.auto_detect());
}
// Compares proxy configurations, but allows different identifiers.
MATCHER_P(ProxyConfigMatches, config, "") {
net::ProxyConfig reference(config);
reference.set_id(arg.id());
return reference.Equals(arg);
}
TEST_F(PrefProxyConfigServiceTest, Observers) {
MockObserver observer;
proxy_config_service_->AddObserver(&observer);
// Firing the observers in the delegate should trigger a notification.
net::ProxyConfig config2;
config2.set_auto_detect(true);
EXPECT_CALL(observer,
OnProxyConfigChanged(ProxyConfigMatches(config2))).Times(1);
delegate_service_->SetProxyConfig(config2);
loop_.RunAllPending();
Mock::VerifyAndClearExpectations(&observer);
// Override configuration, this should trigger a notification.
net::ProxyConfig pref_config;
pref_config.set_pac_url(GURL(kFixedPacUrl));
EXPECT_CALL(observer,
OnProxyConfigChanged(ProxyConfigMatches(pref_config))).Times(1);
pref_service_->SetManagedPref(prefs::kProxyPacUrl,
Value::CreateStringValue(kFixedPacUrl));
// The above does not trigger a notification, because PrefProxyConfig still
// sees the mode as the default (ProxyPrefs::SYSTEM), so that it doesn't claim
// to have proxy config.
// TODO(battre): Remove this comment when http://crbug.com/65732 is
// resolved.
pref_service_->SetManagedPref(
prefs::kProxyMode,
Value::CreateIntegerValue(ProxyPrefs::MODE_PAC_SCRIPT));
loop_.RunAllPending();
Mock::VerifyAndClearExpectations(&observer);
// Since there are pref overrides, delegate changes should be ignored.
net::ProxyConfig config3;
config3.proxy_rules().ParseFromString("http=config3:80");
EXPECT_CALL(observer, OnProxyConfigChanged(_)).Times(0);
fixed_config_.set_auto_detect(true);
delegate_service_->SetProxyConfig(config3);
loop_.RunAllPending();
Mock::VerifyAndClearExpectations(&observer);
// Clear the override should switch back to the fixed configuration.
EXPECT_CALL(observer,
OnProxyConfigChanged(ProxyConfigMatches(config3))).Times(1);
pref_service_->RemoveManagedPref(prefs::kProxyMode);
// The above switches the mode to the default (ProxyPrefs::SYSTEM), so the
// next removal won't bother PrefProxyConfigService.
// TODO(battre): Remove this comment when http://crbug.com/65732 is
// completed.
pref_service_->RemoveManagedPref(prefs::kProxyPacUrl);
loop_.RunAllPending();
Mock::VerifyAndClearExpectations(&observer);
// Delegate service notifications should show up again.
net::ProxyConfig config4;
config4.proxy_rules().ParseFromString("socks:config4");
EXPECT_CALL(observer,
OnProxyConfigChanged(ProxyConfigMatches(config4))).Times(1);
delegate_service_->SetProxyConfig(config4);
loop_.RunAllPending();
Mock::VerifyAndClearExpectations(&observer);
proxy_config_service_->RemoveObserver(&observer);
}
// Test parameter object for testing command line proxy configuration.
struct CommandLineTestParams {
// Explicit assignment operator, so testing::TestWithParam works with MSVC.
CommandLineTestParams& operator=(const CommandLineTestParams& other) {
description = other.description;
for (unsigned int i = 0; i < arraysize(switches); i++)
switches[i] = other.switches[i];
is_null = other.is_null;
auto_detect = other.auto_detect;
pac_url = other.pac_url;
proxy_rules = other.proxy_rules;
return *this;
}
// Short description to identify the test.
const char* description;
// The command line to build a ProxyConfig from.
struct SwitchValue {
const char* name;
const char* value;
} switches[2];
// Expected outputs (fields of the ProxyConfig).
bool is_null;
bool auto_detect;
GURL pac_url;
net::ProxyRulesExpectation proxy_rules;
};
void PrintTo(const CommandLineTestParams& params, std::ostream* os) {
*os << params.description;
}
class PrefProxyConfigServiceCommandLineTest
: public PrefProxyConfigServiceTestBase<
testing::TestWithParam<CommandLineTestParams> > {
protected:
PrefProxyConfigServiceCommandLineTest()
: command_line_(CommandLine::NO_PROGRAM) {}
virtual void SetUp() {
for (size_t i = 0; i < arraysize(GetParam().switches); i++) {
const char* name = GetParam().switches[i].name;
const char* value = GetParam().switches[i].value;
if (name && value)
command_line_.AppendSwitchASCII(name, value);
else if (name)
command_line_.AppendSwitch(name);
}
pref_service_.reset(
PrefServiceMockBuilder().WithCommandLine(&command_line_).Create());
Init(pref_service_.get());
}
private:
CommandLine command_line_;
scoped_ptr<PrefService> pref_service_;
};
TEST_P(PrefProxyConfigServiceCommandLineTest, CommandLine) {
net::ProxyConfig config;
proxy_config_service_->GetLatestProxyConfig(&config);
if (GetParam().is_null) {
EXPECT_EQ(GURL(kFixedPacUrl), config.pac_url());
} else {
EXPECT_NE(GURL(kFixedPacUrl), config.pac_url());
EXPECT_EQ(GetParam().auto_detect, config.auto_detect());
EXPECT_EQ(GetParam().pac_url, config.pac_url());
EXPECT_TRUE(GetParam().proxy_rules.Matches(config.proxy_rules()));
}
}
static const CommandLineTestParams kCommandLineTestParams[] = {
{
"Empty command line",
// Input
{ },
// Expected result
true, // is_null
false, // auto_detect
GURL(), // pac_url
net::ProxyRulesExpectation::Empty(),
},
{
"No proxy",
// Input
{
{ switches::kNoProxyServer, NULL },
},
// Expected result
false, // is_null
false, // auto_detect
GURL(), // pac_url
net::ProxyRulesExpectation::Empty(),
},
{
"No proxy with extra parameters.",
// Input
{
{ switches::kNoProxyServer, NULL },
{ switches::kProxyServer, "http://proxy:8888" },
},
// Expected result
false, // is_null
false, // auto_detect
GURL(), // pac_url
net::ProxyRulesExpectation::Empty(),
},
{
"Single proxy.",
// Input
{
{ switches::kProxyServer, "http://proxy:8888" },
},
// Expected result
false, // is_null
false, // auto_detect
GURL(), // pac_url
net::ProxyRulesExpectation::Single(
"proxy:8888", // single proxy
""), // bypass rules
},
{
"Per scheme proxy.",
// Input
{
{ switches::kProxyServer, "http=httpproxy:8888;ftp=ftpproxy:8889" },
},
// Expected result
false, // is_null
false, // auto_detect
GURL(), // pac_url
net::ProxyRulesExpectation::PerScheme(
"httpproxy:8888", // http
"", // https
"ftpproxy:8889", // ftp
""), // bypass rules
},
{
"Per scheme proxy with bypass URLs.",
// Input
{
{ switches::kProxyServer, "http=httpproxy:8888;ftp=ftpproxy:8889" },
{ switches::kProxyBypassList,
".google.com, foo.com:99, 1.2.3.4:22, 127.0.0.1/8" },
},
// Expected result
false, // is_null
false, // auto_detect
GURL(), // pac_url
net::ProxyRulesExpectation::PerScheme(
"httpproxy:8888", // http
"", // https
"ftpproxy:8889", // ftp
"*.google.com,foo.com:99,1.2.3.4:22,127.0.0.1/8"),
},
{
"Pac URL",
// Input
{
{ switches::kProxyPacUrl, "http://wpad/wpad.dat" },
},
// Expected result
false, // is_null
false, // auto_detect
GURL("http://wpad/wpad.dat"), // pac_url
net::ProxyRulesExpectation::Empty(),
},
{
"Autodetect",
// Input
{
{ switches::kProxyAutoDetect, NULL },
},
// Expected result
false, // is_null
true, // auto_detect
GURL(), // pac_url
net::ProxyRulesExpectation::Empty(),
},
};
INSTANTIATE_TEST_CASE_P(
PrefProxyConfigServiceCommandLineTestInstance,
PrefProxyConfigServiceCommandLineTest,
testing::ValuesIn(kCommandLineTestParams));
} // namespace
|