Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(756)

Side by Side Diff: chromeos/network/network_connection_handler_unittest.cc

Issue 840163004: Enable NetworkConnectionHandler and AutoConnectHandler unittests in debug. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chromeos/network/auto_connect_handler_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h" 13 #include "base/run_loop.h"
14 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 #include "chromeos/cert_loader.h" 15 #include "chromeos/cert_loader.h"
16 #include "chromeos/dbus/dbus_thread_manager.h" 16 #include "chromeos/dbus/dbus_thread_manager.h"
17 #include "chromeos/dbus/shill_device_client.h" 17 #include "chromeos/dbus/shill_device_client.h"
18 #include "chromeos/dbus/shill_manager_client.h" 18 #include "chromeos/dbus/shill_manager_client.h"
19 #include "chromeos/dbus/shill_profile_client.h" 19 #include "chromeos/dbus/shill_profile_client.h"
20 #include "chromeos/dbus/shill_service_client.h" 20 #include "chromeos/dbus/shill_service_client.h"
21 #include "chromeos/network/managed_network_configuration_handler_impl.h" 21 #include "chromeos/network/managed_network_configuration_handler_impl.h"
22 #include "chromeos/network/network_configuration_handler.h" 22 #include "chromeos/network/network_configuration_handler.h"
23 #include "chromeos/network/network_profile_handler.h" 23 #include "chromeos/network/network_profile_handler.h"
24 #include "chromeos/network/network_state_handler.h" 24 #include "chromeos/network/network_state_handler.h"
25 #include "chromeos/network/onc/onc_utils.h" 25 #include "chromeos/network/onc/onc_utils.h"
26 #include "components/onc/onc_constants.h" 26 #include "components/onc/onc_constants.h"
27 #include "crypto/nss_util_internal.h" 27 #include "crypto/scoped_nss_types.h"
28 #include "crypto/scoped_test_nss_chromeos_user.h" 28 #include "crypto/scoped_test_nss_db.h"
29 #include "net/base/net_errors.h" 29 #include "net/base/net_errors.h"
30 #include "net/base/test_data_directory.h" 30 #include "net/base/test_data_directory.h"
31 #include "net/cert/nss_cert_database_chromeos.h" 31 #include "net/cert/nss_cert_database_chromeos.h"
32 #include "net/cert/x509_certificate.h" 32 #include "net/cert/x509_certificate.h"
33 #include "net/test/cert_test_util.h" 33 #include "net/test/cert_test_util.h"
34 #include "testing/gtest/include/gtest/gtest.h" 34 #include "testing/gtest/include/gtest/gtest.h"
35 #include "third_party/cros_system_api/dbus/service_constants.h" 35 #include "third_party/cros_system_api/dbus/service_constants.h"
36 36
37 // http://crbug.com/418369
38 #ifdef NDEBUG
39
40 namespace { 37 namespace {
41 38
42 const char* kSuccessResult = "success"; 39 const char* kSuccessResult = "success";
40 const char* kUsernameHash = "userhash";
43 41
44 void ConfigureCallback(const dbus::ObjectPath& result) { 42 void ConfigureCallback(const dbus::ObjectPath& result) {
45 } 43 }
46 44
47 void ConfigureErrorCallback(const std::string& error_name, 45 void ConfigureErrorCallback(const std::string& error_name,
48 const std::string& error_message) { 46 const std::string& error_message) {
49 } 47 }
50 48
51 } // namespace 49 } // namespace
52 50
53 namespace chromeos { 51 namespace chromeos {
54 52
55 class NetworkConnectionHandlerTest : public testing::Test { 53 class NetworkConnectionHandlerTest : public testing::Test {
56 public: 54 public:
57 NetworkConnectionHandlerTest() 55 NetworkConnectionHandlerTest()
58 : user_("userhash"), 56 : test_manager_client_(nullptr), test_service_client_(nullptr) {}
59 test_manager_client_(NULL),
60 test_service_client_(NULL) {}
61 57
62 virtual ~NetworkConnectionHandlerTest() { 58 ~NetworkConnectionHandlerTest() override {}
63 }
64 59
65 virtual void SetUp() override { 60 void SetUp() override {
66 ASSERT_TRUE(user_.constructed_successfully()); 61 ASSERT_TRUE(test_nssdb_.is_open());
67 user_.FinishInit();
68 62
69 test_nssdb_.reset(new net::NSSCertDatabaseChromeOS( 63 // Use the same DB for public and private slot.
70 crypto::GetPublicSlotForChromeOSUser(user_.username_hash()), 64 test_nsscertdb_.reset(new net::NSSCertDatabaseChromeOS(
71 crypto::GetPrivateSlotForChromeOSUser( 65 crypto::ScopedPK11Slot(PK11_ReferenceSlot(test_nssdb_.slot())),
72 user_.username_hash(), 66 crypto::ScopedPK11Slot(PK11_ReferenceSlot(test_nssdb_.slot()))));
73 base::Callback<void(crypto::ScopedPK11Slot)>()))); 67 test_nsscertdb_->SetSlowTaskRunnerForTest(message_loop_.task_runner());
74 test_nssdb_->SetSlowTaskRunnerForTest(message_loop_.message_loop_proxy());
75 68
76 CertLoader::Initialize(); 69 CertLoader::Initialize();
77 CertLoader::ForceHardwareBackedForTesting(); 70 CertLoader::ForceHardwareBackedForTesting();
78 71
79 DBusThreadManager::Initialize(); 72 DBusThreadManager::Initialize();
80 DBusThreadManager* dbus_manager = DBusThreadManager::Get(); 73 DBusThreadManager* dbus_manager = DBusThreadManager::Get();
81 test_manager_client_ = 74 test_manager_client_ =
82 dbus_manager->GetShillManagerClient()->GetTestInterface(); 75 dbus_manager->GetShillManagerClient()->GetTestInterface();
83 test_service_client_ = 76 test_service_client_ =
84 dbus_manager->GetShillServiceClient()->GetTestInterface(); 77 dbus_manager->GetShillServiceClient()->GetTestInterface();
85 78
86 test_manager_client_->AddTechnology(shill::kTypeWifi, true /* enabled */); 79 test_manager_client_->AddTechnology(shill::kTypeWifi, true /* enabled */);
87 dbus_manager->GetShillDeviceClient()->GetTestInterface()->AddDevice( 80 dbus_manager->GetShillDeviceClient()->GetTestInterface()->AddDevice(
88 "/device/wifi1", shill::kTypeWifi, "wifi_device1"); 81 "/device/wifi1", shill::kTypeWifi, "wifi_device1");
89 test_manager_client_->AddTechnology(shill::kTypeCellular, 82 test_manager_client_->AddTechnology(shill::kTypeCellular,
90 true /* enabled */); 83 true /* enabled */);
91 dbus_manager->GetShillProfileClient()->GetTestInterface()->AddProfile( 84 dbus_manager->GetShillProfileClient()->GetTestInterface()->AddProfile(
92 "shared_profile_path", std::string() /* shared profile */); 85 "shared_profile_path", std::string() /* shared profile */);
93 dbus_manager->GetShillProfileClient()->GetTestInterface()->AddProfile( 86 dbus_manager->GetShillProfileClient()->GetTestInterface()->AddProfile(
94 "user_profile_path", user_.username_hash()); 87 "user_profile_path", kUsernameHash);
95 88
96 base::RunLoop().RunUntilIdle(); 89 base::RunLoop().RunUntilIdle();
97 LoginState::Initialize(); 90 LoginState::Initialize();
98 network_state_handler_.reset(NetworkStateHandler::InitializeForTest()); 91 network_state_handler_.reset(NetworkStateHandler::InitializeForTest());
99 network_config_handler_.reset( 92 network_config_handler_.reset(
100 NetworkConfigurationHandler::InitializeForTest( 93 NetworkConfigurationHandler::InitializeForTest(
101 network_state_handler_.get(), NULL /* network_device_handler */)); 94 network_state_handler_.get(),
95 nullptr /* network_device_handler */));
102 96
103 network_profile_handler_.reset(new NetworkProfileHandler()); 97 network_profile_handler_.reset(new NetworkProfileHandler());
104 network_profile_handler_->Init(); 98 network_profile_handler_->Init();
105 99
106 managed_config_handler_.reset(new ManagedNetworkConfigurationHandlerImpl()); 100 managed_config_handler_.reset(new ManagedNetworkConfigurationHandlerImpl());
107 managed_config_handler_->Init(network_state_handler_.get(), 101 managed_config_handler_->Init(
108 network_profile_handler_.get(), 102 network_state_handler_.get(), network_profile_handler_.get(),
109 network_config_handler_.get(), 103 network_config_handler_.get(), nullptr /* network_device_handler */);
110 NULL /* network_device_handler */);
111 104
112 network_connection_handler_.reset(new NetworkConnectionHandler); 105 network_connection_handler_.reset(new NetworkConnectionHandler);
113 network_connection_handler_->Init(network_state_handler_.get(), 106 network_connection_handler_->Init(network_state_handler_.get(),
114 network_config_handler_.get(), 107 network_config_handler_.get(),
115 managed_config_handler_.get()); 108 managed_config_handler_.get());
116 109
117 base::RunLoop().RunUntilIdle(); 110 base::RunLoop().RunUntilIdle();
118 } 111 }
119 112
120 virtual void TearDown() override { 113 void TearDown() override {
121 managed_config_handler_.reset(); 114 managed_config_handler_.reset();
122 network_profile_handler_.reset(); 115 network_profile_handler_.reset();
123 network_connection_handler_.reset(); 116 network_connection_handler_.reset();
124 network_config_handler_.reset(); 117 network_config_handler_.reset();
125 network_state_handler_.reset(); 118 network_state_handler_.reset();
126 CertLoader::Shutdown(); 119 CertLoader::Shutdown();
127 LoginState::Shutdown(); 120 LoginState::Shutdown();
128 DBusThreadManager::Shutdown(); 121 DBusThreadManager::Shutdown();
129 } 122 }
130 123
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 const std::string& key) { 178 const std::string& key) {
186 std::string result; 179 std::string result;
187 const base::DictionaryValue* properties = 180 const base::DictionaryValue* properties =
188 test_service_client_->GetServiceProperties(service_path); 181 test_service_client_->GetServiceProperties(service_path);
189 if (properties) 182 if (properties)
190 properties->GetStringWithoutPathExpansion(key, &result); 183 properties->GetStringWithoutPathExpansion(key, &result);
191 return result; 184 return result;
192 } 185 }
193 186
194 void StartCertLoader() { 187 void StartCertLoader() {
195 CertLoader::Get()->StartWithNSSDB(test_nssdb_.get()); 188 CertLoader::Get()->StartWithNSSDB(test_nsscertdb_.get());
196 base::RunLoop().RunUntilIdle(); 189 base::RunLoop().RunUntilIdle();
197 } 190 }
198 191
199 void LoginToRegularUser() { 192 void LoginToRegularUser() {
200 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE, 193 LoginState::Get()->SetLoggedInState(LoginState::LOGGED_IN_ACTIVE,
201 LoginState::LOGGED_IN_USER_REGULAR); 194 LoginState::LOGGED_IN_USER_REGULAR);
202 base::RunLoop().RunUntilIdle(); 195 base::RunLoop().RunUntilIdle();
203 } 196 }
204 197
205 scoped_refptr<net::X509Certificate> ImportTestClientCert() { 198 scoped_refptr<net::X509Certificate> ImportTestClientCert() {
206 net::CertificateList ca_cert_list = 199 net::CertificateList ca_cert_list(net::CreateCertificateListFromFile(
207 net::CreateCertificateListFromFile(net::GetTestCertsDirectory(), 200 net::GetTestCertsDirectory(), "client_1_ca.pem",
208 "websocket_cacert.pem", 201 net::X509Certificate::FORMAT_AUTO));
209 net::X509Certificate::FORMAT_AUTO);
210 if (ca_cert_list.empty()) { 202 if (ca_cert_list.empty()) {
211 LOG(ERROR) << "No CA cert loaded."; 203 LOG(ERROR) << "No CA cert loaded.";
212 return NULL; 204 return nullptr;
213 } 205 }
214 net::NSSCertDatabase::ImportCertFailureList failures; 206 net::NSSCertDatabase::ImportCertFailureList failures;
215 EXPECT_TRUE(test_nssdb_->ImportCACerts( 207 EXPECT_TRUE(test_nsscertdb_->ImportCACerts(
216 ca_cert_list, net::NSSCertDatabase::TRUST_DEFAULT, &failures)); 208 ca_cert_list, net::NSSCertDatabase::TRUST_DEFAULT, &failures));
217 if (!failures.empty()) { 209 if (!failures.empty()) {
218 LOG(ERROR) << net::ErrorToString(failures[0].net_error); 210 LOG(ERROR) << net::ErrorToString(failures[0].net_error);
219 return NULL; 211 return nullptr;
220 } 212 }
221 213
222 std::string pkcs12_data; 214 // Import a client cert signed by that CA.
223 base::FilePath pkcs12_path = 215 scoped_refptr<net::X509Certificate> client_cert(
224 net::GetTestCertsDirectory().Append("websocket_client_cert.p12"); 216 net::ImportClientCertAndKeyFromFile(net::GetTestCertsDirectory(),
225 if (!base::ReadFileToString(pkcs12_path, &pkcs12_data)) 217 "client_1.pem", "client_1.pk8",
226 return NULL; 218 test_nssdb_.slot()));
227 219 return client_cert;
228 net::CertificateList loaded_certs;
229 scoped_refptr<net::CryptoModule> module(net::CryptoModule::CreateFromHandle(
230 test_nssdb_->GetPrivateSlot().get()));
231 if (test_nssdb_->ImportFromPKCS12(module.get(),
232 pkcs12_data,
233 base::string16(),
234 false,
235 &loaded_certs) != net::OK) {
236 LOG(ERROR) << "Error while importing to NSSDB.";
237 return NULL;
238 }
239
240 // File contains two certs, the client cert first and the CA cert second.
241 if (loaded_certs.size() != 2U) {
242 LOG(ERROR) << "Expected two certs in file, found " << loaded_certs.size();
243 return NULL;
244 }
245 return loaded_certs[0];
246 } 220 }
247 221
248 void SetupPolicy(const std::string& network_configs_json, 222 void SetupPolicy(const std::string& network_configs_json,
249 const base::DictionaryValue& global_config, 223 const base::DictionaryValue& global_config,
250 bool user_policy) { 224 bool user_policy) {
251 std::string error; 225 std::string error;
252 scoped_ptr<base::Value> network_configs_value( 226 scoped_ptr<base::Value> network_configs_value(
253 base::JSONReader::ReadAndReturnError(network_configs_json, 227 base::JSONReader::ReadAndReturnError(network_configs_json,
254 base::JSON_ALLOW_TRAILING_COMMAS, 228 base::JSON_ALLOW_TRAILING_COMMAS,
255 NULL, 229 nullptr, &error));
256 &error));
257 ASSERT_TRUE(network_configs_value) << error; 230 ASSERT_TRUE(network_configs_value) << error;
258 231
259 base::ListValue* network_configs = NULL; 232 base::ListValue* network_configs = nullptr;
260 ASSERT_TRUE(network_configs_value->GetAsList(&network_configs)); 233 ASSERT_TRUE(network_configs_value->GetAsList(&network_configs));
261 234
262 if (user_policy) { 235 if (user_policy) {
263 managed_config_handler_->SetPolicy( 236 managed_config_handler_->SetPolicy(::onc::ONC_SOURCE_USER_POLICY,
264 ::onc::ONC_SOURCE_USER_POLICY, 237 kUsernameHash, *network_configs,
265 user_.username_hash(), 238 global_config);
266 *network_configs,
267 global_config);
268 } else { 239 } else {
269 managed_config_handler_->SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY, 240 managed_config_handler_->SetPolicy(::onc::ONC_SOURCE_DEVICE_POLICY,
270 std::string(), // no username hash 241 std::string(), // no username hash
271 *network_configs, 242 *network_configs,
272 global_config); 243 global_config);
273 } 244 }
274 base::RunLoop().RunUntilIdle(); 245 base::RunLoop().RunUntilIdle();
275 } 246 }
276 247
277 scoped_ptr<NetworkStateHandler> network_state_handler_; 248 scoped_ptr<NetworkStateHandler> network_state_handler_;
278 scoped_ptr<NetworkConfigurationHandler> network_config_handler_; 249 scoped_ptr<NetworkConfigurationHandler> network_config_handler_;
279 scoped_ptr<NetworkConnectionHandler> network_connection_handler_; 250 scoped_ptr<NetworkConnectionHandler> network_connection_handler_;
280 scoped_ptr<ManagedNetworkConfigurationHandlerImpl> managed_config_handler_; 251 scoped_ptr<ManagedNetworkConfigurationHandlerImpl> managed_config_handler_;
281 scoped_ptr<NetworkProfileHandler> network_profile_handler_; 252 scoped_ptr<NetworkProfileHandler> network_profile_handler_;
282 crypto::ScopedTestNSSChromeOSUser user_;
283 ShillManagerClient::TestInterface* test_manager_client_; 253 ShillManagerClient::TestInterface* test_manager_client_;
284 ShillServiceClient::TestInterface* test_service_client_; 254 ShillServiceClient::TestInterface* test_service_client_;
285 scoped_ptr<net::NSSCertDatabaseChromeOS> test_nssdb_; 255 crypto::ScopedTestNSSDB test_nssdb_;
256 scoped_ptr<net::NSSCertDatabaseChromeOS> test_nsscertdb_;
286 base::MessageLoopForUI message_loop_; 257 base::MessageLoopForUI message_loop_;
287 std::string result_; 258 std::string result_;
288 259
289 private: 260 private:
290 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandlerTest); 261 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandlerTest);
291 }; 262 };
292 263
293 namespace { 264 namespace {
294 265
295 const char* kConfigConnectable = 266 const char* kConfigConnectable =
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 Connect("no-network"); 391 Connect("no-network");
421 EXPECT_EQ(NetworkConnectionHandler::kErrorConfigureFailed, 392 EXPECT_EQ(NetworkConnectionHandler::kErrorConfigureFailed,
422 GetResultAndReset()); 393 GetResultAndReset());
423 394
424 EXPECT_TRUE(Configure(kConfigConnectable)); 395 EXPECT_TRUE(Configure(kConfigConnectable));
425 Disconnect("wifi0"); 396 Disconnect("wifi0");
426 EXPECT_EQ(NetworkConnectionHandler::kErrorNotConnected, GetResultAndReset()); 397 EXPECT_EQ(NetworkConnectionHandler::kErrorNotConnected, GetResultAndReset());
427 } 398 }
428 399
429 } // namespace chromeos 400 } // namespace chromeos
430
431 #endif
OLDNEW
« no previous file with comments | « chromeos/network/auto_connect_handler_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698