blob: 47af9b5736584de35c189a173211053b008601f1 [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);
Winsonb4100202019-02-06 12:05:32 -0800131 ASSERT_NE(result->stdout.find("0x7f02000a -> 0x7f020000 string/str1"), std::string::npos);
132 ASSERT_NE(result->stdout.find("0x7f02000c -> 0x7f020001 string/str3"), std::string::npos);
133 ASSERT_NE(result->stdout.find("0x7f02000d -> 0x7f020002 string/str4"), std::string::npos);
Mårten Kongstad02751232018-04-27 13:16:32 +0200134 ASSERT_EQ(result->stdout.find("00000210: 007f target package id"), std::string::npos);
135
136 // clang-format off
137 result = ExecuteBinary({"idmap2",
138 "dump",
139 "--verbose",
140 "--idmap-path", GetIdmapPath()});
141 // clang-format on
142 ASSERT_THAT(result, NotNull());
143 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
144 ASSERT_NE(result->stdout.find("00000000: 504d4449 magic"), std::string::npos);
145 ASSERT_NE(result->stdout.find("00000210: 007f target package id"), std::string::npos);
146
147 // clang-format off
148 result = ExecuteBinary({"idmap2",
149 "dump",
150 "--verbose",
151 "--idmap-path", GetTestDataPath() + "/DOES-NOT-EXIST"});
152 // clang-format on
153 ASSERT_THAT(result, NotNull());
154 ASSERT_NE(result->status, EXIT_SUCCESS);
155
156 unlink(GetIdmapPath().c_str());
157}
158
159TEST_F(Idmap2BinaryTests, Scan) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100160 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
161
Ryan Mitchell19823452019-01-29 12:01:24 -0800162 const std::string overlay_static_no_name_apk_path =
163 GetTestDataPath() + "/overlay/overlay-no-name-static.apk";
Mårten Kongstad02751232018-04-27 13:16:32 +0200164 const std::string overlay_static_1_apk_path = GetTestDataPath() + "/overlay/overlay-static-1.apk";
165 const std::string overlay_static_2_apk_path = GetTestDataPath() + "/overlay/overlay-static-2.apk";
Ryan Mitchell19823452019-01-29 12:01:24 -0800166 const std::string idmap_static_no_name_path =
167 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_no_name_apk_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200168 const std::string idmap_static_1_path =
169 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_1_apk_path);
170 const std::string idmap_static_2_path =
171 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_2_apk_path);
172
173 // single input directory, recursive
174 // clang-format off
175 auto result = ExecuteBinary({"idmap2",
176 "scan",
177 "--input-directory", GetTestDataPath(),
178 "--recursive",
179 "--target-package-name", "test.target",
180 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800181 "--output-directory", GetTempDirPath(),
182 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200183 // clang-format on
184 ASSERT_THAT(result, NotNull());
185 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
186 std::stringstream expected;
Ryan Mitchell19823452019-01-29 12:01:24 -0800187 expected << idmap_static_no_name_path << std::endl;
Mårten Kongstad02751232018-04-27 13:16:32 +0200188 expected << idmap_static_1_path << std::endl;
189 expected << idmap_static_2_path << std::endl;
190 ASSERT_EQ(result->stdout, expected.str());
191
Ryan Mitchell19823452019-01-29 12:01:24 -0800192 auto idmap_static_no_name_raw_string = utils::ReadFile(idmap_static_no_name_path);
193 auto idmap_static_no_name_raw_stream = std::istringstream(*idmap_static_no_name_raw_string);
Mårten Kongstadce424902019-03-01 08:35:37 +0100194 auto idmap_static_no_name = Idmap::FromBinaryStream(idmap_static_no_name_raw_stream);
195 ASSERT_TRUE(idmap_static_no_name);
196 ASSERT_IDMAP(**idmap_static_no_name, GetTargetApkPath(), overlay_static_no_name_apk_path);
Ryan Mitchell19823452019-01-29 12:01:24 -0800197
Mårten Kongstad02751232018-04-27 13:16:32 +0200198 auto idmap_static_1_raw_string = utils::ReadFile(idmap_static_1_path);
199 auto idmap_static_1_raw_stream = std::istringstream(*idmap_static_1_raw_string);
Mårten Kongstadce424902019-03-01 08:35:37 +0100200 auto idmap_static_1 = Idmap::FromBinaryStream(idmap_static_1_raw_stream);
201 ASSERT_TRUE(idmap_static_1);
202 ASSERT_IDMAP(**idmap_static_1, GetTargetApkPath(), overlay_static_1_apk_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200203
204 auto idmap_static_2_raw_string = utils::ReadFile(idmap_static_2_path);
205 auto idmap_static_2_raw_stream = std::istringstream(*idmap_static_2_raw_string);
Mårten Kongstadce424902019-03-01 08:35:37 +0100206 auto idmap_static_2 = Idmap::FromBinaryStream(idmap_static_2_raw_stream);
207 ASSERT_TRUE(idmap_static_2);
208 ASSERT_IDMAP(**idmap_static_2, GetTargetApkPath(), overlay_static_2_apk_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200209
Ryan Mitchell19823452019-01-29 12:01:24 -0800210 unlink(idmap_static_no_name_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200211 unlink(idmap_static_2_path.c_str());
212 unlink(idmap_static_1_path.c_str());
213
214 // multiple input directories, non-recursive
215 // clang-format off
216 result = ExecuteBinary({"idmap2",
217 "scan",
218 "--input-directory", GetTestDataPath() + "/target",
219 "--input-directory", GetTestDataPath() + "/overlay",
220 "--target-package-name", "test.target",
221 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800222 "--output-directory", GetTempDirPath(),
223 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200224 // clang-format on
225 ASSERT_THAT(result, NotNull());
226 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
227 ASSERT_EQ(result->stdout, expected.str());
Ryan Mitchell19823452019-01-29 12:01:24 -0800228 unlink(idmap_static_no_name_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200229 unlink(idmap_static_2_path.c_str());
230 unlink(idmap_static_1_path.c_str());
231
232 // the same input directory given twice, but no duplicate entries
233 // clang-format off
234 result = ExecuteBinary({"idmap2",
235 "scan",
236 "--input-directory", GetTestDataPath(),
237 "--input-directory", GetTestDataPath(),
238 "--recursive",
239 "--target-package-name", "test.target",
240 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800241 "--output-directory", GetTempDirPath(),
242 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200243 // clang-format on
244 ASSERT_THAT(result, NotNull());
245 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
246 ASSERT_EQ(result->stdout, expected.str());
Ryan Mitchell19823452019-01-29 12:01:24 -0800247 unlink(idmap_static_no_name_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200248 unlink(idmap_static_2_path.c_str());
249 unlink(idmap_static_1_path.c_str());
250
251 // no APKs in input-directory: ok, but no output
252 // clang-format off
253 result = ExecuteBinary({"idmap2",
254 "scan",
255 "--input-directory", GetTempDirPath(),
256 "--target-package-name", "test.target",
257 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800258 "--output-directory", GetTempDirPath(),
259 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200260 // clang-format on
261 ASSERT_THAT(result, NotNull());
262 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
263 ASSERT_EQ(result->stdout, "");
264}
265
266TEST_F(Idmap2BinaryTests, Lookup) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100267 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
268
Mårten Kongstad02751232018-04-27 13:16:32 +0200269 // clang-format off
270 auto result = ExecuteBinary({"idmap2",
271 "create",
272 "--target-apk-path", GetTargetApkPath(),
273 "--overlay-apk-path", GetOverlayApkPath(),
274 "--idmap-path", GetIdmapPath()});
275 // clang-format on
276 ASSERT_THAT(result, NotNull());
277 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
278
279 // clang-format off
280 result = ExecuteBinary({"idmap2",
281 "lookup",
282 "--idmap-path", GetIdmapPath(),
283 "--config", "",
Winsonb4100202019-02-06 12:05:32 -0800284 "--resid", "0x7f02000a"}); // string/str1
Mårten Kongstad02751232018-04-27 13:16:32 +0200285 // clang-format on
286 ASSERT_THAT(result, NotNull());
287 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
288 ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos);
289 ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos);
290
291 // clang-format off
292 result = ExecuteBinary({"idmap2",
293 "lookup",
294 "--idmap-path", GetIdmapPath(),
295 "--config", "",
296 "--resid", "test.target:string/str1"});
297 // clang-format on
298 ASSERT_THAT(result, NotNull());
299 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
300 ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos);
301 ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos);
302
303 // clang-format off
304 result = ExecuteBinary({"idmap2",
305 "lookup",
306 "--idmap-path", GetIdmapPath(),
307 "--config", "sv",
308 "--resid", "test.target:string/str1"});
309 // clang-format on
310 ASSERT_THAT(result, NotNull());
311 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
312 ASSERT_NE(result->stdout.find("overlay-1-sv"), std::string::npos);
313
314 unlink(GetIdmapPath().c_str());
315}
316
317TEST_F(Idmap2BinaryTests, InvalidCommandLineOptions) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100318 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
319
Mårten Kongstad02751232018-04-27 13:16:32 +0200320 const std::string invalid_target_apk_path = GetTestDataPath() + "/DOES-NOT-EXIST";
321
322 // missing mandatory options
323 // clang-format off
324 auto result = ExecuteBinary({"idmap2",
325 "create"});
326 // clang-format on
327 ASSERT_THAT(result, NotNull());
328 ASSERT_NE(result->status, EXIT_SUCCESS);
329
330 // missing argument to option
331 // clang-format off
332 result = ExecuteBinary({"idmap2",
333 "create",
334 "--target-apk-path", GetTargetApkPath(),
335 "--overlay-apk-path", GetOverlayApkPath(),
336 "--idmap-path"});
337 // clang-format on
338 ASSERT_THAT(result, NotNull());
339 ASSERT_NE(result->status, EXIT_SUCCESS);
340
341 // invalid target apk path
342 // clang-format off
343 result = ExecuteBinary({"idmap2",
344 "create",
345 "--target-apk-path", invalid_target_apk_path,
346 "--overlay-apk-path", GetOverlayApkPath(),
347 "--idmap-path", GetIdmapPath()});
348 // clang-format on
349 ASSERT_THAT(result, NotNull());
350 ASSERT_NE(result->status, EXIT_SUCCESS);
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800351
352 // unknown policy
353 // clang-format off
354 result = ExecuteBinary({"idmap2",
355 "create",
356 "--target-apk-path", GetTargetApkPath(),
357 "--overlay-apk-path", GetOverlayApkPath(),
358 "--idmap-path", GetIdmapPath(),
359 "--policy", "this-does-not-exist"});
360 // clang-format on
361 ASSERT_THAT(result, NotNull());
362 ASSERT_NE(result->status, EXIT_SUCCESS);
Mårten Kongstad02751232018-04-27 13:16:32 +0200363}
364
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100365} // namespace android::idmap2