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 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/system/statistics_provider_impl.h"
#include <map>
#include "ash/constants/ash_switches.h"
#include "base/command_line.h"
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/raw_ref.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/system/sys_info.h"
#include "base/test/scoped_chromeos_version_info.h"
#include "base/test/scoped_command_line.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash::system {
namespace {
// Echo is used to fake crossystem tool.
constexpr char kEchoCmd[] = "/bin/echo";
// `false` is used to fake the vpd tool.
constexpr char kFalseCmd[] = "/bin/false";
constexpr char kLsbReleaseContent[] = "CHROMEOS_RELEASE_NAME=Chromium OS\n";
constexpr char kInvalidLsbReleaseContent[] = "Just empty";
constexpr char kCrossystemToolFormat[] = "%s = %s # %s\n";
constexpr char kMachineInfoFormat[] = "\"%s\"=\"%s\"\n";
// Creates a file named with `filename` in the temp dir and fills it with
// `content`. Returns path to the created file.
base::FilePath CreateFileInTempDir(const std::string& content,
const base::ScopedTempDir& temp_dir) {
EXPECT_TRUE(temp_dir.IsValid());
base::FilePath filepath;
base::File file =
base::CreateAndOpenTemporaryFileInDir(temp_dir.GetPath(), &filepath);
EXPECT_TRUE(file.IsValid());
EXPECT_FALSE(filepath.empty());
EXPECT_TRUE(base::WriteFile(filepath, content));
return filepath;
}
void LoadStatistics(StatisticsProviderImpl* provider, bool load_oem_manifest) {
base::RunLoop loading_loop;
provider->ScheduleOnMachineStatisticsLoaded(loading_loop.QuitClosure());
provider->StartLoadingMachineStatistics(load_oem_manifest);
loading_loop.Run();
}
// Exit codes for the dump_filtered_vpd utility.
enum DumpVpdExitCode {
kValid = 0,
kRoInvalid = 1,
kRwInvalid = 2,
kBothInvalid = kRoInvalid | kRwInvalid,
};
base::CommandLine GenerateFakeVpdCommand(
const std::map<std::string, std::string>& contents,
int exit_status = 0) {
std::string shell_arg = kEchoCmd;
shell_arg += " '";
for (const auto& [key, value] : contents) {
shell_arg +=
base::StringPrintf(kMachineInfoFormat, key.c_str(), value.c_str());
}
shell_arg += "'; exit " + base::NumberToString(exit_status);
return base::CommandLine({"/bin/sh", "-c", shell_arg});
}
class SourcesBuilder {
public:
explicit SourcesBuilder(const base::ScopedTempDir& temp_dir)
: temp_dir_(temp_dir) {}
SourcesBuilder(const SourcesBuilder&) = delete;
SourcesBuilder& operator=(const SourcesBuilder&) = delete;
SourcesBuilder& set_crossystem_tool(const base::CommandLine& tool_cmd) {
sources_.crossystem_tool = tool_cmd;
return *this;
}
SourcesBuilder& set_vpd_tool(const base::CommandLine& tool_cmd) {
sources_.vpd_tool = tool_cmd;
return *this;
}
SourcesBuilder& set_machine_info(const base::FilePath& filepath) {
sources_.machine_info_filepath = filepath;
return *this;
}
SourcesBuilder& set_oem_manifest(const base::FilePath& filepath) {
sources_.oem_manifest_filepath = filepath;
return *this;
}
SourcesBuilder& set_cros_regions(const base::FilePath& filepath) {
sources_.cros_regions_filepath = filepath;
return *this;
}
StatisticsProviderImpl::StatisticsSources Build() {
if (sources_.crossystem_tool.GetProgram().empty()) {
sources_.crossystem_tool = base::CommandLine(base::FilePath(kEchoCmd));
}
if (sources_.vpd_tool.GetProgram().empty()) {
sources_.vpd_tool = base::CommandLine(base::FilePath(kFalseCmd));
}
if (sources_.machine_info_filepath.empty()) {
sources_.machine_info_filepath = CreateFileInTempDir("", *temp_dir_);
}
if (sources_.oem_manifest_filepath.empty()) {
sources_.oem_manifest_filepath = CreateFileInTempDir("", *temp_dir_);
}
if (sources_.cros_regions_filepath.empty()) {
sources_.cros_regions_filepath = CreateFileInTempDir("", *temp_dir_);
}
return std::move(sources_);
}
private:
const raw_ref<const base::ScopedTempDir> temp_dir_;
StatisticsProviderImpl::StatisticsSources sources_;
};
} // namespace
class StatisticsProviderImplTest : public testing::Test {
protected:
StatisticsProviderImplTest() { SetupFiles(); }
const base::ScopedTempDir& temp_dir() const { return temp_dir_; }
private:
void SetupFiles() { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
base::ScopedTempDir temp_dir_;
base::test::TaskEnvironment test_task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
};
// Test that the provider loads statistics from the tool if they have correct
// format, and ignores statistics errors. The crossystem tool is faked with
// echo command printing formatted statistics.
TEST_F(StatisticsProviderImplTest, LoadsStatisticsFromCrossystemTool) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(kLsbReleaseContent,
base::Time());
ASSERT_TRUE(base::SysInfo::IsRunningOnChromeOS());
// Setup provider's sources.
const std::string kEchoArgs =
base::StringPrintf(kCrossystemToolFormat, "crossystem_key_1",
"crossystem_value_1", "Valid statistic") +
base::StringPrintf(kCrossystemToolFormat, "crossystem_key_2",
"crossystem_value_2", "Valid statistic") +
base::StringPrintf(kCrossystemToolFormat, "crossystem_key_3", "(error)",
"Invalid statistic to be erased") +
"crossystem_key_4 : invalid_separator # Invalid statistic\n" +
base::StringPrintf(kCrossystemToolFormat, "crossystem_key_5", "(error)",
"Invalid statistic to be erased");
StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir())
.set_crossystem_tool(base::CommandLine({kEchoCmd, kEchoArgs}))
.Build();
// Load statistics.
auto provider = StatisticsProviderImpl::CreateProviderForTesting(
std::move(testing_sources));
LoadStatistics(provider.get(), /*load_oem_manifest=*/false);
// Check statistics.
EXPECT_EQ(provider->GetMachineStatistic("crossystem_key_1"),
"crossystem_value_1");
EXPECT_EQ(provider->GetMachineStatistic("crossystem_key_2"),
"crossystem_value_2");
EXPECT_FALSE(provider->GetMachineStatistic("crossystem_key_3"));
EXPECT_FALSE(provider->GetMachineStatistic("crossystem_key_4"));
EXPECT_FALSE(provider->GetMachineStatistic("crossystem_key_5"));
}
// Tests that provider has special handling for the firmware write protect key
// loaded from crossystem tool.
TEST_F(StatisticsProviderImplTest,
LoadsFirmwareWriteProtectCurrentKeyFromOtherSourceThanCrossystemTool) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(kLsbReleaseContent,
base::Time());
ASSERT_TRUE(base::SysInfo::IsRunningOnChromeOS());
// Setup provider's sources.
const std::string kEchoArgs =
base::StringPrintf(kCrossystemToolFormat, kFirmwareWriteProtectCurrentKey,
"crossytem_value", "");
const std::string kMachineInfoStatistics =
base::StringPrintf(kMachineInfoFormat, kFirmwareWriteProtectCurrentKey,
"machine_info_value");
StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir())
.set_crossystem_tool(base::CommandLine({kEchoCmd, kEchoArgs}))
.set_machine_info(
CreateFileInTempDir(kMachineInfoStatistics, temp_dir()))
.Build();
// Load statistics.
auto provider = StatisticsProviderImpl::CreateProviderForTesting(
std::move(testing_sources));
LoadStatistics(provider.get(), /*load_oem_manifest=*/false);
// Check statistics.
EXPECT_EQ(provider->GetMachineStatistic(kFirmwareWriteProtectCurrentKey),
"machine_info_value");
}
// Tests that provider has special handling for the firmware write protect key
// loaded from crossystem tool.
TEST_F(
StatisticsProviderImplTest,
LoadsFirmwareWriteProtectCurrentKeyFromCrossystemToolIfNoOtherSourceHasIt) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(kLsbReleaseContent,
base::Time());
ASSERT_TRUE(base::SysInfo::IsRunningOnChromeOS());
// Setup provider's sources.
const std::string kEchoArgs =
base::StringPrintf(kCrossystemToolFormat, kFirmwareWriteProtectCurrentKey,
"crossytem_value", "");
const std::string kMachineInfoStatistics = base::StringPrintf(
kMachineInfoFormat, "machine_info_completely_different_key",
"machine_info_value");
StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir())
.set_crossystem_tool(base::CommandLine({kEchoCmd, kEchoArgs}))
.set_machine_info(
CreateFileInTempDir(kMachineInfoStatistics, temp_dir()))
.Build();
// Load statistics.
auto provider = StatisticsProviderImpl::CreateProviderForTesting(
std::move(testing_sources));
LoadStatistics(provider.get(), /*load_oem_manifest=*/false);
// Check statistics.
EXPECT_EQ(provider->GetMachineStatistic(kFirmwareWriteProtectCurrentKey),
"crossytem_value");
}
// Tests that StatisticsProvider skips crossystem tool in non-ChromeOS test
// environment.
TEST_F(StatisticsProviderImplTest,
DoesNotLoadStatisticsFromCrossystemToolIfNotRunningChromeOS) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(
kInvalidLsbReleaseContent, base::Time());
ASSERT_FALSE(base::SysInfo::IsRunningOnChromeOS());
// Setup provider's sources.
const std::string kEchoArgs =
base::StringPrintf(kCrossystemToolFormat, "key_1", "value_1",
"Valid statistic") +
base::StringPrintf(kCrossystemToolFormat, "key_2", "value_2",
"Valid statistic");
StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir())
.set_crossystem_tool(base::CommandLine({kEchoCmd, kEchoArgs}))
.Build();
// Load statistics.
auto provider = StatisticsProviderImpl::CreateProviderForTesting(
std::move(testing_sources));
LoadStatistics(provider.get(), /*load_oem_manifest=*/false);
// Check statistics.
EXPECT_FALSE(provider->GetMachineStatistic("crossystem_key_1"));
EXPECT_FALSE(provider->GetMachineStatistic("crossystem_key_2"));
}
// Test that the provider loads statistics from machine info file if they have
// correct format.
TEST_F(StatisticsProviderImplTest, LoadsStatisticsFromMachineInfoFile) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(kLsbReleaseContent,
base::Time());
ASSERT_TRUE(base::SysInfo::IsRunningOnChromeOS());
// Setup provider's sources.
const std::string kMachineInfoStatistics =
base::StringPrintf(kMachineInfoFormat, "machine_info_key_1",
"machine_info_value_1") +
base::StringPrintf(kMachineInfoFormat, "machine_info_key_2",
"machine_info_value_2") +
"machine_info_malformed_key_3 = machine_info_malformed_value_3\n" +
"machine_info_malformed_key_4 : \"machine_info_malformed_value_4\"\n" +
base::StringPrintf(kMachineInfoFormat, "machine_info_key_5",
"machine_info_value_5");
StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir())
.set_machine_info(
CreateFileInTempDir(kMachineInfoStatistics, temp_dir()))
.Build();
// Load statistics.
auto provider = StatisticsProviderImpl::CreateProviderForTesting(
std::move(testing_sources));
LoadStatistics(provider.get(), /*load_oem_manifest=*/false);
// Check statistics.
EXPECT_EQ(provider->GetMachineStatistic("machine_info_key_1"),
"machine_info_value_1");
EXPECT_EQ(provider->GetMachineStatistic("machine_info_key_2"),
"machine_info_value_2");
EXPECT_FALSE(provider->GetMachineStatistic("machine_info_malformed_key_3"));
EXPECT_FALSE(provider->GetMachineStatistic("machine_info_malformed_key_4"));
EXPECT_EQ(provider->GetMachineStatistic("machine_info_key_5"),
"machine_info_value_5");
}
// Tests that StatisticsProvider generates stub statistics file for machine info
// in in non-ChromeOS test environment.
TEST_F(StatisticsProviderImplTest,
GeneratesStubMachineInfoFileIfNotRunningChromeOS) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(
kInvalidLsbReleaseContent, base::Time());
ASSERT_FALSE(base::SysInfo::IsRunningOnChromeOS());
// Setup provider's sources.
const base::FilePath kMachineInfoFilepath =
temp_dir().GetPath().Append("machine_info");
ASSERT_FALSE(base::PathExists(kMachineInfoFilepath));
const StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir()).set_machine_info(kMachineInfoFilepath).Build();
// Load statistics.
auto provider =
StatisticsProviderImpl::CreateProviderForTesting(testing_sources);
LoadStatistics(provider.get(), /*load_oem_manifest=*/false);
// Check statistics.
const auto initial_machine_id = provider->GetMachineID();
EXPECT_TRUE(initial_machine_id && !initial_machine_id->empty());
// Check stub file exists.
EXPECT_TRUE(base::PathExists(kMachineInfoFilepath));
// Current provider is going to be destroyed, copy it's machine id.
const std::string initial_machine_id_string =
std::string(initial_machine_id.value_or(""));
// Check fresh provider.
provider = StatisticsProviderImpl::CreateProviderForTesting(testing_sources);
LoadStatistics(provider.get(), /*load_oem_manifest=*/false);
// Expect the same statistic as initial.
EXPECT_EQ(provider->GetMachineID(), initial_machine_id_string);
}
// Test that the provider loads statistics from VPD tool.
TEST_F(StatisticsProviderImplTest, LoadsVpdStatistics) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(kLsbReleaseContent,
base::Time());
ASSERT_TRUE(base::SysInfo::IsRunningOnChromeOS());
// Setup provider's sources.
const auto fake_vpd_command = GenerateFakeVpdCommand({
{"region", "nz"},
{"ActivateDate", "2000-11"},
});
StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir()).set_vpd_tool(fake_vpd_command).Build();
// Load statistics.
auto provider = StatisticsProviderImpl::CreateProviderForTesting(
std::move(testing_sources));
LoadStatistics(provider.get(), /*load_oem_manifest=*/false);
// Check statistics.
EXPECT_EQ(provider->GetMachineStatistic("region"), "nz");
EXPECT_EQ(provider->GetMachineStatistic("ActivateDate"), "2000-11");
// Check VPD status.
EXPECT_EQ(provider->GetVpdStatus(), StatisticsProvider::VpdStatus::kValid);
}
// Test that the provider records correct status when all VPD contents are
// missing.
TEST_F(StatisticsProviderImplTest, RecordsErrorIfVpdFileIsMissing) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(kLsbReleaseContent,
base::Time());
ASSERT_TRUE(base::SysInfo::IsRunningOnChromeOS());
// Setup provider's sources.
const auto fake_vpd_command =
GenerateFakeVpdCommand({}, /*exit_status=*/DumpVpdExitCode::kBothInvalid);
StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir()).set_vpd_tool(fake_vpd_command).Build();
// Load statistics.
auto provider = StatisticsProviderImpl::CreateProviderForTesting(
std::move(testing_sources));
LoadStatistics(provider.get(), /*load_oem_manifest=*/false);
// Expect invalid VPD status because all VPD is missing.
EXPECT_EQ(provider->GetVpdStatus(), StatisticsProvider::VpdStatus::kInvalid);
}
// Tests that StatisticsProvider generates stub statistics for VPD in a
// non-ChromeOS test environment.
TEST_F(StatisticsProviderImplTest, GeneratesStubVpdIfNotRunningChromeOS) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(
kInvalidLsbReleaseContent, base::Time());
ASSERT_FALSE(base::SysInfo::IsRunningOnChromeOS());
const StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir())
.Build();
// Load statistics.
auto provider =
StatisticsProviderImpl::CreateProviderForTesting(testing_sources);
LoadStatistics(provider.get(), /*load_oem_manifest=*/false);
// Check statistics.
const auto initial_activate_date =
provider->GetMachineStatistic(kActivateDateKey);
EXPECT_TRUE(initial_activate_date);
// Expect invalid VPD status because we're not ChromeOS.
EXPECT_EQ(provider->GetVpdStatus(), StatisticsProvider::VpdStatus::kInvalid);
// Current provider is going to be destroyed, copy its activate date.
const std::string initial_activate_date_string =
std::string(initial_activate_date.value_or(""));
// Check fresh provider.
provider = StatisticsProviderImpl::CreateProviderForTesting(testing_sources);
LoadStatistics(provider.get(), /*load_oem_manifest=*/false);
// Expect the same statistic as initial.
EXPECT_EQ(provider->GetMachineStatistic(kActivateDateKey),
initial_activate_date_string);
// Still expect invalid VPD status.
EXPECT_EQ(provider->GetVpdStatus(), StatisticsProvider::VpdStatus::kInvalid);
}
// Test that the provider returns correct VPD status with missing RO VPD.
TEST_F(StatisticsProviderImplTest, ReturnsInvalidRoVpdStatus) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(kLsbReleaseContent,
base::Time());
ASSERT_TRUE(base::SysInfo::IsRunningOnChromeOS());
// Setup provider's sources.
const auto fake_vpd_command =
GenerateFakeVpdCommand({{"ActivateDate", "2000-11"}},
/*exit_status=*/DumpVpdExitCode::kRoInvalid);
StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir()).set_vpd_tool(fake_vpd_command).Build();
// Load statistics.
auto provider = StatisticsProviderImpl::CreateProviderForTesting(
std::move(testing_sources));
EXPECT_EQ(provider->GetVpdStatus(), StatisticsProvider::VpdStatus::kUnknown);
base::RunLoop loading_loop;
provider->ScheduleOnMachineStatisticsLoaded(loading_loop.QuitClosure());
provider->StartLoadingMachineStatistics(/*load_oem_manifest=*/false);
loading_loop.Run();
// Check VPD status.
EXPECT_EQ(provider->GetVpdStatus(),
StatisticsProvider::VpdStatus::kRoInvalid);
}
// Test that the provider returns correct VPD status with missing RW VPD.
TEST_F(StatisticsProviderImplTest, ReturnsInvalidRwVpd) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(kLsbReleaseContent,
base::Time());
ASSERT_TRUE(base::SysInfo::IsRunningOnChromeOS());
// Setup provider's sources.
const auto fake_vpd_command =
GenerateFakeVpdCommand({{"region", "nz"}},
/*exit_status=*/DumpVpdExitCode::kRwInvalid);
StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir()).set_vpd_tool(fake_vpd_command).Build();
// Load statistics.
auto provider = StatisticsProviderImpl::CreateProviderForTesting(
std::move(testing_sources));
EXPECT_EQ(provider->GetVpdStatus(), StatisticsProvider::VpdStatus::kUnknown);
base::RunLoop loading_loop;
provider->ScheduleOnMachineStatisticsLoaded(loading_loop.QuitClosure());
provider->StartLoadingMachineStatistics(/*load_oem_manifest=*/false);
loading_loop.Run();
// Check VPD status.
EXPECT_EQ(provider->GetVpdStatus(),
StatisticsProvider::VpdStatus::kRwInvalid);
}
// Test that the provider loads correct statistics OEM file if they
// have correct format.
TEST_F(StatisticsProviderImplTest, LoadsOemManifest) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(kLsbReleaseContent,
base::Time());
ASSERT_TRUE(base::SysInfo::IsRunningOnChromeOS());
// Setup provider's sources.
constexpr char kOemManifestStatistics[] = R"({
"device_requisition": "device_requisition_value",
"not_oem_statistic_key": "not_oem_statistic_value",
"enterprise_managed": true,
"can_exit_enrollment": true,
"keyboard_driven_oobe": true,
"not_oem_flag_key": true
})";
const StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir())
.set_oem_manifest(
CreateFileInTempDir(kOemManifestStatistics, temp_dir()))
.Build();
// Load statistics without OEM flag.
auto provider =
StatisticsProviderImpl::CreateProviderForTesting(testing_sources);
LoadStatistics(provider.get(), /*load_oem_manifest=*/false);
// Check statistics.
EXPECT_FALSE(provider->GetMachineStatistic(kOemDeviceRequisitionKey));
EXPECT_FALSE(provider->GetMachineStatistic("not_oem_statistic_key"));
for (const auto* oem_flag :
{kOemIsEnterpriseManagedKey, kOemCanExitEnterpriseEnrollmentKey,
kOemKeyboardDrivenOobeKey}) {
EXPECT_EQ(provider->GetMachineFlag(oem_flag),
StatisticsProviderImpl::FlagValue::kUnset);
}
EXPECT_EQ(provider->GetMachineFlag("not_oem_flag_key"),
StatisticsProviderImpl::FlagValue::kUnset);
// Load statistics with OEM flag.
provider = StatisticsProviderImpl::CreateProviderForTesting(testing_sources);
LoadStatistics(provider.get(), /*load_oem_manifest=*/true);
// Check statistics.
EXPECT_EQ(provider->GetMachineStatistic(kOemDeviceRequisitionKey),
"device_requisition_value");
EXPECT_FALSE(provider->GetMachineStatistic("not_oem_statistic_key"));
for (const auto* oem_flag :
{kOemIsEnterpriseManagedKey, kOemCanExitEnterpriseEnrollmentKey,
kOemKeyboardDrivenOobeKey}) {
EXPECT_EQ(provider->GetMachineFlag(oem_flag),
StatisticsProviderImpl::FlagValue::kTrue);
}
EXPECT_EQ(provider->GetMachineFlag("not_oem_flag_key"),
StatisticsProviderImpl::FlagValue::kUnset);
}
// Test that the provider loads prefers OEM manifest file set by command line.
TEST_F(StatisticsProviderImplTest, LoadsOemManifestFromCommandLine) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(kLsbReleaseContent,
base::Time());
ASSERT_TRUE(base::SysInfo::IsRunningOnChromeOS());
// Setup provider's sources.
constexpr char kOemManifestStatistics[] = R"({
"device_requisition": "device_requisition_value",
"not_oem_statistic_key": "not_oem_statistic_value",
"enterprise_managed": true,
"can_exit_enrollment": true,
"keyboard_driven_oobe": true,
"not_oem_flag_key": true
})";
constexpr char kOemManifestCommandLineStatistics[] = R"({
"device_requisition": "device_requisition_command_line_value",
"not_oem_statistic_key": "not_oem_statistic_value",
"enterprise_managed": false,
"can_exit_enrollment": false,
"keyboard_driven_oobe": false,
"not_oem_flag_key": true
})";
StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir())
.set_oem_manifest(
CreateFileInTempDir(kOemManifestStatistics, temp_dir()))
.Build();
const base::FilePath oem_manifest_filepath_for_commandline =
CreateFileInTempDir(kOemManifestCommandLineStatistics, temp_dir());
base::test::ScopedCommandLine command_line;
command_line.GetProcessCommandLine()->AppendSwitchPath(
switches::kAppOemManifestFile, oem_manifest_filepath_for_commandline);
// Load statistics with OEM flag.
auto provider = StatisticsProviderImpl::CreateProviderForTesting(
std::move(testing_sources));
LoadStatistics(provider.get(), /*load_oem_manifest=*/true);
// Check statistics.
EXPECT_EQ(provider->GetMachineStatistic(kOemDeviceRequisitionKey),
"device_requisition_command_line_value");
EXPECT_FALSE(provider->GetMachineStatistic("not_oem_statistic_key"));
for (const auto* oem_flag :
{kOemIsEnterpriseManagedKey, kOemCanExitEnterpriseEnrollmentKey,
kOemKeyboardDrivenOobeKey}) {
EXPECT_EQ(provider->GetMachineFlag(oem_flag),
StatisticsProvider::FlagValue::kFalse);
}
EXPECT_EQ(provider->GetMachineFlag("not_oem_flag_key"),
StatisticsProviderImpl::FlagValue::kUnset);
}
// Tests that StatisticsProvider skips OEM manifest statistics in non-ChromeOS
// test environment.
TEST_F(StatisticsProviderImplTest, DoesNotLoadOemManifestIfNotRunningChromeOS) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(
kInvalidLsbReleaseContent, base::Time());
ASSERT_FALSE(base::SysInfo::IsRunningOnChromeOS());
// Setup provider's sources.
constexpr char kOemManifestStatistics[] = R"({
"device_requisition": "device_requisition_value",
"enterprise_managed": true,
"can_exit_enrollment": true,
"keyboard_driven_oobe": true,
})";
StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir())
.set_oem_manifest(
CreateFileInTempDir(kOemManifestStatistics, temp_dir()))
.Build();
// Load statistics with OEM flag.
auto provider = StatisticsProviderImpl::CreateProviderForTesting(
std::move(testing_sources));
LoadStatistics(provider.get(), /*load_oem_manifest=*/true);
// Check statistics.
EXPECT_FALSE(provider->GetMachineStatistic(kOemDeviceRequisitionKey));
for (const auto* oem_flag :
{kOemIsEnterpriseManagedKey, kOemCanExitEnterpriseEnrollmentKey,
kOemKeyboardDrivenOobeKey}) {
EXPECT_EQ(provider->GetMachineFlag(oem_flag),
StatisticsProviderImpl::FlagValue::kUnset);
}
}
// Test that the provider loads statistics from regions file if they
// have correct format.
TEST_F(StatisticsProviderImplTest, LoadsRegionsFile) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(kLsbReleaseContent,
base::Time());
ASSERT_TRUE(base::SysInfo::IsRunningOnChromeOS());
// Setup provider's sources.
const std::string kMachineInfoStatistics =
base::StringPrintf(kMachineInfoFormat, kRegionKey, "region_value");
// NOTE: keys in regions JSON are different from the ones that are provided.
// See `kInitialLocaleKey` and `kLocalesPath`, and
// `GetInitialTimezoneFromRegionalData` for example.
constexpr char kRegionsStatistics[] = R"({
"region_value": {
"locales": ["locale_1", "locale_2", "locale_3"],
"keyboards": ["layout_1", "layout_2", "layout_3"],
"keyboard_mechanical_layout": "mechanical_layout",
"time_zones": ["timezone_1", "timezone_2", "timezone_3"],
"non_region_key": "non_region_value"
}
})";
StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir())
.set_machine_info(
CreateFileInTempDir(kMachineInfoStatistics, temp_dir()))
.set_cros_regions(CreateFileInTempDir(kRegionsStatistics, temp_dir()))
.Build();
// Load statistics.
auto provider =
StatisticsProviderImpl::CreateProviderForTesting(testing_sources);
LoadStatistics(provider.get(), /*load_oem_manifest=*/false);
// Check statistics.
EXPECT_EQ(provider->GetMachineStatistic(kRegionKey), "region_value");
EXPECT_EQ(provider->GetMachineStatistic(kInitialLocaleKey),
"locale_1,locale_2,locale_3");
EXPECT_EQ(provider->GetMachineStatistic(kKeyboardLayoutKey),
"layout_1,layout_2,layout_3");
EXPECT_EQ(provider->GetMachineStatistic(kKeyboardMechanicalLayoutKey),
"mechanical_layout");
EXPECT_EQ(provider->GetMachineStatistic(kInitialTimezoneKey), "timezone_1");
EXPECT_FALSE(provider->GetMachineStatistic("non_region_key"));
}
// Test that the provider loads statistics from regions file from correct region
// set by command line if statistics have correct format.
TEST_F(StatisticsProviderImplTest, SetsRegionFromCommandLine) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(kLsbReleaseContent,
base::Time());
ASSERT_TRUE(base::SysInfo::IsRunningOnChromeOS());
// Setup provider's sources.
const std::string kMachineInfoStatistics =
base::StringPrintf(kMachineInfoFormat, kRegionKey, "region_value") +
base::StringPrintf(kMachineInfoFormat, kInitialLocaleKey,
"machine_info_locale") +
base::StringPrintf(kMachineInfoFormat, kKeyboardLayoutKey,
"machine_info_layout") +
base::StringPrintf(kMachineInfoFormat, kKeyboardMechanicalLayoutKey,
"machine_info_mechanical_layout") +
base::StringPrintf(kMachineInfoFormat, kInitialTimezoneKey,
"machine_info_mechanical_timezone") +
base::StringPrintf(kMachineInfoFormat, "non_region_key",
"machine_info_region_value");
constexpr char kRegionsStatistics[] = R"({
"region_switch": {
"locales": ["locale_1", "locale_2", "locale_3"],
"keyboards": ["layout_1", "layout_2", "layout_3"],
"keyboard_mechanical_layout": "mechanical_layout",
"time_zones": ["timezone_1", "timezone_2", "timezone_3"],
"non_region_key": "non_region_value"
}
})";
StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir())
.set_machine_info(
CreateFileInTempDir(kMachineInfoStatistics, temp_dir()))
.set_cros_regions(CreateFileInTempDir(kRegionsStatistics, temp_dir()))
.Build();
base::test::ScopedCommandLine command_line;
command_line.GetProcessCommandLine()->AppendSwitchASCII(switches::kCrosRegion,
"region_switch");
// Load statistics.
auto provider =
StatisticsProviderImpl::CreateProviderForTesting(testing_sources);
LoadStatistics(provider.get(), /*load_oem_manifest=*/false);
// Check statistics.
EXPECT_EQ(provider->GetMachineStatistic(kRegionKey), "region_switch");
EXPECT_EQ(provider->GetMachineStatistic(kInitialLocaleKey),
"locale_1,locale_2,locale_3");
EXPECT_EQ(provider->GetMachineStatistic(kKeyboardLayoutKey),
"layout_1,layout_2,layout_3");
EXPECT_EQ(provider->GetMachineStatistic(kKeyboardMechanicalLayoutKey),
"mechanical_layout");
EXPECT_EQ(provider->GetMachineStatistic(kInitialTimezoneKey), "timezone_1");
EXPECT_EQ(provider->GetMachineStatistic("non_region_key"),
"machine_info_region_value");
}
// Test that the provider does not load region statistics when region file does
// not exist.
TEST_F(StatisticsProviderImplTest, DoesNotLoadRegionsFileWhenFileIsMissing) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(kLsbReleaseContent,
base::Time());
ASSERT_TRUE(base::SysInfo::IsRunningOnChromeOS());
// Setup provider's sources.
const std::string kMachineInfoStatistics =
base::StringPrintf(kMachineInfoFormat, kRegionKey, "region_value");
const base::FilePath kNonExistingRegionsFilepath =
temp_dir().GetPath().Append("vpd_does_not_exist");
ASSERT_FALSE(base::PathExists(kNonExistingRegionsFilepath));
StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir())
.set_machine_info(
CreateFileInTempDir(kMachineInfoStatistics, temp_dir()))
.set_cros_regions(kNonExistingRegionsFilepath)
.Build();
// Load statistics.
auto provider = StatisticsProviderImpl::CreateProviderForTesting(
std::move(testing_sources));
LoadStatistics(provider.get(), /*load_oem_manifest=*/false);
// Check statistics.
EXPECT_EQ(provider->GetMachineStatistic(kRegionKey), "region_value");
EXPECT_FALSE(provider->GetMachineStatistic(kInitialLocaleKey));
EXPECT_FALSE(provider->GetMachineStatistic(kKeyboardLayoutKey));
EXPECT_FALSE(provider->GetMachineStatistic(kKeyboardMechanicalLayoutKey));
EXPECT_FALSE(provider->GetMachineStatistic(kInitialTimezoneKey));
}
// Test that the provider does not load region statistics when region file has
// incorrect formatting.
TEST_F(StatisticsProviderImplTest, DoesNotLoadRegionsFileWhenFileIsMalformed) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(kLsbReleaseContent,
base::Time());
ASSERT_TRUE(base::SysInfo::IsRunningOnChromeOS());
// Setup provider's sources.
const std::string kMachineInfoStatistics =
base::StringPrintf(kMachineInfoFormat, kRegionKey, "region_value");
constexpr char kRegionsStatistics[] = R"({
"region_value": ["list", "is", "not", "a" "dictionary"]
})";
StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir())
.set_machine_info(
CreateFileInTempDir(kMachineInfoStatistics, temp_dir()))
.set_cros_regions(CreateFileInTempDir(kRegionsStatistics, temp_dir()))
.Build();
// Load statistics.
auto provider = StatisticsProviderImpl::CreateProviderForTesting(
std::move(testing_sources));
LoadStatistics(provider.get(), /*load_oem_manifest=*/false);
// Check statistics.
EXPECT_EQ(provider->GetMachineStatistic(kRegionKey), "region_value");
EXPECT_FALSE(provider->GetMachineStatistic(kInitialLocaleKey));
EXPECT_FALSE(provider->GetMachineStatistic(kKeyboardLayoutKey));
EXPECT_FALSE(provider->GetMachineStatistic(kKeyboardMechanicalLayoutKey));
EXPECT_FALSE(provider->GetMachineStatistic(kInitialTimezoneKey));
}
// Test that the provider does not load region statistics when region file does
// not have correct regions key.
TEST_F(StatisticsProviderImplTest,
DoesNotLoadRegionsFileWhenRegionKeyIsMissing) {
base::test::ScopedChromeOSVersionInfo scoped_version_info(kLsbReleaseContent,
base::Time());
ASSERT_TRUE(base::SysInfo::IsRunningOnChromeOS());
// Setup provider's sources.
const std::string kMachineInfoStatistics =
base::StringPrintf(kMachineInfoFormat, kRegionKey, "region_value");
constexpr char kRegionsStatistics[] = R"({
"different_region_value": {
"locales": ["locale_1", "locale_2", "locale_3"],
"keyboards": ["layout_1", "layout_2", "layout_3"],
"keyboard_mechanical_layout": "mechanical_layout",
"time_zones": ["timezone_1", "timezone_2", "timezone_3"],
"non_region_key": "non_region_value"
}
})";
StatisticsProviderImpl::StatisticsSources testing_sources =
SourcesBuilder(temp_dir())
.set_machine_info(
CreateFileInTempDir(kMachineInfoStatistics, temp_dir()))
.set_cros_regions(CreateFileInTempDir(kRegionsStatistics, temp_dir()))
.Build();
// Load statistics.
auto provider = StatisticsProviderImpl::CreateProviderForTesting(
std::move(testing_sources));
LoadStatistics(provider.get(), /*load_oem_manifest=*/false);
// Check statistics.
EXPECT_EQ(provider->GetMachineStatistic(kRegionKey), "region_value");
EXPECT_FALSE(provider->GetMachineStatistic(kInitialLocaleKey));
EXPECT_FALSE(provider->GetMachineStatistic(kKeyboardLayoutKey));
EXPECT_FALSE(provider->GetMachineStatistic(kKeyboardMechanicalLayoutKey));
EXPECT_FALSE(provider->GetMachineStatistic(kInitialTimezoneKey));
}
} // namespace ash::system
|