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 "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 #include "policy/proto/chrome_settings.pb.h" | 36 #include "policy/proto/chrome_settings.pb.h" |
37 #include "policy/proto/cloud_policy.pb.h" | 37 #include "policy/proto/cloud_policy.pb.h" |
38 #include "testing/gmock/include/gmock/gmock.h" | 38 #include "testing/gmock/include/gmock/gmock.h" |
39 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
40 | 40 |
41 #if defined(OS_CHROMEOS) | 41 #if defined(OS_CHROMEOS) |
42 #include "chrome/browser/chromeos/login/user_manager.h" | 42 #include "chrome/browser/chromeos/login/user_manager.h" |
43 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h" | 43 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h" |
44 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chrom
eos.h" | 44 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chrom
eos.h" |
45 #include "chromeos/chromeos_paths.h" | 45 #include "chromeos/chromeos_paths.h" |
46 #include "chromeos/dbus/mock_cryptohome_client.h" | 46 #include "chromeos/dbus/cryptohome_client.h" |
47 #include "chromeos/dbus/mock_dbus_thread_manager.h" | |
48 #include "chromeos/dbus/mock_session_manager_client.h" | |
49 #include "chromeos/dbus/mock_update_engine_client.h" | |
50 #else | 47 #else |
51 #include "chrome/browser/policy/cloud/user_cloud_policy_manager.h" | 48 #include "chrome/browser/policy/cloud/user_cloud_policy_manager.h" |
52 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h" | 49 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h" |
53 #include "chrome/browser/signin/signin_manager.h" | 50 #include "chrome/browser/signin/signin_manager.h" |
54 #include "chrome/browser/signin/signin_manager_factory.h" | 51 #include "chrome/browser/signin/signin_manager_factory.h" |
55 #endif | 52 #endif |
56 | 53 |
57 using testing::AnyNumber; | 54 using testing::AnyNumber; |
58 using testing::InvokeWithoutArgs; | 55 using testing::InvokeWithoutArgs; |
59 using testing::Mock; | 56 using testing::Mock; |
60 using testing::Return; | 57 using testing::Return; |
61 using testing::_; | 58 using testing::_; |
62 | 59 |
63 namespace em = enterprise_management; | 60 namespace em = enterprise_management; |
64 | 61 |
65 namespace policy { | 62 namespace policy { |
66 | 63 |
67 namespace { | 64 namespace { |
68 | 65 |
69 #if defined(OS_CHROMEOS) | |
70 | |
71 const char kSanitizedUsername[] = "0123456789ABCDEF0123456789ABCDEF01234567"; | |
72 | |
73 ACTION(GetSanitizedUsername) { | |
74 MessageLoop::current()->PostTask( | |
75 FROM_HERE, | |
76 base::Bind(arg1, chromeos::DBUS_METHOD_CALL_SUCCESS, kSanitizedUsername)); | |
77 } | |
78 | |
79 ACTION_P(RetrieveUserPolicy, storage) { | |
80 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(arg0, *storage)); | |
81 } | |
82 | |
83 ACTION_P2(StoreUserPolicy, storage, user_policy_key_file) { | |
84 // The session_manager stores a copy of the policy key at | |
85 // /var/run/user_policy/$hash/policy.pub. Simulate that behavior here, so | |
86 // that the policy signature can be validated. | |
87 em::PolicyFetchResponse policy; | |
88 ASSERT_TRUE(policy.ParseFromString(arg0)); | |
89 if (policy.has_new_public_key()) { | |
90 ASSERT_TRUE(file_util::CreateDirectory(user_policy_key_file.DirName())); | |
91 int result = file_util::WriteFile( | |
92 user_policy_key_file, | |
93 policy.new_public_key().data(), | |
94 policy.new_public_key().size()); | |
95 ASSERT_EQ(static_cast<int>(policy.new_public_key().size()), result); | |
96 } | |
97 | |
98 *storage = arg0; | |
99 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(arg1, true)); | |
100 } | |
101 | |
102 #endif | |
103 | |
104 const char* GetTestUser() { | 66 const char* GetTestUser() { |
105 #if defined(OS_CHROMEOS) | 67 #if defined(OS_CHROMEOS) |
106 return chromeos::UserManager::kStubUser; | 68 return chromeos::UserManager::kStubUser; |
107 #else | 69 #else |
108 return "user@example.com"; | 70 return "user@example.com"; |
109 #endif | 71 #endif |
110 } | 72 } |
111 | 73 |
112 std::string GetEmptyPolicy() { | 74 std::string GetEmptyPolicy() { |
113 const char kEmptyPolicy[] = | 75 const char kEmptyPolicy[] = |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 139 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
178 ASSERT_NO_FATAL_FAILURE(SetServerPolicy(GetEmptyPolicy())); | 140 ASSERT_NO_FATAL_FAILURE(SetServerPolicy(GetEmptyPolicy())); |
179 | 141 |
180 test_server_.reset(new LocalPolicyTestServer(policy_file_path())); | 142 test_server_.reset(new LocalPolicyTestServer(policy_file_path())); |
181 ASSERT_TRUE(test_server_->Start()); | 143 ASSERT_TRUE(test_server_->Start()); |
182 | 144 |
183 std::string url = test_server_->GetServiceURL().spec(); | 145 std::string url = test_server_->GetServiceURL().spec(); |
184 | 146 |
185 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 147 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
186 command_line->AppendSwitchASCII(switches::kDeviceManagementUrl, url); | 148 command_line->AppendSwitchASCII(switches::kDeviceManagementUrl, url); |
187 | |
188 #if defined(OS_CHROMEOS) | |
189 ASSERT_TRUE(PathService::Override(chromeos::DIR_USER_POLICY_KEYS, | |
190 user_policy_key_dir())); | |
191 | |
192 mock_dbus_thread_manager_ = new chromeos::MockDBusThreadManager(); | |
193 chromeos::DBusThreadManager::InitializeForTesting( | |
194 mock_dbus_thread_manager_); | |
195 EXPECT_CALL(*mock_dbus_thread_manager_->mock_cryptohome_client(), | |
196 GetSanitizedUsername(_, _)) | |
197 .WillRepeatedly(GetSanitizedUsername()); | |
198 EXPECT_CALL(*mock_dbus_thread_manager_->mock_session_manager_client(), | |
199 StoreUserPolicy(_, _)) | |
200 .WillRepeatedly(StoreUserPolicy(&session_manager_user_policy_, | |
201 user_policy_key_file())); | |
202 EXPECT_CALL(*mock_dbus_thread_manager_->mock_session_manager_client(), | |
203 RetrieveUserPolicy(_)) | |
204 .WillRepeatedly(RetrieveUserPolicy(&session_manager_user_policy_)); | |
205 EXPECT_CALL(*mock_dbus_thread_manager_->mock_update_engine_client(), | |
206 GetLastStatus()) | |
207 .Times(1) | |
208 .WillOnce(Return(chromeos::MockUpdateEngineClient::Status())); | |
209 #endif | |
210 } | 149 } |
211 | 150 |
212 virtual void SetUpOnMainThread() OVERRIDE { | 151 virtual void SetUpOnMainThread() OVERRIDE { |
213 ASSERT_TRUE(PolicyServiceIsEmpty(g_browser_process->policy_service())) | 152 ASSERT_TRUE(PolicyServiceIsEmpty(g_browser_process->policy_service())) |
214 << "Pre-existing policies in this machine will make this test fail."; | 153 << "Pre-existing policies in this machine will make this test fail."; |
215 | 154 |
216 BrowserPolicyConnector* connector = | 155 BrowserPolicyConnector* connector = |
217 g_browser_process->browser_policy_connector(); | 156 g_browser_process->browser_policy_connector(); |
218 connector->ScheduleServiceInitialization(0); | 157 connector->ScheduleServiceInitialization(0); |
219 | 158 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 em::DeviceRegisterRequest::USER; | 192 em::DeviceRegisterRequest::USER; |
254 #else | 193 #else |
255 em::DeviceRegisterRequest::BROWSER; | 194 em::DeviceRegisterRequest::BROWSER; |
256 #endif | 195 #endif |
257 policy_manager->core()->client()->Register( | 196 policy_manager->core()->client()->Register( |
258 registration_type, "bogus", std::string(), false); | 197 registration_type, "bogus", std::string(), false); |
259 run_loop.Run(); | 198 run_loop.Run(); |
260 Mock::VerifyAndClearExpectations(&observer); | 199 Mock::VerifyAndClearExpectations(&observer); |
261 policy_manager->core()->client()->RemoveObserver(&observer); | 200 policy_manager->core()->client()->RemoveObserver(&observer); |
262 EXPECT_TRUE(policy_manager->core()->client()->is_registered()); | 201 EXPECT_TRUE(policy_manager->core()->client()->is_registered()); |
263 } | |
264 | 202 |
265 #if defined(OS_CHROMEOS) | 203 #if defined(OS_CHROMEOS) |
266 base::FilePath user_policy_key_dir() { | 204 // Get the path to the user policy key file. |
267 return temp_dir_.path().AppendASCII("user_policy"); | 205 base::FilePath user_policy_key_dir; |
| 206 ASSERT_TRUE( |
| 207 PathService::Get(chromeos::DIR_USER_POLICY_KEYS, &user_policy_key_dir)); |
| 208 std::string sanitized_username = |
| 209 chromeos::CryptohomeClient::GetStubSanitizedUsername(GetTestUser()); |
| 210 user_policy_key_file_ = user_policy_key_dir.AppendASCII(sanitized_username) |
| 211 .AppendASCII("policy.pub"); |
| 212 #endif |
268 } | 213 } |
269 | 214 |
270 base::FilePath user_policy_key_file() { | |
271 return user_policy_key_dir().AppendASCII(kSanitizedUsername) | |
272 .AppendASCII("policy.pub"); | |
273 } | |
274 #endif | |
275 | |
276 PolicyService* GetPolicyService() { | 215 PolicyService* GetPolicyService() { |
277 ProfilePolicyConnector* profile_connector = | 216 ProfilePolicyConnector* profile_connector = |
278 ProfilePolicyConnectorFactory::GetForProfile(browser()->profile()); | 217 ProfilePolicyConnectorFactory::GetForProfile(browser()->profile()); |
279 return profile_connector->policy_service(); | 218 return profile_connector->policy_service(); |
280 } | 219 } |
281 | 220 |
282 void SetServerPolicy(const std::string& policy) { | 221 void SetServerPolicy(const std::string& policy) { |
283 int result = file_util::WriteFile(policy_file_path(), policy.data(), | 222 int result = file_util::WriteFile(policy_file_path(), policy.data(), |
284 policy.size()); | 223 policy.size()); |
285 ASSERT_EQ(static_cast<int>(policy.size()), result); | 224 ASSERT_EQ(static_cast<int>(policy.size()), result); |
286 } | 225 } |
287 | 226 |
288 base::FilePath policy_file_path() const { | 227 base::FilePath policy_file_path() const { |
289 return temp_dir_.path().AppendASCII("policy.json"); | 228 return temp_dir_.path().AppendASCII("policy.json"); |
290 } | 229 } |
291 | 230 |
292 base::ScopedTempDir temp_dir_; | 231 base::ScopedTempDir temp_dir_; |
293 scoped_ptr<LocalPolicyTestServer> test_server_; | 232 scoped_ptr<LocalPolicyTestServer> test_server_; |
294 | 233 base::FilePath user_policy_key_file_; |
295 #if defined(OS_CHROMEOS) | |
296 std::string session_manager_user_policy_; | |
297 chromeos::MockDBusThreadManager* mock_dbus_thread_manager_; | |
298 #endif | |
299 }; | 234 }; |
300 | 235 |
301 IN_PROC_BROWSER_TEST_F(CloudPolicyTest, FetchPolicy) { | 236 IN_PROC_BROWSER_TEST_F(CloudPolicyTest, FetchPolicy) { |
302 PolicyService* policy_service = GetPolicyService(); | 237 PolicyService* policy_service = GetPolicyService(); |
303 { | 238 { |
304 base::RunLoop run_loop; | 239 base::RunLoop run_loop; |
305 // This does the initial fetch and stores the initial key. | 240 // This does the initial fetch and stores the initial key. |
306 policy_service->RefreshPolicies(run_loop.QuitClosure()); | 241 policy_service->RefreshPolicies(run_loop.QuitClosure()); |
307 run_loop.Run(); | 242 run_loop.Run(); |
308 } | 243 } |
(...skipping 21 matching lines...) Expand all Loading... |
330 { | 265 { |
331 base::RunLoop run_loop; | 266 base::RunLoop run_loop; |
332 // This does the initial fetch and stores the initial key. | 267 // This does the initial fetch and stores the initial key. |
333 policy_service->RefreshPolicies(run_loop.QuitClosure()); | 268 policy_service->RefreshPolicies(run_loop.QuitClosure()); |
334 run_loop.Run(); | 269 run_loop.Run(); |
335 } | 270 } |
336 | 271 |
337 // Read the initial key. | 272 // Read the initial key. |
338 std::string initial_key; | 273 std::string initial_key; |
339 ASSERT_TRUE( | 274 ASSERT_TRUE( |
340 file_util::ReadFileToString(user_policy_key_file(), &initial_key)); | 275 file_util::ReadFileToString(user_policy_key_file_, &initial_key)); |
341 | 276 |
342 PolicyMap empty; | 277 PolicyMap empty; |
343 EXPECT_TRUE(empty.Equals(policy_service->GetPolicies( | 278 EXPECT_TRUE(empty.Equals(policy_service->GetPolicies( |
344 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())))); | 279 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())))); |
345 | 280 |
346 // Set the new policies and a new key at the server. | 281 // Set the new policies and a new key at the server. |
347 ASSERT_NO_FATAL_FAILURE(SetServerPolicy(GetTestPolicy(1))); | 282 ASSERT_NO_FATAL_FAILURE(SetServerPolicy(GetTestPolicy(1))); |
348 PolicyMap expected; | 283 PolicyMap expected; |
349 GetExpectedTestPolicy(&expected); | 284 GetExpectedTestPolicy(&expected); |
350 { | 285 { |
351 base::RunLoop run_loop; | 286 base::RunLoop run_loop; |
352 // This fetches the new policies and does a key rotation. | 287 // This fetches the new policies and does a key rotation. |
353 policy_service->RefreshPolicies(run_loop.QuitClosure()); | 288 policy_service->RefreshPolicies(run_loop.QuitClosure()); |
354 run_loop.Run(); | 289 run_loop.Run(); |
355 } | 290 } |
356 EXPECT_TRUE(expected.Equals(policy_service->GetPolicies( | 291 EXPECT_TRUE(expected.Equals(policy_service->GetPolicies( |
357 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())))); | 292 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())))); |
358 | 293 |
359 // Verify that the key was rotated. | 294 // Verify that the key was rotated. |
360 std::string rotated_key; | 295 std::string rotated_key; |
361 ASSERT_TRUE( | 296 ASSERT_TRUE( |
362 file_util::ReadFileToString(user_policy_key_file(), &rotated_key)); | 297 file_util::ReadFileToString(user_policy_key_file_, &rotated_key)); |
363 EXPECT_NE(rotated_key, initial_key); | 298 EXPECT_NE(rotated_key, initial_key); |
364 | 299 |
365 // Another refresh using the same key won't rotate it again. | 300 // Another refresh using the same key won't rotate it again. |
366 { | 301 { |
367 base::RunLoop run_loop; | 302 base::RunLoop run_loop; |
368 policy_service->RefreshPolicies(run_loop.QuitClosure()); | 303 policy_service->RefreshPolicies(run_loop.QuitClosure()); |
369 run_loop.Run(); | 304 run_loop.Run(); |
370 } | 305 } |
371 EXPECT_TRUE(expected.Equals(policy_service->GetPolicies( | 306 EXPECT_TRUE(expected.Equals(policy_service->GetPolicies( |
372 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())))); | 307 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())))); |
373 std::string current_key; | 308 std::string current_key; |
374 ASSERT_TRUE( | 309 ASSERT_TRUE( |
375 file_util::ReadFileToString(user_policy_key_file(), ¤t_key)); | 310 file_util::ReadFileToString(user_policy_key_file_, ¤t_key)); |
376 EXPECT_EQ(rotated_key, current_key); | 311 EXPECT_EQ(rotated_key, current_key); |
377 } | 312 } |
378 #endif | 313 #endif |
379 | 314 |
380 TEST(CloudPolicyProtoTest, VerifyProtobufEquivalence) { | 315 TEST(CloudPolicyProtoTest, VerifyProtobufEquivalence) { |
381 // There are 2 protobufs that can be used for user cloud policy: | 316 // There are 2 protobufs that can be used for user cloud policy: |
382 // cloud_policy.proto and chrome_settings.proto. chrome_settings.proto is the | 317 // cloud_policy.proto and chrome_settings.proto. chrome_settings.proto is the |
383 // version used by the server, but generates one proto message per policy; to | 318 // version used by the server, but generates one proto message per policy; to |
384 // save binary size on the client, the other version shares proto messages for | 319 // save binary size on the client, the other version shares proto messages for |
385 // policies of the same type. They generate the same bytes on the wire though, | 320 // policies of the same type. They generate the same bytes on the wire though, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 | 355 |
421 // They should now serialize to the same bytes. | 356 // They should now serialize to the same bytes. |
422 std::string chrome_settings_serialized; | 357 std::string chrome_settings_serialized; |
423 std::string cloud_policy_serialized; | 358 std::string cloud_policy_serialized; |
424 ASSERT_TRUE(chrome_settings.SerializeToString(&chrome_settings_serialized)); | 359 ASSERT_TRUE(chrome_settings.SerializeToString(&chrome_settings_serialized)); |
425 ASSERT_TRUE(cloud_policy.SerializeToString(&cloud_policy_serialized)); | 360 ASSERT_TRUE(cloud_policy.SerializeToString(&cloud_policy_serialized)); |
426 EXPECT_EQ(chrome_settings_serialized, cloud_policy_serialized); | 361 EXPECT_EQ(chrome_settings_serialized, cloud_policy_serialized); |
427 } | 362 } |
428 | 363 |
429 } // namespace policy | 364 } // namespace policy |
OLD | NEW |