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 <cstdio> // fclose
18 #include <memory>
19 #include <regex>
20 #include <sstream>
21 #include <string>
22
23 #include "TestConstants.h"
24 #include "TestHelpers.h"
25 #include "android-base/stringprintf.h"
26 #include "androidfw/ResourceTypes.h"
27 #include "gmock/gmock.h"
28 #include "gtest/gtest.h"
29 #include "idmap2/Idmap.h"
30 #include "idmap2/RawPrintVisitor.h"
31
32 using android::base::StringPrintf;
33 using ::testing::NotNull;
34
35 using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
36
37 namespace android::idmap2 {
38
39 #define ASSERT_CONTAINS_REGEX(pattern, str) \
40 do { \
41 ASSERT_TRUE(std::regex_search(str, std::regex(pattern))) \
42 << "pattern '" << pattern << "' not found in\n--------\n" \
43 << str << "--------"; \
44 } while (0)
45
46 #define ADDRESS "[0-9a-f]{8}: "
47
TEST(RawPrintVisitorTests,CreateRawPrintVisitor)48 TEST(RawPrintVisitorTests, CreateRawPrintVisitor) {
49 fclose(stderr); // silence expected warnings
50
51 const std::string target_apk_path(GetTestDataPath() + "/target/target.apk");
52 std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path);
53 ASSERT_THAT(target_apk, NotNull());
54
55 const std::string overlay_apk_path(GetTestDataPath() + "/overlay/overlay.apk");
56 std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path);
57 ASSERT_THAT(overlay_apk, NotNull());
58
59 const auto idmap = Idmap::FromApkAssets(*target_apk, *overlay_apk, PolicyFlags::PUBLIC,
60 /* enforce_overlayable */ true);
61 ASSERT_TRUE(idmap);
62
63 std::stringstream stream;
64 RawPrintVisitor visitor(stream);
65 (*idmap)->accept(&visitor);
66
67 ASSERT_CONTAINS_REGEX(ADDRESS "504d4449 magic\n", stream.str());
68 ASSERT_CONTAINS_REGEX(ADDRESS "00000004 version\n", stream.str());
69 ASSERT_CONTAINS_REGEX(
70 StringPrintf(ADDRESS "%s target crc\n", android::idmap2::TestConstants::TARGET_CRC_STRING),
71 stream.str());
72 ASSERT_CONTAINS_REGEX(
73 StringPrintf(ADDRESS "%s overlay crc\n", android::idmap2::TestConstants::OVERLAY_CRC_STRING),
74 stream.str());
75 ASSERT_CONTAINS_REGEX(ADDRESS "00000001 fulfilled policies: public\n", stream.str());
76 ASSERT_CONTAINS_REGEX(ADDRESS " 01 enforce overlayable\n", stream.str());
77 ASSERT_CONTAINS_REGEX(ADDRESS " 7f target package id\n", stream.str());
78 ASSERT_CONTAINS_REGEX(ADDRESS " 7f overlay package id\n", stream.str());
79 ASSERT_CONTAINS_REGEX(ADDRESS "00000004 target entry count\n", stream.str());
80 ASSERT_CONTAINS_REGEX(ADDRESS "00000004 overlay entry count\n", stream.str());
81 ASSERT_CONTAINS_REGEX(ADDRESS "00000004 overlay entry count\n", stream.str());
82 ASSERT_CONTAINS_REGEX(ADDRESS "00000008 string pool index offset\n", stream.str());
83 ASSERT_CONTAINS_REGEX(ADDRESS "000000b4 string pool byte length\n", stream.str());
84 ASSERT_CONTAINS_REGEX(ADDRESS "7f010000 target id: integer/int1\n", stream.str());
85 ASSERT_CONTAINS_REGEX(ADDRESS " 07 type: reference \\(dynamic\\)\n", stream.str());
86 ASSERT_CONTAINS_REGEX(ADDRESS "7f010000 value: integer/int1\n", stream.str());
87 ASSERT_CONTAINS_REGEX(ADDRESS "7f010000 overlay id: integer/int1\n", stream.str());
88 ASSERT_CONTAINS_REGEX(ADDRESS "7f010000 target id: integer/int1\n", stream.str());
89 }
90
TEST(RawPrintVisitorTests,CreateRawPrintVisitorWithoutAccessToApks)91 TEST(RawPrintVisitorTests, CreateRawPrintVisitorWithoutAccessToApks) {
92 fclose(stderr); // silence expected warnings from libandroidfw
93
94 std::string raw(reinterpret_cast<const char*>(idmap_raw_data), idmap_raw_data_len);
95 std::istringstream raw_stream(raw);
96
97 const auto idmap = Idmap::FromBinaryStream(raw_stream);
98 ASSERT_TRUE(idmap);
99
100 std::stringstream stream;
101 RawPrintVisitor visitor(stream);
102 (*idmap)->accept(&visitor);
103
104 ASSERT_CONTAINS_REGEX(ADDRESS "504d4449 magic\n", stream.str());
105 ASSERT_CONTAINS_REGEX(ADDRESS "00000004 version\n", stream.str());
106 ASSERT_CONTAINS_REGEX(ADDRESS "00001234 target crc\n", stream.str());
107 ASSERT_CONTAINS_REGEX(ADDRESS "00005678 overlay crc\n", stream.str());
108 ASSERT_CONTAINS_REGEX(ADDRESS "00000011 fulfilled policies: public|signature\n", stream.str());
109 ASSERT_CONTAINS_REGEX(ADDRESS " 01 enforce overlayable\n", stream.str());
110 ASSERT_CONTAINS_REGEX(ADDRESS " 7f target package id\n", stream.str());
111 ASSERT_CONTAINS_REGEX(ADDRESS " 7f overlay package id\n", stream.str());
112 ASSERT_CONTAINS_REGEX(ADDRESS "00000003 target entry count\n", stream.str());
113 ASSERT_CONTAINS_REGEX(ADDRESS "00000003 overlay entry count\n", stream.str());
114 ASSERT_CONTAINS_REGEX(ADDRESS "00000000 string pool index offset\n", stream.str());
115 ASSERT_CONTAINS_REGEX(ADDRESS "00000000 string pool byte length\n", stream.str());
116 ASSERT_CONTAINS_REGEX(ADDRESS "7f020000 target id\n", stream.str());
117 ASSERT_CONTAINS_REGEX(ADDRESS " 01 type: reference\n", stream.str());
118 ASSERT_CONTAINS_REGEX(ADDRESS "7f020000 value\n", stream.str());
119 ASSERT_CONTAINS_REGEX(ADDRESS "7f020000 overlay id\n", stream.str());
120 ASSERT_CONTAINS_REGEX(ADDRESS "7f020000 target id\n", stream.str());
121 }
122
123 } // namespace android::idmap2
124