Merge "Use only signed/unsigned numbers with ParseInt/ParseUint respectively"
diff --git a/tests/component/update_verifier_test.cpp b/tests/component/update_verifier_test.cpp
index 543e427..2420c27 100644
--- a/tests/component/update_verifier_test.cpp
+++ b/tests/component/update_verifier_test.cpp
@@ -16,6 +16,7 @@
 
 #include <update_verifier/update_verifier.h>
 
+#include <functional>
 #include <string>
 #include <unordered_map>
 #include <vector>
@@ -37,8 +38,19 @@
     std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", "");
     verity_supported = android::base::EqualsIgnoreCase(verity_mode, "enforcing");
 
+    care_map_prefix_ = care_map_dir_.path + "/care_map"s;
     care_map_pb_ = care_map_dir_.path + "/care_map.pb"s;
     care_map_txt_ = care_map_dir_.path + "/care_map.txt"s;
+    // Overrides the the care_map_prefix.
+    verifier_.set_care_map_prefix(care_map_prefix_);
+
+    property_id_ = "ro.build.fingerprint";
+    fingerprint_ = android::base::GetProperty(property_id_, "");
+    // Overrides the property_reader if we cannot read the given property on the device.
+    if (fingerprint_.empty()) {
+      fingerprint_ = "mock_fingerprint";
+      verifier_.set_property_reader([](const std::string& /* id */) { return "mock_fingerprint"; });
+    }
   }
 
   void TearDown() override {
@@ -58,6 +70,9 @@
       if (partition.find("ranges") != partition.end()) {
         info.set_ranges(partition.at("ranges"));
       }
+      if (partition.find("id") != partition.end()) {
+        info.set_id(partition.at("id"));
+      }
       if (partition.find("fingerprint") != partition.end()) {
         info.set_fingerprint(partition.at("fingerprint"));
       }
@@ -72,13 +87,16 @@
   UpdateVerifier verifier_;
 
   TemporaryDir care_map_dir_;
+  std::string care_map_prefix_;
   std::string care_map_pb_;
   std::string care_map_txt_;
+
+  std::string property_id_;
+  std::string fingerprint_;
 };
 
 TEST_F(UpdateVerifierTest, verify_image_no_care_map) {
-  // Non-existing care_map is allowed.
-  ASSERT_FALSE(verifier_.ParseCareMap("/doesntexist"));
+  ASSERT_FALSE(verifier_.ParseCareMap());
 }
 
 TEST_F(UpdateVerifierTest, verify_image_smoke) {
@@ -90,26 +108,26 @@
 
   std::string content = "system\n2,0,1";
   ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_txt_));
-  ASSERT_TRUE(verifier_.ParseCareMap(care_map_txt_));
+  ASSERT_TRUE(verifier_.ParseCareMap());
   ASSERT_TRUE(verifier_.VerifyPartitions());
 
   // Leading and trailing newlines should be accepted.
   ASSERT_TRUE(android::base::WriteStringToFile("\n" + content + "\n\n", care_map_txt_));
-  ASSERT_TRUE(verifier_.ParseCareMap(care_map_txt_));
+  ASSERT_TRUE(verifier_.ParseCareMap());
   ASSERT_TRUE(verifier_.VerifyPartitions());
 }
 
 TEST_F(UpdateVerifierTest, verify_image_empty_care_map) {
-  ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_));
+  ASSERT_FALSE(verifier_.ParseCareMap());
 }
 
 TEST_F(UpdateVerifierTest, verify_image_wrong_lines) {
   // The care map file can have only 2 / 4 / 6 lines.
   ASSERT_TRUE(android::base::WriteStringToFile("line1", care_map_txt_));
-  ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_));
+  ASSERT_FALSE(verifier_.ParseCareMap());
 
   ASSERT_TRUE(android::base::WriteStringToFile("line1\nline2\nline3", care_map_txt_));
-  ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_));
+  ASSERT_FALSE(verifier_.ParseCareMap());
 }
 
 TEST_F(UpdateVerifierTest, verify_image_malformed_care_map) {
@@ -121,7 +139,7 @@
 
   std::string content = "system\n2,1,0";
   ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_txt_));
