idmap2: move Idmap.h to Result

Change the signatures of Idmap::FromApkAssets and
Idmap::FromBinaryStream from

  std::unique_ptr<const Idmap> func(..., std::ostream& out_error);

to

  Result<std::unique_ptr<const Idmap>> func(...);

The returned pointer is still a unique pointer to ensure the dynamically
allocated memory is automatically released when no longer used. This
means that using the returned value of either function requires one of
two patterns:

  const auto idmap = func(...);
  if (!idmap) {
    return Error(...);
  }
  (*idmap)->accept(...);

or

  auto result = func(...);
  if (!result) {
    return Error(...);
  }
  const auto idmap = std::move(*result);
  idmap->accept(...);

Note that in the second example, result must be non-const or
the call to std::move(*result) will not compile.

With this change, the entire idmap2 project has been converted to use
Result.

Test: make idmap2_tests
Change-Id: I533f4e03b99645523d94dd5f446ad76fb435f661
diff --git a/cmds/idmap2/idmap2/Create.cpp b/cmds/idmap2/idmap2/Create.cpp
index fdbb210..47617e0 100644
--- a/cmds/idmap2/idmap2/Create.cpp
+++ b/cmds/idmap2/idmap2/Create.cpp
@@ -98,12 +98,10 @@
     return Error("failed to load apk %s", overlay_apk_path.c_str());
   }
 
-  std::stringstream stream;
-  const std::unique_ptr<const Idmap> idmap =
-      Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk,
-                           fulfilled_policies, !ignore_overlayable, stream);
+  const auto idmap = Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path,
+                                          *overlay_apk, fulfilled_policies, !ignore_overlayable);
   if (!idmap) {
-    return Error("failed to create idmap: %s", stream.str().c_str());
+    return Error(idmap.GetError(), "failed to create idmap");
   }
 
   umask(kIdmapFilePermissionMask);
@@ -112,7 +110,7 @@
     return Error("failed to open idmap path %s", idmap_path.c_str());
   }
   BinaryStreamVisitor visitor(fout);
-  idmap->accept(&visitor);
+  (*idmap)->accept(&visitor);
   fout.close();
   if (fout.fail()) {
     return Error("failed to write to idmap path %s", idmap_path.c_str());
diff --git a/cmds/idmap2/idmap2/Dump.cpp b/cmds/idmap2/idmap2/Dump.cpp
index fd58222..8716bf3 100644
--- a/cmds/idmap2/idmap2/Dump.cpp
+++ b/cmds/idmap2/idmap2/Dump.cpp
@@ -49,20 +49,19 @@
   if (!opts_ok) {
     return opts_ok.GetError();
   }
-  std::stringstream stream;
   std::ifstream fin(idmap_path);
-  const std::unique_ptr<const Idmap> idmap = Idmap::FromBinaryStream(fin, stream);
+  const auto idmap = Idmap::FromBinaryStream(fin);
   fin.close();
   if (!idmap) {
-    return Error("failed to load idmap: %s", stream.str().c_str());
+    return Error(idmap.GetError(), "failed to load idmap");
   }
 
   if (verbose) {
     RawPrintVisitor visitor(std::cout);
-    idmap->accept(&visitor);
+    (*idmap)->accept(&visitor);
   } else {
     PrettyPrintVisitor visitor(std::cout);
-    idmap->accept(&visitor);
+    (*idmap)->accept(&visitor);
   }
 
   return Unit{};
diff --git a/cmds/idmap2/idmap2d/Idmap2Service.cpp b/cmds/idmap2/idmap2d/Idmap2Service.cpp
index e03a9cc..4f65379 100644
--- a/cmds/idmap2/idmap2d/Idmap2Service.cpp
+++ b/cmds/idmap2/idmap2d/Idmap2Service.cpp
@@ -138,12 +138,10 @@
     return error("failed to load apk " + overlay_apk_path);
   }
 
-  std::stringstream err;
-  const std::unique_ptr<const Idmap> idmap =
-      Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk,
-                           policy_bitmask, enforce_overlayable, err);
+  const auto idmap = Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path,
+                                          *overlay_apk, policy_bitmask, enforce_overlayable);
   if (!idmap) {
-    return error(err.str());
+    return error(idmap.GetErrorMessage());
   }
 
   umask(kIdmapFilePermissionMask);
@@ -152,7 +150,7 @@
     return error("failed to open idmap path " + idmap_path);
   }
   BinaryStreamVisitor visitor(fout);
-  idmap->accept(&visitor);
+  (*idmap)->accept(&visitor);
   fout.close();
   if (fout.fail()) {
     return error("failed to write to idmap path " + idmap_path);
diff --git a/cmds/idmap2/include/idmap2/Idmap.h b/cmds/idmap2/include/idmap2/Idmap.h
index 673d18d..5cc0664b 100644
--- a/cmds/idmap2/include/idmap2/Idmap.h
+++ b/cmds/idmap2/include/idmap2/Idmap.h
@@ -228,17 +228,18 @@
   static std::string CanonicalIdmapPathFor(const std::string& absolute_dir,
                                            const std::string& absolute_apk_path);
 
-  static std::unique_ptr<const Idmap> FromBinaryStream(std::istream& stream,
-                                                       std::ostream& out_error);
+  static Result<std::unique_ptr<const Idmap>> FromBinaryStream(std::istream& stream);
 
   // In the current version of idmap, the first package in each resources.arsc
   // file is used; change this in the next version of idmap to use a named
   // package instead; also update FromApkAssets to take additional parameters:
   // the target and overlay package names
-  static std::unique_ptr<const Idmap> FromApkAssets(
-      const std::string& target_apk_path, const ApkAssets& target_apk_assets,
-      const std::string& overlay_apk_path, const ApkAssets& overlay_apk_assets,
-      const PolicyBitmask& fulfilled_policies, bool enforce_overlayable, std::ostream& out_error);
+  static Result<std::unique_ptr<const Idmap>> FromApkAssets(const std::string& target_apk_path,
+                                                            const ApkAssets& target_apk_assets,
+                                                            const std::string& overlay_apk_path,
+                                                            const ApkAssets& overlay_apk_assets,
+                                                            const PolicyBitmask& fulfilled_policies,
+                                                            bool enforce_overlayable);
 
   inline const std::unique_ptr<const IdmapHeader>& GetHeader() const {
     return header_;
diff --git a/cmds/idmap2/libidmap2/Idmap.cpp b/cmds/idmap2/libidmap2/Idmap.cpp
index 9afdd437..486f831 100644
--- a/cmds/idmap2/libidmap2/Idmap.cpp
+++ b/cmds/idmap2/libidmap2/Idmap.cpp
@@ -124,7 +124,7 @@
   const Result<uint32_t> b = zip.Crc("AndroidManifest.xml");
   return a && b
              ? Result<uint32_t>(*a ^ *b)
-             : Error("Couldn't get CRC for \"%s\"", a ? "AndroidManifest.xml" : "resources.arsc");
+             : Error("failed to get CRC for \"%s\"", a ? "AndroidManifest.xml" : "resources.arsc");
 }
 
 }  // namespace
@@ -244,15 +244,13 @@
   return absolute_dir + "/" + copy + "@idmap";
 }
 
