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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
|
// Copyright (c) 2012 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/chromeos/proxy_config_service_impl.h"
#include <vector>
#include "base/format_macros.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/prefs/testing_pref_service.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/chromeos/settings/cros_settings.h"
#include "chrome/browser/chromeos/settings/device_settings_service.h"
#include "chrome/browser/chromeos/ui_proxy_config.h"
#include "chrome/common/pref_names.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/shill_profile_client.h"
#include "chromeos/dbus/shill_service_client.h"
#include "chromeos/network/network_handler.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h"
#include "content/public/test/test_browser_thread.h"
#include "net/proxy/proxy_config_service_common_unittest.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
using content::BrowserThread;
namespace chromeos {
namespace {
struct Input {
UIProxyConfig::Mode mode;
std::string pac_url;
std::string server;
std::string bypass_rules;
};
// Builds an identifier for each test in an array.
#define TEST_DESC(desc) base::StringPrintf("at line %d <%s>", __LINE__, desc)
// Shortcuts to declare enums within chromeos's ProxyConfig.
#define MK_MODE(mode) UIProxyConfig::MODE_##mode
// Inspired from net/proxy/proxy_config_service_linux_unittest.cc.
const struct TestParams {
// Short description to identify the test
std::string description;
Input input;
// Expected outputs from fields of net::ProxyConfig (via IO).
bool auto_detect;
GURL pac_url;
net::ProxyRulesExpectation proxy_rules;
} tests[] = {
{ // 0
TEST_DESC("No proxying"),
{ // Input.
MK_MODE(DIRECT), // mode
},
// Expected result.
false, // auto_detect
GURL(), // pac_url
net::ProxyRulesExpectation::Empty(), // proxy_rules
},
{ // 1
TEST_DESC("Auto detect"),
{ // Input.
MK_MODE(AUTO_DETECT), // mode
},
// Expected result.
true, // auto_detect
GURL(), // pac_url
net::ProxyRulesExpectation::Empty(), // proxy_rules
},
{ // 2
TEST_DESC("Valid PAC URL"),
{ // Input.
MK_MODE(PAC_SCRIPT), // mode
"http://wpad/wpad.dat", // pac_url
},
// Expected result.
false, // auto_detect
GURL("http://wpad/wpad.dat"), // pac_url
net::ProxyRulesExpectation::Empty(), // proxy_rules
},
{ // 3
TEST_DESC("Invalid PAC URL"),
{ // Input.
MK_MODE(PAC_SCRIPT), // mode
"wpad.dat", // pac_url
},
// Expected result.
false, // auto_detect
GURL(), // pac_url
net::ProxyRulesExpectation::Empty(), // proxy_rules
},
{ // 4
TEST_DESC("Single-host in proxy list"),
{ // Input.
MK_MODE(SINGLE_PROXY), // mode
"", // pac_url
"www.google.com", // server
},
// Expected result.
false, // auto_detect
GURL(), // pac_url
net::ProxyRulesExpectation::Single( // proxy_rules
"www.google.com:80", // single proxy
"<local>"), // bypass rules
},
{ // 5
TEST_DESC("Single-host, different port"),
{ // Input.
MK_MODE(SINGLE_PROXY), // mode
"", // pac_url
"www.google.com:99", // server
},
// Expected result.
false, // auto_detect
GURL(), // pac_url
net::ProxyRulesExpectation::Single( // proxy_rules
"www.google.com:99", // single
"<local>"), // bypass rules
},
{ // 6
TEST_DESC("Tolerate a scheme"),
{ // Input.
MK_MODE(SINGLE_PROXY), // mode
"", // pac_url
"http://www.google.com:99", // server
},
// Expected result.
false, // auto_detect
GURL(), // pac_url
net::ProxyRulesExpectation::Single( // proxy_rules
"www.google.com:99", // single proxy
"<local>"), // bypass rules
},
{ // 7
TEST_DESC("Per-scheme proxy rules"),
{ // Input.
MK_MODE(PROXY_PER_SCHEME), // mode
"", // pac_url
"http=www.google.com:80;https=https://www.foo.com:110;"
"ftp=ftp.foo.com:121;socks=socks5://socks.com:888", // server
},
// Expected result.
false, // auto_detect
GURL(), // pac_url
net::ProxyRulesExpectation::PerSchemeWithSocks( // proxy_rules
"www.google.com:80", // http
"https://www.foo.com:110", // https
"ftp.foo.com:121", // ftp
"socks5://socks.com:888", // fallback proxy
"<local>"), // bypass rules
},
{ // 8
TEST_DESC("Bypass rules"),
{ // Input.
MK_MODE(SINGLE_PROXY), // mode
"", // pac_url
"www.google.com", // server
"*.google.com, *foo.com:99, 1.2.3.4:22, 127.0.0.1/8", // bypass_rules
},
// Expected result.
false, // auto_detect
GURL(), // pac_url
net::ProxyRulesExpectation::Single( // proxy_rules
"www.google.com:80", // single proxy
// bypass_rules
"*.google.com,*foo.com:99,1.2.3.4:22,127.0.0.1/8,<local>"),
},
}; // tests
const char kUserProfilePath[] = "user_profile";
} // namespace
class ProxyConfigServiceImplTest : public testing::Test {
protected:
ProxyConfigServiceImplTest()
: ui_thread_(BrowserThread::UI, &loop_),
io_thread_(BrowserThread::IO, &loop_) {}
virtual void SetUp() {
DBusThreadManager::Initialize();
NetworkHandler::Initialize();
SetUpNetwork();
PrefProxyConfigTrackerImpl::RegisterPrefs(pref_service_.registry());
// Create a ProxyConfigServiceImpl like for the system request context.
config_service_impl_.reset(
new ProxyConfigServiceImpl(NULL, // no profile prefs
&pref_service_));
proxy_config_service_ =
config_service_impl_->CreateTrackingProxyConfigService(
scoped_ptr<net::ProxyConfigService>());
// CreateTrackingProxyConfigService triggers update of initial prefs proxy
// config by tracker to chrome proxy config service, so flush all pending
// tasks so that tests start fresh.
loop_.RunUntilIdle();
}
void SetUpNetwork() {
ShillProfileClient::TestInterface* profile_test =
DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface();
ShillServiceClient::TestInterface* service_test =
DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
// Process any pending notifications before clearing services.
loop_.RunUntilIdle();
service_test->ClearServices();
// Sends a notification about the added profile.
profile_test->AddProfile(kUserProfilePath, "user_hash");
service_test->AddService("/service/stub_wifi2",
"stub_wifi2" /* guid */,
"wifi2_PSK",
shill::kTypeWifi, shill::kStateOnline,
true /* visible */);
profile_test->AddService(kUserProfilePath, "/service/stub_wifi2");
loop_.RunUntilIdle();
}
virtual void TearDown() {
config_service_impl_->DetachFromPrefService();
loop_.RunUntilIdle();
config_service_impl_.reset();
proxy_config_service_.reset();
NetworkHandler::Shutdown();
DBusThreadManager::Shutdown();
}
void InitConfigWithTestInput(const Input& input,
base::DictionaryValue* result) {
base::DictionaryValue* new_config = NULL;
switch (input.mode) {
case MK_MODE(DIRECT):
new_config = ProxyConfigDictionary::CreateDirect();
break;
case MK_MODE(AUTO_DETECT):
new_config = ProxyConfigDictionary::CreateAutoDetect();
break;
case MK_MODE(PAC_SCRIPT):
new_config =
ProxyConfigDictionary::CreatePacScript(input.pac_url, false);
break;
case MK_MODE(SINGLE_PROXY):
case MK_MODE(PROXY_PER_SCHEME):
new_config =
ProxyConfigDictionary::CreateFixedServers(input.server,
input.bypass_rules);
break;
}
result->Swap(new_config);
delete new_config;
}
void SetConfig(base::DictionaryValue* pref_proxy_config_dict) {
std::string proxy_config;
if (pref_proxy_config_dict)
base::JSONWriter::Write(pref_proxy_config_dict, &proxy_config);
NetworkStateHandler* network_state_handler =
NetworkHandler::Get()->network_state_handler();
const NetworkState* network = network_state_handler->DefaultNetwork();
ASSERT_TRUE(network);
DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface()->
SetServiceProperty(network->path(),
shill::kProxyConfigProperty,
base::StringValue(proxy_config));
}
// Synchronously gets the latest proxy config.
void SyncGetLatestProxyConfig(net::ProxyConfig* config) {
*config = net::ProxyConfig();
// Let message loop process all messages. This will run
// ChromeProxyConfigService::UpdateProxyConfig, which is posted on IO from
// PrefProxyConfigTrackerImpl::OnProxyConfigChanged.
loop_.RunUntilIdle();
net::ProxyConfigService::ConfigAvailability availability =
proxy_config_service_->GetLatestProxyConfig(config);
EXPECT_EQ(net::ProxyConfigService::CONFIG_VALID, availability);
}
base::MessageLoop loop_;
scoped_ptr<net::ProxyConfigService> proxy_config_service_;
scoped_ptr<ProxyConfigServiceImpl> config_service_impl_;
TestingPrefServiceSimple pref_service_;
private:
ScopedTestDeviceSettingsService test_device_settings_service_;
ScopedTestCrosSettings test_cros_settings_;
content::TestBrowserThread ui_thread_;
content::TestBrowserThread io_thread_;
};
TEST_F(ProxyConfigServiceImplTest, NetworkProxy) {
for (size_t i = 0; i < arraysize(tests); ++i) {
SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "] %s", i,
tests[i].description.c_str()));
base::DictionaryValue test_config;
InitConfigWithTestInput(tests[i].input, &test_config);
SetConfig(&test_config);
net::ProxyConfig config;
SyncGetLatestProxyConfig(&config);
EXPECT_EQ(tests[i].auto_detect, config.auto_detect());
EXPECT_EQ(tests[i].pac_url, config.pac_url());
EXPECT_TRUE(tests[i].proxy_rules.Matches(config.proxy_rules()));
}
}
TEST_F(ProxyConfigServiceImplTest, DynamicPrefsOverride) {
// Groupings of 3 test inputs to use for managed, recommended and network
// proxies respectively. Only valid and non-direct test inputs are used.
const size_t proxies[][3] = {
{ 1, 2, 4, },
{ 1, 4, 2, },
{ 4, 2, 1, },
{ 2, 1, 4, },
{ 2, 4, 5, },
{ 2, 5, 4, },
{ 5, 4, 2, },
{ 4, 2, 5, },
{ 4, 5, 6, },
{ 4, 6, 5, },
{ 6, 5, 4, },
{ 5, 4, 6, },
{ 5, 6, 7, },
{ 5, 7, 6, },
{ 7, 6, 5, },
{ 6, 5, 7, },
{ 6, 7, 8, },
{ 6, 8, 7, },
{ 8, 7, 6, },
{ 7, 6, 8, },
};
for (size_t i = 0; i < arraysize(proxies); ++i) {
const TestParams& managed_params = tests[proxies[i][0]];
const TestParams& recommended_params = tests[proxies[i][1]];
const TestParams& network_params = tests[proxies[i][2]];
SCOPED_TRACE(base::StringPrintf(
"Test[%" PRIuS "] managed=[%s], recommended=[%s], network=[%s]", i,
managed_params.description.c_str(),
recommended_params.description.c_str(),
network_params.description.c_str()));
base::DictionaryValue managed_config;
InitConfigWithTestInput(managed_params.input, &managed_config);
base::DictionaryValue recommended_config;
InitConfigWithTestInput(recommended_params.input, &recommended_config);
base::DictionaryValue network_config;
InitConfigWithTestInput(network_params.input, &network_config);
// Managed proxy pref should take effect over recommended proxy and
// non-existent network proxy.
SetConfig(NULL);
pref_service_.SetManagedPref(prefs::kProxy, managed_config.DeepCopy());
pref_service_.SetRecommendedPref(prefs::kProxy,
recommended_config.DeepCopy());
net::ProxyConfig actual_config;
SyncGetLatestProxyConfig(&actual_config);
EXPECT_EQ(managed_params.auto_detect, actual_config.auto_detect());
EXPECT_EQ(managed_params.pac_url, actual_config.pac_url());
EXPECT_TRUE(managed_params.proxy_rules.Matches(
actual_config.proxy_rules()));
// Recommended proxy pref should take effect when managed proxy pref is
// removed.
pref_service_.RemoveManagedPref(prefs::kProxy);
SyncGetLatestProxyConfig(&actual_config);
EXPECT_EQ(recommended_params.auto_detect, actual_config.auto_detect());
EXPECT_EQ(recommended_params.pac_url, actual_config.pac_url());
EXPECT_TRUE(recommended_params.proxy_rules.Matches(
actual_config.proxy_rules()));
// Network proxy should take take effect over recommended proxy pref.
SetConfig(&network_config);
SyncGetLatestProxyConfig(&actual_config);
EXPECT_EQ(network_params.auto_detect, actual_config.auto_detect());
EXPECT_EQ(network_params.pac_url, actual_config.pac_url());
EXPECT_TRUE(network_params.proxy_rules.Matches(
actual_config.proxy_rules()));
// Managed proxy pref should take effect over network proxy.
pref_service_.SetManagedPref(prefs::kProxy, managed_config.DeepCopy());
SyncGetLatestProxyConfig(&actual_config);
EXPECT_EQ(managed_params.auto_detect, actual_config.auto_detect());
EXPECT_EQ(managed_params.pac_url, actual_config.pac_url());
EXPECT_TRUE(managed_params.proxy_rules.Matches(
actual_config.proxy_rules()));
// Network proxy should take effect over recommended proxy pref when managed
// proxy pref is removed.
pref_service_.RemoveManagedPref(prefs::kProxy);
SyncGetLatestProxyConfig(&actual_config);
EXPECT_EQ(network_params.auto_detect, actual_config.auto_detect());
EXPECT_EQ(network_params.pac_url, actual_config.pac_url());
EXPECT_TRUE(network_params.proxy_rules.Matches(
actual_config.proxy_rules()));
// Removing recommended proxy pref should have no effect on network proxy.
pref_service_.RemoveRecommendedPref(prefs::kProxy);
SyncGetLatestProxyConfig(&actual_config);
EXPECT_EQ(network_params.auto_detect, actual_config.auto_detect());
EXPECT_EQ(network_params.pac_url, actual_config.pac_url());
EXPECT_TRUE(network_params.proxy_rules.Matches(
actual_config.proxy_rules()));
}
}
} // namespace chromeos
|