Home | History | Annotate | Download | only in readexternalstorageapp
      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.readexternalstorageapp;
     18 
     19 import static android.test.MoreAsserts.assertNotEqual;
     20 
     21 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.PACKAGE_WRITE;
     22 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.assertFileReadOnlyAccess;
     23 import static com.android.cts.externalstorageapp.CommonExternalStorageTest.assertFileReadWriteAccess;
     24 
     25 import android.system.Os;
     26 
     27 import android.test.AndroidTestCase;
     28 
     29 import java.io.File;
     30 import java.util.List;
     31 
     32 public class ReadMultiViewTest extends AndroidTestCase {
     33     /**
     34      * Create a file in PACKAGE_READ's cache.
     35      */
     36     public void testFolderSetup() throws Exception {
     37         final File ourCache = getContext().getExternalCacheDir();
     38         final File ourTestDir = new File(ourCache, "testDir");
     39         final File ourFile = new File(ourTestDir, "test.probe");
     40 
     41         ourFile.getParentFile().mkdirs();
     42         assertTrue(ourFile.createNewFile());
     43     }
     44 
     45     /**
     46      * Verify that we have R/W access to test.probe in our cache.
     47      */
     48     public void testRWAccess() throws Exception {
     49         final File ourCache = getContext().getExternalCacheDir();
     50         final File ourTestDir = new File(ourCache, "testDir");
     51         final File testFile = new File(ourTestDir, "test.probe");
     52 
     53         assertFileReadWriteAccess(testFile);
     54         assertEquals(Os.getuid(), Os.stat(ourCache.getAbsolutePath()).st_uid);
     55         assertEquals(Os.getuid(), Os.stat(ourTestDir.getAbsolutePath()).st_uid);
     56         assertEquals(Os.getuid(), Os.stat(testFile.getAbsolutePath()).st_uid);
     57     }
     58 
     59     /**
     60      * Verify that we have RO access to test.probe in PACKAGE_WRITE's cache.
     61      */
     62     public void testROAccess() throws Exception {
     63         final File ourCache = getContext().getExternalCacheDir();
     64         final File otherCache = new File(ourCache.getAbsolutePath()
     65                 .replace(getContext().getPackageName(), PACKAGE_WRITE));
     66         final File otherTestDir = new File(otherCache, "testDir");
     67         final File testFile = new File(otherTestDir, "test.probe");
     68 
     69         assertFileReadOnlyAccess(testFile);
     70         assertNotEqual(Os.getuid(), Os.stat(testFile.getAbsolutePath()).st_uid);
     71         assertNotEqual(Os.getuid(), Os.stat(otherCache.getAbsolutePath()).st_uid);
     72         assertNotEqual(Os.getuid(), Os.stat(otherTestDir.getAbsolutePath()).st_uid);
     73     }
     74 }
     75