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 android.permission.cts; 18 19 import java.io.FileNotFoundException; 20 import java.io.FileOutputStream; 21 import java.io.IOException; 22 23 import android.os.Environment; 24 import android.test.AndroidTestCase; 25 26 /** 27 * Test writing to SD card requires permissions 28 */ 29 public class NoSdCardWritePermissionTest extends AndroidTestCase { 30 31 /** 32 * Verify that writing to the external storage device requires {@link 33 * android.permission.WRITE_EXTERNAL_STORAGE}. 34 * @since 4 35 */ testWriteExternalStorage()36 public void testWriteExternalStorage() throws FileNotFoundException, IOException { 37 try { 38 String fl = Environment.getExternalStorageDirectory().toString() + 39 "/this-should-not-exist.txt"; 40 FileOutputStream strm = new FileOutputStream(fl); 41 strm.write("Oops!".getBytes()); 42 strm.flush(); 43 strm.close(); 44 fail("Was able to create and write to " + fl); 45 } catch (SecurityException e) { 46 // expected 47 } catch (FileNotFoundException e) { 48 // expected 49 } 50 } 51 } 52