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

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

Powered by Google App Engine
This is Rietveld 408576698