1 //
2 // Copyright (C) 2017 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "update_engine/cros/metrics_reporter_omaha.h"
18
19 #include <memory>
20 #include <string>
21
22 #include <base/time/time.h>
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25 #include <metrics/metrics_library_mock.h>
26
27 #include "update_engine/common/fake_clock.h"
28 #include "update_engine/cros/fake_system_state.h"
29
30 using base::TimeDelta;
31 using testing::_;
32 using testing::AnyNumber;
33 using testing::Return;
34
35 namespace chromeos_update_engine {
36 class MetricsReporterOmahaTest : public ::testing::Test {
37 protected:
38 MetricsReporterOmahaTest() = default;
39
40 // Reset the metrics_lib_ to a mock library.
SetUp()41 void SetUp() override {
42 FakeSystemState::CreateInstance();
43 fake_clock_ = FakeSystemState::Get()->fake_clock();
44 mock_metrics_lib_ = new testing::NiceMock<MetricsLibraryMock>();
45 reporter_.metrics_lib_.reset(mock_metrics_lib_);
46 }
47
48 testing::NiceMock<MetricsLibraryMock>* mock_metrics_lib_;
49 MetricsReporterOmaha reporter_;
50
51 FakeClock* fake_clock_;
52 };
53
TEST_F(MetricsReporterOmahaTest,ReportDailyMetrics)54 TEST_F(MetricsReporterOmahaTest, ReportDailyMetrics) {
55 TimeDelta age = TimeDelta::FromDays(10);
56 EXPECT_CALL(*mock_metrics_lib_,
57 SendToUMA(metrics::kMetricDailyOSAgeDays, _, _, _, _))
58 .Times(1);
59
60 reporter_.ReportDailyMetrics(age);
61 }
62
TEST_F(MetricsReporterOmahaTest,ReportUpdateCheckMetrics)63 TEST_F(MetricsReporterOmahaTest, ReportUpdateCheckMetrics) {
64 // We need to execute the report twice to test the time since last report.
65 fake_clock_->SetWallclockTime(base::Time::FromInternalValue(1000000));
66 fake_clock_->SetMonotonicTime(base::Time::FromInternalValue(1000000));
67
68 metrics::CheckResult result = metrics::CheckResult::kUpdateAvailable;
69 metrics::CheckReaction reaction = metrics::CheckReaction::kIgnored;
70 metrics::DownloadErrorCode error_code =
71 metrics::DownloadErrorCode::kHttpStatus200;
72
73 EXPECT_CALL(
74 *mock_metrics_lib_,
75 SendEnumToUMA(metrics::kMetricCheckResult, static_cast<int>(result), _))
76 .Times(2);
77 EXPECT_CALL(*mock_metrics_lib_,
78 SendEnumToUMA(
79 metrics::kMetricCheckReaction, static_cast<int>(reaction), _))
80 .Times(2);
81 EXPECT_CALL(*mock_metrics_lib_,
82 SendSparseToUMA(metrics::kMetricCheckDownloadErrorCode,
83 static_cast<int>(error_code)))
84 .Times(2);
85
86 // Not pinned nor rollback
87 EXPECT_CALL(*mock_metrics_lib_,
88 SendSparseToUMA(metrics::kMetricCheckTargetVersion, _))
89 .Times(0);
90 EXPECT_CALL(*mock_metrics_lib_,
91 SendSparseToUMA(metrics::kMetricCheckRollbackTargetVersion, _))
92 .Times(0);
93
94 EXPECT_CALL(
95 *mock_metrics_lib_,
96 SendToUMA(metrics::kMetricCheckTimeSinceLastCheckMinutes, 1, _, _, _))
97 .Times(1);
98 EXPECT_CALL(
99 *mock_metrics_lib_,
100 SendToUMA(
101 metrics::kMetricCheckTimeSinceLastCheckUptimeMinutes, 1, _, _, _))
102 .Times(1);
103
104 reporter_.ReportUpdateCheckMetrics(result, reaction, error_code);
105
106 // Advance the clock by 1 minute and report the same metrics again.
107 fake_clock_->SetWallclockTime(base::Time::FromInternalValue(61000000));
108 fake_clock_->SetMonotonicTime(base::Time::FromInternalValue(61000000));
109 // Allow rollback
110 reporter_.ReportUpdateCheckMetrics(result, reaction, error_code);
111 }
112
TEST_F(MetricsReporterOmahaTest,ReportUpdateCheckMetricsPinned)113 TEST_F(MetricsReporterOmahaTest, ReportUpdateCheckMetricsPinned) {
114 OmahaRequestParams params;
115 params.set_target_version_prefix("10575.");
116 params.set_rollback_allowed(false);
117 FakeSystemState::Get()->set_request_params(¶ms);
118
119 metrics::CheckResult result = metrics::CheckResult::kUpdateAvailable;
120 metrics::CheckReaction reaction = metrics::CheckReaction::kIgnored;
121 metrics::DownloadErrorCode error_code =
122 metrics::DownloadErrorCode::kHttpStatus200;
123
124 EXPECT_CALL(*mock_metrics_lib_,
125 SendSparseToUMA(metrics::kMetricCheckDownloadErrorCode, _));
126 // Target version set, but not a rollback.
127 EXPECT_CALL(*mock_metrics_lib_,
128 SendSparseToUMA(metrics::kMetricCheckTargetVersion, 10575))
129 .Times(1);
130 EXPECT_CALL(*mock_metrics_lib_,
131 SendSparseToUMA(metrics::kMetricCheckRollbackTargetVersion, _))
132 .Times(0);
133
134 reporter_.ReportUpdateCheckMetrics(result, reaction, error_code);
135 }
136
TEST_F(MetricsReporterOmahaTest,ReportUpdateCheckMetricsRollback)137 TEST_F(MetricsReporterOmahaTest, ReportUpdateCheckMetricsRollback) {
138 OmahaRequestParams params;
139 params.set_target_version_prefix("10575.");
140 params.set_rollback_allowed(true);
141 FakeSystemState::Get()->set_request_params(¶ms);
142
143 metrics::CheckResult result = metrics::CheckResult::kUpdateAvailable;
144 metrics::CheckReaction reaction = metrics::CheckReaction::kIgnored;
145 metrics::DownloadErrorCode error_code =
146 metrics::DownloadErrorCode::kHttpStatus200;
147
148 EXPECT_CALL(*mock_metrics_lib_,
149 SendSparseToUMA(metrics::kMetricCheckDownloadErrorCode, _));
150 // Rollback.
151 EXPECT_CALL(*mock_metrics_lib_,
152 SendSparseToUMA(metrics::kMetricCheckTargetVersion, 10575))
153 .Times(1);
154 EXPECT_CALL(
155 *mock_metrics_lib_,
156 SendSparseToUMA(metrics::kMetricCheckRollbackTargetVersion, 10575))
157 .Times(1);
158
159 reporter_.ReportUpdateCheckMetrics(result, reaction, error_code);
160 }
161
TEST_F(MetricsReporterOmahaTest,ReportAbnormallyTerminatedUpdateAttemptMetrics)162 TEST_F(MetricsReporterOmahaTest,
163 ReportAbnormallyTerminatedUpdateAttemptMetrics) {
164 EXPECT_CALL(*mock_metrics_lib_,
165 SendEnumToUMA(metrics::kMetricAttemptResult,
166 static_cast<int>(
167 metrics::AttemptResult::kAbnormalTermination),
168 _))
169 .Times(1);
170
171 reporter_.ReportAbnormallyTerminatedUpdateAttemptMetrics();
172 }
173
TEST_F(MetricsReporterOmahaTest,ReportUpdateAttemptMetrics)174 TEST_F(MetricsReporterOmahaTest, ReportUpdateAttemptMetrics) {
175 fake_clock_->SetWallclockTime(base::Time::FromInternalValue(1000000));
176 fake_clock_->SetMonotonicTime(base::Time::FromInternalValue(1000000));
177
178 int attempt_number = 1;
179 PayloadType payload_type = kPayloadTypeFull;
180 TimeDelta duration = TimeDelta::FromMinutes(1000);
181 TimeDelta duration_uptime = TimeDelta::FromMinutes(1000);
182
183 int64_t payload_size = 100 * kNumBytesInOneMiB;
184
185 metrics::AttemptResult attempt_result =
186 metrics::AttemptResult::kInternalError;
187 ErrorCode internal_error_code = ErrorCode::kDownloadInvalidMetadataSignature;
188
189 EXPECT_CALL(*mock_metrics_lib_,
190 SendToUMA(metrics::kMetricAttemptNumber, attempt_number, _, _, _))
191 .Times(2);
192 EXPECT_CALL(*mock_metrics_lib_,
193 SendEnumToUMA(metrics::kMetricAttemptPayloadType,
194 static_cast<int>(payload_type),
195 _))
196 .Times(2);
197 EXPECT_CALL(*mock_metrics_lib_,
198 SendToUMA(metrics::kMetricAttemptDurationMinutes,
199 duration.InMinutes(),
200 _,
201 _,
202 _))
203 .Times(2);
204 EXPECT_CALL(*mock_metrics_lib_,
205 SendToUMA(metrics::kMetricAttemptDurationUptimeMinutes,
206 duration_uptime.InMinutes(),
207 _,
208 _,
209 _))
210 .Times(2);
211
212 // Check the report of attempt result.
213 EXPECT_CALL(
214 *mock_metrics_lib_,
215 SendEnumToUMA(
216 metrics::kMetricAttemptResult, static_cast<int>(attempt_result), _))
217 .Times(2);
218 EXPECT_CALL(*mock_metrics_lib_,
219 SendEnumToUMA(metrics::kMetricAttemptInternalErrorCode,
220 static_cast<int>(internal_error_code),
221 _))
222 .Times(2);
223 EXPECT_CALL(*mock_metrics_lib_,
224 SendToUMA(metrics::kMetricAttemptPayloadSizeMiB, 100, _, _, _))
225 .Times(2);
226
227 // Check the duration between two reports.
228 EXPECT_CALL(
229 *mock_metrics_lib_,
230 SendToUMA(metrics::kMetricAttemptTimeSinceLastAttemptMinutes, 1, _, _, _))
231 .Times(1);
232 EXPECT_CALL(
233 *mock_metrics_lib_,
234 SendToUMA(
235 metrics::kMetricAttemptTimeSinceLastAttemptUptimeMinutes, 1, _, _, _))
236 .Times(1);
237
238 reporter_.ReportUpdateAttemptMetrics(attempt_number,
239 payload_type,
240 duration,
241 duration_uptime,
242 payload_size,
243 attempt_result,
244 internal_error_code);
245
246 // Advance the clock by 1 minute and report the same metrics again.
247 fake_clock_->SetWallclockTime(base::Time::FromInternalValue(61000000));
248 fake_clock_->SetMonotonicTime(base::Time::FromInternalValue(61000000));
249 reporter_.ReportUpdateAttemptMetrics(attempt_number,
250 payload_type,
251 duration,
252 duration_uptime,
253 payload_size,
254 attempt_result,
255 internal_error_code);
256 }
257
TEST_F(MetricsReporterOmahaTest,ReportUpdateAttemptDownloadMetrics)258 TEST_F(MetricsReporterOmahaTest, ReportUpdateAttemptDownloadMetrics) {
259 int64_t payload_bytes_downloaded = 200 * kNumBytesInOneMiB;
260 int64_t payload_download_speed_bps = 100 * 1000;
261 DownloadSource download_source = kDownloadSourceHttpServer;
262 metrics::DownloadErrorCode payload_download_error_code =
263 metrics::DownloadErrorCode::kDownloadError;
264 metrics::ConnectionType connection_type = metrics::ConnectionType::kCellular;
265
266 EXPECT_CALL(
267 *mock_metrics_lib_,
268 SendToUMA(metrics::kMetricAttemptPayloadBytesDownloadedMiB, 200, _, _, _))
269 .Times(1);
270 EXPECT_CALL(
271 *mock_metrics_lib_,
272 SendToUMA(metrics::kMetricAttemptPayloadDownloadSpeedKBps, 100, _, _, _))
273 .Times(1);
274 EXPECT_CALL(*mock_metrics_lib_,
275 SendEnumToUMA(metrics::kMetricAttemptDownloadSource,
276 static_cast<int>(download_source),
277 _))
278 .Times(1);
279 EXPECT_CALL(*mock_metrics_lib_,
280 SendSparseToUMA(metrics::kMetricAttemptDownloadErrorCode,
281 static_cast<int>(payload_download_error_code)))
282 .Times(1);
283 EXPECT_CALL(*mock_metrics_lib_,
284 SendEnumToUMA(metrics::kMetricAttemptConnectionType,
285 static_cast<int>(connection_type),
286 _))
287 .Times(1);
288
289 reporter_.ReportUpdateAttemptDownloadMetrics(payload_bytes_downloaded,
290 payload_download_speed_bps,
291 download_source,
292 payload_download_error_code,
293 connection_type);
294 }
295
TEST_F(MetricsReporterOmahaTest,ReportSuccessfulUpdateMetrics)296 TEST_F(MetricsReporterOmahaTest, ReportSuccessfulUpdateMetrics) {
297 int attempt_count = 3;
298 int updates_abandoned_count = 2;
299 PayloadType payload_type = kPayloadTypeDelta;
300 int64_t payload_size = 200 * kNumBytesInOneMiB;
301 int64_t num_bytes_downloaded[kNumDownloadSources] = {};
302 // 200MiB payload downloaded from HttpsServer.
303 num_bytes_downloaded[0] = 200 * kNumBytesInOneMiB;
304 int download_overhead_percentage = 20;
305 TimeDelta total_duration = TimeDelta::FromMinutes(30);
306 TimeDelta total_duration_uptime = TimeDelta::FromMinutes(20);
307 int reboot_count = 2;
308 int url_switch_count = 2;
309
310 EXPECT_CALL(
311 *mock_metrics_lib_,
312 SendToUMA(metrics::kMetricSuccessfulUpdatePayloadSizeMiB, 200, _, _, _))
313 .Times(1);
314
315 // Check the report to both BytesDownloadedMiBHttpsServer and
316 // BytesDownloadedMiB
317 std::string DownloadedMiBMetric =
318 metrics::kMetricSuccessfulUpdateBytesDownloadedMiB;
319 DownloadedMiBMetric += "HttpsServer";
320 EXPECT_CALL(*mock_metrics_lib_, SendToUMA(DownloadedMiBMetric, 200, _, _, _))
321 .Times(1);
322 EXPECT_CALL(
323 *mock_metrics_lib_,
324 SendToUMA(
325 metrics::kMetricSuccessfulUpdateBytesDownloadedMiB, 200, _, _, _))
326 .Times(1);
327
328 EXPECT_CALL(
329 *mock_metrics_lib_,
330 SendToUMA(
331 metrics::kMetricSuccessfulUpdateDownloadSourcesUsed, 1, _, _, _))
332 .Times(1);
333 EXPECT_CALL(
334 *mock_metrics_lib_,
335 SendToUMA(metrics::kMetricSuccessfulUpdateDownloadOverheadPercentage,
336 20,
337 _,
338 _,
339 _));
340
341 EXPECT_CALL(*mock_metrics_lib_,
342 SendToUMA(metrics::kMetricSuccessfulUpdateUrlSwitchCount,
343 url_switch_count,
344 _,
345 _,
346 _))
347 .Times(1);
348 EXPECT_CALL(
349 *mock_metrics_lib_,
350 SendToUMA(
351 metrics::kMetricSuccessfulUpdateTotalDurationMinutes, 30, _, _, _))
352 .Times(1);
353 EXPECT_CALL(
354 *mock_metrics_lib_,
355 SendToUMA(metrics::kMetricSuccessfulUpdateTotalDurationUptimeMinutes,
356 20,
357 _,
358 _,
359 _))
360 .Times(1);
361 EXPECT_CALL(
362 *mock_metrics_lib_,
363 SendToUMA(
364 metrics::kMetricSuccessfulUpdateRebootCount, reboot_count, _, _, _))
365 .Times(1);
366 EXPECT_CALL(*mock_metrics_lib_,
367 SendEnumToUMA(
368 metrics::kMetricSuccessfulUpdatePayloadType, payload_type, _))
369 .Times(1);
370 EXPECT_CALL(
371 *mock_metrics_lib_,
372 SendToUMA(
373 metrics::kMetricSuccessfulUpdateAttemptCount, attempt_count, _, _, _))
374 .Times(1);
375 EXPECT_CALL(*mock_metrics_lib_,
376 SendToUMA(metrics::kMetricSuccessfulUpdateUpdatesAbandonedCount,
377 updates_abandoned_count,
378 _,
379 _,
380 _))
381 .Times(1);
382
383 reporter_.ReportSuccessfulUpdateMetrics(attempt_count,
384 updates_abandoned_count,
385 payload_type,
386 payload_size,
387 num_bytes_downloaded,
388 download_overhead_percentage,
389 total_duration,
390 total_duration_uptime,
391 reboot_count,
392 url_switch_count);
393 }
394
TEST_F(MetricsReporterOmahaTest,ReportRollbackMetrics)395 TEST_F(MetricsReporterOmahaTest, ReportRollbackMetrics) {
396 metrics::RollbackResult result = metrics::RollbackResult::kSuccess;
397 EXPECT_CALL(*mock_metrics_lib_,
398 SendEnumToUMA(
399 metrics::kMetricRollbackResult, static_cast<int>(result), _))
400 .Times(1);
401
402 reporter_.ReportRollbackMetrics(result);
403 }
404
TEST_F(MetricsReporterOmahaTest,ReportEnterpriseRollbackMetrics)405 TEST_F(MetricsReporterOmahaTest, ReportEnterpriseRollbackMetrics) {
406 EXPECT_CALL(*mock_metrics_lib_,
407 SendSparseToUMA(metrics::kMetricEnterpriseRollbackSuccess, 10575))
408 .Times(1);
409 EXPECT_CALL(*mock_metrics_lib_,
410 SendSparseToUMA(metrics::kMetricEnterpriseRollbackFailure, 10323))
411 .Times(1);
412
413 reporter_.ReportEnterpriseRollbackMetrics(/*success=*/true, "10575.39.2");
414 reporter_.ReportEnterpriseRollbackMetrics(/*success=*/false, "10323.67.7");
415 }
416
TEST_F(MetricsReporterOmahaTest,ReportCertificateCheckMetrics)417 TEST_F(MetricsReporterOmahaTest, ReportCertificateCheckMetrics) {
418 ServerToCheck server_to_check = ServerToCheck::kUpdate;
419 CertificateCheckResult result = CertificateCheckResult::kValid;
420 EXPECT_CALL(*mock_metrics_lib_,
421 SendEnumToUMA(metrics::kMetricCertificateCheckUpdateCheck,
422 static_cast<int>(result),
423 _))
424 .Times(1);
425
426 reporter_.ReportCertificateCheckMetrics(server_to_check, result);
427 }
428
TEST_F(MetricsReporterOmahaTest,ReportFailedUpdateCount)429 TEST_F(MetricsReporterOmahaTest, ReportFailedUpdateCount) {
430 int target_attempt = 3;
431 EXPECT_CALL(
432 *mock_metrics_lib_,
433 SendToUMA(metrics::kMetricFailedUpdateCount, target_attempt, _, _, _))
434 .Times(1);
435
436 reporter_.ReportFailedUpdateCount(target_attempt);
437 }
438
TEST_F(MetricsReporterOmahaTest,ReportTimeToReboot)439 TEST_F(MetricsReporterOmahaTest, ReportTimeToReboot) {
440 int time_to_reboot_minutes = 1000;
441 EXPECT_CALL(
442 *mock_metrics_lib_,
443 SendToUMA(
444 metrics::kMetricTimeToRebootMinutes, time_to_reboot_minutes, _, _, _))
445 .Times(1);
446
447 reporter_.ReportTimeToReboot(time_to_reboot_minutes);
448 }
449
TEST_F(MetricsReporterOmahaTest,ReportInstallDateProvisioningSource)450 TEST_F(MetricsReporterOmahaTest, ReportInstallDateProvisioningSource) {
451 int source = 2;
452 int max = 5;
453 EXPECT_CALL(
454 *mock_metrics_lib_,
455 SendEnumToUMA(metrics::kMetricInstallDateProvisioningSource, source, max))
456 .Times(1);
457
458 reporter_.ReportInstallDateProvisioningSource(source, max);
459 }
460
TEST_F(MetricsReporterOmahaTest,ReportKeyVersionMetrics)461 TEST_F(MetricsReporterOmahaTest, ReportKeyVersionMetrics) {
462 int kernel_min_version = 0x00040002;
463 int kernel_max_rollforward_version = 0xfffffffe;
464 bool kernel_max_rollforward_success = true;
465 EXPECT_CALL(
466 *mock_metrics_lib_,
467 SendSparseToUMA(metrics::kMetricKernelMinVersion, kernel_min_version))
468 .Times(1);
469 EXPECT_CALL(*mock_metrics_lib_,
470 SendSparseToUMA(metrics::kMetricKernelMaxRollforwardVersion,
471 kernel_max_rollforward_version))
472 .Times(1);
473 EXPECT_CALL(*mock_metrics_lib_,
474 SendBoolToUMA(metrics::kMetricKernelMaxRollforwardSetSuccess,
475 kernel_max_rollforward_success))
476 .Times(1);
477
478 reporter_.ReportKeyVersionMetrics(kernel_min_version,
479 kernel_max_rollforward_version,
480 kernel_max_rollforward_success);
481 }
482
TEST_F(MetricsReporterOmahaTest,ReportEnterpriseUpdateSeenToDownloadDays)483 TEST_F(MetricsReporterOmahaTest, ReportEnterpriseUpdateSeenToDownloadDays) {
484 constexpr int kDaysToUpdate = 10;
485 constexpr int kMinBucket = 1;
486 constexpr int kMaxBucket = 6 * 30; // approximately 6 months
487 constexpr int kNumBuckets = 50;
488
489 EXPECT_CALL(*mock_metrics_lib_,
490 SendToUMA(metrics::kMetricSuccessfulUpdateDurationFromSeenDays,
491 kDaysToUpdate,
492 kMinBucket,
493 kMaxBucket,
494 kNumBuckets))
495 .Times(1);
496
497 reporter_.ReportEnterpriseUpdateSeenToDownloadDays(
498 false /* has_time_restriction_policy */, kDaysToUpdate);
499 }
500
TEST_F(MetricsReporterOmahaTest,ReportEnterpriseTimeRestrictedUpdateSeenToDownloadTime)501 TEST_F(MetricsReporterOmahaTest,
502 ReportEnterpriseTimeRestrictedUpdateSeenToDownloadTime) {
503 const int kDaysToUpdate = 15;
504 constexpr int kMinBucket = 1;
505 constexpr int kMaxBucket = 6 * 30; // approximately 6 months
506 constexpr int kNumBuckets = 50;
507
508 EXPECT_CALL(
509 *mock_metrics_lib_,
510 SendToUMA(
511 metrics::kMetricSuccessfulUpdateDurationFromSeenTimeRestrictedDays,
512 kDaysToUpdate,
513 kMinBucket,
514 kMaxBucket,
515 kNumBuckets))
516 .Times(1);
517
518 reporter_.ReportEnterpriseUpdateSeenToDownloadDays(
519 true /* has_time_restriction_policy */, kDaysToUpdate);
520 }
521
TEST_F(MetricsReporterOmahaTest,WallclockDurationHelper)522 TEST_F(MetricsReporterOmahaTest, WallclockDurationHelper) {
523 base::TimeDelta duration;
524 const std::string state_variable_key = "test-prefs";
525
526 // Initialize wallclock to 1 sec.
527 fake_clock_->SetWallclockTime(base::Time::FromInternalValue(1000000));
528
529 // First time called so no previous measurement available.
530 EXPECT_FALSE(
531 reporter_.WallclockDurationHelper(state_variable_key, &duration));
532
533 // Next time, we should get zero since the clock didn't advance.
534 EXPECT_TRUE(reporter_.WallclockDurationHelper(state_variable_key, &duration));
535 EXPECT_EQ(duration.InSeconds(), 0);
536
537 // We can also call it as many times as we want with it being
538 // considered a failure.
539 EXPECT_TRUE(reporter_.WallclockDurationHelper(state_variable_key, &duration));
540 EXPECT_EQ(duration.InSeconds(), 0);
541 EXPECT_TRUE(reporter_.WallclockDurationHelper(state_variable_key, &duration));
542 EXPECT_EQ(duration.InSeconds(), 0);
543
544 // Advance the clock one second, then we should get 1 sec on the
545 // next call and 0 sec on the subsequent call.
546 fake_clock_->SetWallclockTime(base::Time::FromInternalValue(2000000));
547 EXPECT_TRUE(reporter_.WallclockDurationHelper(state_variable_key, &duration));
548 EXPECT_EQ(duration.InSeconds(), 1);
549 EXPECT_TRUE(reporter_.WallclockDurationHelper(state_variable_key, &duration));
550 EXPECT_EQ(duration.InSeconds(), 0);
551
552 // Advance clock two seconds and we should get 2 sec and then 0 sec.
553 fake_clock_->SetWallclockTime(base::Time::FromInternalValue(4000000));
554 EXPECT_TRUE(reporter_.WallclockDurationHelper(state_variable_key, &duration));
555 EXPECT_EQ(duration.InSeconds(), 2);
556 EXPECT_TRUE(reporter_.WallclockDurationHelper(state_variable_key, &duration));
557 EXPECT_EQ(duration.InSeconds(), 0);
558
559 // There's a possibility that the wallclock can go backwards (NTP
560 // adjustments, for example) so check that we properly handle this
561 // case.
562 fake_clock_->SetWallclockTime(base::Time::FromInternalValue(3000000));
563 EXPECT_FALSE(
564 reporter_.WallclockDurationHelper(state_variable_key, &duration));
565 fake_clock_->SetWallclockTime(base::Time::FromInternalValue(4000000));
566 EXPECT_TRUE(reporter_.WallclockDurationHelper(state_variable_key, &duration));
567 EXPECT_EQ(duration.InSeconds(), 1);
568 }
569
TEST_F(MetricsReporterOmahaTest,MonotonicDurationHelper)570 TEST_F(MetricsReporterOmahaTest, MonotonicDurationHelper) {
571 int64_t storage = 0;
572 base::TimeDelta duration;
573
574 // Initialize monotonic clock to 1 sec.
575 fake_clock_->SetMonotonicTime(base::Time::FromInternalValue(1000000));
576
577 // First time called so no previous measurement available.
578 EXPECT_FALSE(reporter_.MonotonicDurationHelper(&storage, &duration));
579
580 // Next time, we should get zero since the clock didn't advance.
581 EXPECT_TRUE(reporter_.MonotonicDurationHelper(&storage, &duration));
582 EXPECT_EQ(duration.InSeconds(), 0);
583
584 // We can also call it as many times as we want with it being
585 // considered a failure.
586 EXPECT_TRUE(reporter_.MonotonicDurationHelper(&storage, &duration));
587 EXPECT_EQ(duration.InSeconds(), 0);
588 EXPECT_TRUE(reporter_.MonotonicDurationHelper(&storage, &duration));
589 EXPECT_EQ(duration.InSeconds(), 0);
590
591 // Advance the clock one second, then we should get 1 sec on the
592 // next call and 0 sec on the subsequent call.
593 fake_clock_->SetMonotonicTime(base::Time::FromInternalValue(2000000));
594 EXPECT_TRUE(reporter_.MonotonicDurationHelper(&storage, &duration));
595 EXPECT_EQ(duration.InSeconds(), 1);
596 EXPECT_TRUE(reporter_.MonotonicDurationHelper(&storage, &duration));
597 EXPECT_EQ(duration.InSeconds(), 0);
598
599 // Advance clock two seconds and we should get 2 sec and then 0 sec.
600 fake_clock_->SetMonotonicTime(base::Time::FromInternalValue(4000000));
601 EXPECT_TRUE(reporter_.MonotonicDurationHelper(&storage, &duration));
602 EXPECT_EQ(duration.InSeconds(), 2);
603 EXPECT_TRUE(reporter_.MonotonicDurationHelper(&storage, &duration));
604 EXPECT_EQ(duration.InSeconds(), 0);
605 }
606
607 } // namespace chromeos_update_engine
608