OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "components/policy/core/common/cloud/user_cloud_policy_store.h" | 5 #include "components/policy/core/common/cloud/user_cloud_policy_store.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 run_loop.RunUntilIdle(); | 35 run_loop.RunUntilIdle(); |
36 } | 36 } |
37 | 37 |
38 bool WriteStringToFile(const base::FilePath path, const std::string& data) { | 38 bool WriteStringToFile(const base::FilePath path, const std::string& data) { |
39 if (!base::CreateDirectory(path.DirName())) { | 39 if (!base::CreateDirectory(path.DirName())) { |
40 DLOG(WARNING) << "Failed to create directory " << path.DirName().value(); | 40 DLOG(WARNING) << "Failed to create directory " << path.DirName().value(); |
41 return false; | 41 return false; |
42 } | 42 } |
43 | 43 |
44 int size = data.size(); | 44 int size = data.size(); |
45 if (file_util::WriteFile(path, data.c_str(), size) != size) { | 45 if (base::WriteFile(path, data.c_str(), size) != size) { |
46 DLOG(WARNING) << "Failed to write " << path.value(); | 46 DLOG(WARNING) << "Failed to write " << path.value(); |
47 return false; | 47 return false; |
48 } | 48 } |
49 | 49 |
50 return true; | 50 return true; |
51 } | 51 } |
52 | 52 |
53 } // namespace | 53 } // namespace |
54 | 54 |
55 class UserCloudPolicyStoreTest : public testing::Test { | 55 class UserCloudPolicyStoreTest : public testing::Test { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 } | 161 } |
162 | 162 |
163 TEST_F(UserCloudPolicyStoreTest, LoadWithInvalidFile) { | 163 TEST_F(UserCloudPolicyStoreTest, LoadWithInvalidFile) { |
164 EXPECT_FALSE(store_->policy()); | 164 EXPECT_FALSE(store_->policy()); |
165 EXPECT_TRUE(store_->policy_map().empty()); | 165 EXPECT_TRUE(store_->policy_map().empty()); |
166 | 166 |
167 // Create a bogus file. | 167 // Create a bogus file. |
168 ASSERT_TRUE(base::CreateDirectory(policy_file().DirName())); | 168 ASSERT_TRUE(base::CreateDirectory(policy_file().DirName())); |
169 std::string bogus_data = "bogus_data"; | 169 std::string bogus_data = "bogus_data"; |
170 int size = bogus_data.size(); | 170 int size = bogus_data.size(); |
171 ASSERT_EQ(size, file_util::WriteFile(policy_file(), | 171 ASSERT_EQ(size, base::WriteFile(policy_file(), |
172 bogus_data.c_str(), | 172 bogus_data.c_str(), bogus_data.size())); |
173 bogus_data.size())); | |
174 | 173 |
175 ExpectError(store_.get(), CloudPolicyStore::STATUS_LOAD_ERROR); | 174 ExpectError(store_.get(), CloudPolicyStore::STATUS_LOAD_ERROR); |
176 store_->Load(); | 175 store_->Load(); |
177 RunUntilIdle(); | 176 RunUntilIdle(); |
178 | 177 |
179 EXPECT_FALSE(store_->policy()); | 178 EXPECT_FALSE(store_->policy()); |
180 EXPECT_TRUE(store_->policy_map().empty()); | 179 EXPECT_TRUE(store_->policy_map().empty()); |
181 } | 180 } |
182 | 181 |
183 TEST_F(UserCloudPolicyStoreTest, LoadImmediatelyWithNoFile) { | 182 TEST_F(UserCloudPolicyStoreTest, LoadImmediatelyWithNoFile) { |
(...skipping 10 matching lines...) Expand all Loading... |
194 } | 193 } |
195 | 194 |
196 TEST_F(UserCloudPolicyStoreTest, LoadImmediatelyWithInvalidFile) { | 195 TEST_F(UserCloudPolicyStoreTest, LoadImmediatelyWithInvalidFile) { |
197 EXPECT_FALSE(store_->policy()); | 196 EXPECT_FALSE(store_->policy()); |
198 EXPECT_TRUE(store_->policy_map().empty()); | 197 EXPECT_TRUE(store_->policy_map().empty()); |
199 | 198 |
200 // Create a bogus file. | 199 // Create a bogus file. |
201 ASSERT_TRUE(base::CreateDirectory(policy_file().DirName())); | 200 ASSERT_TRUE(base::CreateDirectory(policy_file().DirName())); |
202 std::string bogus_data = "bogus_data"; | 201 std::string bogus_data = "bogus_data"; |
203 int size = bogus_data.size(); | 202 int size = bogus_data.size(); |
204 ASSERT_EQ(size, file_util::WriteFile(policy_file(), | 203 ASSERT_EQ(size, base::WriteFile(policy_file(), |
205 bogus_data.c_str(), | 204 bogus_data.c_str(), bogus_data.size())); |
206 bogus_data.size())); | |
207 | 205 |
208 ExpectError(store_.get(), CloudPolicyStore::STATUS_LOAD_ERROR); | 206 ExpectError(store_.get(), CloudPolicyStore::STATUS_LOAD_ERROR); |
209 store_->LoadImmediately(); // Should load without running the message loop. | 207 store_->LoadImmediately(); // Should load without running the message loop. |
210 | 208 |
211 EXPECT_FALSE(store_->policy()); | 209 EXPECT_FALSE(store_->policy()); |
212 EXPECT_TRUE(store_->policy_map().empty()); | 210 EXPECT_TRUE(store_->policy_map().empty()); |
213 } | 211 } |
214 | 212 |
215 // Load file from cache with no key data, then migrate to have a key. | 213 // Load file from cache with no key data, then migrate to have a key. |
216 TEST_F(UserCloudPolicyStoreTest, Migration) { | 214 TEST_F(UserCloudPolicyStoreTest, Migration) { |
217 UserPolicyBuilder unsigned_builder; | 215 UserPolicyBuilder unsigned_builder; |
218 unsigned_builder.UnsetSigningKey(); | 216 unsigned_builder.UnsetSigningKey(); |
219 InitPolicyPayload(&unsigned_builder.payload()); | 217 InitPolicyPayload(&unsigned_builder.payload()); |
220 unsigned_builder.Build(); | 218 unsigned_builder.Build(); |
221 // Policy should be unsigned. | 219 // Policy should be unsigned. |
222 EXPECT_FALSE(unsigned_builder.policy().has_policy_data_signature()); | 220 EXPECT_FALSE(unsigned_builder.policy().has_policy_data_signature()); |
223 | 221 |
224 // Write policy to disk. | 222 // Write policy to disk. |
225 std::string data; | 223 std::string data; |
226 ASSERT_TRUE(unsigned_builder.policy().SerializeToString(&data)); | 224 ASSERT_TRUE(unsigned_builder.policy().SerializeToString(&data)); |
227 ASSERT_TRUE(base::CreateDirectory(policy_file().DirName())); | 225 ASSERT_TRUE(base::CreateDirectory(policy_file().DirName())); |
228 int size = data.size(); | 226 int size = data.size(); |
229 ASSERT_EQ(size, file_util::WriteFile(policy_file(), data.c_str(), size)); | 227 ASSERT_EQ(size, base::WriteFile(policy_file(), data.c_str(), size)); |
230 | 228 |
231 // Now make sure the data can get loaded. | 229 // Now make sure the data can get loaded. |
232 Sequence s; | 230 Sequence s; |
233 EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s); | 231 EXPECT_CALL(*external_data_manager_, OnPolicyStoreLoaded()).InSequence(s); |
234 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s); | 232 EXPECT_CALL(observer_, OnStoreLoaded(store_.get())).InSequence(s); |
235 store_->LoadImmediately(); // Should load without running the message loop. | 233 store_->LoadImmediately(); // Should load without running the message loop. |
236 Mock::VerifyAndClearExpectations(external_data_manager_.get()); | 234 Mock::VerifyAndClearExpectations(external_data_manager_.get()); |
237 Mock::VerifyAndClearExpectations(&observer_); | 235 Mock::VerifyAndClearExpectations(&observer_); |
238 | 236 |
239 ASSERT_TRUE(store_->policy()); | 237 ASSERT_TRUE(store_->policy()); |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
540 loop_.message_loop_proxy())); | 538 loop_.message_loop_proxy())); |
541 store2->SetSigninUsername(PolicyBuilder::kFakeUsername); | 539 store2->SetSigninUsername(PolicyBuilder::kFakeUsername); |
542 store2->AddObserver(&observer_); | 540 store2->AddObserver(&observer_); |
543 ExpectError(store2.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR); | 541 ExpectError(store2.get(), CloudPolicyStore::STATUS_VALIDATION_ERROR); |
544 store2->Load(); | 542 store2->Load(); |
545 RunUntilIdle(); | 543 RunUntilIdle(); |
546 store2->RemoveObserver(&observer_); | 544 store2->RemoveObserver(&observer_); |
547 } | 545 } |
548 | 546 |
549 } // namespace policy | 547 } // namespace policy |
OLD | NEW |