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

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

Issue 135193007: Use user specific NSSDatabase in CertLoader. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 6 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 | Annotate | Revision Log
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"
9 #include "base/file_util.h"
8 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/strings/stringprintf.h"
10 #include "chromeos/dbus/dbus_thread_manager.h" 14 #include "chromeos/dbus/dbus_thread_manager.h"
11 #include "chromeos/dbus/shill_manager_client.h" 15 #include "chromeos/dbus/shill_manager_client.h"
12 #include "chromeos/dbus/shill_service_client.h" 16 #include "chromeos/dbus/shill_service_client.h"
13 #include "chromeos/network/network_configuration_handler.h" 17 #include "chromeos/network/network_configuration_handler.h"
14 #include "chromeos/network/network_state_handler.h" 18 #include "chromeos/network/network_state_handler.h"
15 #include "chromeos/network/onc/onc_utils.h" 19 #include "chromeos/network/onc/onc_utils.h"
20 #include "crypto/nss_util.h"
21 #include "crypto/nss_util_internal.h"
22 #include "net/base/net_errors.h"
23 #include "net/base/test_data_directory.h"
24 #include "net/cert/nss_cert_database_chromeos.h"
25 #include "net/cert/x509_certificate.h"
26 #include "net/test/cert_test_util.h"
16 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h" 28 #include "third_party/cros_system_api/dbus/service_constants.h"
18 29
19 namespace { 30 namespace {
20 31
21 const char* kSuccessResult = "success"; 32 const char* kSuccessResult = "success";
22 33
23 void ConfigureCallback(const dbus::ObjectPath& result) { 34 void ConfigureCallback(const dbus::ObjectPath& result) {
24 } 35 }
25 36
26 void ConfigureErrorCallback(const std::string& error_name, 37 void ConfigureErrorCallback(const std::string& error_name,
27 const std::string& error_message) { 38 const std::string& error_message) {
28 } 39 }
29 40
30 } // namespace 41 } // namespace
31 42
32 namespace chromeos { 43 namespace chromeos {
33 44
34 class NetworkConnectionHandlerTest : public testing::Test { 45 class NetworkConnectionHandlerTest : public testing::Test {
35 public: 46 public:
36 NetworkConnectionHandlerTest() { 47 NetworkConnectionHandlerTest() : user_("userhash") {
37 } 48 }
38 virtual ~NetworkConnectionHandlerTest() { 49 virtual ~NetworkConnectionHandlerTest() {
39 } 50 }
40 51
41 virtual void SetUp() OVERRIDE { 52 virtual void SetUp() OVERRIDE {
53 ASSERT_TRUE(user_.constructed_successfully());
54 user_.FinishInit();
55
56 test_nssdb_.reset(new net::NSSCertDatabaseChromeOS(
57 crypto::GetPublicSlotForChromeOSUser(user_.username_hash()),
58 crypto::GetPrivateSlotForChromeOSUser(
59 user_.username_hash(),
60 base::Callback<void(crypto::ScopedPK11Slot)>())));
61
62 CertLoader::Initialize();
63 CertLoader* cert_loader = CertLoader::Get();
64 cert_loader->SetCryptoTaskRunner(message_loop_.message_loop_proxy());
65 cert_loader->set_hardware_backed_for_test();
66
42 // Initialize DBusThreadManager with a stub implementation. 67 // Initialize DBusThreadManager with a stub implementation.
43 DBusThreadManager::InitializeWithStub(); 68 DBusThreadManager::InitializeWithStub();
44 message_loop_.RunUntilIdle(); 69 base::RunLoop().RunUntilIdle();
45 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface() 70 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface()
46 ->ClearServices(); 71 ->ClearServices();
47 message_loop_.RunUntilIdle(); 72 base::RunLoop().RunUntilIdle();
48 LoginState::Initialize(); 73 LoginState::Initialize();
49 network_state_handler_.reset(NetworkStateHandler::InitializeForTest()); 74 network_state_handler_.reset(NetworkStateHandler::InitializeForTest());
50 network_configuration_handler_.reset( 75 network_configuration_handler_.reset(
51 NetworkConfigurationHandler::InitializeForTest( 76 NetworkConfigurationHandler::InitializeForTest(
52 network_state_handler_.get())); 77 network_state_handler_.get()));
78
53 network_connection_handler_.reset(new NetworkConnectionHandler); 79 network_connection_handler_.reset(new NetworkConnectionHandler);
54 // TODO(stevenjb): Test integration with CertLoader using a stub or mock.
55 network_connection_handler_->Init(network_state_handler_.get(), 80 network_connection_handler_->Init(network_state_handler_.get(),
56 network_configuration_handler_.get()); 81 network_configuration_handler_.get());
57 } 82 }
58 83
59 virtual void TearDown() OVERRIDE { 84 virtual void TearDown() OVERRIDE {
60 network_connection_handler_.reset(); 85 network_connection_handler_.reset();
61 network_configuration_handler_.reset(); 86 network_configuration_handler_.reset();
62 network_state_handler_.reset(); 87 network_state_handler_.reset();
88 CertLoader::Shutdown();
63 LoginState::Shutdown(); 89 LoginState::Shutdown();
64 DBusThreadManager::Shutdown(); 90 DBusThreadManager::Shutdown();
65 } 91 }
66 92
67 protected: 93 protected:
68 bool Configure(const std::string& json_string) { 94 bool Configure(const std::string& json_string) {
69 scoped_ptr<base::DictionaryValue> json_dict = 95 scoped_ptr<base::DictionaryValue> json_dict =
70 onc::ReadDictionaryFromJson(json_string); 96 onc::ReadDictionaryFromJson(json_string);
71 if (!json_dict) { 97 if (!json_dict) {
72 LOG(ERROR) << "Error parsing json: " << json_string; 98 LOG(ERROR) << "Error parsing json: " << json_string;
73 return false; 99 return false;
74 } 100 }
75 DBusThreadManager::Get()->GetShillManagerClient()->ConfigureService( 101 DBusThreadManager::Get()->GetShillManagerClient()->ConfigureService(
76 *json_dict, 102 *json_dict,
77 base::Bind(&ConfigureCallback), 103 base::Bind(&ConfigureCallback),
78 base::Bind(&ConfigureErrorCallback)); 104 base::Bind(&ConfigureErrorCallback));
79 message_loop_.RunUntilIdle(); 105 base::RunLoop().RunUntilIdle();
80 return true; 106 return true;
81 } 107 }
82 108
83 void Connect(const std::string& service_path) { 109 void Connect(const std::string& service_path) {
84 const bool check_error_state = true; 110 const bool check_error_state = true;
85 network_connection_handler_->ConnectToNetwork( 111 network_connection_handler_->ConnectToNetwork(
86 service_path, 112 service_path,
87 base::Bind(&NetworkConnectionHandlerTest::SuccessCallback, 113 base::Bind(&NetworkConnectionHandlerTest::SuccessCallback,
88 base::Unretained(this)), 114 base::Unretained(this)),
89 base::Bind(&NetworkConnectionHandlerTest::ErrorCallback, 115 base::Bind(&NetworkConnectionHandlerTest::ErrorCallback,
90 base::Unretained(this)), 116 base::Unretained(this)),
91 check_error_state); 117 check_error_state);
92 message_loop_.RunUntilIdle(); 118 base::RunLoop().RunUntilIdle();
93 } 119 }
94 120
95 void Disconnect(const std::string& service_path) { 121 void Disconnect(const std::string& service_path) {
96 network_connection_handler_->DisconnectNetwork( 122 network_connection_handler_->DisconnectNetwork(
97 service_path, 123 service_path,
98 base::Bind(&NetworkConnectionHandlerTest::SuccessCallback, 124 base::Bind(&NetworkConnectionHandlerTest::SuccessCallback,
99 base::Unretained(this)), 125 base::Unretained(this)),
100 base::Bind(&NetworkConnectionHandlerTest::ErrorCallback, 126 base::Bind(&NetworkConnectionHandlerTest::ErrorCallback,
101 base::Unretained(this))); 127 base::Unretained(this)));
102 message_loop_.RunUntilIdle(); 128 base::RunLoop().RunUntilIdle();
103 } 129 }
104 130
105 void SuccessCallback() { 131 void SuccessCallback() {
106 result_ = kSuccessResult; 132 result_ = kSuccessResult;
107 } 133 }
108 134
109 void ErrorCallback(const std::string& error_name, 135 void ErrorCallback(const std::string& error_name,
110 scoped_ptr<base::DictionaryValue> error_data) { 136 scoped_ptr<base::DictionaryValue> error_data) {
111 result_ = error_name; 137 result_ = error_name;
112 } 138 }
113 139
114 std::string GetResultAndReset() { 140 std::string GetResultAndReset() {
115 std::string result; 141 std::string result;
116 result.swap(result_); 142 result.swap(result_);
117 return result; 143 return result;
118 } 144 }
119 145
120 std::string GetServiceStringProperty(const std::string& service_path, 146 std::string GetServiceStringProperty(const std::string& service_path,
121 const std::string& key) { 147 const std::string& key) {
122 std::string result; 148 std::string result;
123 const base::DictionaryValue* properties = 149 const base::DictionaryValue* properties =
124 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface()-> 150 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface()->
125 GetServiceProperties(service_path); 151 GetServiceProperties(service_path);
126 if (properties) 152 if (properties)
127 properties->GetStringWithoutPathExpansion(key, &result); 153 properties->GetStringWithoutPathExpansion(key, &result);
128 return result; 154 return result;
129 } 155 }
130 156
157 void StartCertLoader() {
158 CertLoader::Get()->StartWithNSSDB(test_nssdb_.get());
159 base::RunLoop().RunUntilIdle();
160 }
161
162 bool ImportClientCertAndKey(const std::string& pkcs12_file,
163 net::NSSCertDatabase* nssdb,
164 net::CertificateList* loaded_certs) {
165 std::string pkcs12_data;
166 base::FilePath pkcs12_path =
167 net::GetTestCertsDirectory().Append(pkcs12_file);
168 if (!base::ReadFileToString(pkcs12_path, &pkcs12_data))
169 return false;
170
171 scoped_refptr<net::CryptoModule> module(
172 net::CryptoModule::CreateFromHandle(nssdb->GetPrivateSlot().get()));
173 return net::OK ==
174 nssdb->ImportFromPKCS12(module, pkcs12_data, base::string16(), false,
175 loaded_certs);
176 }
177
131 scoped_ptr<NetworkStateHandler> network_state_handler_; 178 scoped_ptr<NetworkStateHandler> network_state_handler_;
132 scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_; 179 scoped_ptr<NetworkConfigurationHandler> network_configuration_handler_;
133 scoped_ptr<NetworkConnectionHandler> network_connection_handler_; 180 scoped_ptr<NetworkConnectionHandler> network_connection_handler_;
181 crypto::ScopedTestNSSChromeOSUser user_;
182 scoped_ptr<net::NSSCertDatabaseChromeOS> test_nssdb_;
134 base::MessageLoopForUI message_loop_; 183 base::MessageLoopForUI message_loop_;
135 std::string result_; 184 std::string result_;
136 185
137 private: 186 private:
138 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandlerTest); 187 DISALLOW_COPY_AND_ASSIGN(NetworkConnectionHandlerTest);
139 }; 188 };
140 189
141 namespace { 190 namespace {
142 191
143 const char* kConfigConnectable = 192 const char* kConfigConnectable =
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 GetResultAndReset()); 233 GetResultAndReset());
185 234
186 EXPECT_TRUE(Configure(kConfigRequiresActivation)); 235 EXPECT_TRUE(Configure(kConfigRequiresActivation));
187 Connect("cellular1"); 236 Connect("cellular1");
188 EXPECT_EQ(NetworkConnectionHandler::kErrorActivationRequired, 237 EXPECT_EQ(NetworkConnectionHandler::kErrorActivationRequired,
189 GetResultAndReset()); 238 GetResultAndReset());
190 } 239 }
191 240
192 namespace { 241 namespace {
193 242
194 const char* kConfigRequiresCertificate = 243 const char* kConfigRequiresCertificateTemplate =
195 "{ \"GUID\": \"wifi4\", \"Type\": \"wifi\", \"Connectable\": false," 244 "{ \"GUID\": \"wifi4\", \"Type\": \"wifi\", \"Connectable\": false,"
196 " \"Security\": \"802_1x\"," 245 " \"Security\": \"802_1x\","
197 " \"UIData\": \"{" 246 " \"UIData\": \"{"
198 " \\\"certificate_type\\\": \\\"pattern\\\"," 247 " \\\"certificate_type\\\": \\\"pattern\\\","
199 " \\\"certificate_pattern\\\": {" 248 " \\\"certificate_pattern\\\": {"
200 " \\\"Subject\\\": { \\\"CommonName\\\": \\\"Foo\\\" }" 249 " \\\"Subject\\\": {\\\"CommonName\\\": \\\"%s\\\" }"
201 " } }\" }"; 250 " } }\" }";
202 251
203 } // namespace 252 } // namespace
204 253
205 // Handle certificates. TODO(stevenjb): Add certificate stubs to improve 254 // Handle certificates.
206 // test coverage. 255 TEST_F(NetworkConnectionHandlerTest, ConnectCertificateMissing) {
207 TEST_F(NetworkConnectionHandlerTest, 256 StartCertLoader();
208 NetworkConnectionHandlerConnectCertificate) { 257
209 EXPECT_TRUE(Configure(kConfigRequiresCertificate)); 258 EXPECT_TRUE(Configure(
259 base::StringPrintf(kConfigRequiresCertificateTemplate, "unknown")));
260 Connect("wifi4");
261 EXPECT_EQ(NetworkConnectionHandler::kErrorCertificateRequired,
262 GetResultAndReset());
263 }
264
265 TEST_F(NetworkConnectionHandlerTest, ConnectWithCertificateSuccess) {
266 StartCertLoader();
267
268 net::CertificateList certs;
269 ASSERT_TRUE(ImportClientCertAndKey(
270 "websocket_client_cert.p12", test_nssdb_.get(), &certs));
271 ASSERT_EQ(1U, certs.size());
272
273 EXPECT_TRUE(Configure(
274 base::StringPrintf(kConfigRequiresCertificateTemplate,
275 certs[0]->subject().common_name.c_str())));
276
277 Connect("wifi4");
278 EXPECT_EQ(kSuccessResult, GetResultAndReset());
279 }
280
281 TEST_F(NetworkConnectionHandlerTest, ConnectCertificateFromSecondaryUserFails) {
282 StartCertLoader();
283
284 // Create secondary user and get it's nssdb.
285 crypto::ScopedTestNSSChromeOSUser secondary_user("secondary");
286 ASSERT_TRUE(secondary_user.constructed_successfully());
287 secondary_user.FinishInit();
288
289 scoped_ptr<net::NSSCertDatabaseChromeOS>secondary_nssdb(
290 new net::NSSCertDatabaseChromeOS(
291 crypto::GetPublicSlotForChromeOSUser(secondary_user.username_hash()),
292 crypto::GetPrivateSlotForChromeOSUser(
293 secondary_user.username_hash(),
294 base::Callback<void(crypto::ScopedPK11Slot)>())));
295
296 // Import client cert to the secondary user's nssdb. The certificate should
297 // not be visible to the cert loader, so the connection request should fail.
298 net::CertificateList certs;
299 ASSERT_TRUE(ImportClientCertAndKey(
300 "websocket_client_cert.p12", secondary_nssdb.get(), &certs));
301 ASSERT_EQ(1U, certs.size());
302
303 EXPECT_TRUE(Configure(
304 base::StringPrintf(kConfigRequiresCertificateTemplate,
305 certs[0]->subject().common_name.c_str())));
306
210 Connect("wifi4"); 307 Connect("wifi4");
211 EXPECT_EQ(NetworkConnectionHandler::kErrorCertificateRequired, 308 EXPECT_EQ(NetworkConnectionHandler::kErrorCertificateRequired,
212 GetResultAndReset()); 309 GetResultAndReset());
213 } 310 }
214 311
215 TEST_F(NetworkConnectionHandlerTest, 312 TEST_F(NetworkConnectionHandlerTest,
313 ConnectWithCertificateRequestedBeforeCertsAreLoaded) {
314 net::CertificateList certs;
315 ASSERT_TRUE(ImportClientCertAndKey(
316 "websocket_client_cert.p12", test_nssdb_.get(), &certs));
317 ASSERT_EQ(1U, certs.size());
318
319 EXPECT_TRUE(Configure(
320 base::StringPrintf(kConfigRequiresCertificateTemplate,
321 certs[0]->subject().common_name.c_str())));
322
323 Connect("wifi4");
324
325 // Connect request came before the cert loader loaded certificates, so the
326 // connect request should have been throttled until the certificates are
327 // loaded.
328 EXPECT_EQ("", GetResultAndReset());
329
330 StartCertLoader();
331
332 // |StartCertLoader| should have triggered certificate loading.
333 // When the certificates got loaded, the connection request should have
334 // proceeded and eventually succeeded.
335 EXPECT_EQ(kSuccessResult, GetResultAndReset());
336 }
337
338 TEST_F(NetworkConnectionHandlerTest,
216 NetworkConnectionHandlerDisconnectSuccess) { 339 NetworkConnectionHandlerDisconnectSuccess) {
217 EXPECT_TRUE(Configure(kConfigConnected)); 340 EXPECT_TRUE(Configure(kConfigConnected));
218 Disconnect("wifi1"); 341 Disconnect("wifi1");
219 EXPECT_EQ(kSuccessResult, GetResultAndReset()); 342 EXPECT_EQ(kSuccessResult, GetResultAndReset());
220 } 343 }
221 344
222 TEST_F(NetworkConnectionHandlerTest, 345 TEST_F(NetworkConnectionHandlerTest,
223 NetworkConnectionHandlerDisconnectFailure) { 346 NetworkConnectionHandlerDisconnectFailure) {
224 Connect("no-network"); 347 Connect("no-network");
225 EXPECT_EQ(NetworkConnectionHandler::kErrorConfigureFailed, 348 EXPECT_EQ(NetworkConnectionHandler::kErrorConfigureFailed,
226 GetResultAndReset()); 349 GetResultAndReset());
227 350
228 EXPECT_TRUE(Configure(kConfigConnectable)); 351 EXPECT_TRUE(Configure(kConfigConnectable));
229 Disconnect("wifi0"); 352 Disconnect("wifi0");
230 EXPECT_EQ(NetworkConnectionHandler::kErrorNotConnected, GetResultAndReset()); 353 EXPECT_EQ(NetworkConnectionHandler::kErrorNotConnected, GetResultAndReset());
231 } 354 }
232 355
233 } // namespace chromeos 356 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698