1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/metrics/metrics_log.h"
#include <stddef.h>
#include <stdint.h>
#include <string>
#include "base/base64.h"
#include "base/command_line.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/bucket_ranges.h"
#include "base/metrics/sample_vector.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/system/sys_info.h"
#include "base/test/simple_test_clock.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/test/task_environment.h"
#include "base/time/default_clock.h"
#include "base/time/default_tick_clock.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "components/metrics/cpu_metrics_provider.h"
#include "components/metrics/delegating_provider.h"
#include "components/metrics/environment_recorder.h"
#include "components/metrics/metrics_pref_names.h"
#include "components/metrics/metrics_state_manager.h"
#include "components/metrics/test/test_metrics_provider.h"
#include "components/metrics/test/test_metrics_service_client.h"
#include "components/network_time/network_time_test_utils.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/testing_pref_service.h"
#include "components/variations/active_field_trials.h"
#include "services/network/test/test_shared_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/build_info.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "base/win/current_module.h"
#endif
#if BUILDFLAG(IS_LINUX)
#include "base/nix/xdg_util.h"
#include "base/scoped_environment_variable_override.h"
#endif
namespace metrics {
namespace {
const char kClientId[] = "0a94430b-18e5-43c8-a657-580f7e855ce1";
const int kSessionId = 127;
class TestMetricsLog : public MetricsLog {
public:
TestMetricsLog(const std::string& client_id,
int session_id,
LogType log_type,
MetricsServiceClient* client)
: MetricsLog(client_id, session_id, log_type, client) {}
TestMetricsLog(const TestMetricsLog&) = delete;
TestMetricsLog& operator=(const TestMetricsLog&) = delete;
~TestMetricsLog() override {}
const ChromeUserMetricsExtension& uma_proto() const {
return *MetricsLog::uma_proto();
}
ChromeUserMetricsExtension* mutable_uma_proto() {
return MetricsLog::uma_proto();
}
const SystemProfileProto& system_profile() const {
return uma_proto().system_profile();
}
};
// Returns the expected hardware class for a metrics log.
std::string GetExpectedHardwareClass() {
#if BUILDFLAG(IS_CHROMEOS_ASH)
// Currently, we are relying on base/ implementation for functionality on our
// side which can be fragile if in the future someone decides to change that.
// This replicates the logic to get the hardware class for ChromeOS and this
// result should match with the result by calling
// base::SysInfo::HardwareModelName().
std::string board = base::SysInfo::GetLsbReleaseBoard();
if (board == "unknown") {
return "";
}
const size_t index = board.find("-signed-");
if (index != std::string::npos)
board.resize(index);
return base::ToUpperASCII(board);
#else
return base::SysInfo::HardwareModelName();
#endif
}
// Sets the time in |network_time| to |time|.
void UpdateNetworkTime(network_time::NetworkTimeTracker* network_time_tracker,
base::TickClock* tick_clock,
base::Time time) {
network_time_tracker->UpdateNetworkTime(
time,
base::Seconds(1), // resolution
base::Milliseconds(250), // latency
tick_clock->NowTicks()); // posting time
}
} // namespace
class MetricsLogTest : public testing::Test {
public:
MetricsLogTest() { MetricsLog::RegisterPrefs(prefs_.registry()); }
MetricsLogTest(const MetricsLogTest&) = delete;
MetricsLogTest& operator=(const MetricsLogTest&) = delete;
~MetricsLogTest() override {}
protected:
// Check that the values in |system_values| are filled in and expected ones
// correspond to the test data defined at the top of this file.
void CheckSystemProfile(const SystemProfileProto& system_profile) {
// Check for presence of core system profile fields.
EXPECT_TRUE(system_profile.has_build_timestamp());
EXPECT_TRUE(system_profile.has_app_version());
EXPECT_TRUE(system_profile.has_channel());
EXPECT_FALSE(system_profile.has_is_extended_stable_channel());
EXPECT_TRUE(system_profile.has_application_locale());
EXPECT_TRUE(system_profile.has_client_uuid());
const SystemProfileProto::OS& os = system_profile.os();
EXPECT_TRUE(os.has_name());
EXPECT_TRUE(os.has_version());
// Check matching test brand code.
EXPECT_EQ(TestMetricsServiceClient::kBrandForTesting,
system_profile.brand_code());
// Check for presence of fields set by a metrics provider.
const SystemProfileProto::Hardware& hardware = system_profile.hardware();
EXPECT_EQ(hardware.hardware_class(), GetExpectedHardwareClass());
EXPECT_TRUE(hardware.has_cpu());
EXPECT_TRUE(hardware.cpu().has_vendor_name());
EXPECT_TRUE(hardware.cpu().has_signature());
EXPECT_TRUE(hardware.cpu().has_num_cores());
// TODO(isherman): Verify other data written into the protobuf as a result
// of this call.
}
TestMetricsServiceClient client_;
TestingPrefServiceSimple prefs_;
};
TEST_F(MetricsLogTest, RecordId) {
MetricsLog log1(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client_);
MetricsLog log2(kClientId, kSessionId, MetricsLog::INDEPENDENT_LOG, &client_);
MetricsLog log3(kClientId, kSessionId, MetricsLog::INITIAL_STABILITY_LOG,
&client_);
ASSERT_FALSE(log1.uma_proto()->has_record_id());
ASSERT_FALSE(log2.uma_proto()->has_record_id());
ASSERT_FALSE(log3.uma_proto()->has_record_id());
// Set an initial record-id value in prefs, so test values are predictable.
prefs_.SetInteger(prefs::kMetricsLogRecordId, 500);
log1.AssignRecordId(&prefs_);
log2.AssignRecordId(&prefs_);
log3.AssignRecordId(&prefs_);
EXPECT_EQ(501, log1.uma_proto()->record_id());
EXPECT_EQ(502, log2.uma_proto()->record_id());
EXPECT_EQ(503, log3.uma_proto()->record_id());
}
TEST_F(MetricsLogTest, LogType) {
MetricsLog log1(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client_);
EXPECT_EQ(MetricsLog::ONGOING_LOG, log1.log_type());
MetricsLog log2(kClientId, kSessionId, MetricsLog::INITIAL_STABILITY_LOG,
&client_);
EXPECT_EQ(MetricsLog::INITIAL_STABILITY_LOG, log2.log_type());
}
TEST_F(MetricsLogTest, BasicRecord) {
client_.set_version_string("bogus version");
const std::string kOtherClientId = "0a94430b-18e5-43c8-a657-580f7e855ce2";
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
// Clears existing command line flags and sets mock flags:
// "--mock-flag-1 --mock-flag-2=unused_value"
// Hashes of these flags should be populated on the system_profile field.
command_line->InitFromArgv(0, nullptr);
command_line->AppendSwitch("mock-flag-1");
command_line->AppendSwitchASCII("mock-flag-2", "unused_value");
#if BUILDFLAG(IS_LINUX)
base::ScopedEnvironmentVariableOverride scoped_desktop_override(
base::nix::kXdgCurrentDesktopEnvVar, "GNOME");
metrics::SystemProfileProto::OS::XdgCurrentDesktop expected_current_desktop =
metrics::SystemProfileProto::OS::GNOME;
base::ScopedEnvironmentVariableOverride scoped_session_override(
base::nix::kXdgSessionTypeEnvVar, "wayland");
metrics::SystemProfileProto::OS::XdgSessionType expected_session_type =
metrics::SystemProfileProto::OS::WAYLAND;
#endif
MetricsLog log(kOtherClientId, 137, MetricsLog::ONGOING_LOG, &client_);
std::string encoded;
log.FinalizeLog(/*truncate_events=*/false, client_.GetVersionString(),
log.GetCurrentClockTime(/*record_time_zone=*/true), &encoded);
// A couple of fields are hard to mock, so these will be copied over directly
// for the expected output.
ChromeUserMetricsExtension parsed;
ASSERT_TRUE(parsed.ParseFromString(encoded));
ChromeUserMetricsExtension expected;
expected.set_client_id(13917849739535108017ull); // Hashed kOtherClientId
expected.set_session_id(137);
SystemProfileProto* system_profile = expected.mutable_system_profile();
system_profile->set_app_version("bogus version");
// Make sure |client_uuid| in the system profile is the unhashed client id
// and is the same as the client id in |local_prefs|.
system_profile->set_client_uuid(kOtherClientId);
system_profile->set_channel(client_.GetChannel());
system_profile->set_application_locale(client_.GetApplicationLocale());
system_profile->set_brand_code(TestMetricsServiceClient::kBrandForTesting);
// Hashes of "mock-flag-1" and "mock-flag-2" from SetUpCommandLine.
system_profile->add_command_line_key_hash(2578836236);
system_profile->add_command_line_key_hash(2867288449);
#if defined(ADDRESS_SANITIZER) || DCHECK_IS_ON()
system_profile->set_is_instrumented_build(true);
#endif
metrics::SystemProfileProto::Hardware* hardware =
system_profile->mutable_hardware();
hardware->set_cpu_architecture(base::SysInfo::OperatingSystemArchitecture());
auto app_os_arch = base::SysInfo::ProcessCPUArchitecture();
if (!app_os_arch.empty())
hardware->set_app_cpu_architecture(app_os_arch);
hardware->set_system_ram_mb(base::SysInfo::AmountOfPhysicalMemoryMB());
hardware->set_hardware_class(GetExpectedHardwareClass());
#if BUILDFLAG(IS_WIN)
hardware->set_dll_base(reinterpret_cast<uint64_t>(CURRENT_MODULE()));
#endif
#if BUILDFLAG(IS_CHROMEOS_LACROS)
system_profile->mutable_os()->set_name("Lacros");
#elif BUILDFLAG(IS_CHROMEOS_ASH)
system_profile->mutable_os()->set_name("CrOS");
#else
system_profile->mutable_os()->set_name(base::SysInfo::OperatingSystemName());
#endif
system_profile->mutable_os()->set_version(
base::SysInfo::OperatingSystemVersion());
#if BUILDFLAG(IS_CHROMEOS_ASH)
system_profile->mutable_os()->set_kernel_version(
base::SysInfo::KernelVersion());
#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)
system_profile->mutable_os()->set_kernel_version(
base::SysInfo::OperatingSystemVersion());
#elif BUILDFLAG(IS_ANDROID)
system_profile->mutable_os()->set_build_fingerprint(
base::android::BuildInfo::GetInstance()->android_build_fp());
system_profile->set_app_package_name("test app");
#elif BUILDFLAG(IS_IOS)
system_profile->mutable_os()->set_build_number(
base::SysInfo::GetIOSBuildNumber());
#endif
#if BUILDFLAG(IS_LINUX)
system_profile->mutable_os()->set_xdg_session_type(expected_session_type);
system_profile->mutable_os()->set_xdg_current_desktop(
expected_current_desktop);
#endif
// Hard to mock.
system_profile->set_build_timestamp(
parsed.system_profile().build_timestamp());
#if BUILDFLAG(IS_ANDROID)
system_profile->set_installer_package(
parsed.system_profile().installer_package());
#endif
// Not tested here; instead tested in Timestamps_* tests below.
expected.mutable_time_log_created()->CopyFrom(parsed.time_log_created());
expected.mutable_time_log_closed()->CopyFrom(parsed.time_log_closed());
EXPECT_EQ(expected.SerializeAsString(), encoded);
}
TEST_F(MetricsLogTest, FinalizeLog) {
static const char kVersionString[] = "1";
static const char kNewVersionString[] = "2";
client_.set_version_string(kVersionString);
TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client_);
TestMetricsLog log2(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client_);
// Fill logs with user actions and omnibox events. We put more than the limit
// to verify that when calling FinalizeLog(), we may optionally truncate
// those events.
const int kUserActionCount = internal::kUserActionEventLimit * 2;
for (int i = 0; i < kUserActionCount; ++i) {
log.RecordUserAction("BasicAction", base::TimeTicks::Now());
log2.RecordUserAction("BasicAction", base::TimeTicks::Now());
}
const int kOmniboxEventCount = internal::kOmniboxEventLimit * 2;
for (int i = 0; i < kOmniboxEventCount; ++i) {
// Add an empty omnibox event. Not fully realistic since these are normally
// supplied by a metrics provider.
log.mutable_uma_proto()->add_omnibox_event();
log2.mutable_uma_proto()->add_omnibox_event();
}
// Finalize |log|. We truncate events, and we pass the same version string as
// the one that was used when the log was created.
std::string encoded;
log.FinalizeLog(/*truncate_events=*/true, client_.GetVersionString(),
log.GetCurrentClockTime(/*record_time_zone=*/true), &encoded);
// Finalize |log2|. We do not truncate events, and we pass a different version
// string than the one that was used when the log was created.
client_.set_version_string(kNewVersionString);
std::string encoded2;
log2.FinalizeLog(/*truncate_events=*/false, client_.GetVersionString(),
log.GetCurrentClockTime(/*record_time_zone=*/true),
&encoded2);
ChromeUserMetricsExtension parsed;
parsed.ParseFromString(encoded);
ChromeUserMetricsExtension parsed2;
parsed2.ParseFromString(encoded2);
// The user actions and omnibox events in |parsed| should have been truncated
// to the limits, while |parsed2| should be untouched.
EXPECT_EQ(parsed.user_action_event_size(), internal::kUserActionEventLimit);
EXPECT_EQ(parsed.omnibox_event_size(), internal::kOmniboxEventLimit);
EXPECT_EQ(parsed2.user_action_event_size(), kUserActionCount);
EXPECT_EQ(parsed2.omnibox_event_size(), kOmniboxEventCount);
// |kNewVersionString| (the version string when |log2| was closed) should
// have been written to |parsed2| since it differs from |kVersionString|
// (the version string when |log2| was created). |parsed| should not have it
// since the version strings were the same.
EXPECT_EQ(parsed2.system_profile().app_version(), kVersionString);
EXPECT_EQ(parsed2.system_profile().log_written_by_app_version(),
kNewVersionString);
EXPECT_EQ(parsed.system_profile().app_version(), kVersionString);
EXPECT_FALSE(parsed.system_profile().has_log_written_by_app_version());
}
TEST_F(MetricsLogTest, Timestamps_InitialStabilityLog) {
std::unique_ptr<base::SimpleTestClock> clock =
std::make_unique<base::SimpleTestClock>();
// Should not have times from initial stability logs.
clock->SetNow(base::Time::FromTimeT(1));
MetricsLog log(kClientId, kSessionId, MetricsLog::INITIAL_STABILITY_LOG,
clock.get(), nullptr, &client_);
clock->SetNow(base::Time::FromTimeT(2));
std::string encoded;
// Don't set the close_time param since this is an initial stability log.
log.FinalizeLog(/*truncate_events=*/false, client_.GetVersionString(),
/*close_time=*/absl::nullopt, &encoded);
ChromeUserMetricsExtension parsed;
ASSERT_TRUE(parsed.ParseFromString(encoded));
EXPECT_FALSE(parsed.has_time_log_created());
EXPECT_FALSE(parsed.has_time_log_closed());
}
TEST_F(MetricsLogTest, Timestamps_IndependentLog) {
std::unique_ptr<base::SimpleTestClock> clock =
std::make_unique<base::SimpleTestClock>();
// Should not have times from independent logs.
clock->SetNow(base::Time::FromTimeT(1));
MetricsLog log(kClientId, kSessionId, MetricsLog::INDEPENDENT_LOG,
clock.get(), nullptr, &client_);
clock->SetNow(base::Time::FromTimeT(2));
std::string encoded;
// Don't set the close_time param since this is an independent log.
log.FinalizeLog(/*truncate_events=*/false, client_.GetVersionString(),
/*close_time=*/absl::nullopt, &encoded);
ChromeUserMetricsExtension parsed;
ASSERT_TRUE(parsed.ParseFromString(encoded));
EXPECT_FALSE(parsed.has_time_log_created());
EXPECT_FALSE(parsed.has_time_log_closed());
}
TEST_F(MetricsLogTest, Timestamps_OngoingLog) {
std::unique_ptr<base::SimpleTestClock> clock =
std::make_unique<base::SimpleTestClock>();
// Should have times from regular (ongoing) logs.
clock->SetNow(base::Time::FromTimeT(1));
MetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, clock.get(),
nullptr, &client_);
clock->SetNow(base::Time::FromTimeT(2));
std::string encoded;
log.FinalizeLog(/*truncate_events=*/false, client_.GetVersionString(),
log.GetCurrentClockTime(/*record_time_zone=*/true), &encoded);
ChromeUserMetricsExtension parsed;
ASSERT_TRUE(parsed.ParseFromString(encoded));
EXPECT_TRUE(parsed.has_time_log_created());
EXPECT_EQ(parsed.time_log_created().time_sec(), 1);
EXPECT_EQ(parsed.time_log_created().time_source(),
ChromeUserMetricsExtension::RealLocalTime::CLIENT_CLOCK);
// The timezone should not be set in the time_log_created field.
EXPECT_FALSE(parsed.time_log_created().has_time_zone_offset_from_gmt_sec());
EXPECT_TRUE(parsed.has_time_log_closed());
EXPECT_EQ(parsed.time_log_closed().time_sec(), 2);
EXPECT_EQ(parsed.time_log_closed().time_source(),
ChromeUserMetricsExtension::RealLocalTime::CLIENT_CLOCK);
// The timezone should be set, but we don't check what it is.
EXPECT_TRUE(parsed.time_log_closed().has_time_zone_offset_from_gmt_sec());
}
TEST_F(MetricsLogTest,
Timestamps_OngoingLogLog_WithNetworkClockExists_AlwaysUnavailable) {
// Setup a network clock that doesn't provide a timestamp (time unavailable).
base::test::TaskEnvironment task_environment(
base::test::TaskEnvironment::MainThreadType::IO);
std::unique_ptr<network_time::FieldTrialTest> field_trial_test(
new network_time::FieldTrialTest());
field_trial_test->SetFeatureParams(
true, 0.0, network_time::NetworkTimeTracker::FETCHES_ON_DEMAND_ONLY,
network_time::NetworkTimeTracker::ClockDriftSamples::NO_SAMPLES);
scoped_refptr<network::TestSharedURLLoaderFactory> shared_url_loader_factory =
base::MakeRefCounted<network::TestSharedURLLoaderFactory>();
TestingPrefServiceSimple pref_service;
network_time::NetworkTimeTracker::RegisterPrefs(pref_service.registry());
network_time::NetworkTimeTracker network_time_tracker(
std::make_unique<base::DefaultClock>(),
std::make_unique<base::DefaultTickClock>(), &pref_service,
shared_url_loader_factory);
// Set up the backup client clock.
TestMetricsServiceClient client;
std::unique_ptr<base::SimpleTestClock> clock =
std::make_unique<base::SimpleTestClock>();
// Should have times from regular (ongoing) logs. These times should come
// from the backup client clock, not the (unavailable) network clock.
clock->SetNow(base::Time::FromTimeT(1));
MetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, clock.get(),
&network_time_tracker, &client);
clock->SetNow(base::Time::FromTimeT(2));
// Check the output.
std::string encoded;
log.FinalizeLog(/*truncate_events=*/false, client_.GetVersionString(),
log.GetCurrentClockTime(/*record_time_zone=*/true), &encoded);
ChromeUserMetricsExtension parsed;
ASSERT_TRUE(parsed.ParseFromString(encoded));
EXPECT_TRUE(parsed.has_time_log_created());
EXPECT_EQ(parsed.time_log_created().time_sec(), 1);
EXPECT_EQ(parsed.time_log_created().time_source(),
ChromeUserMetricsExtension::RealLocalTime::CLIENT_CLOCK);
// The timezone should not be set in the time_log_created field.
EXPECT_FALSE(parsed.time_log_created().has_time_zone_offset_from_gmt_sec());
EXPECT_TRUE(parsed.has_time_log_closed());
EXPECT_EQ(parsed.time_log_closed().time_sec(), 2);
EXPECT_EQ(parsed.time_log_closed().time_source(),
ChromeUserMetricsExtension::RealLocalTime::CLIENT_CLOCK);
// The timezone should be set, but we don't check what it is.
EXPECT_TRUE(parsed.time_log_closed().has_time_zone_offset_from_gmt_sec());
}
TEST_F(
MetricsLogTest,
Timestamps_OngoingLogLog_WithNetworkClockExists_UnavailableThenAvailable) {
// Setup a network clock that initially doesn't provide a timestamp (time
// unavailable).
base::test::TaskEnvironment task_environment(
base::test::TaskEnvironment::MainThreadType::IO);
std::unique_ptr<network_time::FieldTrialTest> field_trial_test(
new network_time::FieldTrialTest());
field_trial_test->SetFeatureParams(
true, 0.0, network_time::NetworkTimeTracker::FETCHES_ON_DEMAND_ONLY,
network_time::NetworkTimeTracker::ClockDriftSamples::NO_SAMPLES);
scoped_refptr<network::TestSharedURLLoaderFactory> shared_url_loader_factory =
base::MakeRefCounted<network::TestSharedURLLoaderFactory>();
TestingPrefServiceSimple pref_service;
network_time::NetworkTimeTracker::RegisterPrefs(pref_service.registry());
base::SimpleTestClock* clock = new base::SimpleTestClock;
base::SimpleTestTickClock* tick_clock = new base::SimpleTestTickClock();
// Do this to be sure that |is_null| returns false.
clock->Advance(base::Days(111));
tick_clock->Advance(base::Days(222));
network_time::NetworkTimeTracker network_time_tracker(
std::unique_ptr<base::Clock>(clock),
std::unique_ptr<const base::TickClock>(tick_clock), &pref_service,
shared_url_loader_factory);
// Should have times from regular (ongoing) logs. The creation time should
// come from the backup client clock; the closure time should come from the
// network clock.
clock->SetNow(base::Time::FromTimeT(1));
TestMetricsServiceClient client;
MetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, clock,
&network_time_tracker, &client);
// Advance the backup client clock. (Value should not be used; merely
// advanced to make sure the new value doesn't show up anywhere.)
clock->SetNow(base::Time::FromTimeT(2));
// Set the network time tracker.
UpdateNetworkTime(&network_time_tracker, tick_clock,
base::Time::FromTimeT(3));
// Check the output.
std::string encoded;
log.FinalizeLog(/*truncate_events=*/false, client_.GetVersionString(),
log.GetCurrentClockTime(/*record_time_zone=*/true), &encoded);
ChromeUserMetricsExtension parsed;
ASSERT_TRUE(parsed.ParseFromString(encoded));
EXPECT_TRUE(parsed.has_time_log_created());
EXPECT_EQ(parsed.time_log_created().time_sec(), 1);
EXPECT_EQ(parsed.time_log_created().time_source(),
ChromeUserMetricsExtension::RealLocalTime::CLIENT_CLOCK);
// The timezone should not be set in the time_log_created field.
EXPECT_FALSE(parsed.time_log_created().has_time_zone_offset_from_gmt_sec());
EXPECT_TRUE(parsed.has_time_log_closed());
EXPECT_EQ(parsed.time_log_closed().time_sec(), 3);
EXPECT_EQ(parsed.time_log_closed().time_source(),
ChromeUserMetricsExtension::RealLocalTime::NETWORK_TIME_CLOCK);
// The timezone should be set, but we don't check what it is.
EXPECT_TRUE(parsed.time_log_closed().has_time_zone_offset_from_gmt_sec());
}
TEST_F(MetricsLogTest,
Timestamps_OngoingLogLog_WithNetworkClockExists_AlwaysAvailable) {
// Setup a network clock that provides a timestamp.
base::test::TaskEnvironment task_environment(
base::test::TaskEnvironment::MainThreadType::IO);
std::unique_ptr<network_time::FieldTrialTest> field_trial_test(
new network_time::FieldTrialTest());
field_trial_test->SetFeatureParams(
true, 0.0, network_time::NetworkTimeTracker::FETCHES_ON_DEMAND_ONLY,
network_time::NetworkTimeTracker::ClockDriftSamples::NO_SAMPLES);
scoped_refptr<network::TestSharedURLLoaderFactory> shared_url_loader_factory =
base::MakeRefCounted<network::TestSharedURLLoaderFactory>();
TestingPrefServiceSimple pref_service;
network_time::NetworkTimeTracker::RegisterPrefs(pref_service.registry());
base::SimpleTestClock* clock = new base::SimpleTestClock;
base::SimpleTestTickClock* tick_clock = new base::SimpleTestTickClock();
// Do this to be sure that |is_null| returns false.
clock->Advance(base::Days(111));
tick_clock->Advance(base::Days(222));
network_time::NetworkTimeTracker network_time_tracker(
std::unique_ptr<base::Clock>(clock),
std::unique_ptr<const base::TickClock>(tick_clock), &pref_service,
shared_url_loader_factory);
// Should have times from regular (ongoing) logs. These times should come
// from the network clock.
// Set the backup client clock time. (Value should not be used; merely set
// to make sure the value doesn't show up anywhere.)
clock->SetNow(base::Time::FromTimeT(1));
// Set the network time tracker.
UpdateNetworkTime(&network_time_tracker, tick_clock,
base::Time::FromTimeT(2));
TestMetricsServiceClient client;
MetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, clock,
&network_time_tracker, &client);
// Advance the backup client clock. (Value should not be used; merely
// advanced to make sure the new value doesn't show up anywhere.)
clock->SetNow(base::Time::FromTimeT(3));
// Advance and set the network time clock.
UpdateNetworkTime(&network_time_tracker, tick_clock,
base::Time::FromTimeT(4));
// Check the output.
std::string encoded;
log.FinalizeLog(/*truncate_events=*/false, client_.GetVersionString(),
log.GetCurrentClockTime(/*record_time_zone=*/true), &encoded);
ChromeUserMetricsExtension parsed;
ASSERT_TRUE(parsed.ParseFromString(encoded));
EXPECT_TRUE(parsed.has_time_log_created());
// Time should be the first time returned by the network time tracker.
EXPECT_EQ(parsed.time_log_created().time_sec(), 2);
EXPECT_EQ(parsed.time_log_created().time_source(),
ChromeUserMetricsExtension::RealLocalTime::NETWORK_TIME_CLOCK);
// The timezone should not be set in the time_log_created field.
EXPECT_FALSE(parsed.time_log_created().has_time_zone_offset_from_gmt_sec());
EXPECT_TRUE(parsed.has_time_log_closed());
// Time should be the second time returned by the network time tracker.
EXPECT_EQ(parsed.time_log_closed().time_sec(), 4);
EXPECT_EQ(parsed.time_log_closed().time_source(),
ChromeUserMetricsExtension::RealLocalTime::NETWORK_TIME_CLOCK);
// The timezone should be set, but we don't check what it is.
EXPECT_TRUE(parsed.time_log_closed().has_time_zone_offset_from_gmt_sec());
}
TEST_F(MetricsLogTest, HistogramBucketFields) {
// Create buckets: 1-5, 5-7, 7-8, 8-9, 9-10, 10-11, 11-12.
base::BucketRanges ranges(8);
ranges.set_range(0, 1);
ranges.set_range(1, 5);
ranges.set_range(2, 7);
ranges.set_range(3, 8);
ranges.set_range(4, 9);
ranges.set_range(5, 10);
ranges.set_range(6, 11);
ranges.set_range(7, 12);
base::SampleVector samples(1, &ranges);
samples.Accumulate(3, 1); // Bucket 1-5.
samples.Accumulate(6, 1); // Bucket 5-7.
samples.Accumulate(8, 1); // Bucket 8-9. (7-8 skipped)
samples.Accumulate(10, 1); // Bucket 10-11. (9-10 skipped)
samples.Accumulate(11, 1); // Bucket 11-12.
TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client_);
log.RecordHistogramDelta("Test", samples);
const ChromeUserMetricsExtension& uma_proto = log.uma_proto();
const HistogramEventProto& histogram_proto =
uma_proto.histogram_event(uma_proto.histogram_event_size() - 1);
// Buckets with samples: 1-5, 5-7, 8-9, 10-11, 11-12.
// Should become: 1-/, 5-7, /-9, 10-/, /-12.
ASSERT_EQ(5, histogram_proto.bucket_size());
// 1-5 becomes 1-/ (max is same as next min).
EXPECT_TRUE(histogram_proto.bucket(0).has_min());
EXPECT_FALSE(histogram_proto.bucket(0).has_max());
EXPECT_EQ(1, histogram_proto.bucket(0).min());
// 5-7 stays 5-7 (no optimization possible).
EXPECT_TRUE(histogram_proto.bucket(1).has_min());
EXPECT_TRUE(histogram_proto.bucket(1).has_max());
EXPECT_EQ(5, histogram_proto.bucket(1).min());
EXPECT_EQ(7, histogram_proto.bucket(1).max());
// 8-9 becomes /-9 (min is same as max - 1).
EXPECT_FALSE(histogram_proto.bucket(2).has_min());
EXPECT_TRUE(histogram_proto.bucket(2).has_max());
EXPECT_EQ(9, histogram_proto.bucket(2).max());
// 10-11 becomes 10-/ (both optimizations apply, omit max is prioritized).
EXPECT_TRUE(histogram_proto.bucket(3).has_min());
EXPECT_FALSE(histogram_proto.bucket(3).has_max());
EXPECT_EQ(10, histogram_proto.bucket(3).min());
// 11-12 becomes /-12 (last record must keep max, min is same as max - 1).
EXPECT_FALSE(histogram_proto.bucket(4).has_min());
EXPECT_TRUE(histogram_proto.bucket(4).has_max());
EXPECT_EQ(12, histogram_proto.bucket(4).max());
}
TEST_F(MetricsLogTest, HistogramSamplesCount) {
const std::string histogram_name = "test";
TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client_);
// Create buckets: 1-5.
base::BucketRanges ranges(2);
ranges.set_range(0, 1);
ranges.set_range(1, 5);
// Add two samples.
base::SampleVector samples(1, &ranges);
samples.Accumulate(3, 2);
log.RecordHistogramDelta(histogram_name, samples);
EXPECT_EQ(2, log.log_metadata().samples_count.value());
// Add two more samples.
log.RecordHistogramDelta(histogram_name, samples);
EXPECT_EQ(4, log.log_metadata().samples_count.value());
}
TEST_F(MetricsLogTest, RecordEnvironment) {
TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client_);
DelegatingProvider delegating_provider;
auto cpu_provider = std::make_unique<metrics::CPUMetricsProvider>();
delegating_provider.RegisterMetricsProvider(std::move(cpu_provider));
log.RecordEnvironment(&delegating_provider);
// Check non-system profile values.
EXPECT_EQ(MetricsLog::Hash(kClientId), log.uma_proto().client_id());
EXPECT_EQ(kSessionId, log.uma_proto().session_id());
// Check that the system profile on the log has the correct values set.
CheckSystemProfile(log.system_profile());
// Call RecordEnvironment() again and verify things are are still filled in.
log.RecordEnvironment(&delegating_provider);
// Check non-system profile values.
EXPECT_EQ(MetricsLog::Hash(kClientId), log.uma_proto().client_id());
EXPECT_EQ(kSessionId, log.uma_proto().session_id());
// Check that the system profile on the log has the correct values set.
CheckSystemProfile(log.system_profile());
}
TEST_F(MetricsLogTest, RecordEnvironmentExtendedStable) {
client_.set_is_extended_stable_channel(true);
TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client_);
DelegatingProvider delegating_provider;
auto cpu_provider = std::make_unique<metrics::CPUMetricsProvider>();
delegating_provider.RegisterMetricsProvider(std::move(cpu_provider));
log.RecordEnvironment(&delegating_provider);
EXPECT_TRUE(log.system_profile().has_is_extended_stable_channel());
EXPECT_TRUE(log.system_profile().is_extended_stable_channel());
}
TEST_F(MetricsLogTest, RecordEnvironmentEnableDefault) {
TestMetricsLog log_unknown(kClientId, kSessionId, MetricsLog::ONGOING_LOG,
&client_);
DelegatingProvider delegating_provider;
log_unknown.RecordEnvironment(&delegating_provider);
EXPECT_FALSE(log_unknown.system_profile().has_uma_default_state());
client_.set_enable_default(EnableMetricsDefault::OPT_IN);
TestMetricsLog log_opt_in(kClientId, kSessionId, MetricsLog::ONGOING_LOG,
&client_);
log_opt_in.RecordEnvironment(&delegating_provider);
EXPECT_TRUE(log_opt_in.system_profile().has_uma_default_state());
EXPECT_EQ(SystemProfileProto_UmaDefaultState_OPT_IN,
log_opt_in.system_profile().uma_default_state());
client_.set_enable_default(EnableMetricsDefault::OPT_OUT);
TestMetricsLog log_opt_out(kClientId, kSessionId, MetricsLog::ONGOING_LOG,
&client_);
log_opt_out.RecordEnvironment(&delegating_provider);
EXPECT_TRUE(log_opt_out.system_profile().has_uma_default_state());
EXPECT_EQ(SystemProfileProto_UmaDefaultState_OPT_OUT,
log_opt_out.system_profile().uma_default_state());
client_.set_reporting_is_managed(true);
TestMetricsLog log_managed(kClientId, kSessionId, MetricsLog::ONGOING_LOG,
&client_);
log_managed.RecordEnvironment(&delegating_provider);
EXPECT_TRUE(log_managed.system_profile().has_uma_default_state());
EXPECT_EQ(SystemProfileProto_UmaDefaultState_POLICY_FORCED_ENABLED,
log_managed.system_profile().uma_default_state());
}
TEST_F(MetricsLogTest, InitialLogStabilityMetrics) {
TestMetricsLog log(kClientId, kSessionId, MetricsLog::INITIAL_STABILITY_LOG,
&client_);
TestMetricsProvider* test_provider = new TestMetricsProvider();
DelegatingProvider delegating_provider;
delegating_provider.RegisterMetricsProvider(
base::WrapUnique<MetricsProvider>(test_provider));
log.RecordEnvironment(&delegating_provider);
log.RecordPreviousSessionData(&delegating_provider, &prefs_);
// The test provider should have been called upon to provide initial
// stability and regular stability metrics.
EXPECT_TRUE(test_provider->provide_initial_stability_metrics_called());
EXPECT_TRUE(test_provider->provide_stability_metrics_called());
}
TEST_F(MetricsLogTest, OngoingLogStabilityMetrics) {
TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client_);
TestMetricsProvider* test_provider = new TestMetricsProvider();
DelegatingProvider delegating_provider;
delegating_provider.RegisterMetricsProvider(
base::WrapUnique<MetricsProvider>(test_provider));
log.RecordEnvironment(&delegating_provider);
log.RecordCurrentSessionData(base::TimeDelta(), base::TimeDelta(),
&delegating_provider, &prefs_);
// The test provider should have been called upon to provide regular but not
// initial stability metrics.
EXPECT_FALSE(test_provider->provide_initial_stability_metrics_called());
EXPECT_TRUE(test_provider->provide_stability_metrics_called());
}
TEST_F(MetricsLogTest, ChromeChannelWrittenToProtobuf) {
TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client_);
EXPECT_TRUE(log.uma_proto().system_profile().has_channel());
}
TEST_F(MetricsLogTest, ProductNotSetIfDefault) {
EXPECT_EQ(ChromeUserMetricsExtension::CHROME, client_.GetProduct());
TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client_);
// Check that the product isn't set, since it's default and also verify the
// default value is indeed equal to Chrome.
EXPECT_FALSE(log.uma_proto().has_product());
EXPECT_EQ(ChromeUserMetricsExtension::CHROME, log.uma_proto().product());
}
TEST_F(MetricsLogTest, ProductSetIfNotDefault) {
const int32_t kTestProduct = 100;
EXPECT_NE(ChromeUserMetricsExtension::CHROME, kTestProduct);
client_.set_product(kTestProduct);
TestMetricsLog log(kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client_);
// Check that the product is set to |kTestProduct|.
EXPECT_TRUE(log.uma_proto().has_product());
EXPECT_EQ(kTestProduct, log.uma_proto().product());
}
TEST_F(MetricsLogTest, ToInstallerPackage) {
using internal::ToInstallerPackage;
EXPECT_EQ(SystemProfileProto::INSTALLER_PACKAGE_NONE, ToInstallerPackage(""));
EXPECT_EQ(SystemProfileProto::INSTALLER_PACKAGE_GOOGLE_PLAY_STORE,
ToInstallerPackage("com.android.vending"));
EXPECT_EQ(SystemProfileProto::INSTALLER_PACKAGE_OTHER,
ToInstallerPackage("foo"));
}
} // namespace metrics
|