1 /*
2  * Copyright (C) 2020 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 package com.android.cts.appdataisolation.appb;
18 
19 import static com.android.cts.appdataisolation.common.FileUtils.assertDirDoesNotExist;
20 import static com.android.cts.appdataisolation.common.FileUtils.assertDirIsAccessible;
21 import static com.android.cts.appdataisolation.common.UserUtils.getCurrentUserId;
22 
23 import android.content.Context;
24 import android.content.pm.ApplicationInfo;
25 import android.os.UserHandle;
26 
27 import androidx.test.filters.SmallTest;
28 import androidx.test.platform.app.InstrumentationRegistry;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 
33 @SmallTest
34 public class AppBTests {
35 
36     private static final String APPA_PKG = "com.android.cts.appdataisolation.appa";
37 
38     private Context mContext;
39 
40     @Before
setUp()41     public void setUp() throws Exception {
42         mContext = InstrumentationRegistry.getInstrumentation().getContext();
43     }
44 
45     @Test
testCanNotAccessAppADataDir()46     public void testCanNotAccessAppADataDir() {
47         ApplicationInfo applicationInfo = mContext.getApplicationInfo();
48         assertDirDoesNotExist(replacePackageBWithPackageA(applicationInfo.dataDir));
49         assertDirDoesNotExist(replacePackageBWithPackageA(applicationInfo.deviceProtectedDataDir));
50         assertDirDoesNotExist("/data/data/" + APPA_PKG);
51         assertDirDoesNotExist("/data/misc/profiles/cur/" + getCurrentUserId() + "/"
52                 + APPA_PKG);
53         assertDirDoesNotExist("/data/misc/profiles/ref/" + APPA_PKG);
54     }
55 
56     @Test
testCanAccessAppADataDir()57     public void testCanAccessAppADataDir() {
58         ApplicationInfo applicationInfo = mContext.getApplicationInfo();
59         assertDirIsAccessible(replacePackageBWithPackageA(applicationInfo.dataDir));
60         assertDirIsAccessible(replacePackageBWithPackageA(applicationInfo.deviceProtectedDataDir));
61         if (getCurrentUserId() == UserHandle.USER_SYSTEM) {
62             assertDirIsAccessible("/data/data/" + APPA_PKG);
63         }
64         assertDirIsAccessible("/data/misc/profiles/cur/" + getCurrentUserId() + "/" + APPA_PKG);
65         assertDirIsAccessible("/data/misc/profiles/ref/" + APPA_PKG);
66     }
67 
68     @Test
testCanNotAccessAppAExternalDirs()69     public void testCanNotAccessAppAExternalDirs() {
70         String appAExternalDir = replacePackageBWithPackageA(
71                 mContext.getExternalFilesDir("").getParentFile().getAbsolutePath());
72         String appAObbDir = replacePackageBWithPackageA(mContext.getObbDir().getAbsolutePath());
73         assertDirDoesNotExist(appAExternalDir);
74         assertDirDoesNotExist(appAObbDir);
75     }
76 
77     @Test
testCanAccessAppAExternalDirs()78     public void testCanAccessAppAExternalDirs() {
79         String appAExternalDir = replacePackageBWithPackageA(
80                 mContext.getExternalFilesDir("").getAbsolutePath());
81         String appAObbDir = replacePackageBWithPackageA(mContext.getObbDir().getAbsolutePath());
82         assertDirIsAccessible(appAExternalDir);
83         assertDirIsAccessible(appAObbDir);
84     }
85 
replacePackageBWithPackageA(String path)86     private String replacePackageBWithPackageA(String path) {
87         return path.replace(mContext.getPackageName(), APPA_PKG);
88     }
89 
90 }
91