• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <android-base/file.h>
18 
19 #include <cstdio>  // fclose
20 #include <fstream>
21 #include <memory>
22 #include <sstream>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 #include "R.h"
28 #include "TestConstants.h"
29 #include "TestHelpers.h"
30 #include "android-base/macros.h"
31 #include "androidfw/ApkAssets.h"
32 #include "androidfw/ResourceUtils.h"
33 #include "gmock/gmock.h"
34 #include "gtest/gtest.h"
35 #include "idmap2/BinaryStreamVisitor.h"
36 #include "idmap2/CommandLineOptions.h"
37 #include "idmap2/Idmap.h"
38 #include "idmap2/LogInfo.h"
39 
40 using ::testing::NotNull;
41 
42 using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
43 
44 namespace android::idmap2 {
45 
46 #define ASSERT_TARGET_ENTRY(entry, target_resid, overlay_resid) \
47   ASSERT_EQ((entry).target_id, (target_resid));                 \
48   ASSERT_EQ((entry).overlay_id, (overlay_resid))
49 
50 #define ASSERT_TARGET_INLINE_ENTRY(entry, target_resid, ex_config, expected_type, expected_value) \
51   ASSERT_EQ((entry).target_id, target_resid);                                                     \
52   ASSERT_EQ((entry).values.begin()->first.to_string(), (ex_config));                              \
53   ASSERT_EQ((entry).values.begin()->second.data_type, (expected_type));                           \
54   ASSERT_EQ((entry).values.begin()->second.data_value, (expected_value))
55 
56 #define ASSERT_OVERLAY_ENTRY(entry, overlay_resid, target_resid) \
57   ASSERT_EQ((entry).overlay_id, (overlay_resid));                \
58   ASSERT_EQ((entry).target_id, (target_resid))
59 
TEST(IdmapTests,TestCanonicalIdmapPathFor)60 TEST(IdmapTests, TestCanonicalIdmapPathFor) {
61   ASSERT_EQ(Idmap::CanonicalIdmapPathFor("/foo", "/vendor/overlay/bar.apk"),
62             "/foo/vendor@overlay@bar.apk@idmap");
63 }
64 
TEST(IdmapTests,CreateIdmapHeaderFromBinaryStream)65 TEST(IdmapTests, CreateIdmapHeaderFromBinaryStream) {
66   std::string raw(reinterpret_cast<const char*>(kIdmapRawData), kIdmapRawDataLen);
67   std::istringstream stream(raw);
68   std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(stream);
69   ASSERT_THAT(header, NotNull());
70   ASSERT_EQ(header->GetMagic(), 0x504d4449U);
71   ASSERT_EQ(header->GetVersion(), 0x09U);
72   ASSERT_EQ(header->GetTargetCrc(), 0x1234U);
73   ASSERT_EQ(header->GetOverlayCrc(), 0x5678U);
74   ASSERT_EQ(header->GetFulfilledPolicies(), 0x11);
75   ASSERT_EQ(header->GetEnforceOverlayable(), true);
76   ASSERT_EQ(header->GetTargetPath(), "targetX.apk");
77   ASSERT_EQ(header->GetOverlayPath(), "overlayX.apk");
78   ASSERT_EQ(header->GetDebugInfo(), "debug");
79 }
80 
TEST(IdmapTests,IdmapFailParsingDifferentVersion)81 TEST(IdmapTests, IdmapFailParsingDifferentVersion) {
82   constexpr size_t kJunkSize = 2000;
83   std::stringstream stream;
84   stream << android::kIdmapMagic;
85   stream << 0xffffffffU;
86   stream << std::string(kJunkSize, static_cast<char>(0xffU));
87   ASSERT_FALSE(Idmap::FromBinaryStream(stream));
88 }
89 
TEST(IdmapTests,IdmapFailParsingDifferentMagic)90 TEST(IdmapTests, IdmapFailParsingDifferentMagic) {
91   constexpr size_t kJunkSize = 2000;
92   std::stringstream stream;
93   stream << 0xffffffffU;
94   stream << android::kIdmapCurrentVersion;
95   stream << std::string(kJunkSize, static_cast<char>(0xffU));
96   ASSERT_FALSE(Idmap::FromBinaryStream(stream));
97 }
98 
TEST(IdmapTests,CreateIdmapDataHeaderFromBinaryStream)99 TEST(IdmapTests, CreateIdmapDataHeaderFromBinaryStream) {
100   const size_t offset = kIdmapRawDataOffset;
101   std::string raw(reinterpret_cast<const char*>(kIdmapRawData + offset), kIdmapRawDataLen - offset);
102   std::istringstream stream(raw);
103 
104   std::unique_ptr<const IdmapData::Header> header = IdmapData::Header::FromBinaryStream(stream);
105   ASSERT_THAT(header, NotNull());
106   ASSERT_EQ(header->GetTargetEntryCount(), 0x03);
107   ASSERT_EQ(header->GetOverlayEntryCount(), 0x03);
108 }
109 
TEST(IdmapTests,CreateIdmapDataFromBinaryStream)110 TEST(IdmapTests, CreateIdmapDataFromBinaryStream) {
111   const size_t offset = kIdmapRawDataOffset;
112   std::string raw(reinterpret_cast<const char*>(kIdmapRawData + offset), kIdmapRawDataLen - offset);
113   std::istringstream stream(raw);
114 
115   std::unique_ptr<const IdmapData> data = IdmapData::FromBinaryStream(stream);
116   ASSERT_THAT(data, NotNull());
117 
118   const auto& target_entries = data->GetTargetEntries();
119   ASSERT_EQ(target_entries.size(), 3U);
120   ASSERT_TARGET_ENTRY(target_entries[0], 0x7f020000, 0x7f020000);
121   ASSERT_TARGET_ENTRY(target_entries[1], 0x7f030000, 0x7f030000);
122   ASSERT_TARGET_ENTRY(target_entries[2], 0x7f030002, 0x7f030001);
123 
124   const auto& target_inline_entries = data->GetTargetInlineEntries();
125   ASSERT_EQ(target_inline_entries.size(), 1U);
126   ASSERT_TARGET_INLINE_ENTRY(target_inline_entries[0], 0x7f040000,  "land-xxhdpi-v7",
127                              Res_value::TYPE_INT_HEX, 0x12345678);
128 
129   const auto& overlay_entries = data->GetOverlayEntries();
130   ASSERT_EQ(target_entries.size(), 3U);
131   ASSERT_OVERLAY_ENTRY(overlay_entries[0], 0x7f020000, 0x7f020000);
132   ASSERT_OVERLAY_ENTRY(overlay_entries[1], 0x7f030000, 0x7f030000);
133   ASSERT_OVERLAY_ENTRY(overlay_entries[2], 0x7f030001, 0x7f030002);
134 }
135 
TEST(IdmapTests,CreateIdmapFromBinaryStream)136 TEST(IdmapTests, CreateIdmapFromBinaryStream) {
137   std::string raw(reinterpret_cast<const char*>(kIdmapRawData), kIdmapRawDataLen);
138   std::istringstream stream(raw);
139 
140   auto result = Idmap::FromBinaryStream(stream);
141   ASSERT_TRUE(result);
142   const auto idmap = std::move(*result);
143 
144   ASSERT_THAT(idmap->GetHeader(), NotNull());
145   ASSERT_EQ(idmap->GetHeader()->GetMagic(), 0x504d4449U);
146   ASSERT_EQ(idmap->GetHeader()->GetVersion(), 0x09U);
147   ASSERT_EQ(idmap->GetHeader()->GetTargetCrc(), 0x1234U);
148   ASSERT_EQ(idmap->GetHeader()->GetOverlayCrc(), 0x5678U);
149   ASSERT_EQ(idmap->GetHeader()->GetFulfilledPolicies(), kIdmapRawDataPolicies);
150   ASSERT_EQ(idmap->GetHeader()->GetEnforceOverlayable(), true);
151   ASSERT_EQ(idmap->GetHeader()->GetTargetPath(), kIdmapRawTargetPath);
152   ASSERT_EQ(idmap->GetHeader()->GetOverlayPath(), kIdmapRawOverlayPath);
153   ASSERT_EQ(idmap->GetHeader()->GetOverlayName(), kIdmapRawOverlayName);
154 
155   const std::vector<std::unique_ptr<const IdmapData>>& dataBlocks = idmap->GetData();
156   ASSERT_EQ(dataBlocks.size(), 1U);
157 
158   const std::unique_ptr<const IdmapData>& data = dataBlocks[0];
159   ASSERT_THAT(data, NotNull());
160 
161   const auto& target_entries = data->GetTargetEntries();
162   ASSERT_EQ(target_entries.size(), 3U);
163   ASSERT_TARGET_ENTRY(target_entries[0], 0x7f020000, 0x7f020000);
164   ASSERT_TARGET_ENTRY(target_entries[1], 0x7f030000, 0x7f030000);
165   ASSERT_TARGET_ENTRY(target_entries[2], 0x7f030002, 0x7f030001);
166 
167   const auto& target_inline_entries = data->GetTargetInlineEntries();
168   ASSERT_EQ(target_inline_entries.size(), 1U);
169   ASSERT_TARGET_INLINE_ENTRY(target_inline_entries[0], 0x7f040000,  "land-xxhdpi-v7",
170                              Res_value::TYPE_INT_HEX, 0x12345678);
171 
172   const auto& overlay_entries = data->GetOverlayEntries();
173   ASSERT_EQ(target_entries.size(), 3U);
174   ASSERT_OVERLAY_ENTRY(overlay_entries[0], 0x7f020000, 0x7f020000);
175   ASSERT_OVERLAY_ENTRY(overlay_entries[1], 0x7f030000, 0x7f030000);
176   ASSERT_OVERLAY_ENTRY(overlay_entries[2], 0x7f030001, 0x7f030002);
177 }
178 
TEST(IdmapTests,GracefullyFailToCreateIdmapFromCorruptBinaryStream)179 TEST(IdmapTests, GracefullyFailToCreateIdmapFromCorruptBinaryStream) {
180   std::string raw(reinterpret_cast<const char*>(kIdmapRawData),
181                   10);  // data too small
182   std::istringstream stream(raw);
183 
184   const auto result = Idmap::FromBinaryStream(stream);
185   ASSERT_FALSE(result);
186 }
187 
TEST(IdmapTests,CreateIdmapHeaderFromApkAssets)188 TEST(IdmapTests, CreateIdmapHeaderFromApkAssets) {
189   std::string target_apk_path = GetTestDataPath() + "/target/target.apk";
190   std::string overlay_apk_path = GetTestDataPath() + "/overlay/overlay.apk";
191 
192   auto target = TargetResourceContainer::FromPath(target_apk_path);
193   ASSERT_TRUE(target);
194 
195   auto overlay = OverlayResourceContainer::FromPath(overlay_apk_path);
196   ASSERT_TRUE(overlay);
197 
198   auto idmap_result = Idmap::FromContainers(
199       **target, **overlay, TestConstants::OVERLAY_NAME_ALL_POLICIES, PolicyFlags::PUBLIC,
200       /* enforce_overlayable */ true);
201   ASSERT_TRUE(idmap_result) << idmap_result.GetErrorMessage();
202   auto& idmap = *idmap_result;
203   ASSERT_THAT(idmap, NotNull());
204 
205   ASSERT_THAT(idmap->GetHeader(), NotNull());
206   ASSERT_EQ(idmap->GetHeader()->GetMagic(), 0x504d4449U);
207   ASSERT_EQ(idmap->GetHeader()->GetVersion(), 0x09U);
208   ASSERT_EQ(idmap->GetHeader()->GetTargetCrc(), android::idmap2::TestConstants::TARGET_CRC);
209   ASSERT_EQ(idmap->GetHeader()->GetOverlayCrc(), android::idmap2::TestConstants::OVERLAY_CRC);
210   ASSERT_EQ(idmap->GetHeader()->GetFulfilledPolicies(), PolicyFlags::PUBLIC);
211   ASSERT_EQ(idmap->GetHeader()->GetEnforceOverlayable(), true);
212   ASSERT_EQ(idmap->GetHeader()->GetTargetPath(), target_apk_path);
213   ASSERT_EQ(idmap->GetHeader()->GetOverlayPath(), overlay_apk_path);
214   ASSERT_EQ(idmap->GetHeader()->GetOverlayName(), TestConstants::OVERLAY_NAME_ALL_POLICIES);
215 }
216 
TEST(IdmapTests,CreateIdmapDataFromApkAssets)217 TEST(IdmapTests, CreateIdmapDataFromApkAssets) {
218   std::string target_apk_path = GetTestDataPath() + "/target/target.apk";
219   std::string overlay_apk_path = GetTestDataPath() + "/overlay/overlay.apk";
220 
221   auto target = TargetResourceContainer::FromPath(target_apk_path);
222   ASSERT_TRUE(target);
223 
224   auto overlay = OverlayResourceContainer::FromPath(overlay_apk_path);
225   ASSERT_TRUE(overlay);
226 
227   auto idmap_result = Idmap::FromContainers(
228       **target, **overlay, TestConstants::OVERLAY_NAME_DEFAULT, PolicyFlags::PUBLIC,
229       /* enforce_overlayable */ true);
230   ASSERT_TRUE(idmap_result) << idmap_result.GetErrorMessage();
231   auto& idmap = *idmap_result;
232   ASSERT_THAT(idmap, NotNull());
233 
234   const std::vector<std::unique_ptr<const IdmapData>>& dataBlocks = idmap->GetData();
235   ASSERT_EQ(dataBlocks.size(), 1U);
236 
237   const std::unique_ptr<const IdmapData>& data = dataBlocks[0];
238   ASSERT_THAT(data, NotNull());
239 
240   const auto& target_entries = data->GetTargetEntries();
241   ASSERT_EQ(target_entries.size(), 4U);
242   ASSERT_TARGET_ENTRY(target_entries[0], R::target::integer::int1, R::overlay::integer::int1);
243   ASSERT_TARGET_ENTRY(target_entries[1], R::target::string::str1, R::overlay::string::str1);
244   ASSERT_TARGET_ENTRY(target_entries[2], R::target::string::str3, R::overlay::string::str3);
245   ASSERT_TARGET_ENTRY(target_entries[3], R::target::string::str4, R::overlay::string::str4);
246 
247   const auto& target_inline_entries = data->GetTargetInlineEntries();
248   ASSERT_EQ(target_inline_entries.size(), 0U);
249 
250   const auto& overlay_entries = data->GetOverlayEntries();
251   ASSERT_EQ(target_entries.size(), 4U);
252   ASSERT_OVERLAY_ENTRY(overlay_entries[0], R::overlay::integer::int1, R::target::integer::int1);
253   ASSERT_OVERLAY_ENTRY(overlay_entries[1], R::overlay::string::str1, R::target::string::str1);
254   ASSERT_OVERLAY_ENTRY(overlay_entries[2], R::overlay::string::str3, R::target::string::str3);
255   ASSERT_OVERLAY_ENTRY(overlay_entries[3], R::overlay::string::str4, R::target::string::str4);
256 }
257 
TEST(IdmapTests,FabricatedOverlay)258 TEST(IdmapTests, FabricatedOverlay) {
259   std::string target_apk_path = GetTestDataPath() + "/target/target.apk";
260   auto target = TargetResourceContainer::FromPath(target_apk_path);
261   ASSERT_TRUE(target);
262 
263   auto path = GetTestDataPath() + "/overlay/res/drawable/android.png";
264   auto fd = android::base::unique_fd(::open(path.c_str(), O_RDONLY | O_CLOEXEC));
265   ASSERT_TRUE(fd > 0) << "errno " << errno << " for path " << path;
266 
267   auto frro = FabricatedOverlay::Builder("com.example.overlay", "SandTheme", "test.target")
268                   .SetOverlayable("TestResources")
269                   .SetResourceValue("integer/int1", Res_value::TYPE_INT_DEC, 2U, "land-xxhdpi-v7")
270                   .SetResourceValue("string/str1", Res_value::TYPE_REFERENCE, 0x7f010000, "land")
271                   .SetResourceValue("string/str2", Res_value::TYPE_STRING, "foobar", "xxhdpi-v7")
272                   .SetResourceValue("drawable/dr1", fd, "port-xxhdpi-v7")
273                   .setFrroPath("/foo/bar/biz.frro")
274                   .Build();
275 
276   ASSERT_TRUE(frro);
277   TemporaryFile tf;
278   std::ofstream out(tf.path);
279   ASSERT_TRUE((*frro).ToBinaryStream(out));
280   out.close();
281 
282   auto overlay = OverlayResourceContainer::FromPath(tf.path);
283   ASSERT_TRUE(overlay);
284 
285   auto idmap_result = Idmap::FromContainers(**target, **overlay, "SandTheme", PolicyFlags::PUBLIC,
286                                             /* enforce_overlayable */ true);
287   ASSERT_TRUE(idmap_result) << idmap_result.GetErrorMessage();
288   auto& idmap = *idmap_result;
289   ASSERT_THAT(idmap, NotNull());
290 
291   const std::vector<std::unique_ptr<const IdmapData>>& dataBlocks = idmap->GetData();
292   ASSERT_EQ(dataBlocks.size(), 1U);
293 
294   const std::unique_ptr<const IdmapData>& data = dataBlocks[0];
295   ASSERT_THAT(data, NotNull());
296   ASSERT_EQ(data->GetTargetEntries().size(), 0U);
297   ASSERT_EQ(data->GetOverlayEntries().size(), 0U);
298 
299   auto string_pool_data = data->GetStringPoolData();
300   auto string_pool = ResStringPool(string_pool_data.data(), string_pool_data.size(), false);
301 
302   std::u16string expected_uri = u"frro://foo/bar/biz.frro?offset=16&size=8341";
303   uint32_t uri_index
304       = string_pool.indexOfString(expected_uri.data(), expected_uri.length()).value_or(-1);
305 
306   const auto& target_inline_entries = data->GetTargetInlineEntries();
307   ASSERT_EQ(target_inline_entries.size(), 4U);
308   ASSERT_TARGET_INLINE_ENTRY(target_inline_entries[0], R::target::drawable::dr1, "port-xxhdpi-v7",
309                              Res_value::TYPE_STRING, uri_index);
310   ASSERT_TARGET_INLINE_ENTRY(target_inline_entries[1], R::target::integer::int1, "land-xxhdpi-v7",
311                              Res_value::TYPE_INT_DEC, 2U);
312   ASSERT_TARGET_INLINE_ENTRY(target_inline_entries[2], R::target::string::str1, "land",
313                              Res_value::TYPE_REFERENCE, 0x7f010000);
314   ASSERT_TARGET_INLINE_ENTRY(target_inline_entries[3], R::target::string::str2, "xxhdpi-v7",
315                              Res_value::TYPE_STRING,
316                              (uint32_t) (string_pool.indexOfString(u"foobar", 6)).value_or(-1));
317 }
318 
TEST(IdmapTests,FailCreateIdmapInvalidName)319 TEST(IdmapTests, FailCreateIdmapInvalidName) {
320   std::string target_apk_path = GetTestDataPath() + "/target/target.apk";
321   std::string overlay_apk_path = GetTestDataPath() + "/overlay/overlay.apk";
322 
323   auto target = TargetResourceContainer::FromPath(target_apk_path);
324   ASSERT_TRUE(target);
325 
326   auto overlay = OverlayResourceContainer::FromPath(overlay_apk_path);
327   ASSERT_TRUE(overlay);
328 
329   {
330     auto idmap_result = Idmap::FromContainers(**target, **overlay, "", PolicyFlags::PUBLIC,
331                                               /* enforce_overlayable */ true);
332     ASSERT_FALSE(idmap_result);
333   }
334   {
335     auto idmap_result = Idmap::FromContainers(**target, **overlay, "unknown", PolicyFlags::PUBLIC,
336                                               /* enforce_overlayable */ true);
337     ASSERT_FALSE(idmap_result);
338   }
339 }
340 
TEST(IdmapTests,CreateIdmapDataFromApkAssetsSharedLibOverlay)341 TEST(IdmapTests, CreateIdmapDataFromApkAssetsSharedLibOverlay) {
342   std::string target_apk_path = GetTestDataPath() + "/target/target.apk";
343   std::string overlay_apk_path = GetTestDataPath() + "/overlay/overlay-shared.apk";
344 
345   auto target = TargetResourceContainer::FromPath(target_apk_path);
346   ASSERT_TRUE(target);
347 
348   auto overlay = OverlayResourceContainer::FromPath(overlay_apk_path);
349   ASSERT_TRUE(overlay);
350 
351   auto idmap_result = Idmap::FromContainers(
352       **target, **overlay, TestConstants::OVERLAY_NAME_DEFAULT, PolicyFlags::PUBLIC,
353       /* enforce_overlayable */ true);
354   ASSERT_TRUE(idmap_result) << idmap_result.GetErrorMessage();
355   auto& idmap = *idmap_result;
356   ASSERT_THAT(idmap, NotNull());
357 
358   const std::vector<std::unique_ptr<const IdmapData>>& dataBlocks = idmap->GetData();
359   ASSERT_EQ(dataBlocks.size(), 1U);
360 
361   const std::unique_ptr<const IdmapData>& data = dataBlocks[0];
362   ASSERT_THAT(data, NotNull());
363 
364   const auto& target_entries = data->GetTargetEntries();
365   ASSERT_EQ(target_entries.size(), 4U);
366   ASSERT_TARGET_ENTRY(target_entries[0], R::target::integer::int1,
367                       fix_package_id(R::overlay::integer::int1, 0));
368   ASSERT_TARGET_ENTRY(target_entries[1], R::target::string::str1,
369                       fix_package_id(R::overlay::string::str1, 0));
370   ASSERT_TARGET_ENTRY(target_entries[2], R::target::string::str3,
371                       fix_package_id(R::overlay::string::str3, 0));
372   ASSERT_TARGET_ENTRY(target_entries[3], R::target::string::str4,
373                       fix_package_id(R::overlay::string::str4, 0));
374 
375   const auto& target_inline_entries = data->GetTargetInlineEntries();
376   ASSERT_EQ(target_inline_entries.size(), 0U);
377 
378   const auto& overlay_entries = data->GetOverlayEntries();
379   ASSERT_EQ(target_entries.size(), 4U);
380   ASSERT_OVERLAY_ENTRY(overlay_entries[0], fix_package_id(R::overlay::integer::int1, 0),
381                        R::target::integer::int1);
382   ASSERT_OVERLAY_ENTRY(overlay_entries[1], fix_package_id(R::overlay::string::str1, 0),
383                        R::target::string::str1);
384   ASSERT_OVERLAY_ENTRY(overlay_entries[2], fix_package_id(R::overlay::string::str3, 0),
385                        R::target::string::str3);
386   ASSERT_OVERLAY_ENTRY(overlay_entries[3], fix_package_id(R::overlay::string::str4, 0),
387                        R::target::string::str4);
388 }
389 
TestIdmapDataFromApkAssets(const std::string & local_target_path,const std::string & local_overlay_path,const std::string & overlay_name,const PolicyBitmask & fulfilled_policies,bool enforce_overlayable)390 Result<std::unique_ptr<const IdmapData>> TestIdmapDataFromApkAssets(
391     const std::string& local_target_path, const std::string& local_overlay_path,
392     const std::string& overlay_name, const PolicyBitmask& fulfilled_policies,
393     bool enforce_overlayable) {
394   const std::string target_path(GetTestDataPath() + local_target_path);
395   auto target = TargetResourceContainer::FromPath(target_path);
396   if (!target) {
397     return Error(R"(Failed to load target "%s")", target_path.c_str());
398   }
399 
400   const std::string overlay_path(GetTestDataPath() + local_overlay_path);
401   auto overlay = OverlayResourceContainer::FromPath(overlay_path);
402   if (!overlay) {
403     return Error(R"(Failed to load overlay "%s")", overlay_path.c_str());
404   }
405 
406   auto overlay_info = (*overlay)->FindOverlayInfo(overlay_name);
407   if (!overlay_info) {
408     return Error(R"(Failed to find overlay name "%s")", overlay_name.c_str());
409   }
410 
411   LogInfo log_info;
412   auto mapping = ResourceMapping::FromContainers(**target, **overlay, *overlay_info,
413                                                  fulfilled_policies, enforce_overlayable, log_info);
414   if (!mapping) {
415     return mapping.GetError();
416   }
417 
418   return IdmapData::FromResourceMapping(*mapping);
419 }
420 
TEST(IdmapTests,CreateIdmapDataDoNotRewriteNonOverlayResourceId)421 TEST(IdmapTests, CreateIdmapDataDoNotRewriteNonOverlayResourceId) {
422   auto idmap_data = TestIdmapDataFromApkAssets("/target/target.apk", "/overlay/overlay.apk",
423                                                "DifferentPackages", PolicyFlags::PUBLIC,
424                                                /* enforce_overlayable */ false);
425 
426   ASSERT_TRUE(idmap_data) << idmap_data.GetErrorMessage();
427   auto& data = *idmap_data;
428 
429   const auto& target_entries = data->GetTargetEntries();
430   ASSERT_EQ(target_entries.size(), 2U);
431   ASSERT_TARGET_ENTRY(target_entries[0], R::target::string::str1,
432                       0x0104000a);  // -> android:string/ok
433   ASSERT_TARGET_ENTRY(target_entries[1], R::target::string::str3, R::overlay::string::str3);
434 
435   const auto& target_inline_entries = data->GetTargetInlineEntries();
436   ASSERT_EQ(target_inline_entries.size(), 0U);
437 
438   const auto& overlay_entries = data->GetOverlayEntries();
439   ASSERT_EQ(overlay_entries.size(), 1U);
440   ASSERT_OVERLAY_ENTRY(overlay_entries[0], R::overlay::string::str3, R::target::string::str3);
441 }
442 
TEST(IdmapTests,CreateIdmapDataInlineResources)443 TEST(IdmapTests, CreateIdmapDataInlineResources) {
444   auto idmap_data = TestIdmapDataFromApkAssets("/target/target.apk", "/overlay/overlay.apk",
445                                                "Inline", PolicyFlags::PUBLIC,
446                                                /* enforce_overlayable */ false);
447 
448   ASSERT_TRUE(idmap_data) << idmap_data.GetErrorMessage();
449   auto& data = *idmap_data;
450 
451   const auto& target_entries = data->GetTargetEntries();
452   ASSERT_EQ(target_entries.size(), 0U);
453 
454   constexpr size_t overlay_string_pool_size = 10U;
455   const auto& target_inline_entries = data->GetTargetInlineEntries();
456   ASSERT_EQ(target_inline_entries.size(), 2U);
457   ASSERT_TARGET_INLINE_ENTRY(target_inline_entries[0], R::target::integer::int1, std::string(),
458                              Res_value::TYPE_INT_DEC, 73U);  // -> 73
459   ASSERT_TARGET_INLINE_ENTRY(target_inline_entries[1], R::target::string::str1, std::string(),
460                              Res_value::TYPE_STRING,
461                              overlay_string_pool_size + 0U);  // -> "Hello World"
462 
463   const auto& overlay_entries = data->GetOverlayEntries();
464   ASSERT_EQ(overlay_entries.size(), 0U);
465 }
466 
TEST(IdmapTests,IdmapHeaderIsUpToDate)467 TEST(IdmapTests, IdmapHeaderIsUpToDate) {
468   fclose(stderr);  // silence expected warnings from libandroidfw
469 
470   const std::string target_apk_path = kIdmapRawTargetPath;
471   const std::string overlay_apk_path = kIdmapRawOverlayPath;
472   const std::string overlay_name = kIdmapRawOverlayName;
473   const PolicyBitmask policies = kIdmapRawDataPolicies;
474   const uint32_t target_crc = kIdmapRawDataTargetCrc;
475   const uint32_t overlay_crc = kIdmapRawOverlayCrc;
476 
477   std::string raw(reinterpret_cast<const char*>(kIdmapRawData), kIdmapRawDataLen);
478   std::istringstream raw_stream(raw);
479 
480   auto result = Idmap::FromBinaryStream(raw_stream);
481   ASSERT_TRUE(result);
482   const auto idmap = std::move(*result);
483 
484   std::stringstream stream;
485   BinaryStreamVisitor visitor(stream);
486   idmap->accept(&visitor);
487 
488   std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(stream);
489   ASSERT_THAT(header, NotNull());
490   ASSERT_TRUE(header->IsUpToDate(target_apk_path, overlay_apk_path, overlay_name,
491                                  kIdmapRawDataTargetCrc, overlay_crc, policies,
492                                  /* enforce_overlayable */ true));
493 
494   // magic: bytes (0x0, 0x03)
495   std::string bad_magic_string(stream.str());
496   bad_magic_string[0x0] = '.';
497   bad_magic_string[0x1] = '.';
498   bad_magic_string[0x2] = '.';
499   bad_magic_string[0x3] = '.';
500   std::stringstream bad_magic_stream(bad_magic_string);
501   std::unique_ptr<const IdmapHeader> bad_magic_header =
502       IdmapHeader::FromBinaryStream(bad_magic_stream);
503   ASSERT_EQ(nullptr, bad_magic_header);
504 
505   // version: bytes (0x4, 0x07)
506   std::string bad_version_string(stream.str());
507   bad_version_string[0x4] = '.';
508   bad_version_string[0x5] = '.';
509   bad_version_string[0x6] = '.';
510   bad_version_string[0x7] = '.';
511   std::stringstream bad_version_stream(bad_version_string);
512   std::unique_ptr<const IdmapHeader> bad_version_header =
513       IdmapHeader::FromBinaryStream(bad_version_stream);
514   ASSERT_EQ(nullptr, bad_version_header);
515 
516   // target crc: bytes (0x8, 0xb)
517   std::string bad_target_crc_string(stream.str());
518   bad_target_crc_string[0x8] = '.';
519   bad_target_crc_string[0x9] = '.';
520   bad_target_crc_string[0xa] = '.';
521   bad_target_crc_string[0xb] = '.';
522   std::stringstream bad_target_crc_stream(bad_target_crc_string);
523   std::unique_ptr<const IdmapHeader> bad_target_crc_header =
524       IdmapHeader::FromBinaryStream(bad_target_crc_stream);
525   ASSERT_THAT(bad_target_crc_header, NotNull());
526   ASSERT_NE(header->GetTargetCrc(), bad_target_crc_header->GetTargetCrc());
527   ASSERT_FALSE(bad_target_crc_header->IsUpToDate(target_apk_path, overlay_apk_path, overlay_name,
528                                                  target_crc, overlay_crc, policies,
529                                                  /* enforce_overlayable */ true));
530 
531   // overlay crc: bytes (0xc, 0xf)
532   std::string bad_overlay_crc_string(stream.str());
533   bad_overlay_crc_string[0xc] = '.';
534   bad_overlay_crc_string[0xd] = '.';
535   bad_overlay_crc_string[0xe] = '.';
536   bad_overlay_crc_string[0xf] = '.';
537   std::stringstream bad_overlay_crc_stream(bad_overlay_crc_string);
538   std::unique_ptr<const IdmapHeader> bad_overlay_crc_header =
539       IdmapHeader::FromBinaryStream(bad_overlay_crc_stream);
540   ASSERT_THAT(bad_overlay_crc_header, NotNull());
541   ASSERT_NE(header->GetOverlayCrc(), bad_overlay_crc_header->GetOverlayCrc());
542   ASSERT_FALSE(bad_overlay_crc_header->IsUpToDate(target_apk_path, overlay_apk_path, overlay_name,
543                                                   target_crc, overlay_crc, policies,
544                                                   /* enforce_overlayable */ true));
545 
546   // fulfilled policy: bytes (0x10, 0x13)
547   std::string bad_policy_string(stream.str());
548   bad_policy_string[0x10] = '.';
549   bad_policy_string[0x11] = '.';
550   bad_policy_string[0x12] = '.';
551   bad_policy_string[0x13] = '.';
552   std::stringstream bad_policy_stream(bad_policy_string);
553   std::unique_ptr<const IdmapHeader> bad_policy_header =
554       IdmapHeader::FromBinaryStream(bad_policy_stream);
555   ASSERT_THAT(bad_policy_header, NotNull());
556   ASSERT_NE(header->GetFulfilledPolicies(), bad_policy_header->GetFulfilledPolicies());
557   ASSERT_FALSE(bad_policy_header->IsUpToDate(target_apk_path, overlay_apk_path, overlay_name,
558                                              target_crc, overlay_crc, policies,
559                                              /* enforce_overlayable */ true));
560 
561   // enforce overlayable: bytes (0x14)
562   std::string bad_enforce_string(stream.str());
563   bad_enforce_string[0x14] = '\0';
564   std::stringstream bad_enforce_stream(bad_enforce_string);
565   std::unique_ptr<const IdmapHeader> bad_enforce_header =
566       IdmapHeader::FromBinaryStream(bad_enforce_stream);
567   ASSERT_THAT(bad_enforce_header, NotNull());
568   ASSERT_NE(header->GetEnforceOverlayable(), bad_enforce_header->GetEnforceOverlayable());
569   ASSERT_FALSE(bad_enforce_header->IsUpToDate(target_apk_path, overlay_apk_path, overlay_name,
570                                               target_crc, overlay_crc, policies,
571                                               /* enforce_overlayable */ true));
572 
573   // target path: bytes (0x1c, 0x27)
574   std::string bad_target_path_string(stream.str());
575   bad_target_path_string[0x1c] = '\0';
576   std::stringstream bad_target_path_stream(bad_target_path_string);
577   std::unique_ptr<const IdmapHeader> bad_target_path_header =
578       IdmapHeader::FromBinaryStream(bad_target_path_stream);
579   ASSERT_THAT(bad_target_path_header, NotNull());
580   ASSERT_NE(header->GetTargetPath(), bad_target_path_header->GetTargetPath());
581   ASSERT_FALSE(bad_target_path_header->IsUpToDate(target_apk_path, overlay_apk_path, overlay_name,
582                                                   target_crc, overlay_crc, policies,
583                                                   /* enforce_overlayable */ true));
584 
585   // overlay path: bytes (0x2c, 0x37)
586   std::string bad_overlay_path_string(stream.str());
587   bad_overlay_path_string[0x33] = '\0';
588   std::stringstream bad_overlay_path_stream(bad_overlay_path_string);
589   std::unique_ptr<const IdmapHeader> bad_overlay_path_header =
590       IdmapHeader::FromBinaryStream(bad_overlay_path_stream);
591   ASSERT_THAT(bad_overlay_path_header, NotNull());
592   ASSERT_NE(header->GetOverlayPath(), bad_overlay_path_header->GetOverlayPath());
593   ASSERT_FALSE(bad_overlay_path_header->IsUpToDate(target_apk_path, overlay_apk_path, overlay_name,
594                                                    target_crc, overlay_crc, policies,
595                                                    /* enforce_overlayable */ true));
596 
597   // overlay name: bytes (0x3c, 0x47)
598   std::string bad_overlay_name_string(stream.str());
599   bad_overlay_name_string[0x3c] = '\0';
600   std::stringstream bad_overlay_name_stream(bad_overlay_name_string);
601   std::unique_ptr<const IdmapHeader> bad_overlay_name_header =
602       IdmapHeader::FromBinaryStream(bad_overlay_name_stream);
603   ASSERT_THAT(bad_overlay_name_header, NotNull());
604   ASSERT_NE(header->GetOverlayName(), bad_overlay_name_header->GetOverlayName());
605   ASSERT_FALSE(bad_overlay_name_header->IsUpToDate(target_apk_path, overlay_apk_path, overlay_name,
606                                                    target_crc, overlay_crc, policies,
607                                                    /* enforce_overlayable */ true));
608 }
609 
610 class TestVisitor : public Visitor {
611  public:
TestVisitor(std::ostream & stream)612   explicit TestVisitor(std::ostream& stream) : stream_(stream) {
613   }
614 
visit(const Idmap & idmap ATTRIBUTE_UNUSED)615   void visit(const Idmap& idmap ATTRIBUTE_UNUSED) override {
616     stream_ << "TestVisitor::visit(Idmap)" << std::endl;
617   }
618 
visit(const IdmapHeader & idmap ATTRIBUTE_UNUSED)619   void visit(const IdmapHeader& idmap ATTRIBUTE_UNUSED) override {
620     stream_ << "TestVisitor::visit(IdmapHeader)" << std::endl;
621   }
622 
visit(const IdmapData & idmap ATTRIBUTE_UNUSED)623   void visit(const IdmapData& idmap ATTRIBUTE_UNUSED) override {
624     stream_ << "TestVisitor::visit(IdmapData)" << std::endl;
625   }
626 
visit(const IdmapData::Header & idmap ATTRIBUTE_UNUSED)627   void visit(const IdmapData::Header& idmap ATTRIBUTE_UNUSED) override {
628     stream_ << "TestVisitor::visit(IdmapData::Header)" << std::endl;
629   }
630 
631  private:
632   std::ostream& stream_;
633 };
634 
TEST(IdmapTests,TestVisitor)635 TEST(IdmapTests, TestVisitor) {
636   std::string raw(reinterpret_cast<const char*>(kIdmapRawData), kIdmapRawDataLen);
637   std::istringstream stream(raw);
638 
639   const auto idmap = Idmap::FromBinaryStream(stream);
640   ASSERT_TRUE(idmap);
641 
642   std::stringstream test_stream;
643   TestVisitor visitor(test_stream);
644   (*idmap)->accept(&visitor);
645 
646   ASSERT_EQ(test_stream.str(),
647             "TestVisitor::visit(IdmapHeader)\n"
648             "TestVisitor::visit(Idmap)\n"
649             "TestVisitor::visit(IdmapData::Header)\n"
650             "TestVisitor::visit(IdmapData)\n");
651 }
652 
653 }  // namespace android::idmap2
654