OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/bind.h" | |
8 #include "base/command_line.h" | 7 #include "base/command_line.h" |
9 #include "base/file_util.h" | |
10 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
11 #include "base/files/scoped_temp_dir.h" | |
12 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
13 #include "base/message_loop.h" | |
14 #include "base/path_service.h" | 10 #include "base/path_service.h" |
15 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
16 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/extensions/extension_browsertest.h" | 13 #include "chrome/browser/extensions/extension_browsertest.h" |
18 #include "chrome/browser/extensions/extension_test_message_listener.h" | 14 #include "chrome/browser/extensions/extension_test_message_listener.h" |
19 #include "chrome/browser/policy/browser_policy_connector.h" | 15 #include "chrome/browser/policy/browser_policy_connector.h" |
20 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | 16 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" |
21 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" | 17 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" |
22 #include "chrome/browser/policy/policy_service.h" | 18 #include "chrome/browser/policy/policy_service.h" |
23 #include "chrome/browser/policy/profile_policy_connector.h" | 19 #include "chrome/browser/policy/profile_policy_connector.h" |
24 #include "chrome/browser/policy/profile_policy_connector_factory.h" | 20 #include "chrome/browser/policy/profile_policy_connector_factory.h" |
25 #include "chrome/browser/policy/proto/cloud/chrome_extension_policy.pb.h" | 21 #include "chrome/browser/policy/proto/cloud/chrome_extension_policy.pb.h" |
26 #include "chrome/browser/policy/test/local_policy_test_server.h" | 22 #include "chrome/browser/policy/test/local_policy_test_server.h" |
27 #include "chrome/browser/policy/test_utils.h" | 23 #include "chrome/browser/policy/test_utils.h" |
28 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
29 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
30 #include "chrome/common/chrome_switches.h" | 26 #include "chrome/common/chrome_switches.h" |
31 #include "chrome/common/extensions/extension.h" | 27 #include "chrome/common/extensions/extension.h" |
32 #include "policy/proto/cloud_policy.pb.h" | 28 #include "policy/proto/cloud_policy.pb.h" |
33 #include "testing/gmock/include/gmock/gmock.h" | 29 #include "testing/gmock/include/gmock/gmock.h" |
34 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
35 | 31 |
36 #if defined(OS_CHROMEOS) | 32 #if defined(OS_CHROMEOS) |
37 #include "chrome/browser/chromeos/login/user_manager.h" | |
38 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h" | 33 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h" |
39 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chrom
eos.h" | 34 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chrom
eos.h" |
40 #include "chrome/common/chrome_paths.h" | 35 #include "chrome/common/chrome_paths.h" |
41 #include "chromeos/chromeos_paths.h" | |
42 #include "chromeos/chromeos_switches.h" | 36 #include "chromeos/chromeos_switches.h" |
43 #include "chromeos/dbus/mock_cryptohome_client.h" | |
44 #include "chromeos/dbus/mock_dbus_thread_manager.h" | |
45 #include "chromeos/dbus/mock_session_manager_client.h" | |
46 #include "chromeos/dbus/mock_update_engine_client.h" | |
47 #else | 37 #else |
48 #include "chrome/browser/policy/cloud/user_cloud_policy_manager.h" | 38 #include "chrome/browser/policy/cloud/user_cloud_policy_manager.h" |
49 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h" | 39 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h" |
50 #include "chrome/browser/signin/signin_manager.h" | 40 #include "chrome/browser/signin/signin_manager.h" |
51 #include "chrome/browser/signin/signin_manager_factory.h" | 41 #include "chrome/browser/signin/signin_manager_factory.h" |
52 #endif | 42 #endif |
53 | 43 |
54 using testing::InvokeWithoutArgs; | 44 using testing::InvokeWithoutArgs; |
55 using testing::Mock; | 45 using testing::Mock; |
56 using testing::Return; | 46 using testing::Return; |
(...skipping 27 matching lines...) Expand all Loading... |
84 | 74 |
85 const char kTestPolicy2[] = | 75 const char kTestPolicy2[] = |
86 "{" | 76 "{" |
87 " \"Another\": {" | 77 " \"Another\": {" |
88 " \"Value\": \"turn_it_off\"" | 78 " \"Value\": \"turn_it_off\"" |
89 " }" | 79 " }" |
90 "}"; | 80 "}"; |
91 | 81 |
92 const char kTestPolicy2JSON[] = "{\"Another\":\"turn_it_off\"}"; | 82 const char kTestPolicy2JSON[] = "{\"Another\":\"turn_it_off\"}"; |
93 | 83 |
94 #if defined(OS_CHROMEOS) | |
95 | |
96 const char kSanitizedUsername[] = "0123456789ABCDEF0123456789ABCDEF01234567"; | |
97 | |
98 ACTION(GetSanitizedUsername) { | |
99 MessageLoop::current()->PostTask( | |
100 FROM_HERE, | |
101 base::Bind(arg1, chromeos::DBUS_METHOD_CALL_SUCCESS, kSanitizedUsername)); | |
102 } | |
103 | |
104 ACTION_P(RetrieveUserPolicy, storage) { | |
105 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(arg0, *storage)); | |
106 } | |
107 | |
108 ACTION_P2(StoreUserPolicy, storage, user_policy_key_file) { | |
109 // The session_manager stores a copy of the policy key at | |
110 // /var/run/user_policy/$hash/policy.pub. Simulate that behavior here, so | |
111 // that the policy signature can be validated. | |
112 em::PolicyFetchResponse policy; | |
113 ASSERT_TRUE(policy.ParseFromString(arg0)); | |
114 if (policy.has_new_public_key()) { | |
115 ASSERT_TRUE(file_util::CreateDirectory(user_policy_key_file.DirName())); | |
116 int result = file_util::WriteFile( | |
117 user_policy_key_file, | |
118 policy.new_public_key().data(), | |
119 policy.new_public_key().size()); | |
120 ASSERT_EQ(static_cast<int>(policy.new_public_key().size()), result); | |
121 } | |
122 | |
123 *storage = arg0; | |
124 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(arg1, true)); | |
125 } | |
126 | |
127 #else | |
128 | |
129 const char kTestUser[] = "user@example.com"; | |
130 | |
131 #endif // OS_CHROMEOS | |
132 | |
133 } // namespace | 84 } // namespace |
134 | 85 |
135 class ComponentCloudPolicyTest : public ExtensionBrowserTest { | 86 class ComponentCloudPolicyTest : public ExtensionBrowserTest { |
136 protected: | 87 protected: |
137 ComponentCloudPolicyTest() {} | 88 ComponentCloudPolicyTest() {} |
138 virtual ~ComponentCloudPolicyTest() {} | 89 virtual ~ComponentCloudPolicyTest() {} |
139 | 90 |
140 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 91 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
141 ExtensionBrowserTest::SetUpCommandLine(command_line); | 92 ExtensionBrowserTest::SetUpCommandLine(command_line); |
142 #if defined(OS_CHROMEOS) | 93 #if defined(OS_CHROMEOS) |
143 // ExtensionBrowserTest sets the login users to a non-managed value; | 94 // ExtensionBrowserTest sets the login users to a non-managed value; |
144 // replace it. This is the default username sent in policy blobs from the | 95 // replace it. This is the default username sent in policy blobs from the |
145 // testserver. | 96 // testserver. |
146 command_line->AppendSwitchASCII( | 97 command_line->AppendSwitchASCII( |
147 chromeos::switches::kLoginUser, "user@example.com"); | 98 chromeos::switches::kLoginUser, "user@example.com"); |
148 #endif | 99 #endif |
149 } | 100 } |
150 | 101 |
151 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | 102 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
152 test_server_.RegisterClient(kDMToken, kDeviceID); | 103 test_server_.RegisterClient(kDMToken, kDeviceID); |
153 EXPECT_TRUE(test_server_.UpdatePolicyData( | 104 EXPECT_TRUE(test_server_.UpdatePolicyData( |
154 dm_protocol::kChromeExtensionPolicyType, kTestExtension, kTestPolicy)); | 105 dm_protocol::kChromeExtensionPolicyType, kTestExtension, kTestPolicy)); |
155 ASSERT_TRUE(test_server_.Start()); | 106 ASSERT_TRUE(test_server_.Start()); |
156 | 107 |
157 std::string url = test_server_.GetServiceURL().spec(); | 108 std::string url = test_server_.GetServiceURL().spec(); |
158 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 109 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
159 command_line->AppendSwitchASCII(switches::kDeviceManagementUrl, url); | 110 command_line->AppendSwitchASCII(switches::kDeviceManagementUrl, url); |
160 command_line->AppendSwitch(switches::kEnableComponentCloudPolicy); | 111 command_line->AppendSwitch(switches::kEnableComponentCloudPolicy); |
161 | 112 |
162 #if defined(OS_CHROMEOS) | |
163 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
164 base::FilePath user_key_path = | |
165 temp_dir_.path().AppendASCII(kSanitizedUsername) | |
166 .AppendASCII("policy.pub"); | |
167 ASSERT_TRUE(PathService::Override(chromeos::DIR_USER_POLICY_KEYS, | |
168 temp_dir_.path())); | |
169 | |
170 mock_dbus_thread_manager_ = new chromeos::MockDBusThreadManager(); | |
171 chromeos::DBusThreadManager::InitializeForTesting( | |
172 mock_dbus_thread_manager_); | |
173 EXPECT_CALL(*mock_dbus_thread_manager_->mock_cryptohome_client(), | |
174 GetSanitizedUsername(_, _)) | |
175 .WillRepeatedly(GetSanitizedUsername()); | |
176 EXPECT_CALL(*mock_dbus_thread_manager_->mock_session_manager_client(), | |
177 StoreUserPolicy(_, _)) | |
178 .WillRepeatedly(StoreUserPolicy(&session_manager_user_policy_, | |
179 user_key_path)); | |
180 EXPECT_CALL(*mock_dbus_thread_manager_->mock_session_manager_client(), | |
181 RetrieveUserPolicy(_)) | |
182 .WillRepeatedly(RetrieveUserPolicy(&session_manager_user_policy_)); | |
183 #endif // OS_CHROMEOS | |
184 | |
185 ExtensionBrowserTest::SetUpInProcessBrowserTestFixture(); | 113 ExtensionBrowserTest::SetUpInProcessBrowserTestFixture(); |
186 } | 114 } |
187 | 115 |
188 virtual void SetUpOnMainThread() OVERRIDE { | 116 virtual void SetUpOnMainThread() OVERRIDE { |
189 ASSERT_TRUE(PolicyServiceIsEmpty(g_browser_process->policy_service())) | 117 ASSERT_TRUE(PolicyServiceIsEmpty(g_browser_process->policy_service())) |
190 << "Pre-existing policies in this machine will make this test fail."; | 118 << "Pre-existing policies in this machine will make this test fail."; |
191 | 119 |
192 // Install the initial extension. | 120 // Install the initial extension. |
193 ExtensionTestMessageListener ready_listener("ready", true); | 121 ExtensionTestMessageListener ready_listener("ready", true); |
194 event_listener_.reset(new ExtensionTestMessageListener("event", true)); | 122 event_listener_.reset(new ExtensionTestMessageListener("event", true)); |
(...skipping 10 matching lines...) Expand all Loading... |
205 UserCloudPolicyManagerChromeOS* policy_manager = | 133 UserCloudPolicyManagerChromeOS* policy_manager = |
206 UserCloudPolicyManagerFactoryChromeOS::GetForProfile( | 134 UserCloudPolicyManagerFactoryChromeOS::GetForProfile( |
207 browser()->profile()); | 135 browser()->profile()); |
208 ASSERT_TRUE(policy_manager); | 136 ASSERT_TRUE(policy_manager); |
209 #else | 137 #else |
210 // Mock a signed-in user. This is used by the UserCloudPolicyStore to pass | 138 // Mock a signed-in user. This is used by the UserCloudPolicyStore to pass |
211 // the username to the UserCloudPolicyValidator. | 139 // the username to the UserCloudPolicyValidator. |
212 SigninManager* signin_manager = | 140 SigninManager* signin_manager = |
213 SigninManagerFactory::GetForProfile(browser()->profile()); | 141 SigninManagerFactory::GetForProfile(browser()->profile()); |
214 ASSERT_TRUE(signin_manager); | 142 ASSERT_TRUE(signin_manager); |
215 signin_manager->SetAuthenticatedUsername(kTestUser); | 143 signin_manager->SetAuthenticatedUsername("user@example.com"); |
216 | 144 |
217 UserCloudPolicyManager* policy_manager = | 145 UserCloudPolicyManager* policy_manager = |
218 UserCloudPolicyManagerFactory::GetForProfile(browser()->profile()); | 146 UserCloudPolicyManagerFactory::GetForProfile(browser()->profile()); |
219 ASSERT_TRUE(policy_manager); | 147 ASSERT_TRUE(policy_manager); |
220 policy_manager->Connect(g_browser_process->local_state(), | 148 policy_manager->Connect(g_browser_process->local_state(), |
221 UserCloudPolicyManager::CreateCloudPolicyClient( | 149 UserCloudPolicyManager::CreateCloudPolicyClient( |
222 connector->device_management_service()).Pass()); | 150 connector->device_management_service()).Pass()); |
223 #endif // defined(OS_CHROMEOS) | 151 #endif // defined(OS_CHROMEOS) |
224 | 152 |
225 // Register the cloud policy client. | 153 // Register the cloud policy client. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 ProfilePolicyConnectorFactory::GetForProfile(browser()->profile()); | 189 ProfilePolicyConnectorFactory::GetForProfile(browser()->profile()); |
262 PolicyService* policy_service = profile_connector->policy_service(); | 190 PolicyService* policy_service = profile_connector->policy_service(); |
263 base::RunLoop run_loop; | 191 base::RunLoop run_loop; |
264 policy_service->RefreshPolicies(run_loop.QuitClosure()); | 192 policy_service->RefreshPolicies(run_loop.QuitClosure()); |
265 run_loop.Run(); | 193 run_loop.Run(); |
266 } | 194 } |
267 | 195 |
268 LocalPolicyTestServer test_server_; | 196 LocalPolicyTestServer test_server_; |
269 scoped_refptr<const extensions::Extension> extension_; | 197 scoped_refptr<const extensions::Extension> extension_; |
270 scoped_ptr<ExtensionTestMessageListener> event_listener_; | 198 scoped_ptr<ExtensionTestMessageListener> event_listener_; |
271 | |
272 #if defined(OS_CHROMEOS) | |
273 base::ScopedTempDir temp_dir_; | |
274 std::string session_manager_user_policy_; | |
275 chromeos::MockDBusThreadManager* mock_dbus_thread_manager_; | |
276 #endif | |
277 }; | 199 }; |
278 | 200 |
279 // TODO(joaodasilva): enable these for other platforms once ready. | 201 // TODO(joaodasilva): enable these for other platforms once ready. |
280 #if defined(OS_CHROMEOS) | 202 #if defined(OS_CHROMEOS) |
281 | 203 |
282 IN_PROC_BROWSER_TEST_F(ComponentCloudPolicyTest, FetchExtensionPolicy) { | 204 IN_PROC_BROWSER_TEST_F(ComponentCloudPolicyTest, FetchExtensionPolicy) { |
283 // Read the initial policy. | 205 // Read the initial policy. |
284 ExtensionTestMessageListener policy_listener(kTestPolicyJSON, true); | 206 ExtensionTestMessageListener policy_listener(kTestPolicyJSON, true); |
285 event_listener_->Reply("get-policy-Name"); | 207 event_listener_->Reply("get-policy-Name"); |
286 EXPECT_TRUE(policy_listener.WaitUntilSatisfied()); | 208 EXPECT_TRUE(policy_listener.WaitUntilSatisfied()); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 ASSERT_EQ(kTestExtension2, extension2->id()); | 247 ASSERT_EQ(kTestExtension2, extension2->id()); |
326 | 248 |
327 // This extension only sends the 'policy' signal once it receives the policy, | 249 // This extension only sends the 'policy' signal once it receives the policy, |
328 // and after verifying it has the expected value. Otherwise it sends 'fail'. | 250 // and after verifying it has the expected value. Otherwise it sends 'fail'. |
329 EXPECT_TRUE(result_listener.WaitUntilSatisfied()); | 251 EXPECT_TRUE(result_listener.WaitUntilSatisfied()); |
330 } | 252 } |
331 | 253 |
332 #endif // OS_CHROMEOS | 254 #endif // OS_CHROMEOS |
333 | 255 |
334 } // namespace policy | 256 } // namespace policy |
OLD | NEW |