blob: eba102da0763186b826455ef87a7a1a3f1fa1ca1 [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
Winson62ac8b52019-12-04 08:36:48 -080037#include "R.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070038#include "TestHelpers.h"
39#include "androidfw/PosixUtils.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020040#include "gmock/gmock.h"
41#include "gtest/gtest.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020042#include "idmap2/FileUtils.h"
43#include "idmap2/Idmap.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070044#include "private/android_filesystem_config.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020045
46using ::android::util::ExecuteBinary;
47using ::testing::NotNull;
48
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010049namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020050
51class Idmap2BinaryTests : public Idmap2Tests {};
52
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010053namespace {
54
55void AssertIdmap(const Idmap& idmap, const std::string& target_apk_path,
56 const std::string& overlay_apk_path) {
Mårten Kongstad02751232018-04-27 13:16:32 +020057 // check that the idmap file looks reasonable (IdmapTests is responsible for
58 // more in-depth verification)
59 ASSERT_EQ(idmap.GetHeader()->GetMagic(), kIdmapMagic);
60 ASSERT_EQ(idmap.GetHeader()->GetVersion(), kIdmapCurrentVersion);
61 ASSERT_EQ(idmap.GetHeader()->GetTargetPath(), target_apk_path);
62 ASSERT_EQ(idmap.GetHeader()->GetOverlayPath(), overlay_apk_path);
Mårten Kongstadb8779022018-11-29 09:53:17 +010063 ASSERT_EQ(idmap.GetData().size(), 1U);
Mårten Kongstad02751232018-04-27 13:16:32 +020064}
65
66#define ASSERT_IDMAP(idmap_ref, target_apk_path, overlay_apk_path) \
67 do { \
68 ASSERT_NO_FATAL_FAILURE(AssertIdmap(idmap_ref, target_apk_path, overlay_apk_path)); \
69 } while (0)
70
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010071#ifdef __ANDROID__
72#define SKIP_TEST_IF_CANT_EXEC_IDMAP2 \
73 do { \
74 const uid_t uid = getuid(); \
75 if (uid != AID_ROOT && uid != AID_SYSTEM) { \
76 GTEST_SKIP(); \
77 } \
78 } while (0)
79#else
80#define SKIP_TEST_IF_CANT_EXEC_IDMAP2
81#endif
82
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010083} // namespace
84
Mårten Kongstad02751232018-04-27 13:16:32 +020085TEST_F(Idmap2BinaryTests, Create) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010086 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
87
Mårten Kongstad02751232018-04-27 13:16:32 +020088 // clang-format off
89 auto result = ExecuteBinary({"idmap2",
90 "create",
91 "--target-apk-path", GetTargetApkPath(),
92 "--overlay-apk-path", GetOverlayApkPath(),
93 "--idmap-path", GetIdmapPath()});
94 // clang-format on
95 ASSERT_THAT(result, NotNull());
96 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
97
98 struct stat st;
99 ASSERT_EQ(stat(GetIdmapPath().c_str(), &st), 0);
100
Mårten Kongstad02751232018-04-27 13:16:32 +0200101 std::ifstream fin(GetIdmapPath());
Mårten Kongstadce424902019-03-01 08:35:37 +0100102 const auto idmap = Idmap::FromBinaryStream(fin);
Mårten Kongstad02751232018-04-27 13:16:32 +0200103 fin.close();
104
Mårten Kongstadce424902019-03-01 08:35:37 +0100105 ASSERT_TRUE(idmap);
106 ASSERT_IDMAP(**idmap, GetTargetApkPath(), GetOverlayApkPath());
Mårten Kongstad02751232018-04-27 13:16:32 +0200107
108 unlink(GetIdmapPath().c_str());
109}
110
111TEST_F(Idmap2BinaryTests, Dump) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100112 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
113
Mårten Kongstad02751232018-04-27 13:16:32 +0200114 // clang-format off
115 auto result = ExecuteBinary({"idmap2",
116 "create",
117 "--target-apk-path", GetTargetApkPath(),
118 "--overlay-apk-path", GetOverlayApkPath(),
119 "--idmap-path", GetIdmapPath()});
120 // clang-format on
121 ASSERT_THAT(result, NotNull());
122 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
123
124 // clang-format off
125 result = ExecuteBinary({"idmap2",
126 "dump",
127 "--idmap-path", GetIdmapPath()});
128 // clang-format on
129 ASSERT_THAT(result, NotNull());
130 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700131 ASSERT_NE(result->stdout.find(R::target::integer::literal::int1 + " -> 0x7f010000"),
Winson62ac8b52019-12-04 08:36:48 -0800132 std::string::npos);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700133 ASSERT_NE(result->stdout.find(R::target::string::literal::str1 + " -> 0x7f020000"),
Winson62ac8b52019-12-04 08:36:48 -0800134 std::string::npos);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700135 ASSERT_NE(result->stdout.find(R::target::string::literal::str3 + " -> 0x7f020001"),
Winson62ac8b52019-12-04 08:36:48 -0800136 std::string::npos);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700137 ASSERT_NE(result->stdout.find(R::target::string::literal::str4 + " -> 0x7f020002"),
Winson62ac8b52019-12-04 08:36:48 -0800138 std::string::npos);
Mårten Kongstad02751232018-04-27 13:16:32 +0200139
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);
Mårten Kongstad02751232018-04-27 13:16:32 +0200149
150 // clang-format off
151 result = ExecuteBinary({"idmap2",
152 "dump",
153 "--verbose",
154 "--idmap-path", GetTestDataPath() + "/DOES-NOT-EXIST"});
155 // clang-format on
156 ASSERT_THAT(result, NotNull());
157 ASSERT_NE(result->status, EXIT_SUCCESS);
158
159 unlink(GetIdmapPath().c_str());
160}
161
162TEST_F(Idmap2BinaryTests, Scan) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100163 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
164
Ryan Mitchell19823452019-01-29 12:01:24 -0800165 const std::string overlay_static_no_name_apk_path =
166 GetTestDataPath() + "/overlay/overlay-no-name-static.apk";
Mårten Kongstad02751232018-04-27 13:16:32 +0200167 const std::string overlay_static_1_apk_path = GetTestDataPath() + "/overlay/overlay-static-1.apk";
168 const std::string overlay_static_2_apk_path = GetTestDataPath() + "/overlay/overlay-static-2.apk";
Ryan Mitchell19823452019-01-29 12:01:24 -0800169 const std::string idmap_static_no_name_path =
170 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_no_name_apk_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200171 const std::string idmap_static_1_path =
172 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_1_apk_path);
173 const std::string idmap_static_2_path =
174 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_2_apk_path);
175
176 // single input directory, recursive
177 // clang-format off
178 auto result = ExecuteBinary({"idmap2",
179 "scan",
180 "--input-directory", GetTestDataPath(),
181 "--recursive",
182 "--target-package-name", "test.target",
183 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800184 "--output-directory", GetTempDirPath(),
185 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200186 // clang-format on
187 ASSERT_THAT(result, NotNull());
188 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
189 std::stringstream expected;
Ryan Mitchell19823452019-01-29 12:01:24 -0800190 expected << idmap_static_no_name_path << std::endl;
Mårten Kongstad02751232018-04-27 13:16:32 +0200191 expected << idmap_static_1_path << std::endl;
192 expected << idmap_static_2_path << std::endl;
193 ASSERT_EQ(result->stdout, expected.str());
194
Ryan Mitchell19823452019-01-29 12:01:24 -0800195 auto idmap_static_no_name_raw_string = utils::ReadFile(idmap_static_no_name_path);
196 auto idmap_static_no_name_raw_stream = std::istringstream(*idmap_static_no_name_raw_string);
Mårten Kongstadce424902019-03-01 08:35:37 +0100197 auto idmap_static_no_name = Idmap::FromBinaryStream(idmap_static_no_name_raw_stream);
198 ASSERT_TRUE(idmap_static_no_name);
199 ASSERT_IDMAP(**idmap_static_no_name, GetTargetApkPath(), overlay_static_no_name_apk_path);
Ryan Mitchell19823452019-01-29 12:01:24 -0800200
Mårten Kongstad02751232018-04-27 13:16:32 +0200201 auto idmap_static_1_raw_string = utils::ReadFile(idmap_static_1_path);
202 auto idmap_static_1_raw_stream = std::istringstream(*idmap_static_1_raw_string);
Mårten Kongstadce424902019-03-01 08:35:37 +0100203 auto idmap_static_1 = Idmap::FromBinaryStream(idmap_static_1_raw_stream);
204 ASSERT_TRUE(idmap_static_1);
205 ASSERT_IDMAP(**idmap_static_1, GetTargetApkPath(), overlay_static_1_apk_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200206
207 auto idmap_static_2_raw_string = utils::ReadFile(idmap_static_2_path);
208 auto idmap_static_2_raw_stream = std::istringstream(*idmap_static_2_raw_string);
Mårten Kongstadce424902019-03-01 08:35:37 +0100209 auto idmap_static_2 = Idmap::FromBinaryStream(idmap_static_2_raw_stream);
210 ASSERT_TRUE(idmap_static_2);
211 ASSERT_IDMAP(**idmap_static_2, GetTargetApkPath(), overlay_static_2_apk_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200212
Ryan Mitchell19823452019-01-29 12:01:24 -0800213 unlink(idmap_static_no_name_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200214 unlink(idmap_static_2_path.c_str());
215 unlink(idmap_static_1_path.c_str());
216
217 // multiple input directories, non-recursive
218 // clang-format off
219 result = ExecuteBinary({"idmap2",
220 "scan",
221 "--input-directory", GetTestDataPath() + "/target",
222 "--input-directory", GetTestDataPath() + "/overlay",
223 "--target-package-name", "test.target",
224 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800225 "--output-directory", GetTempDirPath(),
226 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200227 // clang-format on
228 ASSERT_THAT(result, NotNull());
229 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
230 ASSERT_EQ(result->stdout, expected.str());
Ryan Mitchell19823452019-01-29 12:01:24 -0800231 unlink(idmap_static_no_name_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200232 unlink(idmap_static_2_path.c_str());
233 unlink(idmap_static_1_path.c_str());
234
235 // the same input directory given twice, but no duplicate entries
236 // clang-format off
237 result = ExecuteBinary({"idmap2",
238 "scan",
239 "--input-directory", GetTestDataPath(),
240 "--input-directory", GetTestDataPath(),
241 "--recursive",
242 "--target-package-name", "test.target",
243 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800244 "--output-directory", GetTempDirPath(),
245 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200246 // clang-format on
247 ASSERT_THAT(result, NotNull());
248 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
249 ASSERT_EQ(result->stdout, expected.str());
Ryan Mitchell19823452019-01-29 12:01:24 -0800250 unlink(idmap_static_no_name_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200251 unlink(idmap_static_2_path.c_str());
252 unlink(idmap_static_1_path.c_str());
253
254 // no APKs in input-directory: ok, but no output
255 // clang-format off
256 result = ExecuteBinary({"idmap2",
257 "scan",
258 "--input-directory", GetTempDirPath(),
259 "--target-package-name", "test.target",
260 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800261 "--output-directory", GetTempDirPath(),
262 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200263 // clang-format on
264 ASSERT_THAT(result, NotNull());
265 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
266 ASSERT_EQ(result->stdout, "");
Ryan Mitchell0503fa52019-04-12 12:29:36 -0700267
268 // the signature idmap failing to generate should not cause scanning to fail
269 // clang-format off
270 result = ExecuteBinary({"idmap2",
271 "scan",
272 "--input-directory", GetTestDataPath(),
273 "--recursive",
274 "--target-package-name", "test.target",
275 "--target-apk-path", GetTargetApkPath(),
276 "--output-directory", GetTempDirPath(),
277 "--override-policy", "public"});
278 // clang-format on
279 ASSERT_THAT(result, NotNull());
280 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
281 ASSERT_EQ(result->stdout, expected.str());
282 unlink(idmap_static_no_name_path.c_str());
283 unlink(idmap_static_2_path.c_str());
284 unlink(idmap_static_1_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200285}
286
287TEST_F(Idmap2BinaryTests, Lookup) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700288 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
289
290 // clang-format off
291 auto result = ExecuteBinary({"idmap2",
292 "create",
293 "--target-apk-path", GetTargetApkPath(),
294 "--overlay-apk-path", GetOverlayApkPath(),
295 "--idmap-path", GetIdmapPath()});
296 // clang-format on
297 ASSERT_THAT(result, NotNull());
298 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
299
300 // clang-format off
301 result = ExecuteBinary({"idmap2",
302 "lookup",
303 "--idmap-path", GetIdmapPath(),
304 "--config", "",
Winson62ac8b52019-12-04 08:36:48 -0800305 "--resid", R::target::string::literal::str1});
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700306 // clang-format on
307 ASSERT_THAT(result, NotNull());
308 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
309 ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos);
310 ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos);
311
312 // clang-format off
313 result = ExecuteBinary({"idmap2",
314 "lookup",
315 "--idmap-path", GetIdmapPath(),
316 "--config", "",
317 "--resid", "test.target:string/str1"});
318 // clang-format on
319 ASSERT_THAT(result, NotNull());
320 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
321 ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos);
322 ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos);
323
324 // clang-format off
325 result = ExecuteBinary({"idmap2",
326 "lookup",
327 "--idmap-path", GetIdmapPath(),
328 "--config", "sv",
329 "--resid", "test.target:string/str1"});
330 // clang-format on
331 ASSERT_THAT(result, NotNull());
332 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
333 ASSERT_NE(result->stdout.find("overlay-1-sv"), std::string::npos);
334
335 unlink(GetIdmapPath().c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200336}
337
338TEST_F(Idmap2BinaryTests, InvalidCommandLineOptions) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100339 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
340
Mårten Kongstad02751232018-04-27 13:16:32 +0200341 const std::string invalid_target_apk_path = GetTestDataPath() + "/DOES-NOT-EXIST";
342
343 // missing mandatory options
344 // clang-format off
345 auto result = ExecuteBinary({"idmap2",
346 "create"});
347 // clang-format on
348 ASSERT_THAT(result, NotNull());
349 ASSERT_NE(result->status, EXIT_SUCCESS);
350
351 // missing argument to option
352 // clang-format off
353 result = ExecuteBinary({"idmap2",
354 "create",
355 "--target-apk-path", GetTargetApkPath(),
356 "--overlay-apk-path", GetOverlayApkPath(),
357 "--idmap-path"});
358 // clang-format on
359 ASSERT_THAT(result, NotNull());
360 ASSERT_NE(result->status, EXIT_SUCCESS);
361
362 // invalid target apk path
363 // clang-format off
364 result = ExecuteBinary({"idmap2",
365 "create",
366 "--target-apk-path", invalid_target_apk_path,
367 "--overlay-apk-path", GetOverlayApkPath(),
368 "--idmap-path", GetIdmapPath()});
369 // clang-format on
370 ASSERT_THAT(result, NotNull());
371 ASSERT_NE(result->status, EXIT_SUCCESS);
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800372
373 // unknown policy
374 // clang-format off
375 result = ExecuteBinary({"idmap2",
376 "create",
377 "--target-apk-path", GetTargetApkPath(),
378 "--overlay-apk-path", GetOverlayApkPath(),
379 "--idmap-path", GetIdmapPath(),
380 "--policy", "this-does-not-exist"});
381 // clang-format on
382 ASSERT_THAT(result, NotNull());
383 ASSERT_NE(result->status, EXIT_SUCCESS);
Mårten Kongstad02751232018-04-27 13:16:32 +0200384}
385
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100386} // namespace android::idmap2