Convert per state thresholds read from XML configs.

Per-state thresholds defined in the XML configs are represented in MiB.
Convert these threholds to bytes before updating the in-memory cache.

Test: atest libwatchdog_test
Bug: 186445673

Change-Id: I431fe6b704472b945e617e0aa4cb6c365a1bcd08
diff --git a/cpp/watchdog/server/src/OveruseConfigurationXmlHelper.cpp b/cpp/watchdog/server/src/OveruseConfigurationXmlHelper.cpp
index f76bcc5..f3d0f04 100644
--- a/cpp/watchdog/server/src/OveruseConfigurationXmlHelper.cpp
+++ b/cpp/watchdog/server/src/OveruseConfigurationXmlHelper.cpp
@@ -233,22 +233,23 @@
         if (seenStates.find(state) != seenStates.end()) {
             return Error() << "Duplicate threshold specified for state '" << state << "'";
         }
-        int64_t bytes = 0;
+        int64_t megaBytes = 0;
         if (const auto text = childElement->GetText(); text == nullptr) {
             return Error() << "Must specify non-empty threshold for state '" << state << "'";
-        } else if (const auto bytesStr = Trim(text); !ParseInt(bytesStr.c_str(), &bytes)) {
+        } else if (const auto megaBytesStr = Trim(text);
+                   !ParseInt(megaBytesStr.c_str(), &megaBytes)) {
             return Error() << "Failed to parse threshold for the state '" << state
-                           << "': Received threshold value '" << bytesStr << "'";
+                           << "': Received threshold value '" << megaBytesStr << "'";
         }
         if (!strcmp(state, kStateIdForegroundMode)) {
             seenStates.insert(kStateIdForegroundMode);
-            perStateBytes.foregroundBytes = bytes;
+            perStateBytes.foregroundBytes = megaBytes * kOneMegaByte;
         } else if (!strcmp(state, kStateIdBackgroundMode)) {
             seenStates.insert(kStateIdBackgroundMode);
-            perStateBytes.backgroundBytes = bytes;
+            perStateBytes.backgroundBytes = megaBytes * kOneMegaByte;
         } else if (!strcmp(state, kStateIdGarageMode)) {
             seenStates.insert(kStateIdGarageMode);
-            perStateBytes.garageModeBytes = bytes;
+            perStateBytes.garageModeBytes = megaBytes * kOneMegaByte;
         } else {
             return Error() << "Invalid state '" << state << "' in per-state bytes";
         }
