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