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
34 using PolicyFlags = android::ResTable_overlayable_policy_header::PolicyFlags;
35
36 namespace android::idmap2 {
37
38 #define ASSERT_CONTAINS_REGEX(pattern, str) \
39 do { \
40 ASSERT_TRUE(std::regex_search(str, std::regex(pattern))) \
41 << "pattern '" << (pattern) << "' not found in\n--------\n" \
42 << (str) << "--------"; \
43 } while (0)
44
45 #define ADDRESS "[0-9a-f]{8}: "
46
TEST(RawPrintVisitorTests,CreateRawPrintVisitor)47 TEST(RawPrintVisitorTests, CreateRawPrintVisitor) {
48 fclose(stderr); // silence expected warnings
49
50 const std::string target_apk_path(GetTestDataPath() + "/target/target.apk");
51 auto target = TargetResourceContainer::FromPath(target_apk_path);
52 ASSERT_TRUE(target);
53
54 const std::string overlay_apk_path(GetTestDataPath() + "/overlay/overlay.apk");
55 auto overlay = OverlayResourceContainer::FromPath(overlay_apk_path);
56 ASSERT_TRUE(overlay);
57
58 const auto idmap = Idmap::FromContainers(**target, **overlay, TestConstants::OVERLAY_NAME_DEFAULT,
59 PolicyFlags::PUBLIC, /* enforce_overlayable */ true);
60 ASSERT_TRUE(idmap);
61
62 std::stringstream stream;
63 RawPrintVisitor visitor(stream);
64 (*idmap)->accept(&visitor);
65
66 ASSERT_CONTAINS_REGEX(ADDRESS "504d4449 magic\n", stream.str());
67 ASSERT_CONTAINS_REGEX(ADDRESS "00000008 version\n", stream.str());
68 ASSERT_CONTAINS_REGEX(
69 StringPrintf(ADDRESS "%s target crc\n", android::idmap2::TestConstants::TARGET_CRC_STRING),
70 stream.str());
71 ASSERT_CONTAINS_REGEX(
72 StringPrintf(ADDRESS "%s overlay crc\n", android::idmap2::TestConstants::OVERLAY_CRC_STRING),
73 stream.str());
74 ASSERT_CONTAINS_REGEX(ADDRESS "00000001 fulfilled policies: public\n", stream.str());
75 ASSERT_CONTAINS_REGEX(ADDRESS "00000001 enforce overlayable\n", stream.str());
76 ASSERT_CONTAINS_REGEX(ADDRESS "00000004 target entry count", stream.str());
77 ASSERT_CONTAINS_REGEX(ADDRESS "00000000 target inline entry count", stream.str());
78 ASSERT_CONTAINS_REGEX(ADDRESS "00000004 overlay entry count", stream.str());
79 ASSERT_CONTAINS_REGEX(ADDRESS "0000000a string pool index offset", stream.str());
80 ASSERT_CONTAINS_REGEX(ADDRESS "7f010000 target id: integer/int1", stream.str());
81 ASSERT_CONTAINS_REGEX(ADDRESS "7f010000 overlay id: integer/int1", stream.str());
82 ASSERT_CONTAINS_REGEX(ADDRESS "7f02000e target id: string/str1", stream.str());
83 ASSERT_CONTAINS_REGEX(ADDRESS "7f02000b overlay id: string/str1", stream.str());
84 ASSERT_CONTAINS_REGEX(ADDRESS "7f020010 target id: string/str3", stream.str());
85 ASSERT_CONTAINS_REGEX(ADDRESS "7f02000c overlay id: string/str3", stream.str());
86 ASSERT_CONTAINS_REGEX(ADDRESS "7f020011 target id: string/str4", stream.str());
87 ASSERT_CONTAINS_REGEX(ADDRESS "7f02000d overlay id: string/str4", stream.str());
88 ASSERT_CONTAINS_REGEX(ADDRESS "7f010000 overlay id: integer/int1", stream.str());
89 ASSERT_CONTAINS_REGEX(ADDRESS "7f010000 target id: integer/int1", stream.str());
90 ASSERT_CONTAINS_REGEX(ADDRESS "7f02000b overlay id: string/str1", stream.str());
91 ASSERT_CONTAINS_REGEX(ADDRESS "7f02000e target id: string/str1", stream.str());
92 ASSERT_CONTAINS_REGEX(ADDRESS "7f02000c overlay id: string/str3", stream.str());
93 ASSERT_CONTAINS_REGEX(ADDRESS "7f020010 target id: string/str3", stream.str());
94 ASSERT_CONTAINS_REGEX(ADDRESS "7f02000d overlay id: string/str4", stream.str());
95 ASSERT_CONTAINS_REGEX(ADDRESS "7f020011 target id: string/str4", stream.str());
96 ASSERT_CONTAINS_REGEX(ADDRESS "000000b4 string pool size", stream.str());
97 ASSERT_CONTAINS_REGEX(ADDRESS "........ string pool", stream.str());
98 }
99
TEST(RawPrintVisitorTests,CreateRawPrintVisitorWithoutAccessToApks)100 TEST(RawPrintVisitorTests, CreateRawPrintVisitorWithoutAccessToApks) {
101 fclose(stderr); // silence expected warnings from libandroidfw
102
103 std::string raw(reinterpret_cast<const char*>(kIdmapRawData), kIdmapRawDataLen);
104 std::istringstream raw_stream(raw);
105
106 const auto idmap = Idmap::FromBinaryStream(raw_stream);
107 ASSERT_TRUE(idmap);
108
109 std::stringstream stream;
110 RawPrintVisitor visitor(stream);
111 (*idmap)->accept(&visitor);
112
113 ASSERT_CONTAINS_REGEX(ADDRESS "504d4449 magic\n", stream.str());
114 ASSERT_CONTAINS_REGEX(ADDRESS "00000008 version\n", stream.str());
115 ASSERT_CONTAINS_REGEX(ADDRESS "00001234 target crc\n", stream.str());
116 ASSERT_CONTAINS_REGEX(ADDRESS "00005678 overlay crc\n", stream.str());
117 ASSERT_CONTAINS_REGEX(ADDRESS "00000011 fulfilled policies: public|signature\n", stream.str());
118 ASSERT_CONTAINS_REGEX(ADDRESS "00000001 enforce overlayable\n", stream.str());
119 ASSERT_CONTAINS_REGEX(ADDRESS "0000000b target path size\n", stream.str());
120 ASSERT_CONTAINS_REGEX(ADDRESS "........ target path: targetX.apk\n", stream.str());
121 ASSERT_CONTAINS_REGEX(ADDRESS "0000000c overlay path size\n", stream.str());
122 ASSERT_CONTAINS_REGEX(ADDRESS "........ overlay path: overlayX.apk\n", stream.str());
123 ASSERT_CONTAINS_REGEX(ADDRESS "0000000b overlay name size\n", stream.str());
124 ASSERT_CONTAINS_REGEX(ADDRESS "........ overlay name: OverlayName\n", stream.str());
125 ASSERT_CONTAINS_REGEX(ADDRESS "00000003 target entry count\n", stream.str());
126 ASSERT_CONTAINS_REGEX(ADDRESS "00000001 target inline entry count\n", stream.str());
127 ASSERT_CONTAINS_REGEX(ADDRESS "00000003 overlay entry count\n", stream.str());
128 ASSERT_CONTAINS_REGEX(ADDRESS "00000000 string pool index offset\n", stream.str());
129 ASSERT_CONTAINS_REGEX(ADDRESS "7f020000 target id\n", stream.str());
130 ASSERT_CONTAINS_REGEX(ADDRESS "7f020000 overlay id\n", stream.str());
131 ASSERT_CONTAINS_REGEX(ADDRESS "7f020000 target id\n", stream.str());
132 ASSERT_CONTAINS_REGEX(ADDRESS " 11 type: integer\n", stream.str());
133 ASSERT_CONTAINS_REGEX(ADDRESS "12345678 data\n", stream.str());
134 ASSERT_CONTAINS_REGEX(ADDRESS "7f020000 overlay id\n", stream.str());
135 ASSERT_CONTAINS_REGEX(ADDRESS "7f030002 target id\n", stream.str());
136 ASSERT_CONTAINS_REGEX(ADDRESS "00000004 string pool size\n", stream.str());
137 ASSERT_CONTAINS_REGEX("000000a4: ........ string pool\n", stream.str());
138 }
139
140 } // namespace android::idmap2
141