Home | History | Annotate | Download | only in appaccessdata
      1 /*
      2  * Copyright (C) 2009 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.appaccessdata;
     18 
     19 import java.io.FileInputStream;
     20 import java.io.FileNotFoundException;
     21 import java.io.IOException;
     22 
     23 import android.test.AndroidTestCase;
     24 
     25 /**
     26  * Test that another app's private data cannot be accessed.
     27  *
     28  * Assumes that {@link APP_WITH_DATA_PKG} has already created the private data.
     29  */
     30 public class AccessPrivateDataTest extends AndroidTestCase {
     31 
     32     /**
     33      * The Android package name of the application that owns the private data
     34      */
     35     private static final String APP_WITH_DATA_PKG = "com.android.cts.appwithdata";
     36 
     37     /**
     38      * Name of private file to access. This must match the name of the file created by
     39      * {@link APP_WITH_DATA_PKG}.
     40      */
     41     private static final String PRIVATE_FILE_NAME = "private_file.txt";
     42 
     43     /**
     44      * Tests that another app's private file cannot be accessed
     45      * @throws IOException
     46      */
     47     public void testAccessPrivateData() throws IOException {
     48         try {
     49             // construct the absolute file path to the app's private file
     50             String privateFilePath = String.format("/data/data/%s/%s", APP_WITH_DATA_PKG,
     51                     PRIVATE_FILE_NAME);
     52             FileInputStream inputStream = new FileInputStream(privateFilePath);
     53             inputStream.read();
     54             inputStream.close();
     55             fail("Was able to access another app's private data");
     56         } catch (FileNotFoundException e) {
     57             // expected
     58         } catch (SecurityException e) {
     59             // also valid
     60         }
     61     }
     62 }
     63