-  ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_));
+  ASSERT_FALSE(verifier_.ParseCareMap());
 }
 
 TEST_F(UpdateVerifierTest, verify_image_legacy_care_map) {
@@ -133,7 +151,7 @@
 
   std::string content = "/dev/block/bootdevice/by-name/system\n2,1,0";
   ASSERT_TRUE(android::base::WriteStringToFile(content, care_map_txt_));
-  ASSERT_FALSE(verifier_.ParseCareMap(care_map_txt_));
+  ASSERT_FALSE(verifier_.ParseCareMap());
 }
 
 TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_smoke) {
@@ -144,12 +162,17 @@
   }
 
   std::vector<std::unordered_map<std::string, std::string>> partitions = {
-    { { "name", "system" }, { "ranges", "2,0,1" } },
+    {
+        { "name", "system" },
+        { "ranges", "2,0,1" },
+        { "id", property_id_ },
+        { "fingerprint", fingerprint_ },
+    },
   };
 
   std::string proto = ConstructProto(partitions);
   ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_));
-  ASSERT_TRUE(verifier_.ParseCareMap(care_map_pb_));
+  ASSERT_TRUE(verifier_.ParseCareMap());
   ASSERT_TRUE(verifier_.VerifyPartitions());
 }
 
@@ -161,12 +184,16 @@
   }
 
   std::vector<std::unordered_map<std::string, std::string>> partitions = {
-    { { "ranges", "2,0,1" } },
+    {
+        { "ranges", "2,0,1" },
+        { "id", property_id_ },
+        { "fingerprint", fingerprint_ },
+    },
   };
 
   std::string proto = ConstructProto(partitions);
   ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_));
-  ASSERT_FALSE(verifier_.ParseCareMap(care_map_pb_));
+  ASSERT_FALSE(verifier_.ParseCareMap());
 }
 
 TEST_F(UpdateVerifierTest, verify_image_protobuf_care_map_bad_ranges) {
@@ -177,10 +204,55 @@
   }
 
   std::vector<std::unordered_map<std::string, std::string>> partitions = {
-    { { "name", "system" }, { "ranges", "3,0,1" } },
+    {
+        { "name", "system" },
+        { "ranges", "3,0,1" },
+        { "id", property_id_ },
+        { "fingerprint", fingerprint_ },
+    },
   };
 
   std::string proto = ConstructProto(partitions);
   ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_));
-  ASSERT_FALSE(verifier_.ParseCareMap(care_map_pb_));
+  ASSERT_FALSE(verifier_.ParseCareMap());
+}
+
+TEST_F(UpdateVerifierTest, verify_image_protobuf_empty_fingerprint) {
+  // This test relies on dm-verity support.
+  if (!verity_supported) {
+    GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support.";
+    return;
+  }
+
+  std::vector<std::unordered_map<std::string, std::string>> partitions = {
+    {
+        { "name", "system" },
+        { "ranges", "2,0,1" },
+    },
+  };
+
+  std::string proto = ConstructProto(partitions);
+  ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_));
+  ASSERT_FALSE(verifier_.ParseCareMap());
+}
+
+TEST_F(UpdateVerifierTest, verify_image_protobuf_fingerprint_mismatch) {
+  // This test relies on dm-verity support.
+  if (!verity_supported) {
+    GTEST_LOG_(INFO) << "Test skipped on devices without dm-verity support.";
+    return;
+  }
+
+  std::vector<std::unordered_map<std::string, std::string>> partitions = {
+    {
+        { "name", "system" },
+        { "ranges", "2,0,1" },
+        { "id", property_id_ },
+        { "fingerprint", "unsupported_fingerprint" },
+    },
+  };
+
+  std::string proto = ConstructProto(partitions);
+  ASSERT_TRUE(android::base::WriteStringToFile(proto, care_map_pb_));
+  ASSERT_FALSE(verifier_.ParseCareMap());
 }
diff --git a/update_verifier/include/update_verifier/update_verifier.h b/update_verifier/include/update_verifier/update_verifier.h
index bdfabed..b00890e 100644
--- a/update_verifier/include/update_verifier/update_verifier.h
+++ b/update_verifier/include/update_verifier/update_verifier.h
@@ -16,6 +16,7 @@
 
 #pragma once
 
+#include <functional>
 #include <map>
 #include <string>
 #include <vector>
