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