blob: 5c4e8576985bc0fc58bd1e5cc1b892756784eb92 [file] [log] [blame]
MÃ¥rten Kongstad02751232018-04-27 13:16:32 +02001/*
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/*
18 * The tests in this file operate on a higher level than the tests in the other
19 * files. Here, all tests execute the idmap2 binary and only depend on
20 * libidmap2 to verify the output of idmap2.
21 */
22#include <fcntl.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <sys/wait.h>
26#include <unistd.h>
27
28#include <cerrno>
29#include <cstdlib>
30#include <cstring> // strerror
31#include <fstream>
32#include <memory>
33#include <sstream>
34#include <string>
35#include <vector>
36
37#include "gmock/gmock.h"
38#include "gtest/gtest.h"
39
40#include "androidfw/PosixUtils.h"
41#include "idmap2/FileUtils.h"
42#include "idmap2/Idmap.h"
43
44#include "TestHelpers.h"
45
46using ::android::util::ExecuteBinary;
47using ::testing::NotNull;
48
49namespace android {
50namespace idmap2 {
51
52class Idmap2BinaryTests : public Idmap2Tests {};
53
54static void AssertIdmap(const Idmap& idmap, const std::string& target_apk_path,
55 const std::string& overlay_apk_path) {
56 // check that the idmap file looks reasonable (IdmapTests is responsible for
57 // more in-depth verification)
58 ASSERT_EQ(idmap.GetHeader()->GetMagic(), kIdmapMagic);
59 ASSERT_EQ(idmap.GetHeader()->GetVersion(), kIdmapCurrentVersion);
60 ASSERT_EQ(idmap.GetHeader()->GetTargetPath(), target_apk_path);
61 ASSERT_EQ(idmap.GetHeader()->GetOverlayPath(), overlay_apk_path);
62 ASSERT_EQ(idmap.GetData().size(), 1u);
63}
64
65#define ASSERT_IDMAP(idmap_ref, target_apk_path, overlay_apk_path) \
66 do { \
67 ASSERT_NO_FATAL_FAILURE(AssertIdmap(idmap_ref, target_apk_path, overlay_apk_path)); \
68 } while (0)
69
70TEST_F(Idmap2BinaryTests, Create) {
71 // clang-format off
72 auto result = ExecuteBinary({"idmap2",
73 "create",
74 "--target-apk-path", GetTargetApkPath(),
75 "--overlay-apk-path", GetOverlayApkPath(),
76 "--idmap-path", GetIdmapPath()});
77 // clang-format on
78 ASSERT_THAT(result, NotNull());
79 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
80
81 struct stat st;
82 ASSERT_EQ(stat(GetIdmapPath().c_str(), &st), 0);
83
84 std::stringstream error;
85 std::ifstream fin(GetIdmapPath());
86 std::unique_ptr<const Idmap> idmap = Idmap::FromBinaryStream(fin, error);
87 fin.close();
88
89 ASSERT_THAT(idmap, NotNull());
90 ASSERT_IDMAP(*idmap, GetTargetApkPath(), GetOverlayApkPath());
91
92 unlink(GetIdmapPath().c_str());
93}
94
95TEST_F(Idmap2BinaryTests, Dump) {
96 // clang-format off
97 auto result = ExecuteBinary({"idmap2",
98 "create",
99 "--target-apk-path", GetTargetApkPath(),
100 "--overlay-apk-path", GetOverlayApkPath(),
101 "--idmap-path", GetIdmapPath()});
102 // clang-format on
103 ASSERT_THAT(result, NotNull());
104 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
105
106 // clang-format off
107 result = ExecuteBinary({"idmap2",
108 "dump",
109 "--idmap-path", GetIdmapPath()});
110 // clang-format on
111 ASSERT_THAT(result, NotNull());
112 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
113 ASSERT_NE(result->stdout.find("0x7f010000 -> 0x7f010000 integer/int1"), std::string::npos);
114 ASSERT_NE(result->stdout.find("0x7f020003 -> 0x7f020000 string/str1"), std::string::npos);
115 ASSERT_NE(result->stdout.find("0x7f020005 -> 0x7f020001 string/str3"), std::string::npos);
116 ASSERT_EQ(result->stdout.find("00000210: 007f target package id"), std::string::npos);
117
118 // clang-format off
119 result = ExecuteBinary({"idmap2",
120 "dump",
121 "--verbose",
122 "--idmap-path", GetIdmapPath()});
123 // clang-format on
124 ASSERT_THAT(result, NotNull());
125 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
126 ASSERT_NE(result->stdout.find("00000000: 504d4449 magic"), std::string::npos);
127 ASSERT_NE(result->stdout.find("00000210: 007f target package id"), std::string::npos);
128
129 // clang-format off
130 result = ExecuteBinary({"idmap2",
131 "dump",
132 "--verbose",
133 "--idmap-path", GetTestDataPath() + "/DOES-NOT-EXIST"});
134 // clang-format on
135 ASSERT_THAT(result, NotNull());
136 ASSERT_NE(result->status, EXIT_SUCCESS);
137
138 unlink(GetIdmapPath().c_str());
139}
140
141TEST_F(Idmap2BinaryTests, Scan) {
142 const std::string overlay_static_1_apk_path = GetTestDataPath() + "/overlay/overlay-static-1.apk";
143 const std::string overlay_static_2_apk_path = GetTestDataPath() + "/overlay/overlay-static-2.apk";
144 const std::string idmap_static_1_path =
145 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_1_apk_path);
146 const std::string idmap_static_2_path =
147 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_2_apk_path);
148
149 // single input directory, recursive
150 // clang-format off
151 auto result = ExecuteBinary({"idmap2",
152 "scan",
153 "--input-directory", GetTestDataPath(),
154 "--recursive",
155 "--target-package-name", "test.target",
156 "--target-apk-path", GetTargetApkPath(),
157 "--output-directory", GetTempDirPath()});
158 // clang-format on
159 ASSERT_THAT(result, NotNull());
160 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
161 std::stringstream expected;
162 expected << idmap_static_1_path << std::endl;
163 expected << idmap_static_2_path << std::endl;
164 ASSERT_EQ(result->stdout, expected.str());
165
166 std::stringstream error;
167 auto idmap_static_1_raw_string = utils::ReadFile(idmap_static_1_path);
168 auto idmap_static_1_raw_stream = std::istringstream(*idmap_static_1_raw_string);
169 auto idmap_static_1 = Idmap::FromBinaryStream(idmap_static_1_raw_stream, error);
170 ASSERT_THAT(idmap_static_1, NotNull());
171 ASSERT_IDMAP(*idmap_static_1, GetTargetApkPath(), overlay_static_1_apk_path);
172
173 auto idmap_static_2_raw_string = utils::ReadFile(idmap_static_2_path);
174 auto idmap_static_2_raw_stream = std::istringstream(*idmap_static_2_raw_string);
175 auto idmap_static_2 = Idmap::FromBinaryStream(idmap_static_2_raw_stream, error);
176 ASSERT_THAT(idmap_static_2, NotNull());
177 ASSERT_IDMAP(*idmap_static_2, GetTargetApkPath(), overlay_static_2_apk_path);
178
179 unlink(idmap_static_2_path.c_str());
180 unlink(idmap_static_1_path.c_str());
181
182 // multiple input directories, non-recursive
183 // clang-format off
184 result = ExecuteBinary({"idmap2",
185 "scan",
186 "--input-directory", GetTestDataPath() + "/target",
187 "--input-directory", GetTestDataPath() + "/overlay",
188 "--target-package-name", "test.target",
189 "--target-apk-path", GetTargetApkPath(),
190 "--output-directory", GetTempDirPath()});
191 // clang-format on
192 ASSERT_THAT(result, NotNull());
193 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
194 ASSERT_EQ(result->stdout, expected.str());
195 unlink(idmap_static_2_path.c_str());
196 unlink(idmap_static_1_path.c_str());
197
198 // the same input directory given twice, but no duplicate entries
199 // clang-format off
200 result = ExecuteBinary({"idmap2",
201 "scan",
202 "--input-directory", GetTestDataPath(),
203 "--input-directory", GetTestDataPath(),
204 "--recursive",
205 "--target-package-name", "test.target",
206 "--target-apk-path", GetTargetApkPath(),
207 "--output-directory", GetTempDirPath()});
208 // clang-format on
209 ASSERT_THAT(result, NotNull());
210 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
211 ASSERT_EQ(result->stdout, expected.str());
212 unlink(idmap_static_2_path.c_str());
213 unlink(idmap_static_1_path.c_str());
214
215 // no APKs in input-directory: ok, but no output
216 // clang-format off
217 result = ExecuteBinary({"idmap2",
218 "scan",
219 "--input-directory", GetTempDirPath(),
220 "--target-package-name", "test.target",
221 "--target-apk-path", GetTargetApkPath(),
222 "--output-directory", GetTempDirPath()});
223 // clang-format on
224 ASSERT_THAT(result, NotNull());
225 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
226 ASSERT_EQ(result->stdout, "");
227}
228
229TEST_F(Idmap2BinaryTests, Lookup) {
230 // clang-format off
231 auto result = ExecuteBinary({"idmap2",
232 "create",
233 "--target-apk-path", GetTargetApkPath(),
234 "--overlay-apk-path", GetOverlayApkPath(),
235 "--idmap-path", GetIdmapPath()});
236 // clang-format on
237 ASSERT_THAT(result, NotNull());
238 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
239
240 // clang-format off
241 result = ExecuteBinary({"idmap2",
242 "lookup",
243 "--idmap-path", GetIdmapPath(),
244 "--config", "",
245 "--resid", "0x7f020003"}); // string/str1
246 // clang-format on
247 ASSERT_THAT(result, NotNull());
248 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
249 ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos);
250 ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos);
251
252 // clang-format off
253 result = ExecuteBinary({"idmap2",
254 "lookup",
255 "--idmap-path", GetIdmapPath(),
256 "--config", "",
257 "--resid", "test.target:string/str1"});
258 // clang-format on
259 ASSERT_THAT(result, NotNull());
260 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
261 ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos);
262 ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos);
263
264 // clang-format off
265 result = ExecuteBinary({"idmap2",
266 "lookup",
267 "--idmap-path", GetIdmapPath(),
268 "--config", "sv",
269 "--resid", "test.target:string/str1"});
270 // clang-format on
271 ASSERT_THAT(result, NotNull());
272 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
273 ASSERT_NE(result->stdout.find("overlay-1-sv"), std::string::npos);
274
275 unlink(GetIdmapPath().c_str());
276}
277
278TEST_F(Idmap2BinaryTests, InvalidCommandLineOptions) {
279 const std::string invalid_target_apk_path = GetTestDataPath() + "/DOES-NOT-EXIST";
280
281 // missing mandatory options
282 // clang-format off
283 auto result = ExecuteBinary({"idmap2",
284 "create"});
285 // clang-format on
286 ASSERT_THAT(result, NotNull());
287 ASSERT_NE(result->status, EXIT_SUCCESS);
288
289 // missing argument to option
290 // clang-format off
291 result = ExecuteBinary({"idmap2",
292 "create",
293 "--target-apk-path", GetTargetApkPath(),
294 "--overlay-apk-path", GetOverlayApkPath(),
295 "--idmap-path"});
296 // clang-format on
297 ASSERT_THAT(result, NotNull());
298 ASSERT_NE(result->status, EXIT_SUCCESS);
299
300 // invalid target apk path
301 // clang-format off
302 result = ExecuteBinary({"idmap2",
303 "create",
304 "--target-apk-path", invalid_target_apk_path,
305 "--overlay-apk-path", GetOverlayApkPath(),
306 "--idmap-path", GetIdmapPath()});
307 // clang-format on
308 ASSERT_THAT(result, NotNull());
309 ASSERT_NE(result->status, EXIT_SUCCESS);
310}
311
312} // namespace idmap2
313} // namespace android