• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.tests.codeprovider.storagetest;
18 
19 import android.app.sdksandbox.SandboxedSdkContext;
20 import android.app.sdksandbox.SandboxedSdkProvider;
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.util.Log;
24 import android.view.View;
25 
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.concurrent.Executor;
31 
32 public class StorageTestSandboxedSdkProvider extends SandboxedSdkProvider {
33     private static final String TAG = "StorageTestSandboxedSdkProvider";
34     private static final String BUNDLE_KEY_PHASE_NAME = "phase-name";
35 
36     @Override
initSdk(SandboxedSdkContext context, Bundle params, Executor executor, InitSdkCallback callback)37     public void initSdk(SandboxedSdkContext context, Bundle params, Executor executor,
38             InitSdkCallback callback) {
39         callback.onInitSdkFinished(null);
40     }
41 
42     @Override
getView(Context windowContext, Bundle params)43     public View getView(Context windowContext, Bundle params) {
44         handlePhase(params);
45         return null;
46     }
47 
48     @Override
onExtraDataReceived(Bundle extraData)49     public void onExtraDataReceived(Bundle extraData) {
50     }
51 
handlePhase(Bundle params)52     private void handlePhase(Bundle params) {
53         String phaseName = params.getString(BUNDLE_KEY_PHASE_NAME, "");
54         Log.i(TAG, "Handling phase: " + phaseName);
55         switch (phaseName) {
56             case "testSdkSandboxDataAppDirectory_SharedStorageIsUsable":
57                 testSdkSandboxDataAppDirectory_SharedStorageIsUsable();
58                 break;
59             case "testSdkDataIsAttributedToApp":
60                 testSdkDataIsAttributedToApp();
61                 break;
62             default:
63         }
64     }
65 
testSdkSandboxDataAppDirectory_SharedStorageIsUsable()66     private void testSdkSandboxDataAppDirectory_SharedStorageIsUsable() {
67         String sharedPath = getSharedStoragePath();
68 
69         try {
70             // Read the file
71             String input = Files.readAllLines(Paths.get(sharedPath, "readme.txt")).get(0);
72 
73             // Create a dir
74             Files.createDirectory(Paths.get(sharedPath, "dir"));
75             // Write to a file
76             Path filepath = Paths.get(sharedPath, "dir", "file");
77             Files.createFile(filepath);
78             Files.write(filepath, input.getBytes());
79         } catch (IOException e) {
80             Log.e(TAG, e.getMessage(), e);
81         }
82     }
83 
testSdkDataIsAttributedToApp()84     private void testSdkDataIsAttributedToApp() {
85         final byte[] buffer = new byte[1000000];
86         String sharedPath = getSharedStoragePath();
87         String sharedCachePath = getSharedStorageCachePath();
88         try {
89             Files.createDirectory(Paths.get(sharedPath, "attribution"));
90             Path filepath = Paths.get(sharedPath, "attribution", "file");
91             Files.createFile(filepath);
92             Files.write(filepath, buffer);
93 
94             Files.createDirectory(Paths.get(sharedCachePath, "attribution"));
95             Path cacheFilepath = Paths.get(sharedCachePath, "attribution", "file");
96             Files.createFile(cacheFilepath);
97             Files.write(cacheFilepath, buffer);
98         } catch (IOException e) {
99             Log.e(TAG, e.getMessage(), e);
100         }
101     }
102 
103     // TODO(209061627): this should be coming from code context instead
getSharedStoragePath()104     private String getSharedStoragePath() {
105         return "/data/misc_ce/0/sdksandbox/com.android.tests.sdksandbox/shared";
106     }
107 
108     // TODO(209061627): this should be coming from code context instead
getSharedStorageCachePath()109     private String getSharedStorageCachePath() {
110         return getSharedStoragePath() + "/cache";
111     }
112 }
113