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