Skip to content

Commit

Permalink
Add stub implementations for state keys and VPD
Browse files Browse the repository at this point in the history
Stubbing out state keys and VPD (containing ActivateDate) is required
to test FRE on Linux.

BUG=703658

Review-Url: https://codereview.chromium.org/2774673004
Cr-Commit-Position: refs/heads/master@{#463656}
  • Loading branch information
thiemonagel authored and Commit bot committed Apr 11, 2017
1 parent 0dd660b commit 9157ba4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 17 deletions.
15 changes: 13 additions & 2 deletions chromeos/chromeos_paths.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const base::FilePath::CharType kInstallAttributesFileName[] =
const base::FilePath::CharType kMachineHardwareInfoFileName[] =
FILE_PATH_LITERAL("/tmp/machine-info");

const base::FilePath::CharType kVpdFileName[] =
FILE_PATH_LITERAL("/var/log/vpd_2.0.txt");

const base::FilePath::CharType kUptimeFileName[] =
FILE_PATH_LITERAL("/proc/uptime");

Expand Down Expand Up @@ -73,6 +76,9 @@ bool PathProvider(int key, base::FilePath* result) {
case FILE_MACHINE_INFO:
*result = base::FilePath(kMachineHardwareInfoFileName);
break;
case FILE_VPD:
*result = base::FilePath(kVpdFileName);
break;
case FILE_UPTIME:
*result = base::FilePath(kUptimeFileName);
break;
Expand Down Expand Up @@ -111,8 +117,8 @@ void RegisterPathProvider() {

void RegisterStubPathOverrides(const base::FilePath& stubs_dir) {
CHECK(!base::SysInfo::IsRunningOnChromeOS());
// Override these paths on the desktop, so that enrollment and cloud
// policy work and can be tested.
// Override these paths on the desktop, so that enrollment and cloud policy
// work and can be tested.
base::FilePath parent = base::MakeAbsoluteFilePath(stubs_dir);
PathService::Override(
DIR_USER_POLICY_KEYS,
Expand All @@ -134,6 +140,11 @@ void RegisterStubPathOverrides(const base::FilePath& stubs_dir) {
parent.AppendASCII("stub_machine-info"),
is_absolute,
create);
PathService::OverrideAndCreateIfNeeded(
FILE_VPD,
parent.AppendASCII("stub_vpd"),
is_absolute,
create);
PathService::Override(
DIR_DEVICE_LOCAL_ACCOUNT_EXTENSIONS,
parent.AppendASCII("stub_device_local_account_extensions"));
Expand Down
1 change: 1 addition & 0 deletions chromeos/chromeos_paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ enum {
FILE_OWNER_KEY, // Full path to the owner key file.
FILE_INSTALL_ATTRIBUTES, // Full path to the install attributes file.
FILE_MACHINE_INFO, // Full path to machine hardware info file.
FILE_VPD, // Full path to VPD file.
FILE_UPTIME, // Full path to the file via which the kernel
// exposes the current device uptime.
FILE_UPDATE_REBOOT_NEEDED_UPTIME, // Full path to a file in which Chrome can
Expand Down
47 changes: 41 additions & 6 deletions chromeos/dbus/session_manager_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ constexpr char kArcLowDiskError[] =

constexpr char kStubPolicyFile[] = "stub_policy";
constexpr char kStubDevicePolicyFile[] = "stub_device_policy";
constexpr char kStubStateKeysFile[] = "stub_state_keys";

// Returns a location for |file| that is specific to the given |cryptohome_id|.
// These paths will be relative to DIR_USER_POLICY_KEYS, and can be used only
Expand Down Expand Up @@ -74,6 +75,34 @@ void StoreFile(const base::FilePath& path, const std::string& data) {
}
}

// Helper to asynchronously read (or if missing create) state key stubs.
std::vector<std::string> ReadCreateStateKeysStub(const base::FilePath& path) {
std::string contents;
if (base::PathExists(path)) {
contents = GetFileContent(path);
} else {
// Create stub state keys on the fly.
for (int i = 0; i < 5; ++i) {
contents += crypto::SHA256HashString(
base::IntToString(i) +
base::Int64ToString(base::Time::Now().ToJavaTime()));
}
StoreFile(path, contents);
}

std::vector<std::string> state_keys;
for (size_t i = 0; i < contents.size() / 32; ++i) {
state_keys.push_back(contents.substr(i * 32, 32));
}
return state_keys;
}

// Turn pass-by-value into pass-by-reference as expected by StateKeysCallback.
void RunStateKeysCallbackStub(SessionManagerClient::StateKeysCallback callback,
std::vector<std::string> state_keys) {
callback.Run(state_keys);
}

} // namespace

// The SessionManagerClient implementation used in production.
Expand Down Expand Up @@ -991,12 +1020,18 @@ class SessionManagerClientStubImpl : public SessionManagerClient {
const std::vector<std::string>& flags) override {}

void GetServerBackedStateKeys(const StateKeysCallback& callback) override {
std::vector<std::string> state_keys;
for (int i = 0; i < 5; ++i)
state_keys.push_back(crypto::SHA256HashString(base::IntToString(i)));

if (!callback.is_null())
callback.Run(state_keys);
base::FilePath owner_key_path;
CHECK(PathService::Get(chromeos::FILE_OWNER_KEY, &owner_key_path));
const base::FilePath state_keys_path =
owner_key_path.DirName().AppendASCII(kStubStateKeysFile);
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE,
base::TaskTraits()
.WithShutdownBehavior(
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)
.MayBlock(),
base::Bind(&ReadCreateStateKeysStub, state_keys_path),
base::Bind(&RunStateKeysCallbackStub, callback));
}

void CheckArcAvailability(const ArcCallback& callback) override {
Expand Down
1 change: 1 addition & 0 deletions chromeos/dbus/session_manager_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "base/callback.h"
#include "base/macros.h"
#include "base/observer_list.h"
#include "base/time/time.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/dbus/dbus_client.h"
#include "chromeos/dbus/dbus_client_implementation_type.h"
Expand Down
26 changes: 17 additions & 9 deletions chromeos/system/statistics_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ const char kEchoCouponFile[] = "/var/cache/echo/vpd_echo.txt";
const char kEchoCouponEq[] = "=";
const char kEchoCouponDelim[] = "\n";

// File to get VPD info from, and key/value delimiters of the file.
const char kVpdFile[] = "/var/log/vpd_2.0.txt";
// Key/value delimiters for VPD file.
const char kVpdEq[] = "=";
const char kVpdDelim[] = "\n";

Expand Down Expand Up @@ -471,13 +470,22 @@ void StatisticsProviderImpl::LoadMachineStatistics(bool load_oem_manifest) {
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<int> is fine because stub_contents is small.
int bytes_written = base::WriteFile(
machine_info_path, stub_contents.c_str(), stub_contents.size());
if (bytes_written < static_cast<int>(stub_contents.size())) {
LOG(ERROR) << "Error writing machine info stub: "
<< machine_info_path.value();
PLOG(ERROR) << "Error writing machine info stub "
<< machine_info_path.value();
}
}

base::FilePath vpd_path;
PathService::Get(chromeos::FILE_VPD, &vpd_path);
if (!base::SysInfo::IsRunningOnChromeOS() && !base::PathExists(vpd_path)) {
std::string stub_contents = "\"ActivateDate\"=\"2000-01\"\n";
int bytes_written =
base::WriteFile(vpd_path, stub_contents.c_str(), stub_contents.size());
if (bytes_written < static_cast<int>(stub_contents.size())) {
PLOG(ERROR) << "Error writing vpd stub " << vpd_path.value();
}
}

Expand All @@ -487,7 +495,7 @@ void StatisticsProviderImpl::LoadMachineStatistics(bool load_oem_manifest) {
parser.GetNameValuePairsFromFile(base::FilePath(kEchoCouponFile),
kEchoCouponEq,
kEchoCouponDelim);
parser.GetNameValuePairsFromFile(base::FilePath(kVpdFile),
parser.GetNameValuePairsFromFile(vpd_path,
kVpdEq,
kVpdDelim);

Expand Down

0 comments on commit 9157ba4

Please sign in to comment.