HardwareInterface::IsPartitionUpdateValid: fine grained error
Let the function emit an error code instead of a boolean to indicate
details of the error that is encountered.
For every partition, if downgrade is detected, emit
kPayloadTimestampError. In this case, still check other partitions for
more severe errors before returning this error.
In some cases, e.g. DeltaArchiveManifest carries a version field that is
not a recognized format, or timestamp sysprops in Android is not an
integer, report a more severe error.
If only downgrade errors are encountered, AllowDowngrade() can still
override the result, and proceed with the update; but, AllowDowngrade
cannot override those severe errors.
Test: update_engine_unittest
Bug: 162623577
Bug: 162553432
Change-Id: Ifc2a6fcd66239c755fb4f6528c3d8c6848afcb27
diff --git a/common/fake_hardware.h b/common/fake_hardware.h
index 30c0897..30b5718 100644
--- a/common/fake_hardware.h
+++ b/common/fake_hardware.h
@@ -23,6 +23,7 @@
#include <base/time/time.h>
+#include "update_engine/common/error_code.h"
#include "update_engine/common/hardware_interface.h"
#include "update_engine/common/utils.h"
@@ -216,8 +217,9 @@
void SetVersion(const std::string& partition_name, std::string timestamp) {
partition_timestamps_[partition_name] = std::move(timestamp);
}
- bool IsPartitionUpdateValid(const std::string& partition_name,
- const std::string& new_version) const override {
+ ErrorCode IsPartitionUpdateValid(
+ const std::string& partition_name,
+ const std::string& new_version) const override {
const auto old_version = GetVersionForLogging(partition_name);
return utils::IsTimestampNewer(old_version, new_version);
}
diff --git a/common/hardware_interface.h b/common/hardware_interface.h
index 0fffbfb..b37b007 100644
--- a/common/hardware_interface.h
+++ b/common/hardware_interface.h
@@ -25,6 +25,8 @@
#include <base/files/file_path.h>
#include <base/time/time.h>
+#include "update_engine/common/error_code.h"
+
namespace chromeos_update_engine {
// The hardware interface allows access to the crossystem exposed properties,
@@ -153,8 +155,15 @@
// version number of partition `partition_name`. The notion of
// "newer" is defined by this function. Caller should not make
// any assumption about the underlying logic.
- virtual bool IsPartitionUpdateValid(const std::string& partition_name,
- const std::string& new_version) const = 0;
+ // Return:
+ // - kSuccess if update is valid.
+ // - kPayloadTimestampError if downgrade is detected
+ // - kDownloadManifestParseError if |new_version| has an incorrect format
+ // - Other error values if the source of error is known, or kError for
+ // a generic error on the device.
+ virtual ErrorCode IsPartitionUpdateValid(
+ const std::string& partition_name,
+ const std::string& new_version) const = 0;
};
} // namespace chromeos_update_engine
diff --git a/common/utils.cc b/common/utils.cc
index bbb155f..5d76f3f 100644
--- a/common/utils.cc
+++ b/common/utils.cc
@@ -990,19 +990,26 @@
return true;
}
-bool IsTimestampNewer(const std::string& old_version,
- const std::string& new_version) {
+ErrorCode IsTimestampNewer(const std::string& old_version,
+ const std::string& new_version) {
if (old_version.empty() || new_version.empty()) {
LOG(WARNING)
<< "One of old/new timestamp is empty, permit update anyway. Old: "
<< old_version << " New: " << new_version;
- return true;
+ return ErrorCode::kSuccess;
}
int64_t old_ver = 0;
- TEST_AND_RETURN_FALSE(ParseTimestamp(old_version, &old_ver));
+ if (!ParseTimestamp(old_version, &old_ver)) {
+ return ErrorCode::kError;
+ }
int64_t new_ver = 0;
- TEST_AND_RETURN_FALSE(ParseTimestamp(new_version, &new_ver));
- return old_ver <= new_ver;
+ if (!ParseTimestamp(new_version, &new_ver)) {
+ return ErrorCode::kDownloadManifestParseError;
+ }
+ if (old_ver > new_ver) {
+ return ErrorCode::kPayloadTimestampError;
+ }
+ return ErrorCode::kSuccess;
}
} // namespace utils
diff --git a/common/utils.h b/common/utils.h
index 5dfee3b..0a1dc0c 100644
--- a/common/utils.h
+++ b/common/utils.h
@@ -324,10 +324,15 @@
std::string GetExclusionName(const std::string& str_to_convert);
// Parse `old_version` and `new_version` as integer timestamps and
-// return true if `new_version` is larger/newer.
-// Returns true if either one is empty. Return false if
-bool IsTimestampNewer(const std::string& old_version,
- const std::string& new_version);
+// Return kSuccess if `new_version` is larger/newer.
+// Return kSuccess if either one is empty.
+// Return kError if |old_version| is not empty and not an integer.
+// Return kDownloadManifestParseError if |new_version| is not empty and not an
+// integer.
+// Return kPayloadTimestampError if both are integers but |new_version| <
+// |old_version|.
+ErrorCode IsTimestampNewer(const std::string& old_version,
+ const std::string& new_version);
} // namespace utils
diff --git a/common/utils_unittest.cc b/common/utils_unittest.cc
index 37871d2..d73b3da 100644
--- a/common/utils_unittest.cc
+++ b/common/utils_unittest.cc
@@ -482,11 +482,13 @@
}
TEST(UtilsTest, ValidatePerPartitionTimestamp) {
- ASSERT_FALSE(utils::IsTimestampNewer("10", "5"));
- ASSERT_TRUE(utils::IsTimestampNewer("10", "11"));
- ASSERT_FALSE(utils::IsTimestampNewer("10", "lol"));
- ASSERT_FALSE(utils::IsTimestampNewer("lol", "ZZZ"));
- ASSERT_TRUE(utils::IsTimestampNewer("10", ""));
+ ASSERT_EQ(ErrorCode::kPayloadTimestampError,
+ utils::IsTimestampNewer("10", "5"));
+ ASSERT_EQ(ErrorCode::kSuccess, utils::IsTimestampNewer("10", "11"));
+ ASSERT_EQ(ErrorCode::kDownloadManifestParseError,
+ utils::IsTimestampNewer("10", "lol"));
+ ASSERT_EQ(ErrorCode::kError, utils::IsTimestampNewer("lol", "ZZZ"));
+ ASSERT_EQ(ErrorCode::kSuccess, utils::IsTimestampNewer("10", ""));
}
} // namespace chromeos_update_engine