Commit 87d534dc authored by Trent Begin's avatar Trent Begin Committed by Chromium LUCI CQ

device_status_collector: check if cros_healthd battery value is null

Some devices do not have batteries and will have a null BatteryInfoPtr
to represent this. This value needs to be checked before being used when
processing the CrosHealthdProbe.

Bug: chromium:1159440
Change-Id: I427ba3a8fd9d6a3b26ef0e0e2a21b30d3bdd92a9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2626097
Commit-Queue: Trent Begin <tbegin@chromium.org>
Reviewed-by: default avatarPaul Moy <pmoy@chromium.org>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Cr-Commit-Position: refs/heads/master@{#843149}
parent d829cb6e
......@@ -959,6 +959,10 @@ class DeviceStatusCollectorState : public StatusCollectorState {
case cros_healthd::BatteryResult::Tag::BATTERY_INFO: {
const auto& battery_info = battery_result->get_battery_info();
// Device does not have a battery.
if (battery_info.is_null())
break;
em::PowerStatus* const power_status_out =
response_params_.device_status->mutable_power_status();
em::BatteryInfo* const battery_info_out =
......
......@@ -496,6 +496,11 @@ cros_healthd::BatteryResultPtr CreateBatteryResult() {
cros_healthd::NullableUint64::New(kFakeSmartBatteryTemperature)));
}
cros_healthd::BatteryResultPtr CreateEmptyBatteryResult() {
return cros_healthd::BatteryResult::NewBatteryInfo(
cros_healthd::BatteryInfoPtr());
}
cros_healthd::NonRemovableBlockDeviceResultPtr CreateBlockDeviceResult() {
std::vector<cros_healthd::NonRemovableBlockDeviceInfoPtr> storage_vector;
storage_vector.push_back(cros_healthd::NonRemovableBlockDeviceInfo::New(
......@@ -580,12 +585,23 @@ cros_healthd::BacklightResultPtr CreateBacklightResult() {
std::move(backlight_vector));
}
cros_healthd::BacklightResultPtr CreateEmptyBacklightResult() {
std::vector<cros_healthd::BacklightInfoPtr> backlight_vector;
return cros_healthd::BacklightResult::NewBacklightInfo(
std::move(backlight_vector));
}
cros_healthd::FanResultPtr CreateFanResult() {
std::vector<cros_healthd::FanInfoPtr> fan_vector;
fan_vector.push_back(cros_healthd::FanInfo::New(kFakeSpeedRpm));
return cros_healthd::FanResult::NewFanInfo(std::move(fan_vector));
}
cros_healthd::FanResultPtr CreateEmptyFanResult() {
std::vector<cros_healthd::FanInfoPtr> fan_vector;
return cros_healthd::FanResult::NewFanInfo(std::move(fan_vector));
}
cros_healthd::BluetoothResultPtr CreateBluetoothResult() {
std::vector<cros_healthd::BluetoothAdapterInfoPtr> adapter_info;
adapter_info.push_back(cros_healthd::BluetoothAdapterInfo::New(
......@@ -623,6 +639,15 @@ void GetFakeCrosHealthdBatteryData(
std::move(receiver).Run(fake_info.Clone(), CreateFakeSampleData());
}
// Creates cros_healthd data with the battery category populated with no battery
// info (chromebox).
void GetFakeEmptyCrosHealthdBatteryData(
policy::DeviceStatusCollector::CrosHealthdDataReceiver receiver) {
cros_healthd::TelemetryInfo fake_info;
fake_info.battery_result = CreateEmptyBatteryResult();
std::move(receiver).Run(fake_info.Clone(), CreateFakeSampleData());
}
// Fake cros_healthd fetching function. Returns data with all probe categories
// populated if |mode| is kFull or only the battery category if |mode| is
// kBattery.
......@@ -674,6 +699,29 @@ void FetchFakePartialCrosHealthdData(
}
}
// Fake cros_healthd fetching function. Returns data with only optional probe
// categories populated, if |mode| is kFull or only the battery category if
// |mode| is kBattery.
void FetchFakeOptionalCrosHealthdData(
policy::CrosHealthdCollectionMode mode,
policy::DeviceStatusCollector::CrosHealthdDataReceiver receiver) {
switch (mode) {
case policy::CrosHealthdCollectionMode::kFull: {
cros_healthd::TelemetryInfo fake_info;
fake_info.battery_result = CreateEmptyBatteryResult();
fake_info.backlight_result = CreateEmptyBacklightResult();
fake_info.fan_result = CreateEmptyFanResult();
std::move(receiver).Run(fake_info.Clone(), CreateFakeSampleData());
return;
}
case policy::CrosHealthdCollectionMode::kBattery: {
GetFakeEmptyCrosHealthdBatteryData(std::move(receiver));
return;
}
}
}
void GetEmptyGraphicsStatus(
policy::DeviceStatusCollector::GraphicsStatusReceiver receiver) {
std::move(receiver).Run(em::GraphicsStatus());
......@@ -3314,6 +3362,30 @@ TEST_F(DeviceStatusCollectorTest, TestCrosHealthdInfo) {
EXPECT_EQ(adapter.num_connected_devices(), kFakeNumConnectedBluetoothDevices);
}
TEST_F(DeviceStatusCollectorTest, TestCrosHealthdInfoOptional) {
// Create a fake cros_healthd response with empty optional data from
// cros_healthd.
auto options = CreateEmptyDeviceStatusCollectorOptions();
options->cros_healthd_data_fetcher =
base::BindRepeating(&FetchFakeOptionalCrosHealthdData);
RestartStatusCollector(std::move(options));
scoped_testing_cros_settings_.device_settings()->SetBoolean(
chromeos::kReportDeviceCpuInfo, true);
scoped_testing_cros_settings_.device_settings()->SetBoolean(
chromeos::kReportDevicePowerStatus, true);
GetStatus();
// Verify the battery data is empty
EXPECT_FALSE(device_status_.has_power_status());
// Verify the backlight info is empty.
EXPECT_EQ(device_status_.backlight_info_size(), 0);
// Verify the fan info is empty.
EXPECT_EQ(device_status_.fan_info_size(), 0);
}
TEST_F(DeviceStatusCollectorTest, TestPartialCrosHealthdInfo) {
// Create a fake partial response from cros_healthd and populate it with some
// arbitrary values.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment