From 53a23380c25032a67b620356b1521d69e3a15d04 Mon Sep 17 00:00:00 2001 From: tnagel Date: Wed, 17 Dec 2014 12:28:50 -0800 Subject: Use machine-info stub with serial number for CHROMEOS=1 builds. The current practice is reading the serial number from /tmp/machine-info if it exists and generating a random one otherwise. This makes re-enrollment testing (which requires a stable serial number) somewhat cumbersome. This CL creates a machine-info stub in the profile directory so that re-enrollment can be tested without the need of manually creating a stub file and without interference between multiple profiles on the same machine. BUG=none Review URL: https://codereview.chromium.org/790673003 Cr-Commit-Position: refs/heads/master@{#308852} --- chromeos/system/statistics_provider.cc | 45 +++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 17 deletions(-) (limited to 'chromeos/system') diff --git a/chromeos/system/statistics_provider.cc b/chromeos/system/statistics_provider.cc index adb33fe..e533dd7 100644 --- a/chromeos/system/statistics_provider.cc +++ b/chromeos/system/statistics_provider.cc @@ -7,9 +7,11 @@ #include "base/bind.h" #include "base/command_line.h" #include "base/files/file_path.h" +#include "base/files/file_util.h" #include "base/location.h" #include "base/logging.h" #include "base/memory/singleton.h" +#include "base/path_service.h" #include "base/strings/string_number_conversions.h" #include "base/synchronization/cancellation_flag.h" #include "base/synchronization/waitable_event.h" @@ -19,6 +21,7 @@ #include "base/time/time.h" #include "chromeos/app_mode/kiosk_oem_manifest_parser.h" #include "chromeos/chromeos_constants.h" +#include "chromeos/chromeos_paths.h" #include "chromeos/chromeos_switches.h" #include "chromeos/system/name_value_pairs_parser.h" @@ -37,12 +40,10 @@ const char kCrosSystemUnknownValue[] = "(error)"; const char kHardwareClassCrosSystemKey[] = "hwid"; const char kUnknownHardwareClass[] = "unknown"; -const char kSerialNumber[] = "sn"; -// File to get machine hardware info from, and key/value delimiters of -// the file. machine-info is generated only for OOBE and enterprise enrollment -// and may not be present. See login-manager/init/machine-info.conf. -const char kMachineHardwareInfoFile[] = "/tmp/machine-info"; +// Key/value delimiters of machine hardware info file. machine-info is generated +// only for OOBE and enterprise enrollment and may not be present. See +// login-manager/init/machine-info.conf. const char kMachineHardwareInfoEq[] = "="; const char kMachineHardwareInfoDelim[] = " \n"; @@ -256,7 +257,28 @@ void StatisticsProviderImpl::LoadMachineStatistics(bool load_oem_manifest) { } } - parser.GetNameValuePairsFromFile(base::FilePath(kMachineHardwareInfoFile), + base::FilePath machine_info_path; + PathService::Get(chromeos::FILE_MACHINE_INFO, &machine_info_path); + if (!base::SysInfo::IsRunningOnChromeOS() && + !base::PathExists(machine_info_path)) { + // Use time value to create an unique stub serial because clashes of the + // same serial for the same domain invalidate earlier enrollments. Persist + // to disk to keep it constant across restarts (required for re-enrollment + // testing). + std::string stub_contents = + "\"serial_number\"=\"stub_" + + base::Int64ToString(base::Time::Now().ToJavaTime()) + "\"\n"; + int bytes_written = base::WriteFile(machine_info_path, + stub_contents.c_str(), + stub_contents.size()); + // static_cast is fine because stub_contents is small. + if (bytes_written < static_cast(stub_contents.size())) { + LOG(ERROR) << "Error writing machine info stub: " + << machine_info_path.value(); + } + } + + parser.GetNameValuePairsFromFile(machine_info_path, kMachineHardwareInfoEq, kMachineHardwareInfoDelim); parser.GetNameValuePairsFromFile(base::FilePath(kEchoCouponFile), @@ -285,17 +307,6 @@ void StatisticsProviderImpl::LoadMachineStatistics(bool load_oem_manifest) { } } - if (!base::SysInfo::IsRunningOnChromeOS() && - machine_info_.find(kSerialNumber) == machine_info_.end()) { - // Set stub value for testing. A time value is appended to avoid clashes of - // the same serial for the same domain, which would invalidate earlier - // enrollments. A fake /tmp/machine-info file should be used instead if - // a stable serial is needed, e.g. to test re-enrollment. - base::TimeDelta time = base::Time::Now() - base::Time::UnixEpoch(); - machine_info_[kSerialNumber] = - "stub_serial_number_" + base::Int64ToString(time.InSeconds()); - } - // Finished loading the statistics. on_statistics_loaded_.Signal(); VLOG(1) << "Finished loading statistics."; -- cgit v1.1