diff --git a/cpp/watchdog/server/src/OveruseConfigurationXmlHelper.h b/cpp/watchdog/server/src/OveruseConfigurationXmlHelper.h
index 9f501c4..382c4f5 100644
--- a/cpp/watchdog/server/src/OveruseConfigurationXmlHelper.h
+++ b/cpp/watchdog/server/src/OveruseConfigurationXmlHelper.h
@@ -25,6 +25,8 @@
 namespace automotive {
 namespace watchdog {
 
+constexpr int64_t kOneMegaByte = 1024 * 1024;
+
 class OveruseConfigurationXmlHelper : public android::RefBase {
 public:
     static android::base::Result<
diff --git a/cpp/watchdog/server/tests/OveruseConfigurationXmlHelperTest.cpp b/cpp/watchdog/server/tests/OveruseConfigurationXmlHelperTest.cpp
index 2595c2b..88006d7 100644
--- a/cpp/watchdog/server/tests/OveruseConfigurationXmlHelperTest.cpp
+++ b/cpp/watchdog/server/tests/OveruseConfigurationXmlHelperTest.cpp
@@ -72,10 +72,14 @@
 
 TEST(OveruseConfigurationXmlHelperTest, TestValidSystemConfiguration) {
     auto ioConfig = constructIoOveruseConfig(
-            /*componentLevel=*/toPerStateIoOveruseThreshold(ComponentType::SYSTEM, 300, 150, 500),
+            /*componentLevel=*/toPerStateIoOveruseThreshold(ComponentType::SYSTEM,
+                                                            300 * kOneMegaByte, 150 * kOneMegaByte,
+                                                            500 * kOneMegaByte),
             /*packageSpecific=*/
-            {toPerStateIoOveruseThreshold("system.package.C", 400, 100, 200),
-             toPerStateIoOveruseThreshold("system.package.D", 1024, 500, 2048)},
+            {toPerStateIoOveruseThreshold("system.package.C", 400 * kOneMegaByte,
+                                          100 * kOneMegaByte, 200 * kOneMegaByte),
+             toPerStateIoOveruseThreshold("system.package.D", 1024 * kOneMegaByte,
+                                          500 * kOneMegaByte, 2048 * kOneMegaByte)},
             /*categorySpecific=*/{},
             /*systemWide=*/{toIoOveruseAlertThreshold(10, 200), toIoOveruseAlertThreshold(5, 50)});
     ResourceOveruseConfiguration expected =
@@ -97,13 +101,19 @@
 
 TEST(OveruseConfigurationXmlHelperTest, TestValidVendorConfiguration) {
     auto ioConfig = constructIoOveruseConfig(
-            /*componentLevel=*/toPerStateIoOveruseThreshold(ComponentType::VENDOR, 1024, 512, 3072),
+            /*componentLevel=*/toPerStateIoOveruseThreshold(ComponentType::VENDOR,
+                                                            1024 * kOneMegaByte, 512 * kOneMegaByte,
+                                                            3072 * kOneMegaByte),
             /*packageSpecific=*/
-            {toPerStateIoOveruseThreshold("com.vendor.package.C", 400, 100, 200),
-             toPerStateIoOveruseThreshold("com.vendor.package.D", 1024, 500, 2048)},
+            {toPerStateIoOveruseThreshold("com.vendor.package.C", 400 * kOneMegaByte,
+                                          100 * kOneMegaByte, 200 * kOneMegaByte),
+             toPerStateIoOveruseThreshold("com.vendor.package.D", 1024 * kOneMegaByte,
+                                          500 * kOneMegaByte, 2048 * kOneMegaByte)},
             /*categorySpecific=*/
-            {toPerStateIoOveruseThreshold("MAPS", 800, 900, 2048),
-             toPerStateIoOveruseThreshold("MEDIA", 600, 700, 1024)},
+            {toPerStateIoOveruseThreshold("MAPS", 800 * kOneMegaByte, 900 * kOneMegaByte,
+                                          2048 * kOneMegaByte),
+             toPerStateIoOveruseThreshold("MEDIA", 600 * kOneMegaByte, 700 * kOneMegaByte,
+                                          1024 * kOneMegaByte)},
             /*systemWide=*/{});
     ResourceOveruseConfiguration expected =
             constructResourceOveruseConfig(ComponentType::VENDOR,
@@ -129,8 +139,9 @@
 
 TEST(OveruseConfigurationXmlHelperTest, TestValidThirdPartyConfiguration) {
     auto ioConfig = constructIoOveruseConfig(
-            /*componentLevel=*/toPerStateIoOveruseThreshold(ComponentType::THIRD_PARTY, 300, 150,
-                                                            500),
+            /*componentLevel=*/toPerStateIoOveruseThreshold(ComponentType::THIRD_PARTY,
+                                                            300 * kOneMegaByte, 150 * kOneMegaByte,
+                                                            500 * kOneMegaByte),
             /*packageSpecific=*/{},
             /*categorySpecific=*/{},
             /*systemWide=*/{});
diff --git a/cpp/watchdog/server/tests/data/valid_overuse_third_party_configuration.xml b/cpp/watchdog/server/tests/data/valid_overuse_third_party_configuration.xml
index b3bacb6..f12a47c 100644
--- a/cpp/watchdog/server/tests/data/valid_overuse_third_party_configuration.xml
+++ b/cpp/watchdog/server/tests/data/valid_overuse_third_party_configuration.xml
@@ -24,4 +24,4 @@
       <state id="garage_mode"> 500 </state>
     </componentLevelThresholds>
   </ioOveruseConfiguration>
-</resourceOveruseConfiguration>
\ No newline at end of file
+</resourceOveruseConfiguration>