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" | |
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 | 46 #else |
51 #include "chrome/browser/policy/cloud/user_cloud_policy_manager.h" | 47 #include "chrome/browser/policy/cloud/user_cloud_policy_manager.h" |
52 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h" | 48 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h" |
53 #include "chrome/browser/signin/signin_manager.h" | 49 #include "chrome/browser/signin/signin_manager.h" |
54 #include "chrome/browser/signin/signin_manager_factory.h" | 50 #include "chrome/browser/signin/signin_manager_factory.h" |
55 #endif | 51 #endif |
56 | 52 |
57 using testing::AnyNumber; | 53 using testing::AnyNumber; |
58 using testing::InvokeWithoutArgs; | 54 using testing::InvokeWithoutArgs; |
59 using testing::Mock; | 55 using testing::Mock; |
60 using testing::Return; | 56 using testing::Return; |
61 using testing::_; | 57 using testing::_; |
62 | 58 |
63 namespace em = enterprise_management; | 59 namespace em = enterprise_management; |
64 | 60 |
65 namespace policy { | 61 namespace policy { |
66 | 62 |
67 namespace { | 63 namespace { |
68 | 64 |
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() { | 65 const char* GetTestUser() { |
105 #if defined(OS_CHROMEOS) | 66 #if defined(OS_CHROMEOS) |
106 return chromeos::UserManager::kStubUser; | 67 return chromeos::UserManager::kStubUser; |
107 #else | 68 #else |
108 return "user@example.com"; | 69 return "user@example.com"; |
109 #endif | 70 #endif |
110 } | 71 } |
111 | 72 |
112 std::string GetEmptyPolicy() { | 73 std::string GetEmptyPolicy() { |
113 const char kEmptyPolicy[] = | 74 const char kEmptyPolicy[] = |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 138 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
178 ASSERT_NO_FATAL_FAILURE(SetServerPolicy(GetEmptyPolicy())); | 139 ASSERT_NO_FATAL_FAILURE(SetServerPolicy(GetEmptyPolicy())); |
179 | 140 |
180 test_server_.reset(new LocalPolicyTestServer(policy_file_path())); | 141 test_server_.reset(new LocalPolicyTestServer(policy_file_path())); |
181 ASSERT_TRUE(test_server_->Start()); | 142 ASSERT_TRUE(test_server_->Start()); |
182 | 143 |
183 std::string url = test_server_->GetServiceURL().spec(); | 144 std::string url = test_server_->GetServiceURL().spec(); |
184 | 145 |
185 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 146 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
186 command_line->AppendSwitchASCII(switches::kDeviceManagementUrl, url); | 147 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 } | 148 } |
211 | 149 |
212 virtual void SetUpOnMainThread() OVERRIDE { | 150 virtual void SetUpOnMainThread() OVERRIDE { |
213 ASSERT_TRUE(PolicyServiceIsEmpty(g_browser_process->policy_service())) | 151 ASSERT_TRUE(PolicyServiceIsEmpty(g_browser_process->policy_service())) |
214 << "Pre-existing policies in this machine will make this test fail."; | 152 << "Pre-existing policies in this machine will make this test fail."; |
215 | 153 |
216 BrowserPolicyConnector* connector = | 154 BrowserPolicyConnector* connector = |
217 g_browser_process->browser_policy_connector(); | 155 g_browser_process->browser_policy_connector(); |
218 connector->ScheduleServiceInitialization(0); | 156 connector->ScheduleServiceInitialization(0); |
219 | 157 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 #endif | 194 #endif |
257 policy_manager->core()->client()->Register( | 195 policy_manager->core()->client()->Register( |
258 registration_type, "bogus", std::string(), false); | 196 registration_type, "bogus", std::string(), false); |
259 run_loop.Run(); | 197 run_loop.Run(); |
260 Mock::VerifyAndClearExpectations(&observer); | 198 Mock::VerifyAndClearExpectations(&observer); |
261 policy_manager->core()->client()->RemoveObserver(&observer); | 199 policy_manager->core()->client()->RemoveObserver(&observer); |
262 EXPECT_TRUE(policy_manager->core()->client()->is_registered()); | 200 EXPECT_TRUE(policy_manager->core()->client()->is_registered()); |
263 } | 201 } |
264 | 202 |
265 #if defined(OS_CHROMEOS) | 203 #if defined(OS_CHROMEOS) |
266 base::FilePath user_policy_key_dir() { | |
267 return temp_dir_.path().AppendASCII("user_policy"); | |
268 } | |
269 | |
270 base::FilePath user_policy_key_file() { | 204 base::FilePath user_policy_key_file() { |
271 return user_policy_key_dir().AppendASCII(kSanitizedUsername) | 205 base::FilePath user_policy_key_dir; |
272 .AppendASCII("policy.pub"); | 206 PathService::Get(chromeos::DIR_USER_POLICY_KEYS, &user_policy_key_dir); |
207 return user_policy_key_dir | |
208 .AppendASCII(std::string(GetTestUser()) + "-profile") | |
Mattias Nissler (ping if slow)
2013/05/10 12:40:15
another call the the common helper here :)
Joao da Silva
2013/05/13 09:39:23
Done.
| |
209 .AppendASCII("policy.pub"); | |
273 } | 210 } |
274 #endif | 211 #endif |
275 | 212 |
276 PolicyService* GetPolicyService() { | 213 PolicyService* GetPolicyService() { |
277 ProfilePolicyConnector* profile_connector = | 214 ProfilePolicyConnector* profile_connector = |
278 ProfilePolicyConnectorFactory::GetForProfile(browser()->profile()); | 215 ProfilePolicyConnectorFactory::GetForProfile(browser()->profile()); |
279 return profile_connector->policy_service(); | 216 return profile_connector->policy_service(); |
280 } | 217 } |
281 | 218 |
282 void SetServerPolicy(const std::string& policy) { | 219 void SetServerPolicy(const std::string& policy) { |
283 int result = file_util::WriteFile(policy_file_path(), policy.data(), | 220 int result = file_util::WriteFile(policy_file_path(), policy.data(), |
284 policy.size()); | 221 policy.size()); |
285 ASSERT_EQ(static_cast<int>(policy.size()), result); | 222 ASSERT_EQ(static_cast<int>(policy.size()), result); |
286 } | 223 } |
287 | 224 |
288 base::FilePath policy_file_path() const { | 225 base::FilePath policy_file_path() const { |
289 return temp_dir_.path().AppendASCII("policy.json"); | 226 return temp_dir_.path().AppendASCII("policy.json"); |
290 } | 227 } |
291 | 228 |
292 base::ScopedTempDir temp_dir_; | 229 base::ScopedTempDir temp_dir_; |
293 scoped_ptr<LocalPolicyTestServer> test_server_; | 230 scoped_ptr<LocalPolicyTestServer> test_server_; |
294 | |
295 #if defined(OS_CHROMEOS) | |
296 std::string session_manager_user_policy_; | |
297 chromeos::MockDBusThreadManager* mock_dbus_thread_manager_; | |
298 #endif | |
299 }; | 231 }; |
300 | 232 |
301 IN_PROC_BROWSER_TEST_F(CloudPolicyTest, FetchPolicy) { | 233 IN_PROC_BROWSER_TEST_F(CloudPolicyTest, FetchPolicy) { |
302 PolicyService* policy_service = GetPolicyService(); | 234 PolicyService* policy_service = GetPolicyService(); |
303 { | 235 { |
304 base::RunLoop run_loop; | 236 base::RunLoop run_loop; |
305 // This does the initial fetch and stores the initial key. | 237 // This does the initial fetch and stores the initial key. |
306 policy_service->RefreshPolicies(run_loop.QuitClosure()); | 238 policy_service->RefreshPolicies(run_loop.QuitClosure()); |
307 run_loop.Run(); | 239 run_loop.Run(); |
308 } | 240 } |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
420 | 352 |
421 // They should now serialize to the same bytes. | 353 // They should now serialize to the same bytes. |
422 std::string chrome_settings_serialized; | 354 std::string chrome_settings_serialized; |
423 std::string cloud_policy_serialized; | 355 std::string cloud_policy_serialized; |
424 ASSERT_TRUE(chrome_settings.SerializeToString(&chrome_settings_serialized)); | 356 ASSERT_TRUE(chrome_settings.SerializeToString(&chrome_settings_serialized)); |
425 ASSERT_TRUE(cloud_policy.SerializeToString(&cloud_policy_serialized)); | 357 ASSERT_TRUE(cloud_policy.SerializeToString(&cloud_policy_serialized)); |
426 EXPECT_EQ(chrome_settings_serialized, cloud_policy_serialized); | 358 EXPECT_EQ(chrome_settings_serialized, cloud_policy_serialized); |
427 } | 359 } |
428 | 360 |
429 } // namespace policy | 361 } // namespace policy |
OLD | NEW |