-std::unique_ptr<const Idmap> Idmap::FromBinaryStream(std::istream& stream,
-                                                     std::ostream& out_error) {
+Result<std::unique_ptr<const Idmap>> Idmap::FromBinaryStream(std::istream& stream) {
   SYSTRACE << "Idmap::FromBinaryStream";
   std::unique_ptr<Idmap> idmap(new Idmap());
 
   idmap->header_ = IdmapHeader::FromBinaryStream(stream);
   if (!idmap->header_) {
-    out_error << "error: failed to parse idmap header" << std::endl;
-    return nullptr;
+    return Error("failed to parse idmap header");
   }
 
   // idmap version 0x01 does not specify the number of data blocks that follow
@@ -260,13 +258,12 @@
   for (int i = 0; i < 1; i++) {
     std::unique_ptr<const IdmapData> data = IdmapData::FromBinaryStream(stream);
     if (!data) {
-      out_error << "error: failed to parse data block " << i << std::endl;
-      return nullptr;
+      return Error("failed to parse data block %d", i);
     }
     idmap->data_.push_back(std::move(data));
   }
 
-  return std::move(idmap);
+  return {std::move(idmap)};
 }
 
 std::string ConcatPolicies(const std::vector<std::string>& policies) {
@@ -323,63 +320,56 @@
   return Result<Unit>({});
 }
 
-std::unique_ptr<const Idmap> Idmap::FromApkAssets(
-    const std::string& target_apk_path, const ApkAssets& target_apk_assets,
-    const std::string& overlay_apk_path, const ApkAssets& overlay_apk_assets,
-    const PolicyBitmask& fulfilled_policies, bool enforce_overlayable, std::ostream& out_error) {
+Result<std::unique_ptr<const Idmap>> Idmap::FromApkAssets(const std::string& target_apk_path,
+                                                          const ApkAssets& target_apk_assets,
+                                                          const std::string& overlay_apk_path,
+                                                          const ApkAssets& overlay_apk_assets,
+                                                          const PolicyBitmask& fulfilled_policies,
+                                                          bool enforce_overlayable) {
   SYSTRACE << "Idmap::FromApkAssets";
   AssetManager2 target_asset_manager;
   if (!target_asset_manager.SetApkAssets({&target_apk_assets}, true, false)) {
-    out_error << "error: failed to create target asset manager" << std::endl;
-    return nullptr;
+    return Error("failed to create target asset manager");
   }
 
   AssetManager2 overlay_asset_manager;
   if (!overlay_asset_manager.SetApkAssets({&overlay_apk_assets}, true, false)) {
-    out_error << "error: failed to create overlay asset manager" << std::endl;
-    return nullptr;
+    return Error("failed to create overlay asset manager");
   }
 
   const LoadedArsc* target_arsc = target_apk_assets.GetLoadedArsc();
   if (target_arsc == nullptr) {
-    out_error << "error: failed to load target resources.arsc" << std::endl;
-    return nullptr;
+    return Error("failed to load target resources.arsc");
   }
 
   const LoadedArsc* overlay_arsc = overlay_apk_assets.GetLoadedArsc();
   if (overlay_arsc == nullptr) {
-    out_error << "error: failed to load overlay resources.arsc" << std::endl;
-    return nullptr;
+    return Error("failed to load overlay resources.arsc");
   }
 
   const LoadedPackage* target_pkg = GetPackageAtIndex0(*target_arsc);
   if (target_pkg == nullptr) {
-    out_error << "error: failed to load target package from resources.arsc" << std::endl;
-    return nullptr;
+    return Error("failed to load target package from resources.arsc");
   }
 
   const LoadedPackage* overlay_pkg = GetPackageAtIndex0(*overlay_arsc);
   if (overlay_pkg == nullptr) {
-    out_error << "error: failed to load overlay package from resources.arsc" << std::endl;
-    return nullptr;
+    return Error("failed to load overlay package from resources.arsc");
   }
 
   const std::unique_ptr<const ZipFile> target_zip = ZipFile::Open(target_apk_path);
   if (!target_zip) {
-    out_error << "error: failed to open target as zip" << std::endl;
-    return nullptr;
+    return Error("failed to open target as zip");
   }
 
   const std::unique_ptr<const ZipFile> overlay_zip = ZipFile::Open(overlay_apk_path);
   if (!overlay_zip) {
-    out_error << "error: failed to open overlay as zip" << std::endl;
-    return nullptr;
+    return Error("failed to open overlay as zip");
   }
 
   auto overlay_info = utils::ExtractOverlayManifestInfo(overlay_apk_path);
   if (!overlay_info) {
-    out_error << "error: " << overlay_info.GetErrorMessage() << std::endl;
-    return nullptr;
+    return overlay_info.GetError();
   }
 
   std::unique_ptr<IdmapHeader> header(new IdmapHeader());
@@ -388,30 +378,26 @@
 
   Result<uint32_t> crc = GetCrc(*target_zip);
   if (!crc) {
-    out_error << "error: failed to get zip crc for target" << std::endl;
-    return nullptr;
+    return Error(crc.GetError(), "failed to get zip CRC for target");
   }
   header->target_crc_ = *crc;
 
   crc = GetCrc(*overlay_zip);
   if (!crc) {
-    out_error << "error: failed to get zip crc for overlay" << std::endl;
-    return nullptr;
+    return Error(crc.GetError(), "failed to get zip CRC for overlay");
   }
   header->overlay_crc_ = *crc;
 
   if (target_apk_path.size() > sizeof(header->target_path_)) {
-    out_error << "error: target apk path \"" << target_apk_path << "\" longer that maximum size "
-              << sizeof(header->target_path_) << std::endl;
-    return nullptr;
+    return Error("target apk path \"%s\" longer than maximum size %zu", target_apk_path.c_str(),
+                 sizeof(header->target_path_));
   }
   memset(header->target_path_, 0, sizeof(header->target_path_));
   memcpy(header->target_path_, target_apk_path.data(), target_apk_path.size());
 
   if (overlay_apk_path.size() > sizeof(header->overlay_path_)) {
-    out_error << "error: overlay apk path \"" << overlay_apk_path << "\" longer that maximum size "
-              << sizeof(header->overlay_path_) << std::endl;
-    return nullptr;
+    return Error("overlay apk path \"%s\" longer than maximum size %zu", target_apk_path.c_str(),
+                 sizeof(header->target_path_));
   }
   memset(header->overlay_path_, 0, sizeof(header->overlay_path_));
   memcpy(header->overlay_path_, overlay_apk_path.data(), overlay_apk_path.size());
@@ -451,9 +437,8 @@
   }
 
   if (matching_resources.Map().empty()) {
-    out_error << "overlay \"" << overlay_apk_path << "\" does not successfully overlay any resource"
-              << std::endl;
-    return nullptr;
+    return Error("overlay \"%s\" does not successfully overlay any resource",
+                 overlay_apk_path.c_str());
   }
 
   // encode idmap data
@@ -484,7 +469,7 @@
 
   idmap->data_.push_back(std::move(data));
 
-  return std::move(idmap);
+  return {std::move(idmap)};
 }
 
 void IdmapHeader::accept(Visitor* v) const {
diff --git a/cmds/idmap2/tests/BinaryStreamVisitorTests.cpp b/cmds/idmap2/tests/BinaryStreamVisitorTests.cpp
index 9a0412e..9a5b633 100644
--- a/cmds/idmap2/tests/BinaryStreamVisitorTests.cpp
+++ b/cmds/idmap2/tests/BinaryStreamVisitorTests.cpp
@@ -17,6 +17,7 @@
 #include <memory>
 #include <sstream>
 #include <string>
+#include <utility>
 
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
@@ -37,16 +38,17 @@
   std::string raw(reinterpret_cast<const char*>(idmap_raw_data), idmap_raw_data_len);
   std::istringstream raw_stream(raw);
 
-  std::stringstream error;
-  std::unique_ptr<const Idmap> idmap1 = Idmap::FromBinaryStream(raw_stream, error);
-  ASSERT_THAT(idmap1, NotNull());
+  auto result1 = Idmap::FromBinaryStream(raw_stream);
+  ASSERT_TRUE(result1);
+  const auto idmap1 = std::move(*result1);
 
   std::stringstream stream;
   BinaryStreamVisitor visitor(stream);
   idmap1->accept(&visitor);
 
-  std::unique_ptr<const Idmap> idmap2 = Idmap::FromBinaryStream(stream, error);
-  ASSERT_THAT(idmap2, NotNull());
+  auto result2 = Idmap::FromBinaryStream(stream);
+  ASSERT_TRUE(result2);
+  const auto idmap2 = std::move(*result2);
 
   ASSERT_EQ(idmap1->GetHeader()->GetTargetCrc(), idmap2->GetHeader()->GetTargetCrc());
   ASSERT_EQ(idmap1->GetHeader()->GetTargetPath(), idmap2->GetHeader()->GetTargetPath());
@@ -76,15 +78,14 @@
   std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
   ASSERT_THAT(overlay_apk, NotNull());
 
-  std::stringstream error;
-  std::unique_ptr<const Idmap> idmap =
+  const auto idmap =
       Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk,
-                           PolicyFlags::POLICY_PUBLIC, /* enforce_overlayable */ true, error);
-  ASSERT_THAT(idmap, NotNull());
+                           PolicyFlags::POLICY_PUBLIC, /* enforce_overlayable */ true);
+  ASSERT_TRUE(idmap);
 
   std::stringstream stream;
   BinaryStreamVisitor visitor(stream);
