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 static org.junit.Assert.fail;
20 
21 import android.os.Environment;
22 import android.os.storage.StorageManager;
23 
24 import org.junit.Assume;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 
28 import java.io.FileNotFoundException;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 
32 import androidx.test.runner.AndroidJUnit4;
33 
34 /**
35  * Test writing to SD card requires permissions
36  */
37 @RunWith(AndroidJUnit4.class)
38 public class NoSdCardWritePermissionTest {
39     @Test
testWriteExternalStorage()40     public void testWriteExternalStorage() throws FileNotFoundException, IOException {
41         Assume.assumeFalse(StorageManager.hasIsolatedStorage());
42 
43         try {
44             String fl = Environment.getExternalStorageDirectory().toString() +
45                          "/this-should-not-exist.txt";
46             FileOutputStream strm = new FileOutputStream(fl);
47             strm.write("Oops!".getBytes());
48             strm.flush();
49             strm.close();
50             fail("Was able to create and write to " + fl);
51         } catch (SecurityException e) {
52             // expected
53         } catch (FileNotFoundException e) {
54             // expected
55         }
56     }
57 }
58