| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chromeos/network/network_connection_handler.h" | 5 #include "chromeos/network/network_connection_handler.h" |
| 6 | 6 |
| 7 #include <map> |
| 8 #include <set> |
| 9 |
| 7 #include "base/bind.h" | 10 #include "base/bind.h" |
| 8 #include "base/callback.h" | 11 #include "base/callback.h" |
| 9 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 10 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
| 11 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 14 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 15 #include "chromeos/cert_loader.h" | 18 #include "chromeos/cert_loader.h" |
| 16 #include "chromeos/dbus/dbus_thread_manager.h" | 19 #include "chromeos/dbus/dbus_thread_manager.h" |
| 17 #include "chromeos/dbus/shill_device_client.h" | 20 #include "chromeos/dbus/shill_device_client.h" |
| 18 #include "chromeos/dbus/shill_manager_client.h" | 21 #include "chromeos/dbus/shill_manager_client.h" |
| 19 #include "chromeos/dbus/shill_profile_client.h" | 22 #include "chromeos/dbus/shill_profile_client.h" |
| 20 #include "chromeos/dbus/shill_service_client.h" | 23 #include "chromeos/dbus/shill_service_client.h" |
| 21 #include "chromeos/network/managed_network_configuration_handler_impl.h" | 24 #include "chromeos/network/managed_network_configuration_handler_impl.h" |
| 22 #include "chromeos/network/network_configuration_handler.h" | 25 #include "chromeos/network/network_configuration_handler.h" |
| 26 #include "chromeos/network/network_connection_observer.h" |
| 23 #include "chromeos/network/network_profile_handler.h" | 27 #include "chromeos/network/network_profile_handler.h" |
| 24 #include "chromeos/network/network_state_handler.h" | 28 #include "chromeos/network/network_state_handler.h" |
| 25 #include "chromeos/network/onc/onc_utils.h" | 29 #include "chromeos/network/onc/onc_utils.h" |
| 26 #include "components/onc/onc_constants.h" | 30 #include "components/onc/onc_constants.h" |
| 27 #include "crypto/nss_util_internal.h" | 31 #include "crypto/nss_util_internal.h" |
| 28 #include "crypto/scoped_test_nss_chromeos_user.h" | 32 #include "crypto/scoped_test_nss_chromeos_user.h" |
| 29 #include "net/base/net_errors.h" | 33 #include "net/base/net_errors.h" |
| 30 #include "net/base/test_data_directory.h" | 34 #include "net/base/test_data_directory.h" |
| 31 #include "net/cert/nss_cert_database_chromeos.h" | 35 #include "net/cert/nss_cert_database_chromeos.h" |
| 32 #include "net/cert/x509_certificate.h" | 36 #include "net/cert/x509_certificate.h" |
| 33 #include "net/test/cert_test_util.h" | 37 #include "net/test/cert_test_util.h" |
| 34 #include "testing/gtest/include/gtest/gtest.h" | 38 #include "testing/gtest/include/gtest/gtest.h" |
| 35 #include "third_party/cros_system_api/dbus/service_constants.h" | 39 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 36 | 40 |
| 37 // http://crbug.com/418369 | 41 // http://crbug.com/418369 |
| 38 #ifdef NDEBUG | 42 #ifdef NDEBUG |
| 39 | 43 |
| 44 namespace chromeos { |
| 45 |
| 40 namespace { | 46 namespace { |
| 41 | 47 |
| 42 const char* kSuccessResult = "success"; | 48 const char* kSuccessResult = "success"; |
| 43 | 49 |
| 44 void ConfigureCallback(const dbus::ObjectPath& result) { | 50 void ConfigureCallback(const dbus::ObjectPath& result) { |
| 45 } | 51 } |
| 46 | 52 |
| 47 void ConfigureErrorCallback(const std::string& error_name, | 53 void ConfigureErrorCallback(const std::string& error_name, |
| 48 const std::string& error_message) { | 54 const std::string& error_message) { |
| 49 } | 55 } |
| 50 | 56 |
| 57 class TestNetworkConnectionObserver : public NetworkConnectionObserver { |
| 58 public: |
| 59 TestNetworkConnectionObserver() {} |
| 60 ~TestNetworkConnectionObserver() override {} |
| 61 |
| 62 // NetworkConnectionObserver |
| 63 void ConnectToNetworkRequested(const std::string& service_path) override { |
| 64 requests_.insert(service_path); |
| 65 } |
| 66 |
| 67 void ConnectSucceeded(const std::string& service_path) override { |
| 68 results_[service_path] = kSuccessResult; |
| 69 } |
| 70 |
| 71 void ConnectFailed(const std::string& service_path, |
| 72 const std::string& error_name) override { |
| 73 results_[service_path] = error_name; |
| 74 } |
| 75 |
| 76 void DiconnectRequested(const std::string& service_path) override { |
| 77 requests_.insert(service_path); |
| 78 } |
| 79 |
| 80 bool GetRequested(const std::string& service_path) { |
| 81 return requests_.count(service_path) != 0; |
| 82 } |
| 83 |
| 84 std::string GetResult(const std::string& service_path) { |
| 85 auto iter = results_.find(service_path); |
| 86 if (iter == results_.end()) |
| 87 return ""; |
| 88 return iter->second; |
| 89 } |
| 90 |
| 91 private: |
| 92 std::set<std::string> requests_; |
| 93 std::map<std::string, std::string> results_; |
| 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(TestNetworkConnectionObserver); |
| 96 }; |
| 97 |
| 51 } // namespace | 98 } // namespace |
| 52 | 99 |
| 53 namespace chromeos { | |
| 54 | |
| 55 class NetworkConnectionHandlerTest : public testing::Test { | 100 class NetworkConnectionHandlerTest : public testing::Test { |
| 56 public: | 101 public: |
| 57 NetworkConnectionHandlerTest() | 102 NetworkConnectionHandlerTest() |
| 58 : user_("userhash"), | 103 : user_("userhash"), |
| 59 test_manager_client_(NULL), | 104 test_manager_client_(NULL), |
| 60 test_service_client_(NULL) {} | 105 test_service_client_(NULL) {} |
| 61 | 106 |
| 62 virtual ~NetworkConnectionHandlerTest() { | 107 virtual ~NetworkConnectionHandlerTest() { |
| 63 } | 108 } |
| 64 | 109 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 managed_config_handler_.reset(new ManagedNetworkConfigurationHandlerImpl()); | 151 managed_config_handler_.reset(new ManagedNetworkConfigurationHandlerImpl()); |
| 107 managed_config_handler_->Init(network_state_handler_.get(), | 152 managed_config_handler_->Init(network_state_handler_.get(), |
| 108 network_profile_handler_.get(), | 153 network_profile_handler_.get(), |
| 109 network_config_handler_.get(), | 154 network_config_handler_.get(), |
| 110 NULL /* network_device_handler */); | 155 NULL /* network_device_handler */); |
| 111 | 156 |
| 112 network_connection_handler_.reset(new NetworkConnectionHandler); | 157 network_connection_handler_.reset(new NetworkConnectionHandler); |
| 113 network_connection_handler_->Init(network_state_handler_.get(), | 158 network_connection_handler_->Init(network_state_handler_.get(), |
| 114 network_config_handler_.get(), | 159 network_config_handler_.get(), |
| 115 managed_config_handler_.get()); | 160 managed_config_handler_.get()); |
| 161 network_connection_observer_.reset(new TestNetworkConnectionObserver); |
| 162 network_connection_handler_->AddObserver( |
| 163 network_connection_observer_.get()); |
| 116 | 164 |
| 117 base::RunLoop().RunUntilIdle(); | 165 base::RunLoop().RunUntilIdle(); |
| 118 } | 166 } |
| 119 | 167 |
| 120 virtual void TearDown() override { | 168 virtual void TearDown() override { |
| 121 managed_config_handler_.reset(); | 169 managed_config_handler_.reset(); |
| 122 network_profile_handler_.reset(); | 170 network_profile_handler_.reset(); |
| 171 network_connection_handler_->RemoveObserver( |
| 172 network_connection_observer_.get()); |
| 173 network_connection_observer_.reset(); |
| 123 network_connection_handler_.reset(); | 174 network_connection_handler_.reset(); |
| 124 network_config_handler_.reset(); | 175 network_config_handler_.reset(); |
| 125 network_state_handler_.reset(); | 176 network_state_handler_.reset(); |
| 126 CertLoader::Shutdown(); | 177 CertLoader::Shutdown(); |
| 127 LoginState::Shutdown(); | 178 LoginState::Shutdown(); |
| 128 DBusThreadManager::Shutdown(); | 179 DBusThreadManager::Shutdown(); |
| 129 } | 180 } |
| 130 | 181 |
| 131 protected: | 182 protected: |
| 132 bool Configure(const std::string& json_string) { | 183 bool Configure(const std::string& json_string) { |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 std::string(), // no username hash | 321 std::string(), // no username hash |
| 271 *network_configs, | 322 *network_configs, |
| 272 global_config); | 323 global_config); |
| 273 } | 324 } |
| 274 base::RunLoop().RunUntilIdle(); | 325 base::RunLoop().RunUntilIdle(); |
| 275 } | 326 } |
| 276 | 327 |
| 277 scoped_ptr<NetworkStateHandler> network_state_handler_; | 328 scoped_ptr<NetworkStateHandler> network_state_handler_; |
| 278 scoped_ptr<NetworkConfigurationHandler> network_config_handler_; | 329 scoped_ptr<NetworkConfigurationHandler> network_config_handler_; |
| 279 scoped_ptr<NetworkConnectionHandler> network_connection_handler_; | 330 scoped_ptr<NetworkConnectionHandler> network_connection_handler_; |
| 331 scoped_ptr<TestNetworkConnectionObserver> network_connection_observer_; |
| 280 scoped_ptr<ManagedNetworkConfigurationHandlerImpl> managed_config_handler_; | 332 scoped_ptr<ManagedNetworkConfigurationHandlerImpl> managed_config_handler_; |
| 281 scoped_ptr<NetworkProfileHandler> network_profile_handler_; | 333 scoped_ptr<NetworkProfileHandler> network_profile_handler_; |
| 282 crypto::ScopedTestNSSChromeOSUser user_; | 334 crypto::ScopedTestNSSChromeOSUser user_; |
| 283 ShillManagerClient::TestInterface* test_manager_client_; | 335 ShillManagerClient::TestInterface* test_manager_client_; |
| 284 ShillServiceClient::TestInterface* test_service_client_; | 336 ShillServiceClient::TestInterface* test_service_client_; |
| 285 scoped_ptr<net::NSSCertDatabaseChromeOS> test_nssdb_; | 337 scoped_ptr<net::NSSCertDatabaseChromeOS> test_nssdb_; |
| 286 base::MessageLoopForUI message_loop_; | 338 base::MessageLoopForUI message_loop_; |
| 287 std::string result_; | 339 std::string result_; |
| 288 | 340 |
| 289 private: | 341 private: |
| 290 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandlerTest); | 342 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandlerTest); |
| 291 }; | 343 }; |
| 292 | 344 |
| 293 namespace { | 345 namespace { |
| 294 | 346 |
| 347 const char* kNoNetwork = "no-network"; |
| 348 const char* kWifi0 = "wifi0"; |
| 349 const char* kWifi1 = "wifi1"; |
| 350 const char* kWifi2 = "wifi2"; |
| 351 const char* kWifi3 = "wifi3"; |
| 352 |
| 295 const char* kConfigConnectable = | 353 const char* kConfigConnectable = |
| 296 "{ \"GUID\": \"wifi0\", \"Type\": \"wifi\", \"State\": \"idle\", " | 354 "{ \"GUID\": \"wifi0\", \"Type\": \"wifi\", \"State\": \"idle\", " |
| 297 " \"Connectable\": true }"; | 355 " \"Connectable\": true }"; |
| 298 const char* kConfigConnected = | 356 const char* kConfigConnected = |
| 299 "{ \"GUID\": \"wifi1\", \"Type\": \"wifi\", \"State\": \"online\" }"; | 357 "{ \"GUID\": \"wifi1\", \"Type\": \"wifi\", \"State\": \"online\" }"; |
| 300 const char* kConfigConnecting = | 358 const char* kConfigConnecting = |
| 301 "{ \"GUID\": \"wifi2\", \"Type\": \"wifi\", \"State\": \"association\" }"; | 359 "{ \"GUID\": \"wifi2\", \"Type\": \"wifi\", \"State\": \"association\" }"; |
| 302 const char* kConfigRequiresPassphrase = | 360 const char* kConfigRequiresPassphrase = |
| 303 "{ \"GUID\": \"wifi3\", \"Type\": \"wifi\", " | 361 "{ \"GUID\": \"wifi3\", \"Type\": \"wifi\", " |
| 304 " \"PassphraseRequired\": true }"; | 362 " \"PassphraseRequired\": true }"; |
| 305 | 363 |
| 306 } // namespace | 364 } // namespace |
| 307 | 365 |
| 308 TEST_F(NetworkConnectionHandlerTest, NetworkConnectionHandlerConnectSuccess) { | 366 TEST_F(NetworkConnectionHandlerTest, NetworkConnectionHandlerConnectSuccess) { |
| 309 EXPECT_TRUE(Configure(kConfigConnectable)); | 367 EXPECT_TRUE(Configure(kConfigConnectable)); |
| 310 Connect("wifi0"); | 368 Connect(kWifi0); |
| 311 EXPECT_EQ(kSuccessResult, GetResultAndReset()); | 369 EXPECT_EQ(kSuccessResult, GetResultAndReset()); |
| 312 EXPECT_EQ(shill::kStateOnline, | 370 EXPECT_EQ(shill::kStateOnline, |
| 313 GetServiceStringProperty("wifi0", shill::kStateProperty)); | 371 GetServiceStringProperty(kWifi0, shill::kStateProperty)); |
| 372 // Observer expectations |
| 373 EXPECT_TRUE(network_connection_observer_->GetRequested(kWifi0)); |
| 374 EXPECT_EQ(kSuccessResult, network_connection_observer_->GetResult(kWifi0)); |
| 314 } | 375 } |
| 315 | 376 |
| 316 // Handles basic failure cases. | 377 // Handles basic failure cases. |
| 317 TEST_F(NetworkConnectionHandlerTest, NetworkConnectionHandlerConnectFailure) { | 378 TEST_F(NetworkConnectionHandlerTest, NetworkConnectionHandlerConnectFailure) { |
| 318 Connect("no-network"); | 379 Connect(kNoNetwork); |
| 319 EXPECT_EQ(NetworkConnectionHandler::kErrorConfigureFailed, | 380 EXPECT_EQ(NetworkConnectionHandler::kErrorConfigureFailed, |
| 320 GetResultAndReset()); | 381 GetResultAndReset()); |
| 382 EXPECT_TRUE(network_connection_observer_->GetRequested(kNoNetwork)); |
| 383 EXPECT_EQ(NetworkConnectionHandler::kErrorConfigureFailed, |
| 384 network_connection_observer_->GetResult(kNoNetwork)); |
| 321 | 385 |
| 322 EXPECT_TRUE(Configure(kConfigConnected)); | 386 EXPECT_TRUE(Configure(kConfigConnected)); |
| 323 Connect("wifi1"); | 387 Connect(kWifi1); |
| 324 EXPECT_EQ(NetworkConnectionHandler::kErrorConnected, GetResultAndReset()); | 388 EXPECT_EQ(NetworkConnectionHandler::kErrorConnected, GetResultAndReset()); |
| 389 EXPECT_TRUE(network_connection_observer_->GetRequested(kWifi1)); |
| 390 EXPECT_EQ(NetworkConnectionHandler::kErrorConnected, |
| 391 network_connection_observer_->GetResult(kWifi1)); |
| 325 | 392 |
| 326 EXPECT_TRUE(Configure(kConfigConnecting)); | 393 EXPECT_TRUE(Configure(kConfigConnecting)); |
| 327 Connect("wifi2"); | 394 Connect(kWifi2); |
| 328 EXPECT_EQ(NetworkConnectionHandler::kErrorConnecting, GetResultAndReset()); | 395 EXPECT_EQ(NetworkConnectionHandler::kErrorConnecting, GetResultAndReset()); |
| 396 EXPECT_TRUE(network_connection_observer_->GetRequested(kWifi2)); |
| 397 EXPECT_EQ(NetworkConnectionHandler::kErrorConnecting, |
| 398 network_connection_observer_->GetResult(kWifi2)); |
| 329 | 399 |
| 330 EXPECT_TRUE(Configure(kConfigRequiresPassphrase)); | 400 EXPECT_TRUE(Configure(kConfigRequiresPassphrase)); |
| 331 Connect("wifi3"); | 401 Connect(kWifi3); |
| 332 EXPECT_EQ(NetworkConnectionHandler::kErrorPassphraseRequired, | 402 EXPECT_EQ(NetworkConnectionHandler::kErrorPassphraseRequired, |
| 333 GetResultAndReset()); | 403 GetResultAndReset()); |
| 404 EXPECT_TRUE(network_connection_observer_->GetRequested(kWifi3)); |
| 405 EXPECT_EQ(NetworkConnectionHandler::kErrorPassphraseRequired, |
| 406 network_connection_observer_->GetResult(kWifi3)); |
| 334 } | 407 } |
| 335 | 408 |
| 336 namespace { | 409 namespace { |
| 337 | 410 |
| 338 const char* kPolicyWithCertPatternTemplate = | 411 const char* kPolicyWithCertPatternTemplate = |
| 339 "[ { \"GUID\": \"wifi4\"," | 412 "[ { \"GUID\": \"wifi4\"," |
| 340 " \"Name\": \"wifi4\"," | 413 " \"Name\": \"wifi4\"," |
| 341 " \"Type\": \"WiFi\"," | 414 " \"Type\": \"WiFi\"," |
| 342 " \"WiFi\": {" | 415 " \"WiFi\": {" |
| 343 " \"Security\": \"WPA-EAP\"," | 416 " \"Security\": \"WPA-EAP\"," |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 | 477 |
| 405 // |StartCertLoader| should have triggered certificate loading. | 478 // |StartCertLoader| should have triggered certificate loading. |
| 406 // When the certificates got loaded, the connection request should have | 479 // When the certificates got loaded, the connection request should have |
| 407 // proceeded and eventually succeeded. | 480 // proceeded and eventually succeeded. |
| 408 EXPECT_EQ(kSuccessResult, GetResultAndReset()); | 481 EXPECT_EQ(kSuccessResult, GetResultAndReset()); |
| 409 } | 482 } |
| 410 | 483 |
| 411 TEST_F(NetworkConnectionHandlerTest, | 484 TEST_F(NetworkConnectionHandlerTest, |
| 412 NetworkConnectionHandlerDisconnectSuccess) { | 485 NetworkConnectionHandlerDisconnectSuccess) { |
| 413 EXPECT_TRUE(Configure(kConfigConnected)); | 486 EXPECT_TRUE(Configure(kConfigConnected)); |
| 414 Disconnect("wifi1"); | 487 Disconnect(kWifi1); |
| 488 EXPECT_TRUE(network_connection_observer_->GetRequested(kWifi1)); |
| 415 EXPECT_EQ(kSuccessResult, GetResultAndReset()); | 489 EXPECT_EQ(kSuccessResult, GetResultAndReset()); |
| 416 } | 490 } |
| 417 | 491 |
| 418 TEST_F(NetworkConnectionHandlerTest, | 492 TEST_F(NetworkConnectionHandlerTest, |
| 419 NetworkConnectionHandlerDisconnectFailure) { | 493 NetworkConnectionHandlerDisconnectFailure) { |
| 420 Connect("no-network"); | 494 Connect(kNoNetwork); |
| 421 EXPECT_EQ(NetworkConnectionHandler::kErrorConfigureFailed, | 495 EXPECT_EQ(NetworkConnectionHandler::kErrorConfigureFailed, |
| 422 GetResultAndReset()); | 496 GetResultAndReset()); |
| 423 | 497 |
| 424 EXPECT_TRUE(Configure(kConfigConnectable)); | 498 EXPECT_TRUE(Configure(kConfigConnectable)); |
| 425 Disconnect("wifi0"); | 499 Disconnect(kWifi0); |
| 426 EXPECT_EQ(NetworkConnectionHandler::kErrorNotConnected, GetResultAndReset()); | 500 EXPECT_EQ(NetworkConnectionHandler::kErrorNotConnected, GetResultAndReset()); |
| 427 } | 501 } |
| 428 | 502 |
| 429 } // namespace chromeos | 503 } // namespace chromeos |
| 430 | 504 |
| 431 #endif | 505 #endif |
| OLD | NEW |