| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 8 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 9 #include "base/values.h" | 10 #include "base/values.h" |
| 10 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/policy/cloud_policy_identity_strategy.h" | 12 #include "chrome/browser/policy/cloud_policy_data_store.h" |
| 12 #include "chrome/browser/policy/logging_work_scheduler.h" | 13 #include "chrome/browser/policy/logging_work_scheduler.h" |
| 13 #include "chrome/browser/policy/mock_configuration_policy_store.h" | |
| 14 #include "chrome/browser/policy/proto/cloud_policy.pb.h" | 14 #include "chrome/browser/policy/proto/cloud_policy.pb.h" |
| 15 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 15 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 16 #include "chrome/browser/policy/testing_cloud_policy_subsystem.h" | 16 #include "chrome/browser/policy/testing_cloud_policy_subsystem.h" |
| 17 #include "chrome/browser/policy/testing_policy_url_fetcher_factory.h" | 17 #include "chrome/browser/policy/testing_policy_url_fetcher_factory.h" |
| 18 #include "chrome/browser/policy/user_policy_cache.h" | 18 #include "chrome/browser/policy/user_policy_cache.h" |
| 19 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 20 #include "chrome/test/testing_browser_process.h" | 20 #include "chrome/test/testing_browser_process.h" |
| 21 #include "chrome/test/testing_pref_service.h" | 21 #include "chrome/test/testing_pref_service.h" |
| 22 #include "content/browser/browser_thread.h" | 22 #include "content/browser/browser_thread.h" |
| 23 #include "content/common/url_fetcher.h" | 23 #include "content/common/url_fetcher.h" |
| 24 #include "policy/policy_constants.h" | 24 #include "policy/policy_constants.h" |
| 25 #include "testing/gmock/include/gmock/gmock.h" | |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 26 |
| 28 namespace policy { | 27 namespace policy { |
| 29 | 28 |
| 30 using ::testing::_; | 29 using ::testing::_; |
| 31 using ::testing::AtLeast; | |
| 32 using ::testing::AtMost; | 30 using ::testing::AtMost; |
| 33 using ::testing::DoAll; | |
| 34 using ::testing::InSequence; | 31 using ::testing::InSequence; |
| 35 using ::testing::Invoke; | |
| 36 using ::testing::Return; | |
| 37 | 32 |
| 38 namespace em = enterprise_management; | 33 namespace em = enterprise_management; |
| 39 | 34 |
| 40 class CloudPolicySubsystemTest; | |
| 41 | |
| 42 namespace { | 35 namespace { |
| 43 | 36 |
| 44 const char kGaiaAuthHeader[] = "GoogleLogin auth=secret123"; | 37 const char kGaiaAuthHeader[] = "GoogleLogin auth=secret123"; |
| 45 const char kDMAuthHeader[] = "GoogleDMToken token=token123456"; | 38 const char kDMAuthHeader[] = "GoogleDMToken token=token123456"; |
| 46 const char kDMToken[] = "token123456"; | 39 const char kDMToken[] = "token123456"; |
| 47 | 40 |
| 48 const char kDeviceManagementUrl[] = | 41 const char kDeviceManagementUrl[] = |
| 49 "http://localhost:12345/device_management_test"; | 42 "http://localhost:12345/device_management_test"; |
| 50 | 43 |
| 51 // Constant responses of the identity strategy. | 44 // Fake data to be included in requests. |
| 52 const char kMachineId[] = "dummy-cros-machine-123"; | |
| 53 const char kMachineModel[] = "Pony"; | |
| 54 const char kDeviceId[] = "abc-xyz-123"; | |
| 55 const char kUsername[] = "john@smith.com"; | 45 const char kUsername[] = "john@smith.com"; |
| 56 const char kAuthToken[] = "secret123"; | 46 const char kAuthToken[] = "secret123"; |
| 57 const char kPolicyType[] = "google/chrome/test"; | 47 const char kPolicyType[] = "google/chrome/test"; |
| 58 | 48 |
| 59 } // namespace | 49 } // namespace |
| 60 | 50 |
| 61 // A stripped-down identity strategy for the tests. | |
| 62 class TestingIdentityStrategy : public CloudPolicyIdentityStrategy { | |
| 63 public: | |
| 64 TestingIdentityStrategy() { | |
| 65 } | |
| 66 | |
| 67 virtual std::string GetDeviceToken() OVERRIDE { | |
| 68 return device_token_; | |
| 69 } | |
| 70 | |
| 71 virtual std::string GetDeviceID() OVERRIDE { | |
| 72 return kDeviceId; | |
| 73 } | |
| 74 | |
| 75 virtual std::string GetMachineID() OVERRIDE { | |
| 76 return kMachineId; | |
| 77 } | |
| 78 | |
| 79 virtual std::string GetMachineModel() OVERRIDE { | |
| 80 return kMachineModel; | |
| 81 } | |
| 82 | |
| 83 virtual em::DeviceRegisterRequest_Type GetPolicyRegisterType() OVERRIDE { | |
| 84 return em::DeviceRegisterRequest::USER; | |
| 85 } | |
| 86 | |
| 87 virtual std::string GetPolicyType() OVERRIDE { | |
| 88 return kPolicyType; | |
| 89 } | |
| 90 | |
| 91 virtual bool GetCredentials(std::string* username, | |
| 92 std::string* auth_token) OVERRIDE { | |
| 93 *username = kUsername; | |
| 94 *auth_token = kAuthToken;; | |
| 95 return !username->empty() && !auth_token->empty(); | |
| 96 } | |
| 97 | |
| 98 virtual void OnDeviceTokenAvailable(const std::string& token) OVERRIDE { | |
| 99 device_token_ = token; | |
| 100 NotifyDeviceTokenChanged(); | |
| 101 } | |
| 102 | |
| 103 private: | |
| 104 std::string device_token_; | |
| 105 | |
| 106 DISALLOW_COPY_AND_ASSIGN(TestingIdentityStrategy); | |
| 107 }; | |
| 108 | |
| 109 // An action that returns an URLRequestJob with an HTTP error code. | 51 // An action that returns an URLRequestJob with an HTTP error code. |
| 110 ACTION_P(CreateFailedResponse, http_error_code) { | 52 ACTION_P(CreateFailedResponse, http_error_code) { |
| 111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 112 em::DeviceManagementResponse response_data; | 54 em::DeviceManagementResponse response_data; |
| 113 | 55 |
| 114 arg2->response_data = response_data.SerializeAsString(); | 56 arg2->response_data = response_data.SerializeAsString(); |
| 115 arg2->response_code = http_error_code; | 57 arg2->response_code = http_error_code; |
| 116 } | 58 } |
| 117 | 59 |
| 118 // An action that returns an URLRequestJob with a successful device | 60 // An action that returns an URLRequestJob with a successful device |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 } | 108 } |
| 167 | 109 |
| 168 virtual void SetUp() { | 110 virtual void SetUp() { |
| 169 prefs_.reset(new TestingPrefService); | 111 prefs_.reset(new TestingPrefService); |
| 170 CloudPolicySubsystem::RegisterPrefs(prefs_.get()); | 112 CloudPolicySubsystem::RegisterPrefs(prefs_.get()); |
| 171 ((TestingBrowserProcess*) g_browser_process)->SetLocalState(prefs_.get()); | 113 ((TestingBrowserProcess*) g_browser_process)->SetLocalState(prefs_.get()); |
| 172 | 114 |
| 173 logger_.reset(new EventLogger); | 115 logger_.reset(new EventLogger); |
| 174 factory_.reset(new TestingPolicyURLFetcherFactory(logger_.get())); | 116 factory_.reset(new TestingPolicyURLFetcherFactory(logger_.get())); |
| 175 URLFetcher::set_factory(factory_.get()); | 117 URLFetcher::set_factory(factory_.get()); |
| 176 identity_strategy_.reset(new TestingIdentityStrategy); | |
| 177 ASSERT_TRUE(temp_user_data_dir_.CreateUniqueTempDir()); | 118 ASSERT_TRUE(temp_user_data_dir_.CreateUniqueTempDir()); |
| 119 data_store_.reset(CloudPolicyDataStore::CreateForUserPolicies()); |
| 178 cache_ = new UserPolicyCache( | 120 cache_ = new UserPolicyCache( |
| 179 temp_user_data_dir_.path().AppendASCII("CloudPolicyControllerTest")); | 121 temp_user_data_dir_.path().AppendASCII("CloudPolicyControllerTest")); |
| 180 cloud_policy_subsystem_.reset(new TestingCloudPolicySubsystem( | 122 cloud_policy_subsystem_.reset(new TestingCloudPolicySubsystem( |
| 181 identity_strategy_.get(), cache_, kDeviceManagementUrl, | 123 data_store_.get(), cache_, |
| 182 logger_.get())); | 124 kDeviceManagementUrl, logger_.get())); |
| 183 cloud_policy_subsystem_->CompleteInitialization( | 125 cloud_policy_subsystem_->CompleteInitialization( |
| 184 prefs::kDevicePolicyRefreshRate, 0); | 126 prefs::kDevicePolicyRefreshRate, 0); |
| 185 } | 127 } |
| 186 | 128 |
| 187 virtual void TearDown() { | 129 virtual void TearDown() { |
| 188 ((TestingBrowserProcess*) g_browser_process)->SetLocalState(NULL); | 130 ((TestingBrowserProcess*) g_browser_process)->SetLocalState(NULL); |
| 189 cloud_policy_subsystem_->Shutdown(); | 131 cloud_policy_subsystem_->Shutdown(); |
| 190 cloud_policy_subsystem_.reset(); | 132 cloud_policy_subsystem_.reset(); |
| 133 data_store_.reset(); |
| 191 factory_.reset(); | 134 factory_.reset(); |
| 192 logger_.reset(); | 135 logger_.reset(); |
| 193 prefs_.reset(); | 136 prefs_.reset(); |
| 194 } | 137 } |
| 195 | 138 |
| 196 void ExecuteTest() { | 139 void ExecuteTest() { |
| 197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 198 // The first unexpected request of the policy subsystem will stop the | 141 // The first unexpected request of the policy subsystem will stop the |
| 199 // message loop. | 142 // message loop. |
| 200 // This code relies on the fact that an InSequence object is active. | 143 // This code relies on the fact that an InSequence object is active. |
| 201 EXPECT_CALL(*(factory_.get()), Intercept(_, _, _)) | 144 EXPECT_CALL(*(factory_.get()), Intercept(_, _, _)) |
| 202 .Times(AtMost(1)) | 145 .Times(AtMost(1)) |
| 203 .WillOnce(InvokeWithoutArgs( | 146 .WillOnce(InvokeWithoutArgs( |
| 204 this, | 147 this, |
| 205 &CloudPolicySubsystemTestBase::StopMessageLoop)); | 148 &CloudPolicySubsystemTestBase::StopMessageLoop)); |
| 149 data_store_->set_user_name(kUsername); |
| 150 data_store_->SetGaiaToken(kAuthToken); |
| 151 data_store_->SetDeviceToken("", true); |
| 206 loop_.RunAllPending(); | 152 loop_.RunAllPending(); |
| 207 } | 153 } |
| 208 | 154 |
| 209 void VerifyTest(const std::string& homepage_location) { | 155 void VerifyTest(const std::string& homepage_location) { |
| 210 // Test conditions. | 156 // Test conditions. |
| 211 EXPECT_EQ(CloudPolicySubsystem::SUCCESS, cloud_policy_subsystem_->state()); | 157 EXPECT_EQ(CloudPolicySubsystem::SUCCESS, cloud_policy_subsystem_->state()); |
| 212 StringValue homepage_value(homepage_location); | 158 StringValue homepage_value(homepage_location); |
| 213 VerifyPolicy(kPolicyHomepageLocation, &homepage_value); | 159 VerifyPolicy(kPolicyHomepageLocation, &homepage_value); |
| 214 VerifyServerLoad(); | 160 VerifyServerLoad(); |
| 215 } | 161 } |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 << "No enough requests were fired during the test run."; | 229 << "No enough requests were fired during the test run."; |
| 284 } | 230 } |
| 285 | 231 |
| 286 ScopedTempDir temp_user_data_dir_; | 232 ScopedTempDir temp_user_data_dir_; |
| 287 | 233 |
| 288 MessageLoop loop_; | 234 MessageLoop loop_; |
| 289 BrowserThread ui_thread_; | 235 BrowserThread ui_thread_; |
| 290 BrowserThread io_thread_; | 236 BrowserThread io_thread_; |
| 291 | 237 |
| 292 scoped_ptr<EventLogger> logger_; | 238 scoped_ptr<EventLogger> logger_; |
| 293 scoped_ptr<TestingIdentityStrategy> identity_strategy_; | 239 scoped_ptr<CloudPolicyDataStore> data_store_; |
| 294 scoped_ptr<CloudPolicySubsystem> cloud_policy_subsystem_; | 240 scoped_ptr<CloudPolicySubsystem> cloud_policy_subsystem_; |
| 295 scoped_ptr<PrefService> prefs_; | 241 scoped_ptr<PrefService> prefs_; |
| 296 CloudPolicyCacheBase* cache_; | 242 CloudPolicyCacheBase* cache_; |
| 297 | 243 |
| 298 scoped_ptr<TestingPolicyURLFetcherFactory> factory_; | 244 scoped_ptr<TestingPolicyURLFetcherFactory> factory_; |
| 299 | 245 |
| 300 DISALLOW_COPY_AND_ASSIGN(CloudPolicySubsystemTestBase); | 246 DISALLOW_COPY_AND_ASSIGN(CloudPolicySubsystemTestBase); |
| 301 }; | 247 }; |
| 302 | 248 |
| 303 // A parameterized test case that simulates 100 failed registration attempts, | 249 // A parameterized test case that simulates 100 failed registration attempts, |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 ExecuteTest(); | 382 ExecuteTest(); |
| 437 VerifyTest("http://www.youtube.com"); | 383 VerifyTest("http://www.youtube.com"); |
| 438 } | 384 } |
| 439 | 385 |
| 440 INSTANTIATE_TEST_CASE_P( | 386 INSTANTIATE_TEST_CASE_P( |
| 441 CloudPolicySubsystemPolicyReregisterTestInstance, | 387 CloudPolicySubsystemPolicyReregisterTestInstance, |
| 442 CloudPolicySubsystemPolicyReregisterTest, | 388 CloudPolicySubsystemPolicyReregisterTest, |
| 443 testing::Values(401, 403, 410)); | 389 testing::Values(401, 403, 410)); |
| 444 | 390 |
| 445 } // policy | 391 } // policy |
| OLD | NEW |