1 /*
2 * Copyright (C) 2011 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 #include <errno.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21
22 #include <android-base/logging.h>
23 #include <android-base/scopeguard.h>
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26
27 #include "InstalldNativeService.h"
28 #include "MatchExtensionGen.h"
29 #include "globals.h"
30 #include "utils.h"
31
32 #undef LOG_TAG
33 #define LOG_TAG "utils_test"
34
35 #define TEST_DATA_DIR "/data/"
36 #define TEST_ROOT_DIR "/system/"
37 #define TEST_APP_DIR "/data/app/"
38 #define TEST_APP_PRIVATE_DIR "/data/app-private/"
39 #define TEST_APP_EPHEMERAL_DIR "/data/app-ephemeral/"
40 #define TEST_ASEC_DIR "/mnt/asec/"
41 #define TEST_EXPAND_DIR "/mnt/expand/00000000-0000-0000-0000-000000000000/"
42
43 #define TEST_SYSTEM_DIR1 "/system/app/"
44 #define TEST_SYSTEM_DIR2 "/vendor/app/"
45
46 #define TEST_PROFILE_DIR "/data/misc/profiles"
47
48 namespace android {
49 namespace installd {
50
51 using ::testing::UnorderedElementsAre;
52
53 class UtilsTest : public testing::Test {
54 protected:
SetUp()55 virtual void SetUp() {
56 setenv("ANDROID_LOG_TAGS", "*:v", 1);
57 android::base::InitLogging(nullptr);
58
59 init_globals_from_data_and_root(TEST_DATA_DIR, TEST_ROOT_DIR);
60 }
61
TearDown()62 virtual void TearDown() {
63 }
64
create_too_long_path(const std::string & seed)65 std::string create_too_long_path(const std::string& seed) {
66 std::string result = seed;
67 for (size_t i = seed.size(); i < PKG_PATH_MAX; i++) {
68 result += "a";
69 }
70 return result;
71 }
72 };
73
TEST_F(UtilsTest,IsValidApkPath_BadPrefix)74 TEST_F(UtilsTest, IsValidApkPath_BadPrefix) {
75 // Bad prefixes directories
76 const char *badprefix1 = "/etc/passwd";
77 EXPECT_EQ(-1, validate_apk_path(badprefix1))
78 << badprefix1 << " should not be allowed as a valid path";
79
80 const char *badprefix2 = "../.." TEST_APP_DIR "../../../blah";
81 EXPECT_EQ(-1, validate_apk_path(badprefix2))
82 << badprefix2 << " should not be allowed as a valid path";
83
84 const char *badprefix3 = "init.rc";
85 EXPECT_EQ(-1, validate_apk_path(badprefix3))
86 << badprefix3 << " should not be allowed as a valid path";
87
88 const char *badprefix4 = "/init.rc";
89 EXPECT_EQ(-1, validate_apk_path(badprefix4))
90 << badprefix4 << " should not be allowed as a valid path";
91 }
92
TEST_F(UtilsTest,IsValidApkPath_Internal)93 TEST_F(UtilsTest, IsValidApkPath_Internal) {
94 // Internal directories
95 const char *internal1 = TEST_APP_DIR "example.apk";
96 EXPECT_EQ(0, validate_apk_path(internal1))
97 << internal1 << " should be allowed as a valid path";
98
99 // b/16888084
100 const char *path2 = TEST_APP_DIR "example.com/example.apk";
101 EXPECT_EQ(0, validate_apk_path(path2))
102 << path2 << " should be allowed as a valid path";
103
104 const char *badint1 = TEST_APP_DIR "../example.apk";
105 EXPECT_EQ(-1, validate_apk_path(badint1))
106 << badint1 << " should be rejected as a invalid path";
107
108 const char *badint2 = TEST_APP_DIR "/../example.apk";
109 EXPECT_EQ(-1, validate_apk_path(badint2))
110 << badint2 << " should be rejected as a invalid path";
111
112 // Should not have more than two sub directories
113 const char *bad_path3 = TEST_APP_DIR "random/example.com/subdir/pkg.apk";
114 EXPECT_EQ(-1, validate_apk_path(bad_path3))
115 << bad_path3 << " should be rejected as a invalid path";
116
117 const char *bad_path4 = TEST_APP_DIR "random/example.com/subdir/pkg.apk";
118 EXPECT_EQ(-1, validate_apk_path(bad_path4))
119 << bad_path4 << " should be rejected as a invalid path";
120
121 const char *bad_path5 = TEST_APP_DIR "example.com1/../example.com2/pkg.apk";
122 EXPECT_EQ(-1, validate_apk_path(bad_path5))
123 << bad_path5 << " should be rejected as a invalid path";
124 }
125
TEST_F(UtilsTest,IsValidApkPath_TopDir)126 TEST_F(UtilsTest, IsValidApkPath_TopDir) {
127 EXPECT_EQ(0, validate_apk_path(TEST_DATA_DIR "app/com.example"));
128 EXPECT_EQ(0, validate_apk_path(TEST_DATA_DIR "app/random/com.example"));
129 EXPECT_EQ(0, validate_apk_path(TEST_EXPAND_DIR "app/com.example"));
130 EXPECT_EQ(-1, validate_apk_path(TEST_DATA_DIR "data/com.example"));
131 EXPECT_EQ(-1, validate_apk_path(TEST_EXPAND_DIR "data/com.example"));
132 }
133
TEST_F(UtilsTest,IsValidApkPath_TopFile)134 TEST_F(UtilsTest, IsValidApkPath_TopFile) {
135 EXPECT_EQ(0, validate_apk_path(TEST_DATA_DIR "app/com.example/base.apk"));
136 EXPECT_EQ(0, validate_apk_path(TEST_DATA_DIR "app/random/com.example/base.apk"));
137 EXPECT_EQ(0, validate_apk_path(TEST_EXPAND_DIR "app/com.example/base.apk"));
138 EXPECT_EQ(-1, validate_apk_path(TEST_DATA_DIR "data/com.example/base.apk"));
139 EXPECT_EQ(-1, validate_apk_path(TEST_EXPAND_DIR "data/com.example/base.apk"));
140 }
141
TEST_F(UtilsTest,IsValidApkPath_OatDir)142 TEST_F(UtilsTest, IsValidApkPath_OatDir) {
143 EXPECT_EQ(0, validate_apk_path_subdirs(TEST_DATA_DIR "app/com.example/oat"));
144 EXPECT_EQ(0, validate_apk_path_subdirs(TEST_DATA_DIR "app/random/com.example/oat"));
145 EXPECT_EQ(0, validate_apk_path_subdirs(TEST_EXPAND_DIR "app/com.example/oat"));
146 EXPECT_EQ(-1, validate_apk_path_subdirs(TEST_DATA_DIR "data/com.example/oat"));
147 EXPECT_EQ(-1, validate_apk_path_subdirs(TEST_EXPAND_DIR "data/com.example/oat"));
148 }
149
TEST_F(UtilsTest,IsValidApkPath_OatDirDir)150 TEST_F(UtilsTest, IsValidApkPath_OatDirDir) {
151 EXPECT_EQ(0, validate_apk_path_subdirs(TEST_DATA_DIR "app/com.example/oat/arm64"));
152 EXPECT_EQ(0, validate_apk_path_subdirs(TEST_DATA_DIR "app/random/com.example/oat/arm64"));
153 EXPECT_EQ(0, validate_apk_path_subdirs(TEST_EXPAND_DIR "app/com.example/oat/arm64"));
154 EXPECT_EQ(-1, validate_apk_path_subdirs(TEST_DATA_DIR "data/com.example/oat/arm64"));
155 EXPECT_EQ(-1, validate_apk_path_subdirs(TEST_EXPAND_DIR "data/com.example/oat/arm64"));
156 }
157
TEST_F(UtilsTest,IsValidApkPath_OatDirDirFile)158 TEST_F(UtilsTest, IsValidApkPath_OatDirDirFile) {
159 EXPECT_EQ(0, validate_apk_path_subdirs(TEST_DATA_DIR "app/com.example/oat/arm64/base.odex"));
160 EXPECT_EQ(0, validate_apk_path_subdirs(TEST_DATA_DIR "app/random/com.example/oat/arm64/base.odex"));
161 EXPECT_EQ(0, validate_apk_path_subdirs(TEST_EXPAND_DIR "app/com.example/oat/arm64/base.odex"));
162 EXPECT_EQ(-1, validate_apk_path_subdirs(TEST_DATA_DIR "data/com.example/oat/arm64/base.odex"));
163 EXPECT_EQ(-1, validate_apk_path_subdirs(TEST_EXPAND_DIR "data/com.example/oat/arm64/base.odex"));
164 }
165
TEST_F(UtilsTest,IsValidApkPath_Private)166 TEST_F(UtilsTest, IsValidApkPath_Private) {
167 // Internal directories
168 const char *private1 = TEST_APP_PRIVATE_DIR "example.apk";
169 EXPECT_EQ(0, validate_apk_path(private1))
170 << private1 << " should be allowed as a valid path";
171
172 // b/16888084
173 const char *path2 = TEST_APP_DIR "example.com/example.apk";
174 EXPECT_EQ(0, validate_apk_path(path2))
175 << path2 << " should be allowed as a valid path";
176
177 const char *path3 = TEST_APP_DIR "random/example.com/example.apk";
178 EXPECT_EQ(0, validate_apk_path(path3))
179 << path3 << " should be allowed as a valid path";
180
181 const char *badpriv1 = TEST_APP_PRIVATE_DIR "../example.apk";
182 EXPECT_EQ(-1, validate_apk_path(badpriv1))
183 << badpriv1 << " should be rejected as a invalid path";
184
185 const char *badpriv2 = TEST_APP_PRIVATE_DIR "/../example.apk";
186 EXPECT_EQ(-1, validate_apk_path(badpriv2))
187 << badpriv2 << " should be rejected as a invalid path";
188
189 // Only one or two subdir should be allowed.
190 const char *bad_path3 = TEST_APP_PRIVATE_DIR "random/example.com/subdir/pkg.apk";
191 EXPECT_EQ(-1, validate_apk_path(bad_path3))
192 << bad_path3 << " should be rejected as a invalid path";
193
194 const char *bad_path4 = TEST_APP_PRIVATE_DIR "random/example.com/subdir/../pkg.apk";
195 EXPECT_EQ(-1, validate_apk_path(bad_path4))
196 << bad_path4 << " should be rejected as a invalid path";
197
198 const char *bad_path5 = TEST_APP_PRIVATE_DIR "random/example.com1/../example.com2/pkg.apk";
199 EXPECT_EQ(-1, validate_apk_path(bad_path5))
200 << bad_path5 << " should be rejected as a invalid path";
201 }
202
203
TEST_F(UtilsTest,IsValidApkPath_AsecGood1)204 TEST_F(UtilsTest, IsValidApkPath_AsecGood1) {
205 const char *asec1 = TEST_ASEC_DIR "example.apk";
206 EXPECT_EQ(0, validate_apk_path(asec1))
207 << asec1 << " should be allowed as a valid path";
208 }
209
TEST_F(UtilsTest,IsValidApkPath_AsecGood2)210 TEST_F(UtilsTest, IsValidApkPath_AsecGood2) {
211 const char *asec2 = TEST_ASEC_DIR "com.example.asec/pkg.apk";
212 EXPECT_EQ(0, validate_apk_path(asec2))
213 << asec2 << " should be allowed as a valid path";
214 }
215
TEST_F(UtilsTest,IsValidApkPath_EscapeFail)216 TEST_F(UtilsTest, IsValidApkPath_EscapeFail) {
217 const char *badasec1 = TEST_ASEC_DIR "../example.apk";
218 EXPECT_EQ(-1, validate_apk_path(badasec1))
219 << badasec1 << " should be rejected as a invalid path";
220 }
221
TEST_F(UtilsTest,IsValidApkPath_SubdirEscapeFail)222 TEST_F(UtilsTest, IsValidApkPath_SubdirEscapeFail) {
223 const char *badasec3 = TEST_ASEC_DIR "com.example.asec/../../../pkg.apk";
224 EXPECT_EQ(-1, validate_apk_path(badasec3))
225 << badasec3 << " should be rejected as a invalid path";
226 }
227
TEST_F(UtilsTest,IsValidApkPath_SlashEscapeFail)228 TEST_F(UtilsTest, IsValidApkPath_SlashEscapeFail) {
229 const char *badasec4 = TEST_ASEC_DIR "/../example.apk";
230 EXPECT_EQ(-1, validate_apk_path(badasec4))
231 << badasec4 << " should be rejected as a invalid path";
232 }
233
TEST_F(UtilsTest,IsValidApkPath_CrazyDirFail)234 TEST_F(UtilsTest, IsValidApkPath_CrazyDirFail) {
235 const char *badasec5 = TEST_ASEC_DIR ".//../..";
236 EXPECT_EQ(-1, validate_apk_path(badasec5))
237 << badasec5 << " should be rejected as a invalid path";
238 }
239
TEST_F(UtilsTest,IsValidApkPath_SubdirEscapeSingleFail)240 TEST_F(UtilsTest, IsValidApkPath_SubdirEscapeSingleFail) {
241 const char *badasec6 = TEST_ASEC_DIR "com.example.asec/../pkg.apk";
242 EXPECT_EQ(-1, validate_apk_path(badasec6))
243 << badasec6 << " should be rejected as a invalid path";
244 }
245
TEST_F(UtilsTest,IsValidApkPath_TwoSubdir)246 TEST_F(UtilsTest, IsValidApkPath_TwoSubdir) {
247 const char *badasec7 = TEST_ASEC_DIR "random/com.example.asec/pkg.apk";
248 EXPECT_EQ(0, validate_apk_path(badasec7))
249 << badasec7 << " should be allowed as a valid path";
250 }
251
TEST_F(UtilsTest,IsValidApkPath_ThreeSubdirFail)252 TEST_F(UtilsTest, IsValidApkPath_ThreeSubdirFail) {
253 const char *badasec8 = TEST_ASEC_DIR "random/com.example.asec/subdir/pkg.apk";
254 EXPECT_EQ(-1, validate_apk_path(badasec8))
255 << badasec8 << " should be rejcted as an invalid path";
256 }
257
TEST_F(UtilsTest,CheckSystemApp_Dir1)258 TEST_F(UtilsTest, CheckSystemApp_Dir1) {
259 const char *sysapp1 = TEST_SYSTEM_DIR1 "Voice.apk";
260 EXPECT_EQ(0, validate_system_app_path(sysapp1))
261 << sysapp1 << " should be allowed as a system path";
262 }
263
TEST_F(UtilsTest,CheckSystemApp_Dir2)264 TEST_F(UtilsTest, CheckSystemApp_Dir2) {
265 const char *sysapp2 = TEST_SYSTEM_DIR2 "com.example.myapp.apk";
266 EXPECT_EQ(0, validate_system_app_path(sysapp2))
267 << sysapp2 << " should be allowed as a system path";
268 }
269
TEST_F(UtilsTest,CheckSystemApp_EscapeFail)270 TEST_F(UtilsTest, CheckSystemApp_EscapeFail) {
271 const char *badapp1 = TEST_SYSTEM_DIR1 "../com.example.apk";
272 EXPECT_EQ(-1, validate_system_app_path(badapp1))
273 << badapp1 << " should be rejected not a system path";
274 }
275
TEST_F(UtilsTest,CheckSystemApp_DoubleEscapeFail)276 TEST_F(UtilsTest, CheckSystemApp_DoubleEscapeFail) {
277 const char *badapp2 = TEST_SYSTEM_DIR2 "/../../com.example.apk";
278 EXPECT_EQ(-1, validate_system_app_path(badapp2))
279 << badapp2 << " should be rejected not a system path";
280 }
281
TEST_F(UtilsTest,CheckSystemApp_BadPathEscapeFail)282 TEST_F(UtilsTest, CheckSystemApp_BadPathEscapeFail) {
283 const char *badapp3 = TEST_APP_DIR "/../../com.example.apk";
284 EXPECT_EQ(-1, validate_system_app_path(badapp3))
285 << badapp3 << " should be rejected not a system path";
286 }
287
TEST_F(UtilsTest,CheckSystemApp_Subdir)288 TEST_F(UtilsTest, CheckSystemApp_Subdir) {
289 const char *sysapp = TEST_SYSTEM_DIR1 "com.example/com.example.apk";
290 EXPECT_EQ(0, validate_system_app_path(sysapp))
291 << sysapp << " should be allowed as a system path";
292
293 const char *badapp = TEST_SYSTEM_DIR1 "com.example/subdir/com.example.apk";
294 EXPECT_EQ(-1, validate_system_app_path(badapp))
295 << badapp << " should be rejected not a system path";
296
297 const char *badapp1 = TEST_SYSTEM_DIR1 "com.example/subdir/../com.example.apk";
298 EXPECT_EQ(-1, validate_system_app_path(badapp1))
299 << badapp1 << " should be rejected not a system path";
300
301 const char *badapp2 = TEST_SYSTEM_DIR1 "com.example1/../com.example2/com.example.apk";
302 EXPECT_EQ(-1, validate_system_app_path(badapp2))
303 << badapp2 << " should be rejected not a system path";
304 }
305
TEST_F(UtilsTest,CreateDataPath)306 TEST_F(UtilsTest, CreateDataPath) {
307 EXPECT_EQ("/data", create_data_path(nullptr));
308 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b",
309 create_data_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b"));
310 }
311
TEST_F(UtilsTest,CreateDataAppPath)312 TEST_F(UtilsTest, CreateDataAppPath) {
313 EXPECT_EQ("/data/app", create_data_app_path(nullptr));
314
315 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/app",
316 create_data_app_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b"));
317 }
318
TEST_F(UtilsTest,CreateDataUserPath)319 TEST_F(UtilsTest, CreateDataUserPath) {
320 EXPECT_EQ("/data/data", create_data_user_ce_path(nullptr, 0));
321 EXPECT_EQ("/data/user/10", create_data_user_ce_path(nullptr, 10));
322
323 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/0",
324 create_data_user_ce_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0));
325 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/10",
326 create_data_user_ce_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 10));
327 }
328
TEST_F(UtilsTest,CreateDataMediaPath)329 TEST_F(UtilsTest, CreateDataMediaPath) {
330 EXPECT_EQ("/data/media/0", create_data_media_path(nullptr, 0));
331 EXPECT_EQ("/data/media/10", create_data_media_path(nullptr, 10));
332
333 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/media/0",
334 create_data_media_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0));
335 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/media/10",
336 create_data_media_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 10));
337 }
338
TEST_F(UtilsTest,CreateDataUserPackagePath)339 TEST_F(UtilsTest, CreateDataUserPackagePath) {
340 EXPECT_EQ("/data/data/com.example", create_data_user_ce_package_path(nullptr, 0, "com.example"));
341 EXPECT_EQ("/data/user/10/com.example", create_data_user_ce_package_path(nullptr, 10, "com.example"));
342
343 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/0/com.example",
344 create_data_user_ce_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0, "com.example"));
345 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/10/com.example",
346 create_data_user_ce_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 10, "com.example"));
347 }
348
TEST_F(UtilsTest,IsValidPackageName)349 TEST_F(UtilsTest, IsValidPackageName) {
350 EXPECT_EQ(true, is_valid_package_name("android"));
351 EXPECT_EQ(true, is_valid_package_name("com.example"));
352 EXPECT_EQ(true, is_valid_package_name("com.example-1"));
353 EXPECT_EQ(true, is_valid_package_name("com.example-1024"));
354 EXPECT_EQ(true, is_valid_package_name("com.example.foo---KiJFj4a_tePVw95pSrjg=="));
355 EXPECT_EQ(true, is_valid_package_name("really_LONG.a1234.package_name"));
356
357 EXPECT_EQ(false, is_valid_package_name("1234.package"));
358 EXPECT_EQ(false, is_valid_package_name("com.1234.package"));
359 EXPECT_EQ(false, is_valid_package_name(""));
360 EXPECT_EQ(false, is_valid_package_name("."));
361 EXPECT_EQ(false, is_valid_package_name(".."));
362 EXPECT_EQ(false, is_valid_package_name("../"));
363 EXPECT_EQ(false, is_valid_package_name("com.example/../com.evil/"));
364 EXPECT_EQ(false, is_valid_package_name("com.example-1/../com.evil/"));
365 EXPECT_EQ(false, is_valid_package_name("/com.evil"));
366 }
367
TEST_F(UtilsTest,CreateDataUserProfilePath)368 TEST_F(UtilsTest, CreateDataUserProfilePath) {
369 EXPECT_EQ("/data/misc/profiles/cur/0", create_primary_cur_profile_dir_path(0));
370 EXPECT_EQ("/data/misc/profiles/cur/1", create_primary_cur_profile_dir_path(1));
371 }
372
TEST_F(UtilsTest,CreateDataUserProfilePackagePath)373 TEST_F(UtilsTest, CreateDataUserProfilePackagePath) {
374 EXPECT_EQ("/data/misc/profiles/cur/0/com.example",
375 create_primary_current_profile_package_dir_path(0, "com.example"));
376 EXPECT_EQ("/data/misc/profiles/cur/1/com.example",
377 create_primary_current_profile_package_dir_path(1, "com.example"));
378 }
379
TEST_F(UtilsTest,CreateDataRefProfilePath)380 TEST_F(UtilsTest, CreateDataRefProfilePath) {
381 EXPECT_EQ("/data/misc/profiles/ref", create_primary_ref_profile_dir_path());
382 }
383
TEST_F(UtilsTest,CreateDataRefProfilePackagePath)384 TEST_F(UtilsTest, CreateDataRefProfilePackagePath) {
385 EXPECT_EQ("/data/misc/profiles/ref/com.example",
386 create_primary_reference_profile_package_dir_path("com.example"));
387 }
388
TEST_F(UtilsTest,CreatePrimaryCurrentProfile)389 TEST_F(UtilsTest, CreatePrimaryCurrentProfile) {
390 std::string expected_base =
391 create_primary_current_profile_package_dir_path(0, "com.example") + "/primary.prof";
392 EXPECT_EQ(expected_base,
393 create_current_profile_path(/*user*/0, "com.example", "primary.prof",
394 /*is_secondary*/false));
395
396 std::string expected_split =
397 create_primary_current_profile_package_dir_path(0, "com.example") + "/split.prof";
398 EXPECT_EQ(expected_split,
399 create_current_profile_path(/*user*/0, "com.example", "split.prof",
400 /*is_secondary*/false));
401 }
402
TEST_F(UtilsTest,CreatePrimaryReferenceProfile)403 TEST_F(UtilsTest, CreatePrimaryReferenceProfile) {
404 std::string expected_base =
405 create_primary_reference_profile_package_dir_path("com.example") + "/primary.prof";
406 EXPECT_EQ(expected_base,
407 create_reference_profile_path("com.example", "primary.prof", /*is_secondary*/false));
408
409 std::string expected_split =
410 create_primary_reference_profile_package_dir_path("com.example") + "/split.prof";
411 EXPECT_EQ(expected_split,
412 create_reference_profile_path("com.example", "split.prof", /*is_secondary*/false));
413 }
414
TEST_F(UtilsTest,CreateProfileSnapshot)415 TEST_F(UtilsTest, CreateProfileSnapshot) {
416 std::string expected_base =
417 create_primary_reference_profile_package_dir_path("com.example") + "/primary.prof.snapshot";
418 EXPECT_EQ(expected_base, create_snapshot_profile_path("com.example", "primary.prof"));
419
420 std::string expected_split =
421 create_primary_reference_profile_package_dir_path("com.example") + "/split.prof.snapshot";
422 EXPECT_EQ(expected_split, create_snapshot_profile_path("com.example", "split.prof"));
423 }
424
TEST_F(UtilsTest,CreateSecondaryCurrentProfile)425 TEST_F(UtilsTest, CreateSecondaryCurrentProfile) {
426 EXPECT_EQ("/data/user/0/com.example/oat/secondary.dex.cur.prof",
427 create_current_profile_path(/*user*/0, "com.example",
428 "/data/user/0/com.example/secondary.dex", /*is_secondary*/true));
429 }
430
TEST_F(UtilsTest,CreateSecondaryReferenceProfile)431 TEST_F(UtilsTest, CreateSecondaryReferenceProfile) {
432 EXPECT_EQ("/data/user/0/com.example/oat/secondary.dex.prof",
433 create_reference_profile_path("com.example",
434 "/data/user/0/com.example/secondary.dex", /*is_secondary*/true));
435 }
436
pass_secondary_dex_validation(const std::string & package_name,const std::string & dex_path,int uid,int storage_flag)437 static void pass_secondary_dex_validation(const std::string& package_name,
438 const std::string& dex_path, int uid, int storage_flag) {
439 EXPECT_TRUE(validate_secondary_dex_path(package_name, dex_path, /*volume_uuid*/ nullptr, uid,
440 storage_flag))
441 << dex_path << " should be allowed as a valid secondary dex path";
442 }
443
fail_secondary_dex_validation(const std::string & package_name,const std::string & dex_path,int uid,int storage_flag)444 static void fail_secondary_dex_validation(const std::string& package_name,
445 const std::string& dex_path, int uid, int storage_flag) {
446 EXPECT_FALSE(validate_secondary_dex_path(package_name, dex_path, /*volume_uuid*/ nullptr, uid,
447 storage_flag))
448 << dex_path << " should not be allowed as a valid secondary dex path";
449 }
450
TEST_F(UtilsTest,ValidateSecondaryDexFilesPath)451 TEST_F(UtilsTest, ValidateSecondaryDexFilesPath) {
452 std::string package_name = "com.test.app";
453 std::string app_dir_ce_user_0 = "/data/data/" + package_name;
454 std::string app_dir_ce_user_0_link = "/data/user/0/" + package_name;
455 std::string app_dir_ce_user_10 = "/data/user/10/" + package_name;
456
457 std::string app_dir_de_user_0 = "/data/user_de/0/" + package_name;
458 std::string app_dir_de_user_10 = "/data/user_de/10/" + package_name;
459
460 EXPECT_EQ(app_dir_ce_user_0,
461 create_data_user_ce_package_path(nullptr, 0, package_name.c_str()));
462 EXPECT_EQ(app_dir_ce_user_10,
463 create_data_user_ce_package_path(nullptr, 10, package_name.c_str()));
464
465 EXPECT_EQ(app_dir_de_user_0,
466 create_data_user_de_package_path(nullptr, 0, package_name.c_str()));
467 EXPECT_EQ(app_dir_de_user_10,
468 create_data_user_de_package_path(nullptr, 10, package_name.c_str()));
469
470 uid_t app_uid_for_user_0 = multiuser_get_uid(/*user_id*/0, /*app_id*/ 1234);
471 uid_t app_uid_for_user_10 = multiuser_get_uid(/*user_id*/10, /*app_id*/ 1234);
472
473 // Standard path for user 0 on CE storage.
474 pass_secondary_dex_validation(
475 package_name, app_dir_ce_user_0 + "/ce0.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
476 pass_secondary_dex_validation(
477 package_name, app_dir_ce_user_0_link + "/ce0.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
478 // Standard path for user 10 on CE storage.
479 pass_secondary_dex_validation(
480 package_name, app_dir_ce_user_10 + "/ce10.dex", app_uid_for_user_10, FLAG_STORAGE_CE);
481
482 // Standard path for user 0 on DE storage.
483 pass_secondary_dex_validation(
484 package_name, app_dir_de_user_0 + "/de0.dex", app_uid_for_user_0, FLAG_STORAGE_DE);
485 // Standard path for user 10 on DE storage.
486 pass_secondary_dex_validation(
487 package_name, app_dir_de_user_10 + "/de0.dex", app_uid_for_user_10, FLAG_STORAGE_DE);
488
489 // Dex path for user 0 accessed from user 10.
490 fail_secondary_dex_validation(
491 package_name, app_dir_ce_user_0 + "/path0_from10.dex",
492 app_uid_for_user_10, FLAG_STORAGE_CE);
493
494 // Dex path for CE storage accessed with DE.
495 fail_secondary_dex_validation(
496 package_name, app_dir_ce_user_0 + "/ce_from_de.dex", app_uid_for_user_0, FLAG_STORAGE_DE);
497
498 // Dex path for DE storage accessed with CE.
499 fail_secondary_dex_validation(
500 package_name, app_dir_de_user_0 + "/de_from_ce.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
501
502 // Location which does not start with '/'.
503 fail_secondary_dex_validation(
504 package_name, "without_slash.dex", app_uid_for_user_10, FLAG_STORAGE_DE);
505
506 // The dex file is not in the specified package directory.
507 fail_secondary_dex_validation(
508 "another.package", app_dir_ce_user_0 + "/for_another_package.dex",
509 app_uid_for_user_0, FLAG_STORAGE_DE);
510
511 // The dex path contains indirect directories.
512 fail_secondary_dex_validation(
513 package_name, app_dir_ce_user_0 + "/1/../foo.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
514 fail_secondary_dex_validation(
515 package_name, app_dir_ce_user_0 + "/1/./foo.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
516
517 // Super long path.
518 std::string too_long = create_too_long_path("too_long_");
519 fail_secondary_dex_validation(
520 package_name, app_dir_ce_user_10 + "/" + too_long, app_uid_for_user_10, FLAG_STORAGE_CE);
521 }
522
TEST_F(UtilsTest,ValidateApkPath)523 TEST_F(UtilsTest, ValidateApkPath) {
524 EXPECT_EQ(0, validate_apk_path("/data/app/com.example"));
525 EXPECT_EQ(0, validate_apk_path("/data/app/com.example/file"));
526 EXPECT_EQ(0, validate_apk_path("/data/app/com.example//file"));
527 EXPECT_EQ(0, validate_apk_path("/data/app/random/com.example/"));
528 EXPECT_EQ(0, validate_apk_path("/data/app/random/com.example/file"));
529 EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/dir/file"));
530 EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/dir//file"));
531 EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/dir/dir/file"));
532 EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/dir/dir//file"));
533 }
534
TEST_F(UtilsTest,ValidateApkPathSubdirs)535 TEST_F(UtilsTest, ValidateApkPathSubdirs) {
536 EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example"));
537 EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/file"));
538 EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example//file"));
539 EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/"));
540 EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/file"));
541 EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir/file"));
542 EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir//file"));
543 EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir/dir/file"));
544 EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir/dir//file"));
545 EXPECT_NE(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir/dir/dir/file"));
546 EXPECT_NE(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir/dir/dir//file"));
547 }
548
TEST_F(UtilsTest,MatchExtension_Valid)549 TEST_F(UtilsTest, MatchExtension_Valid) {
550 EXPECT_EQ(AID_MEDIA_VIDEO, MatchExtension("mpg"));
551 EXPECT_EQ(AID_MEDIA_VIDEO, MatchExtension("mpeg"));
552 EXPECT_EQ(AID_MEDIA_VIDEO, MatchExtension("mPeG"));
553 EXPECT_EQ(AID_MEDIA_VIDEO, MatchExtension("MPEG"));
554 }
555
TEST_F(UtilsTest,MatchExtension_Invalid)556 TEST_F(UtilsTest, MatchExtension_Invalid) {
557 EXPECT_EQ(0, MatchExtension("log"));
558 EXPECT_EQ(0, MatchExtension("3amp"));
559 EXPECT_EQ(0, MatchExtension("fpe"));
560 EXPECT_EQ(0, MatchExtension("docx"));
561 }
562
TEST_F(UtilsTest,TestIsRenamedDeletedDir)563 TEST_F(UtilsTest, TestIsRenamedDeletedDir) {
564 EXPECT_FALSE(is_renamed_deleted_dir(""));
565 EXPECT_FALSE(is_renamed_deleted_dir("1"));
566 EXPECT_FALSE(is_renamed_deleted_dir("="));
567 EXPECT_FALSE(is_renamed_deleted_dir("=="));
568 EXPECT_FALSE(is_renamed_deleted_dir("d=="));
569 EXPECT_FALSE(is_renamed_deleted_dir("ed=="));
570 EXPECT_FALSE(is_renamed_deleted_dir("ted=="));
571 EXPECT_FALSE(is_renamed_deleted_dir("eted=="));
572 EXPECT_FALSE(is_renamed_deleted_dir("leted=="));
573 EXPECT_FALSE(is_renamed_deleted_dir("eleted=="));
574 EXPECT_FALSE(is_renamed_deleted_dir("deleted=="));
575 EXPECT_FALSE(is_renamed_deleted_dir("=deleted=="));
576 EXPECT_TRUE(is_renamed_deleted_dir("==deleted=="));
577 EXPECT_TRUE(is_renamed_deleted_dir("123==deleted=="));
578 EXPECT_TRUE(is_renamed_deleted_dir("5b14b6458a44==deleted=="));
579 }
580
TEST_F(UtilsTest,TestRollbackPaths)581 TEST_F(UtilsTest, TestRollbackPaths) {
582 EXPECT_EQ("/data/misc_ce/0/rollback/239/com.foo",
583 create_data_misc_ce_rollback_package_path(nullptr, 0, 239, "com.foo"));
584 EXPECT_EQ("/data/misc_ce/10/rollback/37/com.foo",
585 create_data_misc_ce_rollback_package_path(nullptr, 10, 37, "com.foo"));
586
587 EXPECT_EQ("/data/misc_de/0/rollback/73/com.foo",
588 create_data_misc_de_rollback_package_path(nullptr, 0, 73, "com.foo"));
589 EXPECT_EQ("/data/misc_de/10/rollback/13/com.foo",
590 create_data_misc_de_rollback_package_path(nullptr, 10, 13, "com.foo"));
591
592 EXPECT_EQ("/data/misc_ce/0/rollback/57",
593 create_data_misc_ce_rollback_path(nullptr, 0, 57));
594 EXPECT_EQ("/data/misc_ce/10/rollback/1543",
595 create_data_misc_ce_rollback_path(nullptr, 10, 1543));
596
597 EXPECT_EQ("/data/misc_de/0/rollback/43",
598 create_data_misc_de_rollback_path(nullptr, 0, 43));
599 EXPECT_EQ("/data/misc_de/10/rollback/41",
600 create_data_misc_de_rollback_path(nullptr, 10, 41));
601
602 EXPECT_EQ("/data/misc_ce/0/rollback/17/com.foo",
603 create_data_misc_ce_rollback_package_path(nullptr, 0, 17, "com.foo", 0));
604 EXPECT_EQ("/data/misc_ce/0/rollback/19/com.foo",
605 create_data_misc_ce_rollback_package_path(nullptr, 0, 19, "com.foo", 239));
606
607 auto rollback_ce_path = create_data_misc_ce_rollback_path(nullptr, 0, 53);
608 auto rollback_ce_package_path = create_data_misc_ce_rollback_package_path(nullptr, 0, 53,
609 "com.foo");
610 auto deleter = [&rollback_ce_path]() {
611 delete_dir_contents_and_dir(rollback_ce_path, true /* ignore_if_missing */);
612 };
613 auto scope_guard = android::base::make_scope_guard(deleter);
614
615 EXPECT_NE(-1, mkdir(rollback_ce_path.c_str(), 700));
616 EXPECT_NE(-1, mkdir(rollback_ce_package_path.c_str(), 700));
617
618 ino_t ce_data_inode;
619 EXPECT_EQ(0, get_path_inode(rollback_ce_package_path, &ce_data_inode));
620
621 EXPECT_EQ("/data/misc_ce/0/rollback/53/com.foo",
622 create_data_misc_ce_rollback_package_path(nullptr, 0, 53, "com.foo", ce_data_inode));
623 // Check that path defined by inode is picked even if it's not the same as
624 // the fallback one.
625 EXPECT_EQ("/data/misc_ce/0/rollback/53/com.foo",
626 create_data_misc_ce_rollback_package_path(nullptr, 0, 53, "com.bar", ce_data_inode));
627
628 // These last couple of cases are never exercised in production because we
629 // only snapshot apps in the primary data partition. Exercise them here for
630 // the sake of completeness.
631 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/misc_ce/0/rollback/7/com.example",
632 create_data_misc_ce_rollback_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0, 7,
633 "com.example"));
634 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/misc_de/0/rollback/11/com.example",
635 create_data_misc_de_rollback_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0, 11,
636 "com.example"));
637 }
638
TEST_F(UtilsTest,TestCreateDirIfNeeded)639 TEST_F(UtilsTest, TestCreateDirIfNeeded) {
640 system("mkdir -p /data/local/tmp/user/0");
641
642 auto deleter = [&]() {
643 delete_dir_contents_and_dir("/data/local/tmp/user/0", true /* ignore_if_missing */);
644 };
645 auto scope_guard = android::base::make_scope_guard(deleter);
646
647 // Create folder and check it's permissions.
648 ASSERT_EQ(0, create_dir_if_needed("/data/local/tmp/user/0/foo", 0700));
649 struct stat st;
650 ASSERT_EQ(0, stat("/data/local/tmp/user/0/foo", &st));
651 ASSERT_EQ(0700, st.st_mode & ALLPERMS);
652
653 // Check that create_dir_if_needed is no-op if folder already exists with
654 // correct permissions.
655 ASSERT_EQ(0, create_dir_if_needed("/data/local/tmp/user/0/foo", 0700));
656
657 // Check -1 is returned if folder exists but with different permissions.
658 ASSERT_EQ(-1, create_dir_if_needed("/data/local/tmp/user/0/foo", 0750));
659
660 // Check that call fails if parent doesn't exist.
661 ASSERT_NE(0, create_dir_if_needed("/data/local/tmp/user/0/bar/baz", 0700));
662 }
663
TEST_F(UtilsTest,TestForEachSubdir)664 TEST_F(UtilsTest, TestForEachSubdir) {
665 auto deleter = [&]() {
666 delete_dir_contents_and_dir("/data/local/tmp/user/0", true /* ignore_if_missing */);
667 };
668 auto scope_guard = android::base::make_scope_guard(deleter);
669
670 system("mkdir -p /data/local/tmp/user/0/com.foo");
671 system("mkdir -p /data/local/tmp/user/0/com.bar");
672 system("touch /data/local/tmp/user/0/some-file");
673
674 std::vector<std::string> result;
675 foreach_subdir("/data/local/tmp/user/0",
676 [&](const std::string &filename) { result.push_back(filename); });
677
678 EXPECT_THAT(result, UnorderedElementsAre("com.foo", "com.bar"));
679 }
680
TEST_F(UtilsTest,TestSdkSandboxDataPaths)681 TEST_F(UtilsTest, TestSdkSandboxDataPaths) {
682 // Ce data paths
683 EXPECT_EQ("/data/misc_ce/0/sdksandbox",
684 create_data_misc_sdk_sandbox_path(nullptr, /*isCeData=*/true, 0));
685 EXPECT_EQ("/data/misc_ce/10/sdksandbox", create_data_misc_sdk_sandbox_path(nullptr, true, 10));
686
687 EXPECT_EQ("/data/misc_ce/0/sdksandbox/com.foo",
688 create_data_misc_sdk_sandbox_package_path(nullptr, true, 0, "com.foo"));
689 EXPECT_EQ("/data/misc_ce/10/sdksandbox/com.foo",
690 create_data_misc_sdk_sandbox_package_path(nullptr, true, 10, "com.foo"));
691
692 EXPECT_EQ("/data/misc_ce/0/sdksandbox/com.foo/shared",
693 create_data_misc_sdk_sandbox_sdk_path(nullptr, true, 0, "com.foo", "shared"));
694 EXPECT_EQ("/data/misc_ce/10/sdksandbox/com.foo/shared",
695 create_data_misc_sdk_sandbox_sdk_path(nullptr, true, 10, "com.foo", "shared"));
696 EXPECT_EQ("/data/misc_ce/10/sdksandbox/com.foo/bar@random",
697 create_data_misc_sdk_sandbox_sdk_path(nullptr, true, 10, "com.foo", "bar@random"));
698
699 // De data paths
700 EXPECT_EQ("/data/misc_de/0/sdksandbox",
701 create_data_misc_sdk_sandbox_path(nullptr, /*isCeData=*/false, 0));
702 EXPECT_EQ("/data/misc_de/10/sdksandbox", create_data_misc_sdk_sandbox_path(nullptr, false, 10));
703
704 EXPECT_EQ("/data/misc_de/0/sdksandbox/com.foo",
705 create_data_misc_sdk_sandbox_package_path(nullptr, false, 0, "com.foo"));
706 EXPECT_EQ("/data/misc_de/10/sdksandbox/com.foo",
707 create_data_misc_sdk_sandbox_package_path(nullptr, false, 10, "com.foo"));
708
709 EXPECT_EQ("/data/misc_de/0/sdksandbox/com.foo/shared",
710 create_data_misc_sdk_sandbox_sdk_path(nullptr, false, 0, "com.foo", "shared"));
711 EXPECT_EQ("/data/misc_de/10/sdksandbox/com.foo/shared",
712 create_data_misc_sdk_sandbox_sdk_path(nullptr, false, 10, "com.foo", "shared"));
713 EXPECT_EQ("/data/misc_de/10/sdksandbox/com.foo/bar@random",
714 create_data_misc_sdk_sandbox_sdk_path(nullptr, false, 10, "com.foo", "bar@random"));
715 }
716
TEST_F(UtilsTest,WaitChild)717 TEST_F(UtilsTest, WaitChild) {
718 pid_t pid = fork();
719 if (pid == 0) {
720 /* child */
721 // Do nothing.
722 _exit(0);
723 }
724 /* parent */
725 int return_code = wait_child_with_timeout(pid, /*timeout_ms=*/100);
726 EXPECT_TRUE(WIFEXITED(return_code));
727 EXPECT_EQ(WEXITSTATUS(return_code), 0);
728 }
729
TEST_F(UtilsTest,WaitChildTimeout)730 TEST_F(UtilsTest, WaitChildTimeout) {
731 pid_t pid = fork();
732 if (pid == 0) {
733 /* child */
734 sleep(1);
735 _exit(0);
736 }
737 /* parent */
738 int return_code = wait_child_with_timeout(pid, /*timeout_ms=*/1);
739 EXPECT_FALSE(WIFEXITED(return_code));
740 EXPECT_EQ(WTERMSIG(return_code), SIGKILL);
741 }
742
TEST_F(UtilsTest,RemoveFileAtFd)743 TEST_F(UtilsTest, RemoveFileAtFd) {
744 std::string filename = "/data/local/tmp/tempfile-XXXXXX";
745 int fd = mkstemp(filename.data());
746 ASSERT_GE(fd, 0);
747 ASSERT_EQ(access(filename.c_str(), F_OK), 0);
748
749 std::string actual_filename;
750 remove_file_at_fd(fd, &actual_filename);
751 EXPECT_NE(access(filename.c_str(), F_OK), 0);
752 EXPECT_EQ(filename, actual_filename);
753
754 close(fd);
755 }
756
757 } // namespace installd
758 } // namespace android
759