1 /*
2  * Copyright (C) 2022 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 android.packagemanagerlocal.cts_root;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static com.google.common.truth.Truth.assertWithMessage;
21 
22 import androidx.test.runner.AndroidJUnit4;
23 
24 import com.android.server.LocalManagerRegistry;
25 import com.android.server.pm.PackageManagerLocal;
26 
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 
31 import java.io.File;
32 import java.nio.file.Files;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.List;
36 
37 @RunWith(AndroidJUnit4.class)
38 public final class PackageManagerLocalTest {
39     private static final String TAG = "PackageManagerLocalTest";
40 
41     private PackageManagerLocal mPackageManagerLocal;
42 
43     @Before
setup()44     public void setup() {
45         mPackageManagerLocal = LocalManagerRegistry.getManager(PackageManagerLocal.class);
46     }
47 
48     @Test
testPackageManagerLocal_ReconcileSdkData_DifferentStorageFlags()49     public void testPackageManagerLocal_ReconcileSdkData_DifferentStorageFlags() throws Exception {
50         final String volumeUuid = null;
51         final String packageName = "android.packagemanagerlocal.test";
52         final List<String> subDirNames = Arrays.asList("one", "two@random");
53         final int userId = 0;
54         final int appId = 10000;
55         final int previousAppId = -1;
56         final String seInfo = "default";
57 
58         // There are two flags: FLAG_STORAGE_CE and FLAG_STORAGE_DE. So total of 4 combination
59         // to test.
60         for (int currentFlag = 0; currentFlag < 4; currentFlag++) {
61             final String errorMsg = "Failed for flag: " + currentFlag;
62 
63             File cePackageDirFile = new File("/data/misc_ce/0/sdksandbox/" + packageName);
64             File dePackageDirFile = new File("/data/misc_de/0/sdksandbox/" + packageName);
65 
66             try {
67                 mPackageManagerLocal.reconcileSdkData(volumeUuid, packageName, subDirNames, userId,
68                         appId, previousAppId, seInfo, currentFlag);
69 
70                 // Verify that sdk data directories have been created in the desired location
71                 boolean hasCeFlag = (currentFlag & PackageManagerLocal.FLAG_STORAGE_CE) != 0;
72                 if (hasCeFlag) {
73                     assertWithMessage(errorMsg).that(cePackageDirFile.isDirectory()).isTrue();
74                     assertWithMessage(errorMsg).that(
75                             cePackageDirFile.list()).asList().containsExactly("one", "two@random");
76                 } else {
77                     assertWithMessage(errorMsg).that(cePackageDirFile.exists()).isFalse();
78                 }
79 
80                 boolean hasDeFlag = (currentFlag & PackageManagerLocal.FLAG_STORAGE_DE) != 0;
81                 if (hasDeFlag) {
82                     assertWithMessage(errorMsg).that(dePackageDirFile.isDirectory()).isTrue();
83                     assertWithMessage(errorMsg).that(
84                             dePackageDirFile.list()).asList().containsExactly("one", "two@random");
85                 } else {
86                     assertWithMessage(errorMsg).that(dePackageDirFile.exists()).isFalse();
87                 }
88             } finally {
89                 // Clean up the created directories
90                 final List<String> emptyDir = new ArrayList<String>();
91                 mPackageManagerLocal.reconcileSdkData(volumeUuid, packageName, emptyDir, userId,
92                         appId, previousAppId, seInfo, currentFlag);
93                 Files.deleteIfExists(cePackageDirFile.toPath());
94                 Files.deleteIfExists(dePackageDirFile.toPath());
95             }
96         }
97     }
98 
99     @Test
testPackageManagerLocal_ReconcileSdkData_Reconcile()100     public void testPackageManagerLocal_ReconcileSdkData_Reconcile() throws Exception {
101         final String volumeUuid = null;
102         final String packageName = "android.packagemanagerlocal.test";
103         final List<String> subDirNames = Arrays.asList("one", "two@random");
104         final int userId = 0;
105         final int appId = 10000;
106         final int previousAppId = -1;
107         final String seInfo = "default";
108         final int flag = PackageManagerLocal.FLAG_STORAGE_CE;
109 
110         File cePackageDirFile = new File("/data/misc_ce/0/sdksandbox/" + packageName);
111 
112         try {
113             mPackageManagerLocal.reconcileSdkData(volumeUuid, packageName, subDirNames, userId,
114                     appId, previousAppId, seInfo, flag);
115 
116             // Call reconcileSdkData again, with different subDirNames
117             final List<String> differentSubDirNames = Arrays.asList("three");
118             mPackageManagerLocal.reconcileSdkData(volumeUuid, packageName, differentSubDirNames,
119                     userId, appId, previousAppId, seInfo, flag);
120 
121             // Verify that sdk data directories have been created in the desired location
122             assertThat(cePackageDirFile.isDirectory()).isTrue();
123             assertThat(cePackageDirFile.list()).asList().containsExactly("three");
124         } finally {
125             // Clean up the created directories
126             final List<String> emptyDir = new ArrayList<String>();
127             mPackageManagerLocal.reconcileSdkData(volumeUuid, packageName, emptyDir, userId, appId,
128                     previousAppId, seInfo, flag);
129             Files.deleteIfExists(cePackageDirFile.toPath());
130         }
131     }
132 }
133