blob: 0c8f164bf096816767035837605776b7a08a5059 [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 Kongstadd10d06d2019-01-07 17:26:25 -080041
Mårten Kongstad02751232018-04-27 13:16:32 +020042#include "idmap2/FileUtils.h"
43#include "idmap2/Idmap.h"
44
45#include "TestHelpers.h"
46
47using ::android::util::ExecuteBinary;
48using ::testing::NotNull;
49
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010050namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020051
52class Idmap2BinaryTests : public Idmap2Tests {};
53
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010054namespace {
55
56void AssertIdmap(const Idmap& idmap, const std::string& target_apk_path,
57 const std::string& overlay_apk_path) {
Mårten Kongstad02751232018-04-27 13:16:32 +020058 // check that the idmap file looks reasonable (IdmapTests is responsible for
59 // more in-depth verification)
60 ASSERT_EQ(idmap.GetHeader()->GetMagic(), kIdmapMagic);
61 ASSERT_EQ(idmap.GetHeader()->GetVersion(), kIdmapCurrentVersion);
62 ASSERT_EQ(idmap.GetHeader()->GetTargetPath(), target_apk_path);
63 ASSERT_EQ(idmap.GetHeader()->GetOverlayPath(), overlay_apk_path);
Mårten Kongstadb8779022018-11-29 09:53:17 +010064 ASSERT_EQ(idmap.GetData().size(), 1U);
Mårten Kongstad02751232018-04-27 13:16:32 +020065}
66
67#define ASSERT_IDMAP(idmap_ref, target_apk_path, overlay_apk_path) \
68 do { \
69 ASSERT_NO_FATAL_FAILURE(AssertIdmap(idmap_ref, target_apk_path, overlay_apk_path)); \
70 } while (0)
71
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010072} // namespace
73
Mårten Kongstad02751232018-04-27 13:16:32 +020074TEST_F(Idmap2BinaryTests, Create) {
75 // clang-format off
76 auto result = ExecuteBinary({"idmap2",
77 "create",
78 "--target-apk-path", GetTargetApkPath(),
79 "--overlay-apk-path", GetOverlayApkPath(),
80 "--idmap-path", GetIdmapPath()});
81 // clang-format on
82 ASSERT_THAT(result, NotNull());
83 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
84
85 struct stat st;
86 ASSERT_EQ(stat(GetIdmapPath().c_str(), &st), 0);
87
88 std::stringstream error;
89 std::ifstream fin(GetIdmapPath());
90 std::unique_ptr<const Idmap> idmap = Idmap::FromBinaryStream(fin, error);
91 fin.close();
92
93 ASSERT_THAT(idmap, NotNull());
94 ASSERT_IDMAP(*idmap, GetTargetApkPath(), GetOverlayApkPath());
95
96 unlink(GetIdmapPath().c_str());
97}
98
99TEST_F(Idmap2BinaryTests, Dump) {
100 // clang-format off
101 auto result = ExecuteBinary({"idmap2",
102 "create",
103 "--target-apk-path", GetTargetApkPath(),
104 "--overlay-apk-path", GetOverlayApkPath(),
105 "--idmap-path", GetIdmapPath()});
106 // clang-format on
107 ASSERT_THAT(result, NotNull());
108 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
109
110 // clang-format off
111 result = ExecuteBinary({"idmap2",
112 "dump",
113 "--idmap-path", GetIdmapPath()});
114 // clang-format on
115 ASSERT_THAT(result, NotNull());
116 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
117 ASSERT_NE(result->stdout.find("0x7f010000 -> 0x7f010000 integer/int1"), std::string::npos);
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800118 ASSERT_NE(result->stdout.find("0x7f020008 -> 0x7f020000 string/str1"), std::string::npos);
119 ASSERT_NE(result->stdout.find("0x7f02000a -> 0x7f020001 string/str3"), std::string::npos);
120 ASSERT_NE(result->stdout.find("0x7f02000b -> 0x7f020002 string/str4"), std::string::npos);
Mårten Kongstad02751232018-04-27 13:16:32 +0200121 ASSERT_EQ(result->stdout.find("00000210: 007f target package id"), std::string::npos);
122
123 // clang-format off
124 result = ExecuteBinary({"idmap2",
125 "dump",
126 "--verbose",
127 "--idmap-path", GetIdmapPath()});
128 // clang-format on
129 ASSERT_THAT(result, NotNull());
130 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
131 ASSERT_NE(result->stdout.find("00000000: 504d4449 magic"), std::string::npos);
132 ASSERT_NE(result->stdout.find("00000210: 007f target package id"), std::string::npos);
133
134 // clang-format off
135 result = ExecuteBinary({"idmap2",
136 "dump",
137 "--verbose",
138 "--idmap-path", GetTestDataPath() + "/DOES-NOT-EXIST"});
139 // clang-format on
140 ASSERT_THAT(result, NotNull());
141 ASSERT_NE(result->status, EXIT_SUCCESS);
142
143 unlink(GetIdmapPath().c_str());
144}
145
146TEST_F(Idmap2BinaryTests, Scan) {
147 const std::string overlay_static_1_apk_path = GetTestDataPath() + "/overlay/overlay-static-1.apk";
148 const std::string overlay_static_2_apk_path = GetTestDataPath() + "/overlay/overlay-static-2.apk";
149 const std::string idmap_static_1_path =
150 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_1_apk_path);
151 const std::string idmap_static_2_path =
152 Idmap::CanonicalIdmapPathFor(GetTempDirPath(), overlay_static_2_apk_path);
153
154 // single input directory, recursive
155 // clang-format off
156 auto result = ExecuteBinary({"idmap2",
157 "scan",
158 "--input-directory", GetTestDataPath(),
159 "--recursive",
160 "--target-package-name", "test.target",
161 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800162 "--output-directory", GetTempDirPath(),
163 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200164 // clang-format on
165 ASSERT_THAT(result, NotNull());
166 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
167 std::stringstream expected;
168 expected << idmap_static_1_path << std::endl;
169 expected << idmap_static_2_path << std::endl;
170 ASSERT_EQ(result->stdout, expected.str());
171
172 std::stringstream error;
173 auto idmap_static_1_raw_string = utils::ReadFile(idmap_static_1_path);
174 auto idmap_static_1_raw_stream = std::istringstream(*idmap_static_1_raw_string);
175 auto idmap_static_1 = Idmap::FromBinaryStream(idmap_static_1_raw_stream, error);
176 ASSERT_THAT(idmap_static_1, NotNull());
177 ASSERT_IDMAP(*idmap_static_1, GetTargetApkPath(), overlay_static_1_apk_path);
178
179 auto idmap_static_2_raw_string = utils::ReadFile(idmap_static_2_path);
180 auto idmap_static_2_raw_stream = std::istringstream(*idmap_static_2_raw_string);
181 auto idmap_static_2 = Idmap::FromBinaryStream(idmap_static_2_raw_stream, error);
182 ASSERT_THAT(idmap_static_2, NotNull());
183 ASSERT_IDMAP(*idmap_static_2, GetTargetApkPath(), overlay_static_2_apk_path);
184
185 unlink(idmap_static_2_path.c_str());
186 unlink(idmap_static_1_path.c_str());
187
188 // multiple input directories, non-recursive
189 // clang-format off
190 result = ExecuteBinary({"idmap2",
191 "scan",
192 "--input-directory", GetTestDataPath() + "/target",
193 "--input-directory", GetTestDataPath() + "/overlay",
194 "--target-package-name", "test.target",
195 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800196 "--output-directory", GetTempDirPath(),
197 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200198 // clang-format on
199 ASSERT_THAT(result, NotNull());
200 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
201 ASSERT_EQ(result->stdout, expected.str());
202 unlink(idmap_static_2_path.c_str());
203 unlink(idmap_static_1_path.c_str());
204
205 // the same input directory given twice, but no duplicate entries
206 // clang-format off
207 result = ExecuteBinary({"idmap2",
208 "scan",
209 "--input-directory", GetTestDataPath(),
210 "--input-directory", GetTestDataPath(),
211 "--recursive",
212 "--target-package-name", "test.target",
213 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800214 "--output-directory", GetTempDirPath(),
215 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200216 // clang-format on
217 ASSERT_THAT(result, NotNull());
218 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
219 ASSERT_EQ(result->stdout, expected.str());
220 unlink(idmap_static_2_path.c_str());
221 unlink(idmap_static_1_path.c_str());
222
223 // no APKs in input-directory: ok, but no output
224 // clang-format off
225 result = ExecuteBinary({"idmap2",
226 "scan",
227 "--input-directory", GetTempDirPath(),
228 "--target-package-name", "test.target",
229 "--target-apk-path", GetTargetApkPath(),
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800230 "--output-directory", GetTempDirPath(),
231 "--override-policy", "public"});
Mårten Kongstad02751232018-04-27 13:16:32 +0200232 // clang-format on
233 ASSERT_THAT(result, NotNull());
234 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
235 ASSERT_EQ(result->stdout, "");
236}
237
238TEST_F(Idmap2BinaryTests, Lookup) {
239 // clang-format off
240 auto result = ExecuteBinary({"idmap2",
241 "create",
242 "--target-apk-path", GetTargetApkPath(),
243 "--overlay-apk-path", GetOverlayApkPath(),
244 "--idmap-path", GetIdmapPath()});
245 // clang-format on
246 ASSERT_THAT(result, NotNull());
247 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
248
249 // clang-format off
250 result = ExecuteBinary({"idmap2",
251 "lookup",
252 "--idmap-path", GetIdmapPath(),
253 "--config", "",
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800254 "--resid", "0x7f020008"}); // string/str1
Mårten Kongstad02751232018-04-27 13:16:32 +0200255 // clang-format on
256 ASSERT_THAT(result, NotNull());
257 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
258 ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos);
259 ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos);
260
261 // clang-format off
262 result = ExecuteBinary({"idmap2",
263 "lookup",
264 "--idmap-path", GetIdmapPath(),
265 "--config", "",
266 "--resid", "test.target:string/str1"});
267 // clang-format on
268 ASSERT_THAT(result, NotNull());
269 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
270 ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos);
271 ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos);
272
273 // clang-format off
274 result = ExecuteBinary({"idmap2",
275 "lookup",
276 "--idmap-path", GetIdmapPath(),
277 "--config", "sv",
278 "--resid", "test.target:string/str1"});
279 // clang-format on
280 ASSERT_THAT(result, NotNull());
281 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
282 ASSERT_NE(result->stdout.find("overlay-1-sv"), std::string::npos);
283
284 unlink(GetIdmapPath().c_str());
285}
286
287TEST_F(Idmap2BinaryTests, InvalidCommandLineOptions) {
288 const std::string invalid_target_apk_path = GetTestDataPath() + "/DOES-NOT-EXIST";
289
290 // missing mandatory options
291 // clang-format off
292 auto result = ExecuteBinary({"idmap2",
293 "create"});
294 // clang-format on
295 ASSERT_THAT(result, NotNull());
296 ASSERT_NE(result->status, EXIT_SUCCESS);
297
298 // missing argument to option
299 // clang-format off
300 result = ExecuteBinary({"idmap2",
301 "create",
302 "--target-apk-path", GetTargetApkPath(),
303 "--overlay-apk-path", GetOverlayApkPath(),
304 "--idmap-path"});
305 // clang-format on
306 ASSERT_THAT(result, NotNull());
307 ASSERT_NE(result->status, EXIT_SUCCESS);
308
309 // invalid target apk path
310 // clang-format off
311 result = ExecuteBinary({"idmap2",
312 "create",
313 "--target-apk-path", invalid_target_apk_path,
314 "--overlay-apk-path", GetOverlayApkPath(),
315 "--idmap-path", GetIdmapPath()});
316 // clang-format on
317 ASSERT_THAT(result, NotNull());
318 ASSERT_NE(result->status, EXIT_SUCCESS);
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800319
320 // unknown policy
321 // clang-format off
322 result = ExecuteBinary({"idmap2",
323 "create",
324 "--target-apk-path", GetTargetApkPath(),
325 "--overlay-apk-path", GetOverlayApkPath(),
326 "--idmap-path", GetIdmapPath(),
327 "--policy", "this-does-not-exist"});
328 // clang-format on
329 ASSERT_THAT(result, NotNull());
330 ASSERT_NE(result->status, EXIT_SUCCESS);
Mårten Kongstad02751232018-04-27 13:16:32 +0200331}
332
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100333} // namespace android::idmap2