libbrillo: Add getting device market segment policy
Created a DeviceMarketSegment enum in device_policy.h which will be
used in update_engine and translate the value from MarketSegment which
in components/policy/proto/device_management_backend.proto
BUG=chromium:1192372
TEST=FEATURES=test emerge-${BOARD} libbrillo
Change-Id: Idee3aa2ed4dd568b2e4880878ac2168997c1967e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2800566
Tested-by: Eric Wang <qianwan@google.com>
Commit-Queue: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Hidehiko Abe <hidehiko@chromium.org>
diff --git a/libbrillo/policy/device_policy.h b/libbrillo/policy/device_policy.h
index 8d03334..76abb51 100644
--- a/libbrillo/policy/device_policy.h
+++ b/libbrillo/policy/device_policy.h
@@ -27,6 +27,7 @@
// This class defines the interface for querying device policy on ChromeOS.
// The implementation is hidden in DevicePolicyImpl to prevent protobuf
// definition from leaking into the libraries using this interface.
+// TODO(b:184745765) Refactor the Getters return bool type and pointer style
class DevicePolicy {
public:
// Identifiers of a USB device or device family.
@@ -63,6 +64,14 @@
int percentage;
};
+ // Device Market Segment enum which is translated from MarketSegment in
+ // components/policy/proto/device_management_backend.proto.
+ enum class DeviceMarketSegment {
+ kUnknown = 0,
+ kEducation,
+ kEnterprise,
+ };
+
DevicePolicy();
DevicePolicy(const DevicePolicy&) = delete;
DevicePolicy& operator=(const DevicePolicy&) = delete;
@@ -264,6 +273,13 @@
virtual bool GetHighestDeviceMinimumVersion(
base::Version* versions_out) const = 0;
+ // Write the value of the DeviceMarketSegment policy in
+ // |device_market_segment|. Returns true on success. If the proto value is
+ // not set, then return false.
+ // Translated value from MarketSegment in device_management_backend.proto
+ virtual bool GetDeviceMarketSegment(
+ DeviceMarketSegment* device_market_segment) const = 0;
+
private:
// Verifies that the policy signature is correct.
virtual bool VerifyPolicySignature() = 0;
diff --git a/libbrillo/policy/device_policy_impl.cc b/libbrillo/policy/device_policy_impl.cc
index b30b154..71a4ad7 100644
--- a/libbrillo/policy/device_policy_impl.cc
+++ b/libbrillo/policy/device_policy_impl.cc
@@ -824,6 +824,31 @@
return true;
}
+bool DevicePolicyImpl::GetDeviceMarketSegment(
+ DeviceMarketSegment* device_market_segment) const {
+ if (!policy_data_.has_market_segment()) {
+ return false;
+ }
+
+ em::PolicyData::MarketSegment market_segment = policy_data_.market_segment();
+ switch (market_segment) {
+ case em::PolicyData::MARKET_SEGMENT_UNSPECIFIED:
+ *device_market_segment = DeviceMarketSegment::kUnknown;
+ break;
+ case em::PolicyData::ENROLLED_EDUCATION:
+ *device_market_segment = DeviceMarketSegment::kEducation;
+ break;
+ case em::PolicyData::ENROLLED_ENTERPRISE:
+ *device_market_segment = DeviceMarketSegment::kEnterprise;
+ break;
+ default:
+ LOG(ERROR) << "MarketSegment enum value has changed!";
+ *device_market_segment = DeviceMarketSegment::kUnknown;
+ }
+
+ return true;
+}
+
bool DevicePolicyImpl::VerifyPolicyFile(const base::FilePath& policy_path) {
if (!verify_root_ownership_) {
return true;
diff --git a/libbrillo/policy/device_policy_impl.h b/libbrillo/policy/device_policy_impl.h
index 3dd59e9..388396c 100644
--- a/libbrillo/policy/device_policy_impl.h
+++ b/libbrillo/policy/device_policy_impl.h
@@ -95,6 +95,8 @@
int* channel_downgrade_behavior_out) const override;
bool GetHighestDeviceMinimumVersion(
base::Version* versions_out) const override;
+ bool GetDeviceMarketSegment(
+ DeviceMarketSegment* device_market_segment) const override;
// Methods that can be used only for testing.
void set_policy_data_for_testing(
diff --git a/libbrillo/policy/mock_device_policy.h b/libbrillo/policy/mock_device_policy.h
index bdae2c1..b8841b6 100644
--- a/libbrillo/policy/mock_device_policy.h
+++ b/libbrillo/policy/mock_device_policy.h
@@ -122,6 +122,10 @@
GetHighestDeviceMinimumVersion,
(base::Version*),
(const, override));
+ MOCK_METHOD(bool,
+ GetDeviceMarketSegment,
+ (DeviceMarketSegment*),
+ (const, override));
};
} // namespace policy
diff --git a/libbrillo/policy/tests/device_policy_impl_test.cc b/libbrillo/policy/tests/device_policy_impl_test.cc
index bee46b8..09794a5 100644
--- a/libbrillo/policy/tests/device_policy_impl_test.cc
+++ b/libbrillo/policy/tests/device_policy_impl_test.cc
@@ -456,4 +456,33 @@
ASSERT_FALSE(device_policy_.GetHighestDeviceMinimumVersion(&version));
}
+// Should only write a value and return true as the
+// |device_market_segment| should be present.
+TEST_F(DevicePolicyImplTest, GetDeviceMarketSegment_EducationDevice) {
+ em::PolicyData policy_data;
+ policy_data.set_market_segment(em::PolicyData::ENROLLED_EDUCATION);
+ device_policy_.set_policy_data_for_testing(policy_data);
+
+ DeviceMarketSegment segment;
+ EXPECT_TRUE(device_policy_.GetDeviceMarketSegment(&segment));
+ EXPECT_EQ(segment, DeviceMarketSegment::kEducation);
+}
+
+TEST_F(DevicePolicyImplTest, GetDeviceMarketSegment_UnspecifiedDevice) {
+ em::PolicyData policy_data;
+ policy_data.set_market_segment(em::PolicyData::MARKET_SEGMENT_UNSPECIFIED);
+ device_policy_.set_policy_data_for_testing(policy_data);
+
+ DeviceMarketSegment segment;
+ EXPECT_TRUE(device_policy_.GetDeviceMarketSegment(&segment));
+ EXPECT_EQ(segment, DeviceMarketSegment::kUnknown);
+}
+
+TEST_F(DevicePolicyImplTest, GetDeviceMarketSegment_NotSet) {
+ em::PolicyData policy_data;
+ device_policy_.set_policy_data_for_testing(policy_data);
+
+ DeviceMarketSegment segment;
+ EXPECT_FALSE(device_policy_.GetDeviceMarketSegment(&segment));
+}
} // namespace policy