Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/basictypes.h" | |
| 6 #include "base/file_util.h" | |
| 7 #include "base/logging.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "base/metrics/histogram.h" | |
| 10 #include "base/time.h" | |
| 11 #include "chrome/browser/policy/cloud_policy_controller.h" | |
| 12 #include "chrome/browser/policy/cloud_policy_data_store.h" | |
| 13 #include "chrome/browser/policy/device_management_backend.h" | |
| 14 #include "chrome/browser/policy/device_management_backend_mock.h" | |
| 15 #include "chrome/browser/policy/device_management_service.h" | |
| 16 #include "chrome/browser/policy/device_token_fetcher.h" | |
| 17 #include "chrome/browser/policy/enterprise_metrics.h" | |
| 18 #include "chrome/browser/policy/mock_cloud_policy_data_store.h" | |
| 19 #include "chrome/browser/policy/mock_device_management_service.h" | |
| 20 #include "chrome/browser/policy/policy_notifier.h" | |
| 21 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
| 22 #include "chrome/browser/policy/proto/device_management_local.pb.h" | |
| 23 #include "chrome/browser/policy/user_policy_cache.h" | |
| 24 #include "chrome/browser/policy/user_policy_token_cache.h" | |
| 25 #include "chrome/test/ui_test_utils.h" | |
| 26 #include "content/browser/browser_thread.h" | |
| 27 #include "net/url_request/url_request_status.h" | |
| 28 #include "testing/gmock/include/gmock/gmock.h" | |
| 29 #include "testing/gtest/include/gtest/gtest.h" | |
| 30 | |
| 31 #if defined(OS_CHROMEOS) | |
| 32 #include "chrome/browser/chromeos/cros/mock_cryptohome_library.h" | |
| 33 #include "chrome/browser/chromeos/login/enterprise_enrollment_screen.h" | |
| 34 #include "chrome/browser/chromeos/login/mock_signed_settings_helper.h" | |
| 35 #include "chrome/browser/chromeos/login/signed_settings.h" | |
| 36 #include "chrome/browser/chromeos/login/wizard_controller.h" | |
| 37 #include "chrome/browser/chromeos/login/wizard_in_process_browser_test.h" | |
| 38 #include "chrome/browser/policy/device_policy_cache.h" | |
| 39 #include "chrome/browser/policy/enterprise_install_attributes.h" | |
| 40 #include "chrome/common/net/gaia/gaia_constants.h" | |
| 41 #include "content/common/test_url_fetcher_factory.h" | |
| 42 #endif | |
| 43 | |
| 44 namespace policy { | |
| 45 | |
| 46 namespace em = enterprise_management; | |
| 47 | |
| 48 using testing::_; | |
| 49 using testing::AnyNumber; | |
| 50 using testing::DoAll; | |
| 51 using testing::Return; | |
| 52 using testing::SetArgumentPointee; | |
| 53 | |
| 54 #if defined(OS_CHROMEOS) | |
| 55 | |
| 56 namespace { | |
| 57 | |
| 58 class TestInstallAttributes { | |
|
gfeher
2011/07/18 12:06:12
Please add 1-2 line comment explaining what does t
Joao da Silva
2011/07/19 09:34:17
Done.
| |
| 59 public: | |
| 60 explicit TestInstallAttributes(const std::string& user) | |
| 61 : user_(user), | |
| 62 owned_("true"), | |
| 63 install_attributes_(&mock_cryptohome_library_) { | |
| 64 EXPECT_CALL(mock_cryptohome_library_, InstallAttributesIsReady()) | |
| 65 .WillOnce(Return(true)); | |
| 66 EXPECT_CALL(mock_cryptohome_library_, InstallAttributesIsInvalid()) | |
| 67 .WillOnce(Return(false)); | |
| 68 EXPECT_CALL(mock_cryptohome_library_, InstallAttributesIsFirstInstall()) | |
| 69 .WillOnce(Return(false)); | |
| 70 EXPECT_CALL(mock_cryptohome_library_, | |
| 71 InstallAttributesGet("enterprise.owned", _)) | |
| 72 .WillOnce(DoAll(SetArgumentPointee<1>(owned_), | |
| 73 Return(true))); | |
| 74 EXPECT_CALL(mock_cryptohome_library_, | |
| 75 InstallAttributesGet("enterprise.user", _)) | |
| 76 .WillOnce(DoAll(SetArgumentPointee<1>(user_), | |
| 77 Return(true))); | |
| 78 } | |
| 79 | |
| 80 EnterpriseInstallAttributes* install_attributes() { | |
| 81 return &install_attributes_; | |
| 82 } | |
| 83 | |
| 84 private: | |
| 85 std::string user_; | |
| 86 std::string owned_; | |
| 87 chromeos::MockCryptohomeLibrary mock_cryptohome_library_; | |
| 88 EnterpriseInstallAttributes install_attributes_; | |
| 89 | |
| 90 DISALLOW_COPY_AND_ASSIGN(TestInstallAttributes); | |
| 91 }; | |
| 92 | |
| 93 } // namespace | |
| 94 | |
| 95 #endif // OS_CHROMEOS | |
| 96 | |
| 97 class EnterpriseMetricsTest : public testing::Test { | |
| 98 public: | |
| 99 EnterpriseMetricsTest() | |
| 100 : ui_thread_(BrowserThread::UI, &loop_), | |
| 101 file_thread_(BrowserThread::FILE, &loop_) { | |
| 102 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 103 } | |
| 104 | |
| 105 void SetMetricName(const std::string metric_name) { | |
|
gfeher
2011/07/18 12:06:12
Nit: pass by constant reference.
Joao da Silva
2011/07/19 09:34:17
Done.
| |
| 106 metric_name_ = metric_name; | |
| 107 if (metric_name == kMetricToken) { | |
| 108 expected_samples_.resize(kMetricTokenSize, 0); | |
| 109 } else if (metric_name == kMetricPolicy) { | |
| 110 expected_samples_.resize(kMetricPolicySize, 0); | |
| 111 } else { | |
| 112 NOTREACHED(); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 virtual void TearDown() OVERRIDE { | |
| 117 RunAllPending(); | |
| 118 EXPECT_TRUE(CheckSamples()); | |
| 119 } | |
| 120 | |
| 121 const FilePath& temp_dir() { | |
| 122 return temp_dir_.path(); | |
| 123 } | |
| 124 | |
| 125 void RunAllPending() { | |
| 126 loop_.RunAllPending(); | |
| 127 } | |
| 128 | |
| 129 void ExpectSample(int sample) { | |
|
gfeher
2011/07/18 12:06:12
Please add a comment, like "Adds a sample to the s
Joao da Silva
2011/07/19 09:34:17
Done.
| |
| 130 ASSERT_FALSE(metric_name_.empty()); | |
| 131 ASSERT_GE(sample, 0); | |
| 132 ASSERT_LT(sample, (int) expected_samples_.size()); | |
| 133 expected_samples_[sample]++; | |
| 134 } | |
| 135 | |
| 136 // Checks the current recorded samples against the expected set. Returns | |
| 137 // true if they match. | |
| 138 bool CheckSamples() { | |
| 139 EXPECT_FALSE(metric_name_.empty()); | |
| 140 if (metric_name_.empty()) | |
| 141 return false; | |
| 142 bool expects_samples = false; | |
| 143 for (size_t i = 0; i < expected_samples_.size(); ++i) { | |
| 144 if (expected_samples_[i] > 0) { | |
| 145 expects_samples = true; | |
| 146 break; | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 // The histogram won't be available until the first sample is measured. | |
| 151 base::Histogram* histogram = NULL; | |
| 152 if (!expects_samples) { | |
| 153 bool found = base::StatisticsRecorder::FindHistogram(metric_name_, | |
| 154 &histogram); | |
| 155 EXPECT_FALSE(found); | |
| 156 return !found; | |
| 157 } | |
| 158 | |
| 159 bool found = base::StatisticsRecorder::FindHistogram(metric_name_, | |
| 160 &histogram); | |
| 161 EXPECT_TRUE(found); | |
| 162 if (!found) | |
| 163 return false; | |
| 164 EXPECT_TRUE(histogram != NULL); | |
| 165 if (histogram == NULL) | |
| 166 return false; | |
| 167 | |
| 168 base::Histogram::SampleSet samples; | |
| 169 histogram->SnapshotSample(&samples); | |
| 170 | |
| 171 bool result = true; | |
| 172 size_t sum = 0; | |
| 173 for (size_t i = 0; i < expected_samples_.size(); ++i) { | |
| 174 EXPECT_EQ(expected_samples_[i], samples.counts(i)) << "i is " << i; | |
| 175 if (expected_samples_[i] != samples.counts(i)) | |
| 176 result = false; | |
| 177 sum += expected_samples_[i]; | |
| 178 } | |
| 179 EXPECT_EQ(sum, (size_t) samples.TotalCount()); | |
| 180 if (sum != (size_t) samples.TotalCount()) | |
| 181 result = false; | |
| 182 return result; | |
| 183 } | |
| 184 | |
| 185 private: | |
| 186 base::StatisticsRecorder statistics_recorder_; | |
| 187 MessageLoop loop_; | |
| 188 BrowserThread ui_thread_; | |
| 189 BrowserThread file_thread_; | |
| 190 std::string metric_name_; | |
|
gfeher
2011/07/18 12:06:12
Please add a comment for this.
Joao da Silva
2011/07/19 09:34:17
Done.
| |
| 191 std::vector<int> expected_samples_; | |
| 192 ScopedTempDir temp_dir_; | |
| 193 }; | |
| 194 | |
| 195 TEST_F(EnterpriseMetricsTest, TokenFetch) { | |
| 196 SetMetricName(kMetricToken); | |
| 197 | |
| 198 MockDeviceManagementService service; | |
| 199 service.ScheduleInitialization(0); | |
| 200 RunAllPending(); | |
| 201 scoped_ptr<DeviceManagementBackend> backend(service.CreateBackend()); | |
| 202 | |
| 203 em::DeviceRegisterRequest request; | |
| 204 DeviceRegisterResponseDelegateMock delegate; | |
| 205 EXPECT_CALL(delegate, OnError(_)).Times(AnyNumber()); | |
| 206 EXPECT_CALL(delegate, HandleRegisterResponse(_)).Times(AnyNumber()); | |
| 207 net::URLRequestStatus status; | |
| 208 | |
| 209 // Test failed requests. | |
| 210 status.set_status(net::URLRequestStatus::FAILED); | |
| 211 service.set_url_request_status(status); | |
| 212 ExpectSample(kMetricTokenFetchRequested); | |
| 213 ExpectSample(kMetricTokenFetchRequestFailed); | |
| 214 backend->ProcessRegisterRequest("token", "testid", request, &delegate); | |
| 215 EXPECT_TRUE(CheckSamples()); | |
| 216 | |
| 217 // Test invalid data. | |
| 218 std::string data("\xff"); | |
| 219 status.set_status(net::URLRequestStatus::SUCCESS); | |
| 220 service.set_url_request_status(status); | |
| 221 service.set_response_code(200); | |
| 222 service.set_data(data); | |
|
gfeher
2011/07/18 12:06:12
I suggest you to use mock_device_management_backen
Joao da Silva
2011/07/19 09:34:17
As discussed, this test is actually exercising the
| |
| 223 ExpectSample(kMetricTokenFetchRequested); | |
| 224 ExpectSample(kMetricTokenFetchBadResponse); | |
| 225 backend->ProcessRegisterRequest("token", "testid", request, &delegate); | |
| 226 EXPECT_TRUE(CheckSamples()); | |
| 227 | |
| 228 // Test various error cases. | |
| 229 struct { | |
| 230 int error_code; | |
| 231 MetricToken sample; | |
| 232 } cases[] = { | |
| 233 { 400, kMetricTokenFetchRequestFailed }, | |
| 234 { 401, kMetricTokenFetchServerFailed }, | |
| 235 { 403, kMetricTokenFetchManagementNotSupported }, | |
| 236 { 404, kMetricTokenFetchServerFailed }, | |
| 237 { 410, kMetricTokenFetchDeviceNotFound }, | |
| 238 { 412, kMetricTokenFetchServerFailed }, | |
| 239 { 500, kMetricTokenFetchServerFailed }, | |
| 240 { 503, kMetricTokenFetchServerFailed }, | |
| 241 { 902, kMetricTokenFetchServerFailed }, | |
| 242 { 491, kMetricTokenFetchServerFailed }, | |
| 243 { 901, kMetricTokenFetchDeviceNotFound }, | |
| 244 }; | |
| 245 | |
| 246 service.set_data(std::string()); | |
| 247 | |
| 248 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | |
| 249 service.set_response_code(cases[i].error_code); | |
| 250 ExpectSample(kMetricTokenFetchRequested); | |
| 251 ExpectSample(cases[i].sample); | |
| 252 backend->ProcessRegisterRequest("token", "testid", request, &delegate); | |
| 253 EXPECT_TRUE(CheckSamples()); | |
| 254 } | |
| 255 | |
| 256 // Test successful token retrieval. | |
| 257 service.set_response_code(200); | |
| 258 ExpectSample(kMetricTokenFetchRequested); | |
| 259 ExpectSample(kMetricTokenFetchResponseReceived); | |
| 260 backend->ProcessRegisterRequest("token", "testid", request, &delegate); | |
| 261 EXPECT_TRUE(CheckSamples()); | |
| 262 | |
| 263 // Test token fetcher. | |
| 264 UserPolicyCache cache(temp_dir().AppendASCII("FetchTokenTest")); | |
| 265 scoped_ptr<CloudPolicyDataStore> data_store; | |
| 266 data_store.reset(CloudPolicyDataStore::CreateForUserPolicies()); | |
| 267 data_store->SetupForTesting("", "fake_device_id", "fake_user_name", | |
| 268 "fake_auth_token", true); | |
| 269 PolicyNotifier notifier; | |
| 270 DeviceTokenFetcher fetcher(&service, &cache, data_store.get(), ¬ifier); | |
| 271 | |
| 272 MockTokenAvailableObserver observer; | |
| 273 data_store->AddObserver(&observer); | |
| 274 EXPECT_CALL(observer, OnDeviceTokenChanged()).Times(1); | |
| 275 | |
| 276 em::DeviceManagementResponse response; | |
| 277 response.mutable_register_response()->set_device_management_token("token"); | |
| 278 response.SerializeToString(&data); | |
| 279 service.set_data(data); | |
| 280 | |
| 281 fetcher.FetchToken(); | |
| 282 | |
| 283 ExpectSample(kMetricTokenFetchRequested); | |
| 284 ExpectSample(kMetricTokenFetchResponseReceived); | |
| 285 ExpectSample(kMetricTokenFetchOK); | |
| 286 EXPECT_TRUE(CheckSamples()); | |
| 287 | |
| 288 // Cleanup. | |
| 289 data_store->RemoveObserver(&observer); | |
| 290 } | |
| 291 | |
| 292 TEST_F(EnterpriseMetricsTest, TokenStorage) { | |
| 293 SetMetricName(kMetricToken); | |
| 294 | |
| 295 FilePath path = temp_dir().AppendASCII("StoreTokenTest"); | |
| 296 | |
| 297 scoped_ptr<CloudPolicyDataStore> data_store; | |
| 298 data_store.reset(CloudPolicyDataStore::CreateForUserPolicies()); | |
| 299 data_store->SetupForTesting("", "fake_device_id", "fake_user_name", | |
| 300 "fake_auth_token", false); | |
| 301 UserPolicyTokenCache cache(data_store.get(), path); | |
| 302 | |
| 303 // Try loading a non-existing file first. | |
| 304 cache.Load(); | |
| 305 RunAllPending(); | |
| 306 // No samples expected. | |
| 307 EXPECT_TRUE(CheckSamples()); | |
| 308 | |
| 309 // Try loading an invalid file. | |
| 310 std::string data("\xff"); | |
| 311 int result = file_util::WriteFile(path, data.c_str(), data.size()); | |
| 312 ASSERT_EQ((int) data.size(), result); | |
| 313 | |
| 314 // Make the data store expect a load from cache again. | |
| 315 data_store->SetupForTesting("", "fake_device_id", "fake_user_name", | |
| 316 "fake_auth_token", false); | |
| 317 cache.Load(); | |
| 318 RunAllPending(); | |
| 319 ExpectSample(kMetricTokenLoadFailed); | |
| 320 EXPECT_TRUE(CheckSamples()); | |
| 321 | |
| 322 // Test storing a valid cache. | |
| 323 data_store->SetupForTesting("token", "fake_device_id", "fake_user_name", | |
| 324 "fake_auth_token", false); | |
| 325 cache.OnDeviceTokenChanged(); | |
| 326 RunAllPending(); | |
| 327 ExpectSample(kMetricTokenStoreSucceeded); | |
| 328 EXPECT_TRUE(CheckSamples()); | |
| 329 | |
| 330 // Test loading a valid cache. | |
| 331 // Make the data store expect a load from cache again. | |
| 332 data_store->SetupForTesting("", "fake_device_id", "fake_user_name", | |
| 333 "fake_auth_token", false); | |
| 334 cache.Load(); | |
| 335 RunAllPending(); | |
| 336 ExpectSample(kMetricTokenLoadSucceeded); | |
| 337 EXPECT_TRUE(CheckSamples()); | |
| 338 } | |
| 339 | |
| 340 TEST_F(EnterpriseMetricsTest, PolicyFetch) { | |
|
gfeher
2011/07/18 12:06:12
I would prefer this test case to be broken up to s
Joao da Silva
2011/07/19 09:34:17
Done.
| |
| 341 SetMetricName(kMetricPolicy); | |
| 342 | |
| 343 MockDeviceManagementService service; | |
| 344 service.ScheduleInitialization(0); | |
| 345 RunAllPending(); | |
| 346 scoped_ptr<DeviceManagementBackend> backend(service.CreateBackend()); | |
| 347 | |
| 348 em::DevicePolicyRequest request; | |
| 349 DevicePolicyResponseDelegateMock delegate; | |
| 350 EXPECT_CALL(delegate, OnError(_)).Times(AnyNumber()); | |
| 351 EXPECT_CALL(delegate, HandlePolicyResponse(_)).Times(AnyNumber()); | |
| 352 net::URLRequestStatus status; | |
| 353 | |
| 354 // Test failed requests. | |
| 355 status.set_status(net::URLRequestStatus::FAILED); | |
| 356 service.set_url_request_status(status); | |
| 357 ExpectSample(kMetricPolicyFetchRequested); | |
| 358 ExpectSample(kMetricPolicyFetchRequestFailed); | |
| 359 backend->ProcessPolicyRequest("token", "testid", request, &delegate); | |
| 360 EXPECT_TRUE(CheckSamples()); | |
| 361 | |
| 362 // Test invalid data. | |
| 363 std::string data("\xff"); | |
| 364 status.set_status(net::URLRequestStatus::SUCCESS); | |
| 365 service.set_url_request_status(status); | |
| 366 service.set_response_code(200); | |
| 367 service.set_data(data); | |
| 368 ExpectSample(kMetricPolicyFetchRequested); | |
| 369 ExpectSample(kMetricPolicyFetchBadResponse); | |
| 370 backend->ProcessPolicyRequest("token", "testid", request, &delegate); | |
| 371 EXPECT_TRUE(CheckSamples()); | |
| 372 | |
| 373 // Test various error cases. | |
| 374 struct { | |
| 375 int error_code; | |
| 376 MetricPolicy sample; | |
| 377 } cases[] = { | |
| 378 { 400, kMetricPolicyFetchRequestFailed }, | |
| 379 { 401, kMetricPolicyFetchInvalidToken }, | |
| 380 { 403, kMetricPolicyFetchServerFailed }, | |
| 381 { 404, kMetricPolicyFetchServerFailed }, | |
| 382 { 410, kMetricPolicyFetchServerFailed }, | |
| 383 { 412, kMetricPolicyFetchServerFailed }, | |
| 384 { 500, kMetricPolicyFetchServerFailed }, | |
| 385 { 503, kMetricPolicyFetchServerFailed }, | |
| 386 { 902, kMetricPolicyFetchNotFound }, | |
| 387 { 491, kMetricPolicyFetchServerFailed }, | |
| 388 { 901, kMetricPolicyFetchServerFailed }, | |
| 389 }; | |
| 390 | |
| 391 service.set_data(std::string()); | |
| 392 | |
| 393 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | |
| 394 service.set_response_code(cases[i].error_code); | |
| 395 ExpectSample(kMetricPolicyFetchRequested); | |
| 396 ExpectSample(cases[i].sample); | |
| 397 backend->ProcessPolicyRequest("token", "testid", request, &delegate); | |
| 398 EXPECT_TRUE(CheckSamples()); | |
| 399 } | |
| 400 | |
| 401 // Test successful policy retrieval. | |
| 402 service.set_response_code(200); | |
| 403 ExpectSample(kMetricPolicyFetchRequested); | |
| 404 ExpectSample(kMetricPolicyFetchResponseReceived); | |
| 405 backend->ProcessPolicyRequest("token", "testid", request, &delegate); | |
| 406 EXPECT_TRUE(CheckSamples()); | |
| 407 | |
| 408 // Test fetching an invalid policy. | |
| 409 UserPolicyCache cache(temp_dir().AppendASCII("UserPolicyCacheTest")); | |
| 410 // This is to bypass the private method override in UserPolicyCache: | |
| 411 UserPolicyDiskCache::Delegate* cache_as_delegate = | |
| 412 implicit_cast<UserPolicyDiskCache::Delegate*>(&cache); | |
| 413 | |
| 414 em::CachedCloudPolicyResponse response; | |
| 415 response.mutable_cloud_policy()->set_policy_data(data); | |
| 416 cache_as_delegate->OnDiskCacheLoaded(response); | |
| 417 ExpectSample(kMetricPolicyFetchInvalidPolicy); | |
| 418 EXPECT_TRUE(CheckSamples()); | |
| 419 | |
| 420 // Test timestamp in future. | |
| 421 em::PolicyData policy_data; | |
| 422 base::TimeDelta timestamp = | |
| 423 (base::Time::NowFromSystemTime() + base::TimeDelta::FromDays(1000)) - | |
| 424 base::Time::UnixEpoch(); | |
| 425 policy_data.set_timestamp(timestamp.InMilliseconds()); | |
| 426 policy_data.SerializeToString(&data); | |
| 427 response.mutable_cloud_policy()->set_policy_data(data); | |
| 428 cache_as_delegate->OnDiskCacheLoaded(response); | |
| 429 ExpectSample(kMetricPolicyFetchTimestampInFuture); | |
| 430 EXPECT_TRUE(CheckSamples()); | |
| 431 | |
| 432 // Test policy not modified. | |
| 433 policy_data.set_timestamp(0); | |
| 434 policy_data.SerializeToString(&data); | |
| 435 response.mutable_cloud_policy()->set_policy_data(data); | |
| 436 cache_as_delegate->OnDiskCacheLoaded(response); | |
| 437 ExpectSample(kMetricPolicyFetchNotModified); | |
| 438 EXPECT_TRUE(CheckSamples()); | |
| 439 | |
| 440 // Test fetch OK. | |
| 441 cache.SetPolicy(response.cloud_policy()); | |
| 442 ExpectSample(kMetricPolicyFetchOK); | |
| 443 ExpectSample(kMetricPolicyFetchNotModified); | |
| 444 EXPECT_TRUE(CheckSamples()); | |
| 445 // This also triggers a store. Update the expected samples. | |
| 446 RunAllPending(); | |
| 447 ExpectSample(kMetricPolicyStoreSucceeded); | |
| 448 EXPECT_TRUE(CheckSamples()); | |
| 449 | |
| 450 // Test bad responses. | |
| 451 PolicyNotifier notifier; | |
| 452 scoped_ptr<CloudPolicyDataStore> data_store; | |
| 453 data_store.reset(CloudPolicyDataStore::CreateForUserPolicies()); | |
| 454 data_store->SetupForTesting("", "fake_device_id", "fake_user_name", | |
| 455 "fake_auth_token", true); | |
| 456 CloudPolicyController controller(NULL, &cache, NULL, data_store.get(), | |
| 457 ¬ifier); | |
| 458 em::DevicePolicyResponse device_policy_response; | |
| 459 controller.HandlePolicyResponse(device_policy_response); | |
| 460 ExpectSample(kMetricPolicyFetchBadResponse); | |
| 461 EXPECT_TRUE(CheckSamples()); | |
| 462 | |
| 463 // More bad responses. | |
| 464 em::PolicyFetchResponse* policy_fetch_response = | |
| 465 device_policy_response.add_response(); | |
| 466 policy_fetch_response->set_error_code( | |
| 467 DeviceManagementBackend::kErrorServicePolicyNotFound); | |
| 468 controller.HandlePolicyResponse(device_policy_response); | |
| 469 ExpectSample(kMetricPolicyFetchBadResponse); | |
| 470 EXPECT_TRUE(CheckSamples()); | |
| 471 } | |
| 472 | |
| 473 TEST_F(EnterpriseMetricsTest, PolicyStorage) { | |
| 474 SetMetricName(kMetricPolicy); | |
| 475 | |
| 476 FilePath path = temp_dir().AppendASCII("UserPolicyDiskCacheTest"); | |
| 477 | |
| 478 scoped_refptr<UserPolicyDiskCache> cache( | |
| 479 new UserPolicyDiskCache(base::WeakPtr<UserPolicyDiskCache::Delegate>(), | |
| 480 path)); | |
| 481 | |
| 482 // Load empty cache. | |
| 483 cache->Load(); | |
| 484 RunAllPending(); | |
| 485 // No samples expected. | |
| 486 EXPECT_TRUE(CheckSamples()); | |
| 487 | |
| 488 // Load an invalid cache. | |
| 489 std::string data("\xff"); | |
| 490 int result = file_util::WriteFile(path, data.c_str(), data.size()); | |
| 491 ASSERT_EQ((int) data.size(), result); | |
| 492 | |
| 493 cache->Load(); | |
| 494 RunAllPending(); | |
| 495 ExpectSample(kMetricPolicyLoadFailed); | |
| 496 EXPECT_TRUE(CheckSamples()); | |
| 497 | |
| 498 // Store a cache. | |
| 499 em::CachedCloudPolicyResponse response; | |
| 500 cache->Store(response); | |
| 501 RunAllPending(); | |
| 502 ExpectSample(kMetricPolicyStoreSucceeded); | |
| 503 EXPECT_TRUE(CheckSamples()); | |
| 504 | |
| 505 // Load a good cache. | |
| 506 cache->Load(); | |
| 507 RunAllPending(); | |
| 508 ExpectSample(kMetricPolicyLoadSucceeded); | |
| 509 EXPECT_TRUE(CheckSamples()); | |
| 510 } | |
| 511 | |
| 512 #if defined(OS_CHROMEOS) | |
| 513 | |
| 514 TEST_F(EnterpriseMetricsTest, DevicePolicyStorage) { | |
|
gfeher
2011/07/18 12:06:12
Could you also break this into smaller tests cases
Joao da Silva
2011/07/19 09:34:17
Done.
| |
| 515 SetMetricName(kMetricPolicy); | |
| 516 | |
| 517 scoped_ptr<CloudPolicyDataStore> data_store; | |
| 518 data_store.reset(CloudPolicyDataStore::CreateForUserPolicies()); | |
| 519 // The DevicePolicyCache is reset to a new object to have | |
| 520 // |starting_up_| set to true again, and test the loading path. | |
| 521 scoped_ptr<DevicePolicyCache> device_policy_cache; | |
| 522 em::PolicyFetchResponse response; | |
| 523 | |
| 524 // Test non-existing policy. | |
| 525 device_policy_cache.reset(new DevicePolicyCache(data_store.get(), NULL)); | |
| 526 data_store->SetupForTesting("", "id", "user", "token", false); | |
| 527 device_policy_cache->OnRetrievePolicyCompleted( | |
| 528 chromeos::SignedSettings::NOT_FOUND, response); | |
| 529 // No samples expected. | |
| 530 EXPECT_TRUE(CheckSamples()); | |
| 531 | |
| 532 // Test bad policy data. | |
| 533 response.set_policy_data(std::string("\xff")); | |
| 534 device_policy_cache.reset(new DevicePolicyCache(data_store.get(), NULL)); | |
| 535 data_store->SetupForTesting("", "id", "user", "token", false); | |
| 536 device_policy_cache->OnRetrievePolicyCompleted( | |
| 537 chromeos::SignedSettings::SUCCESS, response); | |
| 538 ExpectSample(kMetricPolicyLoadFailed); | |
| 539 EXPECT_TRUE(CheckSamples()); | |
| 540 | |
| 541 // Test more bad policy data. | |
| 542 em::PolicyData policy_data; | |
| 543 policy_data.set_request_token("token"); | |
| 544 std::string policy_data_string; | |
| 545 policy_data.SerializeToString(&policy_data_string); | |
| 546 response.set_policy_data(policy_data_string); | |
| 547 device_policy_cache.reset(new DevicePolicyCache(data_store.get(), NULL)); | |
| 548 data_store->SetupForTesting("", "id", "user", "token", false); | |
| 549 device_policy_cache->OnRetrievePolicyCompleted( | |
| 550 chromeos::SignedSettings::SUCCESS, response); | |
| 551 ExpectSample(kMetricPolicyLoadFailed); | |
| 552 EXPECT_TRUE(CheckSamples()); | |
| 553 | |
| 554 // Test good policy data. | |
| 555 policy_data.set_username("user"); | |
| 556 policy_data.set_device_id("device_id"); | |
| 557 policy_data.SerializeToString(&policy_data_string); | |
| 558 response.set_policy_data(policy_data_string); | |
| 559 device_policy_cache.reset(new DevicePolicyCache(data_store.get(), NULL)); | |
| 560 data_store->SetupForTesting("", "id", "user", "token", false); | |
| 561 device_policy_cache->OnRetrievePolicyCompleted( | |
| 562 chromeos::SignedSettings::SUCCESS, response); | |
| 563 ExpectSample(kMetricPolicyLoadSucceeded); | |
| 564 // There is no policy data though. | |
| 565 ExpectSample(kMetricPolicyFetchNotModified); | |
| 566 EXPECT_TRUE(CheckSamples()); | |
| 567 | |
| 568 // Test storing the device policy. | |
| 569 struct { | |
| 570 chromeos::SignedSettings::ReturnCode return_code; | |
| 571 int expected_retrieve; | |
| 572 int expected_sample; | |
| 573 int expected_sample2; | |
| 574 } cases[] = { | |
| 575 { chromeos::SignedSettings::SUCCESS, 1, | |
| 576 kMetricPolicyStoreSucceeded, -1 }, | |
| 577 { chromeos::SignedSettings::BAD_SIGNATURE, 0, | |
| 578 kMetricPolicyStoreFailed, kMetricPolicyFetchBadSignature }, | |
| 579 { chromeos::SignedSettings::OPERATION_FAILED, 0, | |
| 580 kMetricPolicyStoreFailed, kMetricPolicyFetchOtherFailed }, | |
| 581 }; | |
| 582 | |
| 583 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | |
| 584 TestInstallAttributes install_attributes("user"); | |
| 585 MockSignedSettingsHelper mock_signed_settings_helper; | |
| 586 | |
| 587 device_policy_cache.reset( | |
| 588 new DevicePolicyCache(data_store.get(), | |
| 589 install_attributes.install_attributes(), &mock_signed_settings_helper)); | |
| 590 | |
| 591 EXPECT_CALL(mock_signed_settings_helper, StartStorePolicyOp(_, _)).WillOnce( | |
| 592 MockSignedSettingsHelperStorePolicy(cases[i].return_code)); | |
| 593 EXPECT_CALL(mock_signed_settings_helper, CancelCallback(_)).Times(2); | |
| 594 EXPECT_CALL(mock_signed_settings_helper, | |
| 595 StartRetrievePolicyOp(_)).Times(cases[i].expected_retrieve); | |
| 596 | |
| 597 // Make the cache have |starting_up_| set to false. | |
| 598 data_store->SetupForTesting("", "id", "user", "token", false); | |
| 599 device_policy_cache->OnRetrievePolicyCompleted( | |
| 600 chromeos::SignedSettings::NOT_FOUND, response); | |
| 601 // Now trigger the store. | |
| 602 device_policy_cache->SetPolicy(response); | |
| 603 ExpectSample(cases[i].expected_sample); | |
| 604 if (cases[i].expected_sample2 >= 0) | |
| 605 ExpectSample(cases[i].expected_sample2); | |
| 606 EXPECT_TRUE(CheckSamples()); | |
| 607 | |
| 608 // Cleanup while the MockSignedSettingsHelper is alive. | |
| 609 device_policy_cache.reset(); | |
| 610 } | |
| 611 | |
| 612 // Test setting policy on non-enterprise device. | |
| 613 { | |
| 614 EnterpriseInstallAttributes install_attributes(NULL); | |
| 615 device_policy_cache.reset(new DevicePolicyCache(data_store.get(), | |
| 616 &install_attributes)); | |
| 617 // Make the cache have |starting_up_| set to false. | |
| 618 data_store->SetupForTesting("", "id", "user", "token", false); | |
| 619 device_policy_cache->OnRetrievePolicyCompleted( | |
| 620 chromeos::SignedSettings::NOT_FOUND, response); | |
| 621 // Now trigger the error. | |
| 622 device_policy_cache->SetPolicy(response); | |
| 623 ExpectSample(kMetricPolicyFetchNonEnterpriseDevice); | |
| 624 EXPECT_TRUE(CheckSamples()); | |
| 625 } | |
| 626 | |
| 627 // Test user-mismatch between device and policy. | |
| 628 { | |
| 629 TestInstallAttributes install_attributes("bogus"); | |
| 630 device_policy_cache.reset(new DevicePolicyCache(data_store.get(), | |
| 631 install_attributes.install_attributes())); | |
| 632 // Make the cache have |starting_up_| set to false. | |
| 633 data_store->SetupForTesting("", "id", "user", "token", false); | |
| 634 device_policy_cache->OnRetrievePolicyCompleted( | |
| 635 chromeos::SignedSettings::NOT_FOUND, response); | |
| 636 // Now trigger the store. | |
| 637 device_policy_cache->SetPolicy(response); | |
| 638 ExpectSample(kMetricPolicyFetchUserMismatch); | |
| 639 EXPECT_TRUE(CheckSamples()); | |
| 640 } | |
| 641 | |
| 642 // Test bad signature. | |
| 643 device_policy_cache.reset(new DevicePolicyCache(data_store.get(), NULL)); | |
| 644 // Make the cache have |starting_up_| set to false. | |
| 645 data_store->SetupForTesting("", "id", "user", "token", false); | |
| 646 device_policy_cache->OnRetrievePolicyCompleted( | |
| 647 chromeos::SignedSettings::NOT_FOUND, response); | |
| 648 // Now trigger the store. | |
| 649 device_policy_cache->OnRetrievePolicyCompleted( | |
| 650 chromeos::SignedSettings::BAD_SIGNATURE, response); | |
| 651 ExpectSample(kMetricPolicyFetchBadSignature); | |
| 652 EXPECT_TRUE(CheckSamples()); | |
| 653 | |
| 654 // Test other failures. | |
| 655 device_policy_cache.reset(new DevicePolicyCache(data_store.get(), NULL)); | |
| 656 // Make the cache have |starting_up_| set to false. | |
| 657 data_store->SetupForTesting("", "id", "user", "token", false); | |
| 658 device_policy_cache->OnRetrievePolicyCompleted( | |
| 659 chromeos::SignedSettings::NOT_FOUND, response); | |
| 660 // Now trigger the store. | |
| 661 device_policy_cache->OnRetrievePolicyCompleted( | |
| 662 chromeos::SignedSettings::OPERATION_FAILED, response); | |
| 663 ExpectSample(kMetricPolicyFetchOtherFailed); | |
| 664 EXPECT_TRUE(CheckSamples()); | |
| 665 | |
| 666 // Test fetching invalid policy. | |
| 667 { | |
| 668 TestInstallAttributes install_attributes("user"); | |
| 669 device_policy_cache.reset(new DevicePolicyCache(data_store.get(), | |
| 670 install_attributes.install_attributes())); | |
| 671 // Make the cache have |starting_up_| set to false. | |
| 672 data_store->SetupForTesting("", "id", "user", "token", false); | |
| 673 device_policy_cache->OnRetrievePolicyCompleted( | |
| 674 chromeos::SignedSettings::NOT_FOUND, response); | |
| 675 // Now trigger the store. | |
| 676 response.set_policy_data(std::string("\xff")); | |
| 677 device_policy_cache->SetPolicy(response); | |
| 678 ExpectSample(kMetricPolicyFetchInvalidPolicy); | |
| 679 EXPECT_TRUE(CheckSamples()); | |
| 680 } | |
| 681 } | |
| 682 | |
| 683 #endif | |
| 684 | |
| 685 } // namespace policy | |
| 686 | |
| 687 #if defined(OS_CHROMEOS) | |
| 688 | |
| 689 namespace chromeos { | |
| 690 | |
| 691 namespace { | |
| 692 | |
| 693 void assert_handler(const std::string& str) { | |
| 694 LOG(INFO) << "Previous failure was expected, ignoring."; | |
| 695 } | |
| 696 | |
| 697 } // namespace | |
| 698 | |
| 699 class EnterpriseMetricsEnrollmentTest : public WizardInProcessBrowserTest { | |
|
gfeher
2011/07/18 12:06:12
Please put this into its own file.
Joao da Silva
2011/07/19 09:34:17
Done.
| |
| 700 protected: | |
| 701 EnterpriseMetricsEnrollmentTest() | |
| 702 : WizardInProcessBrowserTest( | |
| 703 WizardController::kEnterpriseEnrollmentScreenName) {} | |
| 704 | |
| 705 virtual void SetUpOnMainThread() OVERRIDE { | |
| 706 WizardInProcessBrowserTest::SetUpOnMainThread(); | |
| 707 | |
| 708 ASSERT_TRUE(controller() != NULL); | |
| 709 | |
| 710 // Use mock URLFetchers. | |
| 711 URLFetcher::set_factory(&factory_); | |
|
gfeher
2011/07/18 12:06:12
Are you using this anywhere?
Joao da Silva
2011/07/19 09:34:17
Yes, in the call to screen_->Authenticate(). It wi
| |
| 712 | |
| 713 screen_ = controller()->GetEnterpriseEnrollmentScreen(); | |
| 714 | |
| 715 ASSERT_TRUE(screen_ != NULL); | |
| 716 ASSERT_EQ(controller()->current_screen(), screen_); | |
| 717 } | |
| 718 | |
| 719 virtual void CleanUpOnMainThread() OVERRIDE { | |
| 720 WizardInProcessBrowserTest::CleanUpOnMainThread(); | |
| 721 | |
| 722 // Check that no other counters were sampled. | |
| 723 base::Histogram::SampleSet samples; | |
| 724 GetSampleSet(&samples); | |
| 725 EXPECT_EQ(1, samples.TotalCount()); | |
| 726 } | |
| 727 | |
| 728 // This is used to let expected NOTREACHED paths go on instead of making | |
| 729 // the test fail. It will also let CHECKs and DCHECKs pass, but that should be | |
| 730 // tested elsewhere. | |
| 731 void ExpectNOTREACHED() { | |
| 732 logging::SetLogAssertHandler(assert_handler); | |
|
gfeher
2011/07/18 12:06:12
Which is the particular NOTREACHED you are trying
Joao da Silva
2011/07/19 09:34:17
Some code paths within EnterpriseEnrollmentScreen
| |
| 733 } | |
| 734 | |
| 735 void ExpectSample(int sample) { | |
| 736 ASSERT_GE(sample, 0); | |
| 737 ASSERT_LT(sample, policy::kMetricEnrollmentSize); | |
| 738 | |
| 739 base::Histogram::SampleSet samples; | |
| 740 GetSampleSet(&samples); | |
| 741 EXPECT_EQ(1, samples.counts(sample)); | |
| 742 } | |
| 743 | |
| 744 EnterpriseEnrollmentScreen* screen_; | |
| 745 | |
| 746 private: | |
| 747 void GetSampleSet(base::Histogram::SampleSet* set) { | |
| 748 base::Histogram* histogram = NULL; | |
| 749 EXPECT_TRUE(base::StatisticsRecorder::FindHistogram( | |
| 750 policy::kMetricEnrollment, &histogram)); | |
| 751 ASSERT_TRUE(histogram != NULL); | |
| 752 histogram->SnapshotSample(set); | |
| 753 } | |
| 754 | |
| 755 TestURLFetcherFactory factory_; | |
| 756 | |
| 757 DISALLOW_COPY_AND_ASSIGN(EnterpriseMetricsEnrollmentTest); | |
| 758 }; | |
| 759 | |
| 760 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, EnrollmentStart) { | |
| 761 std::string empty; | |
| 762 screen_->Authenticate(empty, empty, empty, empty); | |
| 763 ExpectSample(policy::kMetricEnrollmentStarted); | |
| 764 } | |
| 765 | |
| 766 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, EnrollmentCancelled) { | |
| 767 screen_->CancelEnrollment(); | |
| 768 ExpectSample(policy::kMetricEnrollmentCancelled); | |
| 769 } | |
| 770 | |
| 771 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, AuthTokenWrongService) { | |
| 772 ExpectNOTREACHED(); | |
| 773 screen_->OnIssueAuthTokenSuccess(std::string(), std::string()); | |
| 774 ExpectSample(policy::kMetricEnrollmentOtherFailed); | |
| 775 } | |
| 776 | |
| 777 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, | |
| 778 AuthTokenCloudPolicyNotReady) { | |
| 779 ExpectNOTREACHED(); | |
| 780 screen_->OnIssueAuthTokenSuccess(GaiaConstants::kDeviceManagementService, | |
| 781 std::string()); | |
| 782 ExpectSample(policy::kMetricEnrollmentOtherFailed); | |
| 783 } | |
| 784 | |
| 785 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, AuthTokenFailure) { | |
| 786 ExpectNOTREACHED(); | |
| 787 GoogleServiceAuthError error(GoogleServiceAuthError::SERVICE_UNAVAILABLE); | |
| 788 screen_->OnIssueAuthTokenFailure(std::string(), error); | |
| 789 ExpectSample(policy::kMetricEnrollmentOtherFailed); | |
| 790 } | |
| 791 | |
| 792 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, OnPolicyBadToken) { | |
| 793 screen_->OnPolicyStateChanged(policy::CloudPolicySubsystem::BAD_GAIA_TOKEN, | |
| 794 policy::CloudPolicySubsystem::NO_DETAILS); | |
| 795 ExpectSample(policy::kMetricEnrollmentPolicyFailed); | |
| 796 } | |
| 797 | |
| 798 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, OnPolicyNetworkError) { | |
| 799 screen_->OnPolicyStateChanged(policy::CloudPolicySubsystem::NETWORK_ERROR, | |
| 800 policy::CloudPolicySubsystem::NO_DETAILS); | |
| 801 ExpectSample(policy::kMetricEnrollmentPolicyFailed); | |
| 802 } | |
| 803 | |
| 804 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, OnPolicyUnmanaged) { | |
| 805 screen_->OnPolicyStateChanged(policy::CloudPolicySubsystem::UNMANAGED, | |
| 806 policy::CloudPolicySubsystem::NO_DETAILS); | |
| 807 ExpectSample(policy::kMetricEnrollmentNotSupported); | |
| 808 } | |
| 809 | |
| 810 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, EnrollmentOK) { | |
| 811 screen_->OnPolicyStateChanged(policy::CloudPolicySubsystem::SUCCESS, | |
| 812 policy::CloudPolicySubsystem::NO_DETAILS); | |
| 813 ExpectSample(policy::kMetricEnrollmentOK); | |
| 814 } | |
| 815 | |
| 816 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, AuthErrorConnection) { | |
| 817 GoogleServiceAuthError error(GoogleServiceAuthError::CONNECTION_FAILED); | |
| 818 screen_->OnClientLoginFailure(error); | |
| 819 ExpectSample(policy::kMetricEnrollmentNetworkFailed); | |
| 820 } | |
| 821 | |
| 822 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, AuthErrorLoginFailed) { | |
| 823 GoogleServiceAuthError error(GoogleServiceAuthError::TWO_FACTOR); | |
| 824 screen_->OnClientLoginFailure(error); | |
| 825 ExpectSample(policy::kMetricEnrollmentLoginFailed); | |
| 826 } | |
| 827 | |
| 828 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, AccountDisabled) { | |
| 829 GoogleServiceAuthError error(GoogleServiceAuthError::ACCOUNT_DISABLED); | |
| 830 screen_->OnClientLoginFailure(error); | |
| 831 ExpectSample(policy::kMetricEnrollmentNotSupported); | |
| 832 } | |
| 833 | |
| 834 IN_PROC_BROWSER_TEST_F(EnterpriseMetricsEnrollmentTest, AuthErrorUnexpected) { | |
| 835 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); | |
| 836 screen_->OnClientLoginFailure(error); | |
| 837 ExpectSample(policy::kMetricEnrollmentNetworkFailed); | |
| 838 } | |
| 839 | |
| 840 } // namespace chromeos | |
| 841 | |
| 842 #endif // OS_CHROMEOS | |
| OLD | NEW |