blob: 1216f9ec736a1ba4057c2b3291d5c10690ebede1 [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"
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010041#include "private/android_filesystem_config.h"
Mårten Kongstadd10d06d2019-01-07 17:26:25 -080042
Mårten Kongstad02751232018-04-27 13:16:32 +020043#include "idmap2/FileUtils.h"
44#include "idmap2/Idmap.h"
45
46#include "TestHelpers.h"
47
48using ::android::util::ExecuteBinary;
49using ::testing::NotNull;
50
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010051namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020052
53class Idmap2BinaryTests : public Idmap2Tests {};
54
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010055namespace {
56
57void AssertIdmap(const Idmap& idmap, const std::string& target_apk_path,
58 const std::string& overlay_apk_path) {
Mårten Kongstad02751232018-04-27 13:16:32 +020059 // check that the idmap file looks reasonable (IdmapTests is responsible for
60 // more in-depth verification)
61 ASSERT_EQ(idmap.GetHeader()->GetMagic(), kIdmapMagic);
62 ASSERT_EQ(idmap.GetHeader()->GetVersion(), kIdmapCurrentVersion);
63 ASSERT_EQ(idmap.GetHeader()->GetTargetPath(), target_apk_path);
64 ASSERT_EQ(idmap.GetHeader()->GetOverlayPath(), overlay_apk_path);
Mårten Kongstadb8779022018-11-29 09:53:17 +010065 ASSERT_EQ(idmap.GetData().size(), 1U);
Mårten Kongstad02751232018-04-27 13:16:32 +020066}
67
68#define ASSERT_IDMAP(idmap_ref, target_apk_path, overlay_apk_path) \
69 do { \
70 ASSERT_NO_FATAL_FAILURE(AssertIdmap(idmap_ref, target_apk_path, overlay_apk_path)); \
71 } while (0)
72
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010073#ifdef __ANDROID__
74#define SKIP_TEST_IF_CANT_EXEC_IDMAP2 \
75 do { \
76 const uid_t uid = getuid(); \
77 if (uid != AID_ROOT && uid != AID_SYSTEM) { \
78 GTEST_SKIP(); \
79 } \
80 } while (0)
81#else
82#define SKIP_TEST_IF_CANT_EXEC_IDMAP2
83#endif
84
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010085} // namespace
86
Mårten Kongstad02751232018-04-27 13:16:32 +020087TEST_F(Idmap2BinaryTests, Create) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010088 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
89
Mårten Kongstad02751232018-04-27 13:16:32 +020090 // clang-format off
91 auto result = ExecuteBinary({"idmap2",
92 "create",
93 "--target-apk-path", GetTargetApkPath(),
94 "--overlay-apk-path", GetOverlayApkPath(),
95 "--idmap-path", GetIdmapPath()});
96 // clang-format on
97 ASSERT_THAT(result, NotNull());
98 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
99
100 struct stat st;
101 ASSERT_EQ(stat(GetIdmapPath().c_str(), &st), 0);
102
103 std::stringstream error;
104 std::ifstream fin(GetIdmapPath());
105 std::unique_ptr<const Idmap> idmap = Idmap::FromBinaryStream(fin, error);
106 fin.close();
107
108 ASSERT_THAT(idmap, NotNull());
109 ASSERT_IDMAP(*idmap, GetTargetApkPath(), GetOverlayApkPath());
110
111 unlink(GetIdmapPath().c_str());
112}
113
114TEST_F(Idmap2BinaryTests, Dump) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100115 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
116
Mårten Kongstad02751232018-04-27 13:16:32 +0200117 // clang-format off
118 auto result = ExecuteBinary({"idmap2",
119 "create",
120 "--target-apk-path", GetTargetApkPath(),
121 "--overlay-apk-path", GetOverlayApkPath(),
122 "--idmap-path", GetIdmapPath()});
123 // clang-format on
124 ASSERT_THAT(result, NotNull());
125 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
126
127 // clang-format off
128 result = ExecuteBinary({"idmap2",
129 "dump",
130 "--idmap-path", GetIdmapPath()});
131 // clang-format on
132 ASSERT_THAT(result, NotNull());
133 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
134 ASSERT_NE(result->stdout.find("0x7f010000 -> 0x7f010000 integer/int1"), std::string::npos);
Ryan Mitchella3628462019-01-14 12:19:40 -0800135 ASSERT_NE(result->stdout.find("0x7f020009 -> 0x7f020000 string/str1"), std::string::npos);
136 ASSERT_NE(result->stdout.find("0x7f02000b -> 0x7f020001 string/str3"), std::string::npos);
137 ASSERT_NE(result->stdout.find("0x7f02000c -> 0x7f020002 string/str4"), std::string::npos);
Mårten Kongstad02751232018-04-27 13:16:32 +0200138 ASSERT_EQ(result->stdout.find("00000210: 007f target package id"), std::string::npos);
139
140 // clang-format off
141 result = ExecuteBinary({"idmap2",
142 "dump",
143 "--verbose",
144 "--idmap-path", GetIdmapPath()});
145 // clang-format on
146 ASSERT_THAT(result, NotNull());
147 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
148 ASSERT_NE(result->stdout.find("00000000: 504d4449 magic"), std::string::npos);
149 ASSERT_NE(result->stdout.find("00000210: 007f target package id"), std::string::npos);
150
151 // clang-format off
152 result = ExecuteBinary({"idmap2",
153 "dump",
154 "--verbose",
155 "--idmap-path", GetTestDataPath() + "/DOES-NOT-EXIST"});
156 // clang-format on
157 ASSERT_THAT(result, NotNull());
158 ASSERT_NE(result->status, EXIT_SUCCESS);
159
160 unlink(GetIdmapPath().c_str());
161}
162
163TEST_F(Idmap2BinaryTests, Scan) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100164 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
165
Ryan Mitchell19823452019-01-29 12:01:24 -0800166 const std::string overlay_static_no_name_apk_path =
167 GetTestDataPath() + "/overlay/overlay-no-name-static.apk";
Mårten Kongstad02751232018-04-27 13:16:32 +0200168 const std::string overlay_static_1_apk_path = GetTestDataPath() + "/overlay/overlay-static-1.apk";
169 const std::string overlay_static_2_apk_path = GetTestDataPath() + "/overlay/overlay-static-2.apk";
Ryan Mitchell19823452019-01-29 12:01:24 -0800170 const std::string idmap_static_no_name_path =
171 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_no_name_apk_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200172 const std::string idmap_static_1_path =
173 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_1_apk_path);
174 const std::string idmap_static_2_path =
175 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_2_apk_path);
176
177 // single input directory, recursive
178 // clang-format off
179 auto result = ExecuteBinary({"idmap2",
180 "scan",
181 "--input-directory", GetTestDataPath(),
182 "--recursive",
183 "--target-package-name", "test.target",
184 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800185 "--output-directory", GetTempDirPath(),
186 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200187 // clang-format on
188 ASSERT_THAT(result, NotNull());
189 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
190 std::stringstream expected;
Ryan Mitchell19823452019-01-29 12:01:24 -0800191 expected << idmap_static_no_name_path << std::endl;
Mårten Kongstad02751232018-04-27 13:16:32 +0200192 expected << idmap_static_1_path << std::endl;
193 expected << idmap_static_2_path << std::endl;
194 ASSERT_EQ(result->stdout, expected.str());
195
196 std::stringstream error;
Ryan Mitchell19823452019-01-29 12:01:24 -0800197 auto idmap_static_no_name_raw_string = utils::ReadFile(idmap_static_no_name_path);
198 auto idmap_static_no_name_raw_stream = std::istringstream(*idmap_static_no_name_raw_string);
199 auto idmap_static_no_name = Idmap::FromBinaryStream(idmap_static_no_name_raw_stream, error);
200 ASSERT_THAT(idmap_static_no_name, NotNull());
201 ASSERT_IDMAP(*idmap_static_no_name, GetTargetApkPath(), overlay_static_no_name_apk_path);
202
Mårten Kongstad02751232018-04-27 13:16:32 +0200203 auto idmap_static_1_raw_string = utils::ReadFile(idmap_static_1_path);
204 auto idmap_static_1_raw_stream = std::istringstream(*idmap_static_1_raw_string);
205 auto idmap_static_1 = Idmap::FromBinaryStream(idmap_static_1_raw_stream, error);
206 ASSERT_THAT(idmap_static_1, NotNull());
207 ASSERT_IDMAP(*idmap_static_1, GetTargetApkPath(), overlay_static_1_apk_path);
208
209 auto idmap_static_2_raw_string = utils::ReadFile(idmap_static_2_path);
210 auto idmap_static_2_raw_stream = std::istringstream(*idmap_static_2_raw_string);
211 auto idmap_static_2 = Idmap::FromBinaryStream(idmap_static_2_raw_stream, error);
212 ASSERT_THAT(idmap_static_2, NotNull());
213 ASSERT_IDMAP(*idmap_static_2, GetTargetApkPath(), overlay_static_2_apk_path);
214
Ryan Mitchell19823452019-01-29 12:01:24 -0800215 unlink(idmap_static_no_name_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200216 unlink(idmap_static_2_path.c_str());
217 unlink(idmap_static_1_path.c_str());
218
219 // multiple input directories, non-recursive
220 // clang-format off
221 result = ExecuteBinary({"idmap2",
222 "scan",
223 "--input-directory", GetTestDataPath() + "/target",
224 "--input-directory", GetTestDataPath() + "/overlay",
225 "--target-package-name", "test.target",
226 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800227 "--output-directory", GetTempDirPath(),
228 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200229 // clang-format on
230 ASSERT_THAT(result, NotNull());
231 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
232 ASSERT_EQ(result->stdout, expected.str());
Ryan Mitchell19823452019-01-29 12:01:24 -0800233 unlink(idmap_static_no_name_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200234 unlink(idmap_static_2_path.c_str());
235 unlink(idmap_static_1_path.c_str());
236
237 // the same input directory given twice, but no duplicate entries
238 // clang-format off
239 result = ExecuteBinary({"idmap2",
240 "scan",
241 "--input-directory", GetTestDataPath(),
242 "--input-directory", GetTestDataPath(),
243 "--recursive",
244 "--target-package-name", "test.target",
245 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800246 "--output-directory", GetTempDirPath(),
247 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200248 // clang-format on
249 ASSERT_THAT(result, NotNull());
250 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
251 ASSERT_EQ(result->stdout, expected.str());
Ryan Mitchell19823452019-01-29 12:01:24 -0800252 unlink(idmap_static_no_name_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200253 unlink(idmap_static_2_path.c_str());
254 unlink(idmap_static_1_path.c_str());
255
256 // no APKs in input-directory: ok, but no output
257 // clang-format off
258 result = ExecuteBinary({"idmap2",
259 "scan",
260 "--input-directory", GetTempDirPath(),
261 "--target-package-name", "test.target",
262 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800263 "--output-directory", GetTempDirPath(),
264 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200265 // clang-format on
266 ASSERT_THAT(result, NotNull());
267 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
268 ASSERT_EQ(result->stdout, "");
269}
270
271TEST_F(Idmap2BinaryTests, Lookup) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100272 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
273
Mårten Kongstad02751232018-04-27 13:16:32 +0200274 // clang-format off
275 auto result = ExecuteBinary({"idmap2",
276 "create",
277 "--target-apk-path", GetTargetApkPath(),
278 "--overlay-apk-path", GetOverlayApkPath(),
279 "--idmap-path", GetIdmapPath()});
280 // clang-format on
281 ASSERT_THAT(result, NotNull());
282 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
283
284 // clang-format off
285 result = ExecuteBinary({"idmap2",
286 "lookup",
287 "--idmap-path", GetIdmapPath(),
288 "--config", "",
Ryan Mitchella3628462019-01-14 12:19:40 -0800289 "--resid", "0x7f020009"}); // string/str1
Mårten Kongstad02751232018-04-27 13:16:32 +0200290 // clang-format on
291 ASSERT_THAT(result, NotNull());
292 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
293 ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos);
294 ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos);
295
296 // clang-format off
297 result = ExecuteBinary({"idmap2",
298 "lookup",
299 "--idmap-path", GetIdmapPath(),
300 "--config", "",
301 "--resid", "test.target:string/str1"});
302 // clang-format on
303 ASSERT_THAT(result, NotNull());
304 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
305 ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos);
306 ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos);
307
308 // clang-format off
309 result = ExecuteBinary({"idmap2",
310 "lookup",
311 "--idmap-path", GetIdmapPath(),
312 "--config", "sv",
313 "--resid", "test.target:string/str1"});
314 // clang-format on
315 ASSERT_THAT(result, NotNull());
316 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
317 ASSERT_NE(result->stdout.find("overlay-1-sv"), std::string::npos);
318
319 unlink(GetIdmapPath().c_str());
320}
321
322TEST_F(Idmap2BinaryTests, InvalidCommandLineOptions) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100323 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
324
Mårten Kongstad02751232018-04-27 13:16:32 +0200325 const std::string invalid_target_apk_path = GetTestDataPath() + "/DOES-NOT-EXIST";
326
327 // missing mandatory options
328 // clang-format off
329 auto result = ExecuteBinary({"idmap2",
330 "create"});
331 // clang-format on
332 ASSERT_THAT(result, NotNull());
333 ASSERT_NE(result->status, EXIT_SUCCESS);
334
335 // missing argument to option
336 // clang-format off
337 result = ExecuteBinary({"idmap2",
338 "create",
339 "--target-apk-path", GetTargetApkPath(),
340 "--overlay-apk-path", GetOverlayApkPath(),
341 "--idmap-path"});
342 // clang-format on
343 ASSERT_THAT(result, NotNull());
344 ASSERT_NE(result->status, EXIT_SUCCESS);
345
346 // invalid target apk path
347 // clang-format off
348 result = ExecuteBinary({"idmap2",
349 "create",
350 "--target-apk-path", invalid_target_apk_path,
351 "--overlay-apk-path", GetOverlayApkPath(),
352 "--idmap-path", GetIdmapPath()});
353 // clang-format on
354 ASSERT_THAT(result, NotNull());
355 ASSERT_NE(result->status, EXIT_SUCCESS);
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800356
357 // unknown policy
358 // clang-format off
359 result = ExecuteBinary({"idmap2",
360 "create",
361 "--target-apk-path", GetTargetApkPath(),
362 "--overlay-apk-path", GetOverlayApkPath(),
363 "--idmap-path", GetIdmapPath(),
364 "--policy", "this-does-not-exist"});
365 // clang-format on
366 ASSERT_THAT(result, NotNull());
367 ASSERT_NE(result->status, EXIT_SUCCESS);
Mårten Kongstad02751232018-04-27 13:16:32 +0200368}
369
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100370} // namespace android::idmap2