@@ -37,15 +38,18 @@
 // it should skip the verification to avoid bricking the device.
 class UpdateVerifier {
  public:
+  UpdateVerifier();
+
   // This function tries to process the care_map.pb as protobuf message; and falls back to use
-  // care_map.txt if the pb format file doesn't exist. If the parsing succeeds, put the result of
-  // the pair <partition_name, ranges> into the |partition_map_|.
-  bool ParseCareMap(const std::string& file_name = "");
+  // care_map.txt if the pb format file doesn't exist. If the parsing succeeds, put the result
+  // of the pair <partition_name, ranges> into the |partition_map_|.
+  bool ParseCareMap();
 
   // Verifies the new boot by reading all the cared blocks for partitions in |partition_map_|.
   bool VerifyPartitions();
 
  private:
+  friend class UpdateVerifierTest;
   // Parses the legacy care_map.txt in plain text format.
   bool ParseCareMapPlainText(const std::string& content);
 
@@ -56,5 +60,15 @@
   bool ReadBlocks(const std::string partition_name, const std::string& dm_block_device,
                   const RangeSet& ranges);
 
+  // Functions to override the care_map_prefix_ and property_reader_, used in test only.
+  void set_care_map_prefix(const std::string& prefix);
+  void set_property_reader(const std::function<std::string(const std::string&)>& property_reader);
+
   std::map<std::string, RangeSet> partition_map_;
+  // The path to the care_map excluding the filename extension; default value:
+  // "/data/ota_package/care_map"
+  std::string care_map_prefix_;
+
+  // The function to read the device property; default value: android::base::GetProperty()
+  std::function<std::string(const std::string&)> property_reader_;
 };
diff --git a/update_verifier/update_verifier.cpp b/update_verifier/update_verifier.cpp
index 6e86ac9..d7cd061 100644
--- a/update_verifier/update_verifier.cpp
+++ b/update_verifier/update_verifier.cpp
@@ -65,6 +65,8 @@
 using android::hardware::boot::V1_0::BoolResult;
 using android::hardware::boot::V1_0::CommandResult;
 
+constexpr const char* kDefaultCareMapPrefix = "/data/ota_package/care_map";
+
 // Find directories in format of "/sys/block/dm-X".
 static int dm_name_filter(const dirent* de) {
   if (android::base::StartsWith(de->d_name, "dm-")) {
@@ -73,6 +75,10 @@
   return 0;
 }
 
+UpdateVerifier::UpdateVerifier()
+    : care_map_prefix_(kDefaultCareMapPrefix),
+      property_reader_([](const std::string& id) { return android::base::GetProperty(id, ""); }) {}
+
 // Iterate the content of "/sys/block/dm-X/dm/name" and find all the dm-wrapped block devices.
 // We will later read all the ("cared") blocks from "/dev/block/dm-X" to ensure the target
 // partition's integrity.
@@ -224,18 +230,14 @@
   return true;
 }
 
-bool UpdateVerifier::ParseCareMap(const std::string& file_name) {
+bool UpdateVerifier::ParseCareMap() {
   partition_map_.clear();
 
-  std::string care_map_name = file_name;
-  if (care_map_name.empty()) {
-    std::string care_map_prefix = "/data/ota_package/care_map";
-    care_map_name = care_map_prefix + ".pb";
-    if (access(care_map_name.c_str(), R_OK) == -1) {
-      LOG(WARNING) << care_map_name
-                   << " doesn't exist, falling back to read the care_map in plain text format.";
-      care_map_name = care_map_prefix + ".txt";
-    }
+  std::string care_map_name = care_map_prefix_ + ".pb";
+  if (access(care_map_name.c_str(), R_OK) == -1) {
+    LOG(WARNING) << care_map_name
+                 << " doesn't exist, falling back to read the care_map in plain text format.";
+    care_map_name = care_map_prefix_ + ".txt";
   }
 
   android::base::unique_fd care_map_fd(TEMP_FAILURE_RETRY(open(care_map_name.c_str(), O_RDONLY)));
@@ -283,7 +285,21 @@
       return false;
     }
 
-    // TODO(xunchang) compare the fingerprint of the partition.
+    // Continues to check other partitions if there is a fingerprint mismatch.
+    if (partition.id().empty() || partition.id() == "unknown") {
+      LOG(WARNING) << "Skip reading partition " << partition.name()
+                   << ": property_id is not provided to get fingerprint.";
+      continue;
+    }
+
+    std::string fingerprint = property_reader_(partition.id());
+    if (fingerprint != partition.fingerprint()) {
+      LOG(WARNING) << "Skip reading partition " << partition.name() << ": fingerprint "
+                   << fingerprint << " doesn't match the expected value "
+                   << partition.fingerprint();
+      continue;
+    }
+
     partition_map_.emplace(partition.name(), ranges);
   }
 
@@ -295,6 +311,15 @@
   return true;
 }
 
+void UpdateVerifier::set_care_map_prefix(const std::string& prefix) {
+  care_map_prefix_ = prefix;
+}
+
+void UpdateVerifier::set_property_reader(
+    const std::function<std::string(const std::string&)>& property_reader) {
+  property_reader_ = property_reader;
+}
+
 static int reboot_device() {
   if (android_reboot(ANDROID_RB_RESTART2, 0, nullptr) == -1) {
     LOG(ERROR) << "Failed to reboot.";