| 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/base_paths.h" | 5 #include "base/base_paths.h" |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.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" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/policy/browser_policy_connector.h" | 14 #include "chrome/browser/policy/browser_policy_connector.h" |
| 15 #include "chrome/browser/policy/cloud_policy_client.h" | 15 #include "chrome/browser/policy/cloud_policy_client.h" |
| 16 #include "chrome/browser/policy/cloud_policy_constants.h" | 16 #include "chrome/browser/policy/cloud_policy_constants.h" |
| 17 #include "chrome/browser/policy/cloud_policy_data_store.h" | |
| 18 #include "chrome/browser/policy/policy_map.h" | 17 #include "chrome/browser/policy/policy_map.h" |
| 19 #include "chrome/browser/policy/policy_service.h" | 18 #include "chrome/browser/policy/policy_service.h" |
| 20 #include "chrome/browser/policy/proto/chrome_settings.pb.h" | 19 #include "chrome/browser/policy/proto/chrome_settings.pb.h" |
| 21 #include "chrome/browser/policy/proto/cloud_policy.pb.h" | 20 #include "chrome/browser/policy/proto/cloud_policy.pb.h" |
| 22 #include "chrome/browser/prefs/pref_service.h" | 21 #include "chrome/browser/prefs/pref_service.h" |
| 23 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 24 #include "chrome/browser/ui/browser.h" | 23 #include "chrome/browser/ui/browser.h" |
| 25 #include "chrome/common/chrome_notification_types.h" | 24 #include "chrome/common/chrome_notification_types.h" |
| 26 #include "chrome/common/chrome_switches.h" | 25 #include "chrome/common/chrome_switches.h" |
| 27 #include "chrome/test/base/in_process_browser_test.h" | 26 #include "chrome/test/base/in_process_browser_test.h" |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 " }" | 101 " }" |
| 103 " }," | 102 " }," |
| 104 " \"managed_users\": [ \"*\" ]," | 103 " \"managed_users\": [ \"*\" ]," |
| 105 " \"policy_user\": \"%s\"" | 104 " \"policy_user\": \"%s\"" |
| 106 "}"; | 105 "}"; |
| 107 | 106 |
| 108 return base::StringPrintf(kTestPolicy, dm_protocol::kChromeUserPolicyType, | 107 return base::StringPrintf(kTestPolicy, dm_protocol::kChromeUserPolicyType, |
| 109 GetTestUser()); | 108 GetTestUser()); |
| 110 } | 109 } |
| 111 | 110 |
| 112 #if defined(OS_CHROMEOS) | |
| 113 void SetUpOldStackBeforeCreatingBrowser() { | |
| 114 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 115 command_line->AppendSwitch(switches::kDisableCloudPolicyService); | |
| 116 } | |
| 117 | |
| 118 void SetUpOldStackAfterCreatingBrowser(Browser* browser) { | |
| 119 // Flush the token cache loading. | |
| 120 content::RunAllPendingInMessageLoop(content::BrowserThread::FILE); | |
| 121 content::RunAllPendingInMessageLoop(); | |
| 122 // Set a fake gaia token. | |
| 123 BrowserPolicyConnector* connector = | |
| 124 g_browser_process->browser_policy_connector(); | |
| 125 CloudPolicyDataStore* store = connector->GetUserCloudPolicyDataStore(); | |
| 126 ASSERT_TRUE(store); | |
| 127 store->SetupForTesting("", "bogus", GetTestUser(), "bogus", true); | |
| 128 } | |
| 129 #endif | |
| 130 | |
| 131 void SetUpNewStackBeforeCreatingBrowser() { | |
| 132 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 133 command_line->AppendSwitch(switches::kLoadCloudPolicyOnSignin); | |
| 134 } | |
| 135 | |
| 136 void SetUpNewStackAfterCreatingBrowser(Browser* browser) { | |
| 137 BrowserPolicyConnector* connector = | |
| 138 g_browser_process->browser_policy_connector(); | |
| 139 connector->ScheduleServiceInitialization(0); | |
| 140 | |
| 141 #if defined(OS_CHROMEOS) | |
| 142 UserCloudPolicyManagerChromeOS* policy_manager = | |
| 143 connector->GetUserCloudPolicyManager(); | |
| 144 ASSERT_TRUE(policy_manager); | |
| 145 #else | |
| 146 // Mock a signed-in user. This is used by the UserCloudPolicyStore to pass the | |
| 147 // username to the UserCloudPolicyValidator. | |
| 148 SigninManager* signin_manager = | |
| 149 SigninManagerFactory::GetForProfile(browser->profile()); | |
| 150 ASSERT_TRUE(signin_manager); | |
| 151 signin_manager->SetAuthenticatedUsername(GetTestUser()); | |
| 152 | |
| 153 UserCloudPolicyManager* policy_manager = | |
| 154 UserCloudPolicyManagerFactory::GetForProfile(browser->profile()); | |
| 155 ASSERT_TRUE(policy_manager); | |
| 156 policy_manager->Connect(g_browser_process->local_state(), | |
| 157 connector->device_management_service()); | |
| 158 #endif // defined(OS_CHROMEOS) | |
| 159 | |
| 160 ASSERT_TRUE(policy_manager->core()->client()); | |
| 161 base::RunLoop run_loop; | |
| 162 MockCloudPolicyClientObserver observer; | |
| 163 EXPECT_CALL(observer, OnRegistrationStateChanged(_)).WillOnce( | |
| 164 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | |
| 165 policy_manager->core()->client()->AddObserver(&observer); | |
| 166 | |
| 167 // Give a bogus OAuth token to the |policy_manager|. This should make its | |
| 168 // CloudPolicyClient fetch the DMToken. | |
| 169 policy_manager->RegisterClient("bogus"); | |
| 170 run_loop.Run(); | |
| 171 Mock::VerifyAndClearExpectations(&observer); | |
| 172 policy_manager->core()->client()->RemoveObserver(&observer); | |
| 173 } | |
| 174 | |
| 175 struct TestSetup { | |
| 176 TestSetup(void (*before)(), void (*after)(Browser*)) | |
| 177 : SetUpBeforeCreatingBrowser(before), | |
| 178 SetUpAfterCreatingBrowser(after) {} | |
| 179 | |
| 180 void (*SetUpBeforeCreatingBrowser)(); | |
| 181 void (*SetUpAfterCreatingBrowser)(Browser*); | |
| 182 }; | |
| 183 | |
| 184 } // namespace | 111 } // namespace |
| 185 | 112 |
| 186 // Tests the cloud policy stack(s). | 113 // Tests the cloud policy stack(s). |
| 187 class CloudPolicyTest : public InProcessBrowserTest, | 114 class CloudPolicyTest : public InProcessBrowserTest { |
| 188 public testing::WithParamInterface<TestSetup> { | |
| 189 protected: | 115 protected: |
| 190 CloudPolicyTest() {} | 116 CloudPolicyTest() {} |
| 191 virtual ~CloudPolicyTest() {} | 117 virtual ~CloudPolicyTest() {} |
| 192 | 118 |
| 193 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | 119 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
| 194 // The TestServer wants the docroot as a path relative to the source dir. | 120 // The TestServer wants the docroot as a path relative to the source dir. |
| 195 FilePath source; | 121 FilePath source; |
| 196 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source)); | 122 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source)); |
| 197 ASSERT_TRUE(temp_dir_.CreateUniqueTempDirUnderPath(source)); | 123 ASSERT_TRUE(temp_dir_.CreateUniqueTempDirUnderPath(source)); |
| 198 ASSERT_NO_FATAL_FAILURE(SetServerPolicy(GetEmptyPolicy())); | 124 ASSERT_NO_FATAL_FAILURE(SetServerPolicy(GetEmptyPolicy())); |
| 199 | 125 |
| 200 test_server_.reset( | 126 test_server_.reset( |
| 201 new net::TestServer( | 127 new net::TestServer( |
| 202 net::TestServer::TYPE_HTTP, | 128 net::TestServer::TYPE_HTTP, |
| 203 net::TestServer::kLocalhost, | 129 net::TestServer::kLocalhost, |
| 204 temp_dir_.path().BaseName())); | 130 temp_dir_.path().BaseName())); |
| 205 ASSERT_TRUE(test_server_->Start()); | 131 ASSERT_TRUE(test_server_->Start()); |
| 206 | 132 |
| 207 std::string url = test_server_->GetURL("device_management").spec(); | 133 std::string url = test_server_->GetURL("device_management").spec(); |
| 208 | 134 |
| 209 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 135 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 210 command_line->AppendSwitchASCII(switches::kDeviceManagementUrl, url); | 136 command_line->AppendSwitchASCII(switches::kDeviceManagementUrl, url); |
| 211 | 137 command_line->AppendSwitch(switches::kLoadCloudPolicyOnSignin); |
| 212 const TestSetup& setup = GetParam(); | |
| 213 setup.SetUpBeforeCreatingBrowser(); | |
| 214 } | 138 } |
| 215 | 139 |
| 216 virtual void SetUpOnMainThread() OVERRIDE { | 140 virtual void SetUpOnMainThread() OVERRIDE { |
| 217 // Checks that no policies have been loaded by the other providers before | 141 // Checks that no policies have been loaded by the other providers before |
| 218 // setting up the cloud connection. Other policies configured in the test | 142 // setting up the cloud connection. Other policies configured in the test |
| 219 // machine will interfere with these tests. | 143 // machine will interfere with these tests. |
| 220 const PolicyMap& map = g_browser_process->policy_service()->GetPolicies( | 144 const PolicyMap& map = g_browser_process->policy_service()->GetPolicies( |
| 221 POLICY_DOMAIN_CHROME, ""); | 145 POLICY_DOMAIN_CHROME, ""); |
| 222 if (!map.empty()) { | 146 if (!map.empty()) { |
| 223 base::DictionaryValue dict; | 147 base::DictionaryValue dict; |
| 224 for (PolicyMap::const_iterator it = map.begin(); it != map.end(); ++it) | 148 for (PolicyMap::const_iterator it = map.begin(); it != map.end(); ++it) |
| 225 dict.SetWithoutPathExpansion(it->first, it->second.value->DeepCopy()); | 149 dict.SetWithoutPathExpansion(it->first, it->second.value->DeepCopy()); |
| 226 ADD_FAILURE() | 150 ADD_FAILURE() |
| 227 << "There are pre-existing policies in this machine that will " | 151 << "There are pre-existing policies in this machine that will " |
| 228 << "interfere with these tests. Policies found: " << dict; | 152 << "interfere with these tests. Policies found: " << dict; |
| 229 } | 153 } |
| 230 | 154 |
| 231 GetParam().SetUpAfterCreatingBrowser(browser()); | 155 BrowserPolicyConnector* connector = |
| 156 g_browser_process->browser_policy_connector(); |
| 157 connector->ScheduleServiceInitialization(0); |
| 158 |
| 159 #if defined(OS_CHROMEOS) |
| 160 UserCloudPolicyManagerChromeOS* policy_manager = |
| 161 connector->GetUserCloudPolicyManager(); |
| 162 ASSERT_TRUE(policy_manager); |
| 163 #else |
| 164 // Mock a signed-in user. This is used by the UserCloudPolicyStore to pass |
| 165 // the username to the UserCloudPolicyValidator. |
| 166 SigninManager* signin_manager = |
| 167 SigninManagerFactory::GetForProfile(browser()->profile()); |
| 168 ASSERT_TRUE(signin_manager); |
| 169 signin_manager->SetAuthenticatedUsername(GetTestUser()); |
| 170 |
| 171 UserCloudPolicyManager* policy_manager = |
| 172 UserCloudPolicyManagerFactory::GetForProfile(browser()->profile()); |
| 173 ASSERT_TRUE(policy_manager); |
| 174 policy_manager->Connect(g_browser_process->local_state(), |
| 175 connector->device_management_service()); |
| 176 #endif // defined(OS_CHROMEOS) |
| 177 |
| 178 ASSERT_TRUE(policy_manager->core()->client()); |
| 179 base::RunLoop run_loop; |
| 180 MockCloudPolicyClientObserver observer; |
| 181 EXPECT_CALL(observer, OnRegistrationStateChanged(_)).WillOnce( |
| 182 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
| 183 policy_manager->core()->client()->AddObserver(&observer); |
| 184 |
| 185 // Give a bogus OAuth token to the |policy_manager|. This should make its |
| 186 // CloudPolicyClient fetch the DMToken. |
| 187 policy_manager->RegisterClient("bogus"); |
| 188 run_loop.Run(); |
| 189 Mock::VerifyAndClearExpectations(&observer); |
| 190 policy_manager->core()->client()->RemoveObserver(&observer); |
| 232 } | 191 } |
| 233 | 192 |
| 234 void SetServerPolicy(const std::string& policy) { | 193 void SetServerPolicy(const std::string& policy) { |
| 235 int result = file_util::WriteFile( | 194 int result = file_util::WriteFile( |
| 236 temp_dir_.path().AppendASCII("device_management"), | 195 temp_dir_.path().AppendASCII("device_management"), |
| 237 policy.data(), policy.size()); | 196 policy.data(), policy.size()); |
| 238 ASSERT_EQ(static_cast<int>(policy.size()), result); | 197 ASSERT_EQ(static_cast<int>(policy.size()), result); |
| 239 } | 198 } |
| 240 | 199 |
| 241 base::ScopedTempDir temp_dir_; | 200 base::ScopedTempDir temp_dir_; |
| 242 scoped_ptr<net::TestServer> test_server_; | 201 scoped_ptr<net::TestServer> test_server_; |
| 243 }; | 202 }; |
| 244 | 203 |
| 245 IN_PROC_BROWSER_TEST_P(CloudPolicyTest, FetchPolicy) { | 204 IN_PROC_BROWSER_TEST_F(CloudPolicyTest, FetchPolicy) { |
| 246 PolicyService* policy_service = browser()->profile()->GetPolicyService(); | 205 PolicyService* policy_service = browser()->profile()->GetPolicyService(); |
| 247 { | 206 { |
| 248 base::RunLoop run_loop; | 207 base::RunLoop run_loop; |
| 249 policy_service->RefreshPolicies(run_loop.QuitClosure()); | 208 policy_service->RefreshPolicies(run_loop.QuitClosure()); |
| 250 run_loop.Run(); | 209 run_loop.Run(); |
| 251 } | 210 } |
| 252 | 211 |
| 253 PolicyMap empty; | 212 PolicyMap empty; |
| 254 EXPECT_TRUE( | 213 EXPECT_TRUE( |
| 255 empty.Equals(policy_service->GetPolicies(POLICY_DOMAIN_CHROME, ""))); | 214 empty.Equals(policy_service->GetPolicies(POLICY_DOMAIN_CHROME, ""))); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 271 POLICY_SCOPE_USER, base::Value::CreateStringValue("google.com")); | 230 POLICY_SCOPE_USER, base::Value::CreateStringValue("google.com")); |
| 272 { | 231 { |
| 273 base::RunLoop run_loop; | 232 base::RunLoop run_loop; |
| 274 policy_service->RefreshPolicies(run_loop.QuitClosure()); | 233 policy_service->RefreshPolicies(run_loop.QuitClosure()); |
| 275 run_loop.Run(); | 234 run_loop.Run(); |
| 276 } | 235 } |
| 277 EXPECT_TRUE( | 236 EXPECT_TRUE( |
| 278 expected.Equals(policy_service->GetPolicies(POLICY_DOMAIN_CHROME, ""))); | 237 expected.Equals(policy_service->GetPolicies(POLICY_DOMAIN_CHROME, ""))); |
| 279 } | 238 } |
| 280 | 239 |
| 281 #if defined(OS_CHROMEOS) | |
| 282 INSTANTIATE_TEST_CASE_P( | |
| 283 OldStackCloudPolicyTest, | |
| 284 CloudPolicyTest, | |
| 285 testing::Values( | |
| 286 TestSetup(SetUpOldStackBeforeCreatingBrowser, | |
| 287 SetUpOldStackAfterCreatingBrowser))); | |
| 288 #endif | |
| 289 | |
| 290 INSTANTIATE_TEST_CASE_P( | |
| 291 NewStackCloudPolicyTest, | |
| 292 CloudPolicyTest, | |
| 293 testing::Values( | |
| 294 TestSetup(SetUpNewStackBeforeCreatingBrowser, | |
| 295 SetUpNewStackAfterCreatingBrowser))); | |
| 296 | |
| 297 TEST(CloudPolicyProtoTest, VerifyProtobufEquivalence) { | 240 TEST(CloudPolicyProtoTest, VerifyProtobufEquivalence) { |
| 298 // There are 2 protobufs that can be used for user cloud policy: | 241 // There are 2 protobufs that can be used for user cloud policy: |
| 299 // cloud_policy.proto and chrome_settings.proto. chrome_settings.proto is the | 242 // cloud_policy.proto and chrome_settings.proto. chrome_settings.proto is the |
| 300 // version used by the server, but generates one proto message per policy; to | 243 // version used by the server, but generates one proto message per policy; to |
| 301 // save binary size on the client, the other version shares proto messages for | 244 // save binary size on the client, the other version shares proto messages for |
| 302 // policies of the same type. They generate the same bytes on the wire though, | 245 // policies of the same type. They generate the same bytes on the wire though, |
| 303 // so they are compatible. This test verifies that that stays true. | 246 // so they are compatible. This test verifies that that stays true. |
| 304 | 247 |
| 305 // Build a ChromeSettingsProto message with one policy of each supported type. | 248 // Build a ChromeSettingsProto message with one policy of each supported type. |
| 306 em::ChromeSettingsProto chrome_settings; | 249 em::ChromeSettingsProto chrome_settings; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 337 | 280 |
| 338 // They should now serialize to the same bytes. | 281 // They should now serialize to the same bytes. |
| 339 std::string chrome_settings_serialized; | 282 std::string chrome_settings_serialized; |
| 340 std::string cloud_policy_serialized; | 283 std::string cloud_policy_serialized; |
| 341 ASSERT_TRUE(chrome_settings.SerializeToString(&chrome_settings_serialized)); | 284 ASSERT_TRUE(chrome_settings.SerializeToString(&chrome_settings_serialized)); |
| 342 ASSERT_TRUE(cloud_policy.SerializeToString(&cloud_policy_serialized)); | 285 ASSERT_TRUE(cloud_policy.SerializeToString(&cloud_policy_serialized)); |
| 343 EXPECT_EQ(chrome_settings_serialized, cloud_policy_serialized); | 286 EXPECT_EQ(chrome_settings_serialized, cloud_policy_serialized); |
| 344 } | 287 } |
| 345 | 288 |
| 346 } // namespace policy | 289 } // namespace policy |
| OLD | NEW |