| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/extensions/api/storage/policy_value_store.h" | 5 #include "chrome/browser/extensions/api/storage/policy_value_store.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/run_loop.h" | |
| 16 #include "components/policy/core/common/external_data_fetcher.h" | 15 #include "components/policy/core/common/external_data_fetcher.h" |
| 17 #include "components/policy/core/common/policy_map.h" | 16 #include "components/policy/core/common/policy_map.h" |
| 18 #include "components/policy/core/common/policy_types.h" | 17 #include "components/policy/core/common/policy_types.h" |
| 19 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "content/public/test/test_utils.h" |
| 20 #include "extensions/browser/api/storage/backend_task_runner.h" |
| 20 #include "extensions/browser/api/storage/settings_observer.h" | 21 #include "extensions/browser/api/storage/settings_observer.h" |
| 21 #include "extensions/browser/value_store/leveldb_value_store.h" | 22 #include "extensions/browser/value_store/leveldb_value_store.h" |
| 22 #include "extensions/browser/value_store/value_store_unittest.h" | 23 #include "extensions/browser/value_store/value_store_unittest.h" |
| 23 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 26 |
| 26 using testing::_; | 27 using testing::_; |
| 27 using testing::Mock; | 28 using testing::Mock; |
| 28 | 29 |
| 29 namespace extensions { | 30 namespace extensions { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 base::MakeUnique<LeveldbValueStore>(kDatabaseUMAClientName, | 105 base::MakeUnique<LeveldbValueStore>(kDatabaseUMAClientName, |
| 105 scoped_temp_dir_.GetPath()))); | 106 scoped_temp_dir_.GetPath()))); |
| 106 } | 107 } |
| 107 | 108 |
| 108 void TearDown() override { | 109 void TearDown() override { |
| 109 observers_->RemoveObserver(&observer_); | 110 observers_->RemoveObserver(&observer_); |
| 110 store_.reset(); | 111 store_.reset(); |
| 111 } | 112 } |
| 112 | 113 |
| 113 protected: | 114 protected: |
| 115 void SetCurrentPolicy(const policy::PolicyMap& policies) { |
| 116 GetBackendTaskRunner()->PostTask( |
| 117 FROM_HERE, |
| 118 base::Bind(&PolicyValueStoreTest::SetCurrentPolicyOnBackendSequence, |
| 119 base::Unretained(this), base::Passed(policies.DeepCopy()))); |
| 120 content::RunAllBlockingPoolTasksUntilIdle(); |
| 121 } |
| 122 |
| 123 void SetCurrentPolicyOnBackendSequence( |
| 124 std::unique_ptr<policy::PolicyMap> policies) { |
| 125 DCHECK(IsOnBackendSequence()); |
| 126 store_->SetCurrentPolicy(*policies); |
| 127 } |
| 128 |
| 114 base::ScopedTempDir scoped_temp_dir_; | 129 base::ScopedTempDir scoped_temp_dir_; |
| 115 content::TestBrowserThreadBundle test_browser_thread_bundle_; | 130 content::TestBrowserThreadBundle test_browser_thread_bundle_; |
| 116 std::unique_ptr<PolicyValueStore> store_; | 131 std::unique_ptr<PolicyValueStore> store_; |
| 117 MockSettingsObserver observer_; | 132 MockSettingsObserver observer_; |
| 118 scoped_refptr<SettingsObserverList> observers_; | 133 scoped_refptr<SettingsObserverList> observers_; |
| 119 }; | 134 }; |
| 120 | 135 |
| 121 TEST_F(PolicyValueStoreTest, DontProvideRecommendedPolicies) { | 136 TEST_F(PolicyValueStoreTest, DontProvideRecommendedPolicies) { |
| 122 policy::PolicyMap policies; | 137 policy::PolicyMap policies; |
| 123 base::Value expected(123); | 138 base::Value expected(123); |
| 124 policies.Set("must", policy::POLICY_LEVEL_MANDATORY, | 139 policies.Set("must", policy::POLICY_LEVEL_MANDATORY, |
| 125 policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD, | 140 policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD, |
| 126 expected.CreateDeepCopy(), nullptr); | 141 expected.CreateDeepCopy(), nullptr); |
| 127 policies.Set("may", policy::POLICY_LEVEL_RECOMMENDED, | 142 policies.Set("may", policy::POLICY_LEVEL_RECOMMENDED, |
| 128 policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD, | 143 policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD, |
| 129 base::MakeUnique<base::Value>(456), nullptr); | 144 base::MakeUnique<base::Value>(456), nullptr); |
| 130 store_->SetCurrentPolicy(policies); | 145 SetCurrentPolicy(policies); |
| 146 |
| 131 ValueStore::ReadResult result = store_->Get(); | 147 ValueStore::ReadResult result = store_->Get(); |
| 132 ASSERT_TRUE(result->status().ok()); | 148 ASSERT_TRUE(result->status().ok()); |
| 133 EXPECT_EQ(1u, result->settings().size()); | 149 EXPECT_EQ(1u, result->settings().size()); |
| 134 base::Value* value = NULL; | 150 base::Value* value = NULL; |
| 135 EXPECT_FALSE(result->settings().Get("may", &value)); | 151 EXPECT_FALSE(result->settings().Get("may", &value)); |
| 136 EXPECT_TRUE(result->settings().Get("must", &value)); | 152 EXPECT_TRUE(result->settings().Get("must", &value)); |
| 137 EXPECT_TRUE(base::Value::Equals(&expected, value)); | 153 EXPECT_TRUE(base::Value::Equals(&expected, value)); |
| 138 } | 154 } |
| 139 | 155 |
| 140 TEST_F(PolicyValueStoreTest, ReadOnly) { | 156 TEST_F(PolicyValueStoreTest, ReadOnly) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 162 changes.push_back(ValueStoreChange("aaa", nullptr, value.CreateDeepCopy())); | 178 changes.push_back(ValueStoreChange("aaa", nullptr, value.CreateDeepCopy())); |
| 163 EXPECT_CALL(observer_, | 179 EXPECT_CALL(observer_, |
| 164 OnSettingsChanged(kTestExtensionId, | 180 OnSettingsChanged(kTestExtensionId, |
| 165 settings_namespace::MANAGED, | 181 settings_namespace::MANAGED, |
| 166 ValueStoreChange::ToJson(changes))); | 182 ValueStoreChange::ToJson(changes))); |
| 167 } | 183 } |
| 168 | 184 |
| 169 policy::PolicyMap policies; | 185 policy::PolicyMap policies; |
| 170 policies.Set("aaa", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | 186 policies.Set("aaa", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, |
| 171 policy::POLICY_SOURCE_CLOUD, value.CreateDeepCopy(), nullptr); | 187 policy::POLICY_SOURCE_CLOUD, value.CreateDeepCopy(), nullptr); |
| 172 store_->SetCurrentPolicy(policies); | 188 SetCurrentPolicy(policies); |
| 173 base::RunLoop().RunUntilIdle(); | |
| 174 Mock::VerifyAndClearExpectations(&observer_); | 189 Mock::VerifyAndClearExpectations(&observer_); |
| 175 | 190 |
| 176 // Notify when new policies are added. | 191 // Notify when new policies are added. |
| 177 { | 192 { |
| 178 ValueStoreChangeList changes; | 193 ValueStoreChangeList changes; |
| 179 changes.push_back(ValueStoreChange("bbb", nullptr, value.CreateDeepCopy())); | 194 changes.push_back(ValueStoreChange("bbb", nullptr, value.CreateDeepCopy())); |
| 180 EXPECT_CALL(observer_, | 195 EXPECT_CALL(observer_, |
| 181 OnSettingsChanged(kTestExtensionId, | 196 OnSettingsChanged(kTestExtensionId, |
| 182 settings_namespace::MANAGED, | 197 settings_namespace::MANAGED, |
| 183 ValueStoreChange::ToJson(changes))); | 198 ValueStoreChange::ToJson(changes))); |
| 184 } | 199 } |
| 185 | 200 |
| 186 policies.Set("bbb", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | 201 policies.Set("bbb", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, |
| 187 policy::POLICY_SOURCE_CLOUD, value.CreateDeepCopy(), nullptr); | 202 policy::POLICY_SOURCE_CLOUD, value.CreateDeepCopy(), nullptr); |
| 188 store_->SetCurrentPolicy(policies); | 203 SetCurrentPolicy(policies); |
| 189 base::RunLoop().RunUntilIdle(); | |
| 190 Mock::VerifyAndClearExpectations(&observer_); | 204 Mock::VerifyAndClearExpectations(&observer_); |
| 191 | 205 |
| 192 // Notify when policies change. | 206 // Notify when policies change. |
| 193 const base::Value new_value("222"); | 207 const base::Value new_value("222"); |
| 194 { | 208 { |
| 195 ValueStoreChangeList changes; | 209 ValueStoreChangeList changes; |
| 196 changes.push_back(ValueStoreChange("bbb", value.CreateDeepCopy(), | 210 changes.push_back(ValueStoreChange("bbb", value.CreateDeepCopy(), |
| 197 new_value.CreateDeepCopy())); | 211 new_value.CreateDeepCopy())); |
| 198 EXPECT_CALL(observer_, | 212 EXPECT_CALL(observer_, |
| 199 OnSettingsChanged(kTestExtensionId, | 213 OnSettingsChanged(kTestExtensionId, |
| 200 settings_namespace::MANAGED, | 214 settings_namespace::MANAGED, |
| 201 ValueStoreChange::ToJson(changes))); | 215 ValueStoreChange::ToJson(changes))); |
| 202 } | 216 } |
| 203 | 217 |
| 204 policies.Set("bbb", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, | 218 policies.Set("bbb", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER, |
| 205 policy::POLICY_SOURCE_CLOUD, new_value.CreateDeepCopy(), | 219 policy::POLICY_SOURCE_CLOUD, new_value.CreateDeepCopy(), |
| 206 nullptr); | 220 nullptr); |
| 207 store_->SetCurrentPolicy(policies); | 221 SetCurrentPolicy(policies); |
| 208 base::RunLoop().RunUntilIdle(); | |
| 209 Mock::VerifyAndClearExpectations(&observer_); | 222 Mock::VerifyAndClearExpectations(&observer_); |
| 210 | 223 |
| 211 // Notify when policies are removed. | 224 // Notify when policies are removed. |
| 212 { | 225 { |
| 213 ValueStoreChangeList changes; | 226 ValueStoreChangeList changes; |
| 214 changes.push_back( | 227 changes.push_back( |
| 215 ValueStoreChange("bbb", new_value.CreateDeepCopy(), nullptr)); | 228 ValueStoreChange("bbb", new_value.CreateDeepCopy(), nullptr)); |
| 216 EXPECT_CALL(observer_, | 229 EXPECT_CALL(observer_, |
| 217 OnSettingsChanged(kTestExtensionId, | 230 OnSettingsChanged(kTestExtensionId, |
| 218 settings_namespace::MANAGED, | 231 settings_namespace::MANAGED, |
| 219 ValueStoreChange::ToJson(changes))); | 232 ValueStoreChange::ToJson(changes))); |
| 220 } | 233 } |
| 221 | 234 |
| 222 policies.Erase("bbb"); | 235 policies.Erase("bbb"); |
| 223 store_->SetCurrentPolicy(policies); | 236 SetCurrentPolicy(policies); |
| 224 base::RunLoop().RunUntilIdle(); | |
| 225 Mock::VerifyAndClearExpectations(&observer_); | 237 Mock::VerifyAndClearExpectations(&observer_); |
| 226 | 238 |
| 227 // Don't notify when there aren't any changes. | 239 // Don't notify when there aren't any changes. |
| 228 EXPECT_CALL(observer_, OnSettingsChanged(_, _, _)).Times(0); | 240 EXPECT_CALL(observer_, OnSettingsChanged(_, _, _)).Times(0); |
| 229 store_->SetCurrentPolicy(policies); | 241 SetCurrentPolicy(policies); |
| 230 base::RunLoop().RunUntilIdle(); | |
| 231 Mock::VerifyAndClearExpectations(&observer_); | 242 Mock::VerifyAndClearExpectations(&observer_); |
| 232 } | 243 } |
| 233 | 244 |
| 234 } // namespace extensions | 245 } // namespace extensions |
| OLD | NEW |