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 android::Res_value;
41 using ::testing::NotNull;
42
43 using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
44
45 namespace android::idmap2 {
46
47 #define ASSERT_TARGET_ENTRY(entry, target_resid, overlay_resid) \
48 ASSERT_EQ((entry).target_id, (target_resid)); \
49 ASSERT_EQ((entry).overlay_id, (overlay_resid))
50
51 #define ASSERT_TARGET_INLINE_ENTRY(entry, target_resid, expected_type, expected_value) \
52 ASSERT_EQ((entry).target_id, target_resid); \
53 ASSERT_EQ((entry).value.data_type, (expected_type)); \
54 ASSERT_EQ((entry).value.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(), 0x08U);
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, Res_value::TYPE_INT_HEX,
127 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(), 0x08U);
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, Res_value::TYPE_INT_HEX,
170 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(), 0x08U);
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 frro = FabricatedOverlay::Builder("com.example.overlay", "SandTheme", "test.target")
264 .SetOverlayable("TestResources")
265 .SetResourceValue("integer/int1", Res_value::TYPE_INT_DEC, 2U)
266 .SetResourceValue("string/str1", Res_value::TYPE_REFERENCE, 0x7f010000)
267 .Build();
268
269 ASSERT_TRUE(frro);
270 TemporaryFile tf;
271 std::ofstream out(tf.path);
272 ASSERT_TRUE((*frro).ToBinaryStream(out));
273 out.close();
274
275 auto overlay = OverlayResourceContainer::FromPath(tf.path);
276 ASSERT_TRUE(overlay);
277
278 auto idmap_result = Idmap::FromContainers(**target, **overlay, "SandTheme", PolicyFlags::PUBLIC,
279 /* enforce_overlayable */ true);
280 ASSERT_TRUE(idmap_result) << idmap_result.GetErrorMessage();
281 auto& idmap = *idmap_result;
282 ASSERT_THAT(idmap, NotNull());
283
284 const std::vector<std::unique_ptr<const IdmapData>>& dataBlocks = idmap->GetData();
285 ASSERT_EQ(dataBlocks.size(), 1U);
286
287 const std::unique_ptr<const IdmapData>& data = dataBlocks[0];
288 ASSERT_THAT(data, NotNull());
289 ASSERT_EQ(data->GetTargetEntries().size(), 0U);
290 ASSERT_EQ(data->GetOverlayEntries().size(), 0U);
291
292 const auto& target_inline_entries = data->GetTargetInlineEntries();
293 ASSERT_EQ(target_inline_entries.size(), 2U);
294 ASSERT_TARGET_INLINE_ENTRY(target_inline_entries[0], R::target::integer::int1,
295 Res_value::TYPE_INT_DEC, 2U);
296 ASSERT_TARGET_INLINE_ENTRY(target_inline_entries[1], R::target::string::str1,
297 Res_value::TYPE_REFERENCE, 0x7f010000);
298 }
299
TEST(IdmapTests,FailCreateIdmapInvalidName)300 TEST(IdmapTests, FailCreateIdmapInvalidName) {
301 std::string target_apk_path = GetTestDataPath() + "/target/target.apk";
302 std::string overlay_apk_path = GetTestDataPath() + "/overlay/overlay.apk";
303
304 auto target = TargetResourceContainer::FromPath(target_apk_path);
305 ASSERT_TRUE(target);
306
307 auto overlay = OverlayResourceContainer::FromPath(overlay_apk_path);
308 ASSERT_TRUE(overlay);
309
310 {
311 auto idmap_result = Idmap::FromContainers(**target, **overlay, "", PolicyFlags::PUBLIC,
312 /* enforce_overlayable */ true);
313 ASSERT_FALSE(idmap_result);
314 }
315 {
316 auto idmap_result = Idmap::FromContainers(**target, **overlay, "unknown", PolicyFlags::PUBLIC,
317 /* enforce_overlayable */ true);
318 ASSERT_FALSE(idmap_result);
319 }
320 }
321
TEST(IdmapTests,CreateIdmapDataFromApkAssetsSharedLibOverlay)322 TEST(IdmapTests, CreateIdmapDataFromApkAssetsSharedLibOverlay) {
323 std::string target_apk_path = GetTestDataPath() + "/target/target.apk";
324 std::string overlay_apk_path = GetTestDataPath() + "/overlay/overlay-shared.apk";
325
326 auto target = TargetResourceContainer::FromPath(target_apk_path);
327 ASSERT_TRUE(target);
328
329 auto overlay = OverlayResourceContainer::FromPath(overlay_apk_path);
330 ASSERT_TRUE(overlay);
331
332 auto idmap_result = Idmap::FromContainers(
333 **target, **overlay, TestConstants::OVERLAY_NAME_DEFAULT, PolicyFlags::PUBLIC,
334 /* enforce_overlayable */ true);
335 ASSERT_TRUE(idmap_result) << idmap_result.GetErrorMessage();
336 auto& idmap = *idmap_result;
337 ASSERT_THAT(idmap, NotNull());
338
339 const std::vector<std::unique_ptr<const IdmapData>>& dataBlocks = idmap->GetData();
340 ASSERT_EQ(dataBlocks.size(), 1U);
341
342 const std::unique_ptr<const IdmapData>& data = dataBlocks[0];
343 ASSERT_THAT(data, NotNull());
344
345 const auto& target_entries = data->GetTargetEntries();
346 ASSERT_EQ(target_entries.size(), 4U);
347 ASSERT_TARGET_ENTRY(target_entries[0], R::target::integer::int1,
348 fix_package_id(R::overlay::integer::int1, 0));
349 ASSERT_TARGET_ENTRY(target_entries[1], R::target::string::str1,
350 fix_package_id(R::overlay::string::str1, 0));
351 ASSERT_TARGET_ENTRY(target_entries[2], R::target::string::str3,
352 fix_package_id(R::overlay::string::str3, 0));
353 ASSERT_TARGET_ENTRY(target_entries[3], R::target::string::str4,
354 fix_package_id(R::overlay::string::str4, 0));
355
356 const auto& target_inline_entries = data->GetTargetInlineEntries();
357 ASSERT_EQ(target_inline_entries.size(), 0U);
358
359 const auto& overlay_entries = data->GetOverlayEntries();
360 ASSERT_EQ(target_entries.size(), 4U);
361 ASSERT_OVERLAY_ENTRY(overlay_entries[0], fix_package_id(R::overlay::integer::int1, 0),
362 R::target::integer::int1);
363 ASSERT_OVERLAY_ENTRY(overlay_entries[1], fix_package_id(R::overlay::string::str1, 0),
364 R::target::string::str1);
365 ASSERT_OVERLAY_ENTRY(overlay_entries[2], fix_package_id(R::overlay::string::str3, 0),
366 R::target::string::str3);
367 ASSERT_OVERLAY_ENTRY(overlay_entries[3], fix_package_id(R::overlay::string::str4, 0),
368 R::target::string::str4);
369 }
370
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)371 Result<std::unique_ptr<const IdmapData>> TestIdmapDataFromApkAssets(
372 const std::string& local_target_path, const std::string& local_overlay_path,
373 const std::string& overlay_name, const PolicyBitmask& fulfilled_policies,
374 bool enforce_overlayable) {
375 const std::string target_path(GetTestDataPath() + local_target_path);
376 auto target = TargetResourceContainer::FromPath(target_path);
377 if (!target) {
378 return Error(R"(Failed to load target "%s")", target_path.c_str());
379 }
380
381 const std::string overlay_path(GetTestDataPath() + local_overlay_path);
382 auto overlay = OverlayResourceContainer::FromPath(overlay_path);
383 if (!overlay) {
384 return Error(R"(Failed to load overlay "%s")", overlay_path.c_str());
385 }
386
387 auto overlay_info = (*overlay)->FindOverlayInfo(overlay_name);
388 if (!overlay_info) {
389 return Error(R"(Failed to find overlay name "%s")", overlay_name.c_str());
390 }
391
392 LogInfo log_info;
393 auto mapping = ResourceMapping::FromContainers(**target, **overlay, *overlay_info,
394 fulfilled_policies, enforce_overlayable, log_info);
395 if (!mapping) {
396 return mapping.GetError();
397 }
398
399 return IdmapData::FromResourceMapping(*mapping);
400 }
401
TEST(IdmapTests,CreateIdmapDataDoNotRewriteNonOverlayResourceId)402 TEST(IdmapTests, CreateIdmapDataDoNotRewriteNonOverlayResourceId) {
403 auto idmap_data = TestIdmapDataFromApkAssets("/target/target.apk", "/overlay/overlay.apk",
404 "DifferentPackages", PolicyFlags::PUBLIC,
405 /* enforce_overlayable */ false);
406
407 ASSERT_TRUE(idmap_data) << idmap_data.GetErrorMessage();
408 auto& data = *idmap_data;
409
410 const auto& target_entries = data->GetTargetEntries();
411 ASSERT_EQ(target_entries.size(), 2U);
412 ASSERT_TARGET_ENTRY(target_entries[0], R::target::string::str1,
413 0x0104000a); // -> android:string/ok
414 ASSERT_TARGET_ENTRY(target_entries[1], R::target::string::str3, R::overlay::string::str3);
415
416 const auto& target_inline_entries = data->GetTargetInlineEntries();
417 ASSERT_EQ(target_inline_entries.size(), 0U);
418
419 const auto& overlay_entries = data->GetOverlayEntries();
420 ASSERT_EQ(overlay_entries.size(), 1U);
421 ASSERT_OVERLAY_ENTRY(overlay_entries[0], R::overlay::string::str3, R::target::string::str3);
422 }
423
TEST(IdmapTests,CreateIdmapDataInlineResources)424 TEST(IdmapTests, CreateIdmapDataInlineResources) {
425 auto idmap_data = TestIdmapDataFromApkAssets("/target/target.apk", "/overlay/overlay.apk",
426 "Inline", PolicyFlags::PUBLIC,
427 /* enforce_overlayable */ false);
428
429 ASSERT_TRUE(idmap_data) << idmap_data.GetErrorMessage();
430 auto& data = *idmap_data;
431
432 const auto& target_entries = data->GetTargetEntries();
433 ASSERT_EQ(target_entries.size(), 0U);
434
435 constexpr size_t overlay_string_pool_size = 10U;
436 const auto& target_inline_entries = data->GetTargetInlineEntries();
437 ASSERT_EQ(target_inline_entries.size(), 2U);
438 ASSERT_TARGET_INLINE_ENTRY(target_inline_entries[0], R::target::integer::int1,
439 Res_value::TYPE_INT_DEC, 73U); // -> 73
440 ASSERT_TARGET_INLINE_ENTRY(target_inline_entries[1], R::target::string::str1,
441 Res_value::TYPE_STRING,
442 overlay_string_pool_size + 0U); // -> "Hello World"
443
444 const auto& overlay_entries = data->GetOverlayEntries();
445 ASSERT_EQ(overlay_entries.size(), 0U);
446 }
447
TEST(IdmapTests,IdmapHeaderIsUpToDate)448 TEST(IdmapTests, IdmapHeaderIsUpToDate) {
449 fclose(stderr); // silence expected warnings from libandroidfw
450
451 const std::string target_apk_path = kIdmapRawTargetPath;
452 const std::string overlay_apk_path = kIdmapRawOverlayPath;
453 const std::string overlay_name = kIdmapRawOverlayName;
454 const PolicyBitmask policies = kIdmapRawDataPolicies;
455 const uint32_t target_crc = kIdmapRawDataTargetCrc;
456 const uint32_t overlay_crc = kIdmapRawOverlayCrc;
457
458 std::string raw(reinterpret_cast<const char*>(kIdmapRawData), kIdmapRawDataLen);
459 std::istringstream raw_stream(raw);
460
461 auto result = Idmap::FromBinaryStream(raw_stream);
462 ASSERT_TRUE(result);
463 const auto idmap = std::move(*result);
464
465 std::stringstream stream;
466 BinaryStreamVisitor visitor(stream);
467 idmap->accept(&visitor);
468
469 std::unique_ptr<const IdmapHeader> header = IdmapHeader::FromBinaryStream(stream);
470 ASSERT_THAT(header, NotNull());
471 ASSERT_TRUE(header->IsUpToDate(target_apk_path, overlay_apk_path, overlay_name,
472 kIdmapRawDataTargetCrc, overlay_crc, policies,
473 /* enforce_overlayable */ true));
474
475 // magic: bytes (0x0, 0x03)
476 std::string bad_magic_string(stream.str());
477 bad_magic_string[0x0] = '.';
478 bad_magic_string[0x1] = '.';
479 bad_magic_string[0x2] = '.';
480 bad_magic_string[0x3] = '.';
481 std::stringstream bad_magic_stream(bad_magic_string);
482 std::unique_ptr<const IdmapHeader> bad_magic_header =
483 IdmapHeader::FromBinaryStream(bad_magic_stream);
484 ASSERT_EQ(nullptr, bad_magic_header);
485
486 // version: bytes (0x4, 0x07)
487 std::string bad_version_string(stream.str());
488 bad_version_string[0x4] = '.';
489 bad_version_string[0x5] = '.';
490 bad_version_string[0x6] = '.';
491 bad_version_string[0x7] = '.';
492 std::stringstream bad_version_stream(bad_version_string);
493 std::unique_ptr<const IdmapHeader> bad_version_header =
494 IdmapHeader::FromBinaryStream(bad_version_stream);
495 ASSERT_EQ(nullptr, bad_version_header);
496
497 // target crc: bytes (0x8, 0xb)
498 std::string bad_target_crc_string(stream.str());
499 bad_target_crc_string[0x8] = '.';
500 bad_target_crc_string[0x9] = '.';
501 bad_target_crc_string[0xa] = '.';
502 bad_target_crc_string[0xb] = '.';
503 std::stringstream bad_target_crc_stream(bad_target_crc_string);
504 std::unique_ptr<const IdmapHeader> bad_target_crc_header =
505 IdmapHeader::FromBinaryStream(bad_target_crc_stream);
506 ASSERT_THAT(bad_target_crc_header, NotNull());
507 ASSERT_NE(header->GetTargetCrc(), bad_target_crc_header->GetTargetCrc());
508 ASSERT_FALSE(bad_target_crc_header->IsUpToDate(target_apk_path, overlay_apk_path, overlay_name,
509 target_crc, overlay_crc, policies,
510 /* enforce_overlayable */ true));
511
512 // overlay crc: bytes (0xc, 0xf)
513 std::string bad_overlay_crc_string(stream.str());
514 bad_overlay_crc_string[0xc] = '.';
515 bad_overlay_crc_string[0xd] = '.';
516 bad_overlay_crc_string[0xe] = '.';
517 bad_overlay_crc_string[0xf] = '.';
518 std::stringstream bad_overlay_crc_stream(bad_overlay_crc_string);
519 std::unique_ptr<const IdmapHeader> bad_overlay_crc_header =
520 IdmapHeader::FromBinaryStream(bad_overlay_crc_stream);
521 ASSERT_THAT(bad_overlay_crc_header, NotNull());
522 ASSERT_NE(header->GetOverlayCrc(), bad_overlay_crc_header->GetOverlayCrc());
523 ASSERT_FALSE(bad_overlay_crc_header->IsUpToDate(target_apk_path, overlay_apk_path, overlay_name,
524 target_crc, overlay_crc, policies,
525 /* enforce_overlayable */ true));
526
527 // fulfilled policy: bytes (0x10, 0x13)
528 std::string bad_policy_string(stream.str());
529 bad_policy_string[0x10] = '.';
530 bad_policy_string[0x11] = '.';
531 bad_policy_string[0x12] = '.';
532 bad_policy_string[0x13] = '.';
533 std::stringstream bad_policy_stream(bad_policy_string);
534 std::unique_ptr<const IdmapHeader> bad_policy_header =
535 IdmapHeader::FromBinaryStream(bad_policy_stream);
536 ASSERT_THAT(bad_policy_header, NotNull());
537 ASSERT_NE(header->GetFulfilledPolicies(), bad_policy_header->GetFulfilledPolicies());
538 ASSERT_FALSE(bad_policy_header->IsUpToDate(target_apk_path, overlay_apk_path, overlay_name,
539 target_crc, overlay_crc, policies,
540 /* enforce_overlayable */ true));
541
542 // enforce overlayable: bytes (0x14)
543 std::string bad_enforce_string(stream.str());
544 bad_enforce_string[0x14] = '\0';
545 std::stringstream bad_enforce_stream(bad_enforce_string);
546 std::unique_ptr<const IdmapHeader> bad_enforce_header =
547 IdmapHeader::FromBinaryStream(bad_enforce_stream);
548 ASSERT_THAT(bad_enforce_header, NotNull());
549 ASSERT_NE(header->GetEnforceOverlayable(), bad_enforce_header->GetEnforceOverlayable());
550 ASSERT_FALSE(bad_enforce_header->IsUpToDate(target_apk_path, overlay_apk_path, overlay_name,
551 target_crc, overlay_crc, policies,
552 /* enforce_overlayable */ true));
553
554 // target path: bytes (0x1c, 0x27)
555 std::string bad_target_path_string(stream.str());
556 bad_target_path_string[0x1c] = '\0';
557 std::stringstream bad_target_path_stream(bad_target_path_string);
558 std::unique_ptr<const IdmapHeader> bad_target_path_header =
559 IdmapHeader::FromBinaryStream(bad_target_path_stream);
560 ASSERT_THAT(bad_target_path_header, NotNull());
561 ASSERT_NE(header->GetTargetPath(), bad_target_path_header->GetTargetPath());
562 ASSERT_FALSE(bad_target_path_header->IsUpToDate(target_apk_path, overlay_apk_path, overlay_name,
563 target_crc, overlay_crc, policies,
564 /* enforce_overlayable */ true));
565
566 // overlay path: bytes (0x2c, 0x37)
567 std::string bad_overlay_path_string(stream.str());
568 bad_overlay_path_string[0x33] = '\0';
569 std::stringstream bad_overlay_path_stream(bad_overlay_path_string);
570 std::unique_ptr<const IdmapHeader> bad_overlay_path_header =
571 IdmapHeader::FromBinaryStream(bad_overlay_path_stream);
572 ASSERT_THAT(bad_overlay_path_header, NotNull());
573 ASSERT_NE(header->GetOverlayPath(), bad_overlay_path_header->GetOverlayPath());
574 ASSERT_FALSE(bad_overlay_path_header->IsUpToDate(target_apk_path, overlay_apk_path, overlay_name,
575 target_crc, overlay_crc, policies,
576 /* enforce_overlayable */ true));
577
578 // overlay name: bytes (0x3c, 0x47)
579 std::string bad_overlay_name_string(stream.str());
580 bad_overlay_name_string[0x3c] = '\0';
581 std::stringstream bad_overlay_name_stream(bad_overlay_name_string);
582 std::unique_ptr<const IdmapHeader> bad_overlay_name_header =
583 IdmapHeader::FromBinaryStream(bad_overlay_name_stream);
584 ASSERT_THAT(bad_overlay_name_header, NotNull());
585 ASSERT_NE(header->GetOverlayName(), bad_overlay_name_header->GetOverlayName());
586 ASSERT_FALSE(bad_overlay_name_header->IsUpToDate(target_apk_path, overlay_apk_path, overlay_name,
587 target_crc, overlay_crc, policies,
588 /* enforce_overlayable */ true));
589 }
590
591 class TestVisitor : public Visitor {
592 public:
TestVisitor(std::ostream & stream)593 explicit TestVisitor(std::ostream& stream) : stream_(stream) {
594 }
595
visit(const Idmap & idmap ATTRIBUTE_UNUSED)596 void visit(const Idmap& idmap ATTRIBUTE_UNUSED) override {
597 stream_ << "TestVisitor::visit(Idmap)" << std::endl;
598 }
599
visit(const IdmapHeader & idmap ATTRIBUTE_UNUSED)600 void visit(const IdmapHeader& idmap ATTRIBUTE_UNUSED) override {
601 stream_ << "TestVisitor::visit(IdmapHeader)" << std::endl;
602 }
603
visit(const IdmapData & idmap ATTRIBUTE_UNUSED)604 void visit(const IdmapData& idmap ATTRIBUTE_UNUSED) override {
605 stream_ << "TestVisitor::visit(IdmapData)" << std::endl;
606 }
607
visit(const IdmapData::Header & idmap ATTRIBUTE_UNUSED)608 void visit(const IdmapData::Header& idmap ATTRIBUTE_UNUSED) override {
609 stream_ << "TestVisitor::visit(IdmapData::Header)" << std::endl;
610 }
611
612 private:
613 std::ostream& stream_;
614 };
615
TEST(IdmapTests,TestVisitor)616 TEST(IdmapTests, TestVisitor) {
617 std::string raw(reinterpret_cast<const char*>(kIdmapRawData), kIdmapRawDataLen);
618 std::istringstream stream(raw);
619
620 const auto idmap = Idmap::FromBinaryStream(stream);
621 ASSERT_TRUE(idmap);
622
623 std::stringstream test_stream;
624 TestVisitor visitor(test_stream);
625 (*idmap)->accept(&visitor);
626
627 ASSERT_EQ(test_stream.str(),
628 "TestVisitor::visit(IdmapHeader)\n"
629 "TestVisitor::visit(Idmap)\n"
630 "TestVisitor::visit(IdmapData::Header)\n"
631 "TestVisitor::visit(IdmapData)\n");
632 }
633
634 } // namespace android::idmap2
635