| 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" | |
| 6 #include "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/file_path.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/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/policy/browser_policy_connector.h" | 15 #include "chrome/browser/policy/browser_policy_connector.h" |
| 16 #include "chrome/browser/policy/cloud_policy_client.h" | 16 #include "chrome/browser/policy/cloud_policy_client.h" |
| 17 #include "chrome/browser/policy/cloud_policy_constants.h" | 17 #include "chrome/browser/policy/cloud_policy_constants.h" |
| 18 #include "chrome/browser/policy/mock_cloud_policy_client.h" | 18 #include "chrome/browser/policy/mock_cloud_policy_client.h" |
| 19 #include "chrome/browser/policy/policy_map.h" | 19 #include "chrome/browser/policy/policy_map.h" |
| 20 #include "chrome/browser/policy/policy_service.h" | 20 #include "chrome/browser/policy/policy_service.h" |
| 21 #include "chrome/browser/policy/proto/chrome_settings.pb.h" | 21 #include "chrome/browser/policy/proto/chrome_settings.pb.h" |
| 22 #include "chrome/browser/policy/proto/cloud_policy.pb.h" | 22 #include "chrome/browser/policy/proto/cloud_policy.pb.h" |
| 23 #include "chrome/browser/policy/test/local_policy_test_server.h" |
| 23 #include "chrome/browser/policy/test_utils.h" | 24 #include "chrome/browser/policy/test_utils.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/browser/ui/browser.h" | 26 #include "chrome/browser/ui/browser.h" |
| 26 #include "chrome/common/chrome_notification_types.h" | 27 #include "chrome/common/chrome_notification_types.h" |
| 27 #include "chrome/common/chrome_switches.h" | 28 #include "chrome/common/chrome_switches.h" |
| 28 #include "chrome/test/base/in_process_browser_test.h" | 29 #include "chrome/test/base/in_process_browser_test.h" |
| 29 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
| 30 #include "content/public/browser/notification_service.h" | 31 #include "content/public/browser/notification_service.h" |
| 31 #include "content/public/browser/notification_source.h" | 32 #include "content/public/browser/notification_source.h" |
| 32 #include "content/public/test/test_utils.h" | 33 #include "content/public/test/test_utils.h" |
| 33 #include "googleurl/src/gurl.h" | 34 #include "googleurl/src/gurl.h" |
| 34 #include "net/test/test_server.h" | |
| 35 #include "policy/policy_constants.h" | 35 #include "policy/policy_constants.h" |
| 36 #include "testing/gmock/include/gmock/gmock.h" | 36 #include "testing/gmock/include/gmock/gmock.h" |
| 37 #include "testing/gtest/include/gtest/gtest.h" | 37 #include "testing/gtest/include/gtest/gtest.h" |
| 38 | 38 |
| 39 #if defined(OS_CHROMEOS) | 39 #if defined(OS_CHROMEOS) |
| 40 #include "chrome/browser/chromeos/login/user_manager.h" | 40 #include "chrome/browser/chromeos/login/user_manager.h" |
| 41 #include "chrome/browser/policy/user_cloud_policy_manager_chromeos.h" | 41 #include "chrome/browser/policy/user_cloud_policy_manager_chromeos.h" |
| 42 #include "chrome/common/chrome_paths.h" | 42 #include "chrome/common/chrome_paths.h" |
| 43 #include "chromeos/dbus/mock_cryptohome_client.h" | 43 #include "chromeos/dbus/mock_cryptohome_client.h" |
| 44 #include "chromeos/dbus/mock_dbus_thread_manager.h" | 44 #include "chromeos/dbus/mock_dbus_thread_manager.h" |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 | 162 |
| 163 } // namespace | 163 } // namespace |
| 164 | 164 |
| 165 // Tests the cloud policy stack(s). | 165 // Tests the cloud policy stack(s). |
| 166 class CloudPolicyTest : public InProcessBrowserTest { | 166 class CloudPolicyTest : public InProcessBrowserTest { |
| 167 protected: | 167 protected: |
| 168 CloudPolicyTest() {} | 168 CloudPolicyTest() {} |
| 169 virtual ~CloudPolicyTest() {} | 169 virtual ~CloudPolicyTest() {} |
| 170 | 170 |
| 171 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | 171 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
| 172 // The TestServer wants the docroot as a path relative to the source dir. | 172 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 173 base::FilePath source; | |
| 174 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &source)); | |
| 175 ASSERT_TRUE(temp_dir_.CreateUniqueTempDirUnderPath(source)); | |
| 176 ASSERT_NO_FATAL_FAILURE(SetServerPolicy(GetEmptyPolicy())); | 173 ASSERT_NO_FATAL_FAILURE(SetServerPolicy(GetEmptyPolicy())); |
| 177 | 174 |
| 178 test_server_.reset( | 175 test_server_.reset(new LocalPolicyTestServer(policy_file_path())); |
| 179 new net::TestServer( | |
| 180 net::TestServer::TYPE_HTTP, | |
| 181 net::TestServer::kLocalhost, | |
| 182 testserver_relative_docroot())); | |
| 183 ASSERT_TRUE(test_server_->Start()); | 176 ASSERT_TRUE(test_server_->Start()); |
| 184 | 177 |
| 185 std::string url = test_server_->GetURL("device_management").spec(); | 178 std::string url = test_server_->GetServiceURL().spec(); |
| 186 | 179 |
| 187 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 180 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 188 command_line->AppendSwitchASCII(switches::kDeviceManagementUrl, url); | 181 command_line->AppendSwitchASCII(switches::kDeviceManagementUrl, url); |
| 189 | 182 |
| 190 #if defined(OS_CHROMEOS) | 183 #if defined(OS_CHROMEOS) |
| 191 PathService::Override(chrome::DIR_USER_POLICY_KEYS, user_policy_key_dir()); | 184 PathService::Override(chrome::DIR_USER_POLICY_KEYS, user_policy_key_dir()); |
| 192 | 185 |
| 193 mock_dbus_thread_manager_ = new chromeos::MockDBusThreadManager(); | 186 mock_dbus_thread_manager_ = new chromeos::MockDBusThreadManager(); |
| 194 chromeos::DBusThreadManager::InitializeForTesting( | 187 chromeos::DBusThreadManager::InitializeForTesting( |
| 195 mock_dbus_thread_manager_); | 188 mock_dbus_thread_manager_); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 policy_manager->core()->client()->AddObserver(&observer); | 235 policy_manager->core()->client()->AddObserver(&observer); |
| 243 | 236 |
| 244 // Give a bogus OAuth token to the |policy_manager|. This should make its | 237 // Give a bogus OAuth token to the |policy_manager|. This should make its |
| 245 // CloudPolicyClient fetch the DMToken. | 238 // CloudPolicyClient fetch the DMToken. |
| 246 policy_manager->RegisterClient("bogus"); | 239 policy_manager->RegisterClient("bogus"); |
| 247 run_loop.Run(); | 240 run_loop.Run(); |
| 248 Mock::VerifyAndClearExpectations(&observer); | 241 Mock::VerifyAndClearExpectations(&observer); |
| 249 policy_manager->core()->client()->RemoveObserver(&observer); | 242 policy_manager->core()->client()->RemoveObserver(&observer); |
| 250 } | 243 } |
| 251 | 244 |
| 252 base::FilePath testserver_relative_docroot() { | |
| 253 return temp_dir_.path().BaseName().AppendASCII("testserver"); | |
| 254 } | |
| 255 | |
| 256 base::FilePath testserver_device_management_file() { | |
| 257 return temp_dir_.path().AppendASCII("testserver") | |
| 258 .AppendASCII("device_management"); | |
| 259 } | |
| 260 | |
| 261 #if defined(OS_CHROMEOS) | 245 #if defined(OS_CHROMEOS) |
| 262 base::FilePath user_policy_key_dir() { | 246 base::FilePath user_policy_key_dir() { |
| 263 return temp_dir_.path().AppendASCII("user_policy"); | 247 return temp_dir_.path().AppendASCII("user_policy"); |
| 264 } | 248 } |
| 265 | 249 |
| 266 base::FilePath user_policy_key_file() { | 250 base::FilePath user_policy_key_file() { |
| 267 return user_policy_key_dir().AppendASCII(kSanitizedUsername) | 251 return user_policy_key_dir().AppendASCII(kSanitizedUsername) |
| 268 .AppendASCII("policy.pub"); | 252 .AppendASCII("policy.pub"); |
| 269 } | 253 } |
| 270 #endif | 254 #endif |
| 271 | 255 |
| 272 void SetServerPolicy(const std::string& policy) { | 256 void SetServerPolicy(const std::string& policy) { |
| 273 ASSERT_TRUE(file_util::CreateDirectory( | 257 int result = file_util::WriteFile(policy_file_path(), policy.data(), |
| 274 testserver_device_management_file().DirName())); | 258 policy.size()); |
| 275 int result = file_util::WriteFile( | |
| 276 testserver_device_management_file(), policy.data(), policy.size()); | |
| 277 ASSERT_EQ(static_cast<int>(policy.size()), result); | 259 ASSERT_EQ(static_cast<int>(policy.size()), result); |
| 278 } | 260 } |
| 279 | 261 |
| 262 base::FilePath policy_file_path() const { |
| 263 return temp_dir_.path().AppendASCII("policy.json"); |
| 264 } |
| 265 |
| 280 base::ScopedTempDir temp_dir_; | 266 base::ScopedTempDir temp_dir_; |
| 281 scoped_ptr<net::TestServer> test_server_; | 267 scoped_ptr<LocalPolicyTestServer> test_server_; |
| 282 | 268 |
| 283 #if defined(OS_CHROMEOS) | 269 #if defined(OS_CHROMEOS) |
| 284 std::string session_manager_user_policy_; | 270 std::string session_manager_user_policy_; |
| 285 chromeos::MockDBusThreadManager* mock_dbus_thread_manager_; | 271 chromeos::MockDBusThreadManager* mock_dbus_thread_manager_; |
| 286 #endif | 272 #endif |
| 287 }; | 273 }; |
| 288 | 274 |
| 289 IN_PROC_BROWSER_TEST_F(CloudPolicyTest, FetchPolicy) { | 275 IN_PROC_BROWSER_TEST_F(CloudPolicyTest, FetchPolicy) { |
| 290 PolicyService* policy_service = browser()->profile()->GetPolicyService(); | 276 PolicyService* policy_service = browser()->profile()->GetPolicyService(); |
| 291 { | 277 { |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 | 394 |
| 409 // They should now serialize to the same bytes. | 395 // They should now serialize to the same bytes. |
| 410 std::string chrome_settings_serialized; | 396 std::string chrome_settings_serialized; |
| 411 std::string cloud_policy_serialized; | 397 std::string cloud_policy_serialized; |
| 412 ASSERT_TRUE(chrome_settings.SerializeToString(&chrome_settings_serialized)); | 398 ASSERT_TRUE(chrome_settings.SerializeToString(&chrome_settings_serialized)); |
| 413 ASSERT_TRUE(cloud_policy.SerializeToString(&cloud_policy_serialized)); | 399 ASSERT_TRUE(cloud_policy.SerializeToString(&cloud_policy_serialized)); |
| 414 EXPECT_EQ(chrome_settings_serialized, cloud_policy_serialized); | 400 EXPECT_EQ(chrome_settings_serialized, cloud_policy_serialized); |
| 415 } | 401 } |
| 416 | 402 |
| 417 } // namespace policy | 403 } // namespace policy |
| OLD | NEW |