| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/device_policy_cache.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/basictypes.h" | |
| 10 #include "base/compiler_specific.h" | |
| 11 #include "base/file_path.h" | |
| 12 #include "base/threading/sequenced_worker_pool.h" | |
| 13 #include "chrome/browser/chromeos/cros/cryptohome_library.h" | |
| 14 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h" | |
| 15 #include "chrome/browser/policy/cloud_policy_data_store.h" | |
| 16 #include "chrome/browser/policy/enterprise_install_attributes.h" | |
| 17 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" | |
| 18 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
| 19 #include "policy/policy_constants.h" | |
| 20 #include "testing/gmock/include/gmock/gmock.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 | |
| 23 using ::testing::Mock; | |
| 24 | |
| 25 namespace em = enterprise_management; | |
| 26 | |
| 27 namespace policy { | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 class MockCloudPolicyCacheObserver : public CloudPolicyCacheBase::Observer { | |
| 32 public: | |
| 33 virtual ~MockCloudPolicyCacheObserver() {} | |
| 34 | |
| 35 MOCK_METHOD1(OnCacheUpdate, void(CloudPolicyCacheBase*)); | |
| 36 }; | |
| 37 | |
| 38 } // namespace | |
| 39 | |
| 40 class DevicePolicyCacheTest : public chromeos::DeviceSettingsTestBase { | |
| 41 protected: | |
| 42 DevicePolicyCacheTest() | |
| 43 : cryptohome_(chromeos::CryptohomeLibrary::GetImpl(true)), | |
| 44 install_attributes_(cryptohome_.get()) {} | |
| 45 | |
| 46 virtual void SetUp() OVERRIDE { | |
| 47 DeviceSettingsTestBase::SetUp(); | |
| 48 device_policy_.payload().mutable_device_policy_refresh_rate()-> | |
| 49 set_device_policy_refresh_rate(120); | |
| 50 device_policy_.Build(); | |
| 51 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob()); | |
| 52 | |
| 53 data_store_.reset(CloudPolicyDataStore::CreateForDevicePolicies()); | |
| 54 cache_.reset(new DevicePolicyCache(data_store_.get(), | |
| 55 &install_attributes_, | |
| 56 &device_settings_service_)); | |
| 57 cache_->AddObserver(&observer_); | |
| 58 } | |
| 59 | |
| 60 virtual void TearDown() OVERRIDE { | |
| 61 cache_->RemoveObserver(&observer_); | |
| 62 cache_.reset(); | |
| 63 | |
| 64 DeviceSettingsTestBase::TearDown(); | |
| 65 } | |
| 66 | |
| 67 void Startup() { | |
| 68 EXPECT_CALL(observer_, OnCacheUpdate(cache_.get())); | |
| 69 device_settings_service_.Load(); | |
| 70 device_settings_test_helper_.Flush(); | |
| 71 cache_->Load(); | |
| 72 Mock::VerifyAndClearExpectations(&observer_); | |
| 73 } | |
| 74 | |
| 75 void MakeEnterpriseDevice() { | |
| 76 ASSERT_EQ(EnterpriseInstallAttributes::LOCK_SUCCESS, | |
| 77 install_attributes_.LockDevice( | |
| 78 device_policy_.policy_data().username(), | |
| 79 DEVICE_MODE_ENTERPRISE, | |
| 80 std::string())); | |
| 81 } | |
| 82 | |
| 83 const Value* GetPolicy(const char* policy_name) { | |
| 84 return cache_->policy()->GetValue(policy_name); | |
| 85 } | |
| 86 | |
| 87 MockCloudPolicyCacheObserver observer_; | |
| 88 | |
| 89 scoped_ptr<chromeos::CryptohomeLibrary> cryptohome_; | |
| 90 EnterpriseInstallAttributes install_attributes_; | |
| 91 | |
| 92 scoped_ptr<CloudPolicyDataStore> data_store_; | |
| 93 scoped_ptr<DevicePolicyCache> cache_; | |
| 94 | |
| 95 private: | |
| 96 DISALLOW_COPY_AND_ASSIGN(DevicePolicyCacheTest); | |
| 97 }; | |
| 98 | |
| 99 TEST_F(DevicePolicyCacheTest, ColdStartup) { | |
| 100 EXPECT_CALL(observer_, OnCacheUpdate(cache_.get())).Times(0); | |
| 101 cache_->Load(); | |
| 102 Mock::VerifyAndClearExpectations(&observer_); | |
| 103 | |
| 104 EXPECT_CALL(observer_, OnCacheUpdate(cache_.get())); | |
| 105 ReloadDeviceSettings(); | |
| 106 Mock::VerifyAndClearExpectations(&observer_); | |
| 107 | |
| 108 base::FundamentalValue expected(120); | |
| 109 EXPECT_TRUE(Value::Equals(&expected, | |
| 110 GetPolicy(key::kDevicePolicyRefreshRate))); | |
| 111 } | |
| 112 | |
| 113 TEST_F(DevicePolicyCacheTest, WarmStartup) { | |
| 114 Startup(); | |
| 115 | |
| 116 base::FundamentalValue expected(120); | |
| 117 EXPECT_TRUE(Value::Equals(&expected, | |
| 118 GetPolicy(key::kDevicePolicyRefreshRate))); | |
| 119 } | |
| 120 | |
| 121 TEST_F(DevicePolicyCacheTest, SetPolicy) { | |
| 122 MakeEnterpriseDevice(); | |
| 123 Startup(); | |
| 124 | |
| 125 base::FundamentalValue expected(120); | |
| 126 EXPECT_TRUE(Value::Equals(&expected, | |
| 127 GetPolicy(key::kDevicePolicyRefreshRate))); | |
| 128 | |
| 129 // Set new policy information. | |
| 130 device_policy_.payload().mutable_device_policy_refresh_rate()-> | |
| 131 set_device_policy_refresh_rate(300); | |
| 132 device_policy_.Build(); | |
| 133 EXPECT_CALL(observer_, OnCacheUpdate(cache_.get())).Times(0); | |
| 134 EXPECT_TRUE(cache_->SetPolicy(device_policy_.policy())); | |
| 135 cache_->SetFetchingDone(); | |
| 136 Mock::VerifyAndClearExpectations(&observer_); | |
| 137 | |
| 138 EXPECT_CALL(observer_, OnCacheUpdate(cache_.get())).Times(0); | |
| 139 device_settings_test_helper_.FlushStore(); | |
| 140 Mock::VerifyAndClearExpectations(&observer_); | |
| 141 | |
| 142 // Cache update notification should only fire in the retrieve callback. | |
| 143 EXPECT_CALL(observer_, OnCacheUpdate(cache_.get())); | |
| 144 FlushDeviceSettings(); | |
| 145 Mock::VerifyAndClearExpectations(&observer_); | |
| 146 | |
| 147 base::FundamentalValue updated_expected(300); | |
| 148 EXPECT_TRUE(Value::Equals(&updated_expected, | |
| 149 GetPolicy(key::kDevicePolicyRefreshRate))); | |
| 150 | |
| 151 cache_->RemoveObserver(&observer_); | |
| 152 } | |
| 153 | |
| 154 TEST_F(DevicePolicyCacheTest, SetPolicyOtherUserSameDomain) { | |
| 155 MakeEnterpriseDevice(); | |
| 156 Startup(); | |
| 157 | |
| 158 // Set new policy information. This should succeed as the domain is the same. | |
| 159 device_policy_.policy_data().set_username("another_user@example.com"); | |
| 160 device_policy_.Build(); | |
| 161 | |
| 162 EXPECT_CALL(observer_, OnCacheUpdate(cache_.get())); | |
| 163 EXPECT_TRUE(cache_->SetPolicy(device_policy_.policy())); | |
| 164 FlushDeviceSettings(); | |
| 165 Mock::VerifyAndClearExpectations(&observer_); | |
| 166 EXPECT_EQ(device_policy_.GetBlob(), | |
| 167 device_settings_test_helper_.policy_blob()); | |
| 168 } | |
| 169 | |
| 170 TEST_F(DevicePolicyCacheTest, SetPolicyOtherUserOtherDomain) { | |
| 171 MakeEnterpriseDevice(); | |
| 172 Startup(); | |
| 173 | |
| 174 // Set new policy information. This should fail because the user is from | |
| 175 // different domain. | |
| 176 device_policy_.policy_data().set_username("foreign_user@hackers.com"); | |
| 177 device_policy_.Build(); | |
| 178 EXPECT_NE(device_policy_.GetBlob(), | |
| 179 device_settings_test_helper_.policy_blob()); | |
| 180 | |
| 181 EXPECT_FALSE(cache_->SetPolicy(device_policy_.policy())); | |
| 182 FlushDeviceSettings(); | |
| 183 EXPECT_NE(device_policy_.GetBlob(), | |
| 184 device_settings_test_helper_.policy_blob()); | |
| 185 } | |
| 186 | |
| 187 TEST_F(DevicePolicyCacheTest, SetPolicyNonEnterpriseDevice) { | |
| 188 Startup(); | |
| 189 | |
| 190 // Set new policy information. This should fail due to invalid user. | |
| 191 device_settings_test_helper_.set_policy_blob(std::string()); | |
| 192 | |
| 193 EXPECT_FALSE(cache_->SetPolicy(device_policy_.policy())); | |
| 194 FlushDeviceSettings(); | |
| 195 EXPECT_TRUE(device_settings_test_helper_.policy_blob().empty()); | |
| 196 } | |
| 197 | |
| 198 TEST_F(DevicePolicyCacheTest, SetProxyPolicy) { | |
| 199 MakeEnterpriseDevice(); | |
| 200 | |
| 201 em::DeviceProxySettingsProto proxy_settings; | |
| 202 proxy_settings.set_proxy_mode("direct"); | |
| 203 proxy_settings.set_proxy_server("http://proxy:8080"); | |
| 204 proxy_settings.set_proxy_pac_url("http://proxy:8080/pac.js"); | |
| 205 proxy_settings.set_proxy_bypass_list("127.0.0.1,example.com"); | |
| 206 device_policy_.payload().mutable_device_proxy_settings()->CopyFrom( | |
| 207 proxy_settings); | |
| 208 device_policy_.Build(); | |
| 209 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob()); | |
| 210 Startup(); | |
| 211 | |
| 212 DictionaryValue expected; | |
| 213 expected.SetString(key::kProxyMode, proxy_settings.proxy_mode()); | |
| 214 expected.SetString(key::kProxyServer, proxy_settings.proxy_server()); | |
| 215 expected.SetString(key::kProxyPacUrl, proxy_settings.proxy_pac_url()); | |
| 216 expected.SetString(key::kProxyBypassList, proxy_settings.proxy_bypass_list()); | |
| 217 EXPECT_TRUE(Value::Equals(&expected, GetPolicy(key::kProxySettings))); | |
| 218 } | |
| 219 | |
| 220 TEST_F(DevicePolicyCacheTest, SetDeviceNetworkConfigurationPolicy) { | |
| 221 MakeEnterpriseDevice(); | |
| 222 | |
| 223 std::string fake_config("{ 'NetworkConfigurations': [] }"); | |
| 224 device_policy_.payload().mutable_open_network_configuration()-> | |
| 225 set_open_network_configuration(fake_config); | |
| 226 device_policy_.Build(); | |
| 227 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob()); | |
| 228 Startup(); | |
| 229 | |
| 230 StringValue expected_config(fake_config); | |
| 231 EXPECT_TRUE( | |
| 232 Value::Equals(&expected_config, | |
| 233 GetPolicy(key::kDeviceOpenNetworkConfiguration))); | |
| 234 } | |
| 235 | |
| 236 } // namespace policy | |
| OLD | NEW |