Allow for RRO internal referencing

This change allows RROs to reference their own internal resources as
expected.

Overlays are loaded as shared libraries so they can have their own
resource id space that does not conflict with the resource id space of
the target or other overlays.

References to overlay resources that override target resources now
appear as references to the target resources.

Overlay values that are inlined into the xml file specified using
android:overlayResources are now able to be used at runtime.

See go/rro-references for more information.

Bug: 135943783
Test: idmap2_tests
Test: libandroidfw_tests
Change-Id: Ie349c56d7fd3f7d94b7d595ed6d01dc6b59b6178
diff --git a/cmds/idmap2/tests/BinaryStreamVisitorTests.cpp b/cmds/idmap2/tests/BinaryStreamVisitorTests.cpp
index 3a01e8f..db4778c 100644
--- a/cmds/idmap2/tests/BinaryStreamVisitorTests.cpp
+++ b/cmds/idmap2/tests/BinaryStreamVisitorTests.cpp
@@ -88,8 +88,4 @@
   ASSERT_EQ(overlay_entries1[2].target_id, overlay_entries2[2].target_id);
 }
 
-TEST(BinaryStreamVisitorTests, CreateIdmapFromApkAssetsInteropWithLoadedIdmap) {
-  // TODO(135943783): Removed temporarily until libandroidfw idmap loading is fixed.
-}
-
 }  // namespace android::idmap2
diff --git a/cmds/idmap2/tests/Idmap2BinaryTests.cpp b/cmds/idmap2/tests/Idmap2BinaryTests.cpp
index b1685b7..b535f30 100644
--- a/cmds/idmap2/tests/Idmap2BinaryTests.cpp
+++ b/cmds/idmap2/tests/Idmap2BinaryTests.cpp
@@ -280,7 +280,54 @@
 }
 
 TEST_F(Idmap2BinaryTests, Lookup) {
-  // TODO(135943783): Removed temporarily until libandroidfw idmap loading is fixed.
+  SKIP_TEST_IF_CANT_EXEC_IDMAP2;
+
+  // clang-format off
+  auto result = ExecuteBinary({"idmap2",
+                               "create",
+                               "--target-apk-path", GetTargetApkPath(),
+                               "--overlay-apk-path", GetOverlayApkPath(),
+                               "--idmap-path", GetIdmapPath()});
+  // clang-format on
+  ASSERT_THAT(result, NotNull());
+  ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
+
+  // clang-format off
+  result = ExecuteBinary({"idmap2",
+                          "lookup",
+                          "--idmap-path", GetIdmapPath(),
+                          "--config", "",
+                          "--resid", "0x7f02000c"});  // string/str1
+  // clang-format on
+  ASSERT_THAT(result, NotNull());
+  ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
+  ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos);
+  ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos);
+
+  // clang-format off
+  result = ExecuteBinary({"idmap2",
+                          "lookup",
+                          "--idmap-path", GetIdmapPath(),
+                          "--config", "",
+                          "--resid", "test.target:string/str1"});
+  // clang-format on
+  ASSERT_THAT(result, NotNull());
+  ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
+  ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos);
+  ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos);
+
+  // clang-format off
+  result = ExecuteBinary({"idmap2",
+                          "lookup",
+                          "--idmap-path", GetIdmapPath(),
+                          "--config", "sv",
+                          "--resid", "test.target:string/str1"});
+  // clang-format on
+  ASSERT_THAT(result, NotNull());
+  ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
+  ASSERT_NE(result->stdout.find("overlay-1-sv"), std::string::npos);
+
+  unlink(GetIdmapPath().c_str());
 }
 
 TEST_F(Idmap2BinaryTests, InvalidCommandLineOptions) {
diff --git a/cmds/idmap2/tests/IdmapTests.cpp b/cmds/idmap2/tests/IdmapTests.cpp
index 30b1372..cd816dd 100644
--- a/cmds/idmap2/tests/IdmapTests.cpp
+++ b/cmds/idmap2/tests/IdmapTests.cpp
@@ -248,7 +248,7 @@
   OverlayManifestInfo info{};
   info.target_package = "test.target";
   info.target_name = "TestResources";
-  info.resource_mapping = 0x7f030002;  // xml/overlays_different_packages
+  info.resource_mapping = 0x7f030001;  // xml/overlays_different_packages
   auto idmap_data = TestIdmapDataFromApkAssets("/target/target.apk", "/overlay/overlay.apk", info,
                                                PolicyFlags::POLICY_PUBLIC,
                                                /* enforce_overlayable */ false);
@@ -272,7 +272,7 @@
   OverlayManifestInfo info{};
   info.target_package = "test.target";
   info.target_name = "TestResources";
-  info.resource_mapping = 0x7f030000;  // xml/overlays_inline
+  info.resource_mapping = 0x7f030002;  // xml/overlays_inline
   auto idmap_data = TestIdmapDataFromApkAssets("/target/target.apk", "/overlay/overlay.apk", info,
                                                PolicyFlags::POLICY_PUBLIC,
                                                /* enforce_overlayable */ false);
diff --git a/cmds/idmap2/tests/RawPrintVisitorTests.cpp b/cmds/idmap2/tests/RawPrintVisitorTests.cpp
index 24f9845d..d387880 100644
--- a/cmds/idmap2/tests/RawPrintVisitorTests.cpp
+++ b/cmds/idmap2/tests/RawPrintVisitorTests.cpp
@@ -49,7 +49,7 @@
   ASSERT_NE(stream.str().find("00000000: 504d4449  magic\n"), std::string::npos);
   ASSERT_NE(stream.str().find("00000004: 00000002  version\n"), std::string::npos);
   ASSERT_NE(stream.str().find("00000008: 76a20829  target crc\n"), std::string::npos);
-  ASSERT_NE(stream.str().find("0000000c: e3c188b6  overlay crc\n"), std::string::npos);
+  ASSERT_NE(stream.str().find("0000000c: c054fb26  overlay crc\n"), std::string::npos);
   ASSERT_NE(stream.str().find("00000210:       7f  target package id\n"), std::string::npos);
   ASSERT_NE(stream.str().find("00000211:       7f  overlay package id\n"), std::string::npos);
   ASSERT_NE(stream.str().find("00000212: 00000004  target entry count\n"), std::string::npos);