| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/user_policy_cache.h" | 5 #include "chrome/browser/policy/user_policy_cache.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/scoped_temp_dir.h" | 12 #include "base/scoped_temp_dir.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/policy/configuration_policy_provider.h" | 14 #include "chrome/browser/policy/configuration_policy_provider.h" |
| 15 #include "chrome/browser/policy/proto/cloud_policy.pb.h" | 15 #include "chrome/browser/policy/proto/cloud_policy.pb.h" |
| 16 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 16 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 17 #include "chrome/browser/policy/proto/device_management_local.pb.h" | 17 #include "chrome/browser/policy/proto/device_management_local.pb.h" |
| 18 #include "chrome/browser/policy/proto/old_generic_format.pb.h" | 18 #include "chrome/browser/policy/proto/old_generic_format.pb.h" |
| 19 #include "content/browser/browser_thread.h" | 19 #include "content/browser/browser_thread.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 22 |
| 23 using testing::_; |
| 24 |
| 23 namespace policy { | 25 namespace policy { |
| 24 | 26 |
| 25 // Decodes a CloudPolicySettings object into two maps with mandatory and | 27 // Decodes a CloudPolicySettings object into two maps with mandatory and |
| 26 // recommended settings, respectively. The implementation is generated code | 28 // recommended settings, respectively. The implementation is generated code |
| 27 // in policy/cloud_policy_generated.cc. | 29 // in policy/cloud_policy_generated.cc. |
| 28 void DecodePolicy(const em::CloudPolicySettings& policy, | 30 void DecodePolicy(const em::CloudPolicySettings& policy, |
| 29 PolicyMap* mandatory, PolicyMap* recommended); | 31 PolicyMap* mandatory, PolicyMap* recommended); |
| 30 | 32 |
| 31 // The implementations of these methods are in cloud_policy_generated.cc. | 33 // The implementations of these methods are in cloud_policy_generated.cc. |
| 32 Value* DecodeIntegerValue(google::protobuf::int64 value); | 34 Value* DecodeIntegerValue(google::protobuf::int64 value); |
| 33 ListValue* DecodeStringList(const em::StringList& string_list); | 35 ListValue* DecodeStringList(const em::StringList& string_list); |
| 34 | 36 |
| 35 class MockConfigurationPolicyProviderObserver | 37 class MockCloudPolicyCacheBaseObserver |
| 36 : public ConfigurationPolicyProvider::Observer { | 38 : public CloudPolicyCacheBase::Observer { |
| 37 public: | 39 public: |
| 38 MockConfigurationPolicyProviderObserver() {} | 40 MockCloudPolicyCacheBaseObserver() {} |
| 39 virtual ~MockConfigurationPolicyProviderObserver() {} | 41 virtual ~MockCloudPolicyCacheBaseObserver() {} |
| 40 MOCK_METHOD0(OnUpdatePolicy, void()); | 42 MOCK_METHOD1(OnCacheUpdate, void(CloudPolicyCacheBase*)); |
| 41 void OnProviderGoingAway() {} | 43 void OnCacheGoingAway(CloudPolicyCacheBase*) {} |
| 42 }; | 44 }; |
| 43 | 45 |
| 44 // Tests the device management policy cache. | 46 // Tests the device management policy cache. |
| 45 class UserPolicyCacheTest : public testing::Test { | 47 class UserPolicyCacheTest : public testing::Test { |
| 46 protected: | 48 protected: |
| 47 UserPolicyCacheTest() | 49 UserPolicyCacheTest() |
| 48 : loop_(MessageLoop::TYPE_UI), | 50 : loop_(MessageLoop::TYPE_UI), |
| 49 ui_thread_(BrowserThread::UI, &loop_), | 51 ui_thread_(BrowserThread::UI, &loop_), |
| 50 file_thread_(BrowserThread::FILE, &loop_) {} | 52 file_thread_(BrowserThread::FILE, &loop_) {} |
| 51 | 53 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 EXPECT_TRUE(cached_policy.SerializeToString(&data)); | 98 EXPECT_TRUE(cached_policy.SerializeToString(&data)); |
| 97 int size = static_cast<int>(data.size()); | 99 int size = static_cast<int>(data.size()); |
| 98 EXPECT_EQ(size, file_util::WriteFile(test_file(), data.c_str(), size)); | 100 EXPECT_EQ(size, file_util::WriteFile(test_file(), data.c_str(), size)); |
| 99 } | 101 } |
| 100 | 102 |
| 101 // Takes ownership of |policy_response|. | 103 // Takes ownership of |policy_response|. |
| 102 void SetPolicy(UserPolicyCache* cache, | 104 void SetPolicy(UserPolicyCache* cache, |
| 103 em::PolicyFetchResponse* policy_response, | 105 em::PolicyFetchResponse* policy_response, |
| 104 bool expect_changed_policy) { | 106 bool expect_changed_policy) { |
| 105 scoped_ptr<em::PolicyFetchResponse> policy(policy_response); | 107 scoped_ptr<em::PolicyFetchResponse> policy(policy_response); |
| 106 ConfigurationPolicyObserverRegistrar registrar; | 108 cache->AddObserver(&observer); |
| 107 registrar.Init(cache->GetManagedPolicyProvider(), &observer); | 109 |
| 108 if (expect_changed_policy) | 110 if (expect_changed_policy) |
| 109 EXPECT_CALL(observer, OnUpdatePolicy()).Times(1); | 111 EXPECT_CALL(observer, OnCacheUpdate(_)).Times(1); |
| 110 else | 112 else |
| 111 EXPECT_CALL(observer, OnUpdatePolicy()).Times(0); | 113 EXPECT_CALL(observer, OnCacheUpdate(_)).Times(0); |
| 112 cache->SetPolicy(*policy); | 114 cache->SetPolicy(*policy); |
| 113 testing::Mock::VerifyAndClearExpectations(&observer); | 115 testing::Mock::VerifyAndClearExpectations(&observer); |
| 116 |
| 117 cache->RemoveObserver(&observer); |
| 114 } | 118 } |
| 115 | 119 |
| 116 FilePath test_file() { | 120 FilePath test_file() { |
| 117 return temp_dir_.path().AppendASCII("UserPolicyCacheTest"); | 121 return temp_dir_.path().AppendASCII("UserPolicyCacheTest"); |
| 118 } | 122 } |
| 119 | 123 |
| 120 const PolicyMap& mandatory_policy(const UserPolicyCache& cache) { | 124 const PolicyMap& mandatory_policy(const UserPolicyCache& cache) { |
| 121 return cache.mandatory_policy_; | 125 return cache.mandatory_policy_; |
| 122 } | 126 } |
| 123 | 127 |
| 124 const PolicyMap& recommended_policy(const UserPolicyCache& cache) { | 128 const PolicyMap& recommended_policy(const UserPolicyCache& cache) { |
| 125 return cache.recommended_policy_; | 129 return cache.recommended_policy_; |
| 126 } | 130 } |
| 127 | 131 |
| 128 MessageLoop loop_; | 132 MessageLoop loop_; |
| 129 MockConfigurationPolicyProviderObserver observer; | 133 MockCloudPolicyCacheBaseObserver observer; |
| 130 | 134 |
| 131 private: | 135 private: |
| 132 ScopedTempDir temp_dir_; | 136 ScopedTempDir temp_dir_; |
| 133 BrowserThread ui_thread_; | 137 BrowserThread ui_thread_; |
| 134 BrowserThread file_thread_; | 138 BrowserThread file_thread_; |
| 135 }; | 139 }; |
| 136 | 140 |
| 137 TEST_F(UserPolicyCacheTest, DecodePolicy) { | 141 TEST_F(UserPolicyCacheTest, DecodePolicy) { |
| 138 em::CloudPolicySettings settings; | 142 em::CloudPolicySettings settings; |
| 139 settings.mutable_homepagelocation()->set_homepagelocation("chromium.org"); | 143 settings.mutable_homepagelocation()->set_homepagelocation("chromium.org"); |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 // If new-style policy comes in, it should override old-style policy. | 377 // If new-style policy comes in, it should override old-style policy. |
| 374 policy = CreateHomepagePolicy("http://www.example.com", | 378 policy = CreateHomepagePolicy("http://www.example.com", |
| 375 base::Time::NowFromSystemTime(), | 379 base::Time::NowFromSystemTime(), |
| 376 em::PolicyOptions::RECOMMENDED); | 380 em::PolicyOptions::RECOMMENDED); |
| 377 SetPolicy(&cache, policy, true); | 381 SetPolicy(&cache, policy, true); |
| 378 EXPECT_TRUE(expected.Equals(recommended_policy(cache))); | 382 EXPECT_TRUE(expected.Equals(recommended_policy(cache))); |
| 379 EXPECT_TRUE(empty.Equals(mandatory_policy(cache))); | 383 EXPECT_TRUE(empty.Equals(mandatory_policy(cache))); |
| 380 } | 384 } |
| 381 | 385 |
| 382 } // namespace policy | 386 } // namespace policy |
| OLD | NEW |