-  idmap->accept(&visitor);
+  (*idmap)->accept(&visitor);
   const std::string str = stream.str();
   const StringPiece data(str);
   std::unique_ptr<const LoadedIdmap> loaded_idmap = LoadedIdmap::Load(data);
diff --git a/cmds/idmap2/tests/Idmap2BinaryTests.cpp b/cmds/idmap2/tests/Idmap2BinaryTests.cpp
index a6a2ada..91bc4dd 100644
--- a/cmds/idmap2/tests/Idmap2BinaryTests.cpp
+++ b/cmds/idmap2/tests/Idmap2BinaryTests.cpp
@@ -100,13 +100,12 @@
   struct stat st;
   ASSERT_EQ(stat(GetIdmapPath().c_str(), &st), 0);
 
-  std::stringstream error;
   std::ifstream fin(GetIdmapPath());
-  std::unique_ptr<const Idmap> idmap = Idmap::FromBinaryStream(fin, error);
+  const auto idmap = Idmap::FromBinaryStream(fin);
   fin.close();
 
-  ASSERT_THAT(idmap, NotNull());
-  ASSERT_IDMAP(*idmap, GetTargetApkPath(), GetOverlayApkPath());
+  ASSERT_TRUE(idmap);
+  ASSERT_IDMAP(**idmap, GetTargetApkPath(), GetOverlayApkPath());
 
   unlink(GetIdmapPath().c_str());
 }
