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 <gtest/gtest.h>
21
22 #include "InstalldNativeService.h"
23 #include "globals.h"
24 #include "utils.h"
25
26 #undef LOG_TAG
27 #define LOG_TAG "utils_test"
28
29 #define TEST_DATA_DIR "/data/"
30 #define TEST_APP_DIR "/data/app/"
31 #define TEST_APP_PRIVATE_DIR "/data/app-private/"
32 #define TEST_APP_EPHEMERAL_DIR "/data/app-ephemeral/"
33 #define TEST_ASEC_DIR "/mnt/asec/"
34 #define TEST_EXPAND_DIR "/mnt/expand/"
35
36 #define TEST_SYSTEM_DIR1 "/system/app/"
37 #define TEST_SYSTEM_DIR2 "/vendor/app/"
38
39 #define TEST_PROFILE_DIR "/data/misc/profiles"
40
41 namespace android {
42 namespace installd {
43
44 class UtilsTest : public testing::Test {
45 protected:
SetUp()46 virtual void SetUp() {
47 android_app_dir.path = (char*) TEST_APP_DIR;
48 android_app_dir.len = strlen(TEST_APP_DIR);
49
50 android_app_private_dir.path = (char*) TEST_APP_PRIVATE_DIR;
51 android_app_private_dir.len = strlen(TEST_APP_PRIVATE_DIR);
52
53 android_app_ephemeral_dir.path = (char*) TEST_APP_EPHEMERAL_DIR;
54 android_app_ephemeral_dir.len = strlen(TEST_APP_EPHEMERAL_DIR);
55
56 android_data_dir.path = (char*) TEST_DATA_DIR;
57 android_data_dir.len = strlen(TEST_DATA_DIR);
58
59 android_asec_dir.path = (char*) TEST_ASEC_DIR;
60 android_asec_dir.len = strlen(TEST_ASEC_DIR);
61
62 android_mnt_expand_dir.path = (char*) TEST_EXPAND_DIR;
63 android_mnt_expand_dir.len = strlen(TEST_EXPAND_DIR);
64
65 android_system_dirs.count = 2;
66
67 android_system_dirs.dirs = (dir_rec_t*) calloc(android_system_dirs.count, sizeof(dir_rec_t));
68 android_system_dirs.dirs[0].path = (char*) TEST_SYSTEM_DIR1;
69 android_system_dirs.dirs[0].len = strlen(TEST_SYSTEM_DIR1);
70
71 android_system_dirs.dirs[1].path = (char*) TEST_SYSTEM_DIR2;
72 android_system_dirs.dirs[1].len = strlen(TEST_SYSTEM_DIR2);
73
74 android_profiles_dir.path = (char*) TEST_PROFILE_DIR;
75 android_profiles_dir.len = strlen(TEST_PROFILE_DIR);
76 }
77
TearDown()78 virtual void TearDown() {
79 free(android_system_dirs.dirs);
80 }
81
create_too_long_path(const std::string & seed)82 std::string create_too_long_path(const std::string& seed) {
83 std::string result = seed;
84 for (size_t i = seed.size(); i < PKG_PATH_MAX; i++) {
85 result += "a";
86 }
87 return result;
88 }
89 };
90
TEST_F(UtilsTest,IsValidApkPath_BadPrefix)91 TEST_F(UtilsTest, IsValidApkPath_BadPrefix) {
92 // Bad prefixes directories
93 const char *badprefix1 = "/etc/passwd";
94 EXPECT_EQ(-1, validate_apk_path(badprefix1))
95 << badprefix1 << " should not be allowed as a valid path";
96
97 const char *badprefix2 = "../.." TEST_APP_DIR "../../../blah";
98 EXPECT_EQ(-1, validate_apk_path(badprefix2))
99 << badprefix2 << " should not be allowed as a valid path";
100
101 const char *badprefix3 = "init.rc";
102 EXPECT_EQ(-1, validate_apk_path(badprefix3))
103 << badprefix3 << " should not be allowed as a valid path";
104
105 const char *badprefix4 = "/init.rc";
106 EXPECT_EQ(-1, validate_apk_path(badprefix4))
107 << badprefix4 << " should not be allowed as a valid path";
108 }
109
TEST_F(UtilsTest,IsValidApkPath_Internal)110 TEST_F(UtilsTest, IsValidApkPath_Internal) {
111 // Internal directories
112 const char *internal1 = TEST_APP_DIR "example.apk";
113 EXPECT_EQ(0, validate_apk_path(internal1))
114 << internal1 << " should be allowed as a valid path";
115
116 // b/16888084
117 const char *path2 = TEST_APP_DIR "example.com/example.apk";
118 EXPECT_EQ(0, validate_apk_path(path2))
119 << path2 << " should be allowed as a valid path";
120
121 const char *badint1 = TEST_APP_DIR "../example.apk";
122 EXPECT_EQ(-1, validate_apk_path(badint1))
123 << badint1 << " should be rejected as a invalid path";
124
125 const char *badint2 = TEST_APP_DIR "/../example.apk";
126 EXPECT_EQ(-1, validate_apk_path(badint2))
127 << badint2 << " should be rejected as a invalid path";
128
129 // Only one subdir should be allowed.
130 const char *bad_path3 = TEST_APP_DIR "example.com/subdir/pkg.apk";
131 EXPECT_EQ(-1, validate_apk_path(bad_path3))
132 << bad_path3 << " should be rejected as a invalid path";
133
134 const char *bad_path4 = TEST_APP_DIR "example.com/subdir/../pkg.apk";
135 EXPECT_EQ(-1, validate_apk_path(bad_path4))
136 << bad_path4 << " should be rejected as a invalid path";
137
138 const char *bad_path5 = TEST_APP_DIR "example.com1/../example.com2/pkg.apk";
139 EXPECT_EQ(-1, validate_apk_path(bad_path5))
140 << bad_path5 << " should be rejected as a invalid path";
141 }
142
TEST_F(UtilsTest,IsValidApkPath_Private)143 TEST_F(UtilsTest, IsValidApkPath_Private) {
144 // Internal directories
145 const char *private1 = TEST_APP_PRIVATE_DIR "example.apk";
146 EXPECT_EQ(0, validate_apk_path(private1))
147 << private1 << " should be allowed as a valid path";
148
149 // b/16888084
150 const char *path2 = TEST_APP_DIR "example.com/example.apk";
151 EXPECT_EQ(0, validate_apk_path(path2))
152 << path2 << " should be allowed as a valid path";
153
154 const char *badpriv1 = TEST_APP_PRIVATE_DIR "../example.apk";
155 EXPECT_EQ(-1, validate_apk_path(badpriv1))
156 << badpriv1 << " should be rejected as a invalid path";
157
158 const char *badpriv2 = TEST_APP_PRIVATE_DIR "/../example.apk";
159 EXPECT_EQ(-1, validate_apk_path(badpriv2))
160 << badpriv2 << " should be rejected as a invalid path";
161
162 // Only one subdir should be allowed.
163 const char *bad_path3 = TEST_APP_PRIVATE_DIR "example.com/subdir/pkg.apk";
164 EXPECT_EQ(-1, validate_apk_path(bad_path3))
165 << bad_path3 << " should be rejected as a invalid path";
166
167 const char *bad_path4 = TEST_APP_PRIVATE_DIR "example.com/subdir/../pkg.apk";
168 EXPECT_EQ(-1, validate_apk_path(bad_path4))
169 << bad_path4 << " should be rejected as a invalid path";
170
171 const char *bad_path5 = TEST_APP_PRIVATE_DIR "example.com1/../example.com2/pkg.apk";
172 EXPECT_EQ(-1, validate_apk_path(bad_path5))
173 << bad_path5 << " should be rejected as a invalid path";
174 }
175
176
TEST_F(UtilsTest,IsValidApkPath_AsecGood1)177 TEST_F(UtilsTest, IsValidApkPath_AsecGood1) {
178 const char *asec1 = TEST_ASEC_DIR "example.apk";
179 EXPECT_EQ(0, validate_apk_path(asec1))
180 << asec1 << " should be allowed as a valid path";
181 }
182
TEST_F(UtilsTest,IsValidApkPath_AsecGood2)183 TEST_F(UtilsTest, IsValidApkPath_AsecGood2) {
184 const char *asec2 = TEST_ASEC_DIR "com.example.asec/pkg.apk";
185 EXPECT_EQ(0, validate_apk_path(asec2))
186 << asec2 << " should be allowed as a valid path";
187 }
188
TEST_F(UtilsTest,IsValidApkPath_EscapeFail)189 TEST_F(UtilsTest, IsValidApkPath_EscapeFail) {
190 const char *badasec1 = TEST_ASEC_DIR "../example.apk";
191 EXPECT_EQ(-1, validate_apk_path(badasec1))
192 << badasec1 << " should be rejected as a invalid path";
193 }
194
TEST_F(UtilsTest,IsValidApkPath_DoubleSlashFail)195 TEST_F(UtilsTest, IsValidApkPath_DoubleSlashFail) {
196 const char *badasec2 = TEST_ASEC_DIR "com.example.asec//pkg.apk";
197 EXPECT_EQ(-1, validate_apk_path(badasec2))
198 << badasec2 << " should be rejected as a invalid path";
199 }
200
TEST_F(UtilsTest,IsValidApkPath_SubdirEscapeFail)201 TEST_F(UtilsTest, IsValidApkPath_SubdirEscapeFail) {
202 const char *badasec3 = TEST_ASEC_DIR "com.example.asec/../../../pkg.apk";
203 EXPECT_EQ(-1, validate_apk_path(badasec3))
204 << badasec3 << " should be rejected as a invalid path";
205 }
206
TEST_F(UtilsTest,IsValidApkPath_SlashEscapeFail)207 TEST_F(UtilsTest, IsValidApkPath_SlashEscapeFail) {
208 const char *badasec4 = TEST_ASEC_DIR "/../example.apk";
209 EXPECT_EQ(-1, validate_apk_path(badasec4))
210 << badasec4 << " should be rejected as a invalid path";
211 }
212
TEST_F(UtilsTest,IsValidApkPath_CrazyDirFail)213 TEST_F(UtilsTest, IsValidApkPath_CrazyDirFail) {
214 const char *badasec5 = TEST_ASEC_DIR ".//../..";
215 EXPECT_EQ(-1, validate_apk_path(badasec5))
216 << badasec5 << " should be rejected as a invalid path";
217 }
218
TEST_F(UtilsTest,IsValidApkPath_SubdirEscapeSingleFail)219 TEST_F(UtilsTest, IsValidApkPath_SubdirEscapeSingleFail) {
220 const char *badasec6 = TEST_ASEC_DIR "com.example.asec/../pkg.apk";
221 EXPECT_EQ(-1, validate_apk_path(badasec6))
222 << badasec6 << " should be rejected as a invalid path";
223 }
224
TEST_F(UtilsTest,IsValidApkPath_TwoSubdirFail)225 TEST_F(UtilsTest, IsValidApkPath_TwoSubdirFail) {
226 const char *badasec7 = TEST_ASEC_DIR "com.example.asec/subdir1/pkg.apk";
227 EXPECT_EQ(-1, validate_apk_path(badasec7))
228 << badasec7 << " should be rejected as a invalid path";
229 }
230
TEST_F(UtilsTest,CheckSystemApp_Dir1)231 TEST_F(UtilsTest, CheckSystemApp_Dir1) {
232 const char *sysapp1 = TEST_SYSTEM_DIR1 "Voice.apk";
233 EXPECT_EQ(0, validate_system_app_path(sysapp1))
234 << sysapp1 << " should be allowed as a system path";
235 }
236
TEST_F(UtilsTest,CheckSystemApp_Dir2)237 TEST_F(UtilsTest, CheckSystemApp_Dir2) {
238 const char *sysapp2 = TEST_SYSTEM_DIR2 "com.example.myapp.apk";
239 EXPECT_EQ(0, validate_system_app_path(sysapp2))
240 << sysapp2 << " should be allowed as a system path";
241 }
242
TEST_F(UtilsTest,CheckSystemApp_EscapeFail)243 TEST_F(UtilsTest, CheckSystemApp_EscapeFail) {
244 const char *badapp1 = TEST_SYSTEM_DIR1 "../com.example.apk";
245 EXPECT_EQ(-1, validate_system_app_path(badapp1))
246 << badapp1 << " should be rejected not a system path";
247 }
248
TEST_F(UtilsTest,CheckSystemApp_DoubleEscapeFail)249 TEST_F(UtilsTest, CheckSystemApp_DoubleEscapeFail) {
250 const char *badapp2 = TEST_SYSTEM_DIR2 "/../../com.example.apk";
251 EXPECT_EQ(-1, validate_system_app_path(badapp2))
252 << badapp2 << " should be rejected not a system path";
253 }
254
TEST_F(UtilsTest,CheckSystemApp_BadPathEscapeFail)255 TEST_F(UtilsTest, CheckSystemApp_BadPathEscapeFail) {
256 const char *badapp3 = TEST_APP_DIR "/../../com.example.apk";
257 EXPECT_EQ(-1, validate_system_app_path(badapp3))
258 << badapp3 << " should be rejected not a system path";
259 }
260
TEST_F(UtilsTest,CheckSystemApp_Subdir)261 TEST_F(UtilsTest, CheckSystemApp_Subdir) {
262 const char *sysapp = TEST_SYSTEM_DIR1 "com.example/com.example.apk";
263 EXPECT_EQ(0, validate_system_app_path(sysapp))
264 << sysapp << " should be allowed as a system path";
265
266 const char *badapp = TEST_SYSTEM_DIR1 "com.example/subdir/com.example.apk";
267 EXPECT_EQ(-1, validate_system_app_path(badapp))
268 << badapp << " should be rejected not a system path";
269
270 const char *badapp1 = TEST_SYSTEM_DIR1 "com.example/subdir/../com.example.apk";
271 EXPECT_EQ(-1, validate_system_app_path(badapp1))
272 << badapp1 << " should be rejected not a system path";
273
274 const char *badapp2 = TEST_SYSTEM_DIR1 "com.example1/../com.example2/com.example.apk";
275 EXPECT_EQ(-1, validate_system_app_path(badapp2))
276 << badapp2 << " should be rejected not a system path";
277 }
278
TEST_F(UtilsTest,GetPathFromString_NullPathFail)279 TEST_F(UtilsTest, GetPathFromString_NullPathFail) {
280 dir_rec_t test1;
281 EXPECT_EQ(-1, get_path_from_string(&test1, (const char *) NULL))
282 << "Should not allow NULL as a path.";
283 }
284
TEST_F(UtilsTest,GetPathFromString_EmptyPathFail)285 TEST_F(UtilsTest, GetPathFromString_EmptyPathFail) {
286 dir_rec_t test1;
287 EXPECT_EQ(-1, get_path_from_string(&test1, ""))
288 << "Should not allow empty paths.";
289 }
290
TEST_F(UtilsTest,GetPathFromString_RelativePathFail)291 TEST_F(UtilsTest, GetPathFromString_RelativePathFail) {
292 dir_rec_t test1;
293 EXPECT_EQ(-1, get_path_from_string(&test1, "mnt/asec"))
294 << "Should not allow relative paths.";
295 }
296
TEST_F(UtilsTest,GetPathFromString_NonCanonical)297 TEST_F(UtilsTest, GetPathFromString_NonCanonical) {
298 dir_rec_t test1;
299
300 EXPECT_EQ(0, get_path_from_string(&test1, "/mnt/asec"))
301 << "Should be able to canonicalize directory /mnt/asec";
302 EXPECT_STREQ("/mnt/asec/", test1.path)
303 << "/mnt/asec should be canonicalized to /mnt/asec/";
304 EXPECT_EQ(10, (ssize_t) test1.len)
305 << "path len should be equal to the length of /mnt/asec/ (10)";
306 free(test1.path);
307 }
308
TEST_F(UtilsTest,GetPathFromString_CanonicalPath)309 TEST_F(UtilsTest, GetPathFromString_CanonicalPath) {
310 dir_rec_t test3;
311 EXPECT_EQ(0, get_path_from_string(&test3, "/data/app/"))
312 << "Should be able to canonicalize directory /data/app/";
313 EXPECT_STREQ("/data/app/", test3.path)
314 << "/data/app/ should be canonicalized to /data/app/";
315 EXPECT_EQ(10, (ssize_t) test3.len)
316 << "path len should be equal to the length of /data/app/ (10)";
317 free(test3.path);
318 }
319
TEST_F(UtilsTest,CreatePkgPath_LongPkgNameSuccess)320 TEST_F(UtilsTest, CreatePkgPath_LongPkgNameSuccess) {
321 char path[PKG_PATH_MAX];
322
323 // Create long packagename of "aaaaa..."
324 size_t pkgnameSize = PKG_NAME_MAX;
325 char pkgname[pkgnameSize + 1];
326 memset(pkgname, 'a', pkgnameSize);
327 pkgname[1] = '.';
328 pkgname[pkgnameSize] = '\0';
329
330 EXPECT_EQ(0, create_pkg_path(path, pkgname, "", 0))
331 << "Should successfully be able to create package name.";
332
333 std::string prefix = std::string(TEST_DATA_DIR) + PRIMARY_USER_PREFIX;
334 size_t offset = prefix.length();
335
336 EXPECT_STREQ(pkgname, path + offset)
337 << "Package path should be a really long string of a's";
338 }
339
TEST_F(UtilsTest,CreatePkgPath_LongPostfixFail)340 TEST_F(UtilsTest, CreatePkgPath_LongPostfixFail) {
341 char path[PKG_PATH_MAX];
342
343 // Create long packagename of "aaaaa..."
344 size_t postfixSize = PKG_PATH_MAX;
345 char postfix[postfixSize + 1];
346 memset(postfix, 'a', postfixSize);
347 postfix[postfixSize] = '\0';
348
349 EXPECT_EQ(-1, create_pkg_path(path, "com.example.package", postfix, 0))
350 << "Should return error because postfix is too long.";
351 }
352
TEST_F(UtilsTest,CreatePkgPath_PrimaryUser)353 TEST_F(UtilsTest, CreatePkgPath_PrimaryUser) {
354 char path[PKG_PATH_MAX];
355
356 EXPECT_EQ(0, create_pkg_path(path, "com.example.package", "", 0))
357 << "Should return error because postfix is too long.";
358
359 std::string p = std::string(TEST_DATA_DIR)
360 + PRIMARY_USER_PREFIX
361 + "com.example.package";
362 EXPECT_STREQ(p.c_str(), path)
363 << "Package path should be in /data/data/";
364 }
365
TEST_F(UtilsTest,CreatePkgPath_SecondaryUser)366 TEST_F(UtilsTest, CreatePkgPath_SecondaryUser) {
367 char path[PKG_PATH_MAX];
368
369 EXPECT_EQ(0, create_pkg_path(path, "com.example.package", "", 1))
370 << "Should successfully create package path.";
371
372 std::string p = std::string(TEST_DATA_DIR)
373 + SECONDARY_USER_PREFIX
374 + "1/com.example.package";
375 EXPECT_STREQ(p.c_str(), path)
376 << "Package path should be in /data/user/";
377 }
378
TEST_F(UtilsTest,CreateMovePath_Primary)379 TEST_F(UtilsTest, CreateMovePath_Primary) {
380 char path[PKG_PATH_MAX];
381
382 EXPECT_EQ(0, create_move_path(path, "com.android.test", "shared_prefs", 0))
383 << "Should be able to create move path for primary user";
384
385 EXPECT_STREQ("/data/data/com.android.test/shared_prefs", path)
386 << "Primary user package directory should be created correctly";
387 }
388
389
TEST_F(UtilsTest,CreateMovePath_Fail_AppTooLong)390 TEST_F(UtilsTest, CreateMovePath_Fail_AppTooLong) {
391 char path[PKG_PATH_MAX];
392 std::string really_long_app_name = create_too_long_path("com.example");
393 EXPECT_EQ(-1, create_move_path(path, really_long_app_name.c_str(), "shared_prefs", 0))
394 << "Should fail to create move path for primary user";
395 }
396
TEST_F(UtilsTest,CreateMovePath_Fail_LeafTooLong)397 TEST_F(UtilsTest, CreateMovePath_Fail_LeafTooLong) {
398 char path[PKG_PATH_MAX];
399 std::string really_long_leaf_name = create_too_long_path("leaf_");
400 EXPECT_EQ(-1, create_move_path(path, "com.android.test", really_long_leaf_name.c_str(), 0))
401 << "Should fail to create move path for primary user";
402 }
403
TEST_F(UtilsTest,CopyAndAppend_Normal)404 TEST_F(UtilsTest, CopyAndAppend_Normal) {
405 //int copy_and_append(dir_rec_t* dst, dir_rec_t* src, char* suffix)
406 dir_rec_t dst;
407 dir_rec_t src;
408
409 src.path = (char*) "/data/";
410 src.len = strlen(src.path);
411
412 EXPECT_EQ(0, copy_and_append(&dst, &src, "app/"))
413 << "Should return error because postfix is too long.";
414
415 EXPECT_STREQ("/data/app/", dst.path)
416 << "Appended path should be correct";
417
418 EXPECT_EQ(10, (ssize_t) dst.len)
419 << "Appended path should be length of '/data/app/' (10)";
420 }
421
TEST_F(UtilsTest,AppendAndIncrement_Normal)422 TEST_F(UtilsTest, AppendAndIncrement_Normal) {
423 size_t dst_size = 10;
424 char dst[dst_size];
425 char *dstp = dst;
426 const char* src = "FOO";
427
428 EXPECT_EQ(0, append_and_increment(&dstp, src, &dst_size))
429 << "String should append successfully";
430
431 EXPECT_STREQ("FOO", dst)
432 << "String should append correctly";
433
434 EXPECT_EQ(0, append_and_increment(&dstp, src, &dst_size))
435 << "String should append successfully again";
436
437 EXPECT_STREQ("FOOFOO", dst)
438 << "String should append correctly again";
439 }
440
TEST_F(UtilsTest,AppendAndIncrement_TooBig)441 TEST_F(UtilsTest, AppendAndIncrement_TooBig) {
442 size_t dst_size = 5;
443 char dst[dst_size];
444 char *dstp = dst;
445 const char* src = "FOO";
446
447 EXPECT_EQ(0, append_and_increment(&dstp, src, &dst_size))
448 << "String should append successfully";
449
450 EXPECT_STREQ("FOO", dst)
451 << "String should append correctly";
452
453 EXPECT_EQ(-1, append_and_increment(&dstp, src, &dst_size))
454 << "String should fail because it's too large to fit";
455 }
456
TEST_F(UtilsTest,CreateDataPath)457 TEST_F(UtilsTest, CreateDataPath) {
458 EXPECT_EQ("/data", create_data_path(nullptr));
459 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b",
460 create_data_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b"));
461 }
462
TEST_F(UtilsTest,CreateDataAppPath)463 TEST_F(UtilsTest, CreateDataAppPath) {
464 EXPECT_EQ("/data/app", create_data_app_path(nullptr));
465
466 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/app",
467 create_data_app_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b"));
468 }
469
TEST_F(UtilsTest,CreateDataUserPath)470 TEST_F(UtilsTest, CreateDataUserPath) {
471 EXPECT_EQ("/data/data", create_data_user_ce_path(nullptr, 0));
472 EXPECT_EQ("/data/user/10", create_data_user_ce_path(nullptr, 10));
473
474 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/0",
475 create_data_user_ce_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0));
476 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/10",
477 create_data_user_ce_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 10));
478 }
479
TEST_F(UtilsTest,CreateDataMediaPath)480 TEST_F(UtilsTest, CreateDataMediaPath) {
481 EXPECT_EQ("/data/media/0", create_data_media_path(nullptr, 0));
482 EXPECT_EQ("/data/media/10", create_data_media_path(nullptr, 10));
483
484 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/media/0",
485 create_data_media_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0));
486 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/media/10",
487 create_data_media_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 10));
488 }
489
TEST_F(UtilsTest,CreateDataAppPackagePath)490 TEST_F(UtilsTest, CreateDataAppPackagePath) {
491 EXPECT_EQ("/data/app/com.example", create_data_app_package_path(nullptr, "com.example"));
492
493 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/app/com.example",
494 create_data_app_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", "com.example"));
495 }
496
TEST_F(UtilsTest,CreateDataUserPackagePath)497 TEST_F(UtilsTest, CreateDataUserPackagePath) {
498 EXPECT_EQ("/data/data/com.example", create_data_user_ce_package_path(nullptr, 0, "com.example"));
499 EXPECT_EQ("/data/user/10/com.example", create_data_user_ce_package_path(nullptr, 10, "com.example"));
500
501 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/0/com.example",
502 create_data_user_ce_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0, "com.example"));
503 EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/10/com.example",
504 create_data_user_ce_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 10, "com.example"));
505 }
506
TEST_F(UtilsTest,IsValidPackageName)507 TEST_F(UtilsTest, IsValidPackageName) {
508 EXPECT_EQ(true, is_valid_package_name("android"));
509 EXPECT_EQ(true, is_valid_package_name("com.example"));
510 EXPECT_EQ(true, is_valid_package_name("com.example-1"));
511 EXPECT_EQ(true, is_valid_package_name("com.example-1024"));
512 EXPECT_EQ(true, is_valid_package_name("com.example.foo---KiJFj4a_tePVw95pSrjg=="));
513 EXPECT_EQ(true, is_valid_package_name("really_LONG.a1234.package_name"));
514
515 EXPECT_EQ(false, is_valid_package_name("1234.package"));
516 EXPECT_EQ(false, is_valid_package_name("com.1234.package"));
517 EXPECT_EQ(false, is_valid_package_name(""));
518 EXPECT_EQ(false, is_valid_package_name("."));
519 EXPECT_EQ(false, is_valid_package_name(".."));
520 EXPECT_EQ(false, is_valid_package_name("../"));
521 EXPECT_EQ(false, is_valid_package_name("com.example/../com.evil/"));
522 EXPECT_EQ(false, is_valid_package_name("com.example-1/../com.evil/"));
523 EXPECT_EQ(false, is_valid_package_name("/com.evil"));
524 }
525
TEST_F(UtilsTest,CreateDataUserProfilePath)526 TEST_F(UtilsTest, CreateDataUserProfilePath) {
527 EXPECT_EQ("/data/misc/profiles/cur/0", create_primary_cur_profile_dir_path(0));
528 EXPECT_EQ("/data/misc/profiles/cur/1", create_primary_cur_profile_dir_path(1));
529 }
530
TEST_F(UtilsTest,CreateDataUserProfilePackagePath)531 TEST_F(UtilsTest, CreateDataUserProfilePackagePath) {
532 EXPECT_EQ("/data/misc/profiles/cur/0/com.example",
533 create_primary_current_profile_package_dir_path(0, "com.example"));
534 EXPECT_EQ("/data/misc/profiles/cur/1/com.example",
535 create_primary_current_profile_package_dir_path(1, "com.example"));
536 }
537
TEST_F(UtilsTest,CreateDataRefProfilePath)538 TEST_F(UtilsTest, CreateDataRefProfilePath) {
539 EXPECT_EQ("/data/misc/profiles/ref", create_primary_ref_profile_dir_path());
540 }
541
TEST_F(UtilsTest,CreateDataRefProfilePackagePath)542 TEST_F(UtilsTest, CreateDataRefProfilePackagePath) {
543 EXPECT_EQ("/data/misc/profiles/ref/com.example",
544 create_primary_reference_profile_package_dir_path("com.example"));
545 }
546
TEST_F(UtilsTest,CreatePrimaryCurrentProfile)547 TEST_F(UtilsTest, CreatePrimaryCurrentProfile) {
548 std::string expected =
549 create_primary_current_profile_package_dir_path(0, "com.example") + "/primary.prof";
550 EXPECT_EQ(expected,
551 create_current_profile_path(/*user*/0, "com.example", /*is_secondary*/false));
552 }
553
TEST_F(UtilsTest,CreatePrimaryReferenceProfile)554 TEST_F(UtilsTest, CreatePrimaryReferenceProfile) {
555 std::string expected =
556 create_primary_reference_profile_package_dir_path("com.example") + "/primary.prof";
557 EXPECT_EQ(expected,
558 create_reference_profile_path("com.example", /*is_secondary*/false));
559 }
560
TEST_F(UtilsTest,CreateSecondaryCurrentProfile)561 TEST_F(UtilsTest, CreateSecondaryCurrentProfile) {
562 EXPECT_EQ("/data/user/0/com.example/oat/secondary.dex.cur.prof",
563 create_current_profile_path(/*user*/0,
564 "/data/user/0/com.example/secondary.dex", /*is_secondary*/true));
565 }
566
TEST_F(UtilsTest,CreateSecondaryReferenceProfile)567 TEST_F(UtilsTest, CreateSecondaryReferenceProfile) {
568 EXPECT_EQ("/data/user/0/com.example/oat/secondary.dex.prof",
569 create_reference_profile_path(
570 "/data/user/0/com.example/secondary.dex", /*is_secondary*/true));
571 }
572
pass_secondary_dex_validation(const std::string & package_name,const std::string & dex_path,int uid,int storage_flag)573 static void pass_secondary_dex_validation(const std::string& package_name,
574 const std::string& dex_path, int uid, int storage_flag) {
575 EXPECT_TRUE(validate_secondary_dex_path(package_name, dex_path, /*volume_uuid*/ nullptr, uid,
576 storage_flag))
577 << dex_path << " should be allowed as a valid secondary dex path";
578 }
579
fail_secondary_dex_validation(const std::string & package_name,const std::string & dex_path,int uid,int storage_flag)580 static void fail_secondary_dex_validation(const std::string& package_name,
581 const std::string& dex_path, int uid, int storage_flag) {
582 EXPECT_FALSE(validate_secondary_dex_path(package_name, dex_path, /*volume_uuid*/ nullptr, uid,
583 storage_flag))
584 << dex_path << " should not be allowed as a valid secondary dex path";
585 }
586
TEST_F(UtilsTest,ValidateSecondaryDexFilesPath)587 TEST_F(UtilsTest, ValidateSecondaryDexFilesPath) {
588 std::string package_name = "com.test.app";
589 std::string app_dir_ce_user_0 = "/data/data/" + package_name;
590 std::string app_dir_ce_user_10 = "/data/user/10/" + package_name;
591
592 std::string app_dir_de_user_0 = "/data/user_de/0/" + package_name;
593 std::string app_dir_de_user_10 = "/data/user_de/10/" + package_name;
594
595 EXPECT_EQ(app_dir_ce_user_0,
596 create_data_user_ce_package_path(nullptr, 0, package_name.c_str()));
597 EXPECT_EQ(app_dir_ce_user_10,
598 create_data_user_ce_package_path(nullptr, 10, package_name.c_str()));
599
600 EXPECT_EQ(app_dir_de_user_0,
601 create_data_user_de_package_path(nullptr, 0, package_name.c_str()));
602 EXPECT_EQ(app_dir_de_user_10,
603 create_data_user_de_package_path(nullptr, 10, package_name.c_str()));
604
605 uid_t app_uid_for_user_0 = multiuser_get_uid(/*user_id*/0, /*app_id*/ 1234);
606 uid_t app_uid_for_user_10 = multiuser_get_uid(/*user_id*/10, /*app_id*/ 1234);
607
608 // Standard path for user 0 on CE storage.
609 pass_secondary_dex_validation(
610 package_name, app_dir_ce_user_0 + "/ce0.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
611 // Standard path for user 10 on CE storage.
612 pass_secondary_dex_validation(
613 package_name, app_dir_ce_user_10 + "/ce10.dex", app_uid_for_user_10, FLAG_STORAGE_CE);
614
615 // Standard path for user 0 on DE storage.
616 pass_secondary_dex_validation(
617 package_name, app_dir_de_user_0 + "/de0.dex", app_uid_for_user_0, FLAG_STORAGE_DE);
618 // Standard path for user 10 on DE storage.
619 pass_secondary_dex_validation(
620 package_name, app_dir_de_user_10 + "/de0.dex", app_uid_for_user_10, FLAG_STORAGE_DE);
621
622 // Dex path for user 0 accessed from user 10.
623 fail_secondary_dex_validation(
624 package_name, app_dir_ce_user_0 + "/path0_from10.dex",
625 app_uid_for_user_10, FLAG_STORAGE_CE);
626
627 // Dex path for CE storage accessed with DE.
628 fail_secondary_dex_validation(
629 package_name, app_dir_ce_user_0 + "/ce_from_de.dex", app_uid_for_user_0, FLAG_STORAGE_DE);
630
631 // Dex path for DE storage accessed with CE.
632 fail_secondary_dex_validation(
633 package_name, app_dir_de_user_0 + "/de_from_ce.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
634
635 // Location which does not start with '/'.
636 fail_secondary_dex_validation(
637 package_name, "without_slash.dex", app_uid_for_user_10, FLAG_STORAGE_DE);
638
639 // The dex file is not in the specified package directory.
640 fail_secondary_dex_validation(
641 "another.package", app_dir_ce_user_0 + "/for_another_package.dex",
642 app_uid_for_user_0, FLAG_STORAGE_DE);
643
644 // The dex path contains indirect directories.
645 fail_secondary_dex_validation(
646 package_name, app_dir_ce_user_0 + "/1/../foo.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
647 fail_secondary_dex_validation(
648 package_name, app_dir_ce_user_0 + "/1/./foo.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
649
650 // Super long path.
651 std::string too_long = create_too_long_path("too_long_");
652 fail_secondary_dex_validation(
653 package_name, app_dir_ce_user_10 + "/" + too_long, app_uid_for_user_10, FLAG_STORAGE_CE);
654 }
655
656 } // namespace installd
657 } // namespace android
658