OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 | 5 #include "base/bind.h" |
| 6 #include "base/bind_helpers.h" |
6 #include "base/callback.h" | 7 #include "base/callback.h" |
7 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
9 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
10 #include "base/values.h" | 11 #include "base/values.h" |
11 #include "chrome/browser/chromeos/login/user.h" | 12 #include "chrome/browser/chromeos/login/user.h" |
12 #include "chrome/browser/chromeos/policy/policy_cert_verifier.h" | |
13 #include "chrome/browser/chromeos/policy/user_network_configuration_updater.h" | 13 #include "chrome/browser/chromeos/policy/user_network_configuration_updater.h" |
14 #include "chrome/browser/policy/external_data_fetcher.h" | 14 #include "chrome/browser/policy/external_data_fetcher.h" |
15 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 15 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
16 #include "chrome/browser/policy/policy_map.h" | 16 #include "chrome/browser/policy/policy_map.h" |
17 #include "chrome/browser/policy/policy_service_impl.h" | 17 #include "chrome/browser/policy/policy_service_impl.h" |
18 #include "chromeos/network/mock_managed_network_configuration_handler.h" | 18 #include "chromeos/network/mock_managed_network_configuration_handler.h" |
19 #include "chromeos/network/onc/mock_certificate_importer.h" | 19 #include "chromeos/network/onc/mock_certificate_importer.h" |
20 #include "chromeos/network/onc/onc_test_utils.h" | 20 #include "chromeos/network/onc/onc_test_utils.h" |
21 #include "chromeos/network/onc/onc_utils.h" | 21 #include "chromeos/network/onc/onc_utils.h" |
22 #include "components/onc/onc_constants.h" | 22 #include "components/onc/onc_constants.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 | 54 |
55 // User overrides | 55 // User overrides |
56 virtual UserType GetType() const OVERRIDE { | 56 virtual UserType GetType() const OVERRIDE { |
57 return USER_TYPE_REGULAR; | 57 return USER_TYPE_REGULAR; |
58 } | 58 } |
59 | 59 |
60 private: | 60 private: |
61 DISALLOW_COPY_AND_ASSIGN(FakeUser); | 61 DISALLOW_COPY_AND_ASSIGN(FakeUser); |
62 }; | 62 }; |
63 | 63 |
| 64 class FakeWebTrustedCertsObserver |
| 65 : public UserNetworkConfigurationUpdater::WebTrustedCertsObserver { |
| 66 public: |
| 67 virtual void OnTrustAnchorsChanged( |
| 68 const net::CertificateList& trust_anchors) OVERRIDE { |
| 69 trust_anchors_ = trust_anchors; |
| 70 } |
| 71 net::CertificateList trust_anchors_; |
| 72 }; |
| 73 |
64 const char kFakeONC[] = | 74 const char kFakeONC[] = |
65 "{ \"NetworkConfigurations\": [" | 75 "{ \"NetworkConfigurations\": [" |
66 " { \"GUID\": \"{485d6076-dd44-6b6d-69787465725f5040}\"," | 76 " { \"GUID\": \"{485d6076-dd44-6b6d-69787465725f5040}\"," |
67 " \"Type\": \"WiFi\"," | 77 " \"Type\": \"WiFi\"," |
68 " \"Name\": \"My WiFi Network\"," | 78 " \"Name\": \"My WiFi Network\"," |
69 " \"WiFi\": {" | 79 " \"WiFi\": {" |
70 " \"SSID\": \"ssid-none\"," | 80 " \"SSID\": \"ssid-none\"," |
71 " \"Security\": \"None\" }" | 81 " \"Security\": \"None\" }" |
72 " }" | 82 " }" |
73 " ]," | 83 " ]," |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 | 209 |
200 StrictMock<MockConfigurationPolicyProvider> provider_; | 210 StrictMock<MockConfigurationPolicyProvider> provider_; |
201 scoped_ptr<PolicyServiceImpl> policy_service_; | 211 scoped_ptr<PolicyServiceImpl> policy_service_; |
202 FakeUser fake_user_; | 212 FakeUser fake_user_; |
203 | 213 |
204 scoped_ptr<NetworkConfigurationUpdater> network_configuration_updater_; | 214 scoped_ptr<NetworkConfigurationUpdater> network_configuration_updater_; |
205 content::TestBrowserThreadBundle thread_bundle_; | 215 content::TestBrowserThreadBundle thread_bundle_; |
206 }; | 216 }; |
207 | 217 |
208 TEST_F(NetworkConfigurationUpdaterTest, PolicyIsValidatedAndRepaired) { | 218 TEST_F(NetworkConfigurationUpdaterTest, PolicyIsValidatedAndRepaired) { |
209 std::string onc_policy = | |
210 chromeos::onc::test_utils::ReadTestData("toplevel_partially_invalid.onc"); | |
211 | |
212 scoped_ptr<base::DictionaryValue> onc_repaired = | 219 scoped_ptr<base::DictionaryValue> onc_repaired = |
213 chromeos::onc::test_utils::ReadTestDictionary( | 220 chromeos::onc::test_utils::ReadTestDictionary( |
214 "repaired_toplevel_partially_invalid.onc"); | 221 "repaired_toplevel_partially_invalid.onc"); |
215 | 222 |
216 base::ListValue* network_configs_repaired = NULL; | 223 base::ListValue* network_configs_repaired = NULL; |
217 onc_repaired->GetListWithoutPathExpansion( | 224 onc_repaired->GetListWithoutPathExpansion( |
218 onc::toplevel_config::kNetworkConfigurations, &network_configs_repaired); | 225 onc::toplevel_config::kNetworkConfigurations, &network_configs_repaired); |
219 ASSERT_TRUE(network_configs_repaired); | 226 ASSERT_TRUE(network_configs_repaired); |
220 | 227 |
221 base::DictionaryValue* global_config_repaired = NULL; | 228 base::DictionaryValue* global_config_repaired = NULL; |
222 onc_repaired->GetDictionaryWithoutPathExpansion( | 229 onc_repaired->GetDictionaryWithoutPathExpansion( |
223 onc::toplevel_config::kGlobalNetworkConfiguration, | 230 onc::toplevel_config::kGlobalNetworkConfiguration, |
224 &global_config_repaired); | 231 &global_config_repaired); |
225 ASSERT_TRUE(global_config_repaired); | 232 ASSERT_TRUE(global_config_repaired); |
226 | 233 |
| 234 std::string onc_policy = |
| 235 chromeos::onc::test_utils::ReadTestData("toplevel_partially_invalid.onc"); |
227 PolicyMap policy; | 236 PolicyMap policy; |
228 policy.Set(key::kOpenNetworkConfiguration, | 237 policy.Set(key::kOpenNetworkConfiguration, |
229 POLICY_LEVEL_MANDATORY, | 238 POLICY_LEVEL_MANDATORY, |
230 POLICY_SCOPE_USER, | 239 POLICY_SCOPE_USER, |
231 new base::StringValue(onc_policy), | 240 new base::StringValue(onc_policy), |
232 NULL); | 241 NULL); |
233 UpdateProviderPolicy(policy); | 242 UpdateProviderPolicy(policy); |
234 | 243 |
235 EXPECT_CALL(network_config_handler_, | 244 EXPECT_CALL(network_config_handler_, |
236 SetPolicy(onc::ONC_SOURCE_USER_POLICY, | 245 SetPolicy(onc::ONC_SOURCE_USER_POLICY, |
(...skipping 18 matching lines...) Expand all Loading... |
255 | 264 |
256 EXPECT_CALL(network_config_handler_, | 265 EXPECT_CALL(network_config_handler_, |
257 SetPolicy(onc::ONC_SOURCE_USER_POLICY, _, _, _)); | 266 SetPolicy(onc::ONC_SOURCE_USER_POLICY, _, _, _)); |
258 EXPECT_CALL(*certificate_importer_, ImportCertificates(_, _, _)) | 267 EXPECT_CALL(*certificate_importer_, ImportCertificates(_, _, _)) |
259 .WillRepeatedly(SetCertificateList(cert_list)); | 268 .WillRepeatedly(SetCertificateList(cert_list)); |
260 | 269 |
261 UserNetworkConfigurationUpdater* updater = | 270 UserNetworkConfigurationUpdater* updater = |
262 CreateNetworkConfigurationUpdaterForUserPolicy( | 271 CreateNetworkConfigurationUpdaterForUserPolicy( |
263 false /* do not allow trusted certs from policy */); | 272 false /* do not allow trusted certs from policy */); |
264 | 273 |
265 // Certificates with the "Web" trust flag set should not be forwarded to the | 274 // Certificates with the "Web" trust flag set should not be forwarded to |
266 // trust provider. | 275 // observers. |
267 policy::PolicyCertVerifier cert_verifier(( | 276 FakeWebTrustedCertsObserver observer; |
268 base::Closure() /* no policy cert trusted callback */)); | 277 updater->AddTrustedCertsObserver(&observer); |
269 updater->SetPolicyCertVerifier(&cert_verifier); | 278 |
270 base::RunLoop().RunUntilIdle(); | 279 base::RunLoop().RunUntilIdle(); |
271 EXPECT_TRUE(cert_verifier.GetAdditionalTrustAnchors().empty()); | |
272 | 280 |
273 // |cert_verifier| must outlive the updater. | 281 net::CertificateList trust_anchors; |
274 network_configuration_updater_.reset(); | 282 updater->GetWebTrustedCertificates(&trust_anchors); |
| 283 EXPECT_TRUE(trust_anchors.empty()); |
| 284 |
| 285 EXPECT_TRUE(observer.trust_anchors_.empty()); |
| 286 updater->RemoveTrustedCertsObserver(&observer); |
275 } | 287 } |
276 | 288 |
277 TEST_F(NetworkConfigurationUpdaterTest, AllowTrustedCertificatesFromPolicy) { | 289 TEST_F(NetworkConfigurationUpdaterTest, |
| 290 AllowTrustedCertificatesFromPolicyInitially) { |
| 291 // Ignore network configuration changes. |
| 292 EXPECT_CALL(network_config_handler_, SetPolicy(_, _, _, _)) |
| 293 .Times(AnyNumber()); |
| 294 |
278 net::CertificateList cert_list; | 295 net::CertificateList cert_list; |
279 cert_list = | 296 cert_list = |
280 net::CreateCertificateListFromFile(net::GetTestCertsDirectory(), | 297 net::CreateCertificateListFromFile(net::GetTestCertsDirectory(), |
281 "ok_cert.pem", | 298 "ok_cert.pem", |
282 net::X509Certificate::FORMAT_AUTO); | 299 net::X509Certificate::FORMAT_AUTO); |
283 ASSERT_EQ(1u, cert_list.size()); | 300 ASSERT_EQ(1u, cert_list.size()); |
284 | 301 |
285 EXPECT_CALL(network_config_handler_, | |
286 SetPolicy(onc::ONC_SOURCE_USER_POLICY, _, _, _)); | |
287 EXPECT_CALL(*certificate_importer_, | 302 EXPECT_CALL(*certificate_importer_, |
288 ImportCertificates(_, onc::ONC_SOURCE_USER_POLICY, _)) | 303 ImportCertificates(_, onc::ONC_SOURCE_USER_POLICY, _)) |
289 .WillRepeatedly(SetCertificateList(cert_list)); | 304 .WillRepeatedly(SetCertificateList(cert_list)); |
290 | 305 |
291 UserNetworkConfigurationUpdater* updater = | 306 UserNetworkConfigurationUpdater* updater = |
292 CreateNetworkConfigurationUpdaterForUserPolicy( | 307 CreateNetworkConfigurationUpdaterForUserPolicy( |
293 true /* allow trusted certs from policy */); | 308 true /* allow trusted certs from policy */); |
294 | 309 |
295 // Certificates with the "Web" trust flag set should be forwarded to the | |
296 // trust provider. | |
297 policy::PolicyCertVerifier cert_verifier(( | |
298 base::Closure() /* no policy cert trusted callback */)); | |
299 updater->SetPolicyCertVerifier(&cert_verifier); | |
300 base::RunLoop().RunUntilIdle(); | 310 base::RunLoop().RunUntilIdle(); |
301 EXPECT_EQ(1u, cert_verifier.GetAdditionalTrustAnchors().size()); | |
302 | 311 |
303 // |cert_verifier| must outlive the updater. | 312 // Certificates with the "Web" trust flag set will be returned. |
304 network_configuration_updater_.reset(); | 313 net::CertificateList trust_anchors; |
| 314 updater->GetWebTrustedCertificates(&trust_anchors); |
| 315 EXPECT_EQ(1u, trust_anchors.size()); |
| 316 } |
| 317 |
| 318 TEST_F(NetworkConfigurationUpdaterTest, |
| 319 AllowTrustedCertificatesFromPolicyOnUpdate) { |
| 320 // Ignore network configuration changes. |
| 321 EXPECT_CALL(network_config_handler_, SetPolicy(_, _, _, _)) |
| 322 .Times(AnyNumber()); |
| 323 |
| 324 // Start with an empty certificate list. |
| 325 EXPECT_CALL(*certificate_importer_, |
| 326 ImportCertificates(_, onc::ONC_SOURCE_USER_POLICY, _)) |
| 327 .WillRepeatedly(SetCertificateList(net::CertificateList())); |
| 328 |
| 329 UserNetworkConfigurationUpdater* updater = |
| 330 CreateNetworkConfigurationUpdaterForUserPolicy( |
| 331 true /* allow trusted certs from policy */); |
| 332 |
| 333 FakeWebTrustedCertsObserver observer; |
| 334 updater->AddTrustedCertsObserver(&observer); |
| 335 |
| 336 base::RunLoop().RunUntilIdle(); |
| 337 |
| 338 // Verify that the returned certificate list is empty. |
| 339 Mock::VerifyAndClearExpectations(certificate_importer_); |
| 340 { |
| 341 net::CertificateList trust_anchors; |
| 342 updater->GetWebTrustedCertificates(&trust_anchors); |
| 343 EXPECT_TRUE(trust_anchors.empty()); |
| 344 } |
| 345 EXPECT_TRUE(observer.trust_anchors_.empty()); |
| 346 |
| 347 // Now use a non-empty certificate list to test the observer notification. |
| 348 net::CertificateList cert_list; |
| 349 cert_list = |
| 350 net::CreateCertificateListFromFile(net::GetTestCertsDirectory(), |
| 351 "ok_cert.pem", |
| 352 net::X509Certificate::FORMAT_AUTO); |
| 353 ASSERT_EQ(1u, cert_list.size()); |
| 354 |
| 355 EXPECT_CALL(*certificate_importer_, |
| 356 ImportCertificates(_, onc::ONC_SOURCE_USER_POLICY, _)) |
| 357 .WillOnce(SetCertificateList(cert_list)); |
| 358 |
| 359 // Change to any non-empty policy, so that updates are triggered. The actual |
| 360 // content of the policy is irrelevant. |
| 361 PolicyMap policy; |
| 362 policy.Set(key::kOpenNetworkConfiguration, |
| 363 POLICY_LEVEL_MANDATORY, |
| 364 POLICY_SCOPE_USER, |
| 365 new base::StringValue(kFakeONC), |
| 366 NULL); |
| 367 UpdateProviderPolicy(policy); |
| 368 base::RunLoop().RunUntilIdle(); |
| 369 |
| 370 // Certificates with the "Web" trust flag set will be returned and forwarded |
| 371 // to observers. |
| 372 { |
| 373 net::CertificateList trust_anchors; |
| 374 updater->GetWebTrustedCertificates(&trust_anchors); |
| 375 EXPECT_EQ(1u, trust_anchors.size()); |
| 376 } |
| 377 EXPECT_EQ(1u, observer.trust_anchors_.size()); |
| 378 |
| 379 updater->RemoveTrustedCertsObserver(&observer); |
305 } | 380 } |
306 | 381 |
307 class NetworkConfigurationUpdaterTestWithParam | 382 class NetworkConfigurationUpdaterTestWithParam |
308 : public NetworkConfigurationUpdaterTest, | 383 : public NetworkConfigurationUpdaterTest, |
309 public testing::WithParamInterface<const char*> { | 384 public testing::WithParamInterface<const char*> { |
310 protected: | 385 protected: |
311 // Returns the currently tested ONC source. | 386 // Returns the currently tested ONC source. |
312 onc::ONCSource CurrentONCSource() { | 387 onc::ONCSource CurrentONCSource() { |
313 if (GetParam() == key::kOpenNetworkConfiguration) | 388 if (GetParam() == key::kOpenNetworkConfiguration) |
314 return onc::ONC_SOURCE_USER_POLICY; | 389 return onc::ONC_SOURCE_USER_POLICY; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 policy.Erase(GetParam()); | 463 policy.Erase(GetParam()); |
389 UpdateProviderPolicy(policy); | 464 UpdateProviderPolicy(policy); |
390 } | 465 } |
391 | 466 |
392 INSTANTIATE_TEST_CASE_P(NetworkConfigurationUpdaterTestWithParamInstance, | 467 INSTANTIATE_TEST_CASE_P(NetworkConfigurationUpdaterTestWithParamInstance, |
393 NetworkConfigurationUpdaterTestWithParam, | 468 NetworkConfigurationUpdaterTestWithParam, |
394 testing::Values(key::kDeviceOpenNetworkConfiguration, | 469 testing::Values(key::kDeviceOpenNetworkConfiguration, |
395 key::kOpenNetworkConfiguration)); | 470 key::kOpenNetworkConfiguration)); |
396 | 471 |
397 } // namespace policy | 472 } // namespace policy |
OLD | NEW |