1 /*
2  * Copyright (C) 2019 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.provider.cts.preconditions;
18 
19 import com.android.tradefed.build.IBuildInfo;
20 import com.android.tradefed.device.DeviceNotAvailableException;
21 import com.android.tradefed.device.ITestDevice;
22 import com.android.tradefed.targetprep.BuildError;
23 import com.android.tradefed.targetprep.ITargetCleaner;
24 import com.android.tradefed.targetprep.ITargetPreparer;
25 import com.android.tradefed.targetprep.TargetSetupError;
26 
27 /**
28  * Creates secondary external storage for use during a test suite.
29  */
30 public class ExternalStoragePreparer implements ITargetPreparer, ITargetCleaner {
31     private static final boolean ENABLED = false;
32 
33     @Override
setUp(ITestDevice device, IBuildInfo buildInfo)34     public void setUp(ITestDevice device, IBuildInfo buildInfo)
35             throws TargetSetupError, BuildError, DeviceNotAvailableException {
36         if (!ENABLED) return;
37         if (!hasIsolatedStorage(device)) return;
38 
39         device.executeShellCommand("sm set-virtual-disk false");
40         device.executeShellCommand("sm set-virtual-disk true");
41 
42         // Partition disks to make sure they're usable by tests
43         final String diskId = getVirtualDisk(device);
44         device.executeShellCommand("sm partition " + diskId + " public");
45     }
46 
47     @Override
tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable throwable)48     public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable throwable)
49             throws DeviceNotAvailableException {
50         if (!ENABLED) return;
51         if (!hasIsolatedStorage(device)) return;
52 
53         device.executeShellCommand("sm set-virtual-disk false");
54     }
55 
hasIsolatedStorage(ITestDevice device)56     private boolean hasIsolatedStorage(ITestDevice device) throws DeviceNotAvailableException {
57         return device.executeShellCommand("getprop sys.isolated_storage_snapshot")
58                 .contains("true");
59     }
60 
getVirtualDisk(ITestDevice device)61     private String getVirtualDisk(ITestDevice device) throws DeviceNotAvailableException {
62         int attempt = 0;
63         String disks = device.executeShellCommand("sm list-disks");
64         while ((disks == null || disks.isEmpty()) && attempt++ < 15) {
65             try {
66                 Thread.sleep(1000);
67             } catch (InterruptedException ignored) {
68             }
69             disks = device.executeShellCommand("sm list-disks");
70         }
71 
72         if (disks == null || disks.isEmpty()) {
73             throw new AssertionError("Device must support virtual disk to verify behavior");
74         }
75         return disks.split("\n")[0].trim();
76     }
77 }
78