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,CreateDataAppPackagePath)334 TEST_F(UtilsTest, CreateDataAppPackagePath) {
335     EXPECT_EQ("/data/app/com.example", create_data_app_package_path(nullptr, "com.example"));
336 
337     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/app/com.example",
338             create_data_app_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", "com.example"));
339 }
340 
TEST_F(UtilsTest,CreateDataUserPackagePath)341 TEST_F(UtilsTest, CreateDataUserPackagePath) {
342     EXPECT_EQ("/data/data/com.example", create_data_user_ce_package_path(nullptr, 0, "com.example"));
343     EXPECT_EQ("/data/user/10/com.example", create_data_user_ce_package_path(nullptr, 10, "com.example"));
344 
345     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/0/com.example",
346             create_data_user_ce_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0, "com.example"));
347     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/user/10/com.example",
348             create_data_user_ce_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 10, "com.example"));
349 }
350 
TEST_F(UtilsTest,IsValidPackageName)351 TEST_F(UtilsTest, IsValidPackageName) {
352     EXPECT_EQ(true, is_valid_package_name("android"));
353     EXPECT_EQ(true, is_valid_package_name("com.example"));
354     EXPECT_EQ(true, is_valid_package_name("com.example-1"));
355     EXPECT_EQ(true, is_valid_package_name("com.example-1024"));
356     EXPECT_EQ(true, is_valid_package_name("com.example.foo---KiJFj4a_tePVw95pSrjg=="));
357     EXPECT_EQ(true, is_valid_package_name("really_LONG.a1234.package_name"));
358 
359     EXPECT_EQ(false, is_valid_package_name("1234.package"));
360     EXPECT_EQ(false, is_valid_package_name("com.1234.package"));
361     EXPECT_EQ(false, is_valid_package_name(""));
362     EXPECT_EQ(false, is_valid_package_name("."));
363     EXPECT_EQ(false, is_valid_package_name(".."));
364     EXPECT_EQ(false, is_valid_package_name("../"));
365     EXPECT_EQ(false, is_valid_package_name("com.example/../com.evil/"));
366     EXPECT_EQ(false, is_valid_package_name("com.example-1/../com.evil/"));
367     EXPECT_EQ(false, is_valid_package_name("/com.evil"));
368 }
369 
TEST_F(UtilsTest,CreateDataUserProfilePath)370 TEST_F(UtilsTest, CreateDataUserProfilePath) {
371     EXPECT_EQ("/data/misc/profiles/cur/0", create_primary_cur_profile_dir_path(0));
372     EXPECT_EQ("/data/misc/profiles/cur/1", create_primary_cur_profile_dir_path(1));
373 }
374 
TEST_F(UtilsTest,CreateDataUserProfilePackagePath)375 TEST_F(UtilsTest, CreateDataUserProfilePackagePath) {
376     EXPECT_EQ("/data/misc/profiles/cur/0/com.example",
377             create_primary_current_profile_package_dir_path(0, "com.example"));
378     EXPECT_EQ("/data/misc/profiles/cur/1/com.example",
379             create_primary_current_profile_package_dir_path(1, "com.example"));
380 }
381 
TEST_F(UtilsTest,CreateDataRefProfilePath)382 TEST_F(UtilsTest, CreateDataRefProfilePath) {
383     EXPECT_EQ("/data/misc/profiles/ref", create_primary_ref_profile_dir_path());
384 }
385 
TEST_F(UtilsTest,CreateDataRefProfilePackagePath)386 TEST_F(UtilsTest, CreateDataRefProfilePackagePath) {
387     EXPECT_EQ("/data/misc/profiles/ref/com.example",
388         create_primary_reference_profile_package_dir_path("com.example"));
389 }
390 
TEST_F(UtilsTest,CreatePrimaryCurrentProfile)391 TEST_F(UtilsTest, CreatePrimaryCurrentProfile) {
392     std::string expected_base =
393         create_primary_current_profile_package_dir_path(0, "com.example") + "/primary.prof";
394     EXPECT_EQ(expected_base,
395             create_current_profile_path(/*user*/0, "com.example", "primary.prof",
396                     /*is_secondary*/false));
397 
398     std::string expected_split =
399         create_primary_current_profile_package_dir_path(0, "com.example") + "/split.prof";
400     EXPECT_EQ(expected_split,
401             create_current_profile_path(/*user*/0, "com.example", "split.prof",
402                     /*is_secondary*/false));
403 }
404 
TEST_F(UtilsTest,CreatePrimaryReferenceProfile)405 TEST_F(UtilsTest, CreatePrimaryReferenceProfile) {
406     std::string expected_base =
407         create_primary_reference_profile_package_dir_path("com.example") + "/primary.prof";
408     EXPECT_EQ(expected_base,
409             create_reference_profile_path("com.example", "primary.prof", /*is_secondary*/false));
410 
411     std::string expected_split =
412         create_primary_reference_profile_package_dir_path("com.example") + "/split.prof";
413     EXPECT_EQ(expected_split,
414             create_reference_profile_path("com.example", "split.prof", /*is_secondary*/false));
415 }
416 
TEST_F(UtilsTest,CreateProfileSnapshot)417 TEST_F(UtilsTest, CreateProfileSnapshot) {
418     std::string expected_base =
419         create_primary_reference_profile_package_dir_path("com.example") + "/primary.prof.snapshot";
420     EXPECT_EQ(expected_base, create_snapshot_profile_path("com.example", "primary.prof"));
421 
422     std::string expected_split =
423         create_primary_reference_profile_package_dir_path("com.example") + "/split.prof.snapshot";
424     EXPECT_EQ(expected_split, create_snapshot_profile_path("com.example", "split.prof"));
425 }
426 
TEST_F(UtilsTest,CreateSecondaryCurrentProfile)427 TEST_F(UtilsTest, CreateSecondaryCurrentProfile) {
428     EXPECT_EQ("/data/user/0/com.example/oat/secondary.dex.cur.prof",
429             create_current_profile_path(/*user*/0, "com.example",
430                     "/data/user/0/com.example/secondary.dex", /*is_secondary*/true));
431 }
432 
TEST_F(UtilsTest,CreateSecondaryReferenceProfile)433 TEST_F(UtilsTest, CreateSecondaryReferenceProfile) {
434     EXPECT_EQ("/data/user/0/com.example/oat/secondary.dex.prof",
435             create_reference_profile_path("com.example",
436                     "/data/user/0/com.example/secondary.dex", /*is_secondary*/true));
437 }
438 
pass_secondary_dex_validation(const std::string & package_name,const std::string & dex_path,int uid,int storage_flag)439 static void pass_secondary_dex_validation(const std::string& package_name,
440         const std::string& dex_path, int uid, int storage_flag) {
441     EXPECT_TRUE(validate_secondary_dex_path(package_name, dex_path, /*volume_uuid*/ nullptr, uid,
442             storage_flag))
443             << dex_path << " should be allowed as a valid secondary dex path";
444 }
445 
fail_secondary_dex_validation(const std::string & package_name,const std::string & dex_path,int uid,int storage_flag)446 static void fail_secondary_dex_validation(const std::string& package_name,
447         const std::string& dex_path, int uid, int storage_flag) {
448     EXPECT_FALSE(validate_secondary_dex_path(package_name, dex_path, /*volume_uuid*/ nullptr, uid,
449             storage_flag))
450             << dex_path << " should not be allowed as a valid secondary dex path";
451 }
452 
TEST_F(UtilsTest,ValidateSecondaryDexFilesPath)453 TEST_F(UtilsTest, ValidateSecondaryDexFilesPath) {
454     std::string package_name = "com.test.app";
455     std::string app_dir_ce_user_0 = "/data/data/" + package_name;
456     std::string app_dir_ce_user_0_link = "/data/user/0/" + package_name;
457     std::string app_dir_ce_user_10 = "/data/user/10/" + package_name;
458 
459     std::string app_dir_de_user_0 = "/data/user_de/0/" + package_name;
460     std::string app_dir_de_user_10 = "/data/user_de/10/" + package_name;
461 
462     EXPECT_EQ(app_dir_ce_user_0,
463             create_data_user_ce_package_path(nullptr, 0, package_name.c_str()));
464     EXPECT_EQ(app_dir_ce_user_10,
465             create_data_user_ce_package_path(nullptr, 10, package_name.c_str()));
466 
467     EXPECT_EQ(app_dir_de_user_0,
468             create_data_user_de_package_path(nullptr, 0, package_name.c_str()));
469     EXPECT_EQ(app_dir_de_user_10,
470             create_data_user_de_package_path(nullptr, 10, package_name.c_str()));
471 
472     uid_t app_uid_for_user_0 = multiuser_get_uid(/*user_id*/0, /*app_id*/ 1234);
473     uid_t app_uid_for_user_10 = multiuser_get_uid(/*user_id*/10, /*app_id*/ 1234);
474 
475     // Standard path for user 0 on CE storage.
476     pass_secondary_dex_validation(
477         package_name, app_dir_ce_user_0 + "/ce0.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
478     pass_secondary_dex_validation(
479         package_name, app_dir_ce_user_0_link + "/ce0.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
480     // Standard path for user 10 on CE storage.
481     pass_secondary_dex_validation(
482         package_name, app_dir_ce_user_10 + "/ce10.dex", app_uid_for_user_10, FLAG_STORAGE_CE);
483 
484     // Standard path for user 0 on DE storage.
485     pass_secondary_dex_validation(
486         package_name, app_dir_de_user_0 + "/de0.dex", app_uid_for_user_0, FLAG_STORAGE_DE);
487     // Standard path for user 10 on DE storage.
488     pass_secondary_dex_validation(
489         package_name, app_dir_de_user_10 + "/de0.dex", app_uid_for_user_10, FLAG_STORAGE_DE);
490 
491     // Dex path for user 0 accessed from user 10.
492     fail_secondary_dex_validation(
493         package_name, app_dir_ce_user_0 + "/path0_from10.dex",
494         app_uid_for_user_10, FLAG_STORAGE_CE);
495 
496     // Dex path for CE storage accessed with DE.
497     fail_secondary_dex_validation(
498         package_name, app_dir_ce_user_0 + "/ce_from_de.dex", app_uid_for_user_0, FLAG_STORAGE_DE);
499 
500     // Dex path for DE storage accessed with CE.
501     fail_secondary_dex_validation(
502         package_name, app_dir_de_user_0 + "/de_from_ce.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
503 
504     // Location which does not start with '/'.
505     fail_secondary_dex_validation(
506         package_name, "without_slash.dex", app_uid_for_user_10, FLAG_STORAGE_DE);
507 
508     // The dex file is not in the specified package directory.
509     fail_secondary_dex_validation(
510         "another.package", app_dir_ce_user_0 + "/for_another_package.dex",
511         app_uid_for_user_0, FLAG_STORAGE_DE);
512 
513     // The dex path contains indirect directories.
514     fail_secondary_dex_validation(
515         package_name, app_dir_ce_user_0 + "/1/../foo.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
516     fail_secondary_dex_validation(
517         package_name, app_dir_ce_user_0 + "/1/./foo.dex", app_uid_for_user_0, FLAG_STORAGE_CE);
518 
519     // Super long path.
520     std::string too_long = create_too_long_path("too_long_");
521     fail_secondary_dex_validation(
522         package_name, app_dir_ce_user_10 + "/" + too_long, app_uid_for_user_10, FLAG_STORAGE_CE);
523 }
524 
TEST_F(UtilsTest,ValidateApkPath)525 TEST_F(UtilsTest, ValidateApkPath) {
526     EXPECT_EQ(0, validate_apk_path("/data/app/com.example"));
527     EXPECT_EQ(0, validate_apk_path("/data/app/com.example/file"));
528     EXPECT_EQ(0, validate_apk_path("/data/app/com.example//file"));
529     EXPECT_EQ(0, validate_apk_path("/data/app/random/com.example/"));
530     EXPECT_EQ(0, validate_apk_path("/data/app/random/com.example/file"));
531     EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/dir/file"));
532     EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/dir//file"));
533     EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/dir/dir/file"));
534     EXPECT_NE(0, validate_apk_path("/data/app/com.example/dir/dir/dir//file"));
535 }
536 
TEST_F(UtilsTest,ValidateApkPathSubdirs)537 TEST_F(UtilsTest, ValidateApkPathSubdirs) {
538     EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example"));
539     EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/file"));
540     EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example//file"));
541     EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/"));
542     EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/file"));
543     EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir/file"));
544     EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir//file"));
545     EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir/dir/file"));
546     EXPECT_EQ(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir/dir//file"));
547     EXPECT_NE(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir/dir/dir/file"));
548     EXPECT_NE(0, validate_apk_path_subdirs("/data/app/com.example/dir/dir/dir/dir//file"));
549 }
550 
TEST_F(UtilsTest,MatchExtension_Valid)551 TEST_F(UtilsTest, MatchExtension_Valid) {
552     EXPECT_EQ(AID_MEDIA_VIDEO, MatchExtension("mpg"));
553     EXPECT_EQ(AID_MEDIA_VIDEO, MatchExtension("mpeg"));
554     EXPECT_EQ(AID_MEDIA_VIDEO, MatchExtension("mPeG"));
555     EXPECT_EQ(AID_MEDIA_VIDEO, MatchExtension("MPEG"));
556 }
557 
TEST_F(UtilsTest,MatchExtension_Invalid)558 TEST_F(UtilsTest, MatchExtension_Invalid) {
559     EXPECT_EQ(0, MatchExtension("log"));
560     EXPECT_EQ(0, MatchExtension("3amp"));
561     EXPECT_EQ(0, MatchExtension("fpe"));
562     EXPECT_EQ(0, MatchExtension("docx"));
563 }
564 
TEST_F(UtilsTest,TestRollbackPaths)565 TEST_F(UtilsTest, TestRollbackPaths) {
566     EXPECT_EQ("/data/misc_ce/0/rollback/239/com.foo",
567             create_data_misc_ce_rollback_package_path(nullptr, 0, 239, "com.foo"));
568     EXPECT_EQ("/data/misc_ce/10/rollback/37/com.foo",
569             create_data_misc_ce_rollback_package_path(nullptr, 10, 37, "com.foo"));
570 
571     EXPECT_EQ("/data/misc_de/0/rollback/73/com.foo",
572             create_data_misc_de_rollback_package_path(nullptr, 0, 73, "com.foo"));
573     EXPECT_EQ("/data/misc_de/10/rollback/13/com.foo",
574             create_data_misc_de_rollback_package_path(nullptr, 10, 13, "com.foo"));
575 
576     EXPECT_EQ("/data/misc_ce/0/rollback/57",
577             create_data_misc_ce_rollback_path(nullptr, 0, 57));
578     EXPECT_EQ("/data/misc_ce/10/rollback/1543",
579             create_data_misc_ce_rollback_path(nullptr, 10, 1543));
580 
581     EXPECT_EQ("/data/misc_de/0/rollback/43",
582             create_data_misc_de_rollback_path(nullptr, 0, 43));
583     EXPECT_EQ("/data/misc_de/10/rollback/41",
584             create_data_misc_de_rollback_path(nullptr, 10, 41));
585 
586     EXPECT_EQ("/data/misc_ce/0/rollback/17/com.foo",
587             create_data_misc_ce_rollback_package_path(nullptr, 0, 17, "com.foo", 0));
588     EXPECT_EQ("/data/misc_ce/0/rollback/19/com.foo",
589             create_data_misc_ce_rollback_package_path(nullptr, 0, 19, "com.foo", 239));
590 
591     auto rollback_ce_path = create_data_misc_ce_rollback_path(nullptr, 0, 53);
592     auto rollback_ce_package_path = create_data_misc_ce_rollback_package_path(nullptr, 0, 53,
593             "com.foo");
594     auto deleter = [&rollback_ce_path]() {
595         delete_dir_contents_and_dir(rollback_ce_path, true /* ignore_if_missing */);
596     };
597     auto scope_guard = android::base::make_scope_guard(deleter);
598 
599     EXPECT_NE(-1, mkdir(rollback_ce_path.c_str(), 700));
600     EXPECT_NE(-1, mkdir(rollback_ce_package_path.c_str(), 700));
601 
602     ino_t ce_data_inode;
603     EXPECT_EQ(0, get_path_inode(rollback_ce_package_path, &ce_data_inode));
604 
605     EXPECT_EQ("/data/misc_ce/0/rollback/53/com.foo",
606             create_data_misc_ce_rollback_package_path(nullptr, 0, 53, "com.foo", ce_data_inode));
607     // Check that path defined by inode is picked even if it's not the same as
608     // the fallback one.
609     EXPECT_EQ("/data/misc_ce/0/rollback/53/com.foo",
610             create_data_misc_ce_rollback_package_path(nullptr, 0, 53, "com.bar", ce_data_inode));
611 
612     // These last couple of cases are never exercised in production because we
613     // only snapshot apps in the primary data partition. Exercise them here for
614     // the sake of completeness.
615     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/misc_ce/0/rollback/7/com.example",
616             create_data_misc_ce_rollback_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0, 7,
617                     "com.example"));
618     EXPECT_EQ("/mnt/expand/57f8f4bc-abf4-655f-bf67-946fc0f9f25b/misc_de/0/rollback/11/com.example",
619             create_data_misc_de_rollback_package_path("57f8f4bc-abf4-655f-bf67-946fc0f9f25b", 0, 11,
620                     "com.example"));
621 }
622 
TEST_F(UtilsTest,TestCreateDirIfNeeded)623 TEST_F(UtilsTest, TestCreateDirIfNeeded) {
624     system("mkdir -p /data/local/tmp/user/0");
625 
626     auto deleter = [&]() {
627         delete_dir_contents_and_dir("/data/local/tmp/user/0", true /* ignore_if_missing */);
628     };
629     auto scope_guard = android::base::make_scope_guard(deleter);
630 
631     // Create folder and check it's permissions.
632     ASSERT_EQ(0, create_dir_if_needed("/data/local/tmp/user/0/foo", 0700));
633     struct stat st;
634     ASSERT_EQ(0, stat("/data/local/tmp/user/0/foo", &st));
635     ASSERT_EQ(0700, st.st_mode & ALLPERMS);
636 
637     // Check that create_dir_if_needed is no-op if folder already exists with
638     // correct permissions.
639     ASSERT_EQ(0, create_dir_if_needed("/data/local/tmp/user/0/foo", 0700));
640 
641     // Check -1 is returned if folder exists but with different permissions.
642     ASSERT_EQ(-1, create_dir_if_needed("/data/local/tmp/user/0/foo", 0750));
643 
644     // Check that call fails if parent doesn't exist.
645     ASSERT_NE(0, create_dir_if_needed("/data/local/tmp/user/0/bar/baz", 0700));
646 }
647 
648 }  // namespace installd
649 }  // namespace android
650