OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/policy/user_policy_cache.h" |
| 6 |
| 7 #include <limits> |
| 8 #include <string> |
| 9 |
| 10 #include "base/file_util.h" |
| 11 #include "base/memory/scoped_temp_dir.h" |
| 12 #include "base/message_loop.h" |
| 13 #include "base/values.h" |
| 14 #include "chrome/browser/policy/configuration_policy_provider.h" |
| 15 #include "chrome/browser/policy/proto/cloud_policy.pb.h" |
| 16 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 17 #include "chrome/browser/policy/proto/device_management_local.pb.h" |
| 18 #include "content/browser/browser_thread.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 namespace policy { |
| 23 |
| 24 // Decodes a CloudPolicySettings object into two maps with mandatory and |
| 25 // recommended settings, respectively. The implementation is generated code |
| 26 // in policy/cloud_policy_generated.cc. |
| 27 void DecodePolicy(const em::CloudPolicySettings& policy, |
| 28 PolicyMap* mandatory, PolicyMap* recommended); |
| 29 |
| 30 // The implementations of these methods are in cloud_policy_generated.cc. |
| 31 Value* DecodeIntegerValue(google::protobuf::int64 value); |
| 32 ListValue* DecodeStringList(const em::StringList& string_list); |
| 33 |
| 34 class MockConfigurationPolicyProviderObserver |
| 35 : public ConfigurationPolicyProvider::Observer { |
| 36 public: |
| 37 MockConfigurationPolicyProviderObserver() {} |
| 38 virtual ~MockConfigurationPolicyProviderObserver() {} |
| 39 MOCK_METHOD0(OnUpdatePolicy, void()); |
| 40 void OnProviderGoingAway() {} |
| 41 }; |
| 42 |
| 43 // Tests the device management policy cache. |
| 44 class UserPolicyCacheTest : public testing::Test { |
| 45 protected: |
| 46 UserPolicyCacheTest() |
| 47 : loop_(MessageLoop::TYPE_UI), |
| 48 ui_thread_(BrowserThread::UI, &loop_), |
| 49 file_thread_(BrowserThread::FILE, &loop_) {} |
| 50 |
| 51 void SetUp() { |
| 52 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 53 } |
| 54 |
| 55 void TearDown() { |
| 56 loop_.RunAllPending(); |
| 57 } |
| 58 |
| 59 // Creates a (signed) PolicyFetchResponse setting the given |homepage| and |
| 60 // featuring the given |timestamp| (as issued by the server). |
| 61 // Mildly hacky special feature: pass an empty string as |homepage| to get |
| 62 // a completely empty policy. |
| 63 em::PolicyFetchResponse* CreateHomepagePolicy( |
| 64 const std::string& homepage, |
| 65 const base::Time& timestamp, |
| 66 const em::PolicyOptions::PolicyMode policy_mode) { |
| 67 em::PolicyData signed_response; |
| 68 if (homepage != "") { |
| 69 em::CloudPolicySettings settings; |
| 70 em::HomepageLocationProto* homepagelocation_proto = |
| 71 settings.mutable_homepagelocation(); |
| 72 homepagelocation_proto->set_homepagelocation(homepage); |
| 73 homepagelocation_proto->mutable_policy_options()->set_mode(policy_mode); |
| 74 EXPECT_TRUE( |
| 75 settings.SerializeToString(signed_response.mutable_policy_value())); |
| 76 } |
| 77 signed_response.set_timestamp( |
| 78 (timestamp - base::Time::UnixEpoch()).InMilliseconds()); |
| 79 std::string serialized_signed_response; |
| 80 EXPECT_TRUE(signed_response.SerializeToString(&serialized_signed_response)); |
| 81 |
| 82 em::PolicyFetchResponse* response = new em::PolicyFetchResponse; |
| 83 response->set_policy_data(serialized_signed_response); |
| 84 // TODO(jkummerow): Set proper new_public_key and signature (when |
| 85 // implementing support for signature verification). |
| 86 response->set_policy_data_signature("TODO"); |
| 87 response->set_new_public_key("TODO"); |
| 88 return response; |
| 89 } |
| 90 |
| 91 void WritePolicy(const em::PolicyFetchResponse& policy) { |
| 92 std::string data; |
| 93 em::CachedCloudPolicyResponse cached_policy; |
| 94 cached_policy.mutable_cloud_policy()->CopyFrom(policy); |
| 95 EXPECT_TRUE(cached_policy.SerializeToString(&data)); |
| 96 int size = static_cast<int>(data.size()); |
| 97 EXPECT_EQ(size, file_util::WriteFile(test_file(), data.c_str(), size)); |
| 98 } |
| 99 |
| 100 // Takes ownership of |policy_response|. |
| 101 void SetPolicy(UserPolicyCache* cache, |
| 102 em::PolicyFetchResponse* policy_response, |
| 103 bool expect_changed_policy) { |
| 104 scoped_ptr<em::PolicyFetchResponse> policy(policy_response); |
| 105 ConfigurationPolicyObserverRegistrar registrar; |
| 106 registrar.Init(cache->GetManagedPolicyProvider(), &observer); |
| 107 if (expect_changed_policy) |
| 108 EXPECT_CALL(observer, OnUpdatePolicy()).Times(1); |
| 109 else |
| 110 EXPECT_CALL(observer, OnUpdatePolicy()).Times(0); |
| 111 cache->SetPolicy(*policy); |
| 112 testing::Mock::VerifyAndClearExpectations(&observer); |
| 113 } |
| 114 |
| 115 FilePath test_file() { |
| 116 return temp_dir_.path().AppendASCII("UserPolicyCacheTest"); |
| 117 } |
| 118 |
| 119 const PolicyMap& mandatory_policy(const UserPolicyCache& cache) { |
| 120 return cache.mandatory_policy_; |
| 121 } |
| 122 |
| 123 const PolicyMap& recommended_policy(const UserPolicyCache& cache) { |
| 124 return cache.recommended_policy_; |
| 125 } |
| 126 |
| 127 MessageLoop loop_; |
| 128 MockConfigurationPolicyProviderObserver observer; |
| 129 |
| 130 private: |
| 131 ScopedTempDir temp_dir_; |
| 132 BrowserThread ui_thread_; |
| 133 BrowserThread file_thread_; |
| 134 }; |
| 135 |
| 136 TEST_F(UserPolicyCacheTest, DecodePolicy) { |
| 137 em::CloudPolicySettings settings; |
| 138 settings.mutable_homepagelocation()->set_homepagelocation("chromium.org"); |
| 139 settings.mutable_javascriptenabled()->set_javascriptenabled(true); |
| 140 settings.mutable_javascriptenabled()->mutable_policy_options()->set_mode( |
| 141 em::PolicyOptions::MANDATORY); |
| 142 settings.mutable_policyrefreshrate()->set_policyrefreshrate(5); |
| 143 settings.mutable_policyrefreshrate()->mutable_policy_options()->set_mode( |
| 144 em::PolicyOptions::RECOMMENDED); |
| 145 PolicyMap mandatory_policy; |
| 146 PolicyMap recommended_policy; |
| 147 DecodePolicy(settings, &mandatory_policy, &recommended_policy); |
| 148 PolicyMap mandatory; |
| 149 mandatory.Set(kPolicyHomepageLocation, |
| 150 Value::CreateStringValue("chromium.org")); |
| 151 mandatory.Set(kPolicyJavascriptEnabled, Value::CreateBooleanValue(true)); |
| 152 PolicyMap recommended; |
| 153 recommended.Set(kPolicyPolicyRefreshRate, Value::CreateIntegerValue(5)); |
| 154 EXPECT_TRUE(mandatory.Equals(mandatory_policy)); |
| 155 EXPECT_TRUE(recommended.Equals(recommended_policy)); |
| 156 } |
| 157 |
| 158 TEST_F(UserPolicyCacheTest, DecodeIntegerValue) { |
| 159 const int min = std::numeric_limits<int>::min(); |
| 160 const int max = std::numeric_limits<int>::max(); |
| 161 scoped_ptr<Value> value( |
| 162 DecodeIntegerValue(static_cast<google::protobuf::int64>(42))); |
| 163 ASSERT_TRUE(value.get()); |
| 164 FundamentalValue expected_42(42); |
| 165 EXPECT_TRUE(value->Equals(&expected_42)); |
| 166 value.reset( |
| 167 DecodeIntegerValue(static_cast<google::protobuf::int64>(min - 1LL))); |
| 168 EXPECT_EQ(NULL, value.get()); |
| 169 value.reset(DecodeIntegerValue(static_cast<google::protobuf::int64>(min))); |
| 170 ASSERT_TRUE(value.get()); |
| 171 FundamentalValue expected_min(min); |
| 172 EXPECT_TRUE(value->Equals(&expected_min)); |
| 173 value.reset( |
| 174 DecodeIntegerValue(static_cast<google::protobuf::int64>(max + 1LL))); |
| 175 EXPECT_EQ(NULL, value.get()); |
| 176 value.reset(DecodeIntegerValue(static_cast<google::protobuf::int64>(max))); |
| 177 ASSERT_TRUE(value.get()); |
| 178 FundamentalValue expected_max(max); |
| 179 EXPECT_TRUE(value->Equals(&expected_max)); |
| 180 } |
| 181 |
| 182 TEST_F(UserPolicyCacheTest, DecodeStringList) { |
| 183 em::StringList string_list; |
| 184 string_list.add_entries("ponies"); |
| 185 string_list.add_entries("more ponies"); |
| 186 scoped_ptr<ListValue> decoded(DecodeStringList(string_list)); |
| 187 ListValue expected; |
| 188 expected.Append(Value::CreateStringValue("ponies")); |
| 189 expected.Append(Value::CreateStringValue("more ponies")); |
| 190 EXPECT_TRUE(decoded->Equals(&expected)); |
| 191 } |
| 192 |
| 193 TEST_F(UserPolicyCacheTest, Empty) { |
| 194 UserPolicyCache cache(test_file()); |
| 195 PolicyMap empty; |
| 196 EXPECT_TRUE(empty.Equals(mandatory_policy(cache))); |
| 197 EXPECT_TRUE(empty.Equals(recommended_policy(cache))); |
| 198 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time()); |
| 199 } |
| 200 |
| 201 TEST_F(UserPolicyCacheTest, LoadNoFile) { |
| 202 UserPolicyCache cache(test_file()); |
| 203 cache.Load(); |
| 204 PolicyMap empty; |
| 205 EXPECT_TRUE(empty.Equals(mandatory_policy(cache))); |
| 206 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time()); |
| 207 } |
| 208 |
| 209 TEST_F(UserPolicyCacheTest, RejectFuture) { |
| 210 scoped_ptr<em::PolicyFetchResponse> policy_response( |
| 211 CreateHomepagePolicy("", base::Time::NowFromSystemTime() + |
| 212 base::TimeDelta::FromMinutes(5), |
| 213 em::PolicyOptions::MANDATORY)); |
| 214 WritePolicy(*policy_response); |
| 215 UserPolicyCache cache(test_file()); |
| 216 cache.Load(); |
| 217 PolicyMap empty; |
| 218 EXPECT_TRUE(empty.Equals(mandatory_policy(cache))); |
| 219 EXPECT_EQ(base::Time(), cache.last_policy_refresh_time()); |
| 220 } |
| 221 |
| 222 TEST_F(UserPolicyCacheTest, LoadWithFile) { |
| 223 scoped_ptr<em::PolicyFetchResponse> policy_response( |
| 224 CreateHomepagePolicy("", base::Time::NowFromSystemTime(), |
| 225 em::PolicyOptions::MANDATORY)); |
| 226 WritePolicy(*policy_response); |
| 227 UserPolicyCache cache(test_file()); |
| 228 cache.Load(); |
| 229 PolicyMap empty; |
| 230 EXPECT_TRUE(empty.Equals(mandatory_policy(cache))); |
| 231 EXPECT_NE(base::Time(), cache.last_policy_refresh_time()); |
| 232 EXPECT_GE(base::Time::Now(), cache.last_policy_refresh_time()); |
| 233 } |
| 234 |
| 235 TEST_F(UserPolicyCacheTest, LoadWithData) { |
| 236 scoped_ptr<em::PolicyFetchResponse> policy( |
| 237 CreateHomepagePolicy("http://www.example.com", |
| 238 base::Time::NowFromSystemTime(), |
| 239 em::PolicyOptions::MANDATORY)); |
| 240 WritePolicy(*policy); |
| 241 UserPolicyCache cache(test_file()); |
| 242 cache.Load(); |
| 243 PolicyMap expected; |
| 244 expected.Set(kPolicyHomepageLocation, |
| 245 Value::CreateStringValue("http://www.example.com")); |
| 246 EXPECT_TRUE(expected.Equals(mandatory_policy(cache))); |
| 247 } |
| 248 |
| 249 TEST_F(UserPolicyCacheTest, SetPolicy) { |
| 250 UserPolicyCache cache(test_file()); |
| 251 em::PolicyFetchResponse* policy = |
| 252 CreateHomepagePolicy("http://www.example.com", |
| 253 base::Time::NowFromSystemTime(), |
| 254 em::PolicyOptions::MANDATORY); |
| 255 SetPolicy(&cache, policy, true); |
| 256 em::PolicyFetchResponse* policy2 = |
| 257 CreateHomepagePolicy("http://www.example.com", |
| 258 base::Time::NowFromSystemTime(), |
| 259 em::PolicyOptions::MANDATORY); |
| 260 SetPolicy(&cache, policy2, false); |
| 261 PolicyMap expected; |
| 262 expected.Set(kPolicyHomepageLocation, |
| 263 Value::CreateStringValue("http://www.example.com")); |
| 264 PolicyMap empty; |
| 265 EXPECT_TRUE(expected.Equals(mandatory_policy(cache))); |
| 266 EXPECT_TRUE(empty.Equals(recommended_policy(cache))); |
| 267 policy = CreateHomepagePolicy("http://www.example.com", |
| 268 base::Time::NowFromSystemTime(), |
| 269 em::PolicyOptions::RECOMMENDED); |
| 270 SetPolicy(&cache, policy, true); |
| 271 EXPECT_TRUE(expected.Equals(recommended_policy(cache))); |
| 272 EXPECT_TRUE(empty.Equals(mandatory_policy(cache))); |
| 273 } |
| 274 |
| 275 TEST_F(UserPolicyCacheTest, ResetPolicy) { |
| 276 UserPolicyCache cache(test_file()); |
| 277 |
| 278 em::PolicyFetchResponse* policy = |
| 279 CreateHomepagePolicy("http://www.example.com", |
| 280 base::Time::NowFromSystemTime(), |
| 281 em::PolicyOptions::MANDATORY); |
| 282 SetPolicy(&cache, policy, true); |
| 283 PolicyMap expected; |
| 284 expected.Set(kPolicyHomepageLocation, |
| 285 Value::CreateStringValue("http://www.example.com")); |
| 286 EXPECT_TRUE(expected.Equals(mandatory_policy(cache))); |
| 287 |
| 288 em::PolicyFetchResponse* empty_policy = |
| 289 CreateHomepagePolicy("", base::Time::NowFromSystemTime(), |
| 290 em::PolicyOptions::MANDATORY); |
| 291 SetPolicy(&cache, empty_policy, true); |
| 292 PolicyMap empty; |
| 293 EXPECT_TRUE(empty.Equals(mandatory_policy(cache))); |
| 294 } |
| 295 |
| 296 TEST_F(UserPolicyCacheTest, PersistPolicy) { |
| 297 { |
| 298 UserPolicyCache cache(test_file()); |
| 299 scoped_ptr<em::PolicyFetchResponse> policy( |
| 300 CreateHomepagePolicy("http://www.example.com", |
| 301 base::Time::NowFromSystemTime(), |
| 302 em::PolicyOptions::MANDATORY)); |
| 303 cache.SetPolicy(*policy); |
| 304 } |
| 305 |
| 306 loop_.RunAllPending(); |
| 307 |
| 308 EXPECT_TRUE(file_util::PathExists(test_file())); |
| 309 UserPolicyCache cache(test_file()); |
| 310 cache.Load(); |
| 311 PolicyMap expected; |
| 312 expected.Set(kPolicyHomepageLocation, |
| 313 Value::CreateStringValue("http://www.example.com")); |
| 314 EXPECT_TRUE(expected.Equals(mandatory_policy(cache))); |
| 315 } |
| 316 |
| 317 TEST_F(UserPolicyCacheTest, FreshPolicyOverride) { |
| 318 scoped_ptr<em::PolicyFetchResponse> policy( |
| 319 CreateHomepagePolicy("http://www.example.com", |
| 320 base::Time::NowFromSystemTime(), |
| 321 em::PolicyOptions::MANDATORY)); |
| 322 WritePolicy(*policy); |
| 323 |
| 324 UserPolicyCache cache(test_file()); |
| 325 em::PolicyFetchResponse* updated_policy = |
| 326 CreateHomepagePolicy("http://www.chromium.org", |
| 327 base::Time::NowFromSystemTime(), |
| 328 em::PolicyOptions::MANDATORY); |
| 329 SetPolicy(&cache, updated_policy, true); |
| 330 |
| 331 cache.Load(); |
| 332 PolicyMap expected; |
| 333 expected.Set(kPolicyHomepageLocation, |
| 334 Value::CreateStringValue("http://www.chromium.org")); |
| 335 EXPECT_TRUE(expected.Equals(mandatory_policy(cache))); |
| 336 } |
| 337 |
| 338 } // namespace policy |
OLD | NEW |