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 <gtest/gtest.h>
22 
23 #include "InstalldNativeService.h"
24 #include "globals.h"
25 #include "utils.h"
26 
27 #undef LOG_TAG
28 #define LOG_TAG "utils_test"
29 
30 #define TEST_DATA_DIR "/data/"
31 #define TEST_ROOT_DIR "/system/"
32 #define TEST_APP_DIR "/data/app/"
33 #define TEST_APP_PRIVATE_DIR "/data/app-private/"
34 #define TEST_APP_EPHEMERAL_DIR "/data/app-ephemeral/"
35 #define TEST_ASEC_DIR "/mnt/asec/"
36 #define TEST_EXPAND_DIR "/mnt/expand/00000000-0000-0000-0000-000000000000/"
37 
38 #define TEST_SYSTEM_DIR1 "/system/app/"
39 #define TEST_SYSTEM_DIR2 "/vendor/app/"
40 
41 #define TEST_PROFILE_DIR "/data/misc/profiles"
42 
43 namespace android {
44 namespace installd {
45 
46 class UtilsTest : public testing::Test {
47 protected:
SetUp()48     virtual void SetUp() {
49         setenv("ANDROID_LOG_TAGS", "*:v", 1);
50         android::base::InitLogging(nullptr);
51 
52         init_globals_from_data_and_root(TEST_DATA_DIR, TEST_ROOT_DIR);
53     }
54 
TearDown()55     virtual void TearDown() {
56     }
57 
create_too_long_path(const std::string & seed)58     std::string create_too_long_path(const std::string& seed) {
59         std::string result = seed;
60         for (size_t i = seed.size(); i < PKG_PATH_MAX; i++) {
61             result += "a";
62         }
63         return result;
64     }
65 };
66 
TEST_F(UtilsTest,IsValidApkPath_BadPrefix)67 TEST_F(UtilsTest, IsValidApkPath_BadPrefix) {
68     // Bad prefixes directories
69     const char *badprefix1 = "/etc/passwd";
70     EXPECT_EQ(-1, validate_apk_path(badprefix1))
71             << badprefix1 << " should not be allowed as a valid path";
72 
73     const char *badprefix2 = "../.." TEST_APP_DIR "../../../blah";
74     EXPECT_EQ(-1, validate_apk_path(badprefix2))
75             << badprefix2 << " should not be allowed as a valid path";
76 
77     const char *badprefix3 = "init.rc";
78     EXPECT_EQ(-1, validate_apk_path(badprefix3))
79             << badprefix3 << " should not be allowed as a valid path";
80 
81     const char *badprefix4 = "/init.rc";
82     EXPECT_EQ(-1, validate_apk_path(badprefix4))
83             << badprefix4 << " should not be allowed as a valid path";
84 }
85 
TEST_F(UtilsTest,IsValidApkPath_Internal)86 TEST_F(UtilsTest, IsValidApkPath_Internal) {
87     // Internal directories
88     const char *internal1 = TEST_APP_DIR "example.apk";
89     EXPECT_EQ(0, validate_apk_path(internal1))
90             << internal1 << " should be allowed as a valid path";
91 
92     // b/16888084
93     const char *path2 = TEST_APP_DIR "example.com/example.apk";
94     EXPECT_EQ(0, validate_apk_path(path2))
95             << path2 << " should be allowed as a valid path";
96 
97     const char *badint1 = TEST_APP_DIR "../example.apk";
98     EXPECT_EQ(-1, validate_apk_path(badint1))
99             << badint1 << " should be rejected as a invalid path";
100 
101     const char *badint2 = TEST_APP_DIR "/../example.apk";
102     EXPECT_EQ(-1, validate_apk_path(badint2))
103             << badint2 << " should be rejected as a invalid path";
104 
105     // Only one subdir should be allowed.
106     const char *bad_path3 = TEST_APP_DIR "example.com/subdir/pkg.apk";
107     EXPECT_EQ(-1, validate_apk_path(bad_path3))
108             << bad_path3 << " should be rejected as a invalid path";
109 
110     const char *bad_path4 = TEST_APP_DIR "example.com/subdir/../pkg.apk";
111     EXPECT_EQ(-1, validate_apk_path(bad_path4))
112             << bad_path4 << " should be rejected as a invalid path";
113 
114     const char *bad_path5 = TEST_APP_DIR "example.com1/../example.com2/pkg.apk";
115     EXPECT_EQ(-1, validate_apk_path(bad_path5))
116             << bad_path5 << " should be rejected as a invalid path";
117 }
118 
TEST_F(UtilsTest,IsValidApkPath_TopDir)119 TEST_F(UtilsTest, IsValidApkPath_TopDir) {
120     EXPECT_EQ(0, validate_apk_path(TEST_DATA_DIR "app/com.example"));
121     EXPECT_EQ(0, validate_apk_path(TEST_EXPAND_DIR "app/com.example"));
122     EXPECT_EQ(-1, validate_apk_path(TEST_DATA_DIR "data/com.example"));
123     EXPECT_EQ(-1, validate_apk_path(TEST_EXPAND_DIR "data/com.example"));
124 }
125 
TEST_F(UtilsTest,IsValidApkPath_TopFile)126 TEST_F(UtilsTest, IsValidApkPath_TopFile) {
127     EXPECT_EQ(0, validate_apk_path(TEST_DATA_DIR "app/com.example/base.apk"));
128     EXPECT_EQ(0, validate_apk_path(TEST_EXPAND_DIR "app/com.example/base.apk"));
129     EXPECT_EQ(-1, validate_apk_path(TEST_DATA_DIR "data/com.example/base.apk"));
130     EXPECT_EQ(-1, validate_apk_path(TEST_EXPAND_DIR "data/com.example/base.apk"));
131 }
132 
TEST_F(UtilsTest,IsValidApkPath_OatDir)133 TEST_F(UtilsTest, IsValidApkPath_OatDir) {
134     EXPECT_EQ(0, validate_apk_path_subdirs(TEST_DATA_DIR "app/com.example/oat"));
135     EXPECT_EQ(0, validate_apk_path_subdirs(TEST_EXPAND_DIR "app/com.example/oat"));
136     EXPECT_EQ(-1, validate_apk_path_subdirs(TEST_DATA_DIR "data/com.example/oat"));
137     EXPECT_EQ(-1, validate_apk_path_subdirs(TEST_EXPAND_DIR "data/com.example/oat"));
138 }
139 
TEST_F(UtilsTest,IsValidApkPath_OatDirDir)140 TEST_F(UtilsTest, IsValidApkPath_OatDirDir) {
141     EXPECT_EQ(0, validate_apk_path_subdirs(TEST_DATA_DIR "app/com.example/oat/arm64"));
142     EXPECT_EQ(0, validate_apk_path_subdirs(TEST_EXPAND_DIR "app/com.example/oat/arm64"));
143     EXPECT_EQ(-1, validate_apk_path_subdirs(TEST_DATA_DIR "data/com.example/oat/arm64"));
144     EXPECT_EQ(-1, validate_apk_path_subdirs(TEST_EXPAND_DIR "data/com.example/oat/arm64"));
145 }
146 
TEST_F(UtilsTest,IsValidApkPath_OatDirDirFile)147 TEST_F(UtilsTest, IsValidApkPath_OatDirDirFile) {
148     EXPECT_EQ(0, validate_apk_path_subdirs(TEST_DATA_DIR "app/com.example/oat/arm64/base.odex"));
149     EXPECT_EQ(0, validate_apk_path_subdirs(TEST_EXPAND_DIR "app/com.example/oat/arm64/base.odex"));
150     EXPECT_EQ(-1, validate_apk_path_subdirs(TEST_DATA_DIR "data/com.example/oat/arm64/base.odex"));
151     EXPECT_EQ(-1, validate_apk_path_subdirs(TEST_EXPAND_DIR "data/com.example/oat/arm64/base.odex"));
152 }
153 
TEST_F(UtilsTest,IsValidApkPath_Private)154 TEST_F(UtilsTest, IsValidApkPath_Private) {
155     // Internal directories
156     const char *private1 = TEST_APP_PRIVATE_DIR "example.apk";
157     EXPECT_EQ(0, validate_apk_path(private1))
158             << private1 << " should be allowed as a valid path";
159 
160     // b/16888084
161     const char *path2 = TEST_APP_DIR "example.com/example.apk";
162     EXPECT_EQ(0, validate_apk_path(path2))
163             << path2 << " should be allowed as a valid path";
164 
165     const char *badpriv1 = TEST_APP_PRIVATE_DIR "../example.apk";
166     EXPECT_EQ(-1, validate_apk_path(badpriv1))
167             << badpriv1 << " should be rejected as a invalid path";
168 
169     const char *badpriv2 = TEST_APP_PRIVATE_DIR "/../example.apk";
170     EXPECT_EQ(-1, validate_apk_path(badpriv2))
171             << badpriv2 << " should be rejected as a invalid path";
172 
173     // Only one subdir should be allowed.
174     const char *bad_path3 = TEST_APP_PRIVATE_DIR "example.com/subdir/pkg.apk";
175     EXPECT_EQ(-1, validate_apk_path(bad_path3))
176             << bad_path3 << " should be rejected as a invalid path";
177 
178     const char *bad_path4 = TEST_APP_PRIVATE_DIR "example.com/subdir/../pkg.apk";
179     EXPECT_EQ(-1, validate_apk_path(bad_path4))
180             << bad_path4 << " should be rejected as a invalid path";
181 
182     const char *bad_path5 = TEST_APP_PRIVATE_DIR "example.com1/../example.com2/pkg.apk";
183     EXPECT_EQ(-1, validate_apk_path(bad_path5))
184             << bad_path5 << " should be rejected as a invalid path";
185 }
186 
187 
TEST_F(UtilsTest,IsValidApkPath_AsecGood1)188 TEST_F(UtilsTest, IsValidApkPath_AsecGood1) {
189     const char *asec1 = TEST_ASEC_DIR "example.apk";
190     EXPECT_EQ(0, validate_apk_path(asec1))
191             << asec1 << " should be allowed as a valid path";
192 }
193 
TEST_F(UtilsTest,IsValidApkPath_AsecGood2)194 TEST_F(UtilsTest, IsValidApkPath_AsecGood2) {
195     const char *asec2 = TEST_ASEC_DIR "com.example.asec/pkg.apk";
196     EXPECT_EQ(0, validate_apk_path(asec2))
197             << asec2 << " should be allowed as a valid path";
198 }
199 
TEST_F(UtilsTest,IsValidApkPath_EscapeFail)200 TEST_F(UtilsTest, IsValidApkPath_EscapeFail) {
201     const char *badasec1 = TEST_ASEC_DIR "../example.apk";
202     EXPECT_EQ(-1, validate_apk_path(badasec1))
203             << badasec1 << " should be rejected as a invalid path";
204 }
205 
TEST_F(UtilsTest,IsValidApkPath_SubdirEscapeFail)206 TEST_F(UtilsTest, IsValidApkPath_SubdirEscapeFail) {
207     const char *badasec3 = TEST_ASEC_DIR "com.example.asec/../../../pkg.apk";
208     EXPECT_EQ(-1, validate_apk_path(badasec3))
209             << badasec3  << " should be rejected as a invalid path";
210 }
211 
TEST_F(UtilsTest,IsValidApkPath_SlashEscapeFail)212 TEST_F(UtilsTest, IsValidApkPath_SlashEscapeFail) {
213     const char *badasec4 = TEST_ASEC_DIR "/../example.apk";
214     EXPECT_EQ(-1, validate_apk_path(badasec4))
215             << badasec4 << " should be rejected as a invalid path";
216 }
217 
TEST_F(UtilsTest,IsValidApkPath_CrazyDirFail)218 TEST_F(UtilsTest, IsValidApkPath_CrazyDirFail) {
219     const char *badasec5 = TEST_ASEC_DIR ".//../..";
220     EXPECT_EQ(-1, validate_apk_path(badasec5))
221             << badasec5 << " should be rejected as a invalid path";
222 }
223 
TEST_F(UtilsTest,IsValidApkPath_SubdirEscapeSingleFail)224 TEST_F(UtilsTest, IsValidApkPath_SubdirEscapeSingleFail) {
225     const char *badasec6 = TEST_ASEC_DIR "com.example.asec/../pkg.apk";
226     EXPECT_EQ(-1, validate_apk_path(badasec6))
227             << badasec6 << " should be rejected as a invalid path";
228 }
229 
TEST_F(UtilsTest,IsValidApkPath_TwoSubdirFail)230 TEST_F(UtilsTest, IsValidApkPath_TwoSubdirFail) {
231     const char *badasec7 = TEST_ASEC_DIR "com.example.asec/subdir1/pkg.apk";
232     EXPECT_EQ(-1, validate_apk_path(badasec7))
233             << badasec7 << " should be rejected as a invalid path";
234 }
235 
TEST_F(UtilsTest,CheckSystemApp_Dir1)236 TEST_F(UtilsTest, CheckSystemApp_Dir1) {
237     const char *sysapp1 = TEST_SYSTEM_DIR1 "Voice.apk";
238     EXPECT_EQ(0, validate_system_app_path(sysapp1))
239             << sysapp1 << " should be allowed as a system path";
240 }
241 
TEST_F(UtilsTest,CheckSystemApp_Dir2)242 TEST_F(UtilsTest, CheckSystemApp_Dir2) {
243     const char *sysapp2 = TEST_SYSTEM_DIR2 "com.example.myapp.apk";
244     EXPECT_EQ(0, validate_system_app_path(sysapp2))
245             << sysapp2 << " should be allowed as a system path";
246 }
247 
TEST_F(UtilsTest,CheckSystemApp_EscapeFail)248 TEST_F(UtilsTest, CheckSystemApp_EscapeFail) {
249     const char *badapp1 = TEST_SYSTEM_DIR1 "../com.example.apk";
250     EXPECT_EQ(-1, validate_system_app_path(badapp1))
251             << badapp1 << " should be rejected not a system path";
252 }
253 
TEST_F(UtilsTest,CheckSystemApp_DoubleEscapeFail)254 TEST_F(UtilsTest, CheckSystemApp_DoubleEscapeFail) {
255     const char *badapp2 = TEST_SYSTEM_DIR2 "/../../com.example.apk";
256     EXPECT_EQ(-1, validate_system_app_path(badapp2))
257             << badapp2 << " should be rejected not a system path";
258 }
259 
TEST_F(UtilsTest,CheckSystemApp_BadPathEscapeFail)260 TEST_F(UtilsTest, CheckSystemApp_BadPathEscapeFail) {
261     const char *badapp3 = TEST_APP_DIR "/../../com.example.apk";
262     EXPECT_EQ(-1, validate_system_app_path(badapp3))
263             << badapp3 << " should be rejected not a system path";
264 }
265 
TEST_F(UtilsTest,CheckSystemApp_Subdir)266 TEST_F(UtilsTest, CheckSystemApp_Subdir) {
267     const char *sysapp = TEST_SYSTEM_DIR1 "com.example/com.example.apk";
268     EXPECT_EQ(0, validate_system_app_path(sysapp))
269             << sysapp << " should be allowed as a system path";
270 
271     const char *badapp = TEST_SYSTEM_DIR1 "com.example/subdir/com.example.apk";
272     EXPECT_EQ(-1, validate_system_app_path(badapp))
273             << badapp << " should be rejected not a system path";
274 
275     const char *badapp1 = TEST_SYSTEM_DIR1 "com.example/subdir/../com.example.apk";
276     EXPECT_EQ(-1, validate_system_app_path(badapp1))
277             << badapp1 << " should be rejected not a system path";
278 
279     const char *badapp2 = TEST_SYSTEM_DIR1 "com.example1/../com.example2/com.example.apk";
280     EXPECT_EQ(-1, validate_system_app_path(badapp2))
281             << badapp2 << " should be rejected not a system path";
282 }
283 
TEST_F(UtilsTest,CreateDataPath)284 TEST_F(UtilsTest, CreateDataPath) {
285     EXPECT_EQ("/data", create_data_path(nullptr));
286     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b",
287             create_data_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b"));
288 }
289 
TEST_F(UtilsTest,CreateDataAppPath)290 TEST_F(UtilsTest, CreateDataAppPath) {
291     EXPECT_EQ("/data/app", create_data_app_path(nullptr));
292 
293     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/app",
294             create_data_app_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b"));
295 }
296 
TEST_F(UtilsTest,CreateDataUserPath)297 TEST_F(UtilsTest, CreateDataUserPath) {
298     EXPECT_EQ("/data/data", create_data_user_ce_path(nullptr, 0));
299     EXPECT_EQ("/data/user/10", create_data_user_ce_path(nullptr, 10));
300 
301     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/0",
302             create_data_user_ce_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0));
303     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/10",
304             create_data_user_ce_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 10));
305 }
306 
TEST_F(UtilsTest,CreateDataMediaPath)307 TEST_F(UtilsTest, CreateDataMediaPath) {
308     EXPECT_EQ("/data/media/0", create_data_media_path(nullptr, 0));
309     EXPECT_EQ("/data/media/10", create_data_media_path(nullptr, 10));
310 
311     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/media/0",
312             create_data_media_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0));
313     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/media/10",
314             create_data_media_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 10));
315 }
316 
TEST_F(UtilsTest,CreateDataAppPackagePath)317 TEST_F(UtilsTest, CreateDataAppPackagePath) {
318     EXPECT_EQ("/data/app/com.example", create_data_app_package_path(nullptr, "com.example"));
319 
320     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/app/com.example",
321             create_data_app_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", "com.example"));
322 }
323 
TEST_F(UtilsTest,CreateDataUserPackagePath)324 TEST_F(UtilsTest, CreateDataUserPackagePath) {
325     EXPECT_EQ("/data/data/com.example", create_data_user_ce_package_path(nullptr, 0, "com.example"));
326     EXPECT_EQ("/data/user/10/com.example", create_data_user_ce_package_path(nullptr, 10, "com.example"));
327 
328     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/0/com.example",
329             create_data_user_ce_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0, "com.example"));
330     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/10/com.example",
331             create_data_user_ce_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 10, "com.example"));
332 }
333 
TEST_F(UtilsTest,IsValidPackageName)334 TEST_F(UtilsTest, IsValidPackageName) {
335     EXPECT_EQ(true, is_valid_package_name("android"));
336     EXPECT_EQ(true, is_valid_package_name("com.example"));
337     EXPECT_EQ(true, is_valid_package_name("com.example-1"));
338     EXPECT_EQ(true, is_valid_package_name("com.example-1024"));
339     EXPECT_EQ(true, is_valid_package_name("com.example.foo---KiJFj4a_tePVw95pSrjg=="));
340     EXPECT_EQ(true, is_valid_package_name("really_LONG.a1234.package_name"));
341 
342     EXPECT_EQ(false, is_valid_package_name("1234.package"));
343     EXPECT_EQ(false, is_valid_package_name("com.1234.package"));
344     EXPECT_EQ(false, is_valid_package_name(""));
345     EXPECT_EQ(false, is_valid_package_name("."));
346     EXPECT_EQ(false, is_valid_package_name(".."));
347     EXPECT_EQ(false, is_valid_package_name("../"));
348     EXPECT_EQ(false, is_valid_package_name("com.example/../com.evil/"));
349     EXPECT_EQ(false, is_valid_package_name("com.example-1/../com.evil/"));
350     EXPECT_EQ(false, is_valid_package_name("/com.evil"));
351 }
352 
TEST_F(UtilsTest,CreateDataUserProfilePath)353 TEST_F(UtilsTest, CreateDataUserProfilePath) {
354     EXPECT_EQ("/data/misc/profiles/cur/0", create_primary_cur_profile_dir_path(0));
355     EXPECT_EQ("/data/misc/profiles/cur/1", create_primary_cur_profile_dir_path(1));
356 }
357 
TEST_F(UtilsTest,CreateDataUserProfilePackagePath)358 TEST_F(UtilsTest, CreateDataUserProfilePackagePath) {
359     EXPECT_EQ("/data/misc/profiles/cur/0/com.example",
360             create_primary_current_profile_package_dir_path(0, "com.example"));
361     EXPECT_EQ("/data/misc/profiles/cur/1/com.example",
362             create_primary_current_profile_package_dir_path(1, "com.example"));
363 }
364 
TEST_F(UtilsTest,CreateDataRefProfilePath)365 TEST_F(UtilsTest, CreateDataRefProfilePath) {
366     EXPECT_EQ("/data/misc/profiles/ref", create_primary_ref_profile_dir_path());
367 }
368 
TEST_F(UtilsTest,CreateDataRefProfilePackagePath)369 TEST_F(UtilsTest, CreateDataRefProfilePackagePath) {
370     EXPECT_EQ("/data/misc/profiles/ref/com.example",
371         create_primary_reference_profile_package_dir_path("com.example"));
372 }
373 
TEST_F(UtilsTest,CreatePrimaryCurrentProfile)374 TEST_F(UtilsTest, CreatePrimaryCurrentProfile) {
375     std::string expected_base =
376         create_primary_current_profile_package_dir_path(0, "com.example") + "/primary.prof";
377     EXPECT_EQ(expected_base,
378             create_current_profile_path(/*user*/0, "com.example", "primary.prof",
379                     /*is_secondary*/false));
380 
381     std::string expected_split =
382         create_primary_current_profile_package_dir_path(0, "com.example") + "/split.prof";
383     EXPECT_EQ(expected_split,
384             create_current_profile_path(/*user*/0, "com.example", "split.prof",
385                     /*is_secondary*/false));
386 }
387 
TEST_F(UtilsTest,CreatePrimaryReferenceProfile)388 TEST_F(UtilsTest, CreatePrimaryReferenceProfile) {
389     std::string expected_base =
390         create_primary_reference_profile_package_dir_path("com.example") + "/primary.prof";
391     EXPECT_EQ(expected_base,
392             create_reference_profile_path("com.example", "primary.prof", /*is_secondary*/false));
393 
394     std::string expected_split =
395         create_primary_reference_profile_package_dir_path("com.example") + "/split.prof";
396     EXPECT_EQ(expected_split,
397             create_reference_profile_path("com.example", "split.prof", /*is_secondary*/false));
398 }
399 
TEST_F(UtilsTest,CreateProfileSnapshot)400 TEST_F(UtilsTest, CreateProfileSnapshot) {
401     std::string expected_base =
402         create_primary_reference_profile_package_dir_path("com.example") + "/primary.prof.snapshot";
403     EXPECT_EQ(expected_base, create_snapshot_profile_path("com.example", "primary.prof"));
404 
405     std::string expected_split =
406         create_primary_reference_profile_package_dir_path("com.example") + "/split.prof.snapshot";
407     EXPECT_EQ(expected_split, create_snapshot_profile_path("com.example", "split.prof"));
408 }
409 
TEST_F(UtilsTest,CreateSecondaryCurrentProfile)410 TEST_F(UtilsTest, CreateSecondaryCurrentProfile) {
411     EXPECT_EQ("/data/user/0/com.example/oat/secondary.dex.cur.prof",
412             create_current_profile_path(/*user*/0, "com.example",
413                     "/data/user/0/com.example/secondary.dex", /*is_secondary*/true));
414 }
415 
TEST_F(UtilsTest,CreateSecondaryReferenceProfile)416 TEST_F(UtilsTest, CreateSecondaryReferenceProfile) {
417     EXPECT_EQ("/data/user/0/com.example/oat/secondary.dex.prof",
418             create_reference_profile_path("com.example",
419                     "/data/user/0/com.example/secondary.dex", /*is_secondary*/true));
420 }
421 
pass_secondary_dex_validation(const std::string & package_name,const std::string & dex_path,int uid,int storage_flag)422 static void pass_secondary_dex_validation(const std::string& package_name,
423         const std::string& dex_path, int uid, int storage_flag) {
424     EXPECT_TRUE(validate_secondary_dex_path(package_name, dex_path, /*volume_uuid*/ nullptr, uid,
425             storage_flag))
426             << dex_path << " should be allowed as a valid secondary dex path";
427 }
428 
fail_secondary_dex_validation(const std::string & package_name,const std::string & dex_path,int uid,int storage_flag)429 static void fail_secondary_dex_validation(const std::string& package_name,
430         const std::string& dex_path, int uid, int storage_flag) {
431     EXPECT_FALSE(validate_secondary_dex_path(package_name, dex_path, /*volume_uuid*/ nullptr, uid,
432             storage_flag))
433             << dex_path << " should not be allowed as a valid secondary dex path";
434 }
435 
TEST_F(UtilsTest,ValidateSecondaryDexFilesPath)436 TEST_F(UtilsTest, ValidateSecondaryDexFilesPath) {
437     std::string package_name = "com.test.app";
438     std::string app_dir_ce_user_0 = "/data/data/" + package_name;
439     std::string app_dir_ce_user_0_link = "/data/user/0/" + package_name;
440     std::string app_dir_ce_user_10 = "/data/user/10/" + package_name;
441 
442     std::string app_dir_de_user_0 = "/data/user_de/0/" + package_name;
443     std::string app_dir_de_user_10 = "/data/user_de/10/" + package_name;
444 
445     EXPECT_EQ(app_dir_ce_user_0,
446             create_data_user_ce_package_path(nullptr, 0, package_name.c_str()));
447     EXPECT_EQ(app_dir_ce_user_10,
448             create_data_user_ce_package_path(nullptr, 10, package_name.c_str()));
449 
450     EXPECT_EQ(app_dir_de_user_0,
451             create_data_user_de_package_path(nullptr, 0, package_name.c_str()));
452     EXPECT_EQ(app_dir_de_user_10,
453             create_data_user_de_package_path(nullptr, 10, package_name.c_str()));
454 
455     uid_t app_uid_for_user_0 = multiuser_get_uid(/*user_id*/0, /*app_id*/ 1234);
456     uid_t app_uid_for_user_10 = multiuser_get_uid(/*user_id*/10, /*app_id*/ 1234);
457 
458     // Standard path for user 0 on CE storage.
459     pass_secondary_dex_validation(
460         package_name, app_dir_ce_user_0 + "/ce0.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
461     pass_secondary_dex_validation(
462         package_name, app_dir_ce_user_0_link + "/ce0.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
463     // Standard path for user 10 on CE storage.
464     pass_secondary_dex_validation(
465         package_name, app_dir_ce_user_10 + "/ce10.dex", app_uid_for_user_10, FLAG_STORAGE_CE);
466 
467     // Standard path for user 0 on DE storage.
468     pass_secondary_dex_validation(
469         package_name, app_dir_de_user_0 + "/de0.dex", app_uid_for_user_0, FLAG_STORAGE_DE);
470     // Standard path for user 10 on DE storage.
471     pass_secondary_dex_validation(
472         package_name, app_dir_de_user_10 + "/de0.dex", app_uid_for_user_10, FLAG_STORAGE_DE);
473 
474     // Dex path for user 0 accessed from user 10.
475     fail_secondary_dex_validation(
476         package_name, app_dir_ce_user_0 + "/path0_from10.dex",
477         app_uid_for_user_10, FLAG_STORAGE_CE);
478 
479     // Dex path for CE storage accessed with DE.
480     fail_secondary_dex_validation(
481         package_name, app_dir_ce_user_0 + "/ce_from_de.dex", app_uid_for_user_0, FLAG_STORAGE_DE);
482 
483     // Dex path for DE storage accessed with CE.
484     fail_secondary_dex_validation(
485         package_name, app_dir_de_user_0 + "/de_from_ce.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
486 
487     // Location which does not start with '/'.
488     fail_secondary_dex_validation(
489         package_name, "without_slash.dex", app_uid_for_user_10, FLAG_STORAGE_DE);
490 
491     // The dex file is not in the specified package directory.
492     fail_secondary_dex_validation(
493         "another.package", app_dir_ce_user_0 + "/for_another_package.dex",
494         app_uid_for_user_0, FLAG_STORAGE_DE);
495 
496     // The dex path contains indirect directories.
497     fail_secondary_dex_validation(
498         package_name, app_dir_ce_user_0 + "/1/../foo.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
499     fail_secondary_dex_validation(
500         package_name, app_dir_ce_user_0 + "/1/./foo.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
501 
502     // Super long path.
503     std::string too_long = create_too_long_path("too_long_");
504     fail_secondary_dex_validation(
505         package_name, app_dir_ce_user_10 + "/" + too_long, app_uid_for_user_10, FLAG_STORAGE_CE);
506 }
507 
TEST_F(UtilsTest,ValidateApkPath)508 TEST_F(UtilsTest, ValidateApkPath) {
509     EXPECT_EQ(0, validate_apk_path("/data/app/com.example"));
510     EXPECT_EQ(0, validate_apk_path("/data/app/com.example/file"));
511     EXPECT_EQ(0, validate_apk_path("/data/app/com.example//file"));
512     EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/"));
513     EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/file"));
514     EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/dir/file"));
515     EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/dir//file"));
516     EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/dir/dir/file"));
517     EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/dir/dir//file"));
518 }
519 
TEST_F(UtilsTest,ValidateApkPathSubdirs)520 TEST_F(UtilsTest, ValidateApkPathSubdirs) {
521     EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example"));
522     EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/file"));
523     EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example//file"));
524     EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/"));
525     EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/file"));
526     EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir/file"));
527     EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir//file"));
528     EXPECT_NE(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir/dir/file"));
529     EXPECT_NE(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir/dir//file"));
530 }
531 
532 }  // namespace installd
533 }  // namespace android
534