blob: 499eb99af290879ad49a418b72d33075e3b068eb [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
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070037#include "TestHelpers.h"
38#include "androidfw/PosixUtils.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020039#include "gmock/gmock.h"
40#include "gtest/gtest.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020041#include "idmap2/FileUtils.h"
42#include "idmap2/Idmap.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070043#include "private/android_filesystem_config.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020044
45using ::android::util::ExecuteBinary;
46using ::testing::NotNull;
47
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010048namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020049
50class Idmap2BinaryTests : public Idmap2Tests {};
51
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010052namespace {
53
54void AssertIdmap(const Idmap& idmap, const std::string& target_apk_path,
55 const std::string& overlay_apk_path) {
Mårten Kongstad02751232018-04-27 13:16:32 +020056 // 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);
Mårten Kongstadb8779022018-11-29 09:53:17 +010062 ASSERT_EQ(idmap.GetData().size(), 1U);
Mårten Kongstad02751232018-04-27 13:16:32 +020063}
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
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010070#ifdef __ANDROID__
71#define SKIP_TEST_IF_CANT_EXEC_IDMAP2 \
72 do { \
73 const uid_t uid = getuid(); \
74 if (uid != AID_ROOT && uid != AID_SYSTEM) { \
75 GTEST_SKIP(); \
76 } \
77 } while (0)
78#else
79#define SKIP_TEST_IF_CANT_EXEC_IDMAP2
80#endif
81
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010082} // namespace
83
Mårten Kongstad02751232018-04-27 13:16:32 +020084TEST_F(Idmap2BinaryTests, Create) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010085 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
86
Mårten Kongstad02751232018-04-27 13:16:32 +020087 // clang-format off
88 auto result = ExecuteBinary({"idmap2",
89 "create",
90 "--target-apk-path", GetTargetApkPath(),
91 "--overlay-apk-path", GetOverlayApkPath(),
92 "--idmap-path", GetIdmapPath()});
93 // clang-format on
94 ASSERT_THAT(result, NotNull());
95 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
96
97 struct stat st;
98 ASSERT_EQ(stat(GetIdmapPath().c_str(), &st), 0);
99
Mårten Kongstad02751232018-04-27 13:16:32 +0200100 std::ifstream fin(GetIdmapPath());
Mårten Kongstadce424902019-03-01 08:35:37 +0100101 const auto idmap = Idmap::FromBinaryStream(fin);
Mårten Kongstad02751232018-04-27 13:16:32 +0200102 fin.close();
103
Mårten Kongstadce424902019-03-01 08:35:37 +0100104 ASSERT_TRUE(idmap);
105 ASSERT_IDMAP(**idmap, GetTargetApkPath(), GetOverlayApkPath());
Mårten Kongstad02751232018-04-27 13:16:32 +0200106
107 unlink(GetIdmapPath().c_str());
108}
109
110TEST_F(Idmap2BinaryTests, Dump) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100111 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
112
Mårten Kongstad02751232018-04-27 13:16:32 +0200113 // clang-format off
114 auto result = ExecuteBinary({"idmap2",
115 "create",
116 "--target-apk-path", GetTargetApkPath(),
117 "--overlay-apk-path", GetOverlayApkPath(),
118 "--idmap-path", GetIdmapPath()});
119 // clang-format on
120 ASSERT_THAT(result, NotNull());
121 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
122
123 // clang-format off
124 result = ExecuteBinary({"idmap2",
125 "dump",
126 "--idmap-path", GetIdmapPath()});
127 // clang-format on
128 ASSERT_THAT(result, NotNull());
129 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
130 ASSERT_NE(result->stdout.find("0x7f010000 -> 0x7f010000 integer/int1"), std::string::npos);
Ryan Mitchell939df092019-04-09 17:13:50 -0700131 ASSERT_NE(result->stdout.find("0x7f02000c -> 0x7f020000 string/str1"), std::string::npos);
132 ASSERT_NE(result->stdout.find("0x7f02000e -> 0x7f020001 string/str3"), std::string::npos);
133 ASSERT_NE(result->stdout.find("0x7f02000f -> 0x7f020002 string/str4"), std::string::npos);
Mårten Kongstad02751232018-04-27 13:16:32 +0200134
135 // clang-format off
136 result = ExecuteBinary({"idmap2",
137 "dump",
138 "--verbose",
139 "--idmap-path", GetIdmapPath()});
140 // clang-format on
141 ASSERT_THAT(result, NotNull());
142 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
143 ASSERT_NE(result->stdout.find("00000000: 504d4449 magic"), std::string::npos);
144 ASSERT_NE(result->stdout.find("00000210: 007f target package id"), std::string::npos);
145
146 // clang-format off
147 result = ExecuteBinary({"idmap2",
148 "dump",
149 "--verbose",
150 "--idmap-path", GetTestDataPath() + "/DOES-NOT-EXIST"});
151 // clang-format on
152 ASSERT_THAT(result, NotNull());
153 ASSERT_NE(result->status, EXIT_SUCCESS);
154
155 unlink(GetIdmapPath().c_str());
156}
157
158TEST_F(Idmap2BinaryTests, Scan) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100159 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
160
Ryan Mitchell19823452019-01-29 12:01:24 -0800161 const std::string overlay_static_no_name_apk_path =
162 GetTestDataPath() + "/overlay/overlay-no-name-static.apk";
Mårten Kongstad02751232018-04-27 13:16:32 +0200163 const std::string overlay_static_1_apk_path = GetTestDataPath() + "/overlay/overlay-static-1.apk";
164 const std::string overlay_static_2_apk_path = GetTestDataPath() + "/overlay/overlay-static-2.apk";
Ryan Mitchell19823452019-01-29 12:01:24 -0800165 const std::string idmap_static_no_name_path =
166 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_no_name_apk_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200167 const std::string idmap_static_1_path =
168 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_1_apk_path);
169 const std::string idmap_static_2_path =
170 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_2_apk_path);
171
172 // single input directory, recursive
173 // clang-format off
174 auto result = ExecuteBinary({"idmap2",
175 "scan",
176 "--input-directory", GetTestDataPath(),
177 "--recursive",
178 "--target-package-name", "test.target",
179 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800180 "--output-directory", GetTempDirPath(),
181 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200182 // clang-format on
183 ASSERT_THAT(result, NotNull());
184 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
185 std::stringstream expected;
Ryan Mitchell19823452019-01-29 12:01:24 -0800186 expected << idmap_static_no_name_path << std::endl;
Mårten Kongstad02751232018-04-27 13:16:32 +0200187 expected << idmap_static_1_path << std::endl;
188 expected << idmap_static_2_path << std::endl;
189 ASSERT_EQ(result->stdout, expected.str());
190
Ryan Mitchell19823452019-01-29 12:01:24 -0800191 auto idmap_static_no_name_raw_string = utils::ReadFile(idmap_static_no_name_path);
192 auto idmap_static_no_name_raw_stream = std::istringstream(*idmap_static_no_name_raw_string);
Mårten Kongstadce424902019-03-01 08:35:37 +0100193 auto idmap_static_no_name = Idmap::FromBinaryStream(idmap_static_no_name_raw_stream);
194 ASSERT_TRUE(idmap_static_no_name);
195 ASSERT_IDMAP(**idmap_static_no_name, GetTargetApkPath(), overlay_static_no_name_apk_path);
Ryan Mitchell19823452019-01-29 12:01:24 -0800196
Mårten Kongstad02751232018-04-27 13:16:32 +0200197 auto idmap_static_1_raw_string = utils::ReadFile(idmap_static_1_path);
198 auto idmap_static_1_raw_stream = std::istringstream(*idmap_static_1_raw_string);
Mårten Kongstadce424902019-03-01 08:35:37 +0100199 auto idmap_static_1 = Idmap::FromBinaryStream(idmap_static_1_raw_stream);
200 ASSERT_TRUE(idmap_static_1);
201 ASSERT_IDMAP(**idmap_static_1, GetTargetApkPath(), overlay_static_1_apk_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200202
203 auto idmap_static_2_raw_string = utils::ReadFile(idmap_static_2_path);
204 auto idmap_static_2_raw_stream = std::istringstream(*idmap_static_2_raw_string);
Mårten Kongstadce424902019-03-01 08:35:37 +0100205 auto idmap_static_2 = Idmap::FromBinaryStream(idmap_static_2_raw_stream);
206 ASSERT_TRUE(idmap_static_2);
207 ASSERT_IDMAP(**idmap_static_2, GetTargetApkPath(), overlay_static_2_apk_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200208
Ryan Mitchell19823452019-01-29 12:01:24 -0800209 unlink(idmap_static_no_name_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200210 unlink(idmap_static_2_path.c_str());
211 unlink(idmap_static_1_path.c_str());
212
213 // multiple input directories, non-recursive
214 // clang-format off
215 result = ExecuteBinary({"idmap2",
216 "scan",
217 "--input-directory", GetTestDataPath() + "/target",
218 "--input-directory", GetTestDataPath() + "/overlay",
219 "--target-package-name", "test.target",
220 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800221 "--output-directory", GetTempDirPath(),
222 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200223 // clang-format on
224 ASSERT_THAT(result, NotNull());
225 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
226 ASSERT_EQ(result->stdout, expected.str());
Ryan Mitchell19823452019-01-29 12:01:24 -0800227 unlink(idmap_static_no_name_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200228 unlink(idmap_static_2_path.c_str());
229 unlink(idmap_static_1_path.c_str());
230
231 // the same input directory given twice, but no duplicate entries
232 // clang-format off
233 result = ExecuteBinary({"idmap2",
234 "scan",
235 "--input-directory", GetTestDataPath(),
236 "--input-directory", GetTestDataPath(),
237 "--recursive",
238 "--target-package-name", "test.target",
239 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800240 "--output-directory", GetTempDirPath(),
241 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200242 // clang-format on
243 ASSERT_THAT(result, NotNull());
244 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
245 ASSERT_EQ(result->stdout, expected.str());
Ryan Mitchell19823452019-01-29 12:01:24 -0800246 unlink(idmap_static_no_name_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200247 unlink(idmap_static_2_path.c_str());
248 unlink(idmap_static_1_path.c_str());
249
250 // no APKs in input-directory: ok, but no output
251 // clang-format off
252 result = ExecuteBinary({"idmap2",
253 "scan",
254 "--input-directory", GetTempDirPath(),
255 "--target-package-name", "test.target",
256 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800257 "--output-directory", GetTempDirPath(),
258 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200259 // clang-format on
260 ASSERT_THAT(result, NotNull());
261 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
262 ASSERT_EQ(result->stdout, "");
Ryan Mitchell0503fa52019-04-12 12:29:36 -0700263
264 // the signature idmap failing to generate should not cause scanning to fail
265 // clang-format off
266 result = ExecuteBinary({"idmap2",
267 "scan",
268 "--input-directory", GetTestDataPath(),
269 "--recursive",
270 "--target-package-name", "test.target",
271 "--target-apk-path", GetTargetApkPath(),
272 "--output-directory", GetTempDirPath(),
273 "--override-policy", "public"});
274 // clang-format on
275 ASSERT_THAT(result, NotNull());
276 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
277 ASSERT_EQ(result->stdout, expected.str());
278 unlink(idmap_static_no_name_path.c_str());
279 unlink(idmap_static_2_path.c_str());
280 unlink(idmap_static_1_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200281}
282
283TEST_F(Idmap2BinaryTests, Lookup) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100284 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
285
Mårten Kongstad02751232018-04-27 13:16:32 +0200286 // clang-format off
287 auto result = ExecuteBinary({"idmap2",
288 "create",
289 "--target-apk-path", GetTargetApkPath(),
290 "--overlay-apk-path", GetOverlayApkPath(),
291 "--idmap-path", GetIdmapPath()});
292 // clang-format on
293 ASSERT_THAT(result, NotNull());
294 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
295
296 // clang-format off
297 result = ExecuteBinary({"idmap2",
298 "lookup",
299 "--idmap-path", GetIdmapPath(),
300 "--config", "",
Ryan Mitchell939df092019-04-09 17:13:50 -0700301 "--resid", "0x7f02000c"}); // string/str1
Mårten Kongstad02751232018-04-27 13:16:32 +0200302 // 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", "",
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"), std::string::npos);
318 ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos);
319
320 // clang-format off
321 result = ExecuteBinary({"idmap2",
322 "lookup",
323 "--idmap-path", GetIdmapPath(),
324 "--config", "sv",
325 "--resid", "test.target:string/str1"});
326 // clang-format on
327 ASSERT_THAT(result, NotNull());
328 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
329 ASSERT_NE(result->stdout.find("overlay-1-sv"), std::string::npos);
330
331 unlink(GetIdmapPath().c_str());
332}
333
334TEST_F(Idmap2BinaryTests, InvalidCommandLineOptions) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100335 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
336
Mårten Kongstad02751232018-04-27 13:16:32 +0200337 const std::string invalid_target_apk_path = GetTestDataPath() + "/DOES-NOT-EXIST";
338
339 // missing mandatory options
340 // clang-format off
341 auto result = ExecuteBinary({"idmap2",
342 "create"});
343 // clang-format on
344 ASSERT_THAT(result, NotNull());
345 ASSERT_NE(result->status, EXIT_SUCCESS);
346
347 // missing argument to option
348 // clang-format off
349 result = ExecuteBinary({"idmap2",
350 "create",
351 "--target-apk-path", GetTargetApkPath(),
352 "--overlay-apk-path", GetOverlayApkPath(),
353 "--idmap-path"});
354 // clang-format on
355 ASSERT_THAT(result, NotNull());
356 ASSERT_NE(result->status, EXIT_SUCCESS);
357
358 // invalid target apk path
359 // clang-format off
360 result = ExecuteBinary({"idmap2",
361 "create",
362 "--target-apk-path", invalid_target_apk_path,
363 "--overlay-apk-path", GetOverlayApkPath(),
364 "--idmap-path", GetIdmapPath()});
365 // clang-format on
366 ASSERT_THAT(result, NotNull());
367 ASSERT_NE(result->status, EXIT_SUCCESS);
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800368
369 // unknown policy
370 // clang-format off
371 result = ExecuteBinary({"idmap2",
372 "create",
373 "--target-apk-path", GetTargetApkPath(),
374 "--overlay-apk-path", GetOverlayApkPath(),
375 "--idmap-path", GetIdmapPath(),
376 "--policy", "this-does-not-exist"});
377 // clang-format on
378 ASSERT_THAT(result, NotNull());
379 ASSERT_NE(result->status, EXIT_SUCCESS);
Mårten Kongstad02751232018-04-27 13:16:32 +0200380}
381
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100382} // namespace android::idmap2