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 package android.os.cts; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertTrue; 22 import static org.junit.Assert.assertNotNull; 23 import static org.junit.Assert.fail; 24 25 import android.content.Context; 26 import android.content.pm.ApplicationInfo; 27 import android.os.Environment; 28 import android.os.UserHandle; 29 import android.platform.test.annotations.AppModeFull; 30 import android.platform.test.annotations.AppModeSdkSandbox; 31 import android.platform.test.annotations.RequiresFlagsEnabled; 32 import android.platform.test.flag.junit.CheckFlagsRule; 33 import android.platform.test.flag.junit.DeviceFlagsValueProvider; 34 import android.system.Os; 35 import android.system.StructStatVfs; 36 37 import androidx.test.platform.app.InstrumentationRegistry; 38 import androidx.test.runner.AndroidJUnit4; 39 40 import com.android.compatibility.common.util.ApiTest; 41 42 import org.junit.Rule; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 46 import java.io.BufferedReader; 47 import java.io.File; 48 import java.io.FileReader; 49 import java.util.UUID; 50 import java.util.function.BiFunction; 51 import java.util.function.Function; 52 53 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).") 54 @RunWith(AndroidJUnit4.class) 55 public class EnvironmentTest { 56 57 private static final Context sContext = 58 InstrumentationRegistry.getInstrumentation().getContext(); 59 60 61 @Rule 62 public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule(); 63 64 @Test testEnvironment()65 public void testEnvironment() { 66 new Environment(); 67 assertNotNull(Environment.getExternalStorageState()); 68 assertTrue(Environment.getRootDirectory().isDirectory()); 69 assertTrue(Environment.getDownloadCacheDirectory().isDirectory()); 70 assertTrue(Environment.getDataDirectory().isDirectory()); 71 } 72 73 @AppModeFull(reason = "External directory not accessible by instant apps") 74 @Test testEnvironmentExternal()75 public void testEnvironmentExternal() { 76 assertTrue(Environment.getStorageDirectory().isDirectory()); 77 assertTrue(Environment.getExternalStorageDirectory().isDirectory()); 78 } 79 80 /** 81 * If TMPDIR points to a global shared directory, 82 * this could compromise the security of the files. 83 */ 84 @Test testNoTmpDir()85 public void testNoTmpDir() { 86 assertTrue(System.getenv("TMPDIR").endsWith("android.os.cts/cache")); 87 } 88 89 /** 90 * Verify that all writable block filesystems are mounted "noatime" to avoid 91 * unnecessary flash churn. 92 */ 93 @Test testNoAtime()94 public void testNoAtime() throws Exception { 95 try (BufferedReader br = new BufferedReader(new FileReader("/proc/mounts"))) { 96 String line; 97 while ((line = br.readLine()) != null) { 98 final String[] fields = line.split(" "); 99 final String source = fields[0]; 100 final String options = fields[3]; 101 102 if (source.startsWith("/dev/block/") && !options.startsWith("ro,") 103 && !options.contains("noatime")) { 104 fail("Found device mounted at " + source + " without 'noatime' option, " 105 + "which can cause unnecessary flash churn; please update your fstab."); 106 } 107 } 108 } 109 } 110 111 /** 112 * verify hidepid=2 on /proc 113 */ 114 @Test testHidePid2()115 public void testHidePid2() throws Exception { 116 try (BufferedReader br = new BufferedReader(new FileReader("/proc/mounts"))) { 117 String line; 118 while ((line = br.readLine()) != null) { 119 final String[] fields = line.split(" "); 120 final String source = fields[0]; 121 final String options = fields[3]; 122 123 if (source.equals("proc") && !options.contains("hidepid=2") 124 && !options.contains("hidepid=invisible")) { 125 fail("proc filesystem mounted without hidepid=2 or hidepid=invisible"); 126 } 127 } 128 } 129 } 130 131 @Test testHidePid2_direct()132 public void testHidePid2_direct() throws Exception { 133 assertFalse(new File("/proc/1").exists()); 134 } 135 136 /** 137 * Verify that all writable block filesystems are mounted with "resgid" to 138 * mitigate disk-full trouble. 139 */ 140 @Test testSaneInodes()141 public void testSaneInodes() throws Exception { 142 final File file = Environment.getDataDirectory(); 143 final StructStatVfs stat = Os.statvfs(file.getAbsolutePath()); 144 145 // By default ext4 creates one inode per 16KiB; we're okay with a much 146 // wider range, but we want to make sure the device isn't going totally 147 // crazy; too few inodes can result in system instability, and too many 148 // inodes can result in wasted space. 149 final long maxsize = stat.f_blocks * stat.f_frsize; 150 final long maxInodes = maxsize / 4096; 151 // Assuming the smallest storage would be 4GB, min # of free inodes 152 // in EXT4/F2FS must be larger than 128k for Android to work properly. 153 long minInodes = 128 * 1024; 154 final long size4GB = 4294967296l; 155 //If the storage size is smaller than 4GB, let's consider 32k for 1GB. 156 if (maxsize < size4GB) { 157 minInodes = 32 * 1024; 158 } 159 160 if (stat.f_ffree >= minInodes && stat.f_ffree <= maxInodes 161 && stat.f_favail <= stat.f_ffree) { 162 // Sweet, sounds great! 163 } else { 164 fail("Number of inodes " + stat.f_ffree + "/" + stat.f_favail 165 + " not within sane range for partition of " + maxsize + " bytes; expected [" 166 + minInodes + "," + maxInodes + "]"); 167 } 168 } 169 170 @Test 171 @ApiTest(apis = "android.os.Environment#getDataCePackageDirectoryForUser") testDataCePackageDirectoryForUser()172 public void testDataCePackageDirectoryForUser() { 173 testDataPackageDirectoryForUser( 174 (uuid, userHandle) -> Environment.getDataCePackageDirectoryForUser( 175 uuid, userHandle, sContext.getPackageName()), 176 (appInfo) -> appInfo.credentialProtectedDataDir 177 ); 178 } 179 180 @Test 181 @ApiTest(apis = "android.os.Environment#getDataDePackageDirectoryForUser") testDataDePackageDirectoryForUser()182 public void testDataDePackageDirectoryForUser() { 183 testDataPackageDirectoryForUser( 184 (uuid, userHandle) -> Environment.getDataDePackageDirectoryForUser( 185 uuid, userHandle, sContext.getPackageName()), 186 (appInfo) -> appInfo.deviceProtectedDataDir 187 ); 188 } 189 190 @Test 191 @ApiTest(apis = "android.os.Environment#getDataSystemDeviceProtectedDirectory") 192 @RequiresFlagsEnabled(android.crashrecovery.flags.Flags.FLAG_ENABLE_CRASHRECOVERY) testGetDataSystemDeviceProtectedDirectory()193 public void testGetDataSystemDeviceProtectedDirectory() { 194 assertTrue(Environment.getDataSystemDeviceProtectedDirectory().isDirectory()); 195 } 196 testDataPackageDirectoryForUser( BiFunction<UUID, UserHandle, File> publicApi, Function<ApplicationInfo, String> publicProperty)197 private void testDataPackageDirectoryForUser( 198 BiFunction<UUID, UserHandle, File> publicApi, 199 Function<ApplicationInfo, String> publicProperty) { 200 var appInfo = sContext.getApplicationInfo(); 201 // Check that public API is consistent with the public property 202 assertThat(publicApi.apply(appInfo.storageUuid, sContext.getUser()).getAbsolutePath()) 203 .isEqualTo(publicProperty.apply(appInfo)); 204 } 205 } 206