@@ -193,24 +192,23 @@
   expected << idmap_static_2_path << std::endl;
   ASSERT_EQ(result->stdout, expected.str());
 
-  std::stringstream error;
   auto idmap_static_no_name_raw_string = utils::ReadFile(idmap_static_no_name_path);
   auto idmap_static_no_name_raw_stream = std::istringstream(*idmap_static_no_name_raw_string);
-  auto idmap_static_no_name = Idmap::FromBinaryStream(idmap_static_no_name_raw_stream, error);
-  ASSERT_THAT(idmap_static_no_name, NotNull());
-  ASSERT_IDMAP(*idmap_static_no_name, GetTargetApkPath(), overlay_static_no_name_apk_path);
+  auto idmap_static_no_name = Idmap::FromBinaryStream(idmap_static_no_name_raw_stream);
+  ASSERT_TRUE(idmap_static_no_name);
+  ASSERT_IDMAP(**idmap_static_no_name, GetTargetApkPath(), overlay_static_no_name_apk_path);
 
   auto idmap_static_1_raw_string = utils::ReadFile(idmap_static_1_path);
   auto idmap_static_1_raw_stream = std::istringstream(*idmap_static_1_raw_string);
-  auto idmap_static_1 = Idmap::FromBinaryStream(idmap_static_1_raw_stream, error);
-  ASSERT_THAT(idmap_static_1, NotNull());
-  ASSERT_IDMAP(*idmap_static_1, GetTargetApkPath(), overlay_static_1_apk_path);
+  auto idmap_static_1 = Idmap::FromBinaryStream(idmap_static_1_raw_stream);
+  ASSERT_TRUE(idmap_static_1);
+  ASSERT_IDMAP(**idmap_static_1, GetTargetApkPath(), overlay_static_1_apk_path);
 
   auto idmap_static_2_raw_string = utils::ReadFile(idmap_static_2_path);
   auto idmap_static_2_raw_stream = std::istringstream(*idmap_static_2_raw_string);
-  auto idmap_static_2 = Idmap::FromBinaryStream(idmap_static_2_raw_stream, error);
-  ASSERT_THAT(idmap_static_2, NotNull());
-  ASSERT_IDMAP(*idmap_static_2, GetTargetApkPath(), overlay_static_2_apk_path);
+  auto idmap_static_2 = Idmap::FromBinaryStream(idmap_static_2_raw_stream);
+  ASSERT_TRUE(idmap_static_2);
+  ASSERT_IDMAP(**idmap_static_2, GetTargetApkPath(), overlay_static_2_apk_path);
 
   unlink(idmap_static_no_name_path.c_str());
   unlink(idmap_static_2_path.c_str());
diff --git a/cmds/idmap2/tests/IdmapTests.cpp b/cmds/idmap2/tests/IdmapTests.cpp
index c20ae7b..621f503 100644
--- a/cmds/idmap2/tests/IdmapTests.cpp
+++ b/cmds/idmap2/tests/IdmapTests.cpp
@@ -20,6 +20,7 @@
 #include <memory>
 #include <sstream>
 #include <string>
+#include <utility>
 #include <vector>
 
 #include "gmock/gmock.h"
@@ -127,9 +128,9 @@
   std::string raw(reinterpret_cast<const char*>(idmap_raw_data), idmap_raw_data_len);
   std::istringstream stream(raw);
 
-  std::stringstream error;
-  std::unique_ptr<const Idmap> idmap = Idmap::FromBinaryStream(stream, error);
-  ASSERT_THAT(idmap, NotNull());
+  auto result = Idmap::FromBinaryStream(stream);
+  ASSERT_TRUE(result);
+  const auto idmap = std::move(*result);
 
   ASSERT_THAT(idmap->GetHeader(), NotNull());
   ASSERT_EQ(idmap->GetHeader()->GetMagic(), 0x504d4449U);
@@ -168,9 +169,8 @@
                   10);  // data too small
   std::istringstream stream(raw);
 
-  std::stringstream error;
-  std::unique_ptr<const Idmap> idmap = Idmap::FromBinaryStream(stream, error);
-  ASSERT_THAT(idmap, IsNull());
+  const auto result = Idmap::FromBinaryStream(stream);
+  ASSERT_FALSE(result);
 }
 
 void CreateIdmap(const StringPiece& target_apk_path, const StringPiece& overlay_apk_path,
@@ -182,10 +182,10 @@
   std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path.to_string());
   ASSERT_THAT(overlay_apk, NotNull());
 
-  std::stringstream error;
-  *out_idmap =
+  auto result =
       Idmap::FromApkAssets(target_apk_path.to_string(), *target_apk, overlay_apk_path.to_string(),
-                           *overlay_apk, fulfilled_policies, enforce_overlayable, error);
+                           *overlay_apk, fulfilled_policies, enforce_overlayable);
+  *out_idmap = result ? std::move(*result) : nullptr;
 }
 
 TEST(IdmapTests, CreateIdmapFromApkAssets) {
@@ -471,11 +471,10 @@
   std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
   ASSERT_THAT(overlay_apk, NotNull());
 
-  std::stringstream error;
-  std::unique_ptr<const Idmap> idmap =
+  const auto result =
       Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk,
-                           PolicyFlags::POLICY_PUBLIC, /* enforce_overlayable */ true, error);
-  ASSERT_THAT(idmap, IsNull());
+                           PolicyFlags::POLICY_PUBLIC, /* enforce_overlayable */ true);
+  ASSERT_FALSE(result);
 }
 
 TEST(IdmapTests, IdmapHeaderIsUpToDate) {
@@ -489,11 +488,11 @@
   std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
   ASSERT_THAT(overlay_apk, NotNull());
 
-  std::stringstream error;
-  std::unique_ptr<const Idmap> idmap = Idmap::FromApkAssets(
-      target_apk_path, *target_apk, overlay_apk_path, *overlay_apk, PolicyFlags::POLICY_PUBLIC,
-      /* enforce_overlayable */ true, error);
-  ASSERT_THAT(idmap, NotNull());
+  auto result = Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk,
+                                     PolicyFlags::POLICY_PUBLIC,
+                                     /* enforce_overlayable */ true);
+  ASSERT_TRUE(result);
+  const auto idmap = std::move(*result);
 
   std::stringstream stream;
   BinaryStreamVisitor visitor(stream);
@@ -609,13 +608,12 @@
   std::string raw(reinterpret_cast<const char*>(idmap_raw_data), idmap_raw_data_len);
   std::istringstream stream(raw);
 
-  std::stringstream error;
-  std::unique_ptr<const Idmap> idmap = Idmap::FromBinaryStream(stream, error);
-  ASSERT_THAT(idmap, NotNull());
+  const auto idmap = Idmap::FromBinaryStream(stream);
+  ASSERT_TRUE(idmap);
 
   std::stringstream test_stream;
   TestVisitor visitor(test_stream);
-  idmap->accept(&visitor);
+  (*idmap)->accept(&visitor);
 
   ASSERT_EQ(test_stream.str(),
             "TestVisitor::visit(Idmap)\n"
diff --git a/cmds/idmap2/tests/PrettyPrintVisitorTests.cpp b/cmds/idmap2/tests/PrettyPrintVisitorTests.cpp
index eaa47cd..27a3880 100644
--- a/cmds/idmap2/tests/PrettyPrintVisitorTests.cpp
+++ b/cmds/idmap2/tests/PrettyPrintVisitorTests.cpp
@@ -46,15 +46,14 @@
   std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
   ASSERT_THAT(overlay_apk, NotNull());
 
-  std::stringstream error;
-  std::unique_ptr<const Idmap> idmap =
+  const auto idmap =
       Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk,
-                           PolicyFlags::POLICY_PUBLIC, /* enforce_overlayable */ true, error);
-  ASSERT_THAT(idmap, NotNull());
+                           PolicyFlags::POLICY_PUBLIC, /* enforce_overlayable */ true);
+  ASSERT_TRUE(idmap);
 
   std::stringstream stream;
   PrettyPrintVisitor visitor(stream);
-  idmap->accept(&visitor);
+  (*idmap)->accept(&visitor);
 
   ASSERT_NE(stream.str().find("target apk path  : "), std::string::npos);
   ASSERT_NE(stream.str().find("overlay apk path : "), std::string::npos);
@@ -67,13 +66,12 @@
   std::string raw(reinterpret_cast<const char*>(idmap_raw_data), idmap_raw_data_len);
   std::istringstream raw_stream(raw);
 
-  std::stringstream error;
-  std::unique_ptr<const Idmap> idmap = Idmap::FromBinaryStream(raw_stream, error);
-  ASSERT_THAT(idmap, NotNull());
+  const auto idmap = Idmap::FromBinaryStream(raw_stream);
+  ASSERT_TRUE(idmap);
 
   std::stringstream stream;
   PrettyPrintVisitor visitor(stream);
-  idmap->accept(&visitor);
+  (*idmap)->accept(&visitor);
 
   ASSERT_NE(stream.str().find("target apk path  : "), std::string::npos);
   ASSERT_NE(stream.str().find("overlay apk path : "), std::string::npos);
diff --git a/cmds/idmap2/tests/RawPrintVisitorTests.cpp b/cmds/idmap2/tests/RawPrintVisitorTests.cpp
index 7ec13ed..7372148 100644
--- a/cmds/idmap2/tests/RawPrintVisitorTests.cpp
+++ b/cmds/idmap2/tests/RawPrintVisitorTests.cpp
@@ -40,15 +40,14 @@
   std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
   ASSERT_THAT(overlay_apk, NotNull());
 
-  std::stringstream error;
-  std::unique_ptr<const Idmap> idmap =
+  const auto idmap =
       Idmap::FromApkAssets(target_apk_path, *target_apk, overlay_apk_path, *overlay_apk,
-                           PolicyFlags::POLICY_PUBLIC, /* enforce_overlayable */ true, error);
-  ASSERT_THAT(idmap, NotNull());
+                           PolicyFlags::POLICY_PUBLIC, /* enforce_overlayable */ true);
+  ASSERT_TRUE(idmap);
 
   std::stringstream stream;
   RawPrintVisitor visitor(stream);
-  idmap->accept(&visitor);
+  (*idmap)->accept(&visitor);
 
   ASSERT_NE(stream.str().find("00000000: 504d4449  magic\n"), std::string::npos);
   ASSERT_NE(stream.str().find("00000004: 00000001  version\n"), std::string::npos);
@@ -64,13 +63,12 @@
   std::string raw(reinterpret_cast<const char*>(idmap_raw_data), idmap_raw_data_len);
   std::istringstream raw_stream(raw);
 
-  std::stringstream error;
-  std::unique_ptr<const Idmap> idmap = Idmap::FromBinaryStream(raw_stream, error);
-  ASSERT_THAT(idmap, NotNull());
+  const auto idmap = Idmap::FromBinaryStream(raw_stream);
+  ASSERT_TRUE(idmap);
 
   std::stringstream stream;
   RawPrintVisitor visitor(stream);
-  idmap->accept(&visitor);
+  (*idmap)->accept(&visitor);
 
   ASSERT_NE(stream.str().find("00000000: 504d4449  magic\n"), std::string::npos);
   ASSERT_NE(stream.str().find("00000004: 00000001  version\n"), std::string::npos);