Only send _NET_ACTIVE_WINDOW hint if the chromium window is not already active.
[chromium-blink-merge.git] / chromeos / network / client_cert_resolver_unittest.cc
blobe3816d0c0ec2fa502fffb00cf086153124d6452d
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 #include "chromeos/network/client_cert_resolver.h"
6 #include <cert.h>
7 #include <pk11pub.h>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/json/json_reader.h"
14 #include "base/run_loop.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/values.h"
17 #include "chromeos/cert_loader.h"
18 #include "chromeos/dbus/dbus_thread_manager.h"
19 #include "chromeos/dbus/shill_manager_client.h"
20 #include "chromeos/dbus/shill_profile_client.h"
21 #include "chromeos/dbus/shill_service_client.h"
22 #include "chromeos/network/managed_network_configuration_handler_impl.h"
23 #include "chromeos/network/network_configuration_handler.h"
24 #include "chromeos/network/network_profile_handler.h"
25 #include "chromeos/network/network_state_handler.h"
26 #include "components/onc/onc_constants.h"
27 #include "crypto/scoped_nss_types.h"
28 #include "crypto/scoped_test_nss_db.h"
29 #include "net/base/net_errors.h"
30 #include "net/base/test_data_directory.h"
31 #include "net/cert/nss_cert_database_chromeos.h"
32 #include "net/cert/x509_certificate.h"
33 #include "net/test/cert_test_util.h"
34 #include "testing/gtest/include/gtest/gtest.h"
35 #include "third_party/cros_system_api/dbus/service_constants.h"
37 namespace chromeos {
39 namespace {
41 const char* kWifiStub = "wifi_stub";
42 const char* kWifiSSID = "wifi_ssid";
43 const char* kUserProfilePath = "user_profile";
44 const char* kUserHash = "user_hash";
46 } // namespace
48 class ClientCertResolverTest : public testing::Test,
49 public ClientCertResolver::Observer {
50 public:
51 ClientCertResolverTest()
52 : network_properties_changed_count_(0),
53 service_test_(NULL),
54 profile_test_(NULL),
55 cert_loader_(NULL) {}
56 ~ClientCertResolverTest() override {}
58 void SetUp() override {
59 ASSERT_TRUE(test_nssdb_.is_open());
61 // Use the same DB for public and private slot.
62 test_nsscertdb_.reset(new net::NSSCertDatabaseChromeOS(
63 crypto::ScopedPK11Slot(PK11_ReferenceSlot(test_nssdb_.slot())),
64 crypto::ScopedPK11Slot(PK11_ReferenceSlot(test_nssdb_.slot()))));
65 test_nsscertdb_->SetSlowTaskRunnerForTest(message_loop_.task_runner());
67 DBusThreadManager::Initialize();
68 service_test_ =
69 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface();
70 profile_test_ =
71 DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface();
72 profile_test_->AddProfile(kUserProfilePath, kUserHash);
73 base::RunLoop().RunUntilIdle();
74 service_test_->ClearServices();
75 base::RunLoop().RunUntilIdle();
77 CertLoader::Initialize();
78 cert_loader_ = CertLoader::Get();
79 CertLoader::ForceHardwareBackedForTesting();
82 void TearDown() override {
83 client_cert_resolver_->RemoveObserver(this);
84 client_cert_resolver_.reset();
85 managed_config_handler_.reset();
86 network_config_handler_.reset();
87 network_profile_handler_.reset();
88 network_state_handler_.reset();
89 CertLoader::Shutdown();
90 DBusThreadManager::Shutdown();
93 protected:
94 void StartCertLoader() {
95 cert_loader_->StartWithNSSDB(test_nsscertdb_.get());
96 if (test_client_cert_.get()) {
97 int slot_id = 0;
98 const std::string pkcs11_id =
99 CertLoader::GetPkcs11IdAndSlotForCert(*test_client_cert_, &slot_id);
100 test_cert_id_ = base::StringPrintf("%i:%s", slot_id, pkcs11_id.c_str());
104 // Imports a CA cert (stored as PEM in test_ca_cert_pem_) and a client
105 // certificate signed by that CA. Its PKCS#11 ID is stored in
106 // |test_cert_id_|.
107 void SetupTestCerts() {
108 // Import a CA cert.
109 net::CertificateList ca_cert_list =
110 net::CreateCertificateListFromFile(net::GetTestCertsDirectory(),
111 "client_1_ca.pem",
112 net::X509Certificate::FORMAT_AUTO);
113 ASSERT_TRUE(!ca_cert_list.empty());
114 net::NSSCertDatabase::ImportCertFailureList failures;
115 EXPECT_TRUE(test_nsscertdb_->ImportCACerts(
116 ca_cert_list, net::NSSCertDatabase::TRUST_DEFAULT, &failures));
117 ASSERT_TRUE(failures.empty()) << net::ErrorToString(failures[0].net_error);
119 net::X509Certificate::GetPEMEncoded(ca_cert_list[0]->os_cert_handle(),
120 &test_ca_cert_pem_);
121 ASSERT_TRUE(!test_ca_cert_pem_.empty());
123 // Import a client cert signed by that CA.
124 test_client_cert_ =
125 net::ImportClientCertAndKeyFromFile(net::GetTestCertsDirectory(),
126 "client_1.pem",
127 "client_1.pk8",
128 test_nssdb_.slot());
129 ASSERT_TRUE(test_client_cert_.get());
132 void SetupNetworkHandlers() {
133 network_state_handler_.reset(NetworkStateHandler::InitializeForTest());
134 network_profile_handler_.reset(new NetworkProfileHandler());
135 network_config_handler_.reset(new NetworkConfigurationHandler());
136 managed_config_handler_.reset(new ManagedNetworkConfigurationHandlerImpl());
137 client_cert_resolver_.reset(new ClientCertResolver());
139 network_profile_handler_->Init();
140 network_config_handler_->Init(network_state_handler_.get(),
141 NULL /* network_device_handler */);
142 managed_config_handler_->Init(network_state_handler_.get(),
143 network_profile_handler_.get(),
144 network_config_handler_.get(),
145 NULL /* network_device_handler */);
146 // Run all notifications before starting the cert loader to reduce run time.
147 base::RunLoop().RunUntilIdle();
149 client_cert_resolver_->Init(network_state_handler_.get(),
150 managed_config_handler_.get());
151 client_cert_resolver_->AddObserver(this);
152 client_cert_resolver_->SetSlowTaskRunnerForTest(
153 message_loop_.task_runner());
156 void SetupWifi() {
157 service_test_->SetServiceProperties(kWifiStub,
158 kWifiStub,
159 kWifiSSID,
160 shill::kTypeWifi,
161 shill::kStateOnline,
162 true /* visible */);
163 // Set an arbitrary cert id, so that we can check afterwards whether we
164 // cleared the property or not.
165 service_test_->SetServiceProperty(
166 kWifiStub, shill::kEapCertIdProperty, base::StringValue("invalid id"));
167 profile_test_->AddService(kUserProfilePath, kWifiStub);
169 DBusThreadManager::Get()
170 ->GetShillManagerClient()
171 ->GetTestInterface()
172 ->AddManagerService(kWifiStub, true);
175 // Setup a policy with a certificate pattern that matches any client cert that
176 // is signed by the test CA cert (stored in |test_ca_cert_pem_|). In
177 // particular it will match the test client cert.
178 void SetupPolicy() {
179 const char* kTestPolicyTemplate =
180 "[ { \"GUID\": \"wifi_stub\","
181 " \"Name\": \"wifi_stub\","
182 " \"Type\": \"WiFi\","
183 " \"WiFi\": {"
184 " \"Security\": \"WPA-EAP\","
185 " \"SSID\": \"wifi_ssid\","
186 " \"EAP\": {"
187 " \"Outer\": \"EAP-TLS\","
188 " \"ClientCertType\": \"Pattern\","
189 " \"ClientCertPattern\": {"
190 " \"IssuerCAPEMs\": [ \"%s\" ]"
191 " }"
192 " }"
193 " }"
194 "} ]";
195 std::string policy_json =
196 base::StringPrintf(kTestPolicyTemplate, test_ca_cert_pem_.c_str());
198 std::string error;
199 scoped_ptr<base::Value> policy_value(
200 base::JSONReader::DeprecatedReadAndReturnError(
201 policy_json, base::JSON_ALLOW_TRAILING_COMMAS, NULL, &error));
202 ASSERT_TRUE(policy_value) << error;
204 base::ListValue* policy = NULL;
205 ASSERT_TRUE(policy_value->GetAsList(&policy));
207 managed_config_handler_->SetPolicy(
208 onc::ONC_SOURCE_USER_POLICY,
209 kUserHash,
210 *policy,
211 base::DictionaryValue() /* no global network config */);
214 void GetClientCertProperties(std::string* pkcs11_id) {
215 pkcs11_id->clear();
216 const base::DictionaryValue* properties =
217 service_test_->GetServiceProperties(kWifiStub);
218 if (!properties)
219 return;
220 properties->GetStringWithoutPathExpansion(shill::kEapCertIdProperty,
221 pkcs11_id);
224 int network_properties_changed_count_;
225 std::string test_cert_id_;
226 scoped_ptr<ClientCertResolver> client_cert_resolver_;
228 private:
229 // ClientCertResolver::Observer:
230 void ResolveRequestCompleted(bool network_properties_changed) override {
231 if (network_properties_changed)
232 ++network_properties_changed_count_;
235 ShillServiceClient::TestInterface* service_test_;
236 ShillProfileClient::TestInterface* profile_test_;
237 CertLoader* cert_loader_;
238 scoped_ptr<NetworkStateHandler> network_state_handler_;
239 scoped_ptr<NetworkProfileHandler> network_profile_handler_;
240 scoped_ptr<NetworkConfigurationHandler> network_config_handler_;
241 scoped_ptr<ManagedNetworkConfigurationHandlerImpl> managed_config_handler_;
242 base::MessageLoop message_loop_;
243 scoped_refptr<net::X509Certificate> test_client_cert_;
244 std::string test_ca_cert_pem_;
245 crypto::ScopedTestNSSDB test_nssdb_;
246 scoped_ptr<net::NSSCertDatabaseChromeOS> test_nsscertdb_;
248 DISALLOW_COPY_AND_ASSIGN(ClientCertResolverTest);
251 TEST_F(ClientCertResolverTest, NoMatchingCertificates) {
252 StartCertLoader();
253 SetupWifi();
254 base::RunLoop().RunUntilIdle();
255 network_properties_changed_count_ = 0;
256 SetupNetworkHandlers();
257 SetupPolicy();
258 base::RunLoop().RunUntilIdle();
260 // Verify that no client certificate was configured.
261 std::string pkcs11_id;
262 GetClientCertProperties(&pkcs11_id);
263 EXPECT_EQ(std::string(), pkcs11_id);
264 EXPECT_EQ(1, network_properties_changed_count_);
265 EXPECT_FALSE(client_cert_resolver_->IsAnyResolveTaskRunning());
268 TEST_F(ClientCertResolverTest, ResolveOnCertificatesLoaded) {
269 SetupTestCerts();
270 SetupWifi();
271 base::RunLoop().RunUntilIdle();
273 SetupNetworkHandlers();
274 SetupPolicy();
275 base::RunLoop().RunUntilIdle();
277 network_properties_changed_count_ = 0;
278 StartCertLoader();
279 base::RunLoop().RunUntilIdle();
281 // Verify that the resolver positively matched the pattern in the policy with
282 // the test client cert and configured the network.
283 std::string pkcs11_id;
284 GetClientCertProperties(&pkcs11_id);
285 EXPECT_EQ(test_cert_id_, pkcs11_id);
286 EXPECT_EQ(1, network_properties_changed_count_);
289 TEST_F(ClientCertResolverTest, ResolveAfterPolicyApplication) {
290 SetupTestCerts();
291 SetupWifi();
292 base::RunLoop().RunUntilIdle();
293 StartCertLoader();
294 SetupNetworkHandlers();
295 base::RunLoop().RunUntilIdle();
297 // Policy application will trigger the ClientCertResolver.
298 network_properties_changed_count_ = 0;
299 SetupPolicy();
300 base::RunLoop().RunUntilIdle();
302 // Verify that the resolver positively matched the pattern in the policy with
303 // the test client cert and configured the network.
304 std::string pkcs11_id;
305 GetClientCertProperties(&pkcs11_id);
306 EXPECT_EQ(test_cert_id_, pkcs11_id);
307 EXPECT_EQ(1, network_properties_changed_count_);
310 } // namespace chromeos