OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #include "chrome/browser/policy/cloud/component_cloud_policy_service.h" | 5 #include "chrome/browser/policy/cloud/component_cloud_policy_service.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.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/sha1.h" | 14 #include "base/sha1.h" |
15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | 18 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" |
19 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" | 19 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" |
20 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" | 20 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" |
21 #include "chrome/browser/policy/cloud/policy_builder.h" | 21 #include "chrome/browser/policy/cloud/policy_builder.h" |
22 #include "chrome/browser/policy/cloud/resource_cache.h" | 22 #include "chrome/browser/policy/cloud/resource_cache.h" |
23 #include "chrome/browser/policy/external_data_fetcher.h" | 23 #include "chrome/browser/policy/external_data_fetcher.h" |
24 #include "chrome/browser/policy/policy_map.h" | 24 #include "chrome/browser/policy/policy_map.h" |
25 #include "chrome/browser/policy/policy_types.h" | 25 #include "chrome/browser/policy/policy_types.h" |
26 #include "chrome/browser/policy/proto/cloud/chrome_extension_policy.pb.h" | 26 #include "chrome/browser/policy/proto/cloud/chrome_extension_policy.pb.h" |
27 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" | 27 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" |
28 #include "chrome/browser/policy/schema_map.h" | 28 #include "chrome/browser/policy/schema_map.h" |
| 29 #include "chrome/browser/policy/test/policy_test_utils.h" |
29 #include "components/policy/core/common/schema.h" | 30 #include "components/policy/core/common/schema.h" |
30 #include "net/url_request/test_url_fetcher_factory.h" | 31 #include "net/url_request/test_url_fetcher_factory.h" |
31 #include "net/url_request/url_fetcher_delegate.h" | 32 #include "net/url_request/url_fetcher_delegate.h" |
32 #include "net/url_request/url_request_context.h" | 33 #include "net/url_request/url_request_context.h" |
33 #include "net/url_request/url_request_context_getter.h" | 34 #include "net/url_request/url_request_context_getter.h" |
34 #include "testing/gmock/include/gmock/gmock.h" | 35 #include "testing/gmock/include/gmock/gmock.h" |
35 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
36 | 37 |
37 namespace em = enterprise_management; | 38 namespace em = enterprise_management; |
38 | 39 |
(...skipping 11 matching lines...) Expand all Loading... |
50 "{" | 51 "{" |
51 " \"Name\": {" | 52 " \"Name\": {" |
52 " \"Value\": \"disabled\"" | 53 " \"Value\": \"disabled\"" |
53 " }," | 54 " }," |
54 " \"Second\": {" | 55 " \"Second\": {" |
55 " \"Value\": \"maybe\"," | 56 " \"Value\": \"maybe\"," |
56 " \"Level\": \"Recommended\"" | 57 " \"Level\": \"Recommended\"" |
57 " }" | 58 " }" |
58 "}"; | 59 "}"; |
59 | 60 |
| 61 const char kInvalidTestPolicy[] = |
| 62 "{" |
| 63 " \"Name\": {" |
| 64 " \"Value\": \"published\"" |
| 65 " }," |
| 66 " \"Undeclared Name\": {" |
| 67 " \"Value\": \"not published\"" |
| 68 " }" |
| 69 "}"; |
| 70 |
60 const char kTestSchema[] = | 71 const char kTestSchema[] = |
61 "{" | 72 "{" |
62 " \"type\": \"object\"," | 73 " \"type\": \"object\"," |
63 " \"properties\": {" | 74 " \"properties\": {" |
64 " \"Name\": { \"type\": \"string\" }," | 75 " \"Name\": { \"type\": \"string\" }," |
65 " \"Second\": { \"type\": \"string\" }" | 76 " \"Second\": { \"type\": \"string\" }" |
66 " }" | 77 " }" |
67 "}"; | 78 "}"; |
68 | 79 |
69 class MockComponentCloudPolicyDelegate | 80 class MockComponentCloudPolicyDelegate |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 store_.policy_.reset(); | 520 store_.policy_.reset(); |
510 store_.NotifyStoreLoaded(); | 521 store_.NotifyStoreLoaded(); |
511 RunUntilIdle(); | 522 RunUntilIdle(); |
512 Mock::VerifyAndClearExpectations(&delegate_); | 523 Mock::VerifyAndClearExpectations(&delegate_); |
513 const PolicyBundle empty_bundle; | 524 const PolicyBundle empty_bundle; |
514 EXPECT_TRUE(service_->policy().Equals(empty_bundle)); | 525 EXPECT_TRUE(service_->policy().Equals(empty_bundle)); |
515 cache_->LoadAllSubkeys("extension-policy", &contents); | 526 cache_->LoadAllSubkeys("extension-policy", &contents); |
516 ASSERT_EQ(0u, contents.size()); | 527 ASSERT_EQ(0u, contents.size()); |
517 } | 528 } |
518 | 529 |
| 530 TEST_F(ComponentCloudPolicyServiceTest, LoadInvalidPolicyFromCache) { |
| 531 // Put the invalid test policy in the cache. One of its policies will be |
| 532 // loaded, the other should be filtered out by the schema. |
| 533 builder_.payload().set_secure_hash(base::SHA1HashString(kInvalidTestPolicy)); |
| 534 EXPECT_TRUE(cache_->Store( |
| 535 "extension-policy", kTestExtension, CreateSerializedResponse())); |
| 536 EXPECT_TRUE(cache_->Store( |
| 537 "extension-policy-data", kTestExtension, kInvalidTestPolicy)); |
| 538 |
| 539 LoadStore(); |
| 540 InitializeRegistry(); |
| 541 |
| 542 // The initial, cached policy will be served once the backend is initialized. |
| 543 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); |
| 544 RunUntilIdle(); |
| 545 Mock::VerifyAndClearExpectations(&delegate_); |
| 546 |
| 547 PolicyBundle expected_bundle; |
| 548 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); |
| 549 expected_bundle.Get(ns).Set("Name", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 550 base::Value::CreateStringValue("published"), |
| 551 NULL); |
| 552 EXPECT_TRUE(service_->policy().Equals(expected_bundle)) << service_->policy(); |
| 553 } |
| 554 |
519 } // namespace policy | 555 } // namespace policy |
OLD | NEW |