1 /*
2  * Copyright (C) 2023 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.app.sdksandbox.testutils;
18 
19 import android.util.Log;
20 
21 import com.android.server.sdksandbox.SdkSandboxStorageManager;
22 
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.util.ArrayList;
27 import java.util.List;
28 
29 public class SdkSandboxStorageManagerUtility {
30 
31     private final SdkSandboxStorageManager mSdkSandboxStorageManager;
32     private static final String TAG = "SdkSandboxStorageManagerUtility";
33 
SdkSandboxStorageManagerUtility(SdkSandboxStorageManager sdkSandboxStorageManager)34     public SdkSandboxStorageManagerUtility(SdkSandboxStorageManager sdkSandboxStorageManager) {
35         mSdkSandboxStorageManager = sdkSandboxStorageManager;
36     }
37 
createSdkStorageForTest( int userId, String clientPackageName, List<String> sdkNames, List<String> nonSdkDirectories)38     public void createSdkStorageForTest(
39             int userId,
40             String clientPackageName,
41             List<String> sdkNames,
42             List<String> nonSdkDirectories) {
43         try {
44             createSdkStorageForTest(
45                     /*volumeUuid=*/ null, userId, clientPackageName, sdkNames, nonSdkDirectories);
46         } catch (Exception e) {
47             Log.d(TAG, "Error while creating files: " + e.toString());
48         }
49     }
50 
51     /**
52      * A helper method for create sdk storage for test purpose.
53      *
54      * <p>It creates <volume>/misc_ce/<userId>/sdksandbox/<packageName>/<name>[@,#]<name>
55      *
56      * <p>We are reusing the name of directory as random suffix for simplicity.
57      */
createSdkStorageForTest( String volumeUuid, int userId, String packageName, List<String> sdkNames, List<String> nonSdkDirectories)58     public void createSdkStorageForTest(
59             String volumeUuid,
60             int userId,
61             String packageName,
62             List<String> sdkNames,
63             List<String> nonSdkDirectories)
64             throws Exception {
65         final List<SdkSandboxStorageManager.StorageDirInfo> sdkStorageInfos =
66                 getSdkStorageDirInfoForTest(volumeUuid, userId, packageName, sdkNames);
67         final List<SdkSandboxStorageManager.StorageDirInfo> internalStorageDirInfos =
68                 getInternalStorageDirInfoForTest(
69                         volumeUuid, userId, packageName, nonSdkDirectories);
70 
71         createPackagePath(volumeUuid, userId, packageName, /*isCeData=*/ true);
72         createPackagePath(volumeUuid, userId, packageName, /*isCeData=*/ false);
73 
74         createFilesFromList(sdkStorageInfos);
75         createFilesFromList(internalStorageDirInfos);
76     }
77 
78     /** A helper method to get the internal storage paths for test purpose */
getInternalStorageDirInfoForTest( String volumeUuid, int userId, String packageName, List<String> nonSdkDirectories)79     public List<SdkSandboxStorageManager.StorageDirInfo> getInternalStorageDirInfoForTest(
80             String volumeUuid, int userId, String packageName, List<String> nonSdkDirectories) {
81         final List<SdkSandboxStorageManager.StorageDirInfo> internalStorageDirInfo =
82                 new ArrayList<>();
83         final String cePackageDir =
84                 mSdkSandboxStorageManager.getSdkDataPackageDirectory(
85                         volumeUuid, userId, packageName, /*isCeData=*/ true);
86         final String dePackageDir =
87                 mSdkSandboxStorageManager.getSdkDataPackageDirectory(
88                         volumeUuid, userId, packageName, /*isCeData=*/ false);
89 
90         for (String dir : nonSdkDirectories) {
91             String path =
92                     dir.equals(SdkSandboxStorageManager.SubDirectories.SHARED_DIR)
93                             ? dir
94                             : dir + '#' + dir;
95             String sdkCeSubDirPath = cePackageDir + "/" + path;
96             String sdkDeSubDirPath = dePackageDir + "/" + path;
97             internalStorageDirInfo.add(
98                     new SdkSandboxStorageManager.StorageDirInfo(sdkCeSubDirPath, sdkDeSubDirPath));
99         }
100         return internalStorageDirInfo;
101     }
102 
103     /** A helper method to get the storage paths of SDKs for test purpose */
getSdkStorageDirInfoForTest( String volumeUuid, int userId, String packageName, List<String> sdkNames)104     public List<SdkSandboxStorageManager.StorageDirInfo> getSdkStorageDirInfoForTest(
105             String volumeUuid, int userId, String packageName, List<String> sdkNames) {
106 
107         final List<SdkSandboxStorageManager.StorageDirInfo> sdkStorageDirInfo = new ArrayList<>();
108         final String cePackageDir =
109                 mSdkSandboxStorageManager.getSdkDataPackageDirectory(
110                         volumeUuid, userId, packageName, /*isCeData=*/ true);
111         final String dePackageDir =
112                 mSdkSandboxStorageManager.getSdkDataPackageDirectory(
113                         volumeUuid, userId, packageName, /*isCeData=*/ false);
114 
115         for (String sdkName : sdkNames) {
116             String sdkCeSubDirPath = cePackageDir + "/" + sdkName + "@" + sdkName;
117             String sdkDeSubDirPath = dePackageDir + "/" + sdkName + "@" + sdkName;
118             sdkStorageDirInfo.add(
119                     new SdkSandboxStorageManager.StorageDirInfo(sdkCeSubDirPath, sdkDeSubDirPath));
120         }
121         return sdkStorageDirInfo;
122     }
123 
createFilesFromList(List<SdkSandboxStorageManager.StorageDirInfo> storageDirInfos)124     private void createFilesFromList(List<SdkSandboxStorageManager.StorageDirInfo> storageDirInfos)
125             throws Exception {
126         final int storageDirInfosSize = storageDirInfos.size();
127 
128         for (int i = 0; i < storageDirInfosSize; i++) {
129             final Path ceSdkStoragePath = Paths.get(storageDirInfos.get(i).getCeDataDir());
130             Files.createDirectories(ceSdkStoragePath);
131 
132             final Path deSdkStoragePath = Paths.get(storageDirInfos.get(i).getDeDataDir());
133             Files.createDirectories(deSdkStoragePath);
134         }
135     }
136 
createPackagePath( String volumeUuid, int userId, String packageName, boolean isCeData)137     private void createPackagePath(
138             String volumeUuid, int userId, String packageName, boolean isCeData) throws Exception {
139         final String packageDir =
140                 mSdkSandboxStorageManager.getSdkDataPackageDirectory(
141                         volumeUuid, userId, packageName, isCeData);
142         Files.createDirectories(Paths.get(packageDir));
143     }
144 }
145