blob: b1685b7f13127ca03a4a8ccc3a1aaaeedd9f82d9 [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);
Mårten Kongstad02751232018-04-27 13:16:32 +0200144
145 // clang-format off
146 result = ExecuteBinary({"idmap2",
147 "dump",
148 "--verbose",
149 "--idmap-path", GetTestDataPath() + "/DOES-NOT-EXIST"});
150 // clang-format on
151 ASSERT_THAT(result, NotNull());
152 ASSERT_NE(result->status, EXIT_SUCCESS);
153
154 unlink(GetIdmapPath().c_str());
155}
156
157TEST_F(Idmap2BinaryTests, Scan) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100158 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
159
Ryan Mitchell19823452019-01-29 12:01:24 -0800160 const std::string overlay_static_no_name_apk_path =
161 GetTestDataPath() + "/overlay/overlay-no-name-static.apk";
Mårten Kongstad02751232018-04-27 13:16:32 +0200162 const std::string overlay_static_1_apk_path = GetTestDataPath() + "/overlay/overlay-static-1.apk";
163 const std::string overlay_static_2_apk_path = GetTestDataPath() + "/overlay/overlay-static-2.apk";
Ryan Mitchell19823452019-01-29 12:01:24 -0800164 const std::string idmap_static_no_name_path =
165 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_no_name_apk_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200166 const std::string idmap_static_1_path =
167 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_1_apk_path);
168 const std::string idmap_static_2_path =
169 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_2_apk_path);
170
171 // single input directory, recursive
172 // clang-format off
173 auto result = ExecuteBinary({"idmap2",
174 "scan",
175 "--input-directory", GetTestDataPath(),
176 "--recursive",
177 "--target-package-name", "test.target",
178 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800179 "--output-directory", GetTempDirPath(),
180 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200181 // clang-format on
182 ASSERT_THAT(result, NotNull());
183 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
184 std::stringstream expected;
Ryan Mitchell19823452019-01-29 12:01:24 -0800185 expected << idmap_static_no_name_path << std::endl;
Mårten Kongstad02751232018-04-27 13:16:32 +0200186 expected << idmap_static_1_path << std::endl;
187 expected << idmap_static_2_path << std::endl;
188 ASSERT_EQ(result->stdout, expected.str());
189
Ryan Mitchell19823452019-01-29 12:01:24 -0800190 auto idmap_static_no_name_raw_string = utils::ReadFile(idmap_static_no_name_path);
191 auto idmap_static_no_name_raw_stream = std::istringstream(*idmap_static_no_name_raw_string);
Mårten Kongstadce424902019-03-01 08:35:37 +0100192 auto idmap_static_no_name = Idmap::FromBinaryStream(idmap_static_no_name_raw_stream);
193 ASSERT_TRUE(idmap_static_no_name);
194 ASSERT_IDMAP(**idmap_static_no_name, GetTargetApkPath(), overlay_static_no_name_apk_path);
Ryan Mitchell19823452019-01-29 12:01:24 -0800195
Mårten Kongstad02751232018-04-27 13:16:32 +0200196 auto idmap_static_1_raw_string = utils::ReadFile(idmap_static_1_path);
197 auto idmap_static_1_raw_stream = std::istringstream(*idmap_static_1_raw_string);
Mårten Kongstadce424902019-03-01 08:35:37 +0100198 auto idmap_static_1 = Idmap::FromBinaryStream(idmap_static_1_raw_stream);
199 ASSERT_TRUE(idmap_static_1);
200 ASSERT_IDMAP(**idmap_static_1, GetTargetApkPath(), overlay_static_1_apk_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200201
202 auto idmap_static_2_raw_string = utils::ReadFile(idmap_static_2_path);
203 auto idmap_static_2_raw_stream = std::istringstream(*idmap_static_2_raw_string);
Mårten Kongstadce424902019-03-01 08:35:37 +0100204 auto idmap_static_2 = Idmap::FromBinaryStream(idmap_static_2_raw_stream);
205 ASSERT_TRUE(idmap_static_2);
206 ASSERT_IDMAP(**idmap_static_2, GetTargetApkPath(), overlay_static_2_apk_path);
Mårten Kongstad02751232018-04-27 13:16:32 +0200207
Ryan Mitchell19823452019-01-29 12:01:24 -0800208 unlink(idmap_static_no_name_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200209 unlink(idmap_static_2_path.c_str());
210 unlink(idmap_static_1_path.c_str());
211
212 // multiple input directories, non-recursive
213 // clang-format off
214 result = ExecuteBinary({"idmap2",
215 "scan",
216 "--input-directory", GetTestDataPath() + "/target",
217 "--input-directory", GetTestDataPath() + "/overlay",
218 "--target-package-name", "test.target",
219 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800220 "--output-directory", GetTempDirPath(),
221 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200222 // clang-format on
223 ASSERT_THAT(result, NotNull());
224 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
225 ASSERT_EQ(result->stdout, expected.str());
Ryan Mitchell19823452019-01-29 12:01:24 -0800226 unlink(idmap_static_no_name_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200227 unlink(idmap_static_2_path.c_str());
228 unlink(idmap_static_1_path.c_str());
229
230 // the same input directory given twice, but no duplicate entries
231 // clang-format off
232 result = ExecuteBinary({"idmap2",
233 "scan",
234 "--input-directory", GetTestDataPath(),
235 "--input-directory", GetTestDataPath(),
236 "--recursive",
237 "--target-package-name", "test.target",
238 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800239 "--output-directory", GetTempDirPath(),
240 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200241 // clang-format on
242 ASSERT_THAT(result, NotNull());
243 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
244 ASSERT_EQ(result->stdout, expected.str());
Ryan Mitchell19823452019-01-29 12:01:24 -0800245 unlink(idmap_static_no_name_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200246 unlink(idmap_static_2_path.c_str());
247 unlink(idmap_static_1_path.c_str());
248
249 // no APKs in input-directory: ok, but no output
250 // clang-format off
251 result = ExecuteBinary({"idmap2",
252 "scan",
253 "--input-directory", GetTempDirPath(),
254 "--target-package-name", "test.target",
255 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800256 "--output-directory", GetTempDirPath(),
257 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200258 // clang-format on
259 ASSERT_THAT(result, NotNull());
260 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
261 ASSERT_EQ(result->stdout, "");
Ryan Mitchell0503fa52019-04-12 12:29:36 -0700262
263 // the signature idmap failing to generate should not cause scanning to fail
264 // clang-format off
265 result = ExecuteBinary({"idmap2",
266 "scan",
267 "--input-directory", GetTestDataPath(),
268 "--recursive",
269 "--target-package-name", "test.target",
270 "--target-apk-path", GetTargetApkPath(),
271 "--output-directory", GetTempDirPath(),
272 "--override-policy", "public"});
273 // clang-format on
274 ASSERT_THAT(result, NotNull());
275 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
276 ASSERT_EQ(result->stdout, expected.str());
277 unlink(idmap_static_no_name_path.c_str());
278 unlink(idmap_static_2_path.c_str());
279 unlink(idmap_static_1_path.c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200280}
281
282TEST_F(Idmap2BinaryTests, Lookup) {
Ryan Mitchelle753ffe2019-09-23 09:47:02 -0700283 // TODO(135943783): Removed temporarily until libandroidfw idmap loading is fixed.
Mårten Kongstad02751232018-04-27 13:16:32 +0200284}
285
286TEST_F(Idmap2BinaryTests, InvalidCommandLineOptions) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100287 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
288
Mårten Kongstad02751232018-04-27 13:16:32 +0200289 const std::string invalid_target_apk_path = GetTestDataPath() + "/DOES-NOT-EXIST";
290
291 // missing mandatory options
292 // clang-format off
293 auto result = ExecuteBinary({"idmap2",
294 "create"});
295 // clang-format on
296 ASSERT_THAT(result, NotNull());
297 ASSERT_NE(result->status, EXIT_SUCCESS);
298
299 // missing argument to option
300 // clang-format off
301 result = ExecuteBinary({"idmap2",
302 "create",
303 "--target-apk-path", GetTargetApkPath(),
304 "--overlay-apk-path", GetOverlayApkPath(),
305 "--idmap-path"});
306 // clang-format on
307 ASSERT_THAT(result, NotNull());
308 ASSERT_NE(result->status, EXIT_SUCCESS);
309
310 // invalid target apk path
311 // clang-format off
312 result = ExecuteBinary({"idmap2",
313 "create",
314 "--target-apk-path", invalid_target_apk_path,
315 "--overlay-apk-path", GetOverlayApkPath(),
316 "--idmap-path", GetIdmapPath()});
317 // clang-format on
318 ASSERT_THAT(result, NotNull());
319 ASSERT_NE(result->status, EXIT_SUCCESS);
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800320
321 // unknown policy
322 // clang-format off
323 result = ExecuteBinary({"idmap2",
324 "create",
325 "--target-apk-path", GetTargetApkPath(),
326 "--overlay-apk-path", GetOverlayApkPath(),
327 "--idmap-path", GetIdmapPath(),
328 "--policy", "this-does-not-exist"});
329 // clang-format on
330 ASSERT_THAT(result, NotNull());
331 ASSERT_NE(result->status, EXIT_SUCCESS);
Mårten Kongstad02751232018-04-27 13:16:32 +0200332}
333
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100334} // namespace android::idmap2