blob: e7e9e4cf5091c0721109d5cb81378c0d2f6d084c [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
Winson62ac8b52019-12-04 08:36:48 -080037#include "R.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070038#include "TestHelpers.h"
39#include "androidfw/PosixUtils.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020040#include "gmock/gmock.h"
41#include "gtest/gtest.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020042#include "idmap2/FileUtils.h"
43#include "idmap2/Idmap.h"
Ryan Mitchell52e1f7a2019-04-12 12:31:42 -070044#include "private/android_filesystem_config.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020045
46using ::android::util::ExecuteBinary;
47using ::testing::NotNull;
48
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010049namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020050
51class Idmap2BinaryTests : public Idmap2Tests {};
52
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010053namespace {
54
55void AssertIdmap(const Idmap& idmap, const std::string& target_apk_path,
56 const std::string& overlay_apk_path) {
Mårten Kongstad02751232018-04-27 13:16:32 +020057 // check that the idmap file looks reasonable (IdmapTests is responsible for
58 // more in-depth verification)
59 ASSERT_EQ(idmap.GetHeader()->GetMagic(), kIdmapMagic);
60 ASSERT_EQ(idmap.GetHeader()->GetVersion(), kIdmapCurrentVersion);
61 ASSERT_EQ(idmap.GetHeader()->GetTargetPath(), target_apk_path);
62 ASSERT_EQ(idmap.GetHeader()->GetOverlayPath(), overlay_apk_path);
Mårten Kongstadb8779022018-11-29 09:53:17 +010063 ASSERT_EQ(idmap.GetData().size(), 1U);
Mårten Kongstad02751232018-04-27 13:16:32 +020064}
65
66#define ASSERT_IDMAP(idmap_ref, target_apk_path, overlay_apk_path) \
67 do { \
68 ASSERT_NO_FATAL_FAILURE(AssertIdmap(idmap_ref, target_apk_path, overlay_apk_path)); \
69 } while (0)
70
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010071#ifdef __ANDROID__
72#define SKIP_TEST_IF_CANT_EXEC_IDMAP2 \
73 do { \
74 const uid_t uid = getuid(); \
75 if (uid != AID_ROOT && uid != AID_SYSTEM) { \
76 GTEST_SKIP(); \
77 } \
78 } while (0)
79#else
80#define SKIP_TEST_IF_CANT_EXEC_IDMAP2
81#endif
82
Mårten Kongstad744ccfe2018-12-20 14:56:14 +010083} // namespace
84
Mårten Kongstad02751232018-04-27 13:16:32 +020085TEST_F(Idmap2BinaryTests, Create) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +010086 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
87
Mårten Kongstad02751232018-04-27 13:16:32 +020088 // clang-format off
89 auto result = ExecuteBinary({"idmap2",
90 "create",
91 "--target-apk-path", GetTargetApkPath(),
92 "--overlay-apk-path", GetOverlayApkPath(),
93 "--idmap-path", GetIdmapPath()});
94 // clang-format on
95 ASSERT_THAT(result, NotNull());
96 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
97
98 struct stat st;
99 ASSERT_EQ(stat(GetIdmapPath().c_str(), &st), 0);
100
Mårten Kongstad02751232018-04-27 13:16:32 +0200101 std::ifstream fin(GetIdmapPath());
Mårten Kongstadce424902019-03-01 08:35:37 +0100102 const auto idmap = Idmap::FromBinaryStream(fin);
Mårten Kongstad02751232018-04-27 13:16:32 +0200103 fin.close();
104
Mårten Kongstadce424902019-03-01 08:35:37 +0100105 ASSERT_TRUE(idmap);
106 ASSERT_IDMAP(**idmap, GetTargetApkPath(), GetOverlayApkPath());
Mårten Kongstad02751232018-04-27 13:16:32 +0200107
108 unlink(GetIdmapPath().c_str());
109}
110
111TEST_F(Idmap2BinaryTests, Dump) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100112 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
113
Mårten Kongstad02751232018-04-27 13:16:32 +0200114 // clang-format off
115 auto result = ExecuteBinary({"idmap2",
116 "create",
117 "--target-apk-path", GetTargetApkPath(),
118 "--overlay-apk-path", GetOverlayApkPath(),
119 "--idmap-path", GetIdmapPath()});
120 // clang-format on
121 ASSERT_THAT(result, NotNull());
122 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
123
124 // clang-format off
125 result = ExecuteBinary({"idmap2",
126 "dump",
127 "--idmap-path", GetIdmapPath()});
128 // clang-format on
129 ASSERT_THAT(result, NotNull());
130 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700131 ASSERT_NE(result->stdout.find(R::target::integer::literal::int1 + " -> 0x7f010000"),
Winson62ac8b52019-12-04 08:36:48 -0800132 std::string::npos);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700133 ASSERT_NE(result->stdout.find(R::target::string::literal::str1 + " -> 0x7f020000"),
Winson62ac8b52019-12-04 08:36:48 -0800134 std::string::npos);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700135 ASSERT_NE(result->stdout.find(R::target::string::literal::str3 + " -> 0x7f020001"),
Winson62ac8b52019-12-04 08:36:48 -0800136 std::string::npos);
Ryan Mitchellbf1f45b2020-09-29 17:22:52 -0700137 ASSERT_NE(result->stdout.find(R::target::string::literal::str4 + " -> 0x7f020002"),
Winson62ac8b52019-12-04 08:36:48 -0800138 std::string::npos);
Mårten Kongstad02751232018-04-27 13:16:32 +0200139
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);
Mårten Kongstad02751232018-04-27 13:16:32 +0200149
150 // clang-format off
151 result = ExecuteBinary({"idmap2",
152 "dump",
153 "--verbose",
154 "--idmap-path", GetTestDataPath() + "/DOES-NOT-EXIST"});
155 // clang-format on
156 ASSERT_THAT(result, NotNull());
157 ASSERT_NE(result->status, EXIT_SUCCESS);
158
159 unlink(GetIdmapPath().c_str());
160}
161
Mårten Kongstad02751232018-04-27 13:16:32 +0200162TEST_F(Idmap2BinaryTests, Lookup) {
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700163 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
164
165 // clang-format off
166 auto result = ExecuteBinary({"idmap2",
167 "create",
168 "--target-apk-path", GetTargetApkPath(),
169 "--overlay-apk-path", GetOverlayApkPath(),
170 "--idmap-path", GetIdmapPath()});
171 // clang-format on
172 ASSERT_THAT(result, NotNull());
173 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
174
175 // clang-format off
176 result = ExecuteBinary({"idmap2",
177 "lookup",
178 "--idmap-path", GetIdmapPath(),
179 "--config", "",
Winson62ac8b52019-12-04 08:36:48 -0800180 "--resid", R::target::string::literal::str1});
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700181 // clang-format on
182 ASSERT_THAT(result, NotNull());
183 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
184 ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos);
185 ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos);
186
187 // clang-format off
188 result = ExecuteBinary({"idmap2",
189 "lookup",
190 "--idmap-path", GetIdmapPath(),
191 "--config", "",
192 "--resid", "test.target:string/str1"});
193 // clang-format on
194 ASSERT_THAT(result, NotNull());
195 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
196 ASSERT_NE(result->stdout.find("overlay-1"), std::string::npos);
197 ASSERT_EQ(result->stdout.find("overlay-1-sv"), std::string::npos);
198
199 // clang-format off
200 result = ExecuteBinary({"idmap2",
201 "lookup",
202 "--idmap-path", GetIdmapPath(),
203 "--config", "sv",
204 "--resid", "test.target:string/str1"});
205 // clang-format on
206 ASSERT_THAT(result, NotNull());
207 ASSERT_EQ(result->status, EXIT_SUCCESS) << result->stderr;
208 ASSERT_NE(result->stdout.find("overlay-1-sv"), std::string::npos);
209
210 unlink(GetIdmapPath().c_str());
Mårten Kongstad02751232018-04-27 13:16:32 +0200211}
212
213TEST_F(Idmap2BinaryTests, InvalidCommandLineOptions) {
Mårten Kongstad1da49dc2019-01-14 10:03:53 +0100214 SKIP_TEST_IF_CANT_EXEC_IDMAP2;
215
Mårten Kongstad02751232018-04-27 13:16:32 +0200216 const std::string invalid_target_apk_path = GetTestDataPath() + "/DOES-NOT-EXIST";
217
218 // missing mandatory options
219 // clang-format off
220 auto result = ExecuteBinary({"idmap2",
221 "create"});
222 // clang-format on
223 ASSERT_THAT(result, NotNull());
224 ASSERT_NE(result->status, EXIT_SUCCESS);
225
226 // missing argument to option
227 // clang-format off
228 result = ExecuteBinary({"idmap2",
229 "create",
230 "--target-apk-path", GetTargetApkPath(),
231 "--overlay-apk-path", GetOverlayApkPath(),
232 "--idmap-path"});
233 // clang-format on
234 ASSERT_THAT(result, NotNull());
235 ASSERT_NE(result->status, EXIT_SUCCESS);
236
237 // invalid target apk path
238 // clang-format off
239 result = ExecuteBinary({"idmap2",
240 "create",
241 "--target-apk-path", invalid_target_apk_path,
242 "--overlay-apk-path", GetOverlayApkPath(),
243 "--idmap-path", GetIdmapPath()});
244 // clang-format on
245 ASSERT_THAT(result, NotNull());
246 ASSERT_NE(result->status, EXIT_SUCCESS);
Mårten Kongstadd10d06d2019-01-07 17:26:25 -0800247
248 // unknown policy
249 // clang-format off
250 result = ExecuteBinary({"idmap2",
251 "create",
252 "--target-apk-path", GetTargetApkPath(),
253 "--overlay-apk-path", GetOverlayApkPath(),
254 "--idmap-path", GetIdmapPath(),
255 "--policy", "this-does-not-exist"});
256 // clang-format on
257 ASSERT_THAT(result, NotNull());
258 ASSERT_NE(result->status, EXIT_SUCCESS);
Mårten Kongstad02751232018-04-27 13:16:32 +0200259}
260
Mårten Kongstad0eba72a2018-11-29 08:23:14 +0100261} // namespace android::idmap2