1 /*
2  * Copyright (C) 2015 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.writeexternalstorageapp;
18 
19 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.PACKAGE_NONE;
20 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.PACKAGE_WRITE;
21 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.assertDirNoAccess;
22 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.assertFileNoAccess;
23 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.assertFileReadWriteAccess;
24 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.getAllPackageSpecificNoGiftPaths;
25 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.getAllPackageSpecificObbGiftPaths;
26 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.getAllPackageSpecificPaths;
27 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.readInt;
28 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.writeInt;
29 
30 import android.os.Environment;
31 import android.test.AndroidTestCase;
32 
33 import java.io.File;
34 import java.util.List;
35 
36 public class WriteGiftTest extends AndroidTestCase {
testStageNonGifts()37     public void testStageNonGifts() throws Exception {
38         final List<File> writeList = getAllPackageSpecificNoGiftPaths(getContext(), PACKAGE_WRITE);
39         for (File write : writeList) {
40             write.getParentFile().mkdirs();
41             assertTrue(write.createNewFile());
42             writeInt(write, 102);
43         }
44     }
45 
46     /**
47      * Verify that we can't leave gifts in other package specific directories.
48      */
testNoGifts()49     public void testNoGifts() throws Exception {
50         final List<File> noneList = getAllPackageSpecificNoGiftPaths(getContext(), PACKAGE_NONE);
51         for (File none : noneList) {
52             assertFileNoAccess(none);
53         }
54 
55         final List<File> writeList = getAllPackageSpecificNoGiftPaths(getContext(), PACKAGE_WRITE);
56         for (File write : writeList) {
57             assertFileReadWriteAccess(write);
58             assertEquals(102, readInt(write));
59         }
60     }
61 
62     /**
63      * Leave gifts for other packages in their obb directories.
64      */
testObbGifts()65     public void testObbGifts() throws Exception {
66         final List<File> noneList = getAllPackageSpecificObbGiftPaths(getContext(), PACKAGE_NONE);
67         for (File none : noneList) {
68 
69             none.getParentFile().mkdirs();
70             none.createNewFile();
71             assertFileReadWriteAccess(none);
72 
73             writeInt(none, 100);
74             assertEquals(100, readInt(none));
75         }
76     }
77 
78     /**
79      * Verify we can't access gifts in obb dirs.
80      */
testAccessObbGifts()81     public void testAccessObbGifts() throws Exception {
82         final List<File> noneList = getAllPackageSpecificObbGiftPaths(getContext(), PACKAGE_NONE);
83         for (File none : noneList) {
84             assertFileReadWriteAccess(none);
85             assertEquals(100, readInt(none));
86         }
87     }
88 
89     /**
90      * Verify we can't access gifts in obb dirs.
91      */
testCantAccessObbGifts()92     public void testCantAccessObbGifts() throws Exception {
93         final List<File> noneList = getAllPackageSpecificObbGiftPaths(getContext(), PACKAGE_NONE);
94         for (File none : noneList) {
95             assertFileNoAccess(none);
96             assertDirNoAccess(none.getParentFile());
97         }
98     }
99 
testClearingWrite()100     public void testClearingWrite() throws Exception {
101         for (File dir : getAllPackageSpecificPaths(getContext())) {
102             dir.mkdirs();
103             new File(dir, "probe").createNewFile();
104             assertTrue(new File(dir, "probe").exists());
105         }
106     }
107 
testClearingRead()108     public void testClearingRead() throws Exception {
109         for (File dir : getAllPackageSpecificPaths(getContext())) {
110             assertFalse(new File(dir, "probe").exists());
111         }
112     }
113 
testIsExternalStorageLegacy()114     public void testIsExternalStorageLegacy() {
115         assertTrue(Environment.isExternalStorageLegacy());
116     }
117 
testNotIsExternalStorageLegacy()118     public void testNotIsExternalStorageLegacy() {
119         assertFalse(Environment.isExternalStorageLegacy());
120     }
121 }
122