blob: c550eafe5ffe3bcc67ef37963bff526a0c160fc1 [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
Mårten Kongstad02751232018-04-27 13:16:32 +0200166 const std::string overlay_static_1_apk_path = GetTestDataPath() + "/overlay/overlay-static-1.apk";
167 const std::string overlay_static_2_apk_path = GetTestDataPath() + "/overlay/overlay-static-2.apk";
168 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;
187 expected << idmap_static_1_path << std::endl;
188 expected << idmap_static_2_path << std::endl;
189 ASSERT_EQ(result->stdout, expected.str());
190
191 std::stringstream error;
192 auto idmap_static_1_raw_string = utils::ReadFile(idmap_static_1_path);
193 auto idmap_static_1_raw_stream = std::istringstream(*idmap_static_1_raw_string);
194 auto idmap_static_1 = Idmap::FromBinaryStream(idmap_static_1_raw_stream, error);
195 ASSERT_THAT(idmap_static_1, NotNull());
196 ASSERT_IDMAP(*idmap_static_1, GetTargetApkPath(), overlay_static_1_apk_path);
197
198 auto idmap_static_2_raw_string = utils::ReadFile(idmap_static_2_path);
199 auto idmap_static_2_raw_stream = std::istringstream(*idmap_static_2_raw_string);
200 auto idmap_static_2 = Idmap::FromBinaryStream(idmap_static_2_raw_stream, error);
201 ASSERT_THAT(idmap_static_2, NotNull());
202 ASSERT_IDMAP(*idmap_static_2, GetTargetApkPath(), overlay_static_2_apk_path);
203
204 unlink(idmap_static_2_path.c_str());
205 unlink(idmap_static_1_path.c_str());
206
207 // multiple input directories, non-recursive
208 // clang-format off
209 result = ExecuteBinary({"idmap2",
210 "scan",
211 "--input-directory", GetTestDataPath() + "/target",
212 "--input-directory", GetTestDataPath() + "/overlay",
213 "--target-package-name", "test.target",
214 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800215 "--output-directory", GetTempDirPath(),
216 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200217 // clang-format on
218 ASSERT_THAT(result, NotNull());
219 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
220 ASSERT_EQ(result->stdout, expected.str());
221 unlink(idmap_static_2_path.c_str());
222 unlink(idmap_static_1_path.c_str());
223
224 // the same input directory given twice, but no duplicate entries
225 // clang-format off
226 result = ExecuteBinary({"idmap2",
227 "scan",
228 "--input-directory", GetTestDataPath(),
229 "--input-directory", GetTestDataPath(),
230 "--recursive",
231 "--target-package-name", "test.target",
232 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800233 "--output-directory", GetTempDirPath(),
234 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200235 // clang-format on
236 ASSERT_THAT(result, NotNull());
237 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
238 ASSERT_EQ(result->stdout, expected.str());
239 unlink(idmap_static_2_path.c_str());
240 unlink(idmap_static_1_path.c_str());
241
242 // no APKs in input-directory: ok, but no output
243 // clang-format off
244 result = ExecuteBinary({"idmap2",
245 "scan",
246 "--input-directory", GetTempDirPath(),
247 "--target-package-name", "test.target",
248 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800249 "--output-directory", GetTempDirPath(),
250 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200251 // clang-format on
252 ASSERT_THAT(result, NotNull());
253 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
254 ASSERT_EQ(result->stdout, "");
255}
256
257TEST_F(Idmap2BinaryTests, Lookup) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100258 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
259
Mårten Kongstad02751232018-04-27 13:16:32 +0200260 // clang-format off
261 auto result = ExecuteBinary({"idmap2",
262 "create",
263 "--target-apk-path", GetTargetApkPath(),
264 "--overlay-apk-path", GetOverlayApkPath(),
265 "--idmap-path", GetIdmapPath()});
266 // clang-format on
267 ASSERT_THAT(result, NotNull());
268 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
269
270 // clang-format off
271 result = ExecuteBinary({"idmap2",
272 "lookup",
273 "--idmap-path", GetIdmapPath(),
274 "--config", "",
Ryan Mitchella3628462019-01-14 12:19:40 -0800275 "--resid", "0x7f020009"}); // string/str1
Mårten Kongstad02751232018-04-27 13:16:32 +0200276 // clang-format on
277 ASSERT_THAT(result, NotNull());
278 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
279 ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos);
280 ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos);
281
282 // clang-format off
283 result = ExecuteBinary({"idmap2",
284 "lookup",
285 "--idmap-path", GetIdmapPath(),
286 "--config", "",
287 "--resid", "test.target:string/str1"});
288 // clang-format on
289 ASSERT_THAT(result, NotNull());
290 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
291 ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos);
292 ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos);
293
294 // clang-format off
295 result = ExecuteBinary({"idmap2",
296 "lookup",
297 "--idmap-path", GetIdmapPath(),
298 "--config", "sv",
299 "--resid", "test.target:string/str1"});
300 // clang-format on
301 ASSERT_THAT(result, NotNull());
302 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
303 ASSERT_NE(result->stdout.find("overlay-1-sv"), std::string::npos);
304
305 unlink(GetIdmapPath().c_str());
306}
307
308TEST_F(Idmap2BinaryTests, InvalidCommandLineOptions) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100309 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
310
Mårten Kongstad02751232018-04-27 13:16:32 +0200311 const std::string invalid_target_apk_path = GetTestDataPath() + "/DOES-NOT-EXIST";
312
313 // missing mandatory options
314 // clang-format off
315 auto result = ExecuteBinary({"idmap2",
316 "create"});
317 // clang-format on
318 ASSERT_THAT(result, NotNull());
319 ASSERT_NE(result->status, EXIT_SUCCESS);
320
321 // missing argument to option
322 // clang-format off
323 result = ExecuteBinary({"idmap2",
324 "create",
325 "--target-apk-path", GetTargetApkPath(),
326 "--overlay-apk-path", GetOverlayApkPath(),
327 "--idmap-path"});
328 // clang-format on
329 ASSERT_THAT(result, NotNull());
330 ASSERT_NE(result->status, EXIT_SUCCESS);
331
332 // invalid target apk path
333 // clang-format off
334 result = ExecuteBinary({"idmap2",
335 "create",
336 "--target-apk-path", invalid_target_apk_path,
337 "--overlay-apk-path", GetOverlayApkPath(),
338 "--idmap-path", GetIdmapPath()});
339 // clang-format on
340 ASSERT_THAT(result, NotNull());
341 ASSERT_NE(result->status, EXIT_SUCCESS);
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800342
343 // unknown policy
344 // clang-format off
345 result = ExecuteBinary({"idmap2",
346 "create",
347 "--target-apk-path", GetTargetApkPath(),
348 "--overlay-apk-path", GetOverlayApkPath(),
349 "--idmap-path", GetIdmapPath(),
350 "--policy", "this-does-not-exist"});
351 // clang-format on
352 ASSERT_THAT(result, NotNull());
353 ASSERT_NE(result->status, EXIT_SUCCESS);
Mårten Kongstad02751232018-04-27 13:16:32 +0200354}
355
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100356} // namespace